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 sealed class poly1d : IDisposable
- Inheritance
-
poly1d
- Implements
- Inherited Members
Remarks
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
xdouble
Returns
Dispose()
Releases the coefficient array this polynomial owns. Idempotent. A polynomial constructed
inside an [NDScoped] method has its array tracked by that scope as well (the
constructor yields it to the ambient scope), so disposing there is a no-op either way.
public void Dispose()
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.
public poly1d deriv(int m = 1)
Parameters
mint
Returns
integ(int, NDArray)
Return an antiderivative (indefinite integral) of this polynomial.
public poly1d integ(int m = 1, NDArray k = null)
Parameters
Returns
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