Skip to content

Instantly share code, notes, and snippets.

@hoogenm
Created August 6, 2017 20:09
Show Gist options
  • Save hoogenm/4a23009b22ffea1630cb7408b96c7d80 to your computer and use it in GitHub Desktop.
Save hoogenm/4a23009b22ffea1630cb7408b96c7d80 to your computer and use it in GitHub Desktop.
Matlab-like Matrix-constructor for Python's sympy
import sympy as sp
def Mtrix(matrix_def):
'''
Constructs a Sympy Matrix from the string matrix_def that has a Matlab-like syntax:
E.g.: Mtrix("a b c; d e f; g h i") creates a 3x3 matrix where [a b c], [d e f], [g h i] are the rows.
Example: Mtrix("0 1/2 7; 3 -4 8; 17/3 4 87")
returns the same matrix as ...
sp.Matrix([[0, "1/2", 7], [3, -4, 8], ["17/3", 4, 87]])
would do
'''
return sp.Matrix([line.strip().split(' ') for line in matrix_def.split(';')])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment