List of usage examples for org.apache.commons.math3.linear RealVector toArray
public double[] toArray()
From source file:org.apache.solr.client.solrj.io.eval.UnitEvaluator.java
@Override public Object doWork(Object value) throws IOException { if (null == value) { return null; } else if (value instanceof Matrix) { Matrix matrix = (Matrix) value;/* w ww. j av a 2s . c om*/ double[][] data = matrix.getData(); double[][] unitData = new double[data.length][]; for (int i = 0; i < data.length; i++) { double[] row = data[i]; ArrayRealVector vector = new ArrayRealVector(row); double[] unitRow = vector.unitVector().toArray(); unitData[i] = unitRow; } return new Matrix(unitData); } else if (value instanceof List) { List<Number> values = (List<Number>) value; double[] doubles = new double[values.size()]; for (int i = 0; i < doubles.length; i++) { doubles[i] = values.get(i).doubleValue(); } ArrayRealVector vector = new ArrayRealVector(doubles); RealVector unitVector = vector.unitVector(); List<Number> unitList = new ArrayList(doubles.length); double[] unitArray = unitVector.toArray(); for (double d : unitArray) { unitList.add(d); } return unitList; } else { throw new IOException("The unit function expects either a numeric array or matrix as a parameter"); } }
From source file:org.drugis.addis.util.JSMAAintegration.NetworkBenefitRiskIT.java
private static void assertRelativeGaussian(final RelativeGaussianCriterionMeasurement expected, final RelativeGaussianCriterionMeasurement actual, final double d) { final GaussianMeasurement expectedBaseline = expected.getBaselineMeasurement(); final GaussianMeasurement actualBaseline = actual.getBaselineMeasurement(); assertEquals(expectedBaseline.getMean(), actualBaseline.getMean(), d); assertEquals(expectedBaseline.getStDev(), actualBaseline.getStDev(), d); final double[] expectedCovariance = flatten( expected.getRelativeMeasurement().getCovarianceMatrix().getData()); final double[] actualCovariance = flatten(actual.getRelativeMeasurement().getCovarianceMatrix().getData()); assertArrayEquals(expectedCovariance, actualCovariance, d); final RealVector expectedMeanVector = expected.getRelativeMeasurement().getMeanVector(); final RealVector actualMeanVector = actual.getRelativeMeasurement().getMeanVector(); assertArrayEquals(expectedMeanVector.toArray(), actualMeanVector.toArray(), d); }
From source file:org.jgrasstools.hortonmachine.modules.geomorphology.curvatures.OmsCurvaturesBivariate.java
/** * Calculates the parameters of a bivariate quadratic equation. * /*from w w w. ja v a 2 s . c om*/ * @param elevationValues the window of points to use. * @return the parameters of the bivariate quadratic equation as [a, b, c, d, e, f] */ private static double[] calculateParameters(final double[][] elevationValues) { int rows = elevationValues.length; int cols = elevationValues[0].length; int pointsNum = rows * cols; final double[][] xyMatrix = new double[pointsNum][6]; final double[] valueArray = new double[pointsNum]; // TODO check on resolution int index = 0; for (int y = 0; y < rows; y++) { for (int x = 0; x < cols; x++) { xyMatrix[index][0] = x * x; // x^2 xyMatrix[index][1] = y * y; // y^2 xyMatrix[index][2] = x * y; // xy xyMatrix[index][3] = x; // x xyMatrix[index][4] = y; // y xyMatrix[index][5] = 1; valueArray[index] = elevationValues[y][x]; index++; } } RealMatrix A = MatrixUtils.createRealMatrix(xyMatrix); RealVector z = MatrixUtils.createRealVector(valueArray); DecompositionSolver solver = new RRQRDecomposition(A).getSolver(); RealVector solution = solver.solve(z); // start values for a, b, c, d, e, f, all set to 0.0 final double[] parameters = solution.toArray(); return parameters; }
From source file:org.jgrasstools.lesto.modules.raster.Las2BivariateRasterMosaic.java
private double[] calculateParameters(final List<LasRecord> pointsInGeometry) { int pointsNum = pointsInGeometry.size(); final double[][] xyMatrix = new double[pointsNum][6]; final double[] valueArray = new double[pointsNum]; for (int i = 0; i < pointsNum; i++) { LasRecord dot = pointsInGeometry.get(i); xyMatrix[i][0] = dot.x * dot.x; // x^2 xyMatrix[i][1] = dot.y * dot.y; // y^2 xyMatrix[i][2] = dot.x * dot.y; // xy xyMatrix[i][3] = dot.x; // x xyMatrix[i][4] = dot.y; // y xyMatrix[i][5] = 1;/* ww w .j av a 2 s.c om*/ if (doIntensity) { valueArray[i] = dot.intensity; } else { valueArray[i] = dot.z; } } RealMatrix A = MatrixUtils.createRealMatrix(xyMatrix); RealVector z = MatrixUtils.createRealVector(valueArray); DecompositionSolver solver = new RRQRDecomposition(A).getSolver(); RealVector solution = solver.solve(z); // start values for a, b, c, d, e, f, all set to 0.0 final double[] parameters = solution.toArray(); return parameters; }
From source file:org.knime.al.util.noveltydetection.knfst.MultiClassKNFST.java
private NoveltyScores score(final RealMatrix kernelMatrix) { final RealMatrix projectionVectors = kernelMatrix.transpose().multiply(m_projection); // squared euclidean distances to target points: final RealMatrix squared_distances = squared_euclidean_distances(projectionVectors, m_targetPoints); // novelty scores as minimum distance to one of the target points final RealVector scoreVector = MatrixFunctions.sqrt(MatrixFunctions.rowMins(squared_distances)); return new NoveltyScores(scoreVector.toArray(), projectionVectors); }
From source file:org.knime.al.util.noveltydetection.knfst.OneClassKNFST.java
private NoveltyScores score(final RealMatrix kernelMatrix) { // projected test samples: final RealMatrix projectionVectors = kernelMatrix.transpose().multiply(m_projection); // differences to the target value: final RealMatrix diff = projectionVectors.subtract(MatrixFunctions .ones(kernelMatrix.getColumnDimension(), 1).scalarMultiply(m_targetPoints.getEntry(0, 0))); // distances to the target value: final RealVector scoresVector = MatrixFunctions .sqrt(MatrixFunctions.rowSums(MatrixFunctions.multiplyElementWise(diff, diff))); return new NoveltyScores(scoresVector.toArray(), projectionVectors); }
From source file:org.lambda3.indra.core.function.PearsonRelatednessFunction.java
@Override public double sim(RealVector r1, RealVector r2, boolean sparse) { return pearsonsCorrelation.correlation(r1.toArray(), r2.toArray()); }
From source file:org.lambda3.indra.core.function.SpearmanRelatednessFunction.java
@Override public double sim(RealVector r1, RealVector r2, boolean sparse) { return spearmansCorrelation.correlation(r1.toArray(), r2.toArray()); }
From source file:org.lambda3.indra.core.impl.PearsonClient.java
@Override protected double sim(RealVector r1, RealVector r2, boolean sparse) { return pearsonsCorrelation.correlation(r1.toArray(), r2.toArray()); }
From source file:org.lambda3.indra.core.impl.SpearmanClient.java
@Override protected double sim(RealVector r1, RealVector r2, boolean sparse) { return spearmansCorrelation.correlation(r1.toArray(), r2.toArray()); }