diff --git a/src/Numerics.Tests/PolynomialTests.cs b/src/Numerics.Tests/PolynomialTests.cs index f50e857d..ab252c3d 100644 --- a/src/Numerics.Tests/PolynomialTests.cs +++ b/src/Numerics.Tests/PolynomialTests.cs @@ -218,25 +218,25 @@ namespace MathNet.Numerics.UnitTests Assert.Throws(typeof(DivideByZeroException), () => { var p1 = new Polynomial(1.0d); - var p2 = new Polynomial(); + var p2 = Polynomial.Zero; GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); }); Assert.DoesNotThrow(() => { - var p1 = new Polynomial(); + var p1 = Polynomial.Zero; var p2 = new Polynomial(1.0d); GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); }); Assert.Throws(typeof(DivideByZeroException), () => { - var p1 = new Polynomial(); - var p2 = new Polynomial(); + var p1 = Polynomial.Zero; + var p2 = Polynomial.Zero; GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); }); Assert.Throws(typeof(DivideByZeroException), () => { var p1 = new Polynomial(1.0d); - var p2 = new Polynomial(); + var p2 = Polynomial.Zero; GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); }); } diff --git a/src/Numerics/Polynomial.cs b/src/Numerics/Polynomial.cs index 30b25cc8..edceedfc 100644 --- a/src/Numerics/Polynomial.cs +++ b/src/Numerics/Polynomial.cs @@ -47,6 +47,7 @@ namespace MathNet.Numerics /// /// Create a zero-polynomial with a coefficient array of the given length. + /// An array of length N can support polynomials of a degree of at most N-1. /// /// Length of the coefficient array public Polynomial(int n) @@ -64,7 +65,11 @@ namespace MathNet.Numerics /// public Polynomial() { +#if NET40 Coefficients = new double[0]; +#else + Coefficients = Array.Empty(); +#endif } /// @@ -109,6 +114,8 @@ namespace MathNet.Numerics return -1; } + public static Polynomial Zero => new Polynomial(); + /// /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k /// @@ -119,6 +126,7 @@ namespace MathNet.Numerics } #region Evaluation + /// /// Evaluate a polynomial at point x. /// Coefficients are ordered by power with power k at index k. @@ -211,9 +219,11 @@ namespace MathNet.Numerics { return z.Select(Evaluate); } + #endregion #region Calculus + public Polynomial Differentiate() { int n = Degree; @@ -221,10 +231,11 @@ namespace MathNet.Numerics { return this; } + if (n == 0) { // Zero - return new Polynomial(); + return Zero; } var c = new double[n]; @@ -252,9 +263,11 @@ namespace MathNet.Numerics return new Polynomial(c); } + #endregion #region Linear Algebra + /// /// Calculates the complex roots of the Polynomial by eigenvalue decomposition /// @@ -304,9 +317,11 @@ namespace MathNet.Numerics return A; } + #endregion #region Arithmetic Operations + /// /// Addition of two Polynomials (point-wise). /// @@ -538,14 +553,14 @@ namespace MathNet.Numerics if (bDegree == 0) { // division by scalar - return Tuple.Create(Divide(a, b.Coefficients[0]), new Polynomial()); + return Tuple.Create(Divide(a, b.Coefficients[0]), Zero); } if (aDegree < bDegree) { // denominator degree higher than nominator degree // quotient always be 0 and return c1 as remainder - return Tuple.Create(new Polynomial(), a); + return Tuple.Create(Zero, a); } var c1 = a.Coefficients.ToArray(); @@ -567,6 +582,7 @@ namespace MathNet.Numerics { c1[k] -= c22[k - i] * v; } + i--; j--; } @@ -588,9 +604,11 @@ namespace MathNet.Numerics return Tuple.Create(new Polynomial(quo), new Polynomial(rem)); } + #endregion #region Arithmetic Pointwise Operations + /// /// Point-wise division of two Polynomials /// @@ -639,9 +657,11 @@ namespace MathNet.Numerics return new Polynomial(result); } + #endregion #region Arithmetic Instance Methods (forwarders) + /// /// Division of two polynomials returning the quotient-with-remainder of the two polynomials given /// @@ -651,9 +671,11 @@ namespace MathNet.Numerics { return DivideRemainder(this, b); } + #endregion #region Arithmetic Operator Overloads (forwarders) + /// /// Addition of two Polynomials (piecewise) /// @@ -773,9 +795,11 @@ namespace MathNet.Numerics { return Divide(a, k); } + #endregion #region ToString + /// /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3". /// @@ -807,6 +831,7 @@ namespace MathNet.Numerics { return ToStringDescending(format, CultureInfo.CurrentCulture); } + /// /// Format the polynomial in ascending order, e.g. "4.3 + 2.0x^2 - x^3". /// @@ -850,6 +875,7 @@ namespace MathNet.Numerics { sb.Append(VariableName); } + if (i > 1) { sb.Append("^"); @@ -870,10 +896,12 @@ namespace MathNet.Numerics sb.Append(" + "); sb.Append(c.ToString(format, formatProvider)); } + if (i > 0) { sb.Append(VariableName); } + if (i > 1) { sb.Append("^"); @@ -912,6 +940,7 @@ namespace MathNet.Numerics { sb.Append(VariableName); } + if (i > 1) { sb.Append("^"); @@ -932,10 +961,12 @@ namespace MathNet.Numerics sb.Append(" + "); sb.Append(c.ToString(format, formatProvider)); } + if (i > 0) { sb.Append(VariableName); } + if (i > 1) { sb.Append("^"); @@ -946,9 +977,11 @@ namespace MathNet.Numerics return sb.ToString(); } + #endregion #region Equality + public bool Equals(Polynomial other) { if (ReferenceEquals(null, other)) return false; @@ -990,11 +1023,14 @@ namespace MathNet.Numerics hash = hash * 31 + Coefficients[i].GetHashCode(); } } + return hash; } + #endregion #region Clone + public Polynomial Clone() { int degree = EvaluateDegree(Coefficients); @@ -1015,6 +1051,7 @@ namespace MathNet.Numerics return Clone(); } #endif + #endregion } }