Example usage for org.apache.commons.math.linear RealMatrix setRow

List of usage examples for org.apache.commons.math.linear RealMatrix setRow

Introduction

In this page you can find the example usage for org.apache.commons.math.linear RealMatrix setRow.

Prototype

void setRow(int row, double[] array) throws MatrixIndexException, InvalidMatrixException;

Source Link

Document

Sets the entries in row number row as a row matrix.

Usage

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

public static PolynomialCoefficients fitPolynomial(ExponentPairs exponentPairs,
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    final List<Point2D.Double> srcPoints = new ArrayList(pointPairs.keySet());
    final RealMatrix matrix = new Array2DRowRealMatrix(srcPoints.size(), exponentPairs.size());
    for (int i = 0; i < srcPoints.size(); ++i) {
        matrix.setRow(i, powerTerms(srcPoints.get(i).x, srcPoints.get(i).y, exponentPairs));
    }//from www. j  av  a2s.  co  m
    final DecompositionSolver solver = new LUDecompositionImpl(matrix).getSolver();
    final double[] destX = new double[srcPoints.size()];
    final double[] destY = new double[srcPoints.size()];
    for (int i = 0; i < srcPoints.size(); ++i) {
        final Point2D.Double destPoint = pointPairs.get(srcPoints.get(i));
        destX[i] = destPoint.x;
        destY[i] = destPoint.y;
    }
    final PolynomialCoefficients polys = new PolynomialCoefficients();
    polys.polyX = solver.solve(destX);
    polys.polyY = solver.solve(destY);
    return polys;
}

From source file:edu.valelab.GaussianFit.CoordinateMapper.java

/*** Rigid body transform (rotation and translation only)
        //from   www.java 2 s .c  o m
        
 /**
 * Creates an AffineTransform object that uses only rotation and translation
 * 
 * @pointPairs is a Map of points measured in the two coordinates systems (srcPt->destPt)
 */
public static AffineTransform generateRigidBodyTransform(Map<Point2D.Double, Point2D.Double> pointPairs) {
    int number = pointPairs.size();

    RealMatrix X = new Array2DRowRealMatrix(2 * number, 4);
    RealMatrix U = new Array2DRowRealMatrix(2 * number, 1);

    int i = 0;
    for (Map.Entry<Point2D.Double, Point2D.Double> pair : pointPairs.entrySet()) {
        double[] thisRow = { pair.getKey().x, pair.getKey().y, 1.0, 0.0 };
        X.setRow(i, thisRow);
        double[] otherRow = { pair.getKey().y, -pair.getKey().x, 0.0, 1.0 };
        X.setRow(i + number, otherRow);

        U.setEntry(i, 0, pair.getValue().x);
        U.setEntry(i + number, 0, pair.getValue().y);
        i++;
    }

    DecompositionSolver solver = (new QRDecompositionImpl(X)).getSolver();
    double[][] m = solver.solve(U).getData();

    return new AffineTransform(m[0][0], m[1][0], -m[1][0], m[0][0], m[2][0], m[3][0]);
}

From source file:edu.valelab.gaussianfit.datasettransformations.CoordinateMapper.java

public static PolynomialCoefficients fitPolynomial(ExponentPairs exponentPairs,
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    final List<Point2D.Double> srcPoints = new ArrayList<Point2D.Double>(pointPairs.keySet());
    final RealMatrix matrix = new Array2DRowRealMatrix(srcPoints.size(), exponentPairs.size());
    for (int i = 0; i < srcPoints.size(); ++i) {
        matrix.setRow(i, powerTerms(srcPoints.get(i).x, srcPoints.get(i).y, exponentPairs));
    }/*from   w  w w  .j  ava2 s  .  c o m*/
    final DecompositionSolver solver = new LUDecompositionImpl(matrix).getSolver();
    final double[] destX = new double[srcPoints.size()];
    final double[] destY = new double[srcPoints.size()];
    for (int i = 0; i < srcPoints.size(); ++i) {
        final Point2D.Double destPoint = pointPairs.get(srcPoints.get(i));
        destX[i] = destPoint.x;
        destY[i] = destPoint.y;
    }
    final PolynomialCoefficients polys = new PolynomialCoefficients();
    polys.polyX = solver.solve(destX);
    polys.polyY = solver.solve(destY);
    return polys;
}

