http://www.massapi.com/method/org/jblas/DoubleMatrix.mmul.html
mul是矩阵和元素相乘。
mmul是矩阵和矩阵相乘。
源码:
public DoubleMatrix mmuli(double v) { return this.mmuli(v, this); } public DoubleMatrix mmul(double v) { return this.mmuli(v, new DoubleMatrix(this.rows, this.columns)); } public DoubleMatrix mmuli(DoubleMatrix other, DoubleMatrix result) { if (other.isScalar()) { return this.muli(other.scalar(), result); } else if (this.isScalar()) { return other.muli(this.scalar(), result); } else { this.assertMultipliesWith(other); if (result.rows != this.rows || result.columns != other.columns) { if (result == this || result == other) { throw new SizeException("Cannot resize result matrix because it is used in-place."); } result.resize(this.rows, other.columns); } if (result != this && result != other) { if (other.columns == 1) { SimpleBlas.gemv(1.0D, this, other, 0.0D, result); } else { SimpleBlas.gemm(1.0D, this, other, 0.0D, result); } } else { DoubleMatrix temp = new DoubleMatrix(result.rows, result.columns); if (other.columns == 1) { SimpleBlas.gemv(1.0D, this, other, 0.0D, temp); } else { SimpleBlas.gemm(1.0D, this, other, 0.0D, temp); } SimpleBlas.copy(temp, result); } return result; } }
@Override public void update(Boolean label, double[] features) { if (this.weights == null || this.variance == null) { this.init(features.length); } DoubleMatrix weightsVector = new DoubleMatrix(1, this.weights.length, this.weights); DoubleMatrix varianceMatrix = new DoubleMatrix(this.variance); DoubleMatrix featuresVector = new DoubleMatrix(1, features.length, features); double margin = weightsVector.dot(featuresVector); double labelAsDouble = label ? 1.0 : -1.0; if (margin * labelAsDouble < 1) { double confidence = featuresVector.dot(featuresVector.mmul(varianceMatrix)); double beta = 1 / (confidence + this.r); double alpha = Math.max(0, beta * (1 - labelAsDouble * margin)); DoubleMatrix delta = featuresVector.mmul(varianceMatrix).mul(alpha * labelAsDouble); boolean zeroVector = MathUtil.isZeros(delta); if (!zeroVector) { this.weights = weightsVector.add(delta).toArray(); // Matrix library needed! this.variance = varianceMatrix.sub(featuresVector.mmul(varianceMatrix).transpose().mmul(featuresVector).mul(beta).mmul(varianceMatrix)).toArray2(); } } }
jblas is a fast linear algebra library for Java. jblas is based on BLAS and LAPACK, the de-facto industry standard for matrix computations, and uses state-of-the-art implementations like ATLAS for all its computational routines, making jBLAS very fast.
主页地址:http://jblas.org/
该库的API文档地址:http://jblas.org/javadoc/index.html
在Java工程中我们只要 引入jar
包文件就可以了。
下面是其小册子:
jblas - Fast matrix computations for Java
import org.jblas.*; import static org.jblas.DoubleMatrix.*; import static org.jblas.MatrixFunctions.*;