Reputation: 612
This problem is caused by using SymPy to find reduced row echelon matrix, I have screen capture below. By using SymPy, I can get a NumPy array of object, it is quite confusing for me here. First, I don't see numbers in the rref matrix is wrapped by ' ' or " ",how come they are 'object'. Second, is there any way to convert it into a 'dtype = float' np.array?
M = sy.Matrix([[4, 0, 11, 3], [7, 23, -3, 7], [12, 11, 3, -4]])
M_rref = M.rref()
M_rref = np.array(M_rref[0])
M_rref.astype(float)
Upvotes: 0
Views: 2097
Reputation: 945
M_rref.astype(float)
will fix your problem.
Why does this happen?
In order to find out, try going back to your matrix M
and try the following:
>>> print(M[0])
>>> print(type(M[0]))
You will get:
4
sympy.core.numbers.Integer
That's why. Even though it says it's 4, it's actually a SymPy object that is stored inside the SymPy matrices. And that's the way it should be: SymPy is for symbolic mathematics. Therefore, when you convert it to a numpy array, NumPy will recognize sympy.core.numbers.Integer
as an object.
Hope that answers your question.
Upvotes: 1
Reputation: 18939
In this PR there is a note about incorrect conversion of numpy matrices to SymPy matrices. Using that (and not float
, which I don' think is intended to convert contents of an object to floats) I would do:
>>> sy.Matrix(M.rows, M.cols, [sympify(x) for x in M_rref]).n()
This should give you a SymPy matrix with entries evaluated as floats.
Upvotes: 0