Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
> 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 0 0 1 -- Stanley C. Kitching Human Being Phoenix, Arizona -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Thursday, May 19, 2022 at 5:26:25 AM UTC+8, Cousin Stanley wrote: > #!/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() > > # -- 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 > -- > Stanley C. Kitching > Human Being > Phoenix, Arizona -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
#!/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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Tue, 17 May 2022 17:20:54 +0100, MRAB 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. -- Wulfraed Dennis Lee Bieber AF6VN wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
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" 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("'", "") -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
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" > 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 Description: PGP signature -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
> On 17 May 2022, at 05:59, 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" >> 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 > -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
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" > 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) ``` > 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
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Tuesday, May 17, 2022 at 7:11:24 AM UTC+8, 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" > > 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 See here [1] for the related discussion. [1] https://discuss.python.org/t/convert-the-decimal-numbers-expressed-in-a-numpy-ndarray-into-a-matrix-representing-elements-in-fractional-form/15780 -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
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" > 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
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" > 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 > -- > Wulfraed Dennis Lee Bieber AF6VN > wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Mon, 16 May 2022 02:03:26 -0700 (PDT), "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 wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
On Mon, 16 May 2022 17:22:17 -0700 (PDT), "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. -- Wulfraed Dennis Lee Bieber AF6VN wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/ -- https://mail.python.org/mailman/listinfo/python-list