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, old=None, expect_one_var=False, *vars)#

Tries to create a function that evaluates f quickly using floating-point numbers, if possible. There are two implementations of fast_float in Sage; by default we use the newer, which is slightly faster on most tests.

On failure, returns the input unchanged.

INPUT:

  • f – an expression

  • vars – the names of the arguments

  • old – deprecated, do not use

  • 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')
sage: f = fast_float(sqrt(x^2+y^2), 'x', 'y')
sage: f(3,4)
5.0

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

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
sage.ext.fast_eval.is_fast_float(x)#