└── README.md /README.md: -------------------------------------------------------------------------------- 1 |

2 | orthopy 3 |

All about orthogonal polynomials.

4 |

5 | 6 | [![PyPi Version](https://img.shields.io/pypi/v/orthopy.svg?style=flat-square)](https://pypi.org/project/orthopy) 7 | [![PyPI pyversions](https://img.shields.io/pypi/pyversions/orthopy.svg?style=flat-square)](https://pypi.org/pypi/orthopy/) 8 | [![GitHub stars](https://img.shields.io/github/stars/nschloe/orthopy.svg?style=flat-square&logo=github&label=Stars&logoColor=white)](https://github.com/nschloe/orthopy) 9 | [![Downloads](https://pepy.tech/badge/orthopy/month?style=flat-square)](https://pepy.tech/project/orthopy) 10 | 11 | 12 | 13 | [![Discord](https://img.shields.io/static/v1?logo=discord&logoColor=white&label=chat&message=on%20discord&color=7289da&style=flat-square)](https://discord.gg/hnTJ5MRX2Y) 14 | [![orthogonal](https://img.shields.io/badge/orthogonal-yes-ff69b4.svg?style=flat-square)](https://github.com/nschloe/orthopy) 15 | 16 | orthopy provides various orthogonal polynomial classes for 17 | [lines](#line-segment--1-1-with-weight-function-1-x%CE%B1-1-x%CE%B2), 18 | [triangles](#triangle-42), 19 | [disks](#disk-s2), 20 | [spheres](#sphere-u2), 21 | [n-cubes](#n-cube-cn), 22 | [the nD space with weight function exp(-r2)](#nd-space-with-weight-function-exp-r2-enr2) 23 | and more. 24 | All computations are done using numerically stable recurrence schemes. Furthermore, all 25 | functions are fully vectorized and can return results in _exact arithmetic_. 26 | 27 | ### Installation 28 | 29 | Install orthopy [from PyPI](https://pypi.org/project/orthopy/) with 30 | 31 | ``` 32 | pip install orthopy 33 | ``` 34 | 35 | ### How to get a license 36 | 37 | Licenses for personal and academic use can be purchased 38 | [here](https://buy.stripe.com/aEUg1H38OgDw5qMfZ3). 39 | You'll receive a confirmation email with a license key. 40 | Install the key with 41 | 42 | ``` 43 | plm add 44 | ``` 45 | 46 | on your machine and you're good to go. 47 | 48 | For commercial use, please contact support@mondaytech.com. 49 | 50 | ### Basic usage 51 | 52 | The main function of all submodules is the iterator `Eval` which evaluates the series of 53 | orthogonal polynomials with increasing degree at given points using a recurrence 54 | relation, e.g., 55 | 56 | ```python 57 | import orthopy 58 | 59 | x = 0.5 60 | 61 | evaluator = orthopy.c1.legendre.Eval(x, "classical") 62 | for _ in range(5): 63 | print(next(evaluator)) 64 | ``` 65 | 66 | ```python 67 | 1.0 # P_0(0.5) 68 | 0.5 # P_1(0.5) 69 | -0.125 # P_2(0.5) 70 | -0.4375 # P_3(0.5) 71 | -0.2890625 # P_4(0.5) 72 | ``` 73 | 74 | Other ways of getting the first `n` items are 75 | 76 | 77 | 78 | ```python 79 | evaluator = Eval(x, "normal") 80 | vals = [next(evaluator) for _ in range(n)] 81 | 82 | import itertools 83 | vals = list(itertools.islice(Eval(x, "normal"), n)) 84 | ``` 85 | 86 | Instead of evaluating at only one point, you can provide any array for `x`; the 87 | polynomials will then be evaluated for all points at once. You can also use sympy for 88 | symbolic computation: 89 | 90 | ```python 91 | import itertools 92 | import orthopy 93 | import sympy 94 | 95 | x = sympy.Symbol("x") 96 | 97 | evaluator = orthopy.c1.legendre.Eval(x, "classical") 98 | for val in itertools.islice(evaluator, 5): 99 | print(sympy.expand(val)) 100 | ``` 101 | 102 | ``` 103 | 1 104 | x 105 | 3*x**2/2 - 1/2 106 | 5*x**3/2 - 3*x/2 107 | 35*x**4/8 - 15*x**2/4 + 3/8 108 | ``` 109 | 110 | All `Eval` methods have a `scaling` argument which can have three values: 111 | 112 | - `"monic"`: The leading coefficient is 1. 113 | - `"classical"`: The maximum value is 1 (or (n+alpha over n)). 114 | - `"normal"`: The integral of the squared function over the domain is 1. 115 | 116 | For univariate ("one-dimensional") integrals, every new iteration contains one function. 117 | For bivariate ("two-dimensional") domains, every level will contain one function more 118 | than the previous, and similarly for multivariate families. See the tree plots below. 119 | 120 | ### Line segment (-1, +1) with weight function (1-x)α (1+x)β 121 | 122 | | | | | 123 | | :---------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------: | 124 | | Legendre | Chebyshev 1 | Chebyshev 2 | 125 | 126 | Jacobi, Gegenbauer (α=β), Chebyshev 1 (α=β=-1/2), Chebyshev 2 (α=β=1/2), Legendre 127 | (α=β=0) polynomials. 128 | 129 | 130 | 131 | ```python 132 | import orthopy 133 | 134 | orthopy.c1.legendre.Eval(x, "normal") 135 | orthopy.c1.chebyshev1.Eval(x, "normal") 136 | orthopy.c1.chebyshev2.Eval(x, "normal") 137 | orthopy.c1.gegenbauer.Eval(x, "normal", lmbda) 138 | orthopy.c1.jacobi.Eval(x, "normal", alpha, beta) 139 | ``` 140 | 141 | The plots above are generated with 142 | 143 | ```python 144 | import orthopy 145 | 146 | orthopy.c1.jacobi.show(5, "normal", 0.0, 0.0) 147 | # plot, savefig also exist 148 | ``` 149 | 150 | Recurrence coefficients can be explicitly retrieved by 151 | 152 | ```python 153 | import orthopy 154 | 155 | rc = orthopy.c1.jacobi.RecurrenceCoefficients( 156 | "monic", # or "classical", "normal" 157 | alpha=0, beta=0, symbolic=True 158 | ) 159 | print(rc.p0) 160 | for k in range(5): 161 | print(rc[k]) 162 | ``` 163 | 164 | ``` 165 | 1 166 | (1, 0, None) 167 | (1, 0, 1/3) 168 | (1, 0, 4/15) 169 | (1, 0, 9/35) 170 | (1, 0, 16/63) 171 | ``` 172 | 173 | ### 1D half-space with weight function xα exp(-r) 174 | 175 | 176 | 177 | (Generalized) Laguerre polynomials. 178 | 179 | 180 | 181 | ```python 182 | evaluator = orthopy.e1r.Eval(x, alpha=0, scaling="normal") 183 | ``` 184 | 185 | ### 1D space with weight function exp(-r2) 186 | 187 | 188 | 189 | Hermite polynomials come in two standardizations: 190 | 191 | - `"physicists"` (against the weight function `exp(-x ** 2)` 192 | - `"probabilists"` (against the weight function `1 / sqrt(2 * pi) * exp(-x ** 2 / 2)` 193 | 194 | 195 | 196 | ```python 197 | evaluator = orthopy.e1r2.Eval( 198 | x, 199 | "probabilists", # or "physicists" 200 | "normal" 201 | ) 202 | ``` 203 | 204 | #### Associated Legendre "polynomials" 205 | 206 | 207 | 208 | Not all of those are polynomials, so they should really be called associated Legendre 209 | _functions_. The kth iteration contains _2k+1_ functions, indexed from 210 | _-k_ to _k_. (See the color grouping in the above plot.) 211 | 212 | 213 | 214 | ```python 215 | evaluator = orthopy.c1.associated_legendre.Eval( 216 | x, phi=None, standardization="natural", with_condon_shortley_phase=True 217 | ) 218 | ``` 219 | 220 | ### Triangle (_T2_) 221 | 222 | 223 | 224 | orthopy's triangle orthogonal polynomials are evaluated in terms of [barycentric 225 | coordinates](https://en.wikipedia.org/wiki/Barycentric_coordinate_system), so the 226 | `X.shape[0]` has to be 3. 227 | 228 | ```python 229 | import orthopy 230 | 231 | bary = [0.1, 0.7, 0.2] 232 | evaluator = orthopy.t2.Eval(bary, "normal") 233 | ``` 234 | 235 | ### Disk (_S2_) 236 | 237 | | | | | 238 | | :------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------------------: | :------------------------------------------------------------------------------------------------------: | 239 | | Xu | [Zernike](https://en.wikipedia.org/wiki/Zernike_polynomials) | Zernike 2 | 240 | 241 | orthopy contains several families of orthogonal polynomials on the unit disk: After 242 | [Xu](https://arxiv.org/abs/1701.02709), 243 | [Zernike](https://en.wikipedia.org/wiki/Zernike_polynomials), and a simplified version 244 | of Zernike polynomials. 245 | 246 | ```python 247 | import orthopy 248 | 249 | x = [0.1, -0.3] 250 | 251 | evaluator = orthopy.s2.xu.Eval(x, "normal") 252 | # evaluator = orthopy.s2.zernike.Eval(x, "normal") 253 | # evaluator = orthopy.s2.zernike2.Eval(x, "normal") 254 | ``` 255 | 256 | ### Sphere (_U3_) 257 | 258 | 259 | 260 | Complex-valued _spherical harmonics,_ (black=zero, green=real positive, 261 | pink=real negative, blue=imaginary positive, yellow=imaginary negative). The 262 | functions in the middle are real-valued. The complex angle takes _n_ turns on 263 | the nth level. 264 | 265 | 266 | 267 | ```python 268 | evaluator = orthopy.u3.EvalCartesian( 269 | x, 270 | scaling="quantum mechanic" # or "acoustic", "geodetic", "schmidt" 271 | ) 272 | 273 | evaluator = orthopy.u3.EvalSpherical( 274 | theta_phi, # polar, azimuthal angles 275 | scaling="quantum mechanic" # or "acoustic", "geodetic", "schmidt" 276 | ) 277 | ``` 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | ### _n_-Cube (_Cn_) 291 | 292 | | | | | 293 | | :---------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------: | 294 | | C1 (Legendre) | C2 | C3 | 295 | 296 | Jacobi product polynomials. 297 | All polynomials are normalized on the n-dimensional cube. The dimensionality is 298 | determined by `X.shape[0]`. 299 | 300 | 301 | 302 | ```python 303 | evaluator = orthopy.cn.Eval(X, alpha=0, beta=0) 304 | values, degrees = next(evaluator) 305 | ``` 306 | 307 | ### nD space with weight function exp(-r2) (_Enr2_) 308 | 309 | | | | | 310 | | :-----------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------: | 311 | | _E1r2_ | _E2r2_ | _E3r2_ | 312 | 313 | Hermite product polynomials. 314 | All polynomials are normalized over the measure. The dimensionality is determined by 315 | `X.shape[0]`. 316 | 317 | 318 | 319 | ```python 320 | evaluator = orthopy.enr2.Eval( 321 | x, 322 | standardization="probabilists" # or "physicists" 323 | ) 324 | values, degrees = next(evaluator) 325 | ``` 326 | 327 | ### Other tools 328 | 329 | - Generating recurrence coefficients for 1D domains with 330 | [Stieltjes](https://github.com/nschloe/orthopy/wiki/Generating-1D-recurrence-coefficients-for-a-given-weight#stieltjes), 331 | [Golub-Welsch](https://github.com/nschloe/orthopy/wiki/Generating-1D-recurrence-coefficients-for-a-given-weight#golub-welsch), 332 | [Chebyshev](https://github.com/nschloe/orthopy/wiki/Generating-1D-recurrence-coefficients-for-a-given-weight#chebyshev), and 333 | [modified 334 | Chebyshev](https://github.com/nschloe/orthopy/wiki/Generating-1D-recurrence-coefficients-for-a-given-weight#modified-chebyshev). 335 | 336 | - The the sanity of recurrence coefficients with test 3 from [Gautschi's article](https://doi.org/10.1007/BF02218441): 337 | computing the weighted sum of orthogonal polynomials: 338 | 339 | 340 | ```python 341 | orthopy.tools.gautschi_test_3(moments, alpha, beta) 342 | ``` 343 | 344 | - [Clenshaw algorithm](https://en.wikipedia.org/wiki/Clenshaw_algorithm) for 345 | computing the weighted sum of orthogonal polynomials: 346 | 347 | ```python 348 | vals = orthopy.c1.clenshaw(a, alpha, beta, t) 349 | ``` 350 | 351 | ### Relevant publications 352 | 353 | - [Robert C. Kirby, Singularity-free evaluation of collapsed-coordinate orthogonal polynomials, ACM Transactions on Mathematical Software (TOMS), Volume 37, Issue 1, January 2010](https://doi.org/10.1145/1644001.1644006) 354 | - [Abedallah Rababah, Recurrence Relations for Orthogonal Polynomials on Triangular Domains, MDPI Mathematics 2016, 4(2)](https://doi.org/10.3390/math4020025) 355 | - [Yuan Xu, Orthogonal polynomials of several variables, arxiv.org, January 2017](https://arxiv.org/abs/1701.02709) 356 | --------------------------------------------------------------------------------