lazy_poly
Module¶
Polynomial model and WaringLagrange polynomial interpolator
Summary of module contents:
Name  Description 

PolyMeta  Poly metaclass. This class overloads few operators to the Poly class. All binary dunders (non reverse) should be implemented on the Poly class 
Poly  Model for a polynomial, a Laurent polynomial or a sum of arbitrary powers. 
x  Model for a polynomial, a Laurent polynomial or a sum of arbitrary powers. 
lagrange  This is a StrategyDict instance object called lagrange . Strategies
stored: 2. 
resample  Generic resampler based on WaringLagrange interpolators. 

class
PolyMeta
[source]¶ Bases:
audiolazy.lazy_core.AbstractOperatorOverloaderMeta
Poly metaclass. This class overloads few operators to the Poly class. All binary dunders (non reverse) should be implemented on the Poly class

__operators__
= '+  * pow truediv eq ne '¶


class
Poly
(data=None, zero=None)[source]¶ Bases:
object
Model for a polynomial, a Laurent polynomial or a sum of arbitrary powers.
The “values” method allows casting simple polynomials to a list with
list(Poly.values())
where the index values are the powers. The “terms” method allows casting any sum of powers to dict withdict(Poly.terms())
where the keys are the powers and the values are the coefficients, and this method can also give the terms sorted by their power value if needed. Both methods return a generator.Usually the instances of this class should be seen as immutable (this is a hashable instance), although there’s no enforcement for that (and item set is allowed) until the hash is required.
You can use the
x
object and operators to create your own instances. Examples:
>>> x ** 5  x + 7 7  x + x^5 >>> type(x + 1) <class 'audiolazy.lazy_poly.Poly'> >>> (x + 2)(17) 19 >>> (x ** 2 + 2 * x + 1)(2) 9 >>> (x ** 2 + 2 * x + 1)(.5) 2.25 >>> (x ** 2 + x)(10) 10.01 >>> spow = x ** 2.1 + x ** .3 + x ** 1 + x  6 >>> value = spow(5) >>> "{:.6f}".format(value) # Just to see the first few digits '0.854710'

__call__
(value, horner='auto')[source]¶ Apply the given value to the Poly.
Parameters:  value – The value to be applied. This can possibly be another Poly, or perhaps a Stream instance.
 horner – A value in [True, False, “auto”] which chooses whether a Hornerlike scheme should be used for the evaluation. Defaults to
"auto"
, which means the scheme is done only for simple polynomials. This scheme can be forced on Laurent polynomials and any other sum of powers that have sortable powers. This input is neglect whenvalue
is a Poly instance.
Note
For a polynomial with all values, the Horner scheme is done like expected, e.g. replacing
x ** 2 + 2 * x + 1
byx * (x + 2) + 1
in the evaluation. However, for zeroed coeffs, it will merge steps, using powers instead of several multiplications, e.g.x ** 7 + x ** 6 + 4
would evaluate asx ** 6 * (x + 1) + 4
, taking thex ** 6
instead of multiplying(x + 1)
byx
six times (i.e., the evaluation order is changed). This allows a faster approach for sparse polynomials and extends the scheme for any sum of powers with sortable powers.

__init__
(data=None, zero=None)[source]¶ Inits a polynomial from given data, which can be a list or a dict.
A list \([a_0, a_1, a_2, a_3, ...]\) inits a polynomial like
\[a_0 + a_1 . x + a_2 . x^2 + a_3 . x^3 + ...\]If data is a dictionary, powers are the keys and the \(a_i\) factors are the values, so negative powers are allowed and you can neglect the zeros in between, i.e., a dict with terms like
{power: value}
can also be used.

__neg__
()¶

__pos__
()¶

__pow__
(other)[source]¶ Power operator. The “other” parameter should be an int (or anything like), but it works with float when the Poly has only one term.

__radd__
(other)¶

__repr__
()¶

