Class LegendreModule
- Namespace
- NumSharp
- Assembly
- NumSharp.dll
numpy.polynomial.legendre — Legendre series p(x) = c[0]*P_0(x) + c[1]*P_1(x) + …, reachable as
np.polynomial.legendre. Coefficients are LOW degree first.
[ModuleName("np.polynomial.legendre")]
public class LegendreModule
- Inheritance
-
LegendreModule
- Inherited Members
Remarks
Properties
legdomain
legdomain — the default domain of the Legendre series class, float64 [-1.0, 1.0].
public NDArray legdomain { get; }
Property Value
Remarks
NumPy's module constants are ordinary WRITEABLE ndarrays, the same object on every access, and so is this one: a process-wide instance (a write through it persists, as in NumPy). It is shared — do not dispose it (a dispose is harmless: the buffer is pinned for the process lifetime) and copy it before mutating unless the change is meant to be global.
https://numpy.org/doc/stable/reference/generated/numpy.polynomial.legendre.html
legone
legone — the Legendre series representing 1, int64 [1] (shared and writeable, see legdomain).
public NDArray legone { get; }
Property Value
Remarks
legx
legx — the Legendre series representing x, int64 [0, 1] (shared and writeable, see legdomain).
public NDArray legx { get; }
Property Value
Remarks
legzero
legzero — the Legendre series representing 0, int64 [0] (shared and writeable, see legdomain).
public NDArray legzero { get; }
Property Value
Remarks
Methods
Legendre(object, object, object, string)
NumPy's numpy.polynomial.legendre.Legendre(coef, domain=None, window=None, symbol='x'): a Legendre series — a new
NDLegendre (the class itself; its class methods fit, fromroots, identity,
basis and cast are static members of NDLegendre).
public NDLegendre Legendre(object coef, object domain = null, object window = null, string symbol = "x")
Parameters
coefobjectThe coefficients, low degree first (see NDLegendre(object, object, object, string)).
domainobjectThe domain, or null for the class default.
windowobjectThe window, or null for the class default.
symbolstringThe variable's name for printing: a Python identifier.
Returns
- NDLegendre
The series (dispose it when done).
Remarks
Exceptions
- ValueError
The constructor's errors (an empty or non-1-d series, a domain or window of the wrong length, an invalid symbol).
- TypeError
A null symbol.
- NotSupportedException
Coefficients NumPy keeps as an object array.
leg2poly(object)
Converts the Legendre series c to a power series — NumPy's backward recurrence over power-series
ops (polyadd, polysub, polymulx) from the highest coefficient down; a series of fewer than three terms is returned as is. Keeps the
series' common type. Bit-identical to NumPy 2.4.2's leg2poly.
public NDArray leg2poly(object c)
Parameters
cobjectThe Legendre series's coefficients. The coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.
Returns
- NDArray
The power-series coefficients, low degree first (a new array, or a view of one when trailing zeros were trimmed).
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array, a dtype NumSharp does not have) — raised where NumPy starts computing with it, after every series check. A str series is NumPy's ValueError (no common type).
legadd(object, object)
Adds two Legendre series (polyutils._add): both converted to their common type (integers become float64),
the shorter added into a copy of the longer, trailing zeros trimmed.
public NDArray legadd(object c1, object c2)
Parameters
c1objectFirst series, low degree first (an array, a C# array or list, or a single number).
c2objectSecond series.
Returns
- NDArray
The sum: a new array, or a VIEW of one when trailing zeros were trimmed (as in NumPy).
Remarks
Exceptions
- ValueError
An empty or non-1-d series, or no common type (a bool series).
- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array), raised after both series' checks.
legcompanion(object)
The scaled companion matrix of the Legendre series c: the basis is scaled by scl = 1 / sqrt(2k + 1) so that the matrix is symmetric when c is a single Legendre polynomial — off-diagonals k * scl[k-1] * scl[k], and the last column reduced by (c[:-1] / c[-1]) * (scl / scl[-1]) * (n / (2n - 1)). NumPy's scale vector is float64, so a float16 / float32 series' last column is computed in float64 and rounded once into the matrix and its off-diagonals are float64 values cast by assignment; a complex series runs the complex128 loops. Bit-identical to NumPy 2.4.2's legcompanion.
public NDArray legcompanion(object c)
Parameters
cobjectThe coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Trailing zeros are trimmed first; integer series are computed in float64; a bool series has no common type.
Returns
- NDArray
A new owning (deg, deg) matrix of the series' common type; a two-term series gives the 1x1 matrix
[[-c[0] / c[1]]](NumPy's arithmetic on the two coefficients: scalarmath, or — for an object series — the items' own: CPython for Python numbers, ufuncs for 0-d arrays; the 1x1 array is numeric either way).
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text). Fewer than two terms after trimming:Series must have maximum degree of at least 1.- NotSupportedException
An OBJECT series (None, a non-numeric object or a Python int past uint64 among the terms makes NumPy's as_series copy an object array) of three or more terms after trimming whose arithmetic does not raise — NumPy's result is an object matrix, a dtype NumSharp does not have; or one where NumPy's object arithmetic reaches None or a str (CPython's TypeError there). A two-term object series of numbers computes: NumPy's linear root of the items is a numeric 1x1 array. A str series is NumPy's ValueError (no common type).
- OverflowError
An object series whose arithmetic meets a Python int too large for a float — CPython's
integer division result too large for a floatwhen it is divided by a Python int,int too large to convert to floatwhen it meets anything else — whichever NumPy's statements raise first.
legder(object, int, object, int)
Differentiates the Legendre series c m times along axis,
multiplying by scl at every order. Bit-identical to NumPy 2.4.2's legder: its
recurrence der[j-1] = (2*j - 1)c[j]; c[j-2] += c[j] (then der[1] = 3c[2],
der[0] = c[1]), with c *= scl before every order, in NumPy's statement order and NEP 50 dtypes.
public NDArray legder(object c, int m = 1, object scl = null, int axis = 0)
Parameters
cobjectThe coefficients, LOW degree first (see polyder(object, int, object, int)).
mintHow many times to differentiate (≥ 0). 0 returns a copy;
m >= len(c)returnsc[:1]*0.sclobjectThe multiplier applied at each order (null for NumPy's 1; a scalar or a broadcasting array).
axisintThe axis the series runs along (negative counts from the end).
Returns
- NDArray
The derivative's coefficients, of the series' dtype (a view of a new array in NumPy's layout).
Remarks
Exceptions
- ValueError
The order of derivation must be non-negative, or an array scl whose broadcast would stretch the series.- AxisError
axisis out of range.- TypeError
A scl the series cannot absorb in place (NumPy's UFuncTypeError).
- IncorrectShapeException
An array scl that does not broadcast with the series.
- NotSupportedException
A null or string series.
legdiv(object, object)
Divides the Legendre series c1 by c2, returning quotient and remainder:
polyutils._div: for each quotient coefficient, the divisor times the i-th basis polynomial (legmul([0]*i + [1], c2)) is scaled and subtracted from the remainder. A dividend shorter than the divisor gives (c1[:1]*0, c1), a one-term divisor
(c1/c2[-1], c1[:1]*0). The quotient keeps the common type of the series, but the remainder is computed against divisor products NumPy promotes with an int list, so it is float64 for float16 / float32 series (probed 2.4.2). Bit-identical to NumPy 2.4.2's legdiv.
public (NDArray quo, NDArray rem) legdiv(object c1, object c2)
Parameters
c1objectThe dividend. The coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.c2objectThe divisor.
Returns
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array, a dtype NumSharp does not have) — raised where NumPy starts computing with it, after every series check. A str series is NumPy's ValueError (no common type).
- ZeroDivisionError
The divisor is zero (all its coefficients, after trimming) — NumPy's bare
ZeroDivisionError, empty message.
legfit(object, object, object, object, bool, object)
The least-squares fit of a Legendre series to data: the coefficients c that minimise the weighted squared error
sum_j w_j**2 * |y_j - legval(x_j, c)|**2, computed as NumPy 2.4.2 writes it (polyutils._fit: the
legvander(object, int) design matrix with its columns scaled to unit norm, solved by
np.linalg.lstsq), so the coefficients are NumPy's bit for bit with the OpenBLAS backend NumPy loads.
public NDPolyFitResult legfit(object x, object y, object deg, object rcond = null, bool full = false, object w = null)
Parameters
xobjectThe x-coordinates of the M sample points: anything
np.asarraytakes that is 1-D — a list, a typed C# array, an NDArray of any real or complex dtype (bool and integers compute in float64, float32 in float32).yobjectThe y-coordinates: 1-D (M,), or 2-D (M, K) to fit K data sets that share the x-coordinates (one per column).
degobjectThe degree of the fit: an integer (every term up to it is fitted), or a 1-D array_like of integer degrees (only those terms are fitted; the others are zero in the result). A bool, a float or an empty list is refused.
rcondobjectThe relative cutoff below which singular values of the scaled design matrix count as zero, or null for NumPy's default
len(x)*epsof x's float dtype.fullboolAlso return the residuals, rank, singular values and rcond of the least-squares problem (NumPy's
full=True): the result then deconstructs as(c, (residuals, rank, singular_values, rcond)).wobjectWeights of the sample points (1-D, length M), multiplying each point's residual, or null. For Gaussian uncertainties use
1/sigma.
Returns
- NDPolyFitResult
The coefficients, low degree first, as an NDPolyFitResult: an NDArray of shape (deg + 1,) or (deg + 1, K) in the dtype lstsq computes in (float64; float32 for float32 x and y; complex128), carrying the statistics when
fullis true.
Remarks
NumPy warns (RankWarning: The fit may be poorly conditioned) when the rank falls short of the number
of fitted terms and full is false; NumSharp emits no warnings — read rank with
full: true.
https://numpy.org/doc/stable/reference/generated/numpy.polynomial.legendre.legfit.html
Exceptions
- TypeError
NumPy's argument TypeErrors (
deg must be an int or non-empty 1-D array of int,expected 1D vector for x,expected non-empty vector for x,expected 1D or 2D array for y,expected x and y to have same length,expected 1D vector for w,expected x and w to have same length); a null x / y / w (Python'sunsupported operand type(s) for +: 'NoneType' and 'float') or a str one (the ufunc add's no-loop text); a float16 x (lstsq'sarray type float16 is unsupported in linalg); a complex or str rcond.- ValueError
expected deg >= 0; np.asarray's inhomogeneous-shape text for a ragged list; a degree too large for the design matrix (Maximum allowed dimension exceeded,array is too big; …).- NotSupportedException
No LAPACK backend (
np.linalg.lstsqneedsNumSharp.Interop.OpenBLAS); an argument NumPy computes with as Python objects; an rcond array with one or more dimensions (NumPy broadcasts lstsq over it).- MemoryError
A design matrix the machine cannot hold (NumPy's MemoryError).
legfromroots(object)
The Legendre series whose roots are roots: the roots sorted, one linear factor per root
(legline(-r, 1) = [-r, 1]), multiplied pairwise as a balanced tree (polyutils._fromroots) with legmul.
Empty roots give [1.]. The factors are built by np.array([-r, …]), whose coercion promotes a
float16 / float32 root to float64 — so the series is float64 (complex128 for complex roots). Bit-identical to
NumPy 2.4.2's legfromroots; one documented difference: roots that compare equal but differ in bits (+0.0 and -0.0) are ordered
deterministically here, where NumPy's unstable SIMD sort decides by CPU, which can flip the sign of a zero
coefficient.
public NDArray legfromroots(object roots)
Parameters
rootsobjectThe roots: an NDArray, a typed C# array, a Python list or tuple, … (anything with a length — NumPy's
len(roots)runs first).
Returns
- NDArray
A new array of
len(roots) + 1coefficients.
Remarks
Exceptions
- TypeError
A scalar (
object of type 'float' has no len()) or a 0-d array (len() of unsized object).- ValueError
Roots that are not 1-d, a zero-size array of nonzero length, or no common type (bool roots).
- NotSupportedException
An object root array (None, or roots holding None, a non-numeric object or a Python int past uint64), raised after its checks; str roots are NumPy's ValueError (no common type).
leggauss(object)
Gauss-Legendre quadrature: the sample points x_k (the roots of P_deg, increasing) and weights
w_k, so that Σ w_k f(x_k) integrates f over [-1, 1] exactly for every polynomial of degree
up to 2*deg - 1. Computed as NumPy 2.4.2 writes it: the eigenvalues of the (symmetric) companion matrix
of P_deg, one Newton step, the weights scaled against overflow, both symmetrised, and the weights
normalised to sum to 2 — bit-identical to NumPy.
public (NDArray x, NDArray w) leggauss(object deg)
Parameters
degobjectThe number of points: an integer under Python's
operator.index— a C# integer, BigInteger,boolorchar, or a 0-d integer NDArray. A float, even an integral one, is refused. NumPy builds the series as[0] * deg + [1]from the argument itself, so a 0-d array (not a list repeat) gives the one-coefficient series [1] and the companion's ValueError.
Returns
Remarks
NumPy's time goes into its three legval calls, Python loops over the deg + 1 coefficients;
here each is one kernel call. From degree ~100 both sides are bound by the same LAPACK syevd.
https://numpy.org/doc/stable/reference/generated/numpy.polynomial.legendre.leggauss.html
Exceptions
- TypeError
deg must be an integer, received {deg}, withdegas NumPy's f-string formats it (received 3.0,received Truefor a 0-d bool array,received [3]).- ValueError
deg must be a positive integer; for a 0-d arraydegthe companion'sSeries must have maximum degree of at least 1.- OverflowError
A count past 2**63 - 1: CPython's
cannot fit 'int' into an index-sized integerfrom the list repeat.- MemoryError
A count CPython's list cannot hold (2**60 - 1 or more: NumPy's empty MemoryError), or a companion matrix the machine cannot hold.
- NotSupportedException
No LAPACK backend is installed: the eigenvalues are
np.linalg.eigvalsh's, which needsNumSharp.Interop.OpenBLAS(bit-identical to NumPy with the OpenBLAS NumPy itself loads, at one thread).
leggrid2d(object, object, object)
Evaluates the 2-D Legendre series on the Cartesian product of x and y
(NumPy's _gridnd: two tensor passes). Unlike legval2d(object, object, object) the coordinates are NOT
converted first, so a Python-scalar coordinate stays weak.
public NDArray leggrid2d(object x, object y, object c)
Parameters
xobjectFirst-axis points (array or Python scalar, as in legval(object, object, bool)).
yobjectSecond-axis points.
cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar);c[i,j]is the coefficient of the degree (i, j) term.
Returns
- NDArray
The grid, shape
c.shape[2:] + x.shape + y.shape.
Remarks
Exceptions
- ArgumentNullException
A coordinate (or
pts) is null; a null c is NotSupportedException (NumPy's object array).- IndexError
cis empty along an evaluated axis.- ValueError
A ragged tuple / list c or point argument (np.array's inhomogeneous-shape text; c is converted before the first points).
- NotSupportedException
A null or str
c, or a tuple / list holding a str / null.
leggrid3d(object, object, object, object)
Evaluates the 3-D Legendre series on the Cartesian product of x, y and
z (NumPy's _gridnd: three tensor passes).
public NDArray leggrid3d(object x, object y, object z, object c)
Parameters
xobjectFirst-axis points.
yobjectSecond-axis points.
zobjectThird-axis points.
cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar);c[i,j,k]is the coefficient of the degree (i, j, k) term.
Returns
- NDArray
The grid, shape
c.shape[3:] + x.shape + y.shape + z.shape.
Remarks
Exceptions
- ArgumentNullException
A coordinate (or
pts) is null; a null c is NotSupportedException (NumPy's object array).- IndexError
cis empty along an evaluated axis.- ValueError
A ragged tuple / list c or point argument (np.array's inhomogeneous-shape text; c is converted before the first points).
- NotSupportedException
A null or str
c, or a tuple / list holding a str / null.
legint(object, int, object, object, object, int)
Integrates the Legendre series c m times along axis:
each order multiplies by scl, integrates (t = c[j]/(2*j + 1); tmp[j+1] = t;
tmp[j-1] -= t, with tmp[2] = c[1]/3) and adds the constant that makes the new series equal
k[i] at lbnd (tmp[0] += k[i] - legval(lbnd, tmp)). Bit-identical to NumPy
2.4.2's legint.
public NDArray legint(object c, int m = 1, object k = null, object lbnd = null, object scl = null, int axis = 0)
Parameters
cobjectThe coefficients, LOW degree first.
mintHow many times to integrate (≥ 0); 0 returns a copy.
kobjectThe integration constants (see polyint(object, int, object, object, object, int)).
lbndobjectThe lower bound (null for NumPy's 0); a scalar.
sclobjectThe multiplier applied at each order (null for NumPy's 1); a scalar.
axisintThe axis the series runs along.
Returns
- NDArray
The integral's coefficients, of the series' dtype (a view of a new array in NumPy's layout).
Remarks
Exceptions
- ValueError
NumPy's argument checks (see polyint(object, int, object, object, object, int)).
- AxisError
axisis out of range.- IndexError
An empty series.
- TypeError
An array constant for a complex 1-D series (NumPy's
complex()text).- TypeError
A value an N-D series cannot absorb in place (NumPy's UFuncTypeError).
- IncorrectShapeException
Constants that do not broadcast with the series' columns.
- NotSupportedException
A null or string series, constant or bound.
legline(object, object)
The Legendre series of the line off + scl*x: [off, scl] when scl != 0, else [off] — built
with np.array, so the dtype is the one NumPy DISCOVERS for the entries (legline(1, 2) is int64,
legline(1.0, 2) float64, a bool pair stays bool) rather than a coefficient dtype.
public NDArray legline(object off, object scl)
Parameters
offobjectThe offset: a Python number (C# bool / integer / float / double / Complex), a NumPy scalar (Half,
char,decimal, a 0-d NDArray) or an array.sclobjectThe scale; its truth value
scl != 0picks the one- or two-coefficient form (NaN is nonzero).
Returns
- NDArray
A new 1-D array (2-D when the entries are equal-shape arrays, as NumPy stacks them).
Remarks
Exceptions
- ValueError
An array scale with several elements or none (NumPy's truth-value errors), or entries of different shapes (
setting an array element with a sequence…).- TypeError
NumPy's boolean negative / subtract.
- OverflowError
A Python int out of a NumPy integer dtype's range, or CPython's int/float overflow.
- NotSupportedException
An entry NumPy would store in an object array: a Python int beyond uint64, null, a string.
legmul(object, object)
Multiplies two Legendre series: NumPy's backward recurrence over series — for each coefficient of the shorter factor, c0 = legsub(c[-i]xs, (c1(nd-1))/nd); c1 = legadd(tmp, (legmulx(c1)(2nd-1))/nd) — ending in legadd(c0, legmulx(c1)). When either factor is a single term, NumPy's recurrence binds the Python int 0, so a float16 / float32 product is float64 (probed 2.4.2). Bit-identical to NumPy 2.4.2: every statement runs through the kernel NumPy's statement runs through (the house ufunc loops, the U1 combine, the mulx kernel) in NumPy's order and dtypes.
public NDArray legmul(object c1, object c2)
Parameters
c1objectFirst factor. The coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.c2objectSecond factor.
Returns
- NDArray
The product: a new array, or a VIEW of one when trailing zeros were trimmed (as in NumPy).
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array, a dtype NumSharp does not have) — raised where NumPy starts computing with it, after every series check. A str series is NumPy's ValueError (no common type).
legmulx(object)
Multiplies the Legendre series c by x: xP_i = ((i+1)P_{i+1} + iP_{i-1})/(2i+1): NumPy's prd[i+1] = (c[i](i+1))/(2i+1); prd[i-1] += (c[i]*i)/(2i+1) per coefficient. The zero series [0] is returned as is.
Bit-identical to NumPy 2.4.2's legmulx — one IL kernel pass where NumPy runs a Python loop.
public NDArray legmulx(object c)
Parameters
cobjectThe coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.
Returns
- NDArray
A new array one coefficient longer (the zero series: a one-element copy), of
np.common_type(c).
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array, a dtype NumSharp does not have) — raised where NumPy starts computing with it, after every series check. A str series is NumPy's ValueError (no common type).
legpow(object, int, int?)
Raises the Legendre series c to the power pow: [1] of the series' dtype
for 0, the (trimmed) series for 1, otherwise legmul applied pow - 1 times. Bit-identical to NumPy 2.4.2's legpow.
public NDArray legpow(object c, int pow, int? maxpower = 16)
Parameters
cobjectThe coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.powintThe power (≥ 0).
maxpowerint?The largest power allowed: 16; null for no limit. NumPy's default keeps an accidental huge power from running away.
Returns
- NDArray
The power series, a new array of
np.common_type(c).
Remarks
Exceptions
- ValueError
The series' errors (see legmul(object, object)), then
Power must be a non-negative integer., thenPower is too large— NumPy's order.- NotSupportedException
An object series (see legmul(object, object)), raised after the power checks.
legroots(object)
The roots of the Legendre series c: the eigenvalues of legcompanion(c)[::-1, ::-1] — the ROTATED companion matrix, which NumPy notes reduces the error, computed by
eigvals(NDArray) and sorted in place, ascending (complex numbers lexicographically, real part
first). A constant series has no roots — an empty array of its dtype — and a linear one the single root
-c[0] / c[1] (scalarmath, in its own dtype). Roots far from the origin, and multiple roots, carry larger
errors (NumPy's documented caveat). With NumSharp.Interop.OpenBLAS referenced the eigenvalues come from NumPy's
own LAPACK geev and are bit-identical to NumPy 2.4.2's legroots (one BLAS thread); where
+0.0 and -0.0 roots meet, NumPy's SIMD sort orders them by CPU and NumSharp's sort deterministically.
public NDArray legroots(object c)
Parameters
cobjectThe coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Trailing zeros are trimmed first; integer series are computed in float64; a bool series has no common type.
Returns
- NDArray
The sorted roots: for degree 2 or more the eigenvalue array eigvals(NDArray) returned, sorted in place — always complex128 since NumPy 2.5 (real roots carry zero imaginary parts; NumPy's complex64 for a float32 series: NumSharp has one complex width); a linear series' single root keeps the series dtype.
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- TypeError
A float16 (or decimal) series of degree 2 or more: linalg's
array type float16 is unsupported in linalg. An OBJECT series of three or more terms whose arithmetic does not raise: eigvals'ufunc 'isfinite' not supported for the input types, …on NumPy's object companion matrix (raised before LAPACK — no backend needed).- LinAlgError
A companion matrix holding an infinity or NaN (
Array must not contain infs or NaNs, checked before the dtype), or eigenvalues that do not converge.- MissingBackendException
Degree 2 or more with no LAPACK backend: reference NumSharp.Interop.OpenBLAS (NumSharp.Core ships no eigensolver).
- NotSupportedException
An OBJECT series (None, a non-numeric object or a Python int past uint64 among the terms) of one term — NumPy returns
np.array([], dtype=object)— or one where NumPy's object arithmetic reaches None or a str (CPython's TypeError there). A two-term object series of numbers computes: NumPy's linear root of the items is a numeric array.- OverflowError
An object series whose arithmetic meets a Python int too large for a float — CPython's
integer division result too large for a floatwhen it is divided by a Python int,int too large to convert to floatwhen it meets anything else — whichever NumPy's statements raise first.
legsub(object, object)
Subtracts the Legendre series c2 from c1 (polyutils._sub): both converted
to their common type; when c2 is at least as long it is NEGATED first and
c1 added into it (NumPy's operation order, visible in which NaN survives), then trailing
zeros are trimmed.
public NDArray legsub(object c1, object c2)
Parameters
Returns
- NDArray
The difference: a new array, or a VIEW of one when trailing zeros were trimmed.
Remarks
Exceptions
- ValueError
An empty or non-1-d series, or no common type (a bool series).
- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array), raised after both series' checks.
legtrim(object, double)
legtrim — NumPy's alias of trimcoef(object, double): removes the trailing coefficients whose
magnitude is at most tol (np.abs(c) > tol decides what stays) and returns the
rest as a fresh copy of the coefficient dtype; when nothing exceeds the tolerance the result is
c[:1]*0.
public NDArray legtrim(object c, double tol = 0)
Parameters
cobjectThe series (1-D).
toldoubleThe non-negative tolerance, NumPy's Python number (0, the default, trims only exact zeros): it adopts the coefficients' precision, so a float16 series compares against the float16-rounded tolerance; a NaN tolerance trims everything.
Returns
- NDArray
The trimmed copy (
c[:1]*0when nothing exceeds the tolerance).
Remarks
https://numpy.org/doc/stable/reference/generated/numpy.polynomial.legendre.legtrim.html
API-parity batch 11 (the owner's decision 2, typed parameters only): one signature with NumPy's
default; the (object c) and (object c, object tol) forms went, so a NumPy scalar or a 0-d
array tolerance (compared in the promoted dtype) is no longer a C# spelling - pass the number.
Exceptions
- ValueError
tol must be non-negative, an empty or non-1-d series, or no common type.
legval(object, object, bool)
Evaluates the Legendre series with coefficients c at x by Clenshaw's recurrence exactly as NumPy writes it (c0 = c[-i] - c1*((nd - 1)/nd); c1 = tmp + c1x((2nd - 1)/nd); return c0 + c1x),
bit-identical to NumPy 2.4.2: the first steps run in the COEFFICIENTS' dtype exactly as NumPy's
do, and every intermediate carries NumPy's NEP 50 dtype.
public NDArray legval(object x, object c, bool tensor = true)
Parameters
xobjectThe points. An NDArray (any layout) or a C# array is an array; a C#
bool, integer,float,doubleor Complex is a Python scalar — it adopts the coefficients' dtype (legval(2.0, float32_c)is float32), Python-level arithmetic on it (2*x,1 - x) is done in double as CPython does, and a 1-D series then yields a 0-d result.char/Half/decimalare strong 0-d arrays.cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar), low degree first; for an N-D array the series run along axis 0 and the other axes index independent series. Integer and bool coefficients are evaluated as float64.tensorboolWhen true (the default) and x is an array, every series is evaluated at every point and the result has shape
c.shape[1:] + x.shape; when false x is broadcast over the series (c.shape[1:]againstx.shape). Irrelevant for a 1-D series or a scalar x.
Returns
- NDArray
The values, of NumPy's result dtype.
Remarks
Exceptions
- ArgumentNullException
xis null.- ValueError
A ragged tuple / list
corx(np.array's inhomogeneous-shape text; c is converted first, so its error wins).- NotSupportedException
A null or str
c, or a tuple / list holding a str or null (NumPy builds an object / str array, dtypes NumSharp does not have).- IndexError
cis empty:index -2 is out of bounds for axis 0 with size 0.- IncorrectShapeException
tensoris false and the shapes do not broadcast (NumPy's ValueError text).- OverflowError
A Python int of the recurrence does not fit an integer x dtype (NumPy's OverflowError).
legval2d(object, object, object)
Evaluates the 2-D Legendre series Σ c[i,j] * P_i(x) * P_j(y) at the point pairs (x, y).
NumPy's _valnd: legval(x, c) then legval(y, ·, tensor=False) — its exact
intermediate, so the bits match.
public NDArray legval2d(object x, object y, object c)
Parameters
xobjectFirst coordinates: an NDArray, a typed C# array, a Python tuple / list (np.array's coercion) or a scalar, converted before anything else (NumPy's
np.asanyarray) — so a scalar is a STRONG 0-d array (a Python int an int64 one).yobjectSecond coordinates (converted like
x); must have x's shape.cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar);c[i,j]is the coefficient of the degree (i, j) term. Extra trailing axes index independent series (they lead the result's shape).
Returns
- NDArray
The values, shape
c.shape[2:] + x.shape.
Remarks
Exceptions
- ArgumentNullException
A coordinate (or
pts) is null; a null c is NotSupportedException (NumPy's object array).- ValueError
x and y differ in shape (
x, y are incompatible), or a tuple / list argument is ragged (np.array's inhomogeneous-shape text: the coordinates' before the shape check, c's after it).- NotSupportedException
A null or str
c, a str coordinate, or a tuple / list holding one (NumPy's object / str arrays).- IndexError
cis empty along axis 0.
legval3d(object, object, object, object)
Evaluates the 3-D Legendre series at the point triples (x, y, z) (NumPy's _valnd: one tensor
pass, then two per-point passes).
public NDArray legval3d(object x, object y, object z, object c)
Parameters
xobjectFirst coordinates: an NDArray, a typed C# array, a Python tuple / list (np.array's coercion) or a scalar, converted before anything else (NumPy's
np.asanyarray) — so a scalar is a STRONG 0-d array (a Python int an int64 one).yobjectSecond coordinates (converted like
x; x's shape).zobjectThird coordinates (converted like
x; x's shape).cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar);c[i,j,k]is the coefficient of the degree (i, j, k) term.
Returns
- NDArray
The values, shape
c.shape[3:] + x.shape.
Remarks
Exceptions
- ArgumentNullException
A coordinate (or
pts) is null; a null c is NotSupportedException (NumPy's object array).- ValueError
The coordinates differ in shape (
x, y, z are incompatible), or a tuple / list argument is ragged (the coordinates' before the shape check, c's after it).- NotSupportedException
A null or str
c, a str coordinate, or a tuple / list holding one (NumPy's object / str arrays).- IndexError
cis empty along axis 0.
legvalnd(object[], object)
Evaluates the N-D Legendre series at the points pts = (x, y, …) — the n-dimensional
legval2d(object, object, object). NumPy 2.5's addition (gh-30857): pu._valnd(legval, c, *pts), the computation NumPy
2.4 kept private as polyutils._valnd.
public NDArray legvalnd(object[] pts, object c)
Parameters
ptsobject[]The coordinates, all of one shape (one per series axis evaluated): each an NDArray, a typed C# array, a Python tuple / list or a scalar, converted first as in
_valnd(a scalar is a STRONG 0-d array). AnNDArray[]passes as it is.cobjectCoefficients (anything
np.arrayaccepts — an NDArray of any layout, a typed C# array, a Python tuple / list such as a ValueTuple,object[]or a jagged /NDArray[]array, nested to any depth, or a scalar); axis i ispts[i]'s degree.
Returns
- NDArray
The values, shape
c.shape[len(pts):] + pts[0].shape.
Remarks
Exceptions
- ArgumentNullException
A coordinate (or
pts) is null; a null c is NotSupportedException (NumPy's object array).- IndexError
ptsis empty (list index out of range).- ValueError
The coordinates differ in shape, or a tuple / list argument is ragged.
- NotSupportedException
A null or str
c, or a str coordinate.
legvander(object, int)
The pseudo-Vandermonde matrix of degree deg: V[..., i] = L_i(x), by NumPy's forward
recurrence v[0] = x0 + 1; v[1] = x; v[i] = (v[i-1]x(2i - 1) - v[i-2]*(i - 1)) / i (the Python ints
converted to the dtype NEP 50's way — in float16 they round past 2048). Bit-identical to NumPy 2.4.2's
legvander (one IL kernel pass).
public NDArray legvander(object x, int deg)
Parameters
xobjectThe points (see polyvander(object, int): any layout; the matrix dtype is
(x + 0.0).dtype).degintThe degree (≥ 0).
Returns
- NDArray
A view of shape
x.shape + (deg + 1,)over a new array, the degree axis last (OWNDATA false).
Remarks
Exceptions
- ValueError
deg must be non-negative; a ragged list x;array is too big; ….- MemoryError
The matrix cannot be allocated (NumPy's MemoryError).
- NotSupportedException
A null or str x, or a list NumPy makes a str / object array of.
legvander2d(object, object, object)
The 2-D pseudo-Vandermonde matrix: column a*(deg[1] + 1) + b is L_a(x) * L_b(y) — NumPy's
_vander_nd_flat outer product of the two 1-D matrices. Bit-identical to NumPy 2.4.2's legvander2d.
public NDArray legvander2d(object x, object y, object deg)
Parameters
xobjectThe first coordinates (stacked with y: one shape, one promoted dtype — see polyvander2d(object, object, object)).
yobjectThe second coordinates.
degobjectThe two degrees
[x_deg, y_deg].
Returns
- NDArray
A view of shape
x.shape + ((deg[0] + 1) * (deg[1] + 1),)over a new array.
Remarks
Exceptions
- TypeError
A deg without a length, or a non-integer degree.
- ValueError
Expected 2 dimensions of degrees, got {len}; points of different shapes;deg must be non-negative;array is too big; ….- IncorrectShapeException
No points (NumPy's reshape text).
- MemoryError
A matrix NumPy allocates on the way cannot be allocated.
- OverflowError
Scalar points with a Python int past the float range (NumPy's
int too large to convert to floatat its per-element+ 0.0).- NotSupportedException
Points NumPy stacks into a str array, or into an object array it computes with as Python objects or fails on. Scalar points that are all numbers but stack into an object array (a Python int past uint64 among them) ARE computed: each dimension gets its own number, as in NumPy.
legvander3d(object, object, object, object)
The 3-D pseudo-Vandermonde matrix: column (a*(deg[1] + 1) + b)*(deg[2] + 1) + c is
L_a(x) * L_b(y) * L_c(z), the first product rounded before the second (NumPy's reduce). Bit-identical to
NumPy 2.4.2's legvander3d.
public NDArray legvander3d(object x, object y, object z, object deg)
Parameters
xobjectThe first coordinates (x, y, z stacked: one shape).
yobjectThe second coordinates.
zobjectThe third coordinates.
degobjectThe three degrees.
Returns
- NDArray
A view of shape
x.shape + ((deg[0] + 1) * (deg[1] + 1) * (deg[2] + 1),)over a new array.
Remarks
Exceptions
- TypeError
A deg without a length, or a non-integer degree.
- ValueError
Expected 3 dimensions of degrees, got {len}, and the 2-D form's other errors.- IncorrectShapeException
No points (NumPy's reshape text).
- MemoryError
A matrix NumPy allocates on the way cannot be allocated.
- OverflowError
Scalar points with a Python int past the float range (NumPy's
int too large to convert to floatat its per-element+ 0.0).- NotSupportedException
Points NumPy stacks into a str array, or into an object array it computes with as Python objects or fails on. Scalar points that are all numbers but stack into an object array (a Python int past uint64 among them) ARE computed: each dimension gets its own number, as in NumPy.
legweight(object)
The Legendre weight function, the constant 1, computed as NumPy 2.4.2 writes it: x0.0 + 1.0. So the
result has x's shape and NumPy's dtype, and it is NOT 1 everywhere: an infinite or NaN x gives NaN
(inf0.0 is NaN). An ndarray is computed in one fused pass.
public NDArray legweight(object x)
Parameters
xobjectThe points: an NDArray of any layout and dtype, or a typed C# array (an ndarray); a Python scalar — a C#
bool, integer, BigInteger,float,doubleor Complex, computed with CPython's arithmetic (NumPy returns a Python float or complex there); or a NumPy scalar (Half,char,decimal). x is not converted first, so a Python list or tuple is refused by Python's*.
Returns
- NDArray
The weights: an array of x's shape for an ndarray x (float64 for an integer or bool x, x's own dtype for a float or complex x), a 0-d array for a scalar x.
Remarks
Exceptions
- TypeError
xis a Python list or tuple (anobject[], a C# list, aValueTuple) or a string: Python'scan't multiply sequence by non-int of type 'float'; null:unsupported operand type(s) for *: 'NoneType' and 'float'. Or a string array:np.multiply's no-loop error.- OverflowError
A Python int too large for a float: CPython's
int too large to convert to floatfromx*0.0.
poly2leg(object)
Converts the power series pol to a Legendre series — Horner's scheme over the Legendre series's own ops:
res = legadd(legmulx(res), pol[i]) from the highest coefficient down, starting from the Python int 0,
which makes the result float64 for any real series (complex128 for a complex one). Bit-identical to NumPy
2.4.2's poly2leg.
public NDArray poly2leg(object pol)
Parameters
polobjectThe power-series coefficients. The coefficients, LOW degree first: an NDArray (any layout), a typed C# array, a Python list or tuple (
object[], aValueTuple, a jagged array — np.array's coercion), or a single number. Integer series are computed in float64; a bool series has no common type.
Returns
- NDArray
The Legendre series's coefficients, low degree first (a new array, or a view of one when trailing zeros were trimmed).
Remarks
Exceptions
- ValueError
An empty series (
Coefficient array is empty), a series that is not 1-d (Coefficient array is not 1-d), or no common type (a bool or str series), in NumPy's order; a ragged list (np.array's inhomogeneous-shape text).- NotSupportedException
A null series, or one holding None, a non-numeric object or a Python int past uint64 (NumPy's object array, a dtype NumSharp does not have) — raised where NumPy starts computing with it, after every series check. A str series is NumPy's ValueError (no common type).