Struct Monomial
Represents a single term of a Polynomial, of the form
c * x^n, where c is the Coefficient and n is the
Degree. Supports evaluation, differentiation, integration, and
scalar/monomial arithmetic.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Math
Assembly: ScyllaCore.dll
Syntax
[Serializable]
public struct Monomial : IEquatable<Monomial>
Remarks
A monomial is the building block of a Polynomial. Adding or
subtracting two monomials via the + and - operators produces a
Polynomial because the result may contain two distinct terms.
Multiplying or dividing monomials always produces another Monomial
because the degrees add or subtract.
This struct is serializable and can be exposed in the Unity Inspector. Equality comparisons use Approximately(float, float, float) to tolerate floating-point rounding errors in the coefficient while requiring exact degree equality.
Constructors
Monomial(float, int)
Initializes a new Monomial representing the term
.coefficient * x^degree
Declaration
public Monomial(float coefficient, int degree)
Parameters
| Type | Name | Description |
|---|---|---|
| float | coefficient | The scalar multiplier of the term. A value of zero creates a trivial monomial that always evaluates to zero. |
| int | degree | The exponent applied to the variable. Zero produces a constant term, negative values produce inverse-power terms. |
Fields
Coefficient
The coefficient c of the term c * x^n. A coefficient of zero
makes the monomial trivial (evaluates to zero for all x).
Declaration
[SerializeField]
public float Coefficient
Field Value
| Type | Description |
|---|---|
| float |
Degree
The degree (exponent) n of the term c * x^n. A degree of zero
makes the monomial a constant equal to Coefficient. Negative degrees
are permitted and represent negative powers of x.
Declaration
[SerializeField]
public int Degree
Field Value
| Type | Description |
|---|---|
| int |
Methods
Equals(Monomial)
Determines whether this Monomial is equal to another Monomial. The coefficients are compared using Approximately(float, float, float) to tolerate floating-point rounding errors, while the degrees are compared exactly.
Declaration
public bool Equals(Monomial other)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | other | The Monomial to compare with this instance. |
Returns
| Type | Description |
|---|---|
| bool |
|
Equals(object)
Determines whether this Monomial 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 monomial at the given value of the variable, returning
Coefficient * x^Degree.
Declaration
public float Evaluate(float x)
Parameters
| Type | Name | Description |
|---|---|---|
| float | x | The value at which to evaluate the monomial. |
Returns
| Type | Description |
|---|---|
| float | The result of |
GetAntiderivative()
Computes the symbolic antiderivative (indefinite integral) of this monomial,
excluding the constant of integration. The antiderivative of c * x^n
is (c / (n+1)) * x^(n+1).
Declaration
public Monomial GetAntiderivative()
Returns
| Type | Description |
|---|---|
| Monomial | A new Monomial with coefficient |
Remarks
This method does not guard against Degree equal to -1
(the logarithmic case). Calling it on a monomial with degree -1 will
produce a division by zero in the coefficient, resulting in Infinity or
NaN.
See Also
GetDerivative()
Computes the symbolic derivative of this monomial using the power rule.
The derivative of c * x^n is (n*c) * x^(n-1).
Declaration
public Monomial GetDerivative()
Returns
| Type | Description |
|---|---|
| Monomial | A new Monomial with coefficient |
See Also
GetHashCode()
Returns a hash code for this Monomial based on both the Coefficient and the Degree.
Declaration
public override int GetHashCode()
Returns
| Type | Description |
|---|---|
| int | An int hash code suitable for use in hash-based collections. |
Overrides
Integrate(float, float)
Computes the definite integral of this monomial 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 monomial curve between |
Remarks
Delegates to GetAntiderivative() and therefore inherits its
restriction: do not use on monomials with Degree equal to -1.
See Also
ToString()
Returns a string representation of this monomial using "x" as the
variable name.
Declaration
public override string ToString()
Returns
| Type | Description |
|---|---|
| string | A string such as |
Overrides
See Also
ToString(string)
Returns a string representation of this monomial using the specified variable name.
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 |
Operators
operator +(Monomial, Monomial)
Adds two monomials together, returning a Polynomial containing both terms. If both monomials share the same Degree, the Polynomial will combine them into a single term with a summed coefficient.
Declaration
public static Polynomial operator +(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The left-hand addend. |
| Monomial | b | The right-hand addend. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial representing the sum |
operator /(Monomial, Monomial)
Divides one monomial by another. The resulting monomial has the quotient of their
coefficients and the difference of their degrees:
(a * x^n) / (b * x^m) = (a/b) * x^(n-m).
Declaration
public static Monomial operator /(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The dividend monomial. |
| Monomial | b | The divisor monomial. Its coefficient must not be zero. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to
|
operator /(Monomial, float)
Divides a monomial by a scalar, reducing its Coefficient while leaving the Degree unchanged.
Declaration
public static Monomial operator /(Monomial m, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial dividend. |
| float | k | The scalar divisor. Must not be zero. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to |
operator ==(Monomial, Monomial)
Returns true if two Monomial values are equal, comparing
coefficients approximately and degrees exactly.
Declaration
public static bool operator ==(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The left-hand operand. |
| Monomial | b | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
implicit operator Monomial(float)
Implicitly converts a float constant to a Monomial
of degree 0 with a coefficient equal to the constant. This allows scalar
values to be used wherever a constant term is expected.
Declaration
public static implicit operator Monomial(float constant)
Parameters
| Type | Name | Description |
|---|---|---|
| float | constant | The scalar constant to convert. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to |
operator !=(Monomial, Monomial)
Returns true if two Monomial values are not equal.
Declaration
public static bool operator !=(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The left-hand operand. |
| Monomial | b | The right-hand operand. |
Returns
| Type | Description |
|---|---|
| bool |
|
operator *(Monomial, Monomial)
Multiplies two monomials together. The resulting monomial has the product of their
coefficients and the sum of their degrees: (a * x^n) * (b * x^m) = (a*b) * x^(n+m).
Declaration
public static Monomial operator *(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The left-hand factor. |
| Monomial | b | The right-hand factor. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to
|
operator *(Monomial, float)
Multiplies a monomial by a scalar, scaling its Coefficient while leaving the Degree unchanged.
Declaration
public static Monomial operator *(Monomial m, float k)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial to scale. |
| float | k | The scalar multiplier. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to |
operator *(float, Monomial)
Multiplies a scalar by a monomial, scaling its Coefficient while leaving the Degree unchanged.
Declaration
public static Monomial operator *(float k, Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| float | k | The scalar multiplier. |
| Monomial | m | The monomial to scale. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with Coefficient equal to |
operator -(Monomial, Monomial)
Subtracts one monomial from another, returning a Polynomial containing both terms. If both monomials share the same Degree, the Polynomial will combine them into a single term with the difference as its coefficient.
Declaration
public static Polynomial operator -(Monomial a, Monomial b)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | a | The minuend monomial. |
| Monomial | b | The subtrahend monomial. |
Returns
| Type | Description |
|---|---|
| Polynomial | A Polynomial representing the difference |
operator -(Monomial)
Unary negation operator. Returns the additive inverse of the monomial by negating its Coefficient while preserving the Degree.
Declaration
public static Monomial operator -(Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The monomial to negate. |
Returns
| Type | Description |
|---|---|
| Monomial | A Monomial with the same Degree and negated Coefficient. |
operator +(Monomial)
Unary plus operator. Returns the monomial unchanged.
Declaration
public static Monomial operator +(Monomial m)
Parameters
| Type | Name | Description |
|---|---|---|
| Monomial | m | The operand. |
Returns
| Type | Description |
|---|---|
| Monomial | The same Monomial value as |