Example usage for org.apache.commons.math3.linear ArrayRealVector ebeMultiply

List of usage examples for org.apache.commons.math3.linear ArrayRealVector ebeMultiply

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear ArrayRealVector ebeMultiply.

Prototype

@Override
public ArrayRealVector ebeMultiply(RealVector v) throws DimensionMismatchException 

Source Link

Usage

From source file:gamlss.utilities.MatrixFunctions.java

/**
 * Multiply vector with every row of the matrix.
 * @param v - vector/*from ww w. j a v  a2 s  . c om*/
 * @param m - matrix
 * @return v*m
 */
public static BlockRealMatrix multVectorMatrix(final ArrayRealVector v, final BlockRealMatrix m) {

    BlockRealMatrix tempM = new BlockRealMatrix(m.getRowDimension(), m.getColumnDimension());
    for (int i = 0; i < m.getColumnDimension(); i++) {
        tempM.setColumnVector(i, v.ebeMultiply(m.getColumnVector(i)));
    }
    return tempM;
}

From source file:de.terministic.serein.core.genome.translators.LinearTranslator.java

@Override
public RealVector translate(DoubleGenome doubleGenome) {
    ArrayRealVector input = new ArrayRealVector(doubleGenome.getGeneVector());
    ArrayRealVector result = input.ebeMultiply(distance).add(origin);
    return result;
}

From source file:edu.utexas.cs.tactex.subscriptionspredictors.LWRCustOldAppache.java

/**
  * LWR prediction/*from www .  j av a2 s  . c o m*/
  * 
  * @param X
  * @param Y
  * @param x0
  * @param tau
  * @return
  */
public Double LWRPredict(ArrayRealVector X, ArrayRealVector Y, double x0, final double tau) {
    ArrayRealVector X0 = new ArrayRealVector(X.getDimension(), x0);
    ArrayRealVector delta = X.subtract(X0);
    ArrayRealVector sqDists = delta.ebeMultiply(delta);
    UnivariateFunction expTau = new UnivariateFunction() {
        @Override
        public double value(double arg0) {
            //log.info(" cp univariate tau " + tau);
            return Math.pow(Math.E, -arg0 / (2 * tau));
        }
    };
    ArrayRealVector W = sqDists.map(expTau);
    double Xt_W_X = X.dotProduct(W.ebeMultiply(X));
    if (Xt_W_X == 0.0) {
        log.error(" cp LWR cannot predict - 0 denominator returning NULL");
        log.error("Xcv is " + X.toString());
        log.error("Ycv is " + Y.toString());
        log.error("x0 is " + x0);
        return null; // <==== NOTE: a caller must be prepared for it
    }
    double theta = (1.0 / Xt_W_X) * X.ebeMultiply(W).dotProduct(Y);

    return theta * x0;
}

From source file:gamlss.utilities.WLSMultipleLinearRegression.java

/**
 * /*w w  w  .  j a  va 2  s .co  m*/
 * @param y
 * @param x
 * @param w
 */
private void newSampleDataNoCopy(ArrayRealVector y, RealMatrix x, ArrayRealVector w) {
    for (int row = 0; row < x.getRowDimension(); row++) {
        x.setRowVector(row, x.getRowVector(row).mapMultiplyToSelf(w.getEntry(row)));
    }
    //double[][] xw=x.getData();
    //double[] yw= y.ebeMultiply(w).getDataRef();
    //validateSampleData(xw, yw); //we have already checked this in the gamlss algorithm. 
    newYSampleData(y.ebeMultiply(w));
    newXSampleData(x.getData(), w);
}

From source file:gamlss.utilities.WLSMultipleLinearRegression.java

/**
 * /*from w ww  . j  ava  2s .  c o  m*/
 * @param y
 * @param x
 * @param w
 */
private void newSampleDataCopyOriginal(ArrayRealVector y, RealMatrix x, ArrayRealVector w) {
    this.y = y.copy(); //we need this for the fitted values and residuals.
    if (this.isNoIntercept()) {
        this.X = x.copy();

    } else {
        setDesignMatrix(x);//add 1 for the Intercept;
    }
    for (int row = 0; row < x.getRowDimension(); row++) {
        x.setRowVector(row, x.getRowVector(row).mapMultiplyToSelf(w.getEntry(row)));
    }
    //double[][] xw=x.getData();
    //double[] yw= y.ebeMultiply(w).getDataRef();
    //validateSampleData(xw, yw); //we have already checked this in the gamlss algorithm. 
    newYSampleData(y.ebeMultiply(w));
    newXSampleData(x.getData(), w);
}

