chebyshev module¶
Chebyshev interpolation and integration in 1, 2, and 4 dimensions
Note
if the math looks strange in the documentation, just reload the page.
Interval
,Rectangle
: basic classes to define the integration domainmove_from1m1, move_to1m1
: rescale to and from the \([-1,1]\) intervalcheb_get_nodes_1d
: get Chebyshev nodes and weights on an intervalcheb_eval_fun_at_nodes_1d
: evaluates a function at is nodes on an intervalcheb_get_coefficients_1d
: get the Chebyshev coefficients for a functioncheb_interp_1d
: interpolate a function on an interval given its definition or its coefficientscheb_interp_1d_from_nodes
: interpolate a function on an interval given its values at the nodescheb_find_root
: finds the roots of a function in an intervalcheb_integrate_from_coeffs_1d
: integrates a function given its coefficientscheb_integrate_from_nodes_1d
: integrates a function given its values at the nodes (less precise)cheb_get_nodes_2d
: get Chebyshev nodes and weights on a rectanglecheb_eval_fun_at_nodes_2d
: evaluates a function at is nodes on a rectanglecheb_get_coefficients_2d
: get the Chebyshev coefficients for a function of 2 argumentscheb_interp_2d
: interpolate a function on a rectangle given its definition or its coefficientscheb_interp_2d_from_nodes
: interpolate a function on a rectangle given its values at the nodescheb_integrate_from_nodes_4d
: integrate over a product of rectangles given values at the tensor products of the 2d nodes.
Interval
dataclass
¶
a real interval \([x_0,x_1]\)
Source code in bs_python_utils/chebyshev.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
|
Rectangle
dataclass
¶
a product interval \([x_0,x_1] imes [y_0, y_1]\)
Source code in bs_python_utils/chebyshev.py
57 58 59 60 61 62 |
|
cheb_eval_fun_at_nodes_1d(fun, nodes=None, interval=None, degree=None)
¶
evaluate a function at the Chebyshev nodes on an interval
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun |
ArrayFunctionOfArray
|
the function to evaluate on an interval |
required |
nodes |
ndarray | None
|
the Chebyshev nodes on that interval, if precomputed |
None
|
interval |
Interval | None
|
the Interval |
None
|
degree |
int | None
|
the degree of the Chebyshev expansion |
None
|
Notes
interval
, degree
are required if nodes
is not provided
Returns:
Type | Description |
---|---|
ndarray
|
the values of the function at the Chebyshev nodes |
Source code in bs_python_utils/chebyshev.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
|
cheb_eval_fun_at_nodes_2d(fun, nodes=None, rectangle=None, degree=None)
¶
evaluate a function at the Chebyshev nodes on a rectangle $
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun |
ArrayFunctionOfArray
|
the function to evaluate on the rectangle |
required |
nodes |
ndarray | None
|
the Chebyshev nodes on that rectangle, if precomputed |
None
|
rectangle |
Rectangle | None
|
the Rectangle |
None
|
degree |
int | None
|
the degree of the Chebyshev expansion in each dimension |
None
|
Notes
rectangle
and degree
are required if nodes
is not provided
Returns:
Type | Description |
---|---|
ndarray
|
the values of the function at the Chebyshev nodes |
Source code in bs_python_utils/chebyshev.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
cheb_find_root(f, degree, interval=None)
¶
find the roots of \(f(x)=0\) in \([0,1]\); also return the one(s) within the interval, if given
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
ArrayFunctionOfArray
|
the function |
required |
degree |
int
|
the degree of the Chebyshev expansion |
required |
interval |
Interval | None
|
the interval where we want the root |
None
|
Returns:
Type | Description |
---|---|
ndarray | tuple[ndarray, ndarray | float | None]
|
the roots in \([0,1]\); and the one(s) in |
Source code in bs_python_utils/chebyshev.py
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
cheb_get_coefficients_1d(fun, interval, degree)
¶
get the Chebyshev coefficients for fun
on an interval
Parameters:
Name | Type | Description | Default |
---|---|---|---|
fun |
ArrayFunctionOfArray
|
the function |
required |
interval |
Interval
|
the Interval |
required |
degree |
int
|
the degree of the Chebyshev expansion |
required |
Returns:
Type | Description |
---|---|
ndarray
|
a |
Source code in bs_python_utils/chebyshev.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
cheb_get_coefficients_2d(rectangle, degree, vals_at_nodes=None, fun=None)
¶
get the Chebyshev coefficients for fun
on a rectangle,
using an OLS fit on the values on the grid of nodes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rectangle |
Rectangle
|
the Rectangle |
required |
degree |
int
|
the degree of the Chebyshev expansion in each dimension |
required |
vals_at_nodes |
ndarray | None
|
the values on the grid, if precomputed |
None
|
fun |
ArrayFunctionOfArray | None
|
the function |
None
|
Notes
if vals_at-nodes
is not provided then fun
must be.
Returns:
Type | Description |
---|---|
ndarray
|
the Chebyshev coefficients of the OLS Chebyshev fit, an |
ndarray
|
the approximation is \(f(x_1,x_2) = \sum_{k,l} c_{kl} T_k(x_1)T_l(x_2)\) |
Source code in bs_python_utils/chebyshev.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
cheb_get_nodes_1d(interval, degree)
¶
get the Chebyshev nodes and weights on the interval \([x0, x1]\)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
interval |
Interval
|
the Interval \([x_0, x_1]\) |
required |
degree |
int
|
the degree of the highest Chebyshev polynomial |
required |
Returns:
Type | Description |
---|---|
TwoArrays
|
two |
TwoArrays
|
so that |
Source code in bs_python_utils/chebyshev.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|
cheb_get_nodes_2d(rectangle, degree)
¶
get the Chebyshev nodes and weights on a rectangle
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rectangle |
Rectangle
|
the Rectangle |
required |
degree |
int
|
the degree of the highest Chebyshev polynomial in each dimension |
required |
Returns:
Type | Description |
---|---|
TwoArrays
|
two \(( ext{degree}^2, 2)\)`-matrices of Chebyshev nodes and weights |
TwoArrays
|
on the rectangle \([x0, x1] imes [y0, y1]\) |
Source code in bs_python_utils/chebyshev.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
|
cheb_integrate_from_coeffs_1d(c, interval)
¶
integrate a function on an interval using the coefficients of its Chebyshev expansion
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c |
ndarray
|
the Chebyshev coefficients for |
required |
interval |
Interval
|
the Interval |
required |
Returns:
Type | Description |
---|---|
float
|
the value of the integral over the interval |
Source code in bs_python_utils/chebyshev.py
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
|
cheb_integrate_from_coeffs_2d(c, rectangle)
¶
integrate a function on an interval using the coefficients of its Chebyshev expansion
Parameters:
Name | Type | Description | Default |
---|---|---|---|
c |
ndarray
|
the Chebyshev coefficients for |
required |
rectangle |
Rectangle
|
the Rectangle |
required |
Returns:
Type | Description |
---|---|
float
|
the value of the integral over the interval |
Source code in bs_python_utils/chebyshev.py
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
|
cheb_integrate_from_nodes_1d(vals_at_nodes, weights)
¶
integrate a function given its values at the Chebyshev nodes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vals_at_nodes |
ndarray
|
the values of the function at these nodes |
required |
weights |
ndarray
|
the Chebyshev nodes |
required |
Returns:
Type | Description |
---|---|
float
|
the value of the integral |
Notes
this is much less precise than cheb_integrate_from_coeffs_1d
Source code in bs_python_utils/chebyshev.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
|
cheb_integrate_from_nodes_2d(vals_at_nodes, weights)
¶
integrate a function given its values at the Chebyshev nodes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vals_at_nodes |
ndarray
|
the values of the function on the grid of 2d nodes |
required |
weights |
ndarray
|
the Chebyshev weights in 2d |
required |
Returns:
Type | Description |
---|---|
float
|
the value of the integral |
Warning
this is much less precise than cheb_integrate_from_coeffs_2d
Source code in bs_python_utils/chebyshev.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 |
|
cheb_integrate_from_nodes_4d(vals_at_nodes4d, weights2d)
¶
integrate a function on the square of a rectangle given its values at the 4d Chebyshev nodes
Parameters:
Name | Type | Description | Default |
---|---|---|---|
vals_at_nodes4d |
ndarray
|
the values of the function on the square of the grid of 2d nodes, an \((M^2, M^2)\) matrix |
required |
weights2d |
ndarray
|
the Chebyshev weights on the rectangular grid, an \(M^2\)-vector |
required |
Returns:
Type | Description |
---|---|
float
|
the value of the integral |
Warning
it would be better to have a cheb_integrate_from_coeffs_4d
Source code in bs_python_utils/chebyshev.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 |
|
cheb_interp_1d(x_vals, interval, c=None, fun=None, degree=None)
¶
interpolate a function on on interval using Chebyshev polynomials
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_vals |
ndarray
|
the values at which to interpolate |
required |
interval |
Interval
|
the Interval |
required |
c |
ndarray | None
|
the Chebyshev coefficients for |
None
|
fun |
ArrayFunctionOfArray | None
|
the function to interpolate |
None
|
degree |
int | None
|
the degree of the Chebyshev expansion |
None
|
Notes
fun
and degree
are required if c
is not provided
Returns:
Type | Description |
---|---|
TwoArrays
|
the values of the interpolation at |
Source code in bs_python_utils/chebyshev.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
|
cheb_interp_1d_from_nodes(f_vals_at_nodes, x, interval=None)
¶
interpolate \(f(x)\) given the values \(f(x_m)\) for \(m=1,\ldots,M^2\) at the Chebyshev nodes on an intervak
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f_vals_at_nodes |
ndarray
|
an \(M^2\) vector of values \(f(x_m)\) |
required |
x |
ndarray
|
a scalar where we want \(f(x)\) |
required |
interval |
Interval | None
|
the interval on which the function acts; by default, \([0,1]\) |
None
|
Returns:
Type | Description |
---|---|
float
|
the interpolated value of \(f(x)\). |
Source code in bs_python_utils/chebyshev.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
|
cheb_interp_2d(xy_vals, rectangle, c=None, fun=None, degree=None, vals_at_nodes=None)
¶
interpolate a function on a rectangle using Chebyshev polynomials
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xy_vals |
ndarray
|
the values at which to interpolate, an |
required |
rectangle |
Rectangle
|
the Rectangle |
required |
c |
ndarray | None
|
the Chebyshev coefficients for |
None
|
fun |
ArrayFunctionOfArray | None
|
the function to interpolate |
None
|
degree |
int | None
|
the degree of the Chebyshev expansion |
None
|
vals_at_nodes |
ndarray | None
|
the values on the grid, if precomputed |
None
|
Notes
degree
is required if c
is not provided, as well as either fun
or vals_at_nodes
Returns:
Type | Description |
---|---|
TwoArrays | tuple[float, ndarray]
|
the |
Source code in bs_python_utils/chebyshev.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 |
|
cheb_interp_2d_from_nodes(f_vals_at_nodes, x, rectangle=None)
¶
interpolate \(f(x)\) given the values \(f(x_m)\) for \(m=1,\ldots,M^2\) at the Chebyshev nodes on a rectangle
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f_vals_at_nodes |
ndarray
|
an \(M^2\) vector of values \(f(x_m)\) |
required |
x |
ndarray
|
a 2-vector where we want \(f(x)\) |
required |
rectangle |
Rectangle | None
|
the rectangle on which the function acts; by default, \([0,1]^2\) |
None
|
Returns:
Type | Description |
---|---|
float
|
the interpolated value of \(f(x)\). |
Source code in bs_python_utils/chebyshev.py
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 |
|
move_from1m1(t, interval)
¶
get the position of t
in \([-1,1]\) and move it in the same position in interval
Parameters:
Name | Type | Description | Default |
---|---|---|---|
t |
FloatOrArray
|
position(s) within `\([-1,1]\) |
required |
interval |
Interval
|
the Interval |
required |
Returns:
Type | Description |
---|---|
FloatOrArray
|
|
Source code in bs_python_utils/chebyshev.py
65 66 67 68 69 70 71 72 73 74 75 76 |
|
move_to1m1(x, interval)
¶
get the position of x
in interval
and move it to the same position in \([-1,1]\)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
FloatOrArray
|
position(s) within |
required |
interval |
Interval
|
the Interval |
required |
Returns:
Type | Description |
---|---|
FloatOrArray
|
|
Source code in bs_python_utils/chebyshev.py
79 80 81 82 83 84 85 86 87 88 89 90 |
|