List of usage examples for org.apache.commons.math3.analysis.polynomials PolynomialFunction multiply
public PolynomialFunction multiply(final PolynomialFunction p)
From source file:jurbano.melodyshape.comparison.bspline.BSplineTimeNGramComparer.java
/** * {@inheritDoc}//from ww w .j a v a2 s.c o m * * @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}//from w w w . j a v a 2 s .c o m * * @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 w w . j av a 2s . c o 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:dom.rootlocus.utils.Utils.java
public PolynomialFunction getEC(PolynomialFunction num, PolynomialFunction den, double k) { double[] gain = { k }; return num.multiply(new PolynomialFunction(gain)).add(den); }
From source file:dom.rootlocus.utils.Utils.java
public PolynomialFunction getEC(PolynomialFunction pNum, PolynomialFunction pDen, PolynomialFunction sNum, PolynomialFunction sDen, double k) { double[] gain = { k }; return pNum.multiply(sNum).multiply(new PolynomialFunction(gain)).add(pDen.multiply(sDen)); }