Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to convert a SymPy matrix to a numpy array/matrix

I am not sure if the approach I've been using in sympy to convert a MutableDenseMatrix to a numpy.array or numpy.matrix is a good current practice.

I have a symbolic matrix like:

g = sympy.Matrix( [[   x,  2*x,  3*x,  4*x,  5*x,  6*x,  7*x,  8*x,   9*x,  10*x],                    [x**2, x**3, x**4, x**5, x**6, x**7, x**8, x**9, x**10, x**11]] ) 

and I am converting to a numpy.array doing:

g_func = lambda val: numpy.array( g.subs( {x:val} ).tolist(), dtype=float ) 

where I get an array for a given value of x.

Is there a better built-in solution in SymPy to do that?

Thank you!

like image 764
Saullo G. P. Castro Avatar asked Jun 12 '13 15:06

Saullo G. P. Castro


People also ask

Is SymPy compatible with NumPy?

In general, SymPy functions do not work with objects from other libraries, such as NumPy arrays, and functions from numeric libraries like NumPy or mpmath do not work on SymPy expressions.

Is NumPy array and matrix same?

Numpy matrices are strictly 2-dimensional, while numpy arrays (ndarrays) are N-dimensional. Matrix objects are a subclass of ndarray, so they inherit all the attributes and methods of ndarrays.

How do you create a NumPy matrix?

We can create a matrix in Numpy using functions like array(), ndarray() or matrix(). Matrix function by default creates a specialized 2D array from the given input. The input should be in the form of a string or an array object-like.


2 Answers

This looks like the most straightforward:

np.array(g).astype(np.float64) 

If you skip the astype method, numpy will create a matrix of type 'object', which won't work with common array operations.

like image 192
Turtles Are Cute Avatar answered Sep 25 '22 13:09

Turtles Are Cute


This answer is based on the advices from Krastanov and asmeurer. This little snippet uses sympy.lambdify:

from sympy import lambdify from sympy.abc import x, y  g = sympy.Matrix([[   x,  2*x,  3*x,  4*x,  5*x,  6*x,  7*x,  8*x,   9*x,  10*x],                   [y**2, y**3, y**4, y**5, y**6, y**7, y**8, y**9, y**10, y**11]]) s = (x, y) g_func = lambdify(s, g, modules='numpy') 

where g is your expression containing all symbols grouped in s.

If modules='numpy' is used the output of function g_func will be a np.ndarray object:

g_func(2, 3) #array([[     2,      4,      6,      8,     10,     12,     14,     16,       18,     20], #       [     9,     27,     81,    243,    729,   2187,   6561,  19683,    59049, 177147]])  g_func(2, y) #array([[2, 4, 6, 8, 10, 12, 14, 16, 18, 20], #       [y**2, y**3, y**4, y**5, y**6, y**7, y**8, y**9, y**10, y**11]], dtype=object) 

If modules='sympy' the output is a sympy.Matrix object.

g_func = lambdify(vars, g, modules='sympy') g_func(2, 3) #Matrix([[2,  4,  6,   8,  10,   12,   14,    16,    18,     20], #        [9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147]])  g_func(2, y) #Matrix([[   2,    4,    6,    8,   10,   12,   14,   16,    18,    20], #        [y**2, y**3, y**4, y**5, y**6, y**7, y**8, y**9, y**10, y**11]]) 
like image 26
Saullo G. P. Castro Avatar answered Sep 22 '22 13:09

Saullo G. P. Castro