From source file:fi.smaa.libror.AcceptanceCriterionTest.java

@Before
public void setUp() {
    int rows = 2;
    int cols = 2;
    RealMatrix p = new Array2DRowRealMatrix(rows, cols);
    p.setRow(0, new double[] { 1.0, 2.0 });
    p.setRow(1, new double[] { 2.0, 1.0 });
    model = new RORModel(new PerformanceMatrix(p));
    model.addPreference(0, 1); // a0 > a1
    criterion = new AcceptanceCriterion(model);
}

From source file:fi.smaa.libror.RejectionValueFunctionSamplerTest.java

@Before
public void setUp() {
    int rows = 2;
    int cols = 3;
    RealMatrix p = new Array2DRowRealMatrix(rows, cols);
    p.setRow(0, new double[] { 1.0, 2.0, 3.0 });
    p.setRow(1, new double[] { 1.0, 3.0, 4.0 });
    model = new RORModel(new PerformanceMatrix(p));
    sampler = new RejectionValueFunctionSampler(model, 5);
}

From source file:fi.smaa.libror.RejectionValueFunctionSamplerTest.java

@Test
public void testSampleFunctions() throws SamplingException {
    int rows = 2;
    int cols = 2;
    RealMatrix p = new Array2DRowRealMatrix(rows, cols);
    p.setRow(0, new double[] { 1.0, 2.0 });
    p.setRow(1, new double[] { 2.0, 1.0 });
    model = new RORModel(new PerformanceMatrix(p));
    model.addPreference(0, 1); // a0 > a1
    sampler = new RejectionValueFunctionSampler(model, 2);
    sampler.sample();/*from www .j ava  2 s . c  om*/
    for (FullValueFunction vf : sampler.getValueFunctions()) {
        assertEquals(2, vf.getPartialValueFunctions().size());
        assertEquals(new PartialValueFunction(2), vf.getPartialValueFunctions().get(0));
        assertEquals(new PartialValueFunction(2), vf.getPartialValueFunctions().get(1));
        double[] w = vf.getWeights();
        assertTrue(w[0] < w[1]);
    }
}

From source file:geogebra.kernel.implicit.GeoImplicitPoly.java

/**
 * make curve through given points//from ww w  .  j av  a2s. co m
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1);
    RealMatrix matrix = new RealMatrixImpl(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results;

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new RealMatrixImpl(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints + 1; i++)
            if (i == solutionColumn)
                continue;
            else
                matrix.setColumn(j++, extendMatrix.getColumn(i));
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    double[] solution = new double[partialSolution.length + 1];

    for (int i = 0, j = 0; i < solution.length; i++)
        if (i == solutionColumn - 1)
            solution[i] = 1;
        else {
            solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j];
            j++;
        }

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; i + j < realDegree + 1; j++)
            coeffMatrix[i][j] = solution[k++];

    this.setCoeff(coeffMatrix, true);

    this.defined = true;
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i), 1)) {
            this.setUndefined();
            return;
        }

}

From source file:geogebra.kernel.GeoImplicitPoly.java

/**
 * make curve through given points//www .j ava2  s  .c  o m
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    if (pointsOnCurve == null)
        pointsOnCurve = new Coords[points.size()];

    for (int i = 0; i < points.size(); i++) {
        if (pointsOnCurve[i] == null)
            pointsOnCurve[i] = new Coords(points.get(i).x, points.get(i).y, points.get(i).z);
        else {
            pointsOnCurve[i].setX(points.get(i).x);
            pointsOnCurve[i].setY(points.get(i).y);
            pointsOnCurve[i].setZ(points.get(i).z);
        }
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new RealMatrixImpl(points.size(), points.size() + 1);
    RealMatrix matrix = new RealMatrixImpl(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results = new double[points.size()];

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new RealMatrixImpl(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new RealMatrixImpl(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints; i++)
            if (i == solutionColumn)
                continue;
            else
                matrix.setColumn(j++, extendMatrix.getColumn(i));
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    for (int i = 0; i < partialSolution.length; i++)
        if (Kernel.isZero(partialSolution[i]))
            partialSolution[i] = 0;

    for (int i = 0; i < partialSolution.length; i++)
        if (Kernel.isZero(partialSolution[i]))
            partialSolution[i] = 0;

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; j + i != realDegree + 1; j++)
            if (k == solutionColumn - 1)
                coeffMatrix[i][j] = 1;
            else
                coeffMatrix[i][j] = partialSolution[k++];

    this.setCoeff(coeffMatrix);
    this.update();
    this.defined = true;
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i))) {
            this.setUndefined();
            return;
        }

    this.type = IMPLICIT_POLY_THROUGH_POINTS;
}

From source file:geogebra.common.kernel.implicit.GeoImplicitPoly.java

/**
 * make curve through given points/*from  w ww  .  j  av  a2  s  .c om*/
 * @param points ArrayList of points
 */
