Search in sources :

Example 1 with SparseFloatVector

use of com.tencent.angel.ml.math.vector.SparseFloatVector in project angel by Tencent.

the class DenseFloatMatrixTest method plusBySparseFloatVectorTest.

@Test
public void plusBySparseFloatVectorTest() throws Exception {
    float[][] value = { { 1.0f, 2.0f }, { 3.0f, 4.0f } };
    DenseFloatMatrix mat = new DenseFloatMatrix(2, 2, value);
    TVector vec = new SparseFloatVector(2);
    ((SparseFloatVector) vec).set(1, 1.0f);
    vec.setRowId(0);
    mat.plusBy(vec);
    assertEquals(1.0f, mat.get(0, 0));
    assertEquals(3.0f, mat.get(0, 1));
    assertEquals(3.0f, mat.get(1, 0));
    assertEquals(4.0f, mat.get(1, 1));
}
Also used : TVector(com.tencent.angel.ml.math.TVector) SparseFloatVector(com.tencent.angel.ml.math.vector.SparseFloatVector) Test(org.junit.Test)

Example 2 with SparseFloatVector

use of com.tencent.angel.ml.math.vector.SparseFloatVector in project angel by Tencent.

the class SparseFloatMatrixTest method testPlusByGet.

@Test
public void testPlusByGet() {
    SparseFloatMatrix matrix = new SparseFloatMatrix(2, 2);
    matrix.plusBy(0, 0, 1.0f);
    matrix.plusBy(1, 1, 1.0f);
    assertEquals(matrix.get(0, 0), 1.0f);
    assertEquals(matrix.get(0, 1), 0.0f);
    assertEquals(matrix.get(1, 0), 0.0f);
    assertEquals(matrix.get(1, 1), 1.0f);
    matrix.clear();
    SparseFloatVector incVec = new SparseFloatVector(2);
    incVec.set(0, 1);
    incVec.set(1, 1);
    incVec.setRowId(0);
    matrix.plusBy(incVec);
    assertEquals(matrix.get(0, 0), 1.0f);
    assertEquals(matrix.get(0, 1), 1.0f);
    assertEquals(matrix.get(1, 0), 0.0f);
    assertEquals(matrix.get(1, 1), 0.0f);
    matrix.clear();
    int[] rowIndexes = { 0, 1 };
    int[] colIndexes = { 0, 1 };
    float[] values = { 1.0f, 1.0f };
    matrix.plusBy(rowIndexes, colIndexes, values);
    assertEquals(matrix.get(0, 0), 1.0f);
    assertEquals(matrix.get(0, 1), 0.0f);
    assertEquals(matrix.get(1, 0), 0.0f);
    assertEquals(matrix.get(1, 1), 1.0f);
    matrix.clear();
    colIndexes[0] = 0;
    colIndexes[1] = 1;
    values[0] = 1.0f;
    values[1] = 1.0f;
    matrix.plusBy(0, colIndexes, values);
    assertEquals(matrix.get(0, 0), 1.0f);
    assertEquals(matrix.get(0, 1), 1.0f);
    assertEquals(matrix.get(1, 0), 0.0f);
    assertEquals(matrix.get(1, 1), 0.0f);
    SparseFloatMatrix matrix1 = new SparseFloatMatrix(2, 2);
    matrix.clear();
    matrix.plusBy(0, 0, 1.0f);
    matrix.plusBy(1, 1, 1.0f);
    matrix1.plusBy(0, 0, 1.0f);
    matrix1.plusBy(1, 1, 1.0f);
    matrix.plusBy(matrix1);
    assertEquals(matrix.get(0, 0), 2.0f);
    assertEquals(matrix.get(0, 1), 0.0f);
    assertEquals(matrix.get(1, 0), 0.0f);
    assertEquals(matrix.get(1, 1), 2.0f);
    assertEquals(((SparseFloatVector) matrix.getRow(0)).get(0), 2.0f);
    assertEquals(((SparseFloatVector) matrix.getRow(0)).get(1), 0.0f);
    assertEquals(((SparseFloatVector) matrix.getRow(1)).get(0), 0.0f);
    assertEquals(((SparseFloatVector) matrix.getRow(1)).get(1), 2.0f);
}
Also used : SparseFloatVector(com.tencent.angel.ml.math.vector.SparseFloatVector) Test(org.junit.Test)

Example 3 with SparseFloatVector

use of com.tencent.angel.ml.math.vector.SparseFloatVector in project angel by Tencent.

the class SparseFloatMatrix method initVector.

@Override
public SparseFloatVector initVector(int rowIndex) {
    SparseFloatVector ret = new SparseFloatVector((int) col);
    ret.setMatrixId(matrixId);
    ret.setRowId(rowIndex);
    return ret;
}
Also used : SparseFloatVector(com.tencent.angel.ml.math.vector.SparseFloatVector)

Aggregations

SparseFloatVector (com.tencent.angel.ml.math.vector.SparseFloatVector)3 Test (org.junit.Test)2 TVector (com.tencent.angel.ml.math.TVector)1