Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

95 views
Skip to first unread message

hongy...@gmail.com

unread,
May 16, 2022, 5:03:37 AM5/16/22
to
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 from within the resulting array. Any hints will be highly appreciated.

Regards,
HZ

Dennis Lee Bieber

unread,
May 16, 2022, 11:27:58 AM5/16/22
to
On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com"
<hongy...@gmail.com> declaimed the following:


>print(lst)

Printing higher level structures uses the repr() of the structure and
its contents -- theoretically a form that could be used within code as a
literal. If you want human-readable str() you will need to write your own
output loop to do the formatting of the structure, and explicitly print
each item of the structure.


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

hongy...@gmail.com

unread,
May 16, 2022, 7:11:24 PM5/16/22
to
On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com"
> <hongy...@gmail.com> declaimed the following:
>
>
> >print(lst)
>
> Printing higher level structures uses the repr() of the structure and
> its contents -- theoretically a form that could be used within code as a
> literal. If you want human-readable str() you will need to write your own
> output loop to do the formatting of the structure, and explicitly print
> each item of the structure.

Thank you for your explanation. I have come up with the following methods:
```
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]]
import numpy as np
from fractions import Fraction
import re

def strmat(m):
if(np.array([m]).ndim==1):
return str(Fraction(m))
else: return list(map(lambda L:strmat(L), np.array(m)))

a=str(strmat(b))
a=re.sub(r"'","",a)
repr(a)
print(repr(a))
'[[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]'
```
Best,
HZ

hongy...@gmail.com

unread,
May 16, 2022, 7:31:20 PM5/16/22
to

hongy...@gmail.com

unread,
May 16, 2022, 8:22:29 PM5/16/22
to
On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com"
> <hongy...@gmail.com> declaimed the following:
>
>
> >print(lst)
>
> Printing higher level structures uses the repr() of the structure and
> its contents -- theoretically a form that could be used within code as a
> literal.

I tried with the repr() method as follows, but it doesn't give any output:

```
import os,sys
import numpy as np
from fractions import Fraction
import re
from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
from pymatgen.core import Lattice, Structure, Molecule, IStructure

def filepath(file):
script_dirname=os.path.dirname(os.path.realpath(__file__))
return (script_dirname + '/' + file)

s=IStructure.from_file(filepath('EntryWithCollCode136212.cif'))
a = SpacegroupAnalyzer(s)
SymOp=a.get_symmetry_operations()
b=SymOp[1].affine_matrix.tolist()

def strmat(m):
if(np.array([m]).ndim==1):
return str(Fraction(m))
else: return list(map(lambda L:strmat(L), np.array(m)))

lst=[]
for i in SymOp:
lst.append(i.affine_matrix.tolist())

a=str(strmat(lst))
a=re.sub(r"'","",a)
repr(a)
```

Dennis Lee Bieber

unread,
May 16, 2022, 8:48:27 PM5/16/22
to
On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com"
<hongy...@gmail.com> declaimed the following:


>
>I tried with the repr() method as follows, but it doesn't give any output:


I have no idea what 50% of those libraries are supposed to do, and am
not going to install them just to try out your posted code. If you really
want such help, post the MINIMUM example code the produces your problem.

>a=str(strmat(lst))
>a=re.sub(r"'","",a)

Explain what you believe this operation is doing, show us the input and
the output.

The best I can make out of that is that it is looking for single quote
characters within whatever "a" is, and replacing them with nothing.
Something much more understandable, without invoking a regular expression
library (especially when neither the search nor the replacement terms are
regular expressions) with simple string operations...

stripped = "".join(quoted.split("'"))

You also don't need to specify RAW format for the "'" -- Python is quite
happy mixing single and double quotes (that is: single quotes inside a
string using double quotes, double quotes inside a string using single
quotes, either inside strings using triply quoted delimiters)

>>> "'"
"'"
>>> '"'
'"'
>>> """'"'"""
'\'"\''
>>> '''"'"'''
'"\'"'
>>>

(Note that the interactive console displays results using repr(), and hence
escapes ' that are internal to avoid conflict with the ones wrapping the
output)

>>> repr('''"'"''')
'\'"\\\'"\''
>>> str('''"'"''')
'"\'"'
>>> print('''"'"''')
"'"
>>>

The print() operation does not wrap the output with extraneous quotes.

