/// <summary> /// Adds another polynomial to this /// </summary> /// <returns> /// this polynomial /// </returns> /// <param name="other">The polynomial to add</param> public MyPolynomial Add(MyPolynomial other) { double[] result; bool longer = this.GetDegree() >= other.GetDegree(); if (longer) { result = new double[_coeffs.Length]; } else { result = new double[other.GetDegree() + 1]; } for (int i = 0; i < result.Length; i++) { try { result[i] = _coeffs[i] += other.CoeffAt(i); } catch (IndexOutOfRangeException) { if (longer) { result[i] = _coeffs[i]; } else { result[i] = other.CoeffAt(i); } } } _coeffs = result; return(this); }
public static String CoeffsToString(MyPolynomial poly) { String result = "["; for (int i = 0; i < poly.GetDegree(); i++) { result += poly.CoeffAt(i) + ", "; } result += poly.CoeffAt(poly.GetDegree()) + "]: "; return(result); }
/// <summary> /// Multiplies this by another polynomial /// </summary> /// <returns> /// this polynomial /// </returns> /// <param name="other">The polynomial to multiply by</param> public MyPolynomial Multiply(MyPolynomial other) { double[] product = new double[_coeffs.Length + other.GetDegree()]; for (int i = 0; i < _coeffs.Length; i++) { for (int j = 0; j < other.GetDegree() + 1; j++) { product[i + j] += _coeffs[i] * other.CoeffAt(j); } } _coeffs = product; return(this); }