Table of Contents

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_r NDArray

Coefficients, or (if r) the polynomial's roots.

r bool

If true, c_or_r gives the roots.

variable string

Variable 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

source poly1d
variable string

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

k int

Property Value

NDArray

c

Alias of coeffs (NumPy's c).

public NDArray c { get; }

Property Value

NDArray

coef

Alias of coeffs (NumPy's coef).

public NDArray coef { get; }

Property Value

NDArray

coefficients

Alias of coeffs (NumPy's coefficients).

public NDArray coefficients { get; }

Property Value

NDArray

coeffs

The polynomial coefficients, highest power first.

public NDArray coeffs { get; }

Property Value

NDArray

o

Alias of order (NumPy's o).

public int o { get; }

Property Value

int

order

The order (degree) of the polynomial.

public int order { get; }

Property Value

int

r

Alias of roots (NumPy's r).

public NDArray r { get; }

Property Value

NDArray

roots

The roots of the polynomial, where self(x) == 0.

public NDArray roots { get; }

Property Value

NDArray

variable

The name of the polynomial variable.

public string variable { get; }

Property Value

string

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

x NDArray

Returns

NDArray

Call(double)

Evaluates the polynomial at a scalar — NumPy's p(x) for a Python float.

public double Call(double x)

Parameters

x double

The 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

disposing bool

True 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

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.

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

string

deriv(int)

Return a derivative of this polynomial — a NEW polynomial the caller owns.

public poly1d deriv(int m = 1)

Parameters

m int

Order of the derivative.

Returns

poly1d

The derivative; dispose it.

Exceptions

ValueError

m is 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

m int

Order of the antiderivative.

k NDArray

Integration constants (NumPy's k); null means zeros.

Returns

poly1d

The antiderivative; dispose it.

Exceptions

ValueError

m is negative, or k has 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

a poly1d
b NDArray

Returns

poly1d

operator +(poly1d, poly1d)

Sum of two polynomials.

public static poly1d operator +(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

poly1d

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

a poly1d
b NDArray

Returns

(poly1d q, poly1d r)

operator /(poly1d, poly1d)

Polynomial division — returns (quotient, remainder) as poly1d objects.

public static (poly1d q, poly1d r) operator /(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

(poly1d q, poly1d r)

operator /(poly1d, double)

Divide every coefficient by a scalar.

public static poly1d operator /(poly1d a, double s)

Parameters

a poly1d
s double

Returns

poly1d

operator ==(poly1d, poly1d)

Value equality of two polynomials (same coefficient shape and values).

public static bool operator ==(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

bool

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

p poly1d

Returns

NDArray

operator !=(poly1d, poly1d)

Value inequality of two polynomials.

public static bool operator !=(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

bool

operator *(poly1d, NDArray)

Product of a polynomial and coefficients.

public static poly1d operator *(poly1d a, NDArray b)

Parameters

a poly1d
b NDArray

Returns

poly1d

operator *(poly1d, poly1d)

Product of two polynomials.

public static poly1d operator *(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

poly1d

operator *(poly1d, double)

Scale every coefficient by a scalar (NumPy's isscalar branch).

public static poly1d operator *(poly1d a, double s)

Parameters

a poly1d
s double

Returns

poly1d

operator *(double, poly1d)

Scale every coefficient by a scalar.

public static poly1d operator *(double s, poly1d a)

Parameters

s double
a poly1d

Returns

poly1d

operator -(poly1d, NDArray)

Difference of a polynomial and coefficients (NumPy's poly1d.sub -> polysub).

public static poly1d operator -(poly1d a, NDArray b)

Parameters

a poly1d
b NDArray

Returns

poly1d

operator -(poly1d, poly1d)

Difference of two polynomials.

public static poly1d operator -(poly1d a, poly1d b)

Parameters

a poly1d
b poly1d

Returns

poly1d

operator -(poly1d)

Negation.

public static poly1d operator -(poly1d a)

Parameters

a poly1d

Returns

poly1d

operator +(poly1d)

Unary plus (returns the same polynomial).

public static poly1d operator +(poly1d a)

Parameters

a poly1d

Returns

poly1d