List of usage examples for java.lang Math acos
public static double acos(double a)
From source file:sphericalGeo.SphericalDiffusionModel.java
public double getLogLikelihood(Node node, double[] start, double[] stop, double time) { if (node == null || node.getNr() == 0) { maxTau = 0;// w w w .j a va2 s . c o m meanTau = (tauCount == 0 ? 0 : sumTau / tauCount); tauCount = 0; sumTau = 0; } // if (time <= 1e-4) { // time = 1e-4; // } if (time <= 1e-20) { return -1e99; } // if (time <= 1e-4) { // return -20; // } if (fast) { return getLogLikelihood2(node, start, stop, time); } if (start[0] == stop[0] && start[1] == stop[1]) { return -1e100; } double latitude1 = start[0]; double longitude1 = start[1]; double theta1 = (latitude1) * Math.PI / 180.0; if (longitude1 < 0) longitude1 += 360; double phi1 = longitude1 * Math.PI / 180; double latitude2 = stop[0]; double longitude2 = stop[1]; double theta2 = (latitude2) * Math.PI / 180.0; if (longitude2 < 0) longitude2 += 360; double phi2 = longitude2 * Math.PI / 180; double Deltalambda = phi2 - phi1; // See http://en.wikipedia.org/wiki/Great-circle_distance#Formulas double angle = Math.acos( Math.sin(theta1) * Math.sin(theta2) + Math.cos(theta1) * Math.cos(theta2) * Math.cos(Deltalambda)); final double tau = time / precision.getValue(0); final double logN = calcLogN(tau, node); final double logP = 0.5 * Math.log(angle * sin(angle)) - Math.log(tau) + -angle * angle / (tau * 2.0); // double inverseVariance = precision.getValue(0) / time; // // See Equation (8) from http://arxiv.org/pdf/1303.1278v1.pdf // // logN normalising 'constant' // double logP = 0.5 * Math.log(angle * Math.sin(angle)) + 0.5 * Math.log(inverseVariance) -0.5 * angle*angle * inverseVariance; // //double logP = - 0.5 * angle*angle * inverseVariance; // System.err.println(start[0] + " " + start[1] + " -> " + stop[0] + " " + stop[1] + " => " + logP + " " + angle + " " + // (0.5 * Math.log(angle * Math.sin(angle))) + " " + ( -0.5 * angle*angle * inverseVariance) + " " + logN); return logP - logN; }
From source file:org.matsim.contrib.drt.routing.DefaultAccessEgressStopFinder.java
/** * @param stopLinkVector/*w w w .j a v a 2 s .c om*/ * @param destinationVector * @return */ private double calcHeading(double[] stopLinkVector, double[] destinationVector) { return Math.acos((stopLinkVector[0] * destinationVector[0] + stopLinkVector[1] * destinationVector[1]) // / (Math.sqrt(stopLinkVector[0] * stopLinkVector[0] + stopLinkVector[1] * stopLinkVector[1])// * Math.sqrt(destinationVector[0] * destinationVector[0] + destinationVector[1] * destinationVector[1]))); }
From source file:com.logicdrop.fordchallenge.api.services.IterisService.java
private double distanceFrom(List<String> item) { double lat = Double.parseDouble(item.get(0)); double lng = Double.parseDouble(item.get(1)); if (IncidentsActivity.getLocation() == null) return 0; double theta = lng - IncidentsActivity.getLocation().getLongitude(); double distance = Math.sin(deg2rad(lat)) * Math.sin(deg2rad(IncidentsActivity.getLocation().getLatitude())) + Math.cos(deg2rad(lat)) * Math.cos(deg2rad(IncidentsActivity.getLocation().getLatitude())) * Math.cos(deg2rad(theta)); distance = Math.acos(distance); distance = Math.toDegrees(distance); distance = distance * 60 * 1.1515;// ww w. j a va2 s.co m return distance; }
From source file:jat.core.cm.TwoBodyAPL.java
public double eccentricAnomaly(double ta) { double cta = Math.cos(ta); double e0 = Math.acos((e + cta) / (1.0 + e * cta)); return e0;/*from www .j a va2 s . c o m*/ }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("arc cosine") @ScalarFunction/*from w w w . ja v a 2 s . co m*/ @SqlType(StandardTypes.DOUBLE) public static double acos(@SqlType(StandardTypes.DOUBLE) double num) { return Math.acos(num); }
From source file:ExtendedGeneralPath.java
/** * This constructs an unrotated Arc2D from the SVG specification of an * Elliptical arc. To get the final arc you need to apply a rotation * transform such as:/*from ww w . j a va 2 s. co m*/ * * AffineTransform.getRotateInstance * (angle, arc.getX()+arc.getWidth()/2, arc.getY()+arc.getHeight()/2); */ public static Arc2D computeArc(double x0, double y0, double rx, double ry, double angle, boolean largeArcFlag, boolean sweepFlag, double x, double y) { // // Elliptical arc implementation based on the SVG specification notes // // Compute the half distance between the current and the final point double dx2 = (x0 - x) / 2.0; double dy2 = (y0 - y) / 2.0; // Convert angle from degrees to radians angle = Math.toRadians(angle % 360.0); double cosAngle = Math.cos(angle); double sinAngle = Math.sin(angle); // // Step 1 : Compute (x1, y1) // double x1 = (cosAngle * dx2 + sinAngle * dy2); double y1 = (-sinAngle * dx2 + cosAngle * dy2); // Ensure radii are large enough rx = Math.abs(rx); ry = Math.abs(ry); double Prx = rx * rx; double Pry = ry * ry; double Px1 = x1 * x1; double Py1 = y1 * y1; // check that radii are large enough double radiiCheck = Px1 / Prx + Py1 / Pry; if (radiiCheck > 1) { rx = Math.sqrt(radiiCheck) * rx; ry = Math.sqrt(radiiCheck) * ry; Prx = rx * rx; Pry = ry * ry; } // // Step 2 : Compute (cx1, cy1) // double sign = (largeArcFlag == sweepFlag) ? -1 : 1; double sq = ((Prx * Pry) - (Prx * Py1) - (Pry * Px1)) / ((Prx * Py1) + (Pry * Px1)); sq = (sq < 0) ? 0 : sq; double coef = (sign * Math.sqrt(sq)); double cx1 = coef * ((rx * y1) / ry); double cy1 = coef * -((ry * x1) / rx); // // Step 3 : Compute (cx, cy) from (cx1, cy1) // double sx2 = (x0 + x) / 2.0; double sy2 = (y0 + y) / 2.0; double cx = sx2 + (cosAngle * cx1 - sinAngle * cy1); double cy = sy2 + (sinAngle * cx1 + cosAngle * cy1); // // Step 4 : Compute the angleStart (angle1) and the angleExtent (dangle) // double ux = (x1 - cx1) / rx; double uy = (y1 - cy1) / ry; double vx = (-x1 - cx1) / rx; double vy = (-y1 - cy1) / ry; double p, n; // Compute the angle start n = Math.sqrt((ux * ux) + (uy * uy)); p = ux; // (1 * ux) + (0 * uy) sign = (uy < 0) ? -1.0 : 1.0; double angleStart = Math.toDegrees(sign * Math.acos(p / n)); // Compute the angle extent n = Math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy)); p = ux * vx + uy * vy; sign = (ux * vy - uy * vx < 0) ? -1.0 : 1.0; double angleExtent = Math.toDegrees(sign * Math.acos(p / n)); if (!sweepFlag && angleExtent > 0) { angleExtent -= 360f; } else if (sweepFlag && angleExtent < 0) { angleExtent += 360f; } angleExtent %= 360f; angleStart %= 360f; // // We can now build the resulting Arc2D in double precision // Arc2D.Double arc = new Arc2D.Double(); arc.x = cx - rx; arc.y = cy - ry; arc.width = rx * 2.0; arc.height = ry * 2.0; arc.start = -angleStart; arc.extent = -angleExtent; return arc; }
From source file:sceneGraph.Rot.java
public Quaternion slerp(double amount, Quaternion value1, Quaternion value2) { if (amount < 0.0) return value1; else if (amount > 1.0) return value2; double dot = value1.dotProduct(value2); double x2, y2, z2, w2; if (dot < 0.0) { dot = 0.0 - dot;//from w w w . java 2s . c o m x2 = 0.0 - value2.getQ1(); y2 = 0.0 - value2.getQ2(); z2 = 0.0 - value2.getQ3(); w2 = 0.0 - value2.getQ0(); } else { x2 = value2.getQ1(); y2 = value2.getQ2(); z2 = value2.getQ3(); w2 = value2.getQ0(); } double t1, t2; final double EPSILON = 0.0001; if ((1.0 - dot) > EPSILON) // standard case (slerp) { double angle = Math.acos(dot); double sinAngle = Math.sin(angle); t1 = Math.sin((1.0 - amount) * angle) / sinAngle; t2 = Math.sin(amount * angle) / sinAngle; } else // just lerp { t1 = 1.0 - amount; t2 = amount; } return new Quaternion((value1.getQ1() * t1) + (x2 * t2), (value1.getQ2() * t1) + (y2 * t2), (value1.getQ3() * t1) + (z2 * t2), (value1.getQ0() * t1) + (w2 * t2)); }
From source file:edu.snu.leader.discrete.simulator.GautraisConflictDecisionCalculator.java
private double calculateConflict(Decision decision) { Agent agent = decision.getAgent();/* w w w. ja va 2 s .c o m*/ Agent leader = decision.getLeader(); double Ci = 0.1; // calculate the leader's next location Vector2D leaderNextLocation = leader.getCurrentDestination().add(leader.getCurrentVelocity()); // calculate the sides of a triangle // calculate side from agent's preferred destination to leader's next double A = Vector2D.distance(agent.getPreferredDestination().getVector(), leaderNextLocation); // calculate side from agent's preferred destination to leader's current double B = Vector2D.distance(agent.getPreferredDestination().getVector(), leader.getCurrentLocation()); // calculate side from leader's current to leader's next double C = Vector2D.distance(leader.getCurrentLocation(), leaderNextLocation); // check if the leader is in the agent's preferred destination if (leader.getCurrentLocation() .distance1(agent.getPreferredDestination().getVector()) < _destinationSizeRadius) { Ci = 0.1; } // check if the leader is not moving else if (leader.getCurrentVelocity().equals(Vector2D.ZERO)) { Ci = .9; } else { double angle = 0.0; if (A <= 0 || B <= 0 || C <= 0) { // if a side is 0 then there is no triangle it is a line // if segment B is longer than C then the degree should be 180 if (B > C) { angle = 180; } // if the segment B is shorter than C then the degree should be // 0 else { angle = 0.0; } } // have three sides so use law of cosines else { // calculate angle between leader's current position and agent's // preferred destination by law of cosines double lawOfCosines = (Math.pow(A, 2) - Math.pow(B, 2) - Math.pow(C, 2)) / (-2 * B * C); // because of rounding error there can be lawOfCosines values // that are oh so slightly larger or smaller than 1 or -1 // this augments them to their correct values if (lawOfCosines < -1) { lawOfCosines = -1; } else if (lawOfCosines > 1) { lawOfCosines = 1; } angle = Math.acos(lawOfCosines); } // if angle is greater than 180 than it becomes 360 - angle if (angle > 180) { angle = 360 - angle; } // make it into degrees angle = angle * 180 / Math.PI; // calculate conflict Ci = angle / 180; } // prevent K value from becoming 0 if (Ci < .1) { Ci = .1; } else if (Ci > .9) { Ci = .9; } // set the conflict for the decision decision.setConflict(Ci); // return the conflict value for whatever needs to use it return Ci; }
From source file:etomica.virial.MCMoveClusterRingRegrowOrientation.java
public boolean doTrial() { weightOld = ((BoxCluster) box).getSampleCluster().value((BoxCluster) box); IVectorRandom e1 = (IVectorRandom) space.makeVector(); IVectorMutable ex = space.makeVector(); IVectorMutable ey = space.makeVector(); IVectorMutable ez = space.makeVector(); ex.setX(0, 1);//from www . jav a 2s . c o m ey.setX(1, 1); ez.setX(2, 1); IVectorMutable oldCenter = space.makeVector(); IVectorMutable newCenter = space.makeVector(); IMoleculeList molecules = box.getMoleculeList(); IOrientation3D[][] newOrientations = new IOrientation3D[molecules.getMoleculeCount()][P + 1]; double[] oldAlpha = new double[P]; newAlpha = new double[P]; double[] theta = new double[P]; int fromImage = 0; int toImage = 0; double uOld = 0; double uNew = 0; double pGenRatio = 1.00; IVectorMutable pVecOld = space.makeVector(); IVectorMutable pVecNew = space.makeVector(); int nMolecules = molecules.getMoleculeCount(); for (int i = 0; i < nMolecules; i++) { IMolecule molecule = molecules.getMolecule(i); IAtomList atoms = molecule.getChildList(); for (int j = 0; j < P; j++) { int prev = j - 1; if (prev < 0) prev = P - 1; AtomHydrogen jAtom = (AtomHydrogen) atoms.getAtom(j); AtomHydrogen jPrev = (AtomHydrogen) atoms.getAtom(prev); double distance = dist(jAtom, jPrev); uOld += kHarmonic * distance; } oldOrientations[i][0].setDirection(((IAtomOriented) atoms.getAtom(0)).getOrientation().getDirection()); IVectorRandom rV1 = (IVectorRandom) space.makeVector(); rV1.setRandomSphere(random); newOrientations[i][0] = (IOrientation3D) ((IAtomOriented) atoms.getAtom(0)).getOrientation(); newOrientations[i][0].setDirection(rV1); oldOrientations[i][P].setDirection(oldOrientations[i][0].getDirection()); newOrientations[i][P] = (IOrientation3D) space.makeOrientation(); newOrientations[i][P].setDirection(newOrientations[i][0].getDirection()); pVecOld.E(oldOrientations[i][0].getDirection()); pVecNew.E(newOrientations[i][0].getDirection()); for (int dr = 2; dr <= P; dr *= 2) { double kEff = 8 * kHarmonic * dr / P; double y0 = 0; double sA = 0; double kEff1Old = 0; double kEff1New = 0; double a = 0; double y1 = 0; for (int nr = 1; nr < dr; nr += 2) { int imageIndex = nr * P / dr; // System.out.println("image # = "+imageIndex); IAtomOriented jAtom = ((IAtomOriented) atoms.getAtom(imageIndex)); oldOrientations[i][imageIndex].setDirection(jAtom.getOrientation().getDirection()); newOrientations[i][imageIndex] = (IOrientation3D) jAtom.getOrientation(); fromImage = (nr - 1) * P / dr; toImage = (nr + 1) * P / dr; double r = 0; for (int k = fromImage; k <= toImage; k++) { if (k == P) { r += ((AtomHydrogen) atoms.getAtom(0)).getBondLength() / 2.0; } else { r += ((AtomHydrogen) atoms.getAtom(k)).getBondLength() / 2.0; } } r *= dr / (2.0 * P + dr); // same as r /= (toImage - fromImage + 1) if (imageIndex == P / 2 && doExchange) { pVecOld.TE(-1); pVecNew.TE(-1); oldOrientations[i][P].setDirection(pVecOld); newOrientations[i][P].setDirection(pVecNew); } else { oldCenter.Ev1Pv2(oldOrientations[i][fromImage].getDirection(), oldOrientations[i][toImage].getDirection()); oldCenter.normalize(); y0 = oldOrientations[i][fromImage].getDirection() .dot(oldOrientations[i][toImage].getDirection()); if (y0 > 1.0) y0 = 1.0; if (y0 < -1.0) y0 = -1.0; kEff1Old = kEff * r * r * Math.sqrt((1 + y0) / 2.0); y0 = newOrientations[i][fromImage].getDirection() .dot(newOrientations[i][toImage].getDirection()); if (y0 > 1.0) y0 = 1.0; if (y0 < -1.0) y0 = -1.0; kEff1New = kEff * r * r * Math.sqrt((1 + y0) / 2.0); y0 = oldCenter.dot(oldOrientations[i][imageIndex].getDirection()); if (y0 > 1.0) y0 = 1.0; if (y0 < -1.0) y0 = -1.0; oldAlpha[imageIndex] = Math.acos(y0); double x = random.nextDouble(); // double y1_new = (-kEff1New + Math.log(Math.exp(2*kEff1New)-x*(Math.exp(2*kEff1New)-1)))/kEff1New; a = Math.log(1 - x) / kEff1New + Math.log(1 + x * Math.exp(-2 * kEff1New) / (1 - x)) / kEff1New; // double a = Math.log(1 - xNew[i][imageIndex])/kEff1New + Math.log(1 + xNew[i][imageIndex]*Math.exp(-2*kEff1New)/(1-xNew[i][imageIndex]))/kEff1New; if (a > 0) { a = 0; } if (a < -2.0) { a = -2.0; } y1 = 1 + a; sA = Math.sqrt(-2 * a - a * a); if (Double.isNaN(sA)) throw new RuntimeException(a + " " + (2 * a + a * a)); newAlpha[imageIndex] = Math.acos(y1); if (newAlpha[imageIndex] != newAlpha[imageIndex] || y1 != y1) throw new RuntimeException("x = " + 2 * kEff1New); } if (imageIndex == P / 2) { newOrientations[i][imageIndex].setDirection(rV1); IVectorMutable rV2 = space.makeVector(); if (Math.abs(rV1.getX(0)) > 0.5) { rV2.setX(1, 1); } else { rV2.setX(0, 1); } rV2.PEa1Tv1(-rV2.dot(rV1), rV1); rV2.normalize(); double dummyAlpha = 2 * Math.PI * random.nextDouble(); rotateVectorV(dummyAlpha, rV1, rV2); if (!doExchange) { rotateVectorV(newAlpha[imageIndex], rV2, (IVectorMutable) newOrientations[i][imageIndex].getDirection()); } else { double angle = 2 * Math.PI * random.nextDouble(); rotateVectorV(angle, rV2, (IVectorMutable) newOrientations[i][imageIndex].getDirection()); } theta[imageIndex] = 0; } else { newCenter.Ev1Pv2(newOrientations[i][fromImage].getDirection(), newOrientations[i][toImage].getDirection()); newCenter.normalize(); newOrientations[i][imageIndex].setDirection(newCenter); e1.E(0); if (Math.abs(newCenter.getX(0)) > 0.5) { e1.setX(1, 1); } else { e1.setX(0, 1); } e1.PEa1Tv1(-e1.dot(newCenter), newCenter); e1.normalize(); rotateVectorV(newAlpha[imageIndex], e1, (IVectorMutable) newOrientations[i][imageIndex].getDirection()); theta[imageIndex] = random.nextDouble() * 2 * Math.PI; newOrientations[i][imageIndex].rotateBy(theta[imageIndex], newCenter); } if (newOrientations[i][imageIndex].getDirection().isNaN()) throw new RuntimeException("bead " + imageIndex + " orientation is NaN"); if (!doExchange || imageIndex != P / 2) { oldCenter.ME(oldOrientations[i][imageIndex].getDirection()); double v = oldCenter.squared(); double v1 = oldCenter.dot(oldOrientations[i][imageIndex].getDirection()); double s1 = v - v1 * v1; double y0m1 = -s1 / (1 + y0); // if (xOld[i][imageIndex] == 0) xOld[i][imageIndex] = xNew[i][imageIndex]; // pGenOld *= Math.exp(kEff1Old*y0)*kEff1Old/Math.sinh(kEff1Old); // pGenNew *= Math.exp(kEff1New*y1)*kEff1New/Math.sinh(kEff1New); // pGenOld *= 2*Math.exp(kEff1Old*y0m1)*kEff1Old/(1 - Math.exp(-2*kEff1Old)); // pGenNew *= 2*Math.exp(kEff1New*a)*kEff1New/(1 - Math.exp(-2*kEff1New)); // System.out.println(kEff1Old+" "+kEff1New+" "+y0m1+" "+(y0-1)+" "+a+" "+(y1_new-1)); // pGenRatio *= Math.exp(kEff1New*a - kEff1Old*y0m1)*kEff1New*(1-Math.exp(-2*kEff1Old))/(kEff1Old*(1-Math.exp(-2*kEff1New))); // double aNew = kEff1New*(-xNew[i][imageIndex] + 1/(1 - Math.exp(-2*kEff1New)))/(kEff1Old*(-xOld[i][imageIndex] + 1/(1 - Math.exp(-2*kEff1Old)))); double aOld = Math.exp(kEff1New * a - kEff1Old * y0m1) * kEff1New * (1 - Math.exp(-2 * kEff1Old)) / (kEff1Old * (1 - Math.exp(-2 * kEff1New))); pGenRatio *= aOld; if (Double.isNaN(pGenRatio)) throw new RuntimeException(); } } } for (int j = 0; j < P; j++) { int prev = j - 1; if (prev < 0) prev = P - 1; AtomHydrogen jAtom = (AtomHydrogen) atoms.getAtom(j); AtomHydrogen jPrev = (AtomHydrogen) atoms.getAtom(prev); uNew += kHarmonic * dist(jAtom, jPrev); } } double pActRatio = Math.exp(uOld - uNew); uAcc = uNew; pacc = pActRatio / pGenRatio; ((BoxCluster) box).trialNotify(); weightNew = ((BoxCluster) box).getSampleCluster().value((BoxCluster) box); return true; }
From source file:com.simiacryptus.mindseye.applications.ObjectLocationBase.java
/** * Run./*from w w w .j a va 2s. c om*/ * * @param log the log */ public void run(@Nonnull final NotebookOutput log) { // @Nonnull String logName = "cuda_" + log.getName() + ".log"; // log.p(log.file((String) null, logName, "GPU Log")); // CudaSystem.addLog(new PrintStream(log.file(logName))); ImageClassifierBase classifier = getClassifierNetwork(); Layer classifyNetwork = classifier.getNetwork(); ImageClassifierBase locator = getLocatorNetwork(); Layer locatorNetwork = locator.getNetwork(); ArtistryUtil.setPrecision((DAGNetwork) classifyNetwork, Precision.Float); ArtistryUtil.setPrecision((DAGNetwork) locatorNetwork, Precision.Float); Tensor[][] inputData = loadImages_library(); // Tensor[][] inputData = loadImage_Caltech101(log); double alphaPower = 0.8; final AtomicInteger index = new AtomicInteger(0); Arrays.stream(inputData).limit(10).forEach(row -> { log.h3("Image " + index.getAndIncrement()); final Tensor img = row[0]; log.p(log.image(img.toImage(), "")); Result classifyResult = classifyNetwork.eval(new MutableResult(row)); Result locationResult = locatorNetwork.eval(new MutableResult(row)); Tensor classification = classifyResult.getData().get(0); List<CharSequence> categories = classifier.getCategories(); int[] sortedIndices = IntStream.range(0, categories.size()).mapToObj(x -> x) .sorted(Comparator.comparing(i -> -classification.get(i))).mapToInt(x -> x).limit(10).toArray(); logger.info(Arrays.stream(sortedIndices) .mapToObj( i -> String.format("%s: %s = %s%%", i, categories.get(i), classification.get(i) * 100)) .reduce((a, b) -> a + "\n" + b).orElse("")); LinkedHashMap<CharSequence, Tensor> vectors = new LinkedHashMap<>(); List<CharSequence> predictionList = Arrays.stream(sortedIndices).mapToObj(categories::get) .collect(Collectors.toList()); Arrays.stream(sortedIndices).limit(6).forEach(category -> { CharSequence name = categories.get(category); log.h3(name); Tensor alphaTensor = renderAlpha(alphaPower, img, locationResult, classification, category); log.p(log.image(img.toRgbImageAlphaMask(0, 1, 2, alphaTensor), "")); vectors.put(name, alphaTensor.unit()); }); Tensor avgDetection = vectors.values().stream().reduce((a, b) -> a.add(b)).get() .scale(1.0 / vectors.size()); Array2DRowRealMatrix covarianceMatrix = new Array2DRowRealMatrix(predictionList.size(), predictionList.size()); for (int x = 0; x < predictionList.size(); x++) { for (int y = 0; y < predictionList.size(); y++) { Tensor l = vectors.get(predictionList.get(x)); Tensor r = vectors.get(predictionList.get(y)); covarianceMatrix.setEntry(x, y, null == l || null == r ? 0 : (l.minus(avgDetection)).dot(r.minus(avgDetection))); } } @Nonnull final EigenDecomposition decomposition = new EigenDecomposition(covarianceMatrix); for (int objectVector = 0; objectVector < 10; objectVector++) { log.h3("Eigenobject " + objectVector); double eigenvalue = decomposition.getRealEigenvalue(objectVector); RealVector eigenvector = decomposition.getEigenvector(objectVector); Tensor detectionRegion = IntStream.range(0, eigenvector.getDimension()).mapToObj(i -> { Tensor tensor = vectors.get(predictionList.get(i)); return null == tensor ? null : tensor.scale(eigenvector.getEntry(i)); }).filter(x -> null != x).reduce((a, b) -> a.add(b)).get(); detectionRegion = detectionRegion.scale(255.0 / detectionRegion.rms()); CharSequence categorization = IntStream.range(0, eigenvector.getDimension()).mapToObj(i -> { CharSequence category = predictionList.get(i); double component = eigenvector.getEntry(i); return String.format("<li>%s = %.4f</li>", category, component); }).reduce((a, b) -> a + "" + b).get(); log.p(String.format("Object Detected: <ol>%s</ol>", categorization)); log.p("Object Eigenvalue: " + eigenvalue); log.p("Object Region: " + log.image(img.toRgbImageAlphaMask(0, 1, 2, detectionRegion), "")); log.p("Object Region Compliment: " + log.image(img.toRgbImageAlphaMask(0, 1, 2, detectionRegion.scale(-1)), "")); } // final int[] orderedVectors = IntStream.range(0, 10).mapToObj(x -> x) // .sorted(Comparator.comparing(x -> -decomposition.getRealEigenvalue(x))).mapToInt(x -> x).toArray(); // IntStream.range(0, orderedVectors.length) // .mapToObj(i -> { // //double realEigenvalue = decomposition.getRealEigenvalue(orderedVectors[i]); // return decomposition.getEigenvector(orderedVectors[i]).toArray(); // } // ).toArray(i -> new double[i][]); log.p(String.format( "<table><tr><th>Cosine Distance</th>%s</tr>%s</table>", Arrays.stream(sortedIndices).limit(10) .mapToObj(col -> "<th>" + categories.get(col) + "</th>").reduce((a, b) -> a + b).get(), Arrays.stream(sortedIndices).limit(10).mapToObj(r -> { return String.format("<tr><td>%s</td>%s</tr>", categories.get(r), Arrays.stream(sortedIndices).limit(10).mapToObj(col -> { Tensor l = vectors.get(categories.get(r)); Tensor r2 = vectors.get(categories.get(col)); return String.format("<td>%.4f</td>", (null == l || null == r2) ? 0 : Math.acos(l.dot(r2))); }).reduce((a, b) -> a + b).get()); }).reduce((a, b) -> a + b).orElse(""))); }); log.setFrontMatterProperty("status", "OK"); }