Table of Contents

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

T

Floating-point scalar type.

Inheritance
Polynomial<T>
Implements
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

coefficients IEnumerable<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

int

this[int]

Returns the coefficient of xⁱ.

public T this[int i] { get; }

Parameters

i int

Power 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

other Polynomial<T>

The polynomial to compare with.

tolerance T

Maximum allowed absolute difference per coefficient. Must be finite and non-negative.

Returns

bool

Exceptions

ArgumentOutOfRangeException

Thrown when tolerance is 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

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

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

other Polynomial<T>

Returns

bool

Evaluate(T)

Evaluates the polynomial at x using Horner's method.

public T Evaluate(T x)

Parameters

x T

The 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

initialGuess T

Starting point for the iteration. Must be finite.

maxIterations int

Maximum number of iterations. Must be greater than zero.

tolerance T?

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 maxIterations is not positive, initialGuess is not finite, or tolerance is 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

constant T

Constant 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

string

Operators

operator +(Polynomial<T>, Polynomial<T>)

Returns the sum of two polynomials.

public static Polynomial<T> operator +(Polynomial<T> a, Polynomial<T> b)

Parameters

a Polynomial<T>
b Polynomial<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

a Polynomial<T>
b Polynomial<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

p Polynomial<T>
scalar T

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

scalar T
p Polynomial<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

a Polynomial<T>
b Polynomial<T>

Returns

Polynomial<T>