public void throughPoints(ArrayList<GeoPoint> points) {
    if ((int) Math.sqrt(9 + 8 * points.size()) != Math.sqrt(9 + 8 * points.size())) {
        setUndefined();
        return;
    }

    int degree = (int) (0.5 * Math.sqrt(8 * (1 + points.size()))) - 1;
    int realDegree = degree;

    RealMatrix extendMatrix = new Array2DRowRealMatrix(points.size(), points.size() + 1);
    RealMatrix matrix = new Array2DRowRealMatrix(points.size(), points.size());
    double[][] coeffMatrix = new double[degree + 1][degree + 1];

    DecompositionSolver solver;

    double[] matrixRow = new double[points.size() + 1];
    double[] results;

    for (int i = 0; i < points.size(); i++) {
        double x = points.get(i).x / points.get(i).z;
        double y = points.get(i).y / points.get(i).z;

        for (int j = 0, m = 0; j < degree + 1; j++)
            for (int k = 0; j + k != degree + 1; k++)
                matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
        extendMatrix.setRow(i, matrixRow);
    }

    int solutionColumn = 0, noPoints = points.size();

    do {
        if (solutionColumn > noPoints) {
            noPoints = noPoints - realDegree - 1;

            if (noPoints < 2) {
                setUndefined();
                return;
            }

            extendMatrix = new Array2DRowRealMatrix(noPoints, noPoints + 1);
            realDegree -= 1;
            matrixRow = new double[noPoints + 1];

            for (int i = 0; i < noPoints; i++) {
                double x = points.get(i).x;
                double y = points.get(i).y;

                for (int j = 0, m = 0; j < realDegree + 1; j++)
                    for (int k = 0; j + k != realDegree + 1; k++)
                        matrixRow[m++] = Math.pow(x, j) * Math.pow(y, k);
                extendMatrix.setRow(i, matrixRow);
            }

            matrix = new Array2DRowRealMatrix(noPoints, noPoints);
            solutionColumn = 0;
        }

        results = extendMatrix.getColumn(solutionColumn);

        for (int i = 0, j = 0; i < noPoints + 1; i++) {
            if (i == solutionColumn)
                continue;
            matrix.setColumn(j++, extendMatrix.getColumn(i));
        }
        solutionColumn++;

        solver = new LUDecompositionImpl(matrix).getSolver();
    } while (!solver.isNonSingular());

    for (int i = 0; i < results.length; i++)
        results[i] *= -1;

    double[] partialSolution = solver.solve(results);

    double[] solution = new double[partialSolution.length + 1];

    for (int i = 0, j = 0; i < solution.length; i++)
        if (i == solutionColumn - 1)
            solution[i] = 1;
        else {
            solution[i] = (Kernel.isZero(partialSolution[j])) ? 0 : partialSolution[j];
            j++;
        }

    for (int i = 0, k = 0; i < realDegree + 1; i++)
        for (int j = 0; i + j < realDegree + 1; j++)
            coeffMatrix[i][j] = solution[k++];

    this.setCoeff(coeffMatrix, true);

    setDefined();
    for (int i = 0; i < points.size(); i++)
        if (!this.isOnPath(points.get(i), 1)) {
            this.setUndefined();
            return;
        }

}