Class Polynomial<T>
- Namespace
- Utils.Mathematics
- Assembly
- Utils.Mathematics.dll
Represents a polynomial with coefficients of type T.
Coefficients are stored as [a₀, a₁, …, aₙ] representing a₀ + a₁x + … + aₙxⁿ.
public sealed class Polynomial<T> : IEquatable<Polynomial<T>> where T : struct, IFloatingPoint<T>
Type Parameters
TFloating-point scalar type.
- Inheritance
-
Polynomial<T>
- Implements
-
IEquatable<Polynomial<T>>
- Inherited Members
- Extension Methods
Constructors
Polynomial(params IEnumerable<T>)
Initializes a polynomial from its coefficients in ascending power order. Trailing exact-zero coefficients are trimmed so the stored degree is minimal.
public Polynomial(params IEnumerable<T> coefficients)
Parameters
coefficientsIEnumerable<T>Coefficients [a₀, a₁, …, aₙ]. At least one value is required.
Exceptions
- ArgumentException
Thrown when no coefficients are provided.
Properties
Degree
Gets the degree of the polynomial (highest power with a non-zero coefficient).
public int Degree { get; }
Property Value
this[int]
Returns the coefficient of xⁱ.
public T this[int i] { get; }
Parameters
iintPower index.
Property Value
- T
Methods
ApproximatelyEquals(Polynomial<T>?, T)
Determines whether this polynomial is equal to other within
tolerance on every coefficient. This is a separate, explicitly opt-in
comparison: it is not used by Equals(Polynomial<T>?) or
GetHashCode() and must not be relied upon for hashed-collection membership, since
approximate equality is not transitive in general and would violate the hash contract.
public bool ApproximatelyEquals(Polynomial<T>? other, T tolerance)
Parameters
otherPolynomial<T>The polynomial to compare with.
toleranceTMaximum allowed absolute difference per coefficient. Must be finite and non-negative.
Returns
Exceptions
- ArgumentOutOfRangeException
Thrown when
toleranceis not finite or is negative.
Derive()
Returns the formal derivative of this polynomial.
public Polynomial<T> Derive()
Returns
- Polynomial<T>
A new polynomial representing the derivative.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Equals(Polynomial<T>?)
Determines whether this polynomial is exactly equal to other: same degree
and identical coefficients. Consistent with GetHashCode() by construction, unlike
a tolerance-based comparison would be. Use ApproximatelyEquals(Polynomial<T>?, T) for a
tolerance-aware comparison; it must not be used to implement this member or
GetHashCode(); doing so would let two polynomials compare equal while hashing
differently, breaking the contract required by Dictionary<TKey, TValue>,
HashSet<T>, and LINQ set operations.
public bool Equals(Polynomial<T>? other)
Parameters
otherPolynomial<T>
Returns
Evaluate(T)
Evaluates the polynomial at x using Horner's method.
public T Evaluate(T x)
Parameters
xTThe point at which to evaluate.
Returns
- T
The value of the polynomial at
x.
FindRoot(T, int, T?)
Attempts to find a real root near initialGuess using Newton–Raphson iteration.
public T? FindRoot(T initialGuess, int maxIterations = 100, T? tolerance = null)
Parameters
initialGuessTStarting point for the iteration. Must be finite.
maxIterationsintMaximum number of iterations. Must be greater than zero.
toleranceT?Convergence tolerance; defaults to 1e-10. Must be finite and positive.
Returns
- T?
The found root, or null if the method did not converge.
Exceptions
- ArgumentOutOfRangeException
Thrown when
maxIterationsis not positive,initialGuessis not finite, ortoleranceis not finite and positive.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
Integrate(T)
Returns an antiderivative of this polynomial with the given integration constant.
public Polynomial<T> Integrate(T constant = default)
Parameters
constantTConstant of integration (default: zero).
Returns
- Polynomial<T>
A new polynomial representing the antiderivative.
ToString()
Renders the polynomial as e.g. "3x^2 - 2x + 1": each term's sign is applied as a separator
between terms (" + "/" - " or a leading "-" for the first term) rather than
baked into the coefficient's own ToString(), which previously produced
double-signed sequences such as "2x + -3" whenever a non-leading term was negative. A unit
coefficient (±1) on a non-constant term omits the redundant magnitude ("x"/"-x"
rather than "1x"/"-1x").
public override string ToString()
Returns
Operators
operator +(Polynomial<T>, Polynomial<T>)
Returns the sum of two polynomials.
public static Polynomial<T> operator +(Polynomial<T> a, Polynomial<T> b)
Parameters
aPolynomial<T>bPolynomial<T>
Returns
- Polynomial<T>
operator *(Polynomial<T>, Polynomial<T>)
Returns the product of two polynomials.
public static Polynomial<T> operator *(Polynomial<T> a, Polynomial<T> b)
Parameters
aPolynomial<T>bPolynomial<T>
Returns
- Polynomial<T>
operator *(Polynomial<T>, T)
Returns the polynomial scaled by a scalar.
public static Polynomial<T> operator *(Polynomial<T> p, T scalar)
Parameters
pPolynomial<T>scalarT
Returns
- Polynomial<T>
operator *(T, Polynomial<T>)
Returns the polynomial scaled by a scalar.
public static Polynomial<T> operator *(T scalar, Polynomial<T> p)
Parameters
scalarTpPolynomial<T>
Returns
- Polynomial<T>
operator -(Polynomial<T>, Polynomial<T>)
Returns the difference of two polynomials.
public static Polynomial<T> operator -(Polynomial<T> a, Polynomial<T> b)
Parameters
aPolynomial<T>bPolynomial<T>
Returns
- Polynomial<T>