List of usage examples for org.apache.commons.math3.linear RealVector dotProduct
public double dotProduct(RealVector v) throws DimensionMismatchException
From source file:org.lenskit.mf.svd.DotProductKernel.java
@Override public double apply(double bias, @Nonnull RealVector user, @Nonnull RealVector item) { return bias + user.dotProduct(item); }
From source file:org.lenskit.pf.HPFItemScorer.java
@Nonnull @Override// ww w. j ava2 s. c om public ResultMap scoreWithDetails(long user, @Nonnull Collection<Long> items) { RealVector uvec = model.getUserVector(user); if (uvec == null) { return Results.newResultMap(); } List<Result> results = new ArrayList<>(items.size()); LongIterator iter = LongIterators.asLongIterator(items.iterator()); while (iter.hasNext()) { long item = iter.nextLong(); RealVector ivec = model.getItemVector(item); if (ivec != null) { double score = uvec.dotProduct(ivec); if (isProbPrediction) { score = 1 - Math.exp(-score); } results.add(Results.create(item, score)); } } return Results.newResultMap(results); }
From source file:org.rhwlab.dispim.nucleus.BHCNucleusData.java
public String getRadiusLabel(int i) { // find the adjusted eigenvector closest to the original unadjusted eigenvector for dimension i // this keeps the order of the adjusted eigenvectors the same as the original unadjusted eigenvectors // the eigendecomposition returns the eigenvectors sorted by eigenvalue // this procedure puts them back in their original order int adjustedI = 0; double maxD = 0.0; RealVector aV = eigenA.getEigenvector(i); for (int j = 0; j < A.getColumnDimension(); ++j) { RealVector v = adjustedEigenA.getEigenvector(j); double d = Math.abs(v.dotProduct(aV)); if (d > maxD) { maxD = d;// ww w .j av a 2 s . c o m adjustedI = j; } } RealVector v = adjustedEigenA.getEigenvector(adjustedI); // double eigenVal = adjustedEigenA.getRealEigenvalue(adjustedI); // double r = 1.0/Math.sqrt(Ace3D_Frame.R*eigenVal); double r = this.getRadius(adjustedI); return String.format("%4.1f(%.2f,%.2f,%.2f)", r, v.getEntry(0), v.getEntry(1), v.getEntry(2)); }
From source file:org.rhwlab.dispim.nucleus.Division.java
private boolean related(RealVector axis1, RealVector axis2) { double cos = axis1.dotProduct(axis2); boolean ret = cos >= cosThresh; if (!ret) {/*from ww w. j a va 2 s . c o m*/ System.out.printf("Cosine: %f\n", cos); } return ret; }
From source file:org.rhwlab.dispim.nucleus.NucleusData.java
public double prob(double[] p) { RealVector v = new ArrayRealVector(p); double d2 = Math.sqrt(this.adjustedEigenA.getDeterminant()); double ex = -0.5 * v.dotProduct(adjustedA.operate(v)); return d2 * Math.exp(ex); }
From source file:org.ssascaling.model.timeseries.learner.apache.OLSMultipleLinearRegression.java
/** * Returns the sum of squared residuals. * * @return residual sum of squares//from w w w .j a va 2 s .co m * @since 2.2 */ public double calculateResidualSumOfSquares() { final RealVector residuals = calculateResiduals(); return residuals.dotProduct(residuals); }
From source file:playground.sergioo.facilitiesGenerator2012.WorkFacilitiesGeneration.java
private static Set<PointPerson> getPCATransformation(Collection<PointPerson> points) { RealMatrix pointsM = new Array2DRowRealMatrix(points.iterator().next().getDimension(), points.size()); int k = 0;//w w w.j av a 2 s . com for (PointND<Double> point : points) { for (int f = 0; f < point.getDimension(); f++) pointsM.setEntry(f, k, point.getElement(f)); k++; } RealMatrix means = new Array2DRowRealMatrix(pointsM.getRowDimension(), 1); for (int r = 0; r < means.getRowDimension(); r++) { double mean = 0; for (int c = 0; c < pointsM.getColumnDimension(); c++) mean += pointsM.getEntry(r, c) / pointsM.getColumnDimension(); means.setEntry(r, 0, mean); } RealMatrix deviations = new Array2DRowRealMatrix(pointsM.getRowDimension(), pointsM.getColumnDimension()); for (int r = 0; r < deviations.getRowDimension(); r++) for (int c = 0; c < deviations.getColumnDimension(); c++) deviations.setEntry(r, c, pointsM.getEntry(r, c) - means.getEntry(r, 0)); RealMatrix covariance = deviations.multiply(deviations.transpose()) .scalarMultiply(1 / (double) pointsM.getColumnDimension()); EigenDecomposition eigenDecomposition = new EigenDecomposition(covariance, 0); RealMatrix eigenVectorsT = eigenDecomposition.getVT(); RealVector eigenValues = new ArrayRealVector(eigenDecomposition.getD().getRowDimension()); for (int r = 0; r < eigenDecomposition.getD().getRowDimension(); r++) eigenValues.setEntry(r, eigenDecomposition.getD().getEntry(r, r)); for (int i = 0; i < eigenValues.getDimension(); i++) { for (int j = i + 1; j < eigenValues.getDimension(); j++) if (eigenValues.getEntry(i) < eigenValues.getEntry(j)) { double tempValue = eigenValues.getEntry(i); eigenValues.setEntry(i, eigenValues.getEntry(j)); eigenValues.setEntry(j, tempValue); RealVector tempVector = eigenVectorsT.getRowVector(i); eigenVectorsT.setRowVector(i, eigenVectorsT.getRowVector(j)); eigenVectorsT.setRowVector(j, tempVector); } eigenVectorsT.setRowVector(i, eigenVectorsT.getRowVector(i).mapMultiply(Math.sqrt(1 / eigenValues.getEntry(i)))); } RealVector standardDeviations = new ArrayRealVector(pointsM.getRowDimension()); for (int r = 0; r < covariance.getRowDimension(); r++) standardDeviations.setEntry(r, Math.sqrt(covariance.getEntry(r, r))); double zValue = standardDeviations.dotProduct(new ArrayRealVector(pointsM.getRowDimension(), 1)); RealMatrix zScore = deviations.scalarMultiply(1 / zValue); pointsM = eigenVectorsT.multiply(zScore); Set<PointPerson> pointsC = new HashSet<PointPerson>(); k = 0; for (PointPerson point : points) { PointPerson pointC = new PointPerson(point.getId(), point.getOccupation(), new Double[] { pointsM.getEntry(0, k), pointsM.getEntry(1, k) }, point.getPlaceType()); pointC.setWeight(point.getWeight()); pointsC.add(pointC); k++; } return pointsC; }
From source file:rcdemo.math.ClosedHermiteSpline.java
@Override public double compute(double t, int deriv) { int n = (int) length(); int k = (int) Math.floor(t); t = t - k;//from w w w .j a v a 2s.c o m k = k % n; if (k < 0) { k += n; } double t2 = t * t; double t3 = t2 * t; double d[]; switch (deriv) { case 0: d = new double[] { 2 * t3 - 3 * t2 + 1, t3 - 2 * t2 + t, -2 * t3 + 3 * t2, t3 - t2 }; break; case 1: d = new double[] { 6 * t2 - 6 * t, 3 * t2 - 4 * t + 1, -6 * t2 + 6 * t, 3 * t2 - 2 * t }; break; case 2: d = new double[] { 12 * t - 6, 6 * t - 4, -12 * t + 6, 6 * t - 2 }; break; default: throw new RuntimeException("Derivative requested to high"); } RealVector h = new ArrayRealVector(d); RealVector c = a.getColumnVector(k); return h.dotProduct(c); }
From source file:rcdemo.track.TrackODE.java
@Override public void computeSecondDerivatives(double t, double[] y, double[] yDot, double[] yDDot) { double s = y[0]; double dsdt = yDot[0]; RealVector x = track.getx(s);/* www . j a v a2 s. c om*/ RealVector dxds = track.getDxDs(s); RealVector ddxdss = track.getDDxDss(s); RealVector u = dxds; RealVector v = dxds.mapMultiply(dsdt); RealVector F = forceModel.getForce(x, v); double ddsdtt = F.subtract(ddxdss.mapMultiply(dsdt * dsdt)).dotProduct(u) / dxds.dotProduct(u); //double E = 0.5 * v.dotProduct(v) + forceModel.getPotentialEnergy(x, v); //System.out.format("%4.1f: %s %s %s\n", t, s, x, E); //System.out.format("%7.5f: %s %s %s\n", t, s, x, E); yDDot[0] = ddsdtt; }
From source file:rcdemo.track.TrackODE.java
public double getEnergy(double s, double dsdt) { RealVector x = track.getx(s);// w ww. jav a2 s . c o m RealVector dxds = track.getDxDs(s); RealVector v = dxds.mapMultiply(dsdt); double E = 0.5 * v.dotProduct(v) + forceModel.getPotentialEnergy(x, v); return E; }