I have a Python script which generates and outputs Mathematica matrices. I use RunProcess to run my Python script and I extract the StandardOutput which is the Mathematica matrix as a string.
What I would like to do is take that string and perform matrix operations on it, for example, compute its eigenvectors.
For example:
RunProcess[{"python", "myscript.py"}][[2]]
which outputs
{{0, 0, 1-r, 0, 0, 1-r},{1-r, 1-r, 0, 1-r, 0, 0},{0, 0, 0, 0, 1,
0},{0, 0, 0, 0, 0, 0},{0, 0, r, 0, 0, r},{r, r, 0, r, 0, 0}}
and I would like to have something like
MyMatrix[r_] := RunProcess[{"python", "myscript.py"}][[2]]
Which would give me the equivalent of:
MyMatrix[r_] := {{0, 0, 1-r, 0, 0, 1-r},{1-r, 1-r, 0, 1-r, 0, 0},{0, 0, 0, 0, 1,
0},{0, 0, 0, 0, 0, 0},{0, 0, r, 0, 0, r},{r, r, 0, r, 0, 0}}
which I can perform matrix operations on. The problem is, when I extract the standard out, the output is in a string format and it isn't converting to a symbolic matrix automatically. Is there some command I can use? Thanks.
ToExpression
- also, side note, consider not using single-character uppercase symbol names likeM
. You might be redefining system symbols! $\endgroup$ToExpression["{{1-r, 1-r, 0},{0, 0, 1},{r, r, 0}}"]
gives me a 3x3 matrix (you could test withDimensions
). You can call (for example)Eigenvalues
on it and get{1, 0, -r}
. Is that what you are expecting? $\endgroup$