Example usage for org.apache.commons.math.linear Array2DRowRealMatrix Array2DRowRealMatrix

List of usage examples for org.apache.commons.math.linear Array2DRowRealMatrix Array2DRowRealMatrix

Introduction

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

Prototype

public Array2DRowRealMatrix(final double[][] d, final boolean copyArray)
        throws IllegalArgumentException, NullPointerException 

Source Link

Document

Create a new RealMatrix using the input array as the underlying data array.

Usage

From source file:math2605.gn_qua.java

/**
 * @param args the command line arguments
 *///  ww w.j a  v  a 2s.c  o m
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix(pairs.size(), 1);
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix(pairs.size(), 3);
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());
}

From source file:math2605.gn_log.java

/**
 * @param args the command line arguments
 *//*ww  w.  ja va  2  s . c  om*/
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix();
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix();
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());
}

From source file:math2605.gn_exp.java

/**
 * @param args the command line arguments
 *//*from   www  .j  a  v  a  2s .c o m*/
public static void main(String[] args) {
    //get file name
    System.out.println("Please enter a file name:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    List<String[]> pairs = new ArrayList<>();
    //get coordinate pairs and add to arraylist
    try {
        BufferedReader br = new BufferedReader(new FileReader(fileName));
        String line;
        while ((line = br.readLine()) != null) {
            String[] pair = line.split(",");
            pairs.add(pair);
        }
        br.close();
    } catch (Exception e) {
    }

    System.out.println("Please enter the value of a:");
    double a = scanner.nextInt();
    System.out.println("Please enter the value of b:");
    double b = scanner.nextInt();
    System.out.println("Please enter the value of c:");
    double c = scanner.nextInt();
    //init B, vector with 3 coordinates
    RealMatrix B = new Array2DRowRealMatrix(3, 1);
    B.setEntry(0, 0, a);
    B.setEntry(1, 0, b);
    B.setEntry(2, 0, c);

    System.out.println("Please enter the number of iteration for the Gauss-newton:");
    //init N, number of iterations
    int N = scanner.nextInt();

    //init r, vector of residuals
    RealMatrix r = new Array2DRowRealMatrix();
    setR(pairs, a, b, c, r);

    //init J, Jacobian of r
    RealMatrix J = new Array2DRowRealMatrix();
    setJ(pairs, a, b, c, r, J);

    System.out.println("J");
    System.out.println(J);
    System.out.println("r");
    System.out.println(r);

    RealMatrix sub = findQR(J, r);

    for (int i = N; i > 0; i--) {
        B = B.subtract(sub);
        double B0 = B.getEntry(0, 0);
        double B1 = B.getEntry(1, 0);
        double B2 = B.getEntry(2, 0);
        //CHANGE ABC TO USE B0, B1, B2
        setR(pairs, B0, B1, B2, r);
        setJ(pairs, B0, B1, B2, r, J);
    }
    System.out.println("B");
    System.out.println(B.toString());

}

From source file:fi.smaa.libror.r.RHelper.java

public static RealMatrix rArrayMatrixToRealMatrix(double[] matrix, int nRows) {
    if (nRows < 1) {
        throw new IllegalArgumentException("PRECOND violation: nRows < 1");
    }//from  ww  w. ja  v a2 s. c  o m
    int nCols = matrix.length / nRows;
    if (matrix.length != nRows * nCols) {
        throw new IllegalArgumentException("PRECOND violation: matrix.length != nRows * nCols");
    }
    RealMatrix perfMatrix = new Array2DRowRealMatrix(nRows, nCols);
    for (int i = 0; i < nRows; i++) {
        for (int j = 0; j < nCols; j++) {
            perfMatrix.setEntry(i, j, matrix[j * nRows + i]);
        }
    }
    return perfMatrix;
}

From source file:math2605.MatrixMethods.java

public RealMatrix inverseMatrix() {
    RealMatrix inverse = new Array2DRowRealMatrix(m.getRowDimension(), m.getColumnDimension());
    //RealMatrix inverse = new AbstractRealMatrix(m.getRowDimension(), m.getColumnDimension());
    double[][] r = invert(m.getData());
    int row = r[0].length;
    int col = r.length;
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            inverse.setEntry(i, j, r[i][j]);
        }//  w  w  w .ja  va2s  .  com
    }
    return inverse;
}

From source file:MathFunctions.java

public static AffineTransform generateAffineTransformFromPointPairs(
        Map<Point2D.Double, Point2D.Double> pointPairs) {
    RealMatrix u = new Array2DRowRealMatrix(pointPairs.size(), 3);
    RealMatrix v = new Array2DRowRealMatrix(pointPairs.size(), 3);

    // Create u (source) and v (dest) matrices whose row vectors
    // are [x,y,1] for each Point2D.Double:

    int i = 0;//from w w w. jav a  2  s. c om
    for (Map.Entry pair : pointPairs.entrySet()) {
        Point2D.Double uPt = (Point2D.Double) pair.getKey();
        Point2D.Double vPt = (Point2D.Double) pair.getValue();

        insertPoint2DInMatrix(u, uPt, i);
        insertPoint2DInMatrix(v, vPt, i);

        i++;
    }
    // Find the 3x3 linear least squares solution to u*m'=v
    // (the last row should be [0,0,1]):
    DecompositionSolver solver = (new QRDecompositionImpl(u)).getSolver();
    double[][] m = solver.solve(v).transpose().getData();

    // Create an AffineTransform object from the elements of m
    // (the last row is omitted as specified in AffineTransform class):
    return new AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][2], m[1][2]);
}

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.UTAGMSSolverTest.java

@Before
public void setUp() {
    perfMatrix = new Array2DRowRealMatrix(3, 3);
    perfMatrix.setRow(0, new double[] { 1.0, 1.0, 1.0 });
    perfMatrix.setRow(1, new double[] { 2.0, 1.0, 1.1 });
    perfMatrix.setRow(2, new double[] { 2.0, 0.5, 3.0 });
    solver = new UTAGMSSolver(new RORModel(new PerformanceMatrix(perfMatrix)));
    solver.getModel().addPreference(2, 1); // a3 > a2
}

From source file:juicebox.tools.utils.common.MatrixTools.java

/**
 * @return matrix of size m x n initialized with a specified value
 *//*from w ww.  ja v a  2 s .  co m*/
private static RealMatrix presetValueMatrix(int numRows, int numCols, int val) {
    RealMatrix matrix = new Array2DRowRealMatrix(numRows, numCols);
    for (int r = 0; r < numRows; r++)
        for (int c = 0; c < numCols; c++)
            matrix.setEntry(r, c, val);
    return matrix;
}