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);
}
}
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);
}
}
}
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);
}
}
Aggregations