List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunction add
public PolynomialFunction add(final PolynomialFunction p)
From source file:jurbano.melodyshape.comparison.bspline.BSplineTimeNGramComparer.java
/** * {@inheritDoc}/*from ww w.j a va 2 s . com*/ * * @return 0 if both {@link NGram}s are equivalent or the area between the * first derivatives of their corresponding Uniform B-Splines. */ @Override public double compare(NGram g1, NGram g2) { if (g1 == null) g1 = g2.getNullSpan(); if (g2 == null) g2 = g1.getNullSpan(); if (g1.size() < 2 || g1.size() > UniformBSpline.BASIS_FUNCTIONS.length) throw new IllegalArgumentException(this.getName() + " only supports n-grams with 2 to " + UniformBSpline.BASIS_FUNCTIONS.length + " notes."); if (this.getNGramId(g1).equals(this.getNGramId(g2))) return 0; PolynomialFunction p1t = new PolynomialFunction(new double[] { 0 }); PolynomialFunction p2t = new PolynomialFunction(new double[] { 0 }); for (int i = 0; i < g1.size(); i++) { PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i]; p1t = p1t.add(basis.multiply(new PolynomialFunction( new double[] { g1.get(g1.size() - i - 1).getDuration() / (double) g1.get(0).getDuration() }))); p2t = p2t.add(basis.multiply(new PolynomialFunction( new double[] { g2.get(g2.size() - i - 1).getDuration() / (double) g2.get(0).getDuration() }))); } PolynomialFunction pt = p1t.polynomialDerivative().subtract(p2t.polynomialDerivative()); Laguerre laguerre = new Laguerre(); ArrayList<Double> realRoots = laguerre.findRoots(pt); return -laguerre.computeAreaBetweenDerivatives(p1t, p2t, realRoots); }
From source file:jurbano.melodyshape.comparison.bspline.BSplinePitchNGramComparer.java
/** * {@inheritDoc}/* w w w .ja va 2 s .com*/ * * @return 0 if both {@link NGram}s are equivalent or the area between the * first derivatives of their corresponding Uniform B-Splines. */ @Override public double compare(NGram g1, NGram g2) { if (g1 == null) g1 = g2.getNullSpan(); if (g2 == null) g2 = g1.getNullSpan(); if (g1.size() < 2 || g1.size() > UniformBSpline.BASIS_FUNCTIONS.length) throw new IllegalArgumentException(this.getName() + " only supports n-grams with 2 to " + UniformBSpline.BASIS_FUNCTIONS.length + " notes."); if (this.getNGramId(g1).equals(this.getNGramId(g2))) return 0; PolynomialFunction p1p = new PolynomialFunction(new double[] { 0 }); PolynomialFunction p2p = new PolynomialFunction(new double[] { 0 }); for (int i = 1; i < g1.size(); i++) { PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i - 1]; p1p = p1p.add(basis.multiply(new PolynomialFunction( new double[] { g1.get(g1.size() - i).getPitch() - g1.get(0).getPitch() }))); p2p = p2p.add(basis.multiply(new PolynomialFunction( new double[] { g2.get(g2.size() - i).getPitch() - g2.get(0).getPitch() }))); } PolynomialFunction pp = p1p.polynomialDerivative().subtract(p2p.polynomialDerivative()); Laguerre laguerre = new Laguerre(); ArrayList<Double> realRoots = laguerre.findRoots(pp); return -laguerre.computeAreaBetweenDerivatives(p1p, p2p, realRoots); }
From source file:jurbano.melodyshape.comparison.bspline.BSplineShapeNGramComparer.java
/** * {@inheritDoc}/*from w ww . ja v a2 s . co m*/ */ @Override public double compare(NGram g1, NGram g2) { if (g1 == null) g1 = g2.getNullSpan(); if (g2 == null) g2 = g1.getNullSpan(); if (g1.size() != 3) throw new IllegalArgumentException(this.getName() + " only supports n-grams with 3 notes."); if (this.getNGramId(g1).equals(this.getNGramId(g2))) return 0; PolynomialFunction p1 = new PolynomialFunction(new double[] { 0 }); PolynomialFunction p2 = new PolynomialFunction(new double[] { 0 }); for (int i = 1; i < g1.size(); i++) { PolynomialFunction basis = UniformBSpline.BASIS_FUNCTIONS[g1.size() - 1][i - 1]; p1 = p1.add(basis.multiply(new PolynomialFunction( new double[] { g1.get(g1.size() - i).getPitch() - g1.get(0).getPitch() }))); p2 = p2.add(basis.multiply(new PolynomialFunction( new double[] { g2.get(g2.size() - i).getPitch() - g2.get(0).getPitch() }))); } PolynomialFunction p1_ = p1.polynomialDerivative(); PolynomialFunction p2_ = p2.polynomialDerivative(); /* double p1_0 = Math.signum(p1_.value(0)); double p1_1 = Math.signum(p1_.value(1)); double p2_0 = Math.signum(p2_.value(0)); double p2_1 = Math.signum(p2_.value(1)); if(p1_0 == p2_0){ // same at 0 if(p1_1 == p2_1) // same at 1 return -this.dMin; else if(p1_1*p2_1<0) // opposite at 1 return -this.dMed; else // some flat at 1 return -this.dMed; }else if(p1_0*p2_0 < 0) { // opposite at 0 if(p1_1 == p2_1) // same at 1 return -this.dMed; else if(p1_1*p2_1<0) // opposite at 1 return -this.dMax; else // some flat at 1 return -this.dMax; }else { // some flat at 0 if(p1_1 == p2_1) // same at 1 return -this.dMed; else if(p1_1*p2_1<0) // opposite at 1 return -this.dMax; else // some flat at 1 return -this.dMax; }*/ double p1_0 = p1_.value(0); double p1_1 = p1_.value(1); double p2_0 = p2_.value(0); double p2_1 = p2_.value(1); // TODO: this similarity function is not symmetric if (p1_0 <= 0 && p1_1 >= 0) { // p1 is \/ if (p2_0 <= 0 && p2_1 >= 0) // p2 is \/ return -this.dMin; else if (p2_0 >= 0 && p2_1 <= 0) // p2 is /\ return -this.dMax; else return -this.dMed; } if (p1_0 >= 0 && p1_1 <= 0) { // p1 is /\ if (p2_0 >= 0 && p2_1 <= 0) // p2 is /\ return -this.dMin; else if (p2_0 <= 0 && p2_1 >= 0) // p2 is \/ return -this.dMax; else return -this.dMed; } if (p1_0 >= 0 && p1_1 >= 0) { // p1 is / if (p2_0 >= 0 && p2_1 >= 0) // p2 is / return -this.dMin; else if (p2_0 <= 0 && p2_1 <= 0) // p2 is \ return -this.dMax; else return -this.dMed; } if (p1_0 <= 0 && p1_1 <= 0) { // p1 is \ if (p2_0 <= 0 && p2_1 <= 0) // p2 is \ return -this.dMin; else if (p2_0 >= 0 && p2_1 >= 0) // p2 is / return -this.dMax; else return -this.dMed; } return -this.dMin; }
From source file:org.orekit.propagation.semianalytical.dsst.utilities.hansen.PolynomialFunctionMatrix.java
/** * Multiply the argument matrix with the current matrix. * * @param matrix//from w w w . j a va 2s. c o m * the argument matrix * @return the result of the multiplication */ public PolynomialFunctionMatrix multiply(final PolynomialFunctionMatrix matrix) { final PolynomialFunctionMatrix result = new PolynomialFunctionMatrix(order); for (int i = 0; i < order; i++) { for (int j = 0; j < order; j++) { PolynomialFunction cc = HansenUtilities.ZERO; for (int k = 0; k < order; k++) { cc = cc.add(matrix.getElem(i, k).multiply(elements[k][j])); } result.setElem(i, j, cc); } } return result; }