how can I check if a matrix is square using sympy

how can I check if a matrix is square using sympy

Questions : how can I check if a matrix is square using sympy

626

To check if a matrix is square when the in4codes_python-3.x input is of the type np.ndarray i used

if len(V) != len(V[0]): raise ValueError('V is not a square matrix') 

but when I want to use sympy, len(V), for in4codes_python-3.x example for a 2×2 matrix, returns 4 instead in4codes_python-3.x of 2. I’ve tried using v.shape which returns in4codes_python-3.x (2,2), but I don’t know how to implement a in4codes_python-3.x ValueError if the input matrix isn’t square in4codes_python-3.x with this output.

Total Answers 1
27

Answers 1 : of how can I check if a matrix is square using sympy

The shape function should do what you in4codes_sympy want:

from sympy import shape def isSquare(M): s = shape(M) return s[0] == s[1] 

Also have a look at the docs

0