From source file:gamlss.smoothing.PB.java

/**
  * GCV smoothing method./* w w  w  .  j  a v  a  2  s. c  o m*/
  * @param lambda - smoothing parameter
  * @return fitted values
  */
private ArrayRealVector functionGCV(Double lambda) {

    //QR <-qr(sqrt(w)*X)
    //Rinv <- solve(qr.R(QR))
    QRDecomposition qr = new QRDecomposition(
            MatrixFunctions.multVectorMatrix(MatrixFunctions.sqrtVec(w), basisM));
    rM = (BlockRealMatrix) qr.getR();
    rM = rM.getSubMatrix(0, rM.getColumnDimension() - 1, 0, rM.getColumnDimension() - 1);
    rMinv = (BlockRealMatrix) new QRDecomposition(rM).getSolver().getInverse();

    //wy <- sqrt(w)*y
    ArrayRealVector wy = MatrixFunctions.sqrtVec(w).ebeMultiply(y);

    //y.y <- sum(wy^2)
    double y_y = MatrixFunctions.sumV(wy.ebeMultiply(wy));

    //S   <- t(D)%*%D
    sM = penaltyM.transpose().multiply(penaltyM);

    //UDU <- eigen(t(Rinv)%*%S%*%Rinv)
    uduM = new BlockRealMatrix(
            new EigenDecomposition(rMinv.transpose().multiply(sM).multiply(rMinv), Controls.SPLIT_TOLERANCE)
                    .getV().getData());

    //yy <- t(UDU$vectors)%*%t(qr.Q(QR))%*%wy
    BlockRealMatrix qM = (BlockRealMatrix) qr.getQ();
    //SingularValueDecomposition svd = new SingularValueDecomposition(
    //MatrixFunctions.multVectorMatrix(MatrixFunctions.sqrtVec(w), basisM));
    //BlockRealMatrix qM = new BlockRealMatrix(svd.getV().getData());

    //.... to finish !!!!!!!
    MatrixFunctions.matrixPrint(qM);
    return null;
}

From source file:org.eclipse.dawnsci.analysis.dataset.roi.fitting.EllipseCoordinatesFunction.java

/**
 * least-squares using algebraic cost function
 * <p>/*  w w  w  .  j  a  v a 2  s. c om*/
 * This uses the method in "Numerically stable direct least squares fitting of ellipses"
 * by R. Halir and J. Flusser, Proceedings of the 6th International Conference in Central Europe
 * on Computer Graphics and Visualization. WSCG '98. CZ, Pilsen 1998, pp 125-132. 
 * <p>
 * @param ix
 * @param iy
 * @return geometric parameters
 */
