sympy / sympy-live

The source for the code at live.sympy.org
https://live.sympy.org
Other
116 stars 65 forks source link

ValueError: The truth value of an array #110

Open jrovegno opened 7 years ago

jrovegno commented 7 years ago

From Docs Basic_operations.html In lambdify example:

Python console for SymPy 1.0 (Python 2.7.5)

These commands were executed:
>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)

>>> import numpy
>>> a = numpy.arange(10)
>>> expr = sin(x)
Exception in SymPy Live of type 
<type 'exceptions.ValueError'>
for reference the last 5 stack trace entries are
Traceback (most recent call last):
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/shell.py", line 781, in post
    live.evaluate(statement, session, printer, stream)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/shell.py", line 453, in evaluate
    if name not in old_globals or val != old_globals[name]:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
>>> f = lambdify(x, expr, "numpy")
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/utilities/lambdify.py", line 387, in lambdify
    func = eval(lstr, namespace)
  File "<string>", line 1
    lambda _Dummy_797: (<module 'sympy.core.expr' from '/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/expr.pyc'>)
                        ^
SyntaxError: invalid syntax
>>> f(a)
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/function.py", line 720, in __new__
    obj = super(AppliedUndef, cls).__new__(cls, *args, **options)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/cache.py", line 93, in wrapper
    retval = cfunc(*args, **kwargs)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/compatibility.py", line 884, in wrapper
    return user_function(*args, **kwds)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/function.py", line 389, in __new__
    pr = max(cls._should_evalf(a) for a in result.args)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/function.py", line 389, in <genexpr>
    pr = max(cls._should_evalf(a) for a in result.args)
  File "/base/data/home/apps/s~sympy-live-hrd/49.400254913747479351/sympy/sympy/core/function.py", line 407, in _should_evalf
    if arg.is_Float:
AttributeError: 'numpy.ndarray' object has no attribute 'is_Float'
ivanistheone commented 7 years ago

The root cause of this is the comparison val != old_globals[name], which doesn't work for numpy.ndarrays. Need to use numpy.allclose to compare nympy arrays. Fix in the works.