Reputation: 81
I have constructed a randomly generated matrix of a specified size and that part works great. Give it a row and column size and boom a matrix of whole numbers from 0 to 100. More recently I tried to perform a sympy operation to a numpy matrix and python kept crashing on me. I soon learned that operations from sympy could not work on a numpy matrix. So I looked into how to convert a numpy into a sympy, but more often than not I have only found sympy into numpy using lambdify. I was wondering if I could use lambdify still to convert from numpy to sympy however. Here is the code I have
import math
import numpy as SHIT
import sympy as GARBAGE
from sympy import *
from sympy import Matrix
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy import Function
import __future__
import __init__
# init_print(use_unicode=True)
alpha = eval(input("How many rows? "))
beta = eval(input("How many columns? "))
def make_matrix(alpha,beta):
matrix_thing = SHIT.random.randint(0,50,(alpha,beta))
return(matrix_thing)
print(make_matrix(alpha,beta))
matrix_thing_sympy = lambdify(alpha,beta,make_matrix(alpha,beta), SHIT)
Traceback: Argument must be either a string, dict or module but it is: [24 11] FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison if modname in modlist:
The [24 11] you see was from a randomly generated 2 by 2 matrix. So if lambdify is reading this row by row, how is this not a string of numbers? This is the string: 24, 11. But python doesn't seem to agree with me on that.
I have varied the statement of the final line to the following, none have worked.
matrix_thing_sympy = lambdify(alpha,beta,make_matrix, SHIT)
AttributeError: module 'numpy' has no attribute 'doprint'
matrix_thing_sympy = lambdify((alpha,beta),make_matrix(alpha,beta), SHIT)
VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future rational=rational) for x in a])
lambda 2,2: ([[17 6] ^ SyntaxError: invalid syntax
More importantly to me is, why won't this just work by default? I had figured if a matrix were a matrix that it is a matrix and who cares about if it were made using numpy sympy or any py for that matter. I digress but maybe this isn't a half bad point for me to understand as well.
Upvotes: 2
Views: 15740
Reputation: 4076
TL;DR Perform sympy.Matrix(numpy_matrix)
From comments, I suggest this
import math
import numpy as SHIT
import sympy as GARBAGE
from sympy import *
from sympy import Matrix
from sympy.utilities.lambdify import lambdify, implemented_function
from sympy import Function
import __future__
import __init__
# init_print(use_unicode=True)
alpha = eval(input("How many rows? "))
beta = eval(input("How many columns? "))
def make_matrix(alpha,beta):
matrix_thing = SHIT.random.randint(0,50,(alpha,beta))
return(matrix_thing)
matrix_sympy = Matrix(make_matrix(alpha, beta)) # use sympy.Matrix()
After then
matrix_sympy.rref()
NumPy also has RREF (strictly speaking, SciPy does)
import numpy as np
import scipy.linalg as la
def make_matrix(alpha,beta):
matrix_thing = np.random.randint(0,50,(alpha,beta))
return(matrix_thing)
matrix_numpy = make_matrix(alpha, beta)
(_, rref) = la.qr(matrix_numpy) # perform QR decomposition, R is RREF
Both methods don't require symbolic variable. NumPy is not a SHIT thing.
Generally, you need SymPy when you want to find a general solution which is represented with a arbitrary variable without specific values.
import sympy
x = symbols('x a b c')
y = a * x ** 2 + b * x + c # generall quadratic equation.
sympy.solve(y, x)
output:
[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
In your example, there is no space for RREF to be represented with respect to alpha
and beta
Upvotes: 4