On 29 Dez., 23:29, Scott <scotta_2...@yahoo.com> wrote:
> Is there a clever way to export a Matrix of sympy symbols and the
> argument list to to a text (.py) file with scipy friendly syntax?
> The C and Fortran printers strip out the sympy syntax and replace it
> with the proper C or Fortran syntax, however the python printer
> assumes that sympy will be the using package.
>
> In particular the python printer retains the Rational function and
> the Symbol class which are foreign to numpy.
>
> My problem involves a creation  of a Jacobian  (42x66 matrix with 201
> parameters) as well as the Residual function of 42 equations, 66
> degrees of freedom and 135 or so parameters.
>
> My current work around is to unwrap the matrix then write the matrix
> elements, arguments list, and residual elements as text strings and
> save as a .py file.  Form the other file I use exec and lambda to make
> numpy friendly functions.  I works but it is ugly.  It is 9 lines with
> 42000 characters.
>
> lambdify has the option to use numpy or python.math libraries.  Is
> this feature hidden somewhere and I simply missed it?
>
> V/R
>
> Scott

If you have a look at the source code of lambdify() (using for example

>>> from sympy import *
>>> source(lambdify)

), you will see that it basically uses

eval(lambdastr(args, expr), namespace).

So you could use lambdastr() and then perform a substitution using the
dicts in sympy/utilities/lambdify.py. Like for example

>>> from sympy.utilities.lambdify import lambdastr, NUMPY_TRANSLATIONS
>>> from sympy import atan, var
>>> var('x y')
(x, y)
>>> s = lambdastr((x,y), atan(x + y))
>>> for k, v in NUMPY_TRANSLATIONS.iteritems():
...   s = s.replace(k, v)
...
>>> s
'lambda x,y: (arctan(x + y))'

I guess that this is not less ugly than your solution, but this is
basically what lambdify() is doing.

Probably lambidify() should be refactored that you can use it to get a
numpy-friendly lambdastr(). (Currently it operates rather on
namespaces than on strings.)

Vinzent

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to 
sympy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to