use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat, IntLongVector 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()) {
long[] 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
data[i * n + idx] = op.apply(data[i * n + idx], entry.getLongValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
long[] 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()) {
long[] 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
newData[i * n + idx] = op.apply(data[i * n + idx], entry.getLongValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
long[] 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()) {
long[] 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
data[idx * n + j] = op.apply(data[idx * n + j], entry.getLongValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
long[] 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()) {
long[] 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
newData[idx * n + j] = op.apply(data[idx * n + j], entry.getLongValue());
}
} else {
// sorted
int[] idxs = v.getStorage().getIndices();
long[] 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);
}
}
use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat, IntDummyVector v, boolean onCol, Binary op) {
float[] data = mat.getData();
int m = mat.getNumRows(), n = mat.getNumCols();
int size = v.size();
byte[] flag = new byte[v.getDim()];
if (onCol && op.isInplace()) {
int[] idxs = v.getIndices();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
for (int j = 0; j < n; j++) {
data[i * n + j] = op.apply(data[i * n + j], 1);
}
}
if (op.getOpType() == INTERSECTION) {
for (int i = 0; i < m; i++) {
if (flag[i] == 0) {
for (int j = 0; j < n; j++) {
data[i * n + j] = 0;
}
}
}
} else if (op.getOpType() == 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()) {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
int[] idxs = v.getIndices();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
for (int j = 0; j < n; j++) {
newData[i * n + j] = op.apply(data[i * n + j], 1);
}
}
if (op.getOpType() == 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(newData[i * n + j], 0);
}
}
}
}
return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
} else if (!onCol && op.isInplace()) {
int[] idxs = v.getIndices();
for (int i = 0; i < n; i++) {
for (int k = 0; k < size; k++) {
int j = idxs[k];
flag[j] = 1;
data[i * n + j] = op.apply(data[i * n + j], 1);
}
}
if (op.getOpType() == INTERSECTION) {
for (int j = 0; j < n; j++) {
if (flag[j] == 0) {
for (int i = 0; i < m; i++) {
data[i * n + j] = 0;
}
}
}
} else if (op.getOpType() == 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 {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
int[] idxs = v.getIndices();
for (int i = 0; i < n; i++) {
for (int k = 0; k < size; k++) {
int j = idxs[k];
flag[j] = 1;
newData[i * n + j] = op.apply(data[i * n + j], 1);
}
}
if (op.getOpType() == 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(newData[i * n + j], 0);
}
}
}
}
return new BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
}
}
use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat1, boolean trans1, BlasFloatMatrix mat2, boolean trans2, Binary op) {
float[] 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
float[] newData = new float[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 BlasFloatMatrix(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 {
float[] newData = new float[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 BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
}
} else if (trans1 && !trans2) {
// T_
float[] newData = new float[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 BlasFloatMatrix(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 {
float[] newData = new float[size];
for (int i = 0; i < size; i++) {
newData[i] = op.apply(mat1Data[i], mat2Data[i]);
}
return new BlasFloatMatrix(mat1.getMatrixId(), mat1.getClock(), m, n, newData);
}
}
}
use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat, IntLongVector v, 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()) {
long[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
long 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getLongValue();
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();
long[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
long 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()) {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
long[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
long 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getLongValue();
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();
long[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
long 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 BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
} else if (!onCol && op.isInplace()) {
if (v.isDense()) {
long[] 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
float value = entry.getLongValue();
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();
long[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
long 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 {
float[] newData;
if (op.getOpType() == INTERSECTION) {
newData = new float[m * n];
} else {
newData = ArrayCopy.copy(data);
}
if (v.isDense()) {
long[] values = v.getStorage().getValues();
for (int j = 0; j < n; j++) {
long 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<Int2LongMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
float value = entry.getLongValue();
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();
long[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
flag[j] = 1;
long 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 BlasFloatMatrix(mat.getMatrixId(), mat.getClock(), m, n, newData);
}
}
use of com.tencent.angel.ml.math2.matrix.BlasFloatMatrix in project angel by Tencent.
the class DotMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat1, boolean trans1, RBIntFloatMatrix mat2, boolean trans2) {
if (trans1 && !trans2) {
int outputRows = mat1.getNumCols();
IntFloatVector[] rows = new IntFloatVector[outputRows];
for (int i = 0; i < outputRows; i++) {
Vector col = mat1.getCol(i);
rows[i] = (IntFloatVector) mat2.transDot(col);
}
return MFactory.rbIntFloatMatrix(rows);
} else if (!trans1 && !trans2) {
int outputRows = mat1.getNumRows();
IntFloatVector[] rows = new IntFloatVector[outputRows];
for (int i = 0; i < outputRows; i++) {
Vector row = mat1.getRow(i);
rows[i] = (IntFloatVector) mat2.transDot(row);
}
return MFactory.rbIntFloatMatrix(rows);
} else {
throw new AngelException("the operation is not supported!");
}
}
Aggregations