Browse Source

Polynomial: add static Zero to get the zero-polynomial

ridge-regression
Christoph Ruegg 8 years ago
parent
commit
6c0bc89342
  1. 10
      src/Numerics.Tests/PolynomialTests.cs
  2. 43
      src/Numerics/Polynomial.cs

10
src/Numerics.Tests/PolynomialTests.cs

@ -218,25 +218,25 @@ namespace MathNet.Numerics.UnitTests
Assert.Throws(typeof(DivideByZeroException), () => Assert.Throws(typeof(DivideByZeroException), () =>
{ {
var p1 = new Polynomial(1.0d); var p1 = new Polynomial(1.0d);
var p2 = new Polynomial(); var p2 = Polynomial.Zero;
GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); GC.KeepAlive(Polynomial.DivideRemainder(p1, p2));
}); });
Assert.DoesNotThrow(() => Assert.DoesNotThrow(() =>
{ {
var p1 = new Polynomial(); var p1 = Polynomial.Zero;
var p2 = new Polynomial(1.0d); var p2 = new Polynomial(1.0d);
GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); GC.KeepAlive(Polynomial.DivideRemainder(p1, p2));
}); });
Assert.Throws(typeof(DivideByZeroException), () => Assert.Throws(typeof(DivideByZeroException), () =>
{ {
var p1 = new Polynomial(); var p1 = Polynomial.Zero;
var p2 = new Polynomial(); var p2 = Polynomial.Zero;
GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); GC.KeepAlive(Polynomial.DivideRemainder(p1, p2));
}); });
Assert.Throws(typeof(DivideByZeroException), () => Assert.Throws(typeof(DivideByZeroException), () =>
{ {
var p1 = new Polynomial(1.0d); var p1 = new Polynomial(1.0d);
var p2 = new Polynomial(); var p2 = Polynomial.Zero;
GC.KeepAlive(Polynomial.DivideRemainder(p1, p2)); GC.KeepAlive(Polynomial.DivideRemainder(p1, p2));
}); });
} }

43
src/Numerics/Polynomial.cs

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

Loading…
Cancel
Save