@Test public void testDivide() { assertCoefficientsEquals(new double[]{A / 2, B / 2, C / 2}, valueOf(A, B, C).divide(2)); assertCoefficientsEquals(new double[]{Double.POSITIVE_INFINITY}, valueOf(A).divide(0)); assertCoefficientsEquals(new double[]{Double.NEGATIVE_INFINITY}, valueOf(-A).divide(0)); assertCoefficientsEquals(new double[]{-1, 1}, valueOf(1, 0, -1, 1).divide(valueOf(0, 0, 1))); assertCoefficientsEquals(new double[]{Double.POSITIVE_INFINITY}, valueOf(A).divide(valueOf(0))); assertCoefficientsEquals(new double[]{Double.NEGATIVE_INFINITY}, valueOf(-A).divide(valueOf(0))); }
public static Division divide(final Polynomial num, final Polynomial denom) { int n = num.getDegree(), nv = denom.getDegree(); if (nv > n) { return new Division(num, Polynomial.ZERO); } double[] r = new double[n + 1]; double[] q = new double[n + 1]; for (int j = 0; j <= n; ++j) { r[j] = num.get(j); q[j] = 0.0; } for (int k = n - nv; k >= 0; --k) { q[k] = r[nv + k] / denom.get(nv); for (int j = nv + k - 1; j >= k; j--) { r[j] -= q[k] * denom.get(j - k); } } Polynomial m_q = Polynomial.of(Arrays.copyOf(q, n - nv + 1)); Polynomial m_r = nv > 0 ? Polynomial.of(Arrays.copyOf(r, nv)).adjustDegree() : Polynomial.ZERO; return new Division(m_r, m_q); }
Polynomial implements IReadDataBlock { public static Division divide(final Polynomial num, final Polynomial denom) { int n = num.getDegree(), nv = denom.getDegree(); if (nv > n) { return new Division(num, Polynomial.ZERO); } double[] r = new double[n + 1]; double[] q = new double[n + 1]; for (int j = 0; j <= n; ++j) { r[j] = num.get(j); q[j] = 0.0; } for (int k = n - nv; k >= 0; --k) { q[k] = r[nv + k] / denom.get(nv); for (int j = nv + k - 1; j >= k; j--) { r[j] -= q[k] * denom.get(j - k); } } Polynomial m_q = Polynomial.of(Arrays.copyOf(q, n - nv + 1)); Polynomial m_r = nv > 0 ? Polynomial.of(Arrays.copyOf(r, nv)).adjustDegree() : Polynomial.ZERO; return new Division(m_r, m_q); } }
Polynomial implements IReadDataBlock { public static Division divide(final Polynomial num, final Polynomial denom) { int n = num.getDegree(), nv = denom.getDegree(); if (nv > n) { return new Division(num, Polynomial.ZERO); } double[] r = new double[n + 1]; double[] q = new double[n + 1]; for (int j = 0; j <= n; ++j) { r[j] = num.get(j); q[j] = 0.0; } for (int k = n - nv; k >= 0; --k) { q[k] = r[nv + k] / denom.get(nv); for (int j = nv + k - 1; j >= k; j--) { r[j] -= q[k] * denom.get(j - k); } } Polynomial m_q = Polynomial.of(Arrays.copyOf(q, n - nv + 1)); Polynomial m_r = nv > 0 ? Polynomial.of(Arrays.copyOf(r, nv)).adjustDegree() : Polynomial.ZERO; return new Division(m_r, m_q); } private  Polynomial(final double[] coefficients, final int degree); }
Polynomial implements IReadDataBlock { public static Division divide(final Polynomial num, final Polynomial denom) { int n = num.getDegree(), nv = denom.getDegree(); if (nv > n) { return new Division(num, Polynomial.ZERO); } double[] r = new double[n + 1]; double[] q = new double[n + 1]; for (int j = 0; j <= n; ++j) { r[j] = num.get(j); q[j] = 0.0; } for (int k = n - nv; k >= 0; --k) { q[k] = r[nv + k] / denom.get(nv); for (int j = nv + k - 1; j >= k; j--) { r[j] -= q[k] * denom.get(j - k); } } Polynomial m_q = Polynomial.of(Arrays.copyOf(q, n - nv + 1)); Polynomial m_r = nv > 0 ? Polynomial.of(Arrays.copyOf(r, nv)).adjustDegree() : Polynomial.ZERO; return new Division(m_r, m_q); } private  Polynomial(final double[] coefficients, final int degree); static Division divide(final Polynomial num, final Polynomial denom); @Override void copyTo(double[] buffer, int start); @Override int getLength(); @Override IReadDataBlock rextract(int start, int length); static Complex[] checkRoots(final Complex[] roots); static double getEpsilon(); static Polynomial of(double[] coefficients); static Polynomial copyOf(double[] coefficients); static Polynomial copyOf(double[] coefficients, int start, int end); static Polynomial valueOf(double firstCoefficient, double... nextCoefficients); static Polynomial fromComplexRoots(Complex[] roots); static Polynomial fromComplexRoots(final Complex[] roots, final double c); static Polynomial fromData(IReadDataBlock data); static Polynomial factor(double c, int n); static void setEpsilon(final double value); static IRootsSolver getDefRootsSearcher(); static void setDefRootsSearcher(final IRootsSolver value); Polynomial adjustDegree(); Polynomial derivate(); Polynomial divide(final double d); Polynomial divide(final Polynomial r); @Override boolean equals(final Object obj); boolean equals(Polynomial other, double epsilon); Complex evaluateAt(final Complex x); double evaluateAt(final double x); static double revaluate(final double[] c, final double x); static double evaluate(final double[] c, final double x); static double evaluate(final int degree, IntToDoubleFunction fn, final double x); Complex evaluateAtFrequency(final double w); @Override double get(final int idx); double[] getCoefficients(); int getDegree(); @Override int hashCode(); boolean isZero(); boolean isIdentity(); Polynomial minus(double d); Polynomial minus(final Polynomial r); Polynomial mirror(); Polynomial negate(); Polynomial plus(final double d); Polynomial plus(final Polynomial r); Complex[] roots(); Complex[] roots(IRootsSolver searcher); Polynomial smooth(); Polynomial times(final double d); Polynomial times(final Polynomial r); Polynomial times(final Polynomial r, boolean computeroots); @Override String toString(); String toString(final char var, final boolean bSmooth); String toString(final String fmt, final char var,
            final boolean bSmooth); }
