use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class SimpleUnaryExecutor method apply.
private static Vector apply(IntDoubleVector v1, Unary op) {
IntDoubleVector res;
if (op.isOrigin() || v1.isDense()) {
if (!op.isInplace()) {
res = v1.copy();
} else {
res = v1;
}
if (v1.isDense()) {
double[] values = res.getStorage().getValues();
for (int i = 0; i < values.length; i++) {
values[i] = op.apply(values[i]);
}
} else if (v1.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = res.getStorage().entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
entry.setValue(op.apply(entry.getDoubleValue()));
}
} else if (v1.isSorted()) {
double[] values = res.getStorage().getValues();
for (int i = 0; i < v1.size(); i++) {
values[i] = op.apply(values[i]);
}
} else {
throw new AngelException("The operation is not support!");
}
} else {
IntDoubleVectorStorage newstorage = v1.getStorage().emptyDense();
IntDoubleVectorStorage storage = v1.getStorage();
double[] values = newstorage.getValues();
double tmp = op.apply((double) 0);
int dim = v1.getDim();
for (int i = 0; i < dim; i++) {
values[i] = tmp;
}
if (v1.isSparse()) {
ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
while (iter.hasNext()) {
Int2DoubleMap.Entry entry = iter.next();
values[entry.getIntKey()] = op.apply(entry.getDoubleValue());
}
} else {
// sort
int[] idxs = storage.getIndices();
double[] v1Values = storage.getValues();
for (int k = 0; k < idxs.length; k++) {
values[idxs[k]] = op.apply(v1Values[k]);
}
}
if (op.isInplace()) {
v1.setStorage(newstorage);
res = v1;
} else {
res = new IntDoubleVector(v1.getMatrixId(), v1.getRowId(), v1.getClock(), v1.getDim(), newstorage);
}
}
return res;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class IntDoubleVector method std.
public double std() {
IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
if (dstorage.size() == 0)
return 0;
double sumval = 0.0;
double sumval2 = 0.0;
if (dstorage.isSparse()) {
DoubleIterator iter = dstorage.valueIterator();
while (iter.hasNext()) {
double val = iter.nextDouble();
sumval += val;
sumval2 += val * val;
}
} else {
for (double val : dstorage.getValues()) {
sumval += val;
sumval2 += val * val;
}
}
sumval /= getDim();
sumval2 /= getDim();
return Math.sqrt(sumval2 - sumval * sumval);
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class IntDoubleVector method average.
public double average() {
IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
if (dstorage.size() == 0)
return 0;
double sumval = 0.0;
if (dstorage.isSparse()) {
DoubleIterator iter = dstorage.valueIterator();
while (iter.hasNext()) {
sumval += iter.nextDouble();
}
} else {
for (double val : dstorage.getValues()) {
sumval += val;
}
}
sumval /= getDim();
return sumval;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class IntDoubleVector method numZeros.
public int numZeros() {
IntDoubleVectorStorage dstorage = (IntDoubleVectorStorage) storage;
if (dstorage.size() == 0)
return (int) dim;
int numZero = 0;
if (dstorage.isSparse()) {
DoubleIterator iter = dstorage.valueIterator();
while (iter.hasNext()) {
if (iter.nextDouble() != 0) {
numZero += 1;
}
}
} else {
for (double val : dstorage.getValues()) {
if (val != 0) {
numZero += 1;
}
}
}
return (int) getDim() - numZero;
}
use of com.tencent.angel.ml.math2.storage.IntDoubleVectorStorage in project angel by Tencent.
the class CompIntDoubleRowUpdateSplit method serialize.
@Override
public void serialize(ByteBuf buf) {
super.serialize(buf);
IntDoubleVectorStorage storage = split.getStorage();
if (storage instanceof IntDoubleSparseVectorStorage) {
buf.writeInt(storage.size());
ObjectIterator<Int2DoubleMap.Entry> iter = storage.entryIterator();
Int2DoubleMap.Entry entry;
while (iter.hasNext()) {
entry = iter.next();
buf.writeInt(entry.getIntKey());
buf.writeDouble(entry.getDoubleValue());
}
} else if (storage instanceof IntDoubleSortedVectorStorage) {
buf.writeInt(storage.size());
int[] indices = storage.getIndices();
double[] values = storage.getValues();
for (int i = 0; i < indices.length; i++) {
buf.writeInt(indices[i]);
buf.writeDouble(values[i]);
}
} else if (storage instanceof IntDoubleDenseVectorStorage) {
double[] values = storage.getValues();
int writeSize = values.length < maxItemNum ? values.length : maxItemNum;
buf.writeInt(writeSize);
for (int i = 0; i < writeSize; i++) {
buf.writeDouble(values[i]);
}
} else {
throw new UnsupportedOperationException("unsupport split for storage " + storage.getClass().getName());
}
}
Aggregations