private static double[] quickfit(IDataset ix, IDataset iy) {
    Dataset x = DatasetUtils.convertToDataset(ix);
    Dataset y = DatasetUtils.convertToDataset(iy);
    final Dataset xx = Maths.square(x);
    final Dataset yy = Maths.square(y);
    final Dataset xxx = Maths.multiply(xx, x);
    final Dataset yyy = Maths.multiply(yy, y);
    final Dataset xy = Maths.multiply(x, y);

    Matrix S1 = new Matrix(3, 3);
    S1.set(0, 0, LinearAlgebra.dotProduct(xx, xx).getDouble());
    S1.set(0, 1, LinearAlgebra.dotProduct(xxx, y).getDouble());
    S1.set(0, 2, LinearAlgebra.dotProduct(xx, yy).getDouble());

    S1.set(1, 0, S1.get(0, 1));
    S1.set(1, 1, S1.get(0, 2));
    S1.set(1, 2, LinearAlgebra.dotProduct(x, yyy).getDouble());

    S1.set(2, 0, S1.get(0, 2));
    S1.set(2, 1, S1.get(1, 2));
    S1.set(2, 2, LinearAlgebra.dotProduct(yy, yy).getDouble());

    Matrix S2 = new Matrix(3, 3);
    S2.set(0, 0, ((Number) xxx.sum()).doubleValue());
    S2.set(0, 1, LinearAlgebra.dotProduct(xx, y).getDouble());
    S2.set(0, 2, ((Number) xx.sum()).doubleValue());

    S2.set(1, 0, S2.get(0, 1));
    S2.set(1, 1, LinearAlgebra.dotProduct(x, yy).getDouble());
    S2.set(1, 2, ((Number) xy.sum()).doubleValue());

    S2.set(2, 0, S2.get(1, 1));
    S2.set(2, 1, ((Number) yyy.sum()).doubleValue());
    S2.set(2, 2, ((Number) yy.sum()).doubleValue());

    Matrix S3 = new Matrix(3, 3);
    S3.set(0, 0, S2.get(0, 2));
    S3.set(0, 1, S2.get(1, 2));
    S3.set(0, 2, ((Number) x.sum()).doubleValue());

    S3.set(1, 0, S3.get(0, 1));
    S3.set(1, 1, S2.get(2, 2));
    S3.set(1, 2, ((Number) y.sum()).doubleValue());

    S3.set(2, 0, S3.get(0, 2));
    S3.set(2, 1, S3.get(1, 2));
    S3.set(2, 2, x.getSize());

    Matrix T = S3.solve(S2.transpose()).uminus();

    Matrix M = S1.plus(S2.times(T));

    Matrix Cinv = new Matrix(new double[] { 0, 0, 0.5, 0, -1.0, 0, 0.5, 0, 0 }, 3);
    Matrix Mp = Cinv.times(M);

    //      System.err.println("M " + Arrays.toString(Mp.getRowPackedCopy()));
    Matrix V = Mp.eig().getV();
    //      System.err.println("V " + Arrays.toString(V.getRowPackedCopy()));
    double[][] mv = V.getArray();
    ArrayRealVector v1 = new ArrayRealVector(mv[0]);
    ArrayRealVector v2 = new ArrayRealVector(mv[1]);
    ArrayRealVector v3 = new ArrayRealVector(mv[2]);

    v1.mapMultiplyToSelf(4);

    ArrayRealVector v = v1.ebeMultiply(v3).subtract(v2.ebeMultiply(v2));
    double[] varray = v.getDataRef();
    int i = 0;
    for (; i < 3; i++) {
        if (varray[i] > 0)
            break;
    }
    if (i == 3) {
        throw new IllegalArgumentException("Could not find solution that satifies constraint");
    }

    v = new ArrayRealVector(new double[] { mv[0][i], mv[1][i], mv[2][i] });
    varray = v.getDataRef();
    final double ca = varray[0];
    final double cb = varray[1];
    final double cc = varray[2];
    Array2DRowRealMatrix mt = new Array2DRowRealMatrix(T.getArray(), false);
    varray = mt.operate(varray);
    final double cd = varray[0];
    final double ce = varray[1];
    final double cf = varray[2];

    //      System.err.println(String.format("Algebraic: %g, %g, %g, %g, %g, %g", ca, cb, cc, cd, ce, cf));
    final double disc = cb * cb - 4. * ca * cc;
    if (disc >= 0) {
        throw new IllegalArgumentException("Solution is not an ellipse");
    }

    if (cb == 0) {
        throw new IllegalArgumentException("Solution is a circle");
    }

    double[] qparameters = new double[PARAMETERS];
    qparameters[3] = (2. * cc * cd - cb * ce) / disc;
    qparameters[4] = (2. * ca * ce - cb * cd) / disc;

    final double sqrt = Math.sqrt((ca - cc) * (ca - cc) + cb * cb);
    qparameters[0] = -2. * (ca * ce * ce + cc * cd * cd + cf * cb * cb - cb * cd * ce - 4. * ca * cc * cf)
            / disc;
    qparameters[1] = qparameters[0] / (ca + cc + sqrt);
    qparameters[0] /= (ca + cc - sqrt);
    qparameters[0] = Math.sqrt(qparameters[0]);
    qparameters[1] = Math.sqrt(qparameters[1]);
    if (cb == 0) {
        qparameters[2] = 0.;
    } else {
        qparameters[2] = 0.5 * Math.atan2(cb, ca - cc);
    }
    if (qparameters[0] < qparameters[1]) {
        final double t = qparameters[0];
        qparameters[0] = qparameters[1];
        qparameters[1] = t;
    } else {
        qparameters[2] += Math.PI * 0.5;
    }

    return qparameters;
}