Package org.apache.commons.math.linear

Examples of org.apache.commons.math.linear.RealMatrixImpl


   * @param x x values
   * @param y y values
   * @param order order of polynomial
   */
  public PolynomialFitter(double[] x, double[] y, int order) {
    RealMatrix yMatrix = new RealMatrixImpl(y);
    stdDev = Math.sqrt(StatUtils.variance(x));
    mean = StatUtils.mean(x);
    //mean centre it...
    for (int i =0; i<x.length;i++) {
      x[i]=(x[i]-mean)/stdDev;
    }
   
    /*
     * This is a horrible cludge to use the jama matrix class for
     * inversion/qr decomposition until the preferable
     * Apache commons class catches up. Matrix is the Jama version
     * RealMatrixImpl is the commons version.
     */
    Matrix v = new Matrix(new VandermondeMatrix(x,order+1).getMatrix().getData());
    QRDecomposition qr = new QRDecomposition(v);
    rMatrix = new RealMatrixImpl(qr.getR().getArray());
    qMatrix = new RealMatrixImpl(qr.getQ().getArray());
    RealMatrix qMatrixTransposed = qMatrix.transpose();
    RealMatrix qByY = qMatrixTransposed.multiply(yMatrix);
    RealMatrix  rMatrixInverse = new RealMatrixImpl(new Matrix(rMatrix.getData()).inverse().getArray());
    RealMatrix resultMatrix = rMatrixInverse.multiply(qByY);
    coefficiants= resultMatrix.getColumn(0);

    //System.out.println(stdDev+" "+mean);
  }
View Full Code Here


      for (int i=0; i<vanderDouble.length; i++) {
        vanderDouble[i][j]=vanderDouble[i][j+1]*v[i];
      }
    }
   
    vandermondeMatrix = new RealMatrixImpl(vanderDouble);
  }
View Full Code Here

    public void setUp() {
        try {
            mean = new double[] { 0.0, 1.0, -3.0, 2.3};

            RealMatrixImpl b = new RealMatrixImpl(4, 3);
            double[][] bData = b.getDataRef();
            int counter = 0;
            for (int i = 0; i < bData.length; ++i) {
                double[] bi = bData[i];
                for (int j = 0; j < b.getColumnDimension(); ++j) {
                    bi[j] = 1.0 + 0.1 * ++counter;
                }
            }
            RealMatrix bbt = b.multiply(b.transpose());
            covariance = new RealMatrixImpl(mean.length, mean.length);
            double[][] covData = covariance.getDataRef();
            for (int i = 0; i < covariance.getRowDimension(); ++i) {
                covData[i][i] = bbt.getEntry(i, i);
                for (int j = 0; j < covariance.getColumnDimension(); ++j) {
                    double s = bbt.getEntry(i, j);
View Full Code Here

            }

        }

        // build the root matrix
        root = new RealMatrixImpl(order, rank);
        for (int i = 0; i < order; ++i) {
            System.arraycopy(b[i], 0, root.getDataRef()[swap[i]], 0, rank);
        }

    }
View Full Code Here

     * @return covariance matrix
     */
    public RealMatrix getResult() {

        int dimension = sums.length;
        RealMatrixImpl result = new RealMatrixImpl(dimension, dimension);

        if (n > 1) {
            double[][] resultData = result.getDataRef();
            double c = 1.0 / (n * (isBiasCorrected ? (n - 1) : n));
            int k = 0;
            for (int i = 0; i < dimension; ++i) {
                for (int j = 0; j <= i; ++j) {
                    double e = c * (n * productsSums[k++] - sums[i] * sums[j]);
 
View Full Code Here

        initializeEstimate(problem);

        // work matrices
        double[] grad             = new double[parameters.length];
        RealMatrixImpl bDecrement = new RealMatrixImpl(parameters.length, 1);
        double[][] bDecrementData = bDecrement.getDataRef();
        RealMatrixImpl wGradGradT = new RealMatrixImpl(parameters.length, parameters.length);
        double[][] wggData        = wGradGradT.getDataRef();

        // iterate until convergence is reached
        double previous = Double.POSITIVE_INFINITY;
        do {

            // build the linear problem
            incrementJacobianEvaluationsCounter();
            RealMatrix b = new RealMatrixImpl(parameters.length, 1);
            RealMatrix a = new RealMatrixImpl(parameters.length, parameters.length);
            for (int i = 0; i < measurements.length; ++i) {
                if (! measurements [i].isIgnored()) {

                    double weight   = measurements[i].getWeight();
                    double residual = measurements[i].getResidual();

                    // compute the normal equation
                    for (int j = 0; j < parameters.length; ++j) {
                        grad[j] = measurements[i].getPartial(parameters[j]);
                        bDecrementData[j][0] = weight * residual * grad[j];
                    }

                    // build the contribution matrix for measurement i
                    for (int k = 0; k < parameters.length; ++k) {
                        double[] wggRow = wggData[k];
                        double gk = grad[k];
                        for (int l = 0; l < parameters.length; ++l) {
                            wggRow[l] =  weight * gk * grad[l];
                        }
                    }

                    // update the matrices
                    a = a.add(wGradGradT);
                    b = b.add(bDecrement);

                }
            }

            try {

                // solve the linearized least squares problem
                RealMatrix dX = a.solve(b);

                // update the estimated parameters
                for (int i = 0; i < parameters.length; ++i) {
                    parameters[i].setEstimate(parameters[i].getEstimate() + dX.getEntry(i, 0));
                }
View Full Code Here

            }
        }

        try {
            // compute the covariances matrix
            return new RealMatrixImpl(jTj).inverse().getData();
        } catch (InvalidMatrixException ime) {
            throw new EstimationException("unable to compute covariances: singular problem",
                                          new Object[0]);
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.math.linear.RealMatrixImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.