Updates:
Summary: Remove the @vectorize decorator
Status: Accepted
Comment #3 on issue 2182 by asmeurer: Remove the @vectorize decorator
http://code.google.com/p/sympy/issues/detail?id=2182
checkodesol() is only vectorized in the second argument. I think something
like this would be just as simple:
def f(x):
if hasattr(x, '__iter__'):
return [f(i) for i in x]
<do the normal function here>
You don't even have to indent the entire function under an else block
because of the return. I think that other than removing the disadvantages
of decorators, the above is much clearer and more Pythonic. And I think it
can be generalized to multiple argument vectorizations by doing something
like:
def f(x, y, z, …):
"""
The arguments of this function are vectorized nested in the order that
they appear.
""""
if hasattr(x, "__iter__"):
return [f(i, y, z, ...) for i in x]
if hasattr(y, "__iter__"):
return [f(x, i, z, …) for i in y]
if hasattr(z, "__iter__"):
return [f(x, y, u, …) for i in z]
…
<do the normal function here>
For example:
In [92]: def f(a, b, c):
"""
Concatenate strings a, b, and c. It is multiple vectorized
"""
if hasattr(a, "__iter__"):
return [f(i, b, c) for i in a]
if hasattr(b, "__iter__"):
return [f(a, i, c) for i in b]
if hasattr(c, "__iter__"):
return [f(a, b, i) for i in c]
return a + b + c
.....:
In [103]: f('a', 'b', 'c')
Out[103]: abc
In [104]: f(['a', 'z'], 'b', 'c')
Out[104]: [abc, zbc]
In [105]: f(['a', 'z'], ['b', 'y'], 'c')
Out[105]: [[abc, ayc], [zbc, zyc]]
In [106]: f(['a', 'z'], ['b', 'y'], ['c', 'x'])
Out[106]: [[[abc, abx], [ayc, ayx]], [[zbc, zbx], [zyc, zyx]]]
In [107]: f(['a', 'z'], 'b', ['c', 'x'])
Out[107]: [[abc, abx], [zbc, zbx]]
In [109]: from sympy.utilities.decorator import *
In [110]: @vectorize(0, 1, 2)
.....: def g(a, b, c):
.....: return a + b + c
.....:
In [112]: g('a', 'b', 'c')
Out[112]: abc
In [113]: g(['a', 'z'], 'b', 'c')
Out[113]: [abc, zbc]
In [114]: g(['a', 'z'], ['b', 'y'], 'c')
Out[114]: [[abc, ayc], [zbc, zyc]]
In [115]: g(['a', 'z'], ['b', 'y'], ['c', 'x'])
Out[115]: [[[abc, abx], [ayc, ayx]], [[zbc, zbx], [zyc, zyx]]]
In [116]: g(['a', 'z'], 'b', ['c', 'x'])
Out[116]: [[abc, abx], [zbc, zbx]]
--
You received this message because you are subscribed to the Google Groups
"sympy-issues" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sympy-issues?hl=en.