List of usage examples for org.apache.commons.math3.linear RealMatrix getColumnDimension
int getColumnDimension();
From source file:com.joptimizer.optimizers.LPPresolverTest.java
private void doPresolving(double[] c, double[][] A, double[] b, double[] lb, double[] ub, double s, double[] expectedSolution, double expectedValue, double expectedTolerance) throws Exception { RealMatrix AMatrix = MatrixUtils.createRealMatrix(A); SingularValueDecomposition dec = new SingularValueDecomposition(AMatrix); int rankA = dec.getRank(); log.debug("p: " + AMatrix.getRowDimension()); log.debug("n: " + AMatrix.getColumnDimension()); log.debug("rank: " + rankA); LPPresolver lpPresolver = new LPPresolver(); lpPresolver.setNOfSlackVariables((short) s); lpPresolver.setExpectedSolution(expectedSolution);//this is just for test! //lpPresolver.setExpectedTolerance(expectedTolerance);//this is just for test! lpPresolver.presolve(c, A, b, lb, ub); int n = lpPresolver.getPresolvedN(); double[] presolvedC = lpPresolver.getPresolvedC().toArray(); double[][] presolvedA = lpPresolver.getPresolvedA().toArray(); double[] presolvedB = lpPresolver.getPresolvedB().toArray(); double[] presolvedLb = lpPresolver.getPresolvedLB().toArray(); double[] presolvedUb = lpPresolver.getPresolvedUB().toArray(); double[] presolvedYlb = lpPresolver.getPresolvedYlb().toArray(); double[] presolvedYub = lpPresolver.getPresolvedYub().toArray(); double[] presolvedZlb = lpPresolver.getPresolvedZlb().toArray(); double[] presolvedZub = lpPresolver.getPresolvedZub().toArray(); log.debug("n : " + n); log.debug("presolvedC : " + ArrayUtils.toString(presolvedC)); log.debug("presolvedA : " + ArrayUtils.toString(presolvedA)); log.debug("presolvedB : " + ArrayUtils.toString(presolvedB)); log.debug("presolvedLb : " + ArrayUtils.toString(presolvedLb)); log.debug("presolvedUb : " + ArrayUtils.toString(presolvedUb)); log.debug("presolvedYlb: " + ArrayUtils.toString(presolvedYlb)); log.debug("presolvedYub: " + ArrayUtils.toString(presolvedYub)); log.debug("presolvedZlb: " + ArrayUtils.toString(presolvedZlb)); log.debug("presolvedZub: " + ArrayUtils.toString(presolvedZub)); //check objective function double delta = expectedTolerance; RealVector presolvedX = MatrixUtils.createRealVector(lpPresolver.presolve(expectedSolution)); log.debug("presolved value: " + MatrixUtils.createRealVector(presolvedC).dotProduct(presolvedX)); RealVector postsolvedX = MatrixUtils.createRealVector(lpPresolver.postsolve(presolvedX.toArray())); double value = MatrixUtils.createRealVector(c).dotProduct(postsolvedX); assertEquals(expectedValue, value, delta); //check postsolved constraints for (int i = 0; i < lb.length; i++) { double di = Double.isNaN(lb[i]) ? -Double.MAX_VALUE : lb[i]; assertTrue(di <= postsolvedX.getEntry(i) + delta); }// ww w. j ava 2 s . com for (int i = 0; i < ub.length; i++) { double di = Double.isNaN(ub[i]) ? Double.MAX_VALUE : ub[i]; assertTrue(di + delta >= postsolvedX.getEntry(i)); } RealVector Axmb = AMatrix.operate(postsolvedX).subtract(MatrixUtils.createRealVector(b)); assertEquals(0., Axmb.getNorm(), expectedTolerance); //check presolved constraints assertEquals(presolvedLb.length, presolvedX.getDimension()); assertEquals(presolvedUb.length, presolvedX.getDimension()); AMatrix = MatrixUtils.createRealMatrix(presolvedA); RealVector bvector = MatrixUtils.createRealVector(presolvedB); for (int i = 0; i < presolvedLb.length; i++) { double di = Double.isNaN(presolvedLb[i]) ? -Double.MAX_VALUE : presolvedLb[i]; assertTrue(di <= presolvedX.getEntry(i) + delta); } for (int i = 0; i < presolvedUb.length; i++) { double di = Double.isNaN(presolvedUb[i]) ? Double.MAX_VALUE : presolvedUb[i]; assertTrue(di + delta >= presolvedX.getEntry(i)); } Axmb = AMatrix.operate(presolvedX).subtract(bvector); assertEquals(0., Axmb.getNorm(), expectedTolerance); //check rank(A): must be A pXn with rank(A)=p < n AMatrix = MatrixUtils.createRealMatrix(presolvedA); dec = new SingularValueDecomposition(AMatrix); rankA = dec.getRank(); log.debug("p: " + AMatrix.getRowDimension()); log.debug("n: " + AMatrix.getColumnDimension()); log.debug("rank: " + rankA); assertEquals(AMatrix.getRowDimension(), rankA); assertTrue(rankA < AMatrix.getColumnDimension()); }
From source file:com.clust4j.algo.HierarchicalAgglomerative.java
/** {@inheritDoc} */ @Override//from www. ja v a 2s.c o m public int[] predict(RealMatrix newData) { final int[] fit_labels = getLabels(); // throws the MNF exception if not fit final int numSamples = newData.getRowDimension(), n = newData.getColumnDimension(); // Make sure matches dimensionally if (n != this.data.getColumnDimension()) throw new DimensionMismatchException(n, data.getColumnDimension()); /* * There's no great way to predict on a hierarchical * algorithm, so we'll treat this like a CentroidLearner, * create centroids from the k clusters formed, then * predict via the CentroidUtils. This works because * Hierarchical is not a NoiseyClusterer */ // CORNER CASE: num_clusters == 1, return only label (0) if (1 == num_clusters) return VecUtils.repInt(fit_labels[0], numSamples); return new NearestCentroidParameters().setMetric(this.dist_metric) // if it fails, falls back to default Euclidean... .setVerbose(false) // just to be sure in case default ever changes... .fitNewModel(this.getData(), fit_labels).predict(newData); }
From source file:edu.cmu.tetrad.search.IndTestMultiFisherZ.java
public boolean isIndependent(Node x, Node y, List<Node> z) { if (verbose) { System.out.println("\n" + x + " _||_ " + y + " | " + z); }//from w w w . j a va 2 s . c om List<Node> aa = nodeMap.get(x); List<Node> bb = nodeMap.get(y); List<Node> cc = new ArrayList<Node>(); for (Node _z : z) { cc.addAll(nodeMap.get(_z)); } TetradMatrix submatrix = subMatrix(cov, aa, bb, cc); TetradMatrix inverse; int rank; try { inverse = submatrix.inverse(); rank = inverse.columns(); } catch (Exception e) { SingularValueDecomposition svd = new SingularValueDecomposition(submatrix.getRealMatrix()); RealMatrix _inverse = svd.getSolver().getInverse(); inverse = new TetradMatrix(_inverse, _inverse.getRowDimension(), _inverse.getColumnDimension()); rank = svd.getRank(); } final List<Double> pValues = new ArrayList<Double>(); List<Integer> _i = new ArrayList<Integer>(); List<Integer> _m = new ArrayList<Integer>(); TetradMatrix stdSubmatrix = TestHippocampusUtils.subset(stdData, aa, bb, cc); for (int i = 0; i < aa.size(); i++) { for (int m = 0; m < bb.size(); m++) { int j = aa.size() + m; double a = -1.0 * inverse.get(i, j); double v0 = inverse.get(i, i); double v1 = inverse.get(j, j); double b = sqrt(v0 * v1); double r = a / b; int dof = cov.getSampleSize() - 1 - rank; if (dof < 0) { // out.println("Negative dof: " + dof + " n = " + cov.getSampleSize() + " cols = " + inverse.columns()); dof = 0; } double t2 = moment22(stdSubmatrix, i, j); double z_ = sqrt(dof) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r)); double p = 2.0 * (1.0 - RandomUtil.getInstance().normalCdf(0, sqrt(t2), abs(z_))); // if (p < alpha) p = 0; // System.out.println("z = " + z_ + " p = " + p); pValues.add(p); _i.add(i); _m.add(m); } } List<Integer> indices = new ArrayList<Integer>(); for (int i = 0; i < pValues.size(); i++) { indices.add(i); } Collections.sort(indices, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return pValues.get(o1).compareTo(pValues.get(o2)); } }); List<Double> pValues2 = new ArrayList<Double>(); List<Integer> _iIndices = new ArrayList<Integer>(); List<Integer> _mIndices = new ArrayList<Integer>(); for (int _y = 0; _y < indices.size(); _y++) { pValues2.add(pValues.get(indices.get(_y))); _iIndices.add(_i.get(indices.get(_y))); _mIndices.add(_m.get(indices.get(_y))); } int k = StatUtils.fdr(alpha, pValues2, false); double cutoff = StatUtils.fdrCutoff(alpha, pValues2, false); System.out.println("** cutoff = " + cutoff); int nonzero = -1; for (int i = 0; i < pValues2.size(); i++) { if (pValues2.get(i) != 0) { nonzero = i; break; } } double q = StatUtils.fdrQ(pValues2, nonzero); System.out.println("Q = " + q); // boolean independent = q >= alpha; //// boolean independent = k > nonzero; // System.out.println("k = " + k); System.out.println("nonzero = " + nonzero); System.out.println("fisher = " + fisherMethodP(pValues2)); // // boolean independent = k <= nonzero; double ratio = ((double) k) / pValues2.size(); System.out.println("ratio = " + ratio); boolean independent = ratio < 0.1; // System.out.println(independent ? "Independent" : "Dependent"); return independent; }
From source file:edu.ucdenver.bios.powersvc.resource.PowerResourceHelper.java
/** * Create a sigma outcomes/covariate matrix from the study design. * @param studyDesign study design object * @return sigma outcomes/covariate matrix *//* w w w. j a va2 s . c o m*/ public static RealMatrix sigmaOutcomesCovariateMatrixFromStudyDesign(StudyDesign studyDesign, RealMatrix sigmaG, RealMatrix sigmaY) { if (studyDesign.getViewTypeEnum() == StudyDesignViewTypeEnum.MATRIX_MODE) { return toRealMatrix(studyDesign.getNamedMatrix(PowerConstants.MATRIX_SIGMA_OUTCOME_GAUSSIAN)); } else { RealMatrix sigmaYG = toRealMatrix( studyDesign.getNamedMatrix(PowerConstants.MATRIX_SIGMA_OUTCOME_GAUSSIAN)); /* * In guided mode, sigmaYG is specified as correlation values. We adjust * to make it into a covariance matrix. We also expand for clustering */ if (sigmaYG != null) { /* * Make into a covariance. We first make sure the other sigma matrices are * of appropriate dimension to allow this */ if (sigmaG == null || sigmaG.getRowDimension() <= 0 || sigmaG.getColumnDimension() <= 0) { throw new IllegalArgumentException("Invalid covariance for Gaussian covariate"); } if (sigmaY == null || sigmaY.getRowDimension() < sigmaYG.getRowDimension() || sigmaY.getColumnDimension() != sigmaY.getRowDimension()) { throw new IllegalArgumentException("Invalid covariance for outcome"); } // assumes sigmaG is already updated to be a variance double varG = sigmaG.getEntry(0, 0); for (int row = 0; row < sigmaYG.getRowDimension(); row++) { double corrYG = sigmaYG.getEntry(row, 0); double varY = sigmaY.getEntry(row, row); sigmaYG.setEntry(row, 0, corrYG * Math.sqrt(varG * varY)); } // calculate cluster size List<ClusterNode> clusterNodeList = studyDesign.getClusteringTree(); if (clusterNodeList != null && clusterNodeList.size() > 0) { int totalRows = 1; for (ClusterNode node : clusterNodeList) { totalRows *= node.getGroupSize(); } // kronecker product the sigmaYG matrix with a matrix of ones to // generate the proper dimensions for a cluster sample RealMatrix oneMatrix = MatrixUtils.getRealMatrixWithFilledValue(totalRows, 1, 1); sigmaYG = MatrixUtils.getKroneckerProduct(oneMatrix, sigmaYG); } } return sigmaYG; } }
From source file:edu.cmu.tetrad.util.TetradMatrix.java
public TetradMatrix(RealMatrix matrix, int rows, int columns) { if (matrix == null) { throw new IllegalArgumentException("Null matrix."); }/* ww w . j av a2s. c o m*/ this.apacheData = matrix; this.m = rows; this.n = columns; int _rows = matrix.getRowDimension(); int _cols = matrix.getColumnDimension(); if (_rows != 0 && _rows != rows) throw new IllegalArgumentException(); if (_cols != 0 && _cols != columns) throw new IllegalArgumentException(); }
From source file:msi.gama.util.matrix.GamaFloatMatrix.java
IList<Double> fromApacheMatrixtoDiagList(final IScope scope, final RealMatrix rm) { final IList<Double> vals = GamaListFactory.create(Types.FLOAT); for (int i = 0; i < rm.getColumnDimension(); i++) { vals.add(rm.getEntry(i, i));//from www.ja v a 2s . c o m } return vals; }
From source file:com.joptimizer.algebra.QRSparseFactorizationTest.java
public void testDecompose() throws Exception { log.debug("testDecompose"); final double[][] A = new double[][] { { 1, 0, 0, 2 }, { 0, 0, 2, 0 }, { 2, 3, 0, 0 }, { 0, 0, 4, 4 } }; double[][] EQ = { { 0.447214, -0.894427, 0., 0. }, { 0., 0., 0.447214, -0.894427 }, { 0.894427, 0.447214, 0., 0. }, { 0., 0., 0.894427, 0.447214 } }; double[][] ER = { { 2.23607, 2.68328, 0., 0.894427 }, { 0., 1.34164, 0., -1.78885 }, { 0., 0., 4.47214, 3.57771 }, { 0., 0., 0., 1.78885 } }; QRDecomposition dFact = new QRDecomposition(new Array2DRowRealMatrix(A)); RealMatrix Q = dFact.getQ();/*from w w w .j a v a 2 s . c o m*/ RealMatrix R = dFact.getR(); RealMatrix H = dFact.getH(); log.debug("Q: " + ArrayUtils.toString(Q.getData())); log.debug("R: " + ArrayUtils.toString(R.getData())); //log.debug("H: " + ArrayUtils.toString(H.getData())); SparseDoubleMatrix2D S = new SparseDoubleMatrix2D(A); QRSparseFactorization qr = new QRSparseFactorization(S); qr.factorize(); log.debug("R: " + ArrayUtils.toString(qr.getR().toArray())); for (int i = 0; i < R.getRowDimension(); i++) { for (int j = 0; j < R.getColumnDimension(); j++) { assertEquals(ER[i][j], qr.getR().getQuick(i, j), 1.e-5); } } assertTrue(qr.hasFullRank()); }
From source file:msi.gama.util.matrix.GamaFloatMatrix.java
public IMatrix fromApacheMatrix(final IScope scope, final RealMatrix rm) { if (rm == null) { return null; }/*from w w w. ja v a 2 s . co m*/ final GamaFloatMatrix matrix = new GamaFloatMatrix(rm.getColumnDimension(), rm.getRowDimension()); for (int i = 0; i < numCols; i++) { for (int j = 0; j < numRows; j++) { matrix.set(scope, i, j, rm.getEntry(j, i)); } } return matrix; }
From source file:edu.cmu.tetrad.util.TetradMatrix1.java
public TetradMatrix1(RealMatrix matrix, int rows, int columns) { if (matrix == null) { throw new IllegalArgumentException("Null matrix."); }/*from w w w .j a v a 2 s.c o m*/ this.apacheData = matrix; this.m = rows; this.n = columns; int _rows = matrix.getRowDimension(); int _cols = matrix.getColumnDimension(); if (_rows != 0 && _rows != rows) throw new IllegalArgumentException(); if (_cols != 0 && _cols != columns) throw new IllegalArgumentException(); }
From source file:edu.cmu.tetrad.search.IndTestMultiCci.java
public boolean isIndependent(Node x, Node y, List<Node> z) { if (verbose) { System.out.println("\n" + x + " _||_ " + y + " | " + z); out.println("\n" + x + " _||_ " + y + " | " + z); }//w w w .ja v a 2 s . c o m List<Node> aa = nodeMap.get(x); List<Node> bb = nodeMap.get(y); // int[][] twod = new int[aa.size()][bb.size()]; List<Node> cc = new ArrayList<Node>(); for (Node _z : z) { cc.addAll(nodeMap.get(_z)); } TetradMatrix submatrix = TestHippocampusUtils.subMatrix(cov, aa, bb, cc); TetradMatrix inverse; int rank; try { inverse = submatrix.inverse(); rank = inverse.columns(); } catch (Exception e) { SingularValueDecomposition svd = new SingularValueDecomposition(submatrix.getRealMatrix()); RealMatrix _inverse = svd.getSolver().getInverse(); inverse = new TetradMatrix(_inverse, _inverse.getRowDimension(), _inverse.getColumnDimension()); rank = svd.getRank(); } final List<Double> pValues = new ArrayList<Double>(); List<Integer> _i = new ArrayList<Integer>(); List<Integer> _m = new ArrayList<Integer>(); System.out.println("# voxels for " + x.getName() + " = " + aa.size()); System.out.println("# voxels for " + y.getName() + " = " + bb.size()); System.out.println("# p values = " + aa.size() * bb.size()); IndTestConditionalCorrelation cciTest = new IndTestConditionalCorrelation(dataSet, alpha); for (int i = 0; i < aa.size(); i++) { for (int m = 0; m < bb.size(); m++) { int j = aa.size() + m; double a = -1.0 * inverse.get(i, j); double v0 = inverse.get(i, i); double v1 = inverse.get(j, j); double b = Math.sqrt(v0 * v1); double r = a / b; int dof = cov.getSampleSize() - 1 - rank; if (dof < 0) { out.println("Negative dof: " + dof + " n = " + cov.getSampleSize() + " cols = " + inverse.columns()); dof = 0; } double z_ = Math.sqrt(dof) * 0.5 * (Math.log(1.0 + r) - Math.log(1.0 - r)); double p = 2.0 * (1.0 - RandomUtil.getInstance().normalCdf(0, 1, abs(z_))); cciTest.isIndependent(aa.get(i), bb.get(m), cc); pValues.add(p); if (m == 0) { System.out.println("i = " + i + " m = " + m + " p = " + p); } _i.add(i); _m.add(m); } } List<Integer> indices = new ArrayList<Integer>(); for (int i = 0; i < pValues.size(); i++) { indices.add(i); } Collections.sort(indices, new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return pValues.get(o1).compareTo(pValues.get(o2)); } }); List<Double> pValues2 = new ArrayList<Double>(); List<Integer> _iIndices = new ArrayList<Integer>(); List<Integer> _mIndices = new ArrayList<Integer>(); for (int _y = 0; _y < indices.size(); _y++) { pValues2.add(pValues.get(indices.get(_y))); _iIndices.add(_i.get(indices.get(_y))); _mIndices.add(_m.get(indices.get(_y))); } int k = StatUtils.fdr(alpha, pValues2, false); double cutoff = StatUtils.fdrCutoff(alpha, pValues2, false); int nonzero = -1; for (int i = 0; i < pValues2.size(); i++) { if (pValues2.get(i) != 0) { nonzero = i; break; } } boolean dependent = k > nonzero; boolean independent = !dependent; TetradMatrix xCoords = coords.get(x); TetradMatrix yCoords = coords.get(y); int[][][] X = TestHippocampusUtils.threeDView(_iIndices, k, xCoords, independent, nonzero); int[][][] Y = TestHippocampusUtils.threeDView(_mIndices, k, yCoords, independent, nonzero); all3D.add(X); all3D.add(Y); String fact = SearchLogUtils.independenceFact(x, y, z); // Printing out stuff for Ruben. First print out dependent voxels. // 2.Second file, contain a list of all the dependencies between voxels. // // 10 -- 50 // 30 -- 2 int[][][] Cx = getC(X); int[][][] Cy = getC(Y); out.println("\n\n" + fact); for (int g = independent ? nonzero : 0; g < k; g++) { int i = getIndex(_iIndices, Cx, g, coords.get(x)); int j = getIndex(_mIndices, Cy, g, coords.get(y)); if (i == -1 || j == -1) throw new IllegalArgumentException(); out.println(i + " -- " + j); } out.println(); // 1. First file, containing info of both ROIs and all their voxels. // Example: // // ROI_LABEL voxel_LABEL COORDINATES #Dependencies // ENT 10 -80 50 38 6 // CA1 50 -70 15 90 2 printDependencies(x, fact, X, Cx); printDependencies(y, fact, Y, Cy); // OK back to work. int xCount = countAboveThreshold(X, threshold); int yCount = countAboveThreshold(Y, threshold); System.out.println("Total above threshold count = " + (xCount + yCount)); out.println("Total above threshold count = " + (xCount + yCount)); boolean thresholdIndep = !(xCount > 0 && yCount > 0); String projection; projection = "Axial"; TestHippocampusUtils.printChart(X, xCoords, 0, 1, x.getName(), projection, fact, false, false, out, threshold); TestHippocampusUtils.printChart(Y, yCoords, 0, 1, y.getName(), projection, fact, false, false, out, threshold); projection = "Coronal"; TestHippocampusUtils.printChart(X, xCoords, 0, 2, x.getName(), projection, fact, true, false, out, threshold); TestHippocampusUtils.printChart(Y, yCoords, 0, 2, y.getName(), projection, fact, true, false, out, threshold); projection = "Saggital"; TestHippocampusUtils.printChart(X, xCoords, 1, 2, x.getName(), projection, fact, true, false, out, threshold); TestHippocampusUtils.printChart(Y, yCoords, 1, 2, y.getName(), projection, fact, true, false, out, threshold); if (thresholdIndep) { // if (independent) { if (verbose) { System.out.println("Independent"); out.println("Independent"); } out.flush(); return true; } else { if (verbose) { System.out.println("Dependent\n"); out.println("Dependent\n"); } out.flush(); return false; } }