Created
August 6, 2017 20:09
-
-
Save hoogenm/4a23009b22ffea1630cb7408b96c7d80 to your computer and use it in GitHub Desktop.
Matlab-like Matrix-constructor for Python's sympy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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