__rmul__
(other)¶

__rsub__
(other)¶

copy
(zero=None)[source]¶ Returns a Poly instance with the same terms, but as a “T” (tee) copy when they’re Stream instances, allowing maths using a polynomial more than once.

is_laurent
()[source]¶ Boolean that indicates whether is a Laurent polynomial or not.
A Laurent polynomial is any sum of integer powers of
x
. Examples:
>>> (x + 4).is_laurent() True >>> (x ** 3 + 4).is_laurent() True >>> (x ** 3 + 4).is_polynomial() False >>> (x ** 1.1 + 4).is_laurent() False

order
¶ Finds the polynomial order.
 Examples:
>>> (x + 4).order 1 >>> (x + 4  x ** 18).order 18 >>> (x  x).order 0 >>> (x ** 3 + 4).order Traceback (most recent call last): ... AttributeError: Power needs to be positive integers

roots
¶ Returns a list with all roots. Needs Numpy.

terms
(sort='auto', reverse=False)[source]¶ Pairs (2tuple) generator where each tuple has a (power, value) term, perhaps sorted by power. Useful for casting as dict.
Parameters:  sort – A boolean value or
"auto"
(default) which chooses whether the terms should be sorted. The"auto"
value meansTrue
for Laurent polynomials (i.e., integer powers),False
otherwise. If sorting is disabled, this method will yield the terms in the creation order.  reverse – Boolean to chooses whether the [sorted or creation] order should be reversed when yielding the pairs. If False (default), yields in ascending or creation order (not counting possible updates in the coefficients).
 sort – A boolean value or

values
()[source]¶ Array values generator for powers from zero to upper power. Useful to cast as list/tuple and for numpy/scipy integration (be careful: numpy use the reversed from the output of this function used as input to a list or a tuple constructor).

zero
¶

resample
(sig, old=1, new=1, order=3, zero=0.0)[source]¶ Generic resampler based on WaringLagrange interpolators.
Parameters:  sig – Input signal (any iterable).
 old – Time duration reference (defaults to 1, allowing percentages to the
new
keyword argument). This can be float number, or perhaps a Stream instance.  new – Time duration that the reference will have after resampling. For example, if
old = 1, new = 2
, then there will be 2 samples yielded for each sample from input. This can be a float number, or perhaps a Stream instance.  order – Lagrange interpolator order. The amount of neighboring samples to be used by the interpolator is
order + 1
.  zero – The input should be thought as zeropadded from the left with this value.
Returns: The first value will be the first sample from
sig
, and then the interpolator will find the next samples towards the end of thesig
. The actual sampling interval (or time step) for this interpolator obeys to theold / new
relationship.Hint
The time step can also be timevarying, although that’s certainly difficult to synchonize (one sample is needed for each output sample). Perhaps the best approach for this case would be a ControlStream keeping the desired value at any time.
Note
The input isn’t zeropadded at right. It means that the last output will be one with interpolated with known data. For endless inputs that’s ok, this makes no difference, but for finite inputs that may be undesirable.
lazy_poly.lagrange
StrategyDict¶
This is a StrategyDict instance object called
lagrange
. Strategies stored: 2.
Strategy lagrange.func (Default). Docstring starts with:
WaringLagrange interpolator function.
Strategy lagrange.poly. Docstring starts with:
WaringLagrange interpolator polynomial.
Note
This docstring is selfgenerated, see the StrategyDict class and the strategies docs for more details.

poly
(pairs)¶ WaringLagrange interpolator polynomial.
Parameters: pairs – Iterable with pairs (tuples with two values), corresponding to points (x, y)
of the function.Returns: A Poly instance that allows finding the interpolated value for any x
.

func
(pairs)¶ WaringLagrange interpolator function.
Parameters: pairs – Iterable with pairs (tuples with two values), corresponding to points (x, y)
of the function.Returns: A function that returns the interpolator result for a given x
.