use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class CompReduceExecutor method apply.
private static UnionEle apply(CompIntIntVector v, ReduceOP op, int start, int end) {
UnionEle res = new UnionEle();
IntIntVector[] parts = v.getPartitions();
switch(op) {
case Sum:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
}
break;
case Avg:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
res.setLong1(res.getLong1() + parts[i].getDim());
}
break;
case Std:
for (int i = start; i <= end; i++) {
res.setDouble1(res.getDouble1() + parts[i].sum());
double norm = parts[i].norm();
res.setDouble2(res.getDouble2() + norm * norm);
res.setLong1(res.getLong1() + parts[i].getDim());
}
break;
case Norm:
for (int i = start; i <= end; i++) {
double norm = parts[i].norm();
res.setDouble2(res.getDouble2() + norm * norm);
}
break;
case Min:
res.setDouble1(Double.MAX_VALUE);
for (int i = start; i <= end; i++) {
res.setDouble1(Math.min(res.getDouble1(), parts[i].min()));
}
break;
case Max:
res.setDouble1(Double.MIN_VALUE);
for (int i = start; i <= end; i++) {
res.setDouble1(Math.max(res.getDouble1(), parts[i].max()));
}
break;
case Size:
for (int i = start; i <= end; i++) {
res.setLong1(res.getLong1() + parts[i].size());
}
break;
case Numzeros:
for (int i = start; i <= end; i++) {
res.setLong1(res.getLong1() + parts[i].numZeros());
}
break;
}
return res;
}
use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasFloatMatrix mat, IntIntVector 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()) {
int[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
int 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getIntValue();
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();
int[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
int 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()) {
int[] values = v.getStorage().getValues();
for (int i = 0; i < m; i++) {
int 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
float value = entry.getIntValue();
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();
int[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int i = idxs[k];
flag[i] = 1;
int 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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
float value = entry.getIntValue();
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();
int[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
int 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()) {
int[] values = v.getStorage().getValues();
for (int j = 0; j < n; j++) {
int 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
float value = entry.getIntValue();
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();
int[] values = v.getStorage().getValues();
for (int k = 0; k < size; k++) {
int j = idxs[k];
flag[j] = 1;
int 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.vector.IntIntVector in project angel by Tencent.
the class RBCompLongIntMatrix method diag.
@Override
public Vector diag() {
int[] resArr = new int[rows.length];
for (int i = 0; i < rows.length; i++) {
if (null == rows[i]) {
resArr[i] = 0;
} else {
resArr[i] = rows[i].get(i);
}
}
IntIntDenseVectorStorage storage = new IntIntDenseVectorStorage(resArr);
return new IntIntVector(getMatrixId(), 0, getClock(), resArr.length, storage);
}
use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class BlasFloatMatrix method setCol.
public Matrix setCol(int i, Vector v) {
if (v instanceof IntFloatVector) {
float[] rowData;
if (v.isDense()) {
rowData = ((IntFloatVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new float[numRows];
ObjectIterator<Int2FloatMap.Entry> iter = ((IntFloatVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2FloatMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getFloatValue();
}
} else {
// sorted
rowData = new float[numRows];
int[] idxs = ((IntFloatVector) v).getStorage().getIndices();
float[] values = ((IntFloatVector) v).getStorage().getValues();
int size = ((IntFloatVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
for (int j = 0; j < numRows; j++) {
data[j * numCols + i] = rowData[j];
}
} else if (v instanceof IntLongVector) {
long[] rowData;
if (v.isDense()) {
rowData = ((IntLongVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new long[numRows];
ObjectIterator<Int2LongMap.Entry> iter = ((IntLongVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getLongValue();
}
} else {
// sorted
rowData = new long[numRows];
int[] idxs = ((IntLongVector) v).getStorage().getIndices();
long[] values = ((IntLongVector) v).getStorage().getValues();
int size = ((IntLongVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
for (int j = 0; j < numRows; j++) {
data[j * numCols + i] = rowData[j];
}
} else if (v instanceof IntIntVector) {
int[] rowData;
if (v.isDense()) {
rowData = ((IntIntVector) v).getStorage().getValues();
} else if (v.isSparse()) {
rowData = new int[numRows];
ObjectIterator<Int2IntMap.Entry> iter = ((IntIntVector) v).getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
rowData[j] = entry.getIntValue();
}
} else {
// sorted
rowData = new int[numRows];
int[] idxs = ((IntIntVector) v).getStorage().getIndices();
int[] values = ((IntIntVector) v).getStorage().getValues();
int size = ((IntIntVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = values[k];
}
}
for (int j = 0; j < numRows; j++) {
data[j * numCols + i] = rowData[j];
}
} else if (v instanceof IntDummyVector) {
int[] rowData = new int[numRows];
int[] idxs = ((IntDummyVector) v).getIndices();
int size = ((IntDummyVector) v).size();
for (int k = 0; k < size; k++) {
int j = idxs[k];
rowData[j] = 1;
}
for (int j = 0; j < numRows; j++) {
data[j * numCols + i] = rowData[j];
}
} else {
throw new AngelException("The operation is not supported!");
}
return this;
}
use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class RBIntIntMatrix method initEmpty.
@Override
public void initEmpty(int idx) {
if (null == rows[idx]) {
IntIntSparseVectorStorage storage = new IntIntSparseVectorStorage((int) getDim());
rows[idx] = new IntIntVector(matrixId, idx, clock, (int) getDim(), storage);
}
}
Aggregations