/** * Subtracts a constant from the polynomial (equivalent to subtracting the value from the * constant term of the polynomial). The result is also a polynomial. * * @param a the value to add * @return $P-a$ */ @Override public RealPolynomialFunction1D subtract(double a) { double[] c = Arrays.copyOf(getCoefficients(), _n); c[0] -= a; return new RealPolynomialFunction1D(c); }
/** * Adds a constant to the polynomial (equivalent to adding the value to the constant * term of the polynomial). The result is also a polynomial. * * @param a the value to add * @return $P+a$ */ @Override public RealPolynomialFunction1D add(double a) { double[] c = Arrays.copyOf(getCoefficients(), _n); c[0] += a; return new RealPolynomialFunction1D(c); }
/** * Divides the polynomial by a constant value (equivalent to dividing each coefficient by this value). * The result is also a polynomial. * * @param a the divisor * @return the polynomial */ @Override public RealPolynomialFunction1D divide(double a) { double[] c = Arrays.copyOf(getCoefficients(), _n); for (int i = 0; i < _n; i++) { c[i] /= a; } return new RealPolynomialFunction1D(c); }
/** * Multiplies the polynomial by a constant value (equivalent to multiplying each * coefficient by this value). The result is also a polynomial. * * @param a the multiplicator * @return the polynomial */ @Override public RealPolynomialFunction1D multiply(double a) { double[] c = Arrays.copyOf(getCoefficients(), _n); for (int i = 0; i < _n; i++) { c[i] *= a; } return new RealPolynomialFunction1D(c); }
@Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coefficients = function.getCoefficients(); if (coefficients.length != 4) { throw new IllegalArgumentException("Function is not a cubic"); } ComplexNumber[] result = ROOT_FINDER.getRoots(function); List<Double> reals = new ArrayList<>(); for (ComplexNumber c : result) { if (DoubleMath.fuzzyEquals(c.getImaginary(), 0d, 1e-16)) { reals.add(c.getReal()); } } ArgChecker.isTrue(reals.size() > 0, "Could not find any real roots"); return reals.toArray(EMPTY_ARRAY); }
/** * Multiplies the polynomial by a function. * If the function is not a {@link RealPolynomialFunction1D} then the multiplication takes * place as in {@link DoubleFunction1D}, otherwise the result will also be a polynomial. * * @param f the function by which to multiply * @return $P \dot f$ */ @Override public DoubleFunction1D multiply(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f instanceof RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D) f; double[] c = _coefficients; double[] c1 = p1.getCoefficients(); int m = c1.length; double[] newC = new double[_n + m - 1]; for (int i = 0; i < newC.length; i++) { newC[i] = 0; for (int j = Math.max(0, i + 1 - m); j < Math.min(_n, i + 1); j++) { newC[i] += c[j] * c1[i - j]; } } return new RealPolynomialFunction1D(newC); } return DoubleFunction1D.super.multiply(f); }
/** * Adds a function to the polynomial. * If the function is not a {@link RealPolynomialFunction1D} then the addition takes * place as in {@link DoubleFunction1D}, otherwise the result will also be a polynomial. * * @param f the function to add * @return $P+f$ */ @Override public DoubleFunction1D add(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f instanceof RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D) f; double[] c1 = p1.getCoefficients(); double[] c = _coefficients; int n = c1.length; boolean longestIsNew = n > _n; double[] c3 = longestIsNew ? Arrays.copyOf(c1, n) : Arrays.copyOf(c, _n); for (int i = 0; i < (longestIsNew ? _n : n); i++) { c3[i] += longestIsNew ? c[i] : c1[i]; } return new RealPolynomialFunction1D(c3); } return DoubleFunction1D.super.add(f); }
/** * {@inheritDoc} * @throws IllegalArgumentException If the function is not a quadratic * @throws MathException If the roots are not real */ @Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coefficients = function.getCoefficients(); ArgChecker.isTrue(coefficients.length == 3, "Function is not a quadratic"); double c = coefficients[0]; double b = coefficients[1]; double a = coefficients[2]; double discriminant = b * b - 4 * a * c; if (discriminant < 0) { throw new MathException("No real roots for quadratic"); } double q = -0.5 * (b + Math.signum(b) * discriminant); return new Double[] {q / a, c / q}; }
RealPolynomialFunction1D p1 = (RealPolynomialFunction1D) f; double[] c = _coefficients; double[] c1 = p1.getCoefficients(); int m = c.length; int n = c1.length;
/** * {@inheritDoc} * @throws MathException If there are no real roots; if the Commons method could not evaluate the function; if the Commons method could not converge. */ @Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); try { Complex[] roots = ROOT_FINDER.solveAllComplex(function.getCoefficients(), 0); List<Double> realRoots = new ArrayList<>(); for (Complex c : roots) { if (DoubleMath.fuzzyEquals(c.getImaginary(), 0d, EPS)) { realRoots.add(c.getReal()); } } if (realRoots.isEmpty()) { throw new MathException("Could not find any real roots"); } return realRoots.toArray(new Double[realRoots.size()]); } catch (TooManyEvaluationsException e) { throw new MathException(e); } }
public ComplexNumber[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coefficients = function.getCoefficients(); ArgChecker.isTrue(coefficients.length == 4, "Function is not a cubic"); double divisor = coefficients[3];
@Override public Double[] getRoots(RealPolynomialFunction1D function) { ArgChecker.notNull(function, "function"); double[] coeffs = function.getCoefficients(); int l = coeffs.length - 1; double[][] hessianDeref = new double[l][l]; for (int i = 0; i < l; i++) { hessianDeref[0][i] = -coeffs[l - i - 1] / coeffs[l]; for (int j = 1; j < l; j++) { hessianDeref[j][i] = 0; if (i != l - 1) { hessianDeref[i + 1][i] = 1; } } } RealMatrix hessian = new Array2DRowRealMatrix(hessianDeref); double[] d = new EigenDecomposition(hessian).getRealEigenvalues(); Double[] result = new Double[d.length]; for (int i = 0; i < d.length; i++) { result[i] = d[i]; } return result; }