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 expressionvars
– the names of the argumentsexpect_one_var
– don’t give deprecation warning ifvars
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
from sage.ext.fast_eval import fast_float x,y = var('x,y') # needs sage.symbolic f = fast_float(sqrt(x^2+y^2), 'x', 'y') # needs sage.symbolic f(3,4) # needs sage.symbolic
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
# needs sage.symbolic f = fast_float(x-y, 'x','y') f(1,2) f = fast_float(x-y, 'y','x') f(1,2)