One - One Code All

Blog Content

java科学计算线性代数库org.jblas.DoubleMatrix.mmul()

统计学-科学计算 Java   2013-11-10 22:02:21

Examples of org.jblas.DoubleMatrix.mmul()

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;
        }
    }


example源码:http://www.massapi.com/source/github/12/75/1275795737/releases/com/github/pmerienne/trident-ml/0.0.4/trident-ml-0.0.4-sources.jar.unzip/com/github/pmerienne/trident/ml/classification/AROWClassifier.java.html#73


  @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();
      }
    }
  }


1.关于jblas

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.*;


上一篇:java金融包org.ojalgo.finance.portfolio.MarkowitzModel
下一篇:java线性代数正态分布org.apache.commons.math3.distribution.NormalDistribution

The minute you think of giving up, think of the reason why you held on so long.