I want to convert the decimal numbers expressed in a numpy.ndarray
into a matrix representing elements in fractional form, and I’ve figured out the following python code snippet:
import os
from fractions import Fraction
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Lattice, Structure, Molecule, IStructure
s=IStructure.from_file(os.path.expanduser('~') + '/pymatgen-gap-affine_matrix/EntryWithCollCode136212.cif')
a = SpacegroupAnalyzer(s, 0.1)
SymOp=a.get_symmetry_operations()
b=SymOp[1].affine_matrix.tolist()
lst=[]
for i in range(len(b)):
lst.append([])
for j in range(len(b[i])):
print(Fraction(str(b[i][j])),end=',')
lst[i].append(Fraction(str(b[i][j])))
print('\n')
print(lst)
0,-1,0,1/4,1,0,0,1/4,0,0,1,1/4,0,0,0,1,
[[Fraction(0, 1), Fraction(-1, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(1, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(1, 1), Fraction(1, 4)], [Fraction(0, 1), Fraction(0, 1), Fraction(0, 1), Fraction(1, 1)]]
I want to obtain the result as shown in the first form, i.e., x/y
, instead of expressed as the [Fraction(x, y)
function, but I still couldn’t find a way to generate the above desired results. Any hints will be highly appreciated.
See here for the related discussion.
Regards,
HZ