hongy...@gmail.com

unread,
May 16, 2022, 10:05:25 PM5/16/22
to
On Tuesday, May 17, 2022 at 8:48:27 AM UTC+8, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com"
> <hongy...@gmail.com> declaimed the following:
>
>
> >
> >I tried with the repr() method as follows, but it doesn't give any output:
> I have no idea what 50% of those libraries are supposed to do, and am
> not going to install them just to try out your posted code. If you really
> want such help, post the MINIMUM example code the produces your problem.
>
> >a=str(strmat(lst))
> >a=re.sub(r"'","",a)
>
> Explain what you believe this operation is doing, show us the input and
> the output.
>
> The best I can make out of that is that it is looking for single quote
> characters within whatever "a" is, and replacing them with nothing.
> Something much more understandable, without invoking a regular expression
> library (especially when neither the search nor the replacement terms are
> regular expressions) with simple string operations...
>
> stripped = "".join(quoted.split("'"))

Thank you for your above trick. I tried with the following code snippet:

```
from fractions import Fraction

def strmat(m):
if(np.array([m]).ndim==1):
return str(Fraction(m))
else: return list(map(lambda L:strmat(L), np.array(m)))

# For test:
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]]

a=str(strmat(b))
a1=stripped = "".join(a.split("'"))
a=re.sub(r"'","",a)
#repr(a)
print("a1 = "+ a1)
print("a = "+ a)
```

As you can see, both methods give the same results:

```
a1 = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]
a = [[0, -1, 0, 1/4], [1, 0, 0, 1/4], [0, 0, 1, 1/4], [0, 0, 0, 1]]
```

> You also don't need to specify RAW format for the "'" -- Python is quite
> happy mixing single and double quotes (that is: single quotes inside a
> string using double quotes, double quotes inside a string using single
> quotes, either inside strings using triply quoted delimiters)
>
> >>> "'"
> "'"
> >>> '"'
> '"'
> >>> """'"'"""
> '\'"\''
> >>> '''"'"'''
> '"\'"'
> >>>
>
> (Note that the interactive console displays results using repr(), and hence
> escapes ' that are internal to avoid conflict with the ones wrapping the
> output)
>
> >>> repr('''"'"''')
> '\'"\\\'"\''
> >>> str('''"'"''')
> '"\'"'
> >>> print('''"'"''')
> "'"
> >>>
>
> The print() operation does not wrap the output with extraneous quotes.

Thank your insightful explanation.

Regards,
HZ

Barry

unread,
May 17, 2022, 2:42:42 AM5/17/22
to


> On 17 May 2022, at 05:59, hongy...@gmail.com <hongy...@gmail.com> wrote:
>
> On Monday, May 16, 2022 at 11:27:58 PM UTC+8, Dennis Lee Bieber wrote:
>> On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com"
>> <hongy...@gmail.com> declaimed the following:
>>
>>
>>> print(lst)
>>
>> Printing higher level structures uses the repr() of the structure and
>> its contents -- theoretically a form that could be used within code as a
>> literal.
>
> I tried with the repr() method as follows, but it doesn't give any output:

Repr returns a string. You need to print its value to see it.

>
> ```
> import os,sys
> import numpy as np
> from fractions import Fraction
> import re
> from pymatgen.symmetry.analyzer import SpacegroupAnalyzer
> from pymatgen.core import Lattice, Structure, Molecule, IStructure
>
> def filepath(file):
> script_dirname=os.path.dirname(os.path.realpath(__file__))
> return (script_dirname + '/' + file)
>
> s=IStructure.from_file(filepath('EntryWithCollCode136212.cif'))
> a = SpacegroupAnalyzer(s)
> SymOp=a.get_symmetry_operations()
> b=SymOp[1].affine_matrix.tolist()
>
> def strmat(m):
> if(np.array([m]).ndim==1):
> return str(Fraction(m))
> else: return list(map(lambda L:strmat(L), np.array(m)))
>
> lst=[]
> for i in SymOp:
> lst.append(i.affine_matrix.tolist())
>
> a=str(strmat(lst))
> a=re.sub(r"'","",a)
> repr(a)

print(repr(a))

Barry

> ```
>
>> If you want human-readable str() you will need to write your own
>> output loop to do the formatting of the structure, and explicitly print
>> each item of the structure.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Peter J. Holzer

