Class poly1d
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
A one-dimensional polynomial class — the NumSharp port of numpy.poly1d. Encapsulates a
coefficient vector (highest power first) and the natural polynomial operations.
C# has no call syntax, so NumPy's p(x) evaluation is spelled np.polyval(p, x); the
indexer p[k] retrieves the coefficient of x**k exactly like NumPy. NumPy's
p ** n (polynomial power) has no ** operator in C# — spell it with repeated
multiplication (p * p * p), which NumPy defines it to equal. ToString() renders
NumPy's repr form (poly1d([...])); NumPy's separate pretty str() form is not
reproduced (C# has a single ToString).
public class poly1d : IDisposable
- Inheritance
-
poly1d
- Implements
- Inherited Members
Remarks
https://numpy.org/doc/stable/reference/generated/numpy.poly1d.html
Unsealed, as NumPy's poly1d is a plain Python class: a subclass adds members of its own, and every
operation returns a plain poly1d, which is NumPy's answer too (its operators build
poly1d(...) explicitly, so type(Named([1, 2]) + Named([1, 2])) is poly1d). A subclass that
owns resources overrides Dispose(bool).
Constructors
poly1d(NDArray, bool, string)
Construct a polynomial from its coefficients (highest power first), or — when
r is true — from its roots.
public poly1d(NDArray c_or_r, bool r = false, string variable = null)
Parameters
c_or_rNDArrayCoefficients, or (if
r) the polynomial's roots.rboolIf true,
c_or_rgives the roots.variablestringVariable name used when printing (default
"x").
poly1d(poly1d, string)
Copy constructor — copies the coefficients and (unless overridden) the variable name. NumPy's copy shares the coefficient array by reference count; here each polynomial owns its own array (see Dispose()), so a copy is taken rather than an alias that two owners would dispose.
public poly1d(poly1d source, string variable = null)
Parameters
Properties
this[int]
The coefficient of x**k (NumPy's p[k]): a 0-d zero of the coefficient dtype when
k is out of [0, order].
public NDArray this[int k] { get; set; }
Parameters
kint
Property Value
c
Alias of coeffs (NumPy's c).
public NDArray c { get; }
Property Value
coef
Alias of coeffs (NumPy's coef).
public NDArray coef { get; }
Property Value
coefficients
Alias of coeffs (NumPy's coefficients).
public NDArray coefficients { get; }
Property Value
coeffs
The polynomial coefficients, highest power first.
public NDArray coeffs { get; }
Property Value
o
Alias of order (NumPy's o).
public int o { get; }
Property Value
order
The order (degree) of the polynomial.
public int order { get; }
Property Value
r
Alias of roots (NumPy's r).
public NDArray r { get; }
Property Value
roots
The roots of the polynomial, where self(x) == 0.
public NDArray roots { get; }
Property Value
variable
The name of the polynomial variable.
public string variable { get; }
Property Value
Methods
Call(NDArray)
Evaluates the polynomial at x — NumPy's p(x) (poly1d.call), which C#
cannot spell as an invocation of the object itself; equivalent to np.polyval(p, x).
public NDArray Call(NDArray x)
Parameters
xNDArray
Returns
Call(double)
Evaluates the polynomial at a scalar — NumPy's p(x) for a Python float.
public double Call(double x)
Parameters
xdoubleThe point to evaluate at.
Returns
- double
The polynomial's value at
x(a double; a complex polynomial's real part is not representable here — use Call(NDArray)).
Dispose()
Releases the coefficient array this polynomial owns. Idempotent. The polynomial is the array's ONLY owner (both constructors detach it from every ambient scope), so a polynomial that is never disposed keeps its coefficient buffer referenced until a GC finalizes it — dispose every polynomial you create, including temporaries.
public void Dispose()
Dispose(bool)
Releases the coefficient array; what a subclass overrides to release resources of its own, calling the base afterwards.
protected virtual void Dispose(bool disposing)
Parameters
disposingboolTrue from Dispose(). False from a subclass's finalizer, which must not touch other managed objects: the coefficient array then releases its buffer through its own finalizer.
Remarks
Idempotent: the field is cleared before the array is disposed, so a second call releases nothing.
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
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
ToString()
Coefficient list, formatted as poly1d([...]) (NumPy's repr).
public override string ToString()
Returns
deriv(int)
Return a derivative of this polynomial — a NEW polynomial the caller owns.
public poly1d deriv(int m = 1)
Parameters
mintOrder of the derivative.
Returns
- poly1d
The derivative; dispose it.
Exceptions
- ValueError
mis negative (np.polyder).
integ(int, NDArray)
Return an antiderivative (indefinite integral) of this polynomial — a NEW polynomial the caller owns.
public poly1d integ(int m = 1, NDArray k = null)
Parameters
Returns
- poly1d
The antiderivative; dispose it.
Exceptions
- ValueError
mis negative, orkhas the wrong length (np.polyint).
Operators
operator +(poly1d, NDArray)
Sum of a polynomial and coefficients (NumPy's poly1d.add -> polyadd).
public static poly1d operator +(poly1d a, NDArray b)
Parameters
Returns
operator +(poly1d, poly1d)
Sum of two polynomials.
public static poly1d operator +(poly1d a, poly1d b)
Parameters
Returns
operator /(poly1d, NDArray)
Polynomial division by raw coefficients — (quotient, remainder). NumPy's
poly1d.truediv wraps a non-scalar in poly1d then divides, so p / array
is polynomial division (a TUPLE), NOT the element-wise coefficient division the implicit
poly1d -> NDArray conversion would otherwise pick.
public static (poly1d q, poly1d r) operator /(poly1d a, NDArray b)
Parameters
Returns
operator /(poly1d, poly1d)
Polynomial division — returns (quotient, remainder) as poly1d objects.
public static (poly1d q, poly1d r) operator /(poly1d a, poly1d b)
Parameters
Returns
operator /(poly1d, double)
Divide every coefficient by a scalar.
public static poly1d operator /(poly1d a, double s)
Parameters
Returns
operator ==(poly1d, poly1d)
Value equality of two polynomials (same coefficient shape and values).
public static bool operator ==(poly1d a, poly1d b)
Parameters
Returns
implicit operator NDArray(poly1d)
NumPy's array: a poly1d IS its coefficient vector, so it flows into any np.* function.
A consequence is that np.polyder(poly1d) etc. return the bare coefficient array — use the
instance methods (deriv(int), integ(int, NDArray)) or the operators for a poly1d result.
public static implicit operator NDArray(poly1d p)
Parameters
ppoly1d
Returns
operator !=(poly1d, poly1d)
Value inequality of two polynomials.
public static bool operator !=(poly1d a, poly1d b)
Parameters
Returns
operator *(poly1d, NDArray)
Product of a polynomial and coefficients.
public static poly1d operator *(poly1d a, NDArray b)
Parameters
Returns
operator *(poly1d, poly1d)
Product of two polynomials.
public static poly1d operator *(poly1d a, poly1d b)
Parameters
Returns
operator *(poly1d, double)
Scale every coefficient by a scalar (NumPy's isscalar branch).
public static poly1d operator *(poly1d a, double s)
Parameters
Returns
operator *(double, poly1d)
Scale every coefficient by a scalar.
public static poly1d operator *(double s, poly1d a)
Parameters
Returns
operator -(poly1d, NDArray)
Difference of a polynomial and coefficients (NumPy's poly1d.sub -> polysub).
public static poly1d operator -(poly1d a, NDArray b)
Parameters
Returns
operator -(poly1d, poly1d)
Difference of two polynomials.
public static poly1d operator -(poly1d a, poly1d b)
Parameters
Returns
operator -(poly1d)
Negation.
public static poly1d operator -(poly1d a)
Parameters
apoly1d
Returns
operator +(poly1d)
Unary plus (returns the same polynomial).
public static poly1d operator +(poly1d a)
Parameters
apoly1d