List of usage examples for org.apache.commons.math3.linear BlockRealMatrix getRowDimension
@Override public int getRowDimension()
From source file:jmbench.PackageMatrixConversion.java
public static void convertToEjml(final BlockRealMatrix src, final DenseMatrix64F dst) { if ((src.getRowDimension() != dst.getNumRows()) || (src.getColumnDimension() != dst.getNumCols())) { throw new IllegalArgumentException("Matrices are not the same shape"); }/* ww w . j a v a 2 s. c o m*/ for (int y = 0; y < src.getRowDimension(); y++) { for (int x = 0; x < src.getColumnDimension(); x++) { dst.set(y, x, src.getEntry(y, x)); } } }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Prints matrix values in the console./*from w w w. j a va 2s.c o m*/ * @param m - matrix to print */ public static void matrixPrint(final BlockRealMatrix m) { for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { System.out.print(m.getEntry(i, j)); System.out.print(" "); } System.out.println(); } System.out.println(" "); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Get main diagonal of the matrix.//w ww . ja va 2 s .co m * @param m - matrix * @return main diagonal as vector */ public static ArrayRealVector getMainDiagonal(final BlockRealMatrix m) { double[] tempArr = new double[m.getRowDimension()]; for (int i = 0; i < m.getColumnDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { if (i == j) { tempArr[i] = m.getEntry(i, j); } } } return new ArrayRealVector(tempArr, false); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * if the length of the return matrix is zero, * then the diff was not completed properly. * @param data - matrix/*from w ww . j a v a 2s . c o m*/ * @return - matrix */ private static BlockRealMatrix getDiff(final BlockRealMatrix data) { double[][] newdata = new double[data.getRowDimension() - 1][data.getColumnDimension()]; for (int i = 0; i < newdata.length; i++) { for (int j = 0; j < newdata[i].length; j++) { newdata[i][j] = data.getEntry(i + 1, j) - data.getEntry(i, j); } } return new BlockRealMatrix(newdata); }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Multiply vector with every row of the matrix. * @param v - vector/*from w ww .java2 s. c o m*/ * @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:gamlss.utilities.MatrixFunctions.java
/** * if the length of the return matrix is zero, * then the function was not completed properly. * Check the 'diff' operator. /*w ww .j a va 2 s. com*/ * @param data - matrix * @param diff - difference value * @return matrix */ public static BlockRealMatrix diff(final BlockRealMatrix data, final int diff) { BlockRealMatrix newdata = new BlockRealMatrix(data.getRowDimension() - 1, data.getColumnDimension()); for (int i = 0; i < diff; i++) { if (i == 0) { newdata = getDiff(data); } else { newdata = getDiff(newdata); } } return newdata; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Append rows of the matrices./*from w ww .j a v a 2 s . c o m*/ * @param m1 - first matrix * @param m2 - second matrix * @return m1.append.m2 */ public static BlockRealMatrix appendMatricesRows(final BlockRealMatrix m1, final BlockRealMatrix m2) { BlockRealMatrix out = new BlockRealMatrix(m1.getRowDimension(), m1.getColumnDimension() + m2.getColumnDimension()); for (int i = 0; i < m1.getRowDimension(); i++) { out.setRowVector(i, m1.getRowVector(i).append(m2.getRowVector(i))); } return out; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * Write matrix values to CSV file.//from w ww . ja va 2s . c o m * @param cmd - path to the file * @param m - matrix to write */ public static void matrixWriteCSV(final String cmd, final BlockRealMatrix m) { try { // Create file FileWriter fstream = new FileWriter(cmd, false); BufferedWriter out = new BufferedWriter(fstream); for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { out.write(Double.toString(m.getEntry(i, j))); if (j < m.getColumnDimension() - 1) { out.append(','); } } out.newLine(); } out.close(); } catch (Exception e) { //Catch exception if any System.err.println("Error: " + e.getMessage()); } }
From source file:gamlss.smoothing.PB.java
/** * Constructs the base matrix./* w w w .j a v a 2 s .co m*/ * @param colValues - values of the certain column of * the smooth matrix which corresponds to the * currently fitting distribution parameter * @return - base matrix */ //bbase <- function(x, xl, xr, ndx, deg, quantiles=FALSE) private static BlockRealMatrix formX(final ArrayRealVector colValues) { //control$inter <- if (lx<99) 10 else control$inter # //this is to prevent singularities when length(x) is small if (colValues.getDimension() < 99) { Controls.INTER = 10; } //xl <- min(x) double xl = colValues.getMinValue(); //xr <- max(x) double xr = colValues.getMaxValue(); //xmax <- xr + 0.01 * (xr - xl) double xmax = xr + 0.01 * (xr - xl); //xmin <- xl - 0.01 * (xr - xl) double xmin = xl - 0.01 * (xr - xl); //dx <- (xr - xl) / ndx double dx = (xmax - xmin) / Controls.INTER; //if (quantiles) # if true use splineDesign if (Controls.QUANTILES) { //knots <- sort(c(seq(xl-deg*dx, xl, dx),quantile(x, //prob=seq(0, 1, length=ndx)), seq(xr, xr+deg*dx, dx))) ArrayRealVector kts = null; //B <- splineDesign(knots, x = x, outer.ok = TRUE, ord=deg+1) //return(B) return null; } else { //kts <- seq(xl - deg * dx, xr + deg * dx, by = dx) //ArrayRealVector kts = new ArrayRealVector( //ArithmeticSeries.getSeries(xl-deg*dx, xr+deg*dx, dx),false); rConnection.assingVar("min", new double[] { xmin - Controls.DEGREE * dx }); rConnection.assingVar("max", new double[] { xmax + Controls.DEGREE * dx }); rConnection.assingVar("step", new double[] { dx }); ArrayRealVector kts = new ArrayRealVector( rConnection.runEvalDoubles("knots <- seq(min, max, by = step)")); //P <- outer(x, kts, FUN = tpower, deg) BlockRealMatrix pM = MatrixFunctions.outertpowerPB(colValues, kts, Controls.DEGREE); //D <- diff(diag(dim(P)[2]), //diff = deg + 1) / (gamma(deg + 1) * dx ^ deg) BlockRealMatrix tempM = MatrixFunctions .diff(MatrixFunctions.buildIdentityMatrix(pM.getColumnDimension()), Controls.DEGREE + 1); double[][] tempArrArr = new double[tempM.getRowDimension()][tempM.getColumnDimension()]; for (int i = 0; i < tempArrArr.length; i++) { for (int j = 0; j < tempArrArr[i].length; j++) { tempArrArr[i][j] = tempM.getEntry(i, j) / ((FastMath.exp(Gamma.logGamma(Controls.DEGREE + 1))) * FastMath.pow(dx, Controls.DEGREE)); } } tempM = new BlockRealMatrix(tempArrArr); //B <- (-1) ^ (deg + 1) * P %*% t(D) return (BlockRealMatrix) pM.multiply(tempM.transpose()) .scalarMultiply(FastMath.pow(-1, (Controls.DEGREE + 1))); } }
From source file:com.itemanalysis.psychometrics.mixture.MvNormalComponentDistributionTest.java
/** * This test computes mean vector and covariance matrix for a single component mixture model. * The estimates should be the same as the sample mean and covariance. *//*w w w . ja va 2s. c o m*/ // @Test public void mixModel2Test() { System.out.println("Mixture model test with Two variables and two components"); /** * x contains 2,000 cases and three variables. * The first two variables are continuous data. The last variable is a group indicator variable */ double[][] x = testData.getMixtureData2(); BlockRealMatrix allData = new BlockRealMatrix(x); int nrow = allData.getRowDimension(); BlockRealMatrix group1Data = allData.getSubMatrix(0, 799, 0, 1); BlockRealMatrix group2Data = allData.getSubMatrix(800, 1999, 0, 1); VectorialMean trueMean1 = new VectorialMean(2); VectorialMean trueMean2 = new VectorialMean(2); Covariance trueCov1 = new Covariance(group1Data); Covariance trueCov2 = new Covariance(group2Data); for (int i = 0; i < group1Data.getRowDimension(); i++) { trueMean1.increment(group1Data.getRow(i)); } for (int i = 0; i < group2Data.getRowDimension(); i++) { trueMean2.increment(group2Data.getRow(i)); } double[] trueMeanGroup1 = trueMean1.getResult(); double[] trueMeanGroup2 = trueMean2.getResult(); /** * actual data in first two columns */ BlockRealMatrix data = allData.getSubMatrix(0, nrow - 1, 0, 1); //run two group mixture model int startGroup = 2; int endGroup = 2; int maxIter = 500; double converge = 1e-12; int starts = 100; MvNormalMixtureModel model = null; model = new MvNormalMixtureModel(data, endGroup); model.setModelConstraints(true, true, true, true); model.setEmOptions(maxIter, converge, starts); model.call(); // System.out.println(model.printResults()); double[] pi = new double[2]; pi[0] = model.getMixingProportion(0); pi[1] = model.getMixingProportion(1); int[] index = { 0, 1 }; //group 1 should be the smaller group. If not, reverse indexes. if (pi[0] > pi[1]) { index[0] = 1; index[1] = 0; } //check convergence System.out.println(" Testing convergence..."); assertTrue("Convergence test", model.converged()); //check mixing proportions System.out.println(" Testing proportions..."); assertEquals("Proportion 1 test: ", 0.40, pi[index[0]], 5e-2); assertEquals("Proportion 2 test: ", 0.60, pi[index[1]], 5e-2); //get estimated parameters double[] estMeanGroup1 = model.getMean(index[0]); double[] estMeanGroup2 = model.getMean(index[1]); double[][] estCovGroup1 = model.getCov(index[0]); double[][] estCovGroup2 = model.getCov(index[1]); //check mean vector for each group System.out.println(" Testing means..."); assertEquals("Mean (1,1) test", trueMeanGroup1[index[0]], estMeanGroup1[index[0]], 5e-2); assertEquals("Mean (1,2) test", trueMeanGroup1[index[1]], estMeanGroup1[index[1]], 5e-2); assertEquals("Mean (2,1) test", trueMeanGroup2[index[0]], estMeanGroup2[index[0]], 5e-2); assertEquals("Mean (2,2) test", trueMeanGroup2[index[1]], estMeanGroup2[index[1]], 5e-2); //check covariance matrix for group 1 System.out.println(" Testing covariances..."); double[][] trueCovGroup1 = trueCov1.getCovarianceMatrix().getData(); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals("CovGroup1[" + i + "," + j + "] test:", trueCovGroup1[i][j], estCovGroup1[i][j], 5e-2); } } //check covariance matrix for group 2 double[][] trueCovGroup2 = trueCov2.getCovarianceMatrix().getData(); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { assertEquals("CovGroup2[" + i + "," + j + "] test:", trueCovGroup2[i][j], estCovGroup2[i][j], 5e-2); } } }