Fast Numerical Evaluation

For many applications such as numerical integration, differential equation approximation, plotting a 3d surface, optimization problems, monte-carlo simulations, etc., one wishes to pass around and evaluate a single algebraic expression many, many times at various floating point values. Doing this via recursive calls over a python representation of the object (even if Maxima or other outside packages are not involved) is extremely inefficient.

The solution implemented in this module, by Robert Bradshaw (2008-10), has been superseded by fast_callable(). All that remains here is a compatible interface function fast_float().

AUTHORS:

  • Robert Bradshaw (2008-10): Initial version

sage.ext.fast_eval.fast_float(f, expect_one_var=False, *vars)[source]

Try to create a function that evaluates f quickly using floating-point numbers, if possible.

On failure, returns the input unchanged.

This is an alternative interface to sage.ext.fast_callable.fast_callable(). Issue #32268 proposes to deprecate this function.

INPUT:

  • f – an expression

  • vars – the names of the arguments

  • expect_one_var – don’t give deprecation warning if vars is omitted, as long as expression has only one var

EXAMPLES:

sage: from sage.ext.fast_eval import fast_float
sage: x,y = var('x,y')                                                          # needs sage.symbolic
sage: f = fast_float(sqrt(x^2+y^2), 'x', 'y')                                   # needs sage.symbolic
sage: f(3,4)                                                                    # needs sage.symbolic
5.0
>>> from sage.all import *
>>> from sage.ext.fast_eval import fast_float
>>> x,y = var('x,y')                                                          # needs sage.symbolic
>>> f = fast_float(sqrt(x**Integer(2)+y**Integer(2)), 'x', 'y')                                   # needs sage.symbolic
>>> f(Integer(3),Integer(4))                                                                    # needs sage.symbolic
5.0

Specifying the argument names is essential, as fast_float objects only distinguish between arguments by order.

sage: # needs sage.symbolic
sage: f = fast_float(x-y, 'x','y')
sage: f(1,2)
-1.0
sage: f = fast_float(x-y, 'y','x')
sage: f(1,2)
1.0
>>> from sage.all import *
>>> # needs sage.symbolic
>>> f = fast_float(x-y, 'x','y')
>>> f(Integer(1),Integer(2))
-1.0
>>> f = fast_float(x-y, 'y','x')
>>> f(Integer(1),Integer(2))
1.0
sage.ext.fast_eval.is_fast_float(x)[source]