/// <summary> /// Subtracts a function from the polynomial. /// <para> /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the subtract takes place /// as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial. /// /// </para> /// </summary> /// <param name="f"> the function to subtract </param> /// <returns> $P-f$ </returns> public override DoubleFunction1D subtract(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f is RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f; double[] c = _coefficients; double[] c1 = p1.Coefficients; int m = c.Length; int n = c1.Length; int min = Math.Min(m, n); int max = Math.Max(m, n); double[] c3 = new double[max]; for (int i = 0; i < min; i++) { c3[i] = c[i] - c1[i]; } for (int i = min; i < max; i++) { if (m == max) { c3[i] = c[i]; } else { c3[i] = -c1[i]; } } return(new RealPolynomialFunction1D(c3)); } return(DoubleFunction1D.this.subtract(f)); }
/// <summary> /// Adds a function to the polynomial. /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the addition takes /// place as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial. /// </summary> /// <param name="f"> the function to add </param> /// <returns> $P+f$ </returns> public override DoubleFunction1D add(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f is RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f; double[] c1 = p1.Coefficients; double[] c = _coefficients; int n = c1.Length; bool 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.this.add(f)); }
//------------------------------------------------------------------------- public override bool Equals(object obj) { if (this == obj) { return(true); } if (obj == null) { return(false); } if (this.GetType() != obj.GetType()) { return(false); } RealPolynomialFunction1D other = (RealPolynomialFunction1D)obj; if (!Arrays.Equals(_coefficients, other._coefficients)) { return(false); } return(true); }
/// <summary> /// Multiplies the polynomial by a function. /// If the function is not a <seealso cref="RealPolynomialFunction1D"/> then the multiplication takes /// place as in <seealso cref="DoubleFunction1D"/>, otherwise the result will also be a polynomial. /// </summary> /// <param name="f"> the function by which to multiply </param> /// <returns> $P \dot f$ </returns> public override DoubleFunction1D multiply(DoubleFunction1D f) { ArgChecker.notNull(f, "function"); if (f is RealPolynomialFunction1D) { RealPolynomialFunction1D p1 = (RealPolynomialFunction1D)f; double[] c = _coefficients; double[] c1 = p1.Coefficients; 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.this.multiply(f)); }