Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional symbolic matrix in Python

I would like to create a 3D matrix of specific size by calculating a value for each combination of indexes. Each value in the matrix will be symbolic.

What I tried up to now:

import numpy as np
import sympy as sp

var1 = np.arange(1,10,2)
var2 = np.arange(1,10,2)
var3 = np.arange(20,50,5)

myMatrix = np.zeros(shape = (len(var1), len(var2), len(var3)))
t = sp.symbols('t')

for ii in range(len(var1)):
    for jj in range(len(var2)):
        for kk in range(len(var3)):
            myMatrix[ii][jj][kk] = var1[ii] * var2[jj] * var3[kk] * t

This gives me the error:

TypeError: can't convert expression to float

which as far as I understand is due to combining numpy and sympy. Therefore, I tried:

myMatrix = sp.MatrixSymbol('temp', len(var1), len(var2), len(var3))

instead of:

myMatrix = np.zeros(shape = (len(var1), len(var2), len(var3)))

and got an error:

TypeError: new() takes exactly 4 arguments (5 given)

To sum up, my question is: how can I create a 3D matrix with any variables inside to be able to use it in the nested loop, which involves symbolic calculation?

(This is my first post in this community, so please let me know if I did anything wrong.)

like image 513
user_185051 Avatar asked Feb 28 '16 11:02

user_185051


People also ask

How to create a multidimensional array in Python?

In Python, Multidimensional Array can be implemented by fitting in a list function inside another list function, which is basically a nesting operation for the list function. Here, a list can have a number of values of any data type that are segregated by a delimiter like a comma. Nesting the list can result in creating a combination ...

How to multiply matrices in Python?

There are mainly 3 ways of implementing matrix multiplication in Python. using @ operator on 2 matrices. Numpy dot method is used to find the product of two arrays. There is a thin-line difference between array and matrices. So if you are seeing error while performing multiplication with other 2 methods then try numpy.dot () method.

How to create a matrix in Python?

Let’s see how to create matrix in python: In Python, there exists a popular library called NumPy. To work with NumPy, we need to install it. The command to install NumPy is ‘pip install NumPy’. To make use of NumPy in code, we have to import it as ‘ import NumPy as np’. Matrix is created using NumPy.matrix () function.

What is the difference between multidimensional and NumPy arrays?

The difference between Multidimensional and Numpy Arrays is that numpy arrays are homogeneous, i.e. it can contain an only integer, string, float, etc., values and their size are fixed. The multidimensional list can be easily converted to Numpy arrays as below:


1 Answers

The first error you get is, as you suggested, because you try to save a sympy type object into a numpy zeros array which is of type numbers. One option would be to use a numpy array of objects, which works as follows,

import numpy as np
import sympy as sp

var1 = np.arange(1,10,2)
var2 = np.arange(1,10,2)
var3 = np.arange(20,50,5)

myMatrix = np.empty((len(var1), len(var2), len(var3)), dtype=object)
t = sp.symbols('t')

for ii in range(len(var1)):
    for jj in range(len(var2)):
        for kk in range(len(var3)):
            myMatrix[ii][jj][kk] = var1[ii] * var2[jj] * var3[kk] * t

Although for large sizes this isn't too efficient and not the way numpy should work. For sympy arrays this may be the only way to go however as it seems that, at least in my version of sympy (0.7.1.rc1), 3D arrays are not supported. For

myMatrix = sp.zeros((len(var1), len(var2), len(var3)))

I get the following error

ValueError: Matrix dimensions should be a two-element tuple of ints or a single int!
like image 132
Ed Smith Avatar answered Sep 23 '22 20:09

Ed Smith