Sparse
Sparse polynomials.
>>> from poly import polysval
>>> p = {0:1, 3:2} #p(x)=2*x^3+1
>>> polysval(p, 5)
Prefixed by polys... (poly - sparse).
All functions accept polynomials and return them as dicts (monomial-exponent:coefficient).
Index keys are expected to be integers.
Docstring conventions
Summary
Math notation (vector notation if possible, index notation, domain & codomain)
More information ("More efficient than ...").
Complexity
For a polynomial with \(n\) elements there will be - \(x\) scalar additions (add), ...
Notes
Design choices
See also
Similar functions
References
Wikipedia, numpy, ...
creation
polyszero = vecszero
Zero polynomial.
An empty dict: {}.
See also
- other constants:
polysone,polysx - for any degree:
polysmono - wraps:
vector.vecszero
polysone = {0: 1}
polysx = {1: 1}
polysmono(i, c=1)
Return a monomial of degree n.
See also
- other constants:
polyszero,polysone,polysx - for all monomials:
polysmonos - wraps:
vector.vecsbasis
Source code in poly\sparse\creation.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
polysmonos(start=0, c=1)
Yield all monomials.
See also
- for single monomial:
polysmono - wraps:
vector.vecsbases
Source code in poly\sparse\creation.py
67 68 69 70 71 72 73 74 75 76 77 78 79 | |
polysrand(n)
Return a random polynomial of degree n.
The coefficients are sampled from a uniform distribution in [0, 1[.
See also
- wraps:
vector.vecsrand
Source code in poly\sparse\creation.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 | |
polysrandn(n, normed=True, mu=0, sigma=1, weights=None)
Return a random polynomial of degree n.
The coefficients are sampled from a normal distribution.
Normed with respect to the euclidian vector norm \(\sum_k|a_k|^2\).
See also
- wraps:
vector.vecsrandn
Source code in poly\sparse\creation.py
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
polysfromroots(*xs, one=1)
Return the polynomial with the given roots.
Source code in poly\sparse\creation.py
113 114 115 116 117 118 119 120 121 122 123 | |
utility
polysdeg(p)
Return the degree of a polynomial.
Doesn't handle leading zeros, use polystrim
if needed.
\(\deg(0)=-1\) is used for the empty zero polynomial.
Source code in poly\sparse\utility.py
9 10 11 12 13 14 15 16 17 18 19 20 21 | |
polyseq(p, q)
Return if two polynomials are equal.
See also
- wraps:
vecseq
Source code in poly\sparse\utility.py
23 24 25 26 27 28 29 30 31 32 33 34 | |
polystrim(p, tol=None)
Remove all near zero (abs(v_i)<=tol) coefficients.
Source code in poly\sparse\utility.py
36 37 38 | |
evaluation
polysval(p, x)
Return the value of polynomial p evaluated at point x.
See also
- implementations:
polyval_naive,polyval_iterative,polyval_horner - for consecutive monomials:
polyvals - for \(x=0\):
polyvalzero - for polynomial arguments:
polycom
Source code in poly\sparse\evaluation.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | |
polysvalzero(p, zero=0)
Return the value of polynomial p evaluated at point 0.
More efficient than polysval(p, 0).
See also
- for any argument:
polysval
Source code in poly\sparse\evaluation.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
polyscom(p, q)
Return the polynomial composition of p & q.
See also
- for \(q=x-s\):
polysshift - for scalar arguments:
polysval
Source code in poly\sparse\evaluation.py
44 45 46 47 48 49 50 51 52 53 54 55 56 | |
polysshift(p, s, one=1)
Return the polynomial p shifted by s on the abscissa.
See also
- for polynomial argument:
polyscom
Source code in poly\sparse\evaluation.py
58 59 60 61 62 63 64 65 66 67 68 69 | |
polysscale(p, a)
Return the polynomial p scaled by 1/a on the abscissa.
More efficient than polyscom(p, polysscalarmul(a, polysx)).
See also
- for polynomial argument:
polyscom
Source code in poly\sparse\evaluation.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
arithmetic
polyspos(p)
Return the polynomial with the unary positive operator applied.
See also
- wraps:
vecspos
Source code in poly\sparse\arithmetic.py
13 14 15 16 17 18 19 20 21 22 23 24 | |
polysneg(p)
Return the polynomial with the unary negative operator applied.
See also
- wraps:
vecsneg
Source code in poly\sparse\arithmetic.py
26 27 28 29 30 31 32 33 34 35 36 37 | |
polysadd(*ps)
Return the sum of polynomials.
See also
- wraps:
vecsadd
Source code in poly\sparse\arithmetic.py
39 40 41 42 43 44 45 46 47 48 49 50 | |
polysaddc(p, c, n=0)
Return the sum of polynomial p and a monomial of degree n.
See also
- wraps:
vecsaddc
Source code in poly\sparse\arithmetic.py
52 53 54 55 56 57 58 59 60 61 62 63 | |
polyssub(p, q)
Return the difference of two polynomials.
See also
- wraps:
vecssub
Source code in poly\sparse\arithmetic.py
65 66 67 68 69 70 71 72 73 74 75 76 | |
polyssubc(p, c, n=0)
Return the difference of polynomial p and a monomial of degree n.
See also
- wraps:
vecssubc
Source code in poly\sparse\arithmetic.py
78 79 80 81 82 83 84 85 86 87 88 89 | |
polysscalarmul(p, a)
Return the product.
See also
- wraps:
vecsmul
Source code in poly\sparse\arithmetic.py
91 92 93 94 95 96 97 98 99 100 101 102 | |
polysscalarrmul(a, p)
Return the product.
See also
- wraps:
vecsrmul
Source code in poly\sparse\arithmetic.py
104 105 106 107 108 109 110 111 112 113 114 115 | |
polysscalartruediv(p, a)
Return the true division of a polynomial and a scalar.
See also
- wraps:
vecstruediv
Source code in poly\sparse\arithmetic.py
117 118 119 120 121 122 123 124 125 126 127 128 | |
polysscalarfloordiv(p, a)
Return the floor division of a polynomial and a scalar.
See also
- wraps:
vecsfloordiv
Source code in poly\sparse\arithmetic.py
130 131 132 133 134 135 136 137 138 139 140 141 | |
polysscalarmod(p, a)
Return the elementwise mod of a polynomial and a scalar.
See also
- wraps:
vecsmod
Source code in poly\sparse\arithmetic.py
143 144 145 146 147 148 149 150 151 152 153 154 | |
polysscalardivmod(p, a)
Return the elementwise divmod of a polynomial and a scalar.
See also
- wraps:
vecsdivmod
Source code in poly\sparse\arithmetic.py
156 157 158 159 160 161 162 163 164 165 166 167 | |
polysmul(*ps, method='naive', one=1)
Return the product of polynomials.
Available methods are
See also
- implementations:
polysmul_naive - for scalar factor:
polysscalarmul - for monomial factor:
polysmulx
Source code in poly\sparse\arithmetic.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
polysmul_naive(p, q)
Return the product of two polynomials.
Uses naive multiplication and summation.
See also
- for any implementation:
polysmul
Source code in poly\sparse\arithmetic.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | |
polysmulx(p, n=1)
Return the product of polynomial p and a monomial of degree n.
More efficient than polysmul(p, polysmonom(n)).
Complexity
There are no scalar arithmetic operations.
See also
- for polynomial factor:
polysmul - wraps:
vector.vecsrshift
Source code in poly\sparse\arithmetic.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |
polyspow(p, n, method='naive')
Return the polynomial p raised to the nonnegative n-th power.
Available methods are
TODO: mod parameter
See also
- implementations:
polyspow_naive,polyspow_binary - for sequence of powers:
polyspows
Source code in poly\sparse\arithmetic.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | |
polyspow_naive(p, n, one=1)
Return the polynomial p raised to the nonnegative n-th power.
Uses repeated multiplication.
See also
- for any implementation:
polyspow - other implementations:
polyspow_binary - uses:
polyspows
Source code in poly\sparse\arithmetic.py
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
polyspow_binary(p, n, one=1)
Return the polynomial p raised to the nonnegative n-th power.
Uses exponentiation by squaring.
See also
- for any implementation:
polyspow - other implementations:
polyspow_naive
References
- Wikipedia - Exponentiation by squaring
- Sequence \(C(k)\): A056791
Source code in poly\sparse\arithmetic.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 | |
polyspows(p, start=0, one=1)
Yield the powers of the polynomial p.
Uses iterative multiplication to calculate powers consecutively.
See also
- used by:
polyspow_naive,polyscom_iterative - for scalar arguments:
polyvals
Source code in poly\sparse\arithmetic.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
calculus
polysder(p, k=1)
Return the k-th derivative of polynomial p.
Source code in poly\sparse\calculus.py
11 12 13 14 15 16 17 18 | |
polysantider(p, c=0, b=0)
Return the antiderivative of polynomial p.
Source code in poly\sparse\calculus.py
20 21 22 23 24 25 26 27 28 | |
conversion
polystod(p, zero=0)
Return a sparse polynomial (dict) as a dense polynomial (tuple).
Source code in poly\sparse\conversion.py
11 12 13 | |
polydtos(p)
Return a dense polynomial (tuple) as a sparse polynomial (dict).
The resulting dict is not trimmed.
Source code in poly\sparse\conversion.py
15 16 17 18 19 20 | |
polyssympify(p, gen=x)
Return the coefficient mapping p as a sympy.Poly.
Source code in poly\sparse\conversion.py
22 23 24 | |
polysunsympify(p, gen=x)
Return sympy.Poly(p) as a coefficient dict.
Source code in poly\sparse\conversion.py
26 27 28 | |