See my following code snippet:
gMatrix=SparseArray[{{1,3}->1,{2,1}->1,{3,2}->1,{4,4}->-(1/2),{4,5}->-(1/2),{4,6}->-(1/2),{4,7}->-(1/2),{5,4}->1/2,{5,5}->-(1/2),{5,6}->1/2,{5,7}->-(1/2),{6,4}->1/2,{6,5}->-(1/2),{6,6}->-(1/2),{6,7}->1/2,{7,4}->1/2,{7,5}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
hMatrix=SparseArray[{{1,5}->1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->1/2,{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->1/2,{4,1}->1,{5,4}->1,{6,2}->-(1/2),{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->-(1/2),{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
kMatrix=SparseArray[{{1,4}->-1,{2,2}->-(1/2),{2,3}->-(1/2),{2,6}->-(1/2),{2,7}->1/2,{3,2}->1/2,{3,3}->-(1/2),{3,6}->-(1/2),{3,7}->-(1/2),{4,5}->-1,{5,1}->1,{6,2}->1/2,{6,3}->1/2,{6,6}->-(1/2),{6,7}->1/2,{7,2}->-(1/2),{7,3}->1/2,{7,6}->-(1/2),{7,7}->-(1/2),{_,_}->0},7];
In[47]:= {gMatrix, hMatrix, kMatrix} // Normal // Rationalize // ExportString[#, "PythonExpression"] &
Out[47]= "[[[0, 0, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [0, 1, 0, \
0, 0, 0, 0], [0, 0, 0, -0.5, -0.5, -0.5, -0.5], [0, 0, 0, 0.5, -0.5, \
0.5, -0.5], [0, 0, 0, 0.5, -0.5, -0.5, 0.5], [0, 0, 0, 0.5, 0.5, \
-0.5, -0.5]], [[0, 0, 0, 0, 1, 0, 0], [0, -0.5, -0.5, 0, 0, 0.5, \
0.5], [0, 0.5, -0.5, 0, 0, -0.5, 0.5], [1, 0, 0, 0, 0, 0, 0], [0, 0, \
0, 1, 0, 0, 0], [0, -0.5, 0.5, 0, 0, -0.5, 0.5], [0, -0.5, -0.5, 0, \
0, -0.5, -0.5]], [[0, 0, 0, -1, 0, 0, 0], [0, -0.5, -0.5, 0, 0, -0.5, \
0.5], [0, 0.5, -0.5, 0, 0, -0.5, -0.5], [0, 0, 0, 0, -1, 0, 0], [1, \
0, 0, 0, 0, 0, 0], [0, 0.5, 0.5, 0, 0, -0.5, 0.5], [0, -0.5, 0.5, 0, \
0, -0.5, -0.5]]]"
I want to export the expression in fraction format, that is, in this example, all 0.5 are displayed as 1/2. Are there any tips to achieve this goal?
Regards,
Zhao