/** * Negate the instance. * * @return a new polynomial with all coefficients negated */ public PolynomialFunction negate() { double[] newCoefficients = new double[coefficients.length]; for (int i = 0; i < coefficients.length; ++i) { newCoefficients[i] = -coefficients[i]; } return new PolynomialFunction(newCoefficients); }
/** * @return the coefficients of the polynomial function. */ protected double[] getCoefficients() { return polynomialFunction.getCoefficients(); } }
yPolyX[j] = new PolynomialFunction(xFitter.fit(new double[xDegree + 1])); final PolynomialFunction f = yPolyX[j]; for (int i = 0; i < xLen; i++) { fval_1[i][j] = f.value(xval[i]); xPolyY[i] = new PolynomialFunction(yFitter.fit(new double[yDegree + 1])); final PolynomialFunction f = xPolyY[i]; for (int j = 0; j < yLen; j++) { fval_2[i][j] = f.value(yval[j]);
/** * Returns the derivative as a {@link PolynomialFunction}. * * @return the derivative polynomial. */ public PolynomialFunction polynomialDerivative() { return new PolynomialFunction(differentiate(coefficients)); }
/** Compute the interpolation polynomials. * @return interpolation polynomials array * @exception NoDataException if sample is empty */ public PolynomialFunction[] getPolynomials() throws NoDataException { // safety check checkInterpolation(); // iteration initialization final PolynomialFunction zero = polynomial(0); PolynomialFunction[] polynomials = new PolynomialFunction[topDiagonal.get(0).length]; for (int i = 0; i < polynomials.length; ++i) { polynomials[i] = zero; } PolynomialFunction coeff = polynomial(1); // build the polynomials by iterating on the top diagonal of the divided differences array for (int i = 0; i < topDiagonal.size(); ++i) { double[] tdi = topDiagonal.get(i); for (int k = 0; k < polynomials.length; ++k) { polynomials[k] = polynomials[k].add(coeff.multiply(polynomial(tdi[k]))); } coeff = coeff.multiply(polynomial(-abscissae.get(i), 1.0)); } return polynomials; }
private static int getNearRoots(double[] roots, EquationSolverInterface solver, double epsilon) { PolynomialFunction poly = new PolynomialFunction(roots); double[] rootsDerivative = poly.polynomialDerivative() .getCoefficients(); if (DoubleUtil.isEqual(poly.value(rootsDerivative[i]), 0, epsilon)) { if (nrRoots < roots.length) { roots[nrRoots++] = rootsDerivative[i]; double[] c = poly.getCoefficients(); int n = c.length - 1; if (n > 0) { double x = -c[n - 1] / n / c[n]; if (DoubleUtil.isEqual(poly.value(x), 0)) { roots[0] = x; return 1; PolynomialFunction derivative = poly.polynomialDerivative(); double x = 0; double err = Math.abs(poly.value(x)); double lastErr = err * 2; while (err < lastErr && err > Kernel.STANDARD_PRECISION) { double devVal = derivative.value(x); if (!DoubleUtil.isZero(devVal)) { x = x - poly.value(x) / devVal; } else { break; err = Math.abs(poly.value(x));
public AD7718(SPI spi) throws IOException { this.spi = spi; this.cs = "CS1"; // Populate Calibrations populateCalibrationMap(); // Set SPI Parameters spi.setParameters(2, 1, 0, 1, 1); writeRegister(FILTER, 20); writeRegister(MODE, MODE_SINGLE | MODE_CHCON | MODE_REFSEL); for (String key : calibs.keySet()) { double[] convertedList = new PolynomialFunction(calibs.get(key)).getCoefficients(); ArrayUtils.reverse(convertedList); caldata.put(key, convertedList); } }
private double[] rootsToPolynomial(Complex[] x){ /* Returns a double array of coefficients of the polynomial, assuming the each complex root has it's conjugate in the same array. */ PolynomialFunction[] polynomialFunctionArray = new PolynomialFunction[x.length/2]; PolynomialFunction product = new PolynomialFunction(new double[]{1}); for(int i = 0; i < x.length / 2; i++){ PolynomialFunction complexRoot = new PolynomialFunction(new double[]{-x[i].getReal(), 1}); complexRoot = complexRoot.multiply(complexRoot); complexRoot = complexRoot.subtract(new PolynomialFunction(new double[]{-1 * x[i].getImaginary() * x[i].getImaginary()})); System.out.println((complexRoot)); polynomialFunctionArray[i] = complexRoot; } for (PolynomialFunction aPolynomialFunctionArray : polynomialFunctionArray) product = aPolynomialFunctionArray.multiply(product); return product.getCoefficients(); }
/** * Compute the value for the function. * See {@link PolynomialSplineFunction} for details on the algorithm for * computing the value of the function. * * @param v Point for which the function value should be computed. * @return the value. * @throws OutOfRangeException if {@code v} is outside of the domain of the * spline function (smaller than the smallest knot point or larger than the * largest knot point). */ public double value(double v) { if (v < knots[0] || v > knots[n]) { throw new OutOfRangeException(v, knots[0], knots[n]); } int i = Arrays.binarySearch(knots, v); if (i < 0) { i = -i - 2; } // This will handle the case where v is the last knot value // There are only n-1 polynomials, so if v is the last knot // then we will use the last polynomial to calculate the value. if ( i >= polynomials.length ) { i--; } return polynomials[i].value(v - knots[i]); }
final double initialGuess) { validateFit(); final PolynomialFunction derivative = pFunction.polynomialDerivative(); debug("Solving derivative for " + pFunction.toString()); debug("LaguerreSolver: Evaluation limit: " + getMaxEvaluations() + ", derivative is " + derivative.toString()); final Complex[] roots = solver.solveAllComplex(derivative.getCoefficients(), initialGuess, getMaxEvaluations()); if (roots == null) return null; if (x < lowerBound || x > upperBound) continue; final double y = pFunction.value(x); if (y > pFunction.value(x - tolerance) && y > pFunction.value(x + tolerance)) { maxima.add(new UPoint(x, y));
System.arraycopy(knots, 0, newKnots, 1, knots.length); double derivativeAtFirstKnot = polynomials[0].derivative().value(0); double valueAtFirstKnot = spline.value(knots[0]); PolynomialFunction beginningFunction = new PolynomialFunction(new double[]{valueAtFirstKnot - (knots[0] - minFrame) * derivativeAtFirstKnot, derivativeAtFirstKnot}); newPolynomials[0] = beginningFunction; System.arraycopy(polynomials, 0, newPolynomials, 1, polynomials.length); double derivativeAtLastKnot = polynomials[polynomials.length - 1].polynomialDerivative().value(knots[knots.length - 1] - knots[knots.length - 2]); double valueAtLastKnot = spline.value(knots[knots.length - 1]); PolynomialFunction endFunction = new PolynomialFunction(new double[]{valueAtLastKnot, derivativeAtLastKnot}); newPolynomials[newPolynomials.length - 1] = endFunction;
/** * @param coeff * implicit poly coefficients * @param tx * function of t to plug into x * @param ty * function of t to plug into y * @return function of t representing f(tx(t),ty(t)) */ public static PolynomialFunction lineIntersect(double[][] coeff, PolynomialFunction tx, PolynomialFunction ty) { PolynomialFunction sum = null; PolynomialFunction zs = null; for (int i = coeff.length - 1; i >= 0; i--) { zs = new PolynomialFunction( new double[] { coeff[i][coeff[i].length - 1] }); for (int j = coeff[i].length - 2; j >= 0; j--) { zs = zs.multiply(ty).add( new PolynomialFunction(new double[] { coeff[i][j] })); // y*zs+coeff[i][j]; } if (sum == null) { sum = zs; } else { sum = sum.multiply(tx).add(zs); // sum*x+zs; } } return sum; }
private void setRootsPolynomialWithinRange( ArrayList<Coords> intersectCoords2, PolynomialFunction rootsPoly, double min, double max) { double[] roots = rootsPoly.getCoefficients(); int nrRealRoots = 0; if (roots.length > 1) { continue; Coords pair = new Coords(tx.value(roots[i]), ty.value(roots[i]), 1); for (int k = 1; k < count + 1; k++) { if (count > 0 && distancePairSq(pair,
/** * Returns the derivative as a {@link UnivariateFunction}. * * @return the derivative function. */ public UnivariateFunction derivative() { return polynomialDerivative(); }
/** {@inheritDoc} */ public double value(final double x, final double ... parameters) throws NoDataException { return PolynomialFunction.evaluate(parameters, x); } }
/** * calculates the quotient of p/d (no calculation of the remainder is done) * * @param p * dividend * @param d * divisor * @return quotient of p/d */ public static PolynomialFunction polynomialDivision(PolynomialFunction p, PolynomialFunction d) { return new PolynomialFunction( polynomialDivision(p.getCoefficients(), d.getCoefficients())); }
bPolys[i] = new PolynomialFunction(b.getCoeff()[i]); aNew[i] = new PolynomialFunction(new double[] { 0 }); aNew[i] = new PolynomialFunction(a.getCoeff()[i - n + 1]); if (!(aNew[leadIndex].degree() == 0 && aNew[leadIndex].getCoefficients()[0] == 0)) { for (int j = n - 1; j < leadIndex - n; ++j) { aNew[j] = aNew[j].multiply(bPolys[n]); aNew[j] = aNew[j].multiply(bPolys[n]) .subtract(bPolys[j - leadIndex + n] .multiply(aNew[leadIndex])); if (!(aNew[leadIndex].degree() == 0 && aNew[leadIndex].getCoefficients()[0] == 0)) { for (int j = leadIndex - n; j < leadIndex; ++j) { aNew[j] = aNew[j].multiply(bPolys[n]) .subtract(bPolys[j - leadIndex + n] .multiply(aNew[leadIndex])); mat[2 * n - 1 - leadIndex][j] = new PolynomialFunction( aNew[leadIndex - n + j].getCoefficients()); double reduceFactor = 1; for (int j = 0; j < n; ++j) { for (int k = 0; k < mat[i][j].getCoefficients().length; ++k) { largestCoeff = Math.max(
/** {@inheritDoc} * @since 3.1 */ public DerivativeStructure value(final DerivativeStructure t) { final double t0 = t.getValue(); if (t0 < knots[0] || t0 > knots[n]) { throw new OutOfRangeException(t0, knots[0], knots[n]); } int i = Arrays.binarySearch(knots, t0); if (i < 0) { i = -i - 2; } // This will handle the case where t is the last knot value // There are only n-1 polynomials, so if t is the last knot // then we will use the last polynomial to calculate the value. if ( i >= polynomials.length ) { i--; } return polynomials[i].value(t.subtract(knots[i])); }
return; tx = new PolynomialFunction(new double[] { 0, 1 }); // x=t ty = new PolynomialFunction(derivY.getCoeffs()); // y=f(t) maxT = f.getMaxParameter(); minT = f.getMinParameter(); tx = new PolynomialFunction(new double[] { startP[0], l.getY() }); // x=p1+t*r1 ty = new PolynomialFunction(new double[] { startP[1], -l.getX() }); // y=p2+t*r2 maxT = l.getMaxParameter(); minT = l.getMinParameter(); if (coeff != null) { for (int i = coeff.length - 1; i >= 0; i--) { zs = new PolynomialFunction( new double[] { coeff[i][coeff[i].length - 1] }); for (int j = coeff[i].length - 2; j >= 0; j--) { zs = zs.multiply(ty).add(new PolynomialFunction( new double[] { coeff[i][j] })); // y*zs+coeff[i][j]; sum = zs; } else { sum = sum.multiply(tx).add(zs); // sum*x+zs;