似乎没有必要创建自定义实现。请考虑以下示例:
import cern.colt.matrix.impl.SparseDoubleMatrix1D;
import cern.jet.math.Functions;
// …
final SparseDoubleMatrix1D aMatrix = new SparseDoubleMatrix1D(new double[] { 3.0 });
final SparseDoubleMatrix1D bMatrix = new SparseDoubleMatrix1D(new double[] { 1.0 });
aMatrix.assign(bMatrix, Functions.minus);
// aMatrix is the result.
System.out.println(aMatrix);
请参考cern.jet.math.Functions 类。
自定义实现
请注意,静态方法可能是多余的。
import cern.colt.matrix.impl.SparseDoubleMatrix1D;
import cern.jet.math.Functions;
final class SparseVector extends SparseDoubleMatrix1D {
public SparseVector(int size) {
super(size);
}
public SparseVector(double[] values) {
super(values);
}
/**
* Subtract otherVector from this vector.
* The result is stored in this vector.
* @param otherVector other vector
* @return this vector
*/
public SparseVector subtract(SparseVector otherVector) {
assign(otherVector, Functions.minus);
return this;
}
public static SparseVector subtract(SparseVector x, SparseVector y) {
final SparseVector result = new SparseVector(x.toArray());
result.subtract(y);
return result;
}
}
例子:
final SparseVector aVector = new SparseVector(new double[] { 3.0 });
final SparseVector bVector = new SparseVector(new double[] { 1.0 });
aVector.subtract(bVector);
// aVector is the result.
System.out.println(aVector);