How to convert a Numpy matrix into a Sympy
<p>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</p>

<pre><code>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)
</code></pre>

<p>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:</p>

<p>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.</p>

<p>I have varied the statement of the final line to the following, none have worked.</p>

<pre><code>matrix_thing_sympy = lambdify(alpha,beta,make_matrix, SHIT)
</code></pre>

<p>AttributeError: module 'numpy' has no attribute 'doprint'</p>

<pre><code>matrix_thing_sympy = lambdify((alpha,beta),make_matrix(alpha,beta), SHIT)
</code></pre>

<p>VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future
  rational=rational) for x in a])</p>

<p>lambda 2,2: ([[17  6]
           ^
SyntaxError: invalid syntax</p>

<p>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.</p>

<p><strong>TL;DR</strong> Perform <code>sympy.Matrix(numpy_matrix)</code></p>

<p>From comments, I suggest this</p>

<h1>Converting NumPy matrix to SymPy matrix</h1>

<pre><code>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()
</code></pre>

<p>After then</p>

<pre><code>matrix_sympy.rref()
</code></pre>

<h1>Alternatively,</h1>

<p>NumPy also has RREF (strictly speaking, SciPy does)</p>

<pre><code>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
</code></pre>

<p>Both methods don't require symbolic variable. NumPy is not a SHIT thing.</p>

<h1>When to use SymPy</h1>

<p>Generally, you need SymPy when you want to find a general solution which is represented with a arbitrary variable without specific values.</p>

<pre><code>import sympy
x = symbols('x a b c')
y = a * x ** 2 + b * x + c # generall quadratic equation.
sympy.solve(y, x)
</code></pre>

<p>output:</p>

<pre><code>[(-b + sqrt(-4*a*c + b**2))/(2*a), -(b + sqrt(-4*a*c + b**2))/(2*a)]
</code></pre>

<p>In your example, there is no space for RREF to be represented with respect to <code>alpha</code> and <code>beta</code></p>

python numpy matrix sympy