Opened 9 years ago

Closed 7 months ago

# numerical integral needs an operand for callable symbolic functions

Reported by: Owned by: kcrisman minor sage-9.5 calculus eviatarbach, mjo, mkoeppe Michael Orlitzky Matthias Koeppe N/A d314399 d314399aeb258a26bdc3e5540b3db13f147dd644 #32234

I feel like I've seen this before, so close if appropriate... See this ask.sagemath question.

```sage: h(x) = x
sage: numerical_integral(h, 1, 2)
---------------------------------------------------------------------------
/Users/.../sage-5.11.rc0/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in fast_float(ex, *vars)
1264         1.4142135623730951
1265     """
-> 1266     return FastFloatConverter(ex, *vars)()
1267
1268 #################

/Users/.../sage-5.11.rc0/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in __call__(self, ex)
206         operator = ex.operator()
207         if operator is None:
--> 208             return self.symbol(ex)
209
210         if operator in arithmetic_operators:

/Users/.../sage-5.11.rc0/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in symbol(self, ex)
1182             return self.ff.fast_float_constant(float(ex))
1183         except TypeError:
-> 1184             raise ValueError, "free variable: %s" % repr(ex)
1185
1186     def arithmetic(self, ex, operator):

ValueError: free variable: x |--> x
```

but

```sage: numerical_integral(x, 1, 2)
(1.5, 1.6653345369377348e-14)
```

I think this should be easy to fix by catching this somehow.

### comment:1 Changed 9 years ago by kcrisman

Here's the relevant code.

```    def symbol(self, ex):
r"""
EXAMPLES::

sage: f = x._fast_float_('x', 'y')
sage: f(1,2)
1.0
sage: f = x._fast_float_('y', 'x')
sage: f(1,2)
2.0
"""
if self.vars == ():
return self.ff.fast_float_arg(0)

vars = list(self.vars)
name = repr(ex)
if name in vars:
return self.ff.fast_float_arg(vars.index(name))
svars = [repr(x) for x in vars]
if name in svars:
return self.ff.fast_float_arg(svars.index(name))

if ex.is_symbol(): # case of callable function which is the variable, like f(x)=x
name = repr(SR(ex)) # this gets back just the 'output' of the function
if name in svars:
return self.ff.fast_float_arg(svars.index(name))

try:
return self.ff.fast_float_constant(float(ex))
except TypeError:
raise ValueError, "free variable: %s" % repr(ex)
```

Note that this case is supposedly already taken care of! But apparently in this case `name` is not in `svars`. I'll investigate...

### comment:2 Changed 9 years ago by kcrisman

• Description modified (diff)
```\$ hg log -r 12287
changeset:   12287:d7533ae4895e
user:        Mike Hansen <mhansen@gmail.com>
date:        Tue May 19 16:11:30 2009 -0700
summary:     Updates for Pynac-0.1.7, main symbolics switch (#5930)
```

So apparently it's been here for a while.

### comment:4 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.1 to sage-6.2

### comment:5 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.2 to sage-6.3

### comment:6 Changed 8 years ago by vbraun_spam

• Milestone changed from sage-6.3 to sage-6.4

### comment:7 Changed 2 years ago by gh-Karn-Tiwari-3003

I was getting error while executing.

```h(x) = x
numerical_integral(h, 1, 2)
----------------------------------------------------------------------------------------------
/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__float__ (build/cythonized/sage/symbolic/expression.cpp:10936)()
1432         try:
-> 1433             ret = float(self._eval_self(float))
1434         except TypeError:

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._eval_self (build/cythonized/sage/symbolic/expression.cpp:10067)()
1240         else:
-> 1241             raise TypeError("Cannot evaluate symbolic expression to a numeric value.")
1242

TypeError: Cannot evaluate symbolic expression to a numeric value.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__float__ (build/cythonized/sage/symbolic/expression.cpp:10998)()
1435             try:
-> 1436                 c = (self._eval_self(complex))
1437                 if imag(c) == 0:

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._eval_self (build/cythonized/sage/symbolic/expression.cpp:10067)()
1240         else:
-> 1241             raise TypeError("Cannot evaluate symbolic expression to a numeric value.")
1242

TypeError: Cannot evaluate symbolic expression to a numeric value.

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression_conversions.py in symbol(self, ex)
1610         try:
-> 1611             return self.ff.fast_float_constant(float(ex))
1612         except TypeError:

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__float__ (build/cythonized/sage/symbolic/expression.cpp:11129)()
1441             except TypeError:
-> 1442                 raise TypeError("unable to simplify to float approximation")
1443         return ret

TypeError: unable to simplify to float approximation

During handling of the above exception, another exception occurred:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-9-0db5c4fa538e> in <module>()
1 __tmp__=var("x"); h = symbolic_expression(x).function(x)
----> 2 numerical_integral(h, Integer(1), Integer(2))

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/calculus/integration.pyx in sage.calculus.integration.numerical_integral (build/cythonized/sage/calculus/integration.c:3236)()
286                 to_sub = dict(zip(vars[1:], params))
287                 func = func.subs(to_sub)
--> 288             func = func._fast_float_(str(vars[0]))
289         except (AttributeError):
290             pass

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._fast_float_ (build/cythonized/sage/symbolic/expression.cpp:62537)()
11913         """
11914         from sage.symbolic.expression_conversions import fast_float
> 11915         return fast_float(self, *vars)
11916
11917     def _fast_callable_(self, etb):

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression_conversions.py in fast_float(ex, *vars)
1697         1.4142135623730951
1698     """
-> 1699     return FastFloatConverter(ex, *vars)()
1700
1701 #################

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression_conversions.py in __call__(self, ex)
210         operator = ex.operator()
211         if operator is None:
--> 212             return self.symbol(ex)
213
214         if operator in arithmetic_operators:

/opt/sagemath-9.0/local/lib/python3.7/site-packages/sage/symbolic/expression_conversions.py in symbol(self, ex)
1611             return self.ff.fast_float_constant(float(ex))
1612         except TypeError:
-> 1613             raise NotImplementedError("free variable: %s" % repr(ex))
1614
1615     def arithmetic(self, ex, operator):

NotImplementedError: free variable: x |--> x
---------------------------------------------------------------------------------------------
```

