List of usage examples for org.apache.commons.math3.linear RealVector toArray
public double[] toArray()
From source file:org.lambda3.indra.core.IndraDriver.java
public final Map<String, double[]> getVectorsAsArray(List<String> terms, Params params) { Map<String, RealVector> inVectors = getVectors(terms, params); Map<String, double[]> outVectors = new HashMap<>(); for (String term : inVectors.keySet()) { RealVector realVector = inVectors.get(term); outVectors.put(term, realVector != null ? realVector.toArray() : null); }/*from ww w .j a v a 2 s . c om*/ return outVectors; }
From source file:org.lenskit.mf.funksvd.FunkSVDModelBuilder.java
private double realVectorSum(RealVector rv) { double total = 0; for (double i : rv.toArray()) { total += i;// ww w .j av a 2 s.com } return total; }
From source file:org.orekit.propagation.conversion.AbstractPropagatorConverter.java
/** Find the propagator that minimize the mean square error for a sample of {@link SpacecraftState states}. * @param initial initial estimation parameters (position, velocity, free parameters) * @return fitted parameters// w ww .j av a 2 s.co m * @exception OrekitException if propagator cannot be adapted * @exception MaxCountExceededException if maximal number of iterations is exceeded */ private double[] fit(final double[] initial) throws OrekitException, MaxCountExceededException { final MultivariateVectorFunction f = getObjectiveFunction(); final MultivariateMatrixFunction jac = getObjectiveFunctionJacobian(); final MultivariateJacobianFunction fJac = new MultivariateJacobianFunction() { /** {@inheritDoc} */ @Override public Pair<RealVector, RealMatrix> value(final RealVector point) { final double[] p = point.toArray(); return new Pair<RealVector, RealMatrix>(MatrixUtils.createRealVector(f.value(p)), MatrixUtils.createRealMatrix(jac.value(p))); } }; final LeastSquaresProblem problem = new LeastSquaresBuilder().maxIterations(maxIterations) .maxEvaluations(Integer.MAX_VALUE).model(fJac).target(target).weight(new DiagonalMatrix(weight)) .start(initial).checker(checker).build(); optimum = optimizer.optimize(problem); return optimum.getPoint().toArray(); }
From source file:org.rhwlab.variationalbayesian.OldFaithfulDataSource.java
public List<CentroidCluster<Clusterable>> cluster(int K) { KMeansPlusPlusClusterer clusterer = new KMeansPlusPlusClusterer(K); ArrayList<DoublePoint> points = new ArrayList<>(); for (RealVector v : data) { DoublePoint point = new DoublePoint(v.toArray()); points.add(point);//www. ja v a 2s . c o m } return clusterer.cluster(points); }
From source file:org.wallerlab.yoink.math.linear.CommonsMatrix.java
/** * Element-by-element multiplication of two 3D vectors. *//* w w w . j a va 2 s . co m*/ @Override public Matrix ebeMultiply(Matrix m) { double[] v1 = this.internalMatrix.getRow(0); RealVector vector1 = new ArrayRealVector(v1); double[] v2 = ((RealMatrix) m.getInternalMatrix()).getRow(0); RealVector vector2 = new ArrayRealVector(v2); RealVector vectorEBE = vector1.ebeMultiply(vector2); double[] vEBE = vectorEBE.toArray(); tempMatrix = MatrixUtils.createRealMatrix(1, 3); tempMatrix.setRow(0, vEBE); Matrix newMatrix = new CommonsMatrix(); newMatrix.setInternalMatrix(tempMatrix); return newMatrix; }
From source file:rcdemo.graphics.TrackHelper.java
public Vector toVector(RealVector v) { double[] x = v.toArray(); return va.fromDouble(x); }
From source file:se.toxbee.sleepfighter.challenge.math.LinearEquationProblem.java
private static RealVector multiply(RealMatrix matrix, RealVector vector) { double[] m1 = matrix.getRow(0); double[] m2 = matrix.getRow(1); double[] m3 = matrix.getRow(2); double[] v = vector.toArray(); return new ArrayRealVector(new double[] { m1[0] * v[0] + m1[1] * v[1] + m1[2] * v[2], m2[0] * v[0] + m2[1] * v[1] + m2[2] * v[2], m3[0] * v[0] + m3[1] * v[1] + m3[2] * v[2], });/*from ww w .j av a 2s . com*/ }
From source file:syncleus.gremlann.Graphs.java
public static double[] doubles(RealVector v) { if (v instanceof ArrayRealVector) return ((ArrayRealVector) v).getDataRef(); return v.toArray(); }
From source file:syncleus.gremlann.train.Backprop.java
/** convenience method which extracts the direct array from a RealVector if possible, avoiding an array copy when a copy isnt necessary */ public double[] array(RealVector v) { if (v instanceof ArrayRealVector) return ((ArrayRealVector) v).getDataRef(); return v.toArray(); }
From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheOptimizer.java
public MultivariateJacobianFunction createJacobianFunction() { final int size = coords[0].getSize(); final AFunction afn; final CoordinatesIterator it; final DoubleDataset vd, pvd; final double[][] dm = new double[size][n]; if (function instanceof AFunction) { afn = (AFunction) function;/* w w w . j a v a 2 s . c o m*/ it = afn.getIterator(coords); vd = (DoubleDataset) DatasetFactory.zeros(coords[0].getShapeRef(), Dataset.FLOAT64); pvd = vd.clone(); } else { afn = null; it = null; vd = null; pvd = null; } MultivariateJacobianFunction f = new MultivariateJacobianFunction() { @SuppressWarnings("null") @Override public Pair<RealVector, RealMatrix> value(RealVector point) { IMonitor monitor = function.getMonitor(); if (monitor != null && monitor.isCancelled()) { throw new IllegalMonitorStateException("Monitor cancelled"); } if (point instanceof ArrayRealVector) { setParameterValues(((ArrayRealVector) point).getDataRef()); } else { setParameterValues(point.toArray()); } final double[] dv; if (afn != null) { dv = vd.getData(); AFunction afn = (AFunction) function; afn.fillWithValues(vd, it); double[] pd = pvd.getData(); for (int i = 0; i < n; i++) { // assuming number of parameters is less than number of coordinates IParameter p = params.get(i); afn.fillWithPartialDerivativeValues(p, pvd, it); for (int j = 0; j < size; j++) { dm[j][i] = pd[j]; } } } else { dv = calculateValues().getData(); for (int i = 0; i < n; i++) { IParameter p = params.get(i); DoubleDataset dp = (DoubleDataset) DatasetUtils .cast(function.calculatePartialDerivativeValues(p, coords), Dataset.FLOAT64); double[] pd = dp.getData(); for (int j = 0; j < size; j++) { dm[j][i] = pd[j]; } } } return new Pair<RealVector, RealMatrix>(new ArrayRealVector(dv, false), new Array2DRowRealMatrix(dm, false)); } }; return f; }