Search in sources :

Example 21 with BlasDoubleMatrix

use of com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat, IntDoubleVector v, boolean onCol, Binary op) {
    double[] 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()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                double value = values[i];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                double value = entry.getDoubleValue();
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                double value = values[k];
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int i = 0; i < m; i++) {
                        if (flag[i] == 0) {
                            for (int j = 0; j < n; j++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else if (onCol && !op.isInplace()) {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                double value = values[i];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                double value = entry.getDoubleValue();
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int i = idxs[k];
                flag[i] = 1;
                double value = values[k];
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        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) {
                            for (int j = 0; j < n; j++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    data[i * n + j] = op.apply(data[i * n + j], values[j]);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                double value = entry.getDoubleValue();
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                double value = values[k];
                flag[j] = 1;
                for (int i = 0; i < m; i++) {
                    data[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        if (!v.isDense()) {
            switch(op.getOpType()) {
                case INTERSECTION:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = 0;
                            }
                        }
                    }
                case UNION:
                    break;
                case ALL:
                    for (int j = 0; j < n; j++) {
                        if (flag[j] == 0) {
                            for (int i = 0; i < m; i++) {
                                data[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return mat;
    } else {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] values = v.getStorage().getValues();
            for (int j = 0; j < n; j++) {
                double value = values[j];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else if (v.isSparse()) {
            ObjectIterator<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                double value = entry.getDoubleValue();
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] values = v.getStorage().getValues();
            for (int k = 0; k < size; k++) {
                int j = idxs[k];
                flag[j] = 1;
                double value = values[k];
                for (int i = 0; i < m; i++) {
                    newData[i * n + j] = op.apply(data[i * n + j], value);
                }
            }
        }
        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) {
                            for (int i = 0; i < m; i++) {
                                newData[i * n + j] = op.apply(data[i * n + j], 0);
                            }
                        }
                    }
            }
        }
        return new BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Example 22 with BlasDoubleMatrix

use of com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat1, boolean trans1, BlasFloatMatrix mat2, boolean trans2, Binary op) {
    double[] mat1Data = mat1.getData();
    float[] mat2Data = mat2.getData();
    int m = mat1.getNumRows(), n = mat1.getNumCols();
    int p = mat2.getNumRows(), q = mat2.getNumCols();
    int size = m * n;
    if (trans1 && trans2) {
        // TT
        double[] newData = new double[size];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newData[i * m + j] = op.apply(mat1Data[j * n + i], mat2Data[j * q + i]);
            }
        }
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), n, m, newData);
    } else if (!trans1 && trans2) {
        // _T
        if (op.isInplace()) {
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    mat1Data[i * n + j] = op.apply(mat1Data[i * n + j], mat2Data[j * q + i]);
                }
            }
            return mat1;
        } else {
            double[] newData = new double[size];
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    newData[i * n + j] = op.apply(mat1Data[i * n + j], mat2Data[j * q + i]);
                }
            }
            return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
        }
    } else if (trans1 && !trans2) {
        // T_
        double[] newData = new double[size];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                newData[i * m + j] = op.apply(mat1Data[j * n + i], mat2Data[i * q + j]);
            }
        }
        return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
    } else {
        if (op.isInplace()) {
            for (int i = 0; i < size; i++) {
                mat1Data[i] = op.apply(mat1Data[i], mat2Data[i]);
            }
            return mat1;
        } else {
            double[] newData = new double[size];
            for (int i = 0; i < size; i++) {
                newData[i] = op.apply(mat1Data[i], mat2Data[i]);
            }
            return new BlasDoubleMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
        }
    }
}
Also used : BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)

Example 23 with BlasDoubleMatrix

use of com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix in project angel by Tencent.

the class BinaryMatrixExecutor method apply.

private static Matrix apply(BlasDoubleMatrix mat, IntDoubleVector v, int idx, boolean onCol, Binary op) {
    double[] 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()) {
            double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                data[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] 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()) {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int i = entry.getIntKey();
                flag[i] = 1;
                newData[i * n + idx] = op.apply(data[i * n + idx], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] 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 BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    } else if (!onCol && op.isInplace()) {
        if (v.isDense()) {
            double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                data[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] 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 {
        double[] newData;
        if (op.getOpType() == INTERSECTION) {
            newData = new double[m * n];
        } else {
            newData = ArrayCopy.copy(data);
        }
        if (v.isDense()) {
            double[] 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<Int2DoubleMap.Entry> iter = v.getStorage().entryIterator();
            while (iter.hasNext()) {
                Int2DoubleMap.Entry entry = iter.next();
                int j = entry.getIntKey();
                flag[j] = 1;
                newData[idx * n + j] = op.apply(data[idx * n + j], entry.getDoubleValue());
            }
        } else {
            // sorted
            int[] idxs = v.getStorage().getIndices();
            double[] 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 BlasDoubleMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
    }
}
Also used : Int2DoubleMap(it.unimi.dsi.fastutil.ints.Int2DoubleMap) BlasDoubleMatrix(com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix) ObjectIterator(it.unimi.dsi.fastutil.objects.ObjectIterator)

Aggregations

BlasDoubleMatrix (com.tencent.angel.ml.math2.matrix.BlasDoubleMatrix)15 ObjectIterator (it.unimi.dsi.fastutil.objects.ObjectIterator)12 IntDoubleVector (com.tencent.angel.ml.math2.vector.IntDoubleVector)9 IntDoubleDenseVectorStorage (com.tencent.angel.ml.math2.storage.IntDoubleDenseVectorStorage)5 AngelException (com.tencent.angel.exception.AngelException)3 IntDummyVector (com.tencent.angel.ml.math2.vector.IntDummyVector)3 IntFloatVector (com.tencent.angel.ml.math2.vector.IntFloatVector)3 IntIntVector (com.tencent.angel.ml.math2.vector.IntIntVector)3 IntLongVector (com.tencent.angel.ml.math2.vector.IntLongVector)3 LongDoubleVector (com.tencent.angel.ml.math2.vector.LongDoubleVector)3 LongFloatVector (com.tencent.angel.ml.math2.vector.LongFloatVector)3 Vector (com.tencent.angel.ml.math2.vector.Vector)3 Int2DoubleMap (it.unimi.dsi.fastutil.ints.Int2DoubleMap)3 Int2FloatMap (it.unimi.dsi.fastutil.ints.Int2FloatMap)3 Int2IntMap (it.unimi.dsi.fastutil.ints.Int2IntMap)3 Int2LongMap (it.unimi.dsi.fastutil.ints.Int2LongMap)3 MatrixExecutors (com.tencent.angel.ml.math2.MatrixExecutors)1 BlasFloatMatrix (com.tencent.angel.ml.math2.matrix.BlasFloatMatrix)1 RBIntDoubleMatrix (com.tencent.angel.ml.math2.matrix.RBIntDoubleMatrix)1 RBIntFloatMatrix (com.tencent.angel.ml.math2.matrix.RBIntFloatMatrix)1