unread,
May 17, 2022, 11:31:07 AM5/17/22
to
On 2022-05-16 20:48:02 -0400, Dennis Lee Bieber wrote:
> On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com"
> <hongy...@gmail.com> declaimed the following:
> >a=re.sub(r"'","",a)
>
> Explain what you believe this operation is doing, show us the input and
> the output.
>
> The best I can make out of that is that it is looking for single quote
> characters within whatever "a" is, and replacing them with nothing.
> Something much more understandable, without invoking a regular expression
> library (especially when neither the search nor the replacement terms are
> regular expressions) with simple string operations...
>
> stripped = "".join(quoted.split("'"))

Whether that's easier to understand it very much in the eye of the
beholder.

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | h...@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
signature.asc

MRAB

unread,
May 17, 2022, 12:24:25 PM5/17/22
to
On 2022-05-17 16:21, Peter J. Holzer wrote:
> On 2022-05-16 20:48:02 -0400, Dennis Lee Bieber wrote:
>> On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com"
>> <hongy...@gmail.com> declaimed the following:
>> >a=re.sub(r"'","",a)
>>
>> Explain what you believe this operation is doing, show us the input and
>> the output.
>>
>> The best I can make out of that is that it is looking for single quote
>> characters within whatever "a" is, and replacing them with nothing.
>> Something much more understandable, without invoking a regular expression
>> library (especially when neither the search nor the replacement terms are
>> regular expressions) with simple string operations...
>>
>> stripped = "".join(quoted.split("'"))
>
> Whether that's easier to understand it very much in the eye of the
> beholder.
>
As it's just a simple replacement, I would've thought that the 'obvious'
solution would be:
a = a.replace("'", "")

Dennis Lee Bieber

unread,
May 17, 2022, 9:11:32 PM5/17/22
to
On Tue, 17 May 2022 17:20:54 +0100, MRAB <pyt...@mrabarnett.plus.com>
declaimed the following:

>As it's just a simple replacement, I would've thought that the 'obvious'
>solution would be:
> a = a.replace("'", "")

Mea culpa...

Guess it's time for me to review the library reference for basic data
types again. I'm so used to the .join(.split()) (usually for other purposes
-- like a quick&dirty TSV/CSV formatting without importing the csv module).
The only firm item is that I do not look at any regular expression module
if I can come up with something using native data type methods -- and using
the overhead of re with just simple text [ie; nothing that might be called
an "expression" designed to match /varying/ content) seems really
inefficient.

Cousin Stanley

unread,
May 18, 2022, 5:26:25 PM5/18/22
to
#!/usr/bin/env python3

'''
NewsGroup .... comp.lang.python

Subject ...... Convert the decimal numbers
expressed in a numpy.ndarray
into a matrix representing elements
in fractiona

Date ......... 2022-05-16

Post_By ...... hongy...

Edit_By ...... Stanley C. Kitching
'''

import numpy as np

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

a = [ ]

print( '\n b .... \n' )

for row in b :
arow = []
print( ' ' , row )

for dec_x in row :
frac_x = Fraction( dec_x )
arow.append( frac_x )

a.append( arow )


# using f-string format

print( '\n a .... \n' )

for row in a :

for item in row :

print( f' {item} ' , end = '' )

print()

# ------------------------------------------

--
Stanley C. Kitching
Human Being
Phoenix, Arizona

hongy...@gmail.com

unread,
May 21, 2022, 8:38:10 PM5/21/22
to
This method doesn't work, as shown below:


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]

a ....

0 0 0 1

jak

unread,
May 22, 2022, 10:55:10 AM5/22/22
to
this way?

from fractions import Fraction

val = Fraction(0.25)

show = f'{str(val.numerator)}/{str(val.denominator)}' if val.denominator
!= 1 else str(val.numerator)

print(show)

Cousin Stanley

unread,
May 23, 2022, 3:20:53 PM5/23/22
to

> hongy... wrote ....
>
> This method doesn't work, as shown below:
>
? 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]
>
> a ....
>
> 0 0 0 1
>

# -----------------------------------

Using ....

debian 11.3 bullseye
python 3.9
numpy 1,21,5

Code as I posted in my reply dated 2022-05-18


$ python3 np_array_to_fractions_2.py

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]

a ....

0 -1 0 1/4
1 0 0 1/4
0 0 1 1/4
0 new messages