List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix getDataRef
public double[][] getDataRef()
From source file:jurls.core.becca.Daisychain.java
public static double[] ravel(Array2DRowRealMatrix t, double[] x, double min, double max) { int size = t.getRowDimension() * t.getColumnDimension(); if ((x == null) || (x.length != size)) x = new double[size]; double[][] xd = t.getDataRef(); int p = 0;/*from ww w . j ava 2 s . c o m*/ double factor = min != max ? (1.0 / (max - min)) : 1.0; for (double[] row : xd) { for (int i = 0; i < row.length; i++) row[i] = (row[i] - min) * factor; System.arraycopy(row, 0, x, p, row.length); p += row.length; } return x; }
From source file:gamlss.utilities.MatrixFunctions.java
/** * <p>Compute the "hat" matrix./*from w w w.j ava 2 s.co m*/ * </p> * <p>The hat matrix is defined in terms of the design matrix X * by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup> * </p> * <p>The implementation here uses the QR decomposition to compute the * hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the * p-dimensional identity matrix augmented by 0's. This computational * formula is from "The Hat Matrix in Regression and ANOVA", * David C. Hoaglin and Roy E. Welsch, * <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. *@param m - matrix * @return the hat matrix */ public static RealMatrix calculateHat(final BlockRealMatrix m) { QRDecomposition qr = new QRDecomposition(m); // Create augmented identity matrix RealMatrix qM = qr.getQ(); final int p = qr.getR().getColumnDimension(); final int n = qM.getColumnDimension(); Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n); double[][] augIData = augI.getDataRef(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j && i < p) { augIData[i][j] = 1d; } else { augIData[i][j] = 0d; } } } // Compute and return Hat matrix return qM.multiply(augI).multiply(qM.transpose()); }
From source file:mr.go.sgfilter.SGFilter.java
/** * Computes Savitzky-Golay coefficients for given parameters * /* ww w.j a v a2 s . c om*/ * @param nl * numer of past data points filter will use * @param nr * number of future data points filter will use * @param degree * order of smoothin polynomial * @return Savitzky-Golay coefficients * @throws IllegalArgumentException * if {@code nl < 0} or {@code nr < 0} or {@code nl + nr < * degree} */ public static double[] computeSGCoefficients(int nl, int nr, int degree) { if (nl < 0 || nr < 0 || nl + nr < degree) throw new IllegalArgumentException("Bad arguments"); Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(degree + 1, degree + 1); double[][] a = matrix.getDataRef(); double sum; for (int i = 0; i <= degree; i++) { for (int j = 0; j <= degree; j++) { sum = (i == 0 && j == 0) ? 1 : 0; for (int k = 1; k <= nr; k++) sum += pow(k, i + j); for (int k = 1; k <= nl; k++) sum += pow(-k, i + j); a[i][j] = sum; } } double[] b = new double[degree + 1]; b[0] = 1; ArrayRealVector b1 = new ArrayRealVector(b); b1 = (ArrayRealVector) (new LUDecomposition(matrix)).getSolver().solve(b1); b = b1.toArray(); double[] coeffs = new double[nl + nr + 1]; for (int n = -nl; n <= nr; n++) { sum = b[0]; for (int m = 1; m <= degree; m++) sum += b[m] * pow(n, m); coeffs[n + nl] = sum; } return coeffs; }
From source file:SGFilterModified.java
/** * Computes Savitzky-Golay coefficients for given parameters * /* ww w . ja v a 2s . co m*/ * @param nl * numer of past data points filter will use * @param nr * number of future data points filter will use * @param degree * order of smoothin polynomial * @return Savitzky-Golay coefficients * @throws IllegalArgumentException * if {@code nl < 0} or {@code nr < 0} or {@code nl + nr < * degree} */ public static double[] computeSGCoefficients(int nl, int nr, int degree) { if (nl < 0 || nr < 0 || nl + nr < degree) throw new IllegalArgumentException("Bad arguments"); //create a matrix Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(degree + 1, degree + 1); double[][] a = matrix.getDataRef(); double sum; for (int i = 0; i <= degree; i++) { for (int j = 0; j <= degree; j++) { sum = (i == 0 && j == 0) ? 1 : 0; for (int k = 1; k <= nr; k++) sum += pow(k, i + j); for (int k = 1; k <= nl; k++) sum += pow(-k, i + j); a[i][j] = sum; } } double[] b = new double[degree + 1]; b[0] = 1; // make a vector RealVector bVec = new ArrayRealVector(b); // get LUDecompostion solver DecompositionSolver solver = new LUDecomposition(matrix).getSolver(); // Finally solve matrices bVec = solver.solve(bVec); double[] coeffs = new double[nl + nr + 1]; for (int n = -nl; n <= nr; n++) { sum = bVec.getEntry(0); for (int m = 1; m <= degree; m++) sum += bVec.getEntry(m) * pow(n, m); coeffs[n + nl] = sum; } return coeffs; }
From source file:com.clust4j.algo.MeanShift.java
/** * For testing...//from w ww . jav a 2s . c o m * @param data * @param quantile * @param sep * @param seed * @param parallel * @return */ final protected static double autoEstimateBW(Array2DRowRealMatrix data, double quantile, GeometricallySeparable sep, Random seed, boolean parallel) { return autoEstimateBW( new NearestNeighbors(data, new NearestNeighborsParameters((int) (data.getRowDimension() * quantile)).setSeed(seed) .setForceParallel(parallel)).fit(), data.getDataRef(), quantile, sep, seed, parallel, null); }
From source file:com.clust4j.algo.NearestNeighborsTests.java
@Test public void testBigWithParallelQuery() { final int k = 3; final Array2DRowRealMatrix big = TestSuite.getRandom(500, k); // need to reduce size for travis CI NearestNeighbors nn;/* w ww . j av a2 s.c o m*/ try { nn = new NearestNeighbors(big, new NearestNeighborsParameters(3).setVerbose(true).setForceParallel(true)).fit(); } catch (OutOfMemoryError | RejectedExecutionException e) { // don't propagate these... return; } // test query now... //final Neighborhood res = nn.tree.query(big.getData(), k, BaseNeighborsModel.DUAL_TREE_SEARCH, BaseNeighborsModel.SORT); //final Neighborhood par = nn.getNeighbors(big.getDataRef(), true); //assertTrue(res.equals(par)); }
From source file:com.clust4j.algo.NNHSTests.java
License:asdf
@Test public void testInfDist() { Array2DRowRealMatrix mat = new Array2DRowRealMatrix( MatUtils.reshape(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, 3, 3), false); KDTree k = new KDTree(mat, Distance.CHEBYSHEV); Neighborhood n = k.query(mat.getDataRef()); Neighborhood p = k.query(mat.getDataRef(), 1, false, true); assertTrue(n.equals(p));/*from w w w . j a v a 2 s .co m*/ assertTrue(n.equals(n)); assertFalse(n.equals("asdf")); Neighborhood res = new Neighborhood( new double[][] { new double[] { 0.0 }, new double[] { 0.0 }, new double[] { 0.0 } }, new int[][] { new int[] { 0 }, new int[] { 1 }, new int[] { 2 } }); assertTrue(n.equals(res)); final int[] corr = k.twoPointCorrelation(mat.getDataRef(), new double[] { 1, 2, 3 }); assertTrue(VecUtils.equalsExactly(corr, new int[] { 3, 3, 7 })); assertTrue(k.infinity_dist); }
From source file:org.apache.hadoop.hive.ql.abm.simulation.MCSimNode.java
public List<SimulationResult> simulate(IntArrayList[] requests) { init(requests);// w ww. j a v a 2s . co m if (dimension == 0) { return null; } if (parent == null) { return defaultSimulate(); } List<SimulationResult> parRet = parent.simulate(targets); if (parRet == null) { return defaultSimulate(); } List<SimulationResult> ret = new ArrayList<SimulationResult>(); for (SimulationResult res : parRet) { ArrayList<IntArrayList> condIds = res.lastCondId; boolean[] fake = new boolean[dimension]; double[] mu = new double[dimension]; double[][] A = new double[dimension][dimension]; double[][] B = new double[res.invSigma.getColumnDimension()][dimension]; double[] zero = new double[dimension]; for (int i = 0; i < within1.length; ++i) { IntArrayList cIds = condIds.get(i); within1[i].fill(cIds, fake, mu, A); InterDistOracle[] w2s = within2[i]; for (int j = i + 1; j < within2.length; ++j) { w2s[j].fillSym(cIds, condIds.get(j), fake, mu, A); } } int dif = between.size() - res.condIds.size(); double[] pmu = res.getMean(); for (int i = 0; i < targets.length; ++i) { int cum = 0; IntArrayList cIds = condIds.get(i); for (int k = 0; k < res.condIds.size(); ++k) { ArrayList<IntArrayList> condIds2 = res.condIds.get(k); InterDistOracle[] os = between.get(k + dif)[i]; for (int j = 0; j < os.length; ++j) { cum += os[j].fillAsym(cIds, condIds2.get(j), fake, mu, pmu, B, cum); } } } for (int k = 0, cum = 0; k < res.condIds.size(); ++k) { ArrayList<IntArrayList> pCIds = res.condIds.get(k); InterDistOracle[][] oss = between.get(k + dif); for (int j = 0; j < oss.length; ++j) { IntArrayList cIds = pCIds.get(j); InterDistOracle[] os = oss[j]; int off = 0; for (int i = 0; i < os.length; ++i) { off = os[i].fillAsym(cIds, condIds.get(i), fake, pmu, mu, B, cum); } cum += off; } } Array2DRowRealMatrix a = new Array2DRowRealMatrix(A); Array2DRowRealMatrix b = new Array2DRowRealMatrix(B); Array2DRowRealMatrix c = (Array2DRowRealMatrix) b.transpose(); Array2DRowRealMatrix tmp = c.multiply(res.invSigma); Array2DRowRealMatrix sigma = a.subtract(tmp.multiply(b)); double[] scale = correct(sigma.getDataRef()); MultivariateNormalDistribution dist = new MultivariateNormalDistribution(zero, sigma.getDataRef()); double[][] smpls = dist.sample(res.samples.size()); int pos = 0; for (double[] smpl : smpls) { if (scale != null) { restore(smpl, scale); } fixFake(fake, mu, smpl); double[] s = res.getSample(pos); subtract(s, pmu); add(smpl, tmp.operate(s)); res.samples.get(pos)[level] = smpl; ++pos; } res.means.add(mu); res.invSigma = new Array2DRowRealMatrix( new LUDecomposition(concat(a, c, b, res.invSigma)).getSolver().getInverse().getData()); dispatch(res, ret); } return ret; }
From source file:org.ssascaling.model.timeseries.learner.apache.OLSMultipleLinearRegression.java
/** * <p>Compute the "hat" matrix./* w ww . ja va2 s . co m*/ * </p> * <p>The hat matrix is defined in terms of the design matrix X * by X(X<sup>T</sup>X)<sup>-1</sup>X<sup>T</sup> * </p> * <p>The implementation here uses the QR decomposition to compute the * hat matrix as Q I<sub>p</sub>Q<sup>T</sup> where I<sub>p</sub> is the * p-dimensional identity matrix augmented by 0's. This computational * formula is from "The Hat Matrix in Regression and ANOVA", * David C. Hoaglin and Roy E. Welsch, * <i>The American Statistician</i>, Vol. 32, No. 1 (Feb., 1978), pp. 17-22. * * @return the hat matrix */ public RealMatrix calculateHat() { // Create augmented identity matrix RealMatrix Q = qr.getQ(); final int p = qr.getR().getColumnDimension(); final int n = Q.getColumnDimension(); Array2DRowRealMatrix augI = new Array2DRowRealMatrix(n, n); double[][] augIData = augI.getDataRef(); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j && i < p) { augIData[i][j] = 1d; } else { augIData[i][j] = 0d; } } } // Compute and return Hat matrix return Q.multiply(augI).multiply(Q.transpose()); }
From source file:syncleus.gremlann.topology.adjacency.RealMatrixAdjacency.java
public static void fill(final RealMatrix m, final double value) { if (m instanceof Array2DRowRealMatrix) { Array2DRowRealMatrix a = (Array2DRowRealMatrix) m; for (double[] n : a.getDataRef()) Arrays.fill(n, value); } else {/*from w w w . j a va 2 s . c om*/ for (int i = 0; i < m.getRowDimension(); i++) { for (int j = 0; j < m.getColumnDimension(); j++) { m.setEntry(i, j, value); } } } }