Search in sources :

Example 91 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasFloatMatrix mat, IntFloatVector v, int idx, boolean onCol, Binary op) {
    float[] data = mat.getData();
    int m = mat.getNumRows(), n = mat.getNumCols();
    int size = v.size();
    byte[] flag = null;
    if (!v.isDense()) {
        flag = new byte[v.getDim()];
    }
    if (onCol && op.isInplace()) {
        if (v.isDense()) {
            float[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                data[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], entry.getFloatValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            float[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            data[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            float[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                newData[i * n + idx] = op.apply(data[i * n + idx], values[i]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], entry.getFloatValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            float[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            newData[i * n + idx] = op.apply(data[i * n + idx], 0);
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            float[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                data[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], entry.getFloatValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            float[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = 0;
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            data[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return mat;
    } else {
        float[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new float[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            float[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                newData[idx * n + j] = op.apply(data[idx * n + j], values[j]);
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2FloatMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2FloatMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], entry.getFloatValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            float[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], values[k]);
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    break;
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            newData[idx * n + j] = op.apply(data[idx * n + j], 0);
                        }
                    }
            }
        }
        return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2FloatMap(it.unimi.dsi.fastutil.ints.Int2FloatMap) BlasFloatMatrix(com.tencent.angel.ml.math2.matrix.BlasFloatMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 92 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.

the class Node method deepClone.

@Override
public Node deepClone() {
    if (feats != null && neighbors != null) {
        IntFloatVector cloneFeats = feats.clone();
        long[] cloneNeighbors = new long[neighbors.length];
        System.arraycopy(neighbors, 0, cloneNeighbors, 0, neighbors.length);
        if (types == null)
            return new Node(cloneFeats, cloneNeighbors);
        else {
            int[] cloneTypes = new int[types.length];
            System.arraycopy(types, 0, cloneTypes, 0, types.length);
            return new Node(cloneFeats, cloneNeighbors, cloneTypes);
        }
    } else {
        return new Node();
    }
}
Also used : IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 93 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.

the class RBIntFloatMatrix method diag.

@Override
public Vector diag() {
    float[] resArr = new float[rows.length];
    for (int i = 0; i < rows.length; i++) {
        if (null == rows[i]) {
            resArr[i] = 0;
        } else {
            resArr[i] = rows[i].get(i);
        }
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(getMatrixId(), 0, getClock(), resArr.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 94 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.

the class RBIntFloatMatrix method initEmpty.

@Override
public void initEmpty(int idx) {
    if (null == rows[idx]) {
        IntFloatSparseVectorStorage storage = new IntFloatSparseVectorStorage((int) getDim());
        rows[idx] = new IntFloatVector(matrixId, idx, clock, (int) getDim(), storage);
    }
}
Also used : IntFloatSparseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Example 95 with IntFloatVector

use of com.tencent.angel.ml.math2.vector.IntFloatVector in project angel by Tencent.

the class RBIntFloatMatrix method dot.

@Override
public Vector dot(Vector other) {
    float[] resArr = new float[rows.length];
    for (int i = 0; i < rows.length; i++) {
        resArr[i] = (float) rows[i].dot(other);
    }
    IntFloatDenseVectorStorage storage = new IntFloatDenseVectorStorage(resArr);
    return new IntFloatVector(matrixId, 0, clock, rows.length, storage);
}
Also used : IntFloatDenseVectorStorage(com.tencent.angel.ml.math2.storage.IntFloatDenseVectorStorage) IntFloatVector(com.tencent.angel.ml.math2.vector.IntFloatVector)

Aggregations

IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)104 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)60 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)57 IntFloatVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatVectorStorage)50 CompIntFloatVector (com.tencent.angel.ml.math2.vector.CompIntFloatVector)34 IntDoubleVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage)33 IntIntVectorStorage (com.tencent.angel.ml.math2.storage.IntIntVectorStorage)32 IntLongVectorStorage (com.tencent.angel.ml.math2.storage.IntLongVectorStorage)32 IntFloatSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSparseVectorStorage)31 LongDoubleVectorStorage (com.tencent.angel.ml.math2.storage.LongDoubleVectorStorage)30 LongFloatVectorStorage (com.tencent.angel.ml.math2.storage.LongFloatVectorStorage)30 LongIntVectorStorage (com.tencent.angel.ml.math2.storage.LongIntVectorStorage)30 LongLongVectorStorage (com.tencent.angel.ml.math2.storage.LongLongVectorStorage)30 Storage (com.tencent.angel.ml.math2.storage.Storage)30 IntFloatSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntFloatSortedVectorStorage)25 IntDoubleSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSortedVectorStorage)21 AngelException (com.tencent.angel.exception.AngelException)20 IntDoubleSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleSparseVectorStorage)20 IntIntSortedVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSortedVectorStorage)20 IntIntSparseVectorStorage (com.tencent.angel.ml.math2.storage.IntIntSparseVectorStorage)20