Aaron,
Thanks a lot for your help! I was initially using sympy version 0.7.3 and
noticed a matrix type is still output with the code as you have given. Upon
upgrading to 0.7.4, it works perfectly!
Nick
On Tuesday, December 10, 2013 9:05:30 PM UTC-5, Aaron Meurer wrote:
>
> Sure. lambdify has a default set of translations (see
> sympy.utilities.lambdify.NUMPY_TRANSLATIONS to see what they are), but
> you can override them. The best way is to pass in a list for the
> module argument to lambdify, where the first argument is a dictionary
> of custom translations, like
>
> In [35]: import numpy
>
> In [36]: lambdify(x, Matrix([[x, 2], [3, 4]]), [{'ImmutableMatrix':
> numpy.array}, "numpy"])(1)
> Out[36]:
> array([[1, 2],
> [3, 4]])
>
> (note that ImmutableMatrix should be used because that is what the
> matrix gets converted to before it gets lambdified)
>
> As you can imagine, you can use this functionality to translate SymPy
> objects into whatever objects or functions you want.
>
> Aaron Meurer
>
>
> On Tue, Dec 10, 2013 at 4:55 PM, Nicholas Chisholm
> <[email protected] <javascript:>> wrote:
> > I'm new to sympy (and very impressed with what I can do with it), but
> have
> > been unable to figure something out about converting certain matrix
> > expressions into their "lambda function" form for fast evaluation.
> >
> > I have a sympy matrix containing symbols x, y, and z. If I invoke
> lambdify
> > on this matrix for those symbols, I get a function that returns a numpy
> > matrix (great!). Is it also possible to make it return a numpy array
> (rather
> > than a matrix)? The reason I need this is because my matrix is actually
> a
> > rank-3 tensor when the arguments x,y,z are supplied (which are vectors).
> In
> > other words, I want to supply 3 equally sized vectors to my matrix to
> create
> > an m-by-n-by-k numpy array where my original [sympy] matrix was m-by-n.
> >
> > For example, I could do:
> >>>> fn = lambda x,y: np.array( [ [x+y, x-y], [x-y, y-x] ] )
> >>>> fn(1,2)
> > array([[ 3, -1],
> > [-1, 1]]) # Gives back original array shape (2x2)
> >>>>
> >>>> arr1 = np.array([1,2,3])
> >>>> arr2 = np.array([4,5,6])
> >>>> fn(arr1, arr2)
> > array([[[ 5, 7, 9],
> > [-3, -3, -3]],
> >
> > [[-3, -3, -3],
> > [ 3, 3, 3]]])
> >>>> fn(arr1, arr2).shape # (2x2x3)
> > (2L, 2L, 3L)
> >
> > I basically want to do the above with a sympy matrix rather than
> explicitly
> > typing the lambda function.
> >
> > Thanks!
> >
> > --
> > You received this message because you are subscribed to the Google
> Groups
> > "sympy" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> an
> > email to [email protected] <javascript:>.
> > To post to this group, send email to [email protected]<javascript:>.
>
> > Visit this group at http://groups.google.com/group/sympy.
> > For more options, visit https://groups.google.com/groups/opt_out.
>
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.