List of usage examples for org.apache.commons.math3.linear RealMatrix getEntry
double getEntry(int row, int column) throws OutOfRangeException;
From source file:ellipsoidFit.FitPoints.java
/** * Fit points to the polynomial expression Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz * + 2Fyz + 2Gx + 2Hy + 2Iz = 1 and determine the center and radii of the * fit ellipsoid.// w ww. j a v a 2s .c o m * * @param points * the points to be fit to the ellipsoid. */ public void fitEllipsoid(ArrayList<ThreeSpacePoint> points) { // Fit the points to Ax^2 + By^2 + Cz^2 + 2Dxy + 2Exz // + 2Fyz + 2Gx + 2Hy + 2Iz = 1 and solve the system. // v = (( d' * d )^-1) * ( d' * ones.mapAddToSelf(1)); RealVector v = solveSystem(points); // Form the algebraic form of the ellipsoid. RealMatrix a = formAlgebraicMatrix(v); // Find the center of the ellipsoid. center = findCenter(a); // Translate the algebraic form of the ellipsoid to the center. RealMatrix r = translateToCenter(center, a); // Generate a submatrix of r. RealMatrix subr = r.getSubMatrix(0, 2, 0, 2); // subr[i][j] = subr[i][j] / -r[3][3]). double divr = -r.getEntry(3, 3); for (int i = 0; i < subr.getRowDimension(); i++) { for (int j = 0; j < subr.getRowDimension(); j++) { subr.setEntry(i, j, subr.getEntry(i, j) / divr); } } // Get the eigenvalues and eigenvectors. EigenDecomposition ed = new EigenDecomposition(subr, 0); evals = ed.getRealEigenvalues(); evecs = ed.getEigenvector(0); evecs1 = ed.getEigenvector(1); evecs2 = ed.getEigenvector(2); // Find the radii of the ellipsoid. radii = findRadii(evals); }
From source file:hivemall.utils.math.MatrixUtilsTest.java
@Test public void testTridiagonalEigen() { // Tridiagonal Matrix RealMatrix T = new Array2DRowRealMatrix( new double[][] { new double[] { 40, 60, 0, 0 }, new double[] { 60, 10, 120, 0 }, new double[] { 0, 120, 10, 120 }, new double[] { 0, 0, 120, 10 } }); double[] eigvals = new double[4]; RealMatrix eigvecs = new Array2DRowRealMatrix(new double[4][4]); MatrixUtils.tridiagonalEigen(T, 2, eigvals, eigvecs); RealMatrix actual = eigvecs.multiply(eigvecs.transpose()); RealMatrix expected = new Array2DRowRealMatrix(new double[4][4]); for (int i = 0; i < 4; i++) { expected.setEntry(i, i, 1);//from w ww. j ava 2s. c o m } for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { Assert.assertEquals(expected.getEntry(i, j), actual.getEntry(i, j), 0.001d); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Varimax() { System.out.println("Varimax rotation test: m255 data"); /**//from w w w . j a v a2s .co m * True result obtained form R using GPArotation package */ double[][] true_Varimax = { { 0.2806153, 0.1303408, 0.7332729 }, { 0.2620153, 0.1714751, 0.7434001 }, { 0.3129422, 0.2735077, 0.6396424 }, { 0.2277365, 0.2707234, 0.5740148 }, { 0.3650465, 0.4339803, 0.5203103 }, { 0.3193267, 0.7036671, 0.2683278 }, { 0.2056908, 0.7280409, 0.1543441 }, { 0.2398168, 0.4488439, 0.2356959 }, { 0.4424600, 0.4637861, 0.3346261 }, { 0.3748683, 0.4973271, 0.1762550 }, { 0.7710955, 0.2932897, 0.3783441 }, { 0.6437897, 0.2491322, 0.2953552 }, }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.VARIMAX, false, 1000, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Varimax[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Olimin() { System.out.println("Oblimin rotation test: m255 data"); /**// w w w. java 2 s . c o m * True result obtained form R using GPArotation package */ double[][] true_Oblimin = { { 0.818982935, -0.086728237, -0.03014828 }, { 0.836948168, -0.028356336, 0.02072749 }, { 0.650083626, 0.102202784, -0.06494440 }, { 0.609805424, 0.145551832, 0.03455997 }, { 0.432053641, 0.310356386, -0.13539510 }, { 0.071824049, 0.723712703, -0.07186957 }, { -0.022431888, 0.822701356, 0.05997316 }, { 0.113162767, 0.434661061, -0.07100987 }, { 0.129672215, 0.353260863, -0.33144976 }, { -0.052966793, 0.454048208, -0.28827799 }, { 0.028011398, -0.006402917, -0.89108626 }, { -0.005289128, 0.004084979, -0.75198003 } }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.OBLIMIN, false, 60, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Oblimin[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255Quartimin() { System.out.println("Quartimin rotation test: m255 data"); /**// w w w. j a v a2 s . c o m * True result obtained form R using GPArotation package */ double[][] true_Quartimin = { { 0.818982935, -0.086728237, -0.03014828 }, { 0.836948168, -0.028356336, 0.02072749 }, { 0.650083626, 0.102202784, -0.06494440 }, { 0.609805424, 0.145551832, 0.03455997 }, { 0.432053641, 0.310356386, -0.13539510 }, { 0.071824049, 0.723712703, -0.07186957 }, { -0.022431888, 0.822701356, 0.05997316 }, { 0.113162767, 0.434661061, -0.07100987 }, { 0.129672215, 0.353260863, -0.33144976 }, { -0.052966793, 0.454048208, -0.28827799 }, { 0.028011398, -0.006402917, -0.89108626 }, { -0.005289128, 0.004084979, -0.75198003 } }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.QUARTIMIN, false, 500, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Quartimin[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotationTest.java
@Test public void testM255GeominOblique() { System.out.println("Oblique Geomin rotation test: m255 data"); /**/* ww w. j ava 2 s . co m*/ * True result obtained form R using GPArotation package */ double[][] true_Geomin_oblique = { { 0.814401032, -0.07246248, -0.02746737 }, { 0.831474480, -0.01326628, 0.02443654 }, { 0.646042722, 0.11864079, -0.05620512 }, { 0.604841472, 0.16035608, 0.04246413 }, { 0.429130847, 0.32919710, -0.11951339 }, { 0.068651498, 0.74475428, -0.04555539 }, { -0.026759588, 0.84183379, 0.08633271 }, { 0.111079049, 0.44919958, -0.05420552 }, { 0.130427210, 0.37116164, -0.31191986 }, { -0.051932818, 0.47010788, -0.26735188 }, { 0.036623600, 0.01140911, -0.87276177 }, { 0.002120624, 0.01879531, -0.73637231 }, }; double[][] true_Phi = { { 1.0000000, 0.5565973, -0.7280542 }, { 0.5565973, 1.0000000, -0.6808553 }, { -0.7280542, -0.6808553, 1.0000000 } }; RealMatrix L = new Array2DRowRealMatrix(m255MINRESLoadings); GPArotation gpa = new GPArotation(); RotationResults R = gpa.rotate(L, RotationMethod.GEOMIN_Q, false, 500, 1e-5); RealMatrix Lr = R.getFactorLoadings(); // System.out.println(R.toString()); for (int i = 0; i < Lr.getRowDimension(); i++) { for (int j = 0; j < Lr.getColumnDimension(); j++) { assertEquals(" loading: ", Precision.round(true_Geomin_oblique[i][j], 4), Precision.round(Lr.getEntry(i, j), 5), 1e-4); } } RealMatrix Phi = R.getPhi(); for (int i = 0; i < Phi.getRowDimension(); i++) { for (int j = 0; j < Phi.getColumnDimension(); j++) { assertEquals(" factor correlation: ", Precision.round(true_Phi[i][j], 4), Precision.round(Phi.getEntry(i, j), 5), 1e-4); } } }
From source file:edu.cudenver.bios.power.glmm.GLMMTestUnivariateRepeatedMeasures.java
/** * Ensure that the within subject contrast is orthonormal for all * UNIREP tests//from ww w . j a v a2s .co m */ protected void createOrthonormalU() { RealMatrix UtU = U.transpose().multiply(U); double upperLeft = UtU.getEntry(0, 0); if (upperLeft != 0) UtU = UtU.scalarMultiply(1 / upperLeft); RealMatrix diffFromIdentity = UtU.subtract( org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix(UtU.getRowDimension())); // get maximum absolute value in U'U double maxValue = Double.NEGATIVE_INFINITY; for (int r = 0; r < diffFromIdentity.getRowDimension(); r++) { for (int c = 0; c < diffFromIdentity.getColumnDimension(); c++) { double entryVal = Math.abs(diffFromIdentity.getEntry(r, c)); if (entryVal > maxValue) maxValue = entryVal; } } if (maxValue > Precision.SAFE_MIN) { // U'U matrix deviates from identity, so create a U matrix that is orthonormal // TODO: thus UNIREP tests may use a different U matrix than HLT/PBT/WLR tests??? // TODO: displayed matrix results are incorrect now? U = new GramSchmidtOrthonormalization(U).getQ(); debug("U replaced by orthonormal", U); } }
From source file:edu.cudenver.bios.power.test.paper.TestConditionalOrthogonalPolynomial2Factor.java
/** * Write the matrix to std out//from ww w . j a v a2 s . c om * @param m */ private void printMatrix(String title, RealMatrix m) { System.out.println(title); DecimalFormat Number = new DecimalFormat("#0.00000000000000000000"); for (int row = 0; row < m.getRowDimension(); row++) { for (int col = 0; col < m.getColumnDimension(); col++) { System.out.print(Number.format(m.getEntry(row, col)) + "\t"); } System.out.print("\n"); } }
From source file:edu.cmu.tetrad.search.Cci.java
private double distance(RealMatrix data, int[] yCols, int i, int j) { double sum = 0.0; for (int yCol : yCols) { double d = data.getEntry(i, yCol) - data.getEntry(j, yCol); sum += d * d;/*from w w w. j ava 2s . c om*/ } return sqrt(sum); }
From source file:com.itemanalysis.psychometrics.factoranalysis.GPArotation.java
/** * For debugging// w w w . ja v a 2s. c o m * * @param x a matrix to print * @param title title for output */ private void printMatrix(RealMatrix x, String title) { System.out.println("PRINTING MATRIX: " + title); for (int i = 0; i < x.getRowDimension(); i++) { for (int j = 0; j < x.getColumnDimension(); j++) { System.out.print(x.getEntry(i, j) + " "); } System.out.println(); } }