Struct Polynomial
Represents a polynomial of a single variable. Supports evaluation, differentiation, integration, root finding, discriminant computation, and arithmetic operations.
Inherited Members
Namespace: Scylla.Core.Util.Math
Assembly: ScyllaCore.dll
Syntax
public struct Polynomial : IEnumerable<Monomial>, IEnumerable, IEquatable<Polynomial>
Remarks
A polynomial is stored as a sparse sorted map from degree to coefficient, so only non-trivial terms consume memory. Terms with coefficients that are negligibly close to zero are automatically omitted.
Polynomials implement IEnumerable<Monomial>,
so they can be iterated with a foreach loop. The enumerator yields terms in
ascending degree order.
Exact root finding (GetRoots()) is supported for polynomials up to degree 3. For higher-degree polynomials use NewtonsMethod(out float, float, float, int) to approximate a root numerically.
Polynomials can be constructed from params float[] coefficients in
ascending degree order, from a dictionary
mapping degrees to coefficients, or from a collection of Monomial
terms. They are also implicitly convertible from float and
Monomial.
Equality comparisons use Approximately(float, float, float) on each coefficient via Equals(Monomial), tolerating floating-point rounding errors.
Constructors
Polynomial(IDictionary<int, float>)
Initializes a new Polynomial from a dictionary that maps each term's degree to its coefficient. This is useful when constructing sparse polynomials where many degrees have zero coefficients.
Declaration
public Polynomial(IDictionary<int, float> coefficients)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<int, float> | coefficients | A dictionary whose keys are term degrees and whose values are the corresponding coefficients. Near-zero coefficient values are automatically omitted. |
Polynomial(IEnumerable<Monomial>)
Initializes a new Polynomial as the sum of a collection of Monomial terms. Terms with the same degree are combined by summing their coefficients.
Declaration
public Polynomial(IEnumerable<Monomial> terms)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<Monomial> | terms | The monomials to sum into the polynomial. May be empty, in which case the result is the zero polynomial. |
Polynomial(params float[])
Initializes a new Polynomial from a flat array of coefficients in ascending degree order. The first element is the constant term (degree 0), the second is the linear coefficient (degree 1), and so on.
Declaration
public Polynomial(params float[] coefficients)
Parameters
| Type | Name | Description |
|---|---|---|
| float[] | coefficients | The coefficients of the polynomial in ascending degree order. For example,
passing |
Remarks
var p = new Polynomial(1f, -3f, 2f); // 2x^2 - 3x + 1
float value = p.Evaluate(2f); // = 2*(4) - 3*(2) + 1 = 3
Properties
Degree
Gets the degree of this polynomial: the highest exponent that has a non-zero
coefficient. Returns 0 for the zero polynomial or a constant polynomial.
Declaration
public int Degree { get; }
Property Value
| Type | Description |
|---|---|
| int | A non-negative int. For example, the polynomial |
IsEven
Gets a value indicating whether this polynomial is an even function -
i.e., all non-trivial terms have even degree. An even polynomial satisfies
f(x) = f(-x) for all x.
Declaration
public bool IsEven { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
IsOdd
Gets a value indicating whether this polynomial is an odd function -
i.e., all non-trivial terms have odd degree. An odd polynomial satisfies
-f(x) = f(-x) for all x.
Declaration
public bool IsOdd { get; }
Property Value
| Type | Description |
|---|---|
| bool |
|
this[int]
Gets or sets the coefficient of the term with the specified degree.
Setting a coefficient to a value negligibly close to zero removes the corresponding
term from the polynomial.
Declaration
public float this[int degree] { get; set; }
Parameters
| Type | Name | Description |
|---|---|---|
| int | degree | The degree of the term whose coefficient is being accessed or mutated. |
Property Value
| Type | Description |
|---|---|
| float | When getting, returns the coefficient of the term at |
Zero
The zero polynomial. Has no non-trivial terms and evaluates to 0 for every
value of x. The additive identity for Polynomial arithmetic.
Declaration
public static Polynomial Zero { get; }
Property Value
| Type | Description |
|---|---|
| Polynomial |
Methods
Bernstein(int, int)
Returns the i-th Bernstein basis polynomial of degree
n, defined as B(i,n,x) = C(n,i) * x^i * (1-x)^(n-i),
where C(n,i) is the binomial coefficient. Bernstein basis polynomials form
a partition of unity over [0, 1] and are the mathematical foundation of
Bezier curves.
Declaration
public static Polynomial Bernstein(int i, int n)
Parameters
| Type | Name | Description |
|---|---|---|
| int | i | The index of the basis polynomial, in the range |
| int | n | The degree of the Bernstein basis. Must be a non-negative integer. All
|
Returns
| Type | Description |
|---|---|
| Polynomial | The Bernstein basis polynomial |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException | Thrown when |
See Also
BernsteinBasis(int)
Returns an array of all n+1 Bernstein basis polynomials of degree
n. The result at index i is equivalent to
Bernstein(i, n).
Declaration
public static Polynomial[] BernsteinBasis(int n)
Parameters
| Type | Name | Description |
|---|---|---|
| int | n | The degree of the Bernstein basis. Must be a non-negative integer. Passing
|
Returns
| Type | Description |
|---|---|
| Polynomial[] | An array of length |
See Also
Equals(Polynomial)
Determines whether this Polynomial is equal to another Polynomial. Equality is established by comparing each pair of corresponding terms using Equals(Monomial), which uses Approximately(float, float, float) for coefficient comparison.
Declaration
public bool Equals(Polynomial other)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | other | The Polynomial to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this Polynomial is equal to another object.
Declaration
public override bool Equals(object obj)
Parameters
| Type | Name | Description |
|---|---|---|
| object | obj | The object to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Overrides
Evaluate(float)
Evaluates this polynomial at the given value of the variable by summing the evaluations of all individual terms.
Declaration
public float Evaluate(float x)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The value at which to evaluate the polynomial. |
Returns
| Type | Description |
|---|---|
| float | The result of substituting |
Expand(Polynomial, uint)
Raises a polynomial to a non-negative integer power by repeated multiplication.
Equivalent to p * p * ... * p (power times).
Declaration
public static Polynomial Expand(Polynomial p, uint power)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial base to expand. |
| uint | power | The non-negative integer exponent. A value of |
Returns
| Type | Description |
|---|---|
| Polynomial | The polynomial |
Remarks
This method is used internally by Bernstein(int, int) to compute
the (1 - x)^(n-i) factor of each Bernstein basis polynomial.
GetAntiderivative()
Computes the symbolic antiderivative (indefinite integral) of this polynomial,
excluding the constant of integration. The antiderivative of each term
c * x^n is (c / (n+1)) * x^(n+1).
Declaration
public Polynomial GetAntiderivative()
Returns
| Type | Description |
|---|---|
| Polynomial | A new Polynomial representing the antiderivative. The degree is one higher than the original. |
Remarks
This method does not handle terms of degree -1 (the logarithmic case).
Polynomials constructed via the standard constructors will not contain such terms,
but monomials created with negative degrees could produce NaN or
Infinity in the result.
See Also
GetDerivative()
Computes the symbolic derivative of this polynomial using the power rule on
each term. The derivative of each term c * x^n is (n*c) * x^(n-1).
Constant terms vanish.
Declaration
public Polynomial GetDerivative()
Returns
| Type | Description |
|---|---|
| Polynomial | A new Polynomial representing the first derivative. For the zero polynomial or a constant, returns the zero polynomial. |
See Also
GetDiscriminant()
Computes the discriminant of this polynomial. The discriminant encodes the nature of the polynomial's roots without requiring them to be computed explicitly.
Declaration
public float GetDiscriminant()
Returns
| Type | Description |
|---|---|
| float | The discriminant value, whose sign has the following interpretation depending on the degree:
|
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when trying to compute the discriminant of a degree 0 (constant) polynomial, for which the discriminant is undefined. |
| NotSupportedException | Thrown for polynomials of degree 5 or greater. The general discriminant is defined for
every degree, but it grows as a degree |
See Also
GetEnumerator()
Returns an enumerator that iterates through the non-trivial Monomial terms of this polynomial in ascending degree order.
Declaration
public IEnumerator<Monomial> GetEnumerator()
Returns
| Type | Description |
|---|---|
| IEnumerator<Monomial> | An IEnumerator<Monomial> over the non-zero terms. Yields no elements for the zero polynomial. |
GetHashCode()
Returns a hash code for this Polynomial derived from the hash codes of all its non-trivial Monomial terms.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | An int hash code suitable for use in hash-based collections. |
Overrides
GetRoots()
Computes the exact real roots of this polynomial (the values of x for which
the polynomial evaluates to zero). Uses closed-form formulae appropriate to each
supported degree.
Declaration
public float[] GetRoots()
Returns
| Type | Description |
|---|---|
| float[] | An array of real roots. The number of roots depends on the degree and discriminant:
|
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the polynomial's Degree is greater than |
See Also
Integrate(float, float)
Computes the definite integral of this polynomial over the interval
[ using the fundamental theorem
of calculus: a, b]F(b) - F(a), where F is the antiderivative.
Declaration
public float Integrate(float a, float b)
Parameters
| Type | Name | Description |
|---|---|---|
| float | a | The lower limit of integration. |
| float | b | The upper limit of integration. |
Returns
| Type | Description |
|---|---|
| float | The signed area under the polynomial curve between |
See Also
NewtonsMethod(out float, float, float, int)
Approximates a real root of this polynomial using Newton's method (Newton-Raphson
iteration). Starting from initialGuess, the method repeatedly
refines the estimate by computing x_next = x - f(x) / f'(x) until the
polynomial evaluates sufficiently close to zero or the iteration limit is reached.
Declaration
public bool NewtonsMethod(out float root, float initialGuess = 0, float delta = 0.01, int maxIterations = 30)
Parameters
| Type | Name | Description |
|---|---|---|
| float | root | When this method returns |
| float | initialGuess | The starting x-value for the iteration. The closer this value is to an actual root,
the faster and more reliably the method converges. Defaults to |
| float | delta | The convergence threshold: the method considers a root found when
|
| int | maxIterations | The maximum number of Newton-Raphson iterations before giving up. Defaults to
|
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
For polynomials of degree 3 or lower it is recommended to use GetRoots() instead, which finds all real roots exactly.
Newton's method may fail to converge if the derivative is zero during iteration
(a local extremum is encountered), or if initialGuess is far
from any real root. It is not guaranteed to find all roots - only one per call.
See Also
ToString()
Returns a string representation of this polynomial using "x" as the
variable name, with terms ordered from highest to lowest degree.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string such as |
Overrides
See Also
ToString(string)
Returns a string representation of this polynomial using the specified variable name, with terms ordered from highest to lowest degree.
Declaration
public string ToString(string variableName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | variableName | The name of the variable to use in the output (e.g., |
Returns
| Type | Description |
|---|---|
| string | A string such as |
Operators
operator +(Monomial, Polynomial)
Adds a Monomial to a Polynomial. The monomial's coefficient is accumulated into the polynomial's term at the matching degree.
Declaration
public static Polynomial operator +(Monomial m, Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial to add. |
| Polynomial | p | The polynomial to add to. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator +(Polynomial, Monomial)
Adds a Monomial to a Polynomial. The monomial's coefficient is accumulated into the polynomial's term at the matching degree. If the resulting coefficient for that degree is negligibly close to zero, the term is removed.
Declaration
public static Polynomial operator +(Polynomial p, Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial to add to. |
| Monomial | m | The monomial to add. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator +(Polynomial, Polynomial)
Adds two polynomials by accumulating each term of p2 into
p1. Terms with the same degree have their coefficients summed.
Terms that cancel to zero are automatically removed.
Declaration
public static Polynomial operator +(Polynomial p1, Polynomial p2)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p1 | The left-hand addend, which is also the base of the result. |
| Polynomial | p2 | The right-hand addend whose terms are merged into |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to the sum |
operator /(Polynomial, float)
Divides a Polynomial by a scalar, dividing every coefficient.
Declaration
public static Polynomial operator /(Polynomial p, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial to divide. |
| float | k | The scalar divisor. Must not be zero. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial with every coefficient divided by |
operator ==(Polynomial, Polynomial)
Returns true if two Polynomial values are equal, comparing
each term using approximate coefficient equality.
Declaration
public static bool operator ==(Polynomial a, Polynomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | a | The left-hand operand. |
| Polynomial | b | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
implicit operator Polynomial(Monomial)
Implicitly converts a Monomial to a Polynomial containing that single term. Allows monomials to be used in polynomial arithmetic expressions directly.
Declaration
public static implicit operator Polynomial(Monomial monomial)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | monomial | The monomial to convert. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial containing only the term |
implicit operator Polynomial(float)
Implicitly converts a float constant to a Polynomial with a single constant term (degree 0). Allows scalar values to be used in polynomial arithmetic expressions directly.
Declaration
public static implicit operator Polynomial(float constant)
Parameters
| Type | Name | Description |
|---|---|---|
| float | constant | The scalar constant to convert. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to the constant value |
operator !=(Polynomial, Polynomial)
Returns true if two Polynomial values are not equal.
Declaration
public static bool operator !=(Polynomial a, Polynomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | a | The left-hand operand. |
| Polynomial | b | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator *(Monomial, Polynomial)
Multiplies a Monomial by a Polynomial by distributing the monomial across each term of the polynomial.
Declaration
public static Polynomial operator *(Monomial m, Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial multiplier. |
| Polynomial | p | The polynomial multiplicand. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator *(Polynomial, Monomial)
Multiplies a Polynomial by a Monomial by distributing
the monomial across each term of the polynomial. Each term c_i * x^i
becomes (c_i * m.Coefficient) * x^(i + m.Degree).
Declaration
public static Polynomial operator *(Polynomial p, Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial multiplicand. |
| Monomial | m | The monomial multiplier. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator *(Polynomial, Polynomial)
Multiplies two polynomials using polynomial long multiplication, distributing
each term of p2 across all terms of p1
and accumulating the products. The degree of the result is the sum of the
degrees of the operands.
Declaration
public static Polynomial operator *(Polynomial p1, Polynomial p2)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p1 | The left-hand factor. |
| Polynomial | p2 | The right-hand factor. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to the product |
operator *(Polynomial, float)
Multiplies a Polynomial by a scalar, scaling every coefficient.
Declaration
public static Polynomial operator *(Polynomial p, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial to scale. |
| float | k | The scalar multiplier. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial with every coefficient multiplied by |
operator *(float, Polynomial)
Multiplies a scalar by a Polynomial, scaling every coefficient.
Declaration
public static Polynomial operator *(float k, Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| float | k | The scalar multiplier. |
| Polynomial | p | The polynomial to scale. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial with every coefficient multiplied by |
operator -(Monomial, Polynomial)
Subtracts a Polynomial from a Monomial, returning
a new polynomial equal to m - p.
Declaration
public static Polynomial operator -(Monomial m, Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial minuend. |
| Polynomial | p | The polynomial subtrahend. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator -(Polynomial, Monomial)
Subtracts a Monomial from a Polynomial by adding its negation. The monomial's coefficient is subtracted from the polynomial's term at the matching degree.
Declaration
public static Polynomial operator -(Polynomial p, Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial to subtract from. |
| Monomial | m | The monomial to subtract. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator -(Polynomial, Polynomial)
Subtracts one polynomial from another by accumulating the negation of each term
of p2 into p1.
Declaration
public static Polynomial operator -(Polynomial p1, Polynomial p2)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p1 | The minuend polynomial. |
| Polynomial | p2 | The subtrahend polynomial. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial equal to |
operator -(Polynomial)
Unary negation operator. Returns a new polynomial with all coefficients negated, i.e., the additive inverse of this polynomial.
Declaration
public static Polynomial operator -(Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The polynomial to negate. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial where every coefficient has the opposite sign. |
operator +(Polynomial)
Unary plus operator. Returns the polynomial unchanged.
Declaration
public static Polynomial operator +(Polynomial p)
Parameters
| Type | Name | Description |
|---|---|---|
| Polynomial | p | The operand. |
Returns
| Type | Description |
|---|---|
| Polynomial | The same Polynomial value as |