use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class MixedBinaryInZAExecutor method apply.
private static Vector apply(CompIntDoubleVector v1, IntIntVector v2, Binary op) {
IntDoubleVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int base = 0;
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < parts.length; i++) {
IntDoubleVector part = parts[i];
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
if (part.isDense()) {
double[] resPartValues = resPart.getValues();
double[] partValues = part.getStorage().getValues();
for (int j = 0; j < partValues.length; j++) {
resPartValues[j] = op.apply(partValues[j], v2Values[base + j]);
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getDoubleValue(), v2Values[idx + base]));
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2Values[idx + base]);
}
} else {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPart.set(idx, op.apply(partValues[j], v2Values[idx + base]));
}
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntDoubleVector part = parts[i];
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2DoubleMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
} else {
// sorted
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntDoubleVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntDoubleVector part = parts[i];
IntDoubleVectorStorage resPart = (IntDoubleVectorStorage) resParts[i];
if (part.isDense()) {
double[] partValues = part.getStorage().getValues();
double[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2DoubleMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getDoubleValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
double[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
double[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
}
IntDoubleVector[] res = new IntDoubleVector[parts.length];
int i = 0;
for (IntDoubleVector part : parts) {
res[i] = new IntDoubleVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntDoubleVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class MixedBinaryInZAExecutor method apply.
private static Vector apply(CompIntIntVector v1, IntIntVector v2, Binary op) {
IntIntVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int base = 0;
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < parts.length; i++) {
IntIntVector part = parts[i];
IntIntVectorStorage resPart = (IntIntVectorStorage) resParts[i];
if (part.isDense()) {
int[] resPartValues = resPart.getValues();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partValues.length; j++) {
resPartValues[j] = op.apply(partValues[j], v2Values[base + j]);
}
} else if (part.isSparse()) {
ObjectIterator<Int2IntMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getIntValue(), v2Values[idx + base]));
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
int[] resPartValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2Values[idx + base]);
}
} else {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPart.set(idx, op.apply(partValues[j], v2Values[idx + base]));
}
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntIntVector part = parts[i];
IntIntVectorStorage resPart = (IntIntVectorStorage) resParts[i];
if (part.isDense()) {
int[] partValues = part.getStorage().getValues();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2IntMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2IntMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getIntValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
} else {
// sorted
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntIntVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntIntVector part = parts[i];
IntIntVectorStorage resPart = (IntIntVectorStorage) resParts[i];
if (part.isDense()) {
int[] partValues = part.getStorage().getValues();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2IntMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2IntMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getIntValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
int[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
int[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
}
IntIntVector[] res = new IntIntVector[parts.length];
int i = 0;
for (IntIntVector part : parts) {
res[i] = new IntIntVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntIntVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
use of com.tencent.angel.ml.math2.vector.IntIntVector in project angel by Tencent.
the class MixedBinaryInZAExecutor method apply.
private static Vector apply(CompIntLongVector v1, IntIntVector v2, Binary op) {
IntLongVector[] parts = v1.getPartitions();
Storage[] resParts = StorageSwitch.applyComp(v1, v2, op);
if (v2.isDense()) {
int base = 0;
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < parts.length; i++) {
IntLongVector part = parts[i];
IntLongVectorStorage resPart = (IntLongVectorStorage) resParts[i];
if (part.isDense()) {
long[] resPartValues = resPart.getValues();
long[] partValues = part.getStorage().getValues();
for (int j = 0; j < partValues.length; j++) {
resPartValues[j] = op.apply(partValues[j], v2Values[base + j]);
}
} else if (part.isSparse()) {
ObjectIterator<Int2LongMap.Entry> iter = part.getStorage().entryIterator();
while (iter.hasNext()) {
Int2LongMap.Entry entry = iter.next();
int idx = entry.getIntKey();
resPart.set(idx, op.apply(entry.getLongValue(), v2Values[idx + base]));
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] resPartIndices = resPart.getIndices();
long[] resPartValues = resPart.getValues();
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2Values[idx + base]);
}
} else {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
resPart.set(idx, op.apply(partValues[j], v2Values[idx + base]));
}
}
}
base += part.getDim();
}
} else if (v2.isSparse()) {
ObjectIterator<Int2IntMap.Entry> iter = v2.getStorage().entryIterator();
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int idx = entry.getIntKey();
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), entry.getIntValue()));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntLongVector part = parts[i];
IntLongVectorStorage resPart = (IntLongVectorStorage) resParts[i];
if (part.isDense()) {
long[] partValues = part.getStorage().getValues();
long[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2LongMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2LongMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getLongValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
long[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
} else {
// sorted
if (v1.size() > v2.size()) {
int subDim = (v1.getDim() + v1.getNumPartitions() - 1) / v1.getNumPartitions();
int[] v2Indices = v2.getStorage().getIndices();
int[] v2Values = v2.getStorage().getValues();
for (int i = 0; i < v2Indices.length; i++) {
int idx = v2Indices[i];
int pidx = (int) (idx / subDim);
int subidx = idx % subDim;
if (parts[pidx].hasKey(subidx)) {
((IntLongVectorStorage) resParts[pidx]).set(subidx, op.apply(parts[pidx].get(subidx), v2Values[i]));
}
}
} else {
int base = 0;
for (int i = 0; i < parts.length; i++) {
IntLongVector part = parts[i];
IntLongVectorStorage resPart = (IntLongVectorStorage) resParts[i];
if (part.isDense()) {
long[] partValues = part.getStorage().getValues();
long[] resPartValues = resPart.getValues();
for (int j = 0; j < partValues.length; j++) {
if (v2.hasKey(j + base)) {
resPartValues[j] = op.apply(partValues[j], v2.get(j + base));
}
}
} else if (part.isSparse()) {
ObjectIterator<Int2LongMap.Entry> piter = part.getStorage().entryIterator();
while (piter.hasNext()) {
Int2LongMap.Entry entry = piter.next();
int idx = entry.getIntKey();
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(entry.getLongValue(), v2.get(idx + base)));
}
}
} else {
// sorted
if (op.isKeepStorage()) {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
int[] resPartIndices = resPart.getIndices();
long[] resPartValues = resPart.getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPartIndices[j] = idx;
resPartValues[j] = op.apply(partValues[j], v2.get(idx + base));
}
}
} else {
int[] partIndices = part.getStorage().getIndices();
long[] partValues = part.getStorage().getValues();
for (int j = 0; j < partIndices.length; j++) {
int idx = partIndices[j];
if (v2.hasKey(idx + base)) {
resPart.set(idx, op.apply(partValues[j], v2.get(idx + base)));
}
}
}
}
base += part.getDim();
}
}
}
IntLongVector[] res = new IntLongVector[parts.length];
int i = 0;
for (IntLongVector part : parts) {
res[i] = new IntLongVector(part.getMatrixId(), part.getRowId(), part.getClock(), part.getDim(), (IntLongVectorStorage) resParts[i]);
i++;
}
v1.setPartitions(res);
return v1;
}
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, 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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
data[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
newData[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
data[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
newData[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
}
} 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;
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.vector.IntIntVector in project angel by Tencent.
the class BinaryMatrixExecutor method apply.
private static Matrix apply(BlasDoubleMatrix mat, IntIntVector 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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
data[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int i = entry.getIntKey();
flag[i] = 1;
newData[i * n + idx] = op.apply(data[i * n + idx], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
data[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
}
} 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;
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()) {
int[] 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<Int2IntMap.Entry> iter = v.getStorage().entryIterator();
while (iter.hasNext()) {
Int2IntMap.Entry entry = iter.next();
int j = entry.getIntKey();
flag[j] = 1;
newData[idx * n + j] = op.apply(data[idx * n + j], entry.getIntValue());
}
} 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;
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