Polynomial implements IReadDataBlock { public static Division divide(final Polynomial num, final Polynomial denom) { int n = num.getDegree(), nv = denom.getDegree(); if (nv > n) { return new Division(num, Polynomial.ZERO); } double[] r = new double[n + 1]; double[] q = new double[n + 1]; for (int j = 0; j <= n; ++j) { r[j] = num.get(j); q[j] = 0.0; } for (int k = n - nv; k >= 0; --k) { q[k] = r[nv + k] / denom.get(nv); for (int j = nv + k - 1; j >= k; j--) { r[j] -= q[k] * denom.get(j - k); } } Polynomial m_q = Polynomial.of(Arrays.copyOf(q, n - nv + 1)); Polynomial m_r = nv > 0 ? Polynomial.of(Arrays.copyOf(r, nv)).adjustDegree() : Polynomial.ZERO; return new Division(m_r, m_q); } private  Polynomial(final double[] coefficients, final int degree); static Division divide(final Polynomial num, final Polynomial denom); @Override void copyTo(double[] buffer, int start); @Override int getLength(); @Override IReadDataBlock rextract(int start, int length); static Complex[] checkRoots(final Complex[] roots); static double getEpsilon(); static Polynomial of(double[] coefficients); static Polynomial copyOf(double[] coefficients); static Polynomial copyOf(double[] coefficients, int start, int end); static Polynomial valueOf(double firstCoefficient, double... nextCoefficients); static Polynomial fromComplexRoots(Complex[] roots); static Polynomial fromComplexRoots(final Complex[] roots, final double c); static Polynomial fromData(IReadDataBlock data); static Polynomial factor(double c, int n); static void setEpsilon(final double value); static IRootsSolver getDefRootsSearcher(); static void setDefRootsSearcher(final IRootsSolver value); Polynomial adjustDegree(); Polynomial derivate(); Polynomial divide(final double d); Polynomial divide(final Polynomial r); @Override boolean equals(final Object obj); boolean equals(Polynomial other, double epsilon); Complex evaluateAt(final Complex x); double evaluateAt(final double x); static double revaluate(final double[] c, final double x); static double evaluate(final double[] c, final double x); static double evaluate(final int degree, IntToDoubleFunction fn, final double x); Complex evaluateAtFrequency(final double w); @Override double get(final int idx); double[] getCoefficients(); int getDegree(); @Override int hashCode(); boolean isZero(); boolean isIdentity(); Polynomial minus(double d); Polynomial minus(final Polynomial r); Polynomial mirror(); Polynomial negate(); Polynomial plus(final double d); Polynomial plus(final Polynomial r); Complex[] roots(); Complex[] roots(IRootsSolver searcher); Polynomial smooth(); Polynomial times(final double d); Polynomial times(final Polynomial r); Polynomial times(final Polynomial r, boolean computeroots); @Override String toString(); String toString(final char var, final boolean bSmooth); String toString(final String fmt, final char var,
            final boolean bSmooth); static final Polynomial ZERO; static final Polynomial ONE; }