简体   繁体   中英

Using numpy arrays of sympy numbers

Is it advisable, while working with arrays of symbolic expresions, to use numpy arrays?

Something like

u0=numpy.array([Number(1.0), Number(1.0), Number(1.0)])

I mean, is it faster to use numpy arrays instead of python lists?

If so, certain operations with numpy arrays seem to convert automatically to float symbolic expresions, for example:

u0=np.array([Number(1.0), Number(1.0), Number(1.0)]) u = np.zeros((10, 3)) u[0] = u0

Now while type(u0[0]) >> sympy.core.numbers.Float ,

type(u[0][0]) >> numpy.float64

How can I avoid numpy to convert the symbolic expresions copied to float64?

I doubt there's much speed difference vs. a list, since using any non-NumPy data type (ie, any SymPy data type) in a NumPy array results in dtype=object , meaning the array is just an array of pointers (which a list is too).

It's really unclear why you want to use a NumPy array?

The first question is, why don't you want to use float64 ? Assumedly you are using

  • Symbolic expressions (such as x**2 or pi ),
  • Rational numbers, or
  • sympy.Float objects with higher precision

Those are the only reasons I can think of that you would want to prefer a SymPy type over a NumPy one.

The main advantage of using a NumPy array would be if you want to take advantage of NumPy's superior indexing syntax. As Stelios pointed out, you can get much of this by using SymPy's tensor module. This is really the only reason to use them, and you have to be careful and be aware of which NumPy methods/functions will work and which won't.

The reason is that any NumPy mathematical function will not work (or at best, will convert the array to float64 first). The reason is that NumPy functions are designed to work on NumPy data types. They don't know about the above data types. To get exact values (symbolic expressions or rational numbers), or higher precision floating point values (for the case of sympy.Float ), you need to use SymPy functions, which do not work on NumPy arrays.

If on the other hand (again, it's not clear what exactly you are trying to do), you want to do calculations in SymPy and then use NumPy functions to numerically evaluate the expressions, you should use SymPy to create your expressions, and then lambdify (or ufuncify if performance becomes an issue) to convert the expressions to equivalent NumPy functions, which can operate on NumPy arrays of NumPy dtypes.

I think it is ok to work with numpy arrays, if necessary. You should bear in mind that arrays are fundamentally different from lists. Most importantly, all array elements have to be of the same type and you cannot change the type.

In particular, you define the array u0 which is per default an array of floats. That is why you cannot assign any sympy objects to it.

I myself use numpy arrays to accommodate sympy expressions. Most notably, in cases where I need more than 2 dimensions and therefore cannot use Sympy matrices.

If the only reason to use arrays instead of lists is speed, it might not be advisable. Especially, since you have to be a bit careful with types (as you find out) and there should be less surprises when using lists or rather sympy.Matrix.

In your example, you can fix the problem by defining a proper data type:

u = np.zeros((10, 3), dtype=sp.Symbol)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM