yroots.approximate

Approximator

yroots.ChebyshevApproximator.chebApproximate(f, a, b, absApproxTol=1e-10, relApproxTol=1e-10)

Generate and return an approximation for the function f on the interval [a,b].

Uses properties of Chebyshev polynomials and the FFT to quickly generate a reliable approximation. Examines approximation one dimension at a time to determine the degree at which the coefficients geometrically converge to 0 in each dimension, then calculates and returns a final approximation of these degree values along with the associated approximation error.

NOTE: The approximate function is only guaranteed to work well on functions that are continuous and smooth on the approximation interval. If the input function is not continuous and smooth on the interval, the approximation may get stuck in recursion.

Examples

>>> f = lambda x,y,z: x**2 - y**2 + 3*x*y
>>> approx, error = yroots.approximate(f,[-1,-1,-1],[1,1,1])
>>> print(approx)
[[[ 0.00000000e+00]
  [ 1.11022302e-16]
  [-5.00000000e-01]]
 [[ 1.11022302e-16]
  [ 3.00000000e+00]
  [-1.11022302e-16]]
 [[ 5.00000000e-01]
  [-1.11022302e-16]
  [ 0.00000000e+00]]]
>>> print(error)
2.8014584982224306e-24
>>> g = np.sqrt
>>> approx = yroots.approximate(g,[0],[5])[0]
>>> print(approx)
[ 1.42352509e+00  9.49016725e-01 -1.89803345e-01 ... -1.24418041e-10
  1.24418045e-10 -6.22090244e-11]
Parameters:
  • f (function) – The function to be approximated. NOTE: Valid input is restricted to callable Python functions (including user-created functions) and yroots Polynomial (MultiCheb and MultiPower) objects. String representations of functions are not valid input.

  • a (list or numpy array) – An array containing the lower bound of the approximation interval in each dimension, listed in dimension order

  • b (list or numpy array) – An array containing the upper bound of the approximation interval in each dimension, listed in dimension order.

  • absApproxTol (float) – The absolute tolerance used to determine at what degree the Chebyshev coefficients have converged to zero. If all coefficients after degree n are within absApproxTol from zero, the coefficients will be considered to have converged at degree n. Defaults to 1e-10.

  • relApproxTol (float) – The relative tolerance used to determine at what degree the Chebyshev coefficients have converged to zero. If all coefficients after degree n are within relApproxTol * supNorm (the maximum function evaluation on the interval) of zero, the coefficients will be considered to have converged at degree n. Defaults to 1e-10.

Returns:

  • coefficient_matrix (numpy array) – The coefficient matrix of the Chebyshev approximation.

  • error (float) – The error associated with the approximation.