D
D
dodo1010001012017-03-22 15:15:32
Python
dodo101000101, 2017-03-22 15:15:32

How to create a symbolic matrix?

It is necessary to create a character matrix of arbitrary size. I wrote a function, but something went wrong. Since this is part of a large code, I do not intend to completely sympy with numpy.
Here is the code:

import numpy as np
from sympy import *

shape = (4,4)
X = np.empty(shape)
for i in range(shape[0]):
    for j in range(shape[1]):
        X[i][j] = Symbol("x"+'_{'+str(i*10+j+11)+'}')

He writes, of course:
224 if result.is_number and result.as_real_imag()[1]:
225 raise TypeError("can't convert complex to float")
--> 226 raise TypeError("can't convert expression to float")
227
228 def __complex__ (self):
TypeError: can't convert expression to float

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sly_tom_cat ., 2017-03-22
@Sly_tom_cat

Because np.empty() is an array of numbers (float), and you shove characters into it

A
Andrew, 2017-03-23
@Dronablo

Maybe something like this:

import numpy as np
shape = (4,4)
np.core.defchararray.add('x_',(((np.arange(shape[0])+1)*10).reshape(-1,1)+1 + np.arange(shape[1])).astype(np.str))

array(, 
      dtype='<U13')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question