# Convert the decimal numbers expressed in a \`numpy.ndarray\` into a matrix representing elements in fractional form

**URL:** <https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780>\
**Category:** Python Help\
**Tags:** help\
**Created:** [May 16, 2022, 4:48am UTC](https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780 "2022-05-16T04:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hongyi-zhao](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hongyi-zhao/32/1981_2.png) [@hongyi-zhao](https://discuss.python.org/u/hongyi-zhao)\
**Post date:** [May 16, 2022, 4:48am UTC](https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780/1 "2022-05-16T04:48:59Z")

</div>

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:

```python
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](https://groups.google.com/g/comp.lang.python/c/F1rzt7ntrh4) for the related discussion.

Regards,  
HZ

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [May 16, 2022, 10:37am UTC](https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780/2 "2022-05-16T10:37:17Z")

</div>

I think you have misread the StackOverflow post.

You don’t need `Fraction(str(i))` to convert floats to fractions, and that is not what the SO post says. It uses `Fraction(i)` alone.

It does say you can use `str(Fraction(i))` to get a string in the format `"1/4"`.

```
x = 0.25
Fraction(x) # returns Fraction(1, 4)
str(Fraction(x)) # returns '1/4' (a string)

```

It is not clear to me whether you want an array of strings, or an array  
of Fraction objects.

What happens if you change this line?

```
a = SpacegroupAnalyzer(s, 0.1)

# Change to this instead:
a = SpacegroupAnalyzer(s, Fraction(1, 10))
```

---

<div class="post-metadata">

**Author:** ![hongyi-zhao](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hongyi-zhao/32/1981_2.png) [@hongyi-zhao](https://discuss.python.org/u/hongyi-zhao)\
**Post date:** [May 16, 2022, 1:16pm UTC](https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780/3 "2022-05-16T13:16:47Z")

</div>

> [@steven.daprano](#):
>
> It is not clear to me whether you want an array of strings, or an array  
> of Fraction objects.

I want to store the following format in the nested list:

```auto
[[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]

```

> [@steven.daprano](#):
>
> ```auto
> # Change to this instead:
> a = SpacegroupAnalyzer(s, Fraction(1, 10))
> 
> ```

I changed to the following:

```auto
from fractions import Fraction
b=[[0.0, -1.0, 0.0, 0.25], [1.0, 0.0, 0.0, 0.25], [0.0, 0.0, 1.0, 0.25], [0.0, 0.0, 0.0, 1.0]]

lst=[]
for i in range(len(b)):
    lst.append([]) 
    for j in range(len(b[i])):
        print(Fraction(b[i][j]))
        lst[i].append(Fraction(b[i][j]))

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)]]

```

As you can see, if I print the elements separately, it will be displayed as the corresponding fractional form, while if I print the list in batch, the following form will be generated:

```auto
[[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)]]

```