but

```numerical_integral(x, 1, 2)
(1.5, 1.6653345369377348e-14)
```

And I think it better if we use the below syntax while evaluating which is working fine.

```h(x) = x
numerical_integral(h(x),1, 2)
(1.5, 1.6653345369377348e-14)
```

### comment:8 Changed 10 months ago by mkoeppe

• Milestone changed from sage-6.4 to sage-9.5

### comment:9 Changed 10 months ago by mjo

• Authors set to Michael Orlitzky
• Branch set to u/mjo/ticket/15219
• Dependencies set to #32234
• Status changed from new to needs_review

Fixed by replacing `fast_float()` with `fast_callable()` in #32234.

Last 10 new commits:

 ​641152e `src/sage/ext/fast_eval.pyx: Remove everything except fast_float, is_fast_float` ​aa285da `src/sage/calculus/integration.pyx: Remove handling of FastDoubleFunc` ​58fdf1e `src/sage/plot/plot3d/parametric_surface.pyx: Remove handling of FastDoubleFunc` ​ec44c61 `src/sage/ext/fast_eval.pyx: Reduce/update documentation` ​4c93ae0 `Trac #32234: use fast_callable() for symbolic find_root().` ​56e9997 `Trac #32234: don't import fast_callable() from sage.ext.fast_eval.` ​18593ff `Trac #32234: replace fast_float_arg() in plot3d.` ​5fc69ca `Trac #32234: fix symbolic find_local_minimum() docs.` ​cdef097 `Trac #32234: use domain=float for fast callables used by numpy.` ​9d8f120 `Trac #15219: add test for numerical_integral() of symbolic functions.`

### comment:10 Changed 10 months ago by mkoeppe

• Reviewers set to Matthias Koeppe
• Status changed from needs_review to positive_review

### comment:11 Changed 9 months ago by mkoeppe

• Branch changed from u/mjo/ticket/15219 to u/mkoeppe/ticket/15219

### comment:12 Changed 9 months ago by mkoeppe

Merged updated #32234 (include 9.5.beta1)

Last 10 new commits:

 ​e6f88d6 `src/sage/ext/fast_eval.pyx: Reduce/update documentation` ​1353baf `Trac #32234: use fast_callable() for symbolic find_root().` ​c56fa05 `Trac #32234: don't import fast_callable() from sage.ext.fast_eval.` ​0fcec8e `Trac #32234: replace fast_float_arg() in plot3d.` ​1479666 `Trac #32234: fix symbolic find_local_minimum() docs.` ​19aa975 `Trac #32234: use domain=float for fast callables used by numpy.` ​df062b3 `Trac #32234: replace two trivial fast-callables in plot3d().` ​a4c840d `Merge tag '9.4.rc2' into t/32234/ticket/32234` ​72f3881 `Merge tag '9.5.beta1' into t/32234/ticket/32234` ​c25ab78 `Merge #32234`

### comment:13 Changed 8 months ago by vbraun

• Status changed from positive_review to needs_work

Merge conflict

### comment:14 Changed 8 months ago by mjo

• Branch changed from u/mkoeppe/ticket/15219 to u/mjo/ticket/15219
• Commit changed from c25ab78efd79e2a93a63b087a781ad2a929fe0a4 to d314399aeb258a26bdc3e5540b3db13f147dd644
• Status changed from needs_work to positive_review

This ticket should have only one trivial commit adding a single doctest. I've rebased (only) that commit onto develop; hopefully it's clear how to solve it if there's still a merge conflict.

New commits:

 ​d314399 `Trac #15219: add test for numerical_integral() of symbolic functions.`

### comment:15 Changed 7 months ago by vbraun

• Branch changed from u/mjo/ticket/15219 to d314399aeb258a26bdc3e5540b3db13f147dd644
• Resolution set to fixed
• Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.