Opened 7 years ago

Last modified 9 months ago

#15219 new defect

numerical integral needs an operand for callable symbolic functions

Reported by: kcrisman Owned by:
Priority: minor Milestone: sage-6.4
Component: calculus Keywords:
Cc: eviatarbach Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by kcrisman)

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.

Change History (7)

comment:1 Changed 7 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 7 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:3 Changed 7 years ago by eviatarbach

  • Cc eviatarbach added

comment:4 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:6 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:7 Changed 9 months 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)
Note: See TracTickets for help on using tickets.