Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

HOLY MACRO!


devel / comp.lang.python / Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

SubjectAuthor
* Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
+* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix repDennis Lee Bieber
|+* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
||`- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
|`* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
| +* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix repDennis Lee Bieber
| |+- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
| |+- Re: Convert the decimal numbers expressed in a `numpy.ndarray` intoPeter J. Holzer
| |`* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into aMRAB
| | `- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix repDennis Lee Bieber
| `- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into aBarry
`* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into aCousin Stanley
 `* Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ahongy...@gmail.com
  +- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into ajak
  `- Re: Convert the decimal numbers expressed in a `numpy.ndarray` into aCousin Stanley

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

<5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18289&group=comp.lang.python#18289

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:59cd:0:b0:2f3:c08d:9ffa with SMTP id f13-20020ac859cd000000b002f3c08d9ffamr14313374qtf.564.1652691806695;
Mon, 16 May 2022 02:03:26 -0700 (PDT)
X-Received: by 2002:a05:620a:44c6:b0:6a0:51fb:49cf with SMTP id
y6-20020a05620a44c600b006a051fb49cfmr11104149qkp.537.1652691806510; Mon, 16
May 2022 02:03:26 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 16 May 2022 02:03:26 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.54.254; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.54.254
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
Subject: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 16 May 2022 09:03:26 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
X-Received-Bytes: 2556
 by: hongy...@gmail.com - Mon, 16 May 2022 09:03 UTC

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

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

<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18292&group=comp.lang.python#18292

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 16 May 2022 10:27:33 -0500
From: wlfr...@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
Date: Mon, 16 May 2022 11:27:34 -0400
Organization: IISS Elusive Unicorn
Message-ID: <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 16
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-Tcm+kEfzt8QxMEMJnr7OrmpbAKm9T6zjSCXKbyvTjmupdSKTAKh8VMLBb4QH8aR70gMrrx1cD+5CeMu!v6cDGTPXMnqpBAFLz8pe/2IdCPHpxT98AI1jcnAymkC5weDB+YgmdKWoeQ4Tln3f58fsYg3d
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 1768
 by: Dennis Lee Bieber - Mon, 16 May 2022 15:27 UTC

On Mon, 16 May 2022 02:03:26 -0700 (PDT), "hongy...@gmail.com"
<hongyi.zhao@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
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

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

<fd240af9-4ab2-4e60-8d29-fc3014dd5814n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18294&group=comp.lang.python#18294

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:620a:2412:b0:6a0:5f8e:c050 with SMTP id d18-20020a05620a241200b006a05f8ec050mr14217870qkn.462.1652742674152;
Mon, 16 May 2022 16:11:14 -0700 (PDT)
X-Received: by 2002:ac8:5f8d:0:b0:2f3:e172:631d with SMTP id
j13-20020ac85f8d000000b002f3e172631dmr18014265qta.248.1652742673948; Mon, 16
May 2022 16:11:13 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 16 May 2022 16:11:13 -0700 (PDT)
In-Reply-To: <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.54.254; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.54.254
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com> <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <fd240af9-4ab2-4e60-8d29-fc3014dd5814n@googlegroups.com>
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 16 May 2022 23:11:14 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2347
 by: hongy...@gmail.com - Mon, 16 May 2022 23:11 UTC

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

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

<20220b6d-504f-4b55-ba15-609bafc71af3n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18295&group=comp.lang.python#18295

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:620a:16c2:b0:69f:ca37:f6b5 with SMTP id a2-20020a05620a16c200b0069fca37f6b5mr14975469qkn.48.1652743867600;
Mon, 16 May 2022 16:31:07 -0700 (PDT)
X-Received: by 2002:ac8:5f88:0:b0:2f3:b80c:a7a4 with SMTP id
j8-20020ac85f88000000b002f3b80ca7a4mr17770862qta.38.1652743867336; Mon, 16
May 2022 16:31:07 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 16 May 2022 16:31:07 -0700 (PDT)
In-Reply-To: <fd240af9-4ab2-4e60-8d29-fc3014dd5814n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.54.254; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.54.254
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com> <fd240af9-4ab2-4e60-8d29-fc3014dd5814n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <20220b6d-504f-4b55-ba15-609bafc71af3n@googlegroups.com>
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Mon, 16 May 2022 23:31:07 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2748
 by: hongy...@gmail.com - Mon, 16 May 2022 23:31 UTC

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

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

<d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18296&group=comp.lang.python#18296

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ad4:5b8c:0:b0:45a:9340:ef92 with SMTP id 12-20020ad45b8c000000b0045a9340ef92mr17585861qvp.85.1652746938109;
Mon, 16 May 2022 17:22:18 -0700 (PDT)
X-Received: by 2002:a05:6214:f26:b0:458:3341:3a81 with SMTP id
iw6-20020a0562140f2600b0045833413a81mr18024058qvb.112.1652746937945; Mon, 16
May 2022 17:22:17 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 16 May 2022 17:22:17 -0700 (PDT)
In-Reply-To: <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.54.254; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.54.254
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com> <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Tue, 17 May 2022 00:22:18 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2669
 by: hongy...@gmail.com - Tue, 17 May 2022 00:22 UTC

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

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

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

<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18297&group=comp.lang.python#18297

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Mon, 16 May 2022 19:48:03 -0500
From: wlfr...@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
Date: Mon, 16 May 2022 20:48:02 -0400
Organization: IISS Elusive Unicorn
Message-ID: <27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com> <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com> <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 59
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-IXSyMoCFUiXqLZemG5BCECrmvqRrq9KYINf7esrH/k5Ybmg3SEPUAsDqIlub3kiYrMC68bn3yqSYzZC!W1chA2cF/GCM2FVRn5JeL24D3QoDSDYNqYO5FW3XEH23prwvc3Nk/4R8qjS9fslusYHXEkXi
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 3063
 by: Dennis Lee Bieber - Tue, 17 May 2022 00:48 UTC

On Mon, 16 May 2022 17:22:17 -0700 (PDT), "hongy...@gmail.com"
<hongyi.zhao@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
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

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

<710536ee-95b0-41b7-acd6-bd2d5a5eed01n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18298&group=comp.lang.python#18298

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ad4:5a03:0:b0:456:5533:4ab1 with SMTP id ei3-20020ad45a03000000b0045655334ab1mr18114987qvb.24.1652753114810;
Mon, 16 May 2022 19:05:14 -0700 (PDT)
X-Received: by 2002:a05:6214:400e:b0:45a:ece1:1789 with SMTP id
kd14-20020a056214400e00b0045aece11789mr17658721qvb.131.1652753114543; Mon, 16
May 2022 19:05:14 -0700 (PDT)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Mon, 16 May 2022 19:05:14 -0700 (PDT)
In-Reply-To: <27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
Injection-Info: google-groups.googlegroups.com; posting-host=165.22.54.254; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 165.22.54.254
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com> <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <710536ee-95b0-41b7-acd6-bd2d5a5eed01n@googlegroups.com>
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Tue, 17 May 2022 02:05:14 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 4085
 by: hongy...@gmail.com - Tue, 17 May 2022 02:05 UTC

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

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

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

<mailman.429.1652769741.20749.python-list@python.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18299&group=comp.lang.python#18299

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: bar...@barrys-emacs.org (Barry)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
Date: Tue, 17 May 2022 07:42:11 +0100
Lines: 70
Message-ID: <mailman.429.1652769741.20749.python-list@python.org>
References: <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<9E85CF38-9A50-42DC-80B5-9EA08673AF3B@barrys-emacs.org>
Mime-Version: 1.0 (1.0)
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de 9mXV6PKNYSdNG3jog1IzAAMlBD1DyBjvNtDFG9w+FXaQ==
Return-Path: <barry@barrys-emacs.org>
X-Original-To: Python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; '2022': 0.05;
'explicitly': 0.07; 'loop': 0.07; 'subject:into': 0.07; 'cc:addr
:python-list': 0.09; 'else:': 0.09; 'from:addr:barry': 0.09;
'numpy': 0.09; 'output:': 0.09; 'received:217.70': 0.09;
'received:gandi.net': 0.09; 'received:mail.gandi.net': 0.09;
'subject:numbers': 0.09; 'cc:no real name:2**0': 0.14; 'import':
0.15; 'url:mailman': 0.15; "'/'": 0.16; '2022,': 0.16; 'barry':
0.16; 'email addr:gmail.com"': 0.16; 'from:addr:barrys-emacs.org':
0.16; 'message-id:@barrys-emacs.org': 0.16; 'structure.': 0.16;
'structures': 0.16; 'wrote:': 0.16; 'uses': 0.19; '16,': 0.19;
'cc:addr:python.org': 0.20; 'returns': 0.22; 'code': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'url:listinfo': 0.25; 'cc:2**0': 0.25; 'url-ip:188.166/16': 0.25;
'tried': 0.26; '>>>': 0.28; 'output': 0.28; 'url-ip:188/8': 0.31;
"doesn't": 0.32; 'structure': 0.32; 'but': 0.32; 'printing': 0.34;
'header:In-Reply-To:1': 0.34; 'item': 0.35; 'mon,': 0.36; 'could':
0.38; 'want': 0.40; 'higher': 0.60; 'method': 0.61; 'skip:b 30':
0.61; 'skip:h 10': 0.61; 'skip:i 20': 0.62; 'email
addr:gmail.com': 0.63; 'structure,': 0.64; 'your': 0.64;
'received:217': 0.67; 'following:': 0.69; 'within': 0.69;
'formatting': 0.76; 'fraction': 0.84
In-Reply-To: <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
X-Mailer: iPad Mail (19F77)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <9E85CF38-9A50-42DC-80B5-9EA08673AF3B@barrys-emacs.org>
X-Mailman-Original-References: <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
 by: Barry - Tue, 17 May 2022 06:42 UTC

> On 17 May 2022, at 05:59, hongy...@gmail.com <hongyi.zhao@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
>

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

<mailman.434.1652801447.20749.python-list@python.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18306&group=comp.lang.python#18306

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-pyt...@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into
a matrix representing elements in fractional form
Date: Tue, 17 May 2022 17:21:29 +0200
Lines: 54
Message-ID: <mailman.434.1652801447.20749.python-list@python.org>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
<d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
<20220517152129.3d2ns6e4yrdlnbrm@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="34ar4nfyvycw6ev4"
X-Trace: news.uni-berlin.de M6USCVt1uXuZbZv/P/TBLwttVrtJjeV0iyb0g7Ig1sOg==
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '2022': 0.05; 'content-
type:multipart/signed': 0.05; 'string': 0.07; 'subject:into':
0.07; 'content-type:application/pgp-signature': 0.09;
'expression': 0.09; 'filename:fname piece:asc': 0.09;
'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; 'subject:numbers': 0.09;
'"creative': 0.16; '(especially': 0.16; '__/': 0.16;
'challenge!"': 0.16; 'email addr:gmail.com"': 0.16; 'from:addr
:hjp-python': 0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j.
holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'nothing.':
0.16; 'reality.': 0.16; 'skip:> 20': 0.16; 'stross,': 0.16; 'url-
ip:212.17.106.137/32': 0.16; 'url-ip:212.17.106/24': 0.16; 'url-
ip:212.17/16': 0.16; 'url:hjp': 0.16; '|_|_)': 0.16; 'wrote:':
0.16; 'to:addr:python-list': 0.20; 'input': 0.21; 'library': 0.26;
'sense': 0.28; 'skip:" 20': 0.34; 'header:In-Reply-To:1': 0.34;
'mon,': 0.36; 'single': 0.39; 'neither': 0.39; 'explain': 0.40;
'something': 0.40; 'best': 0.61; 'search': 0.61; 'skip:o 10':
0.61; 'received:212': 0.62; 'received:userid': 0.66; 'following:':
0.69; 'url-ip:212/8': 0.69; 'within': 0.69; 'terms': 0.70;
'quote': 0.74; 'characters': 0.84; 'received:at': 0.84; 'subject:
\n ': 0.84; 'replacing': 0.91
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <20220517152129.3d2ns6e4yrdlnbrm@hjp.at>
X-Mailman-Original-References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
<d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
 by: Peter J. Holzer - Tue, 17 May 2022 15:21 UTC
Attachments: signature.asc (application/pgp-signature)

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"
> <hongyi.zhao@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.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)
Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form

<mailman.435.1652804644.20749.python-list@python.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18307&group=comp.lang.python#18307

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
Date: Tue, 17 May 2022 17:20:54 +0100
Lines: 23
Message-ID: <mailman.435.1652804644.20749.python-list@python.org>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
<d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
<20220517152129.3d2ns6e4yrdlnbrm@hjp.at>
<e043e637-499f-7da5-e8f6-5107fbacf73a@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: news.uni-berlin.de 6KiG04Gd8MOpm/xjpO03GQ+v+PXQ7wTw7FP1lr5uaCpQ==
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=laVKIMGf;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.015
X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; '2022': 0.05; 'string':
0.07; 'subject:into': 0.07; 'expression': 0.09;
'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
'subject:numbers': 0.09; '(especially': 0.16; 'email
addr:gmail.com"': 0.16; 'from:addr:mrabarnett.plus.com': 0.16;
'from:name:mrab': 0.16; 'holzer': 0.16; 'message-
id:@mrabarnett.plus.com': 0.16; 'nothing.': 0.16;
'received:plus.net': 0.16; 'skip:> 20': 0.16; 'wrote:': 0.16;
'to:addr:python-list': 0.20; 'input': 0.21; 'library': 0.26;
'header:User-Agent:1': 0.30; 'received:192.168.1': 0.32; 'skip:"
20': 0.34; 'header:In-Reply-To:1': 0.34; 'mon,': 0.36; "it's":
0.37; 'received:192.168': 0.37; 'single': 0.39; 'neither': 0.39;
'explain': 0.40; 'something': 0.40; 'best': 0.61; 'search': 0.61;
'skip:o 10': 0.61; 'received:212': 0.62; 'following:': 0.69;
'within': 0.69; 'terms': 0.70; 'quote': 0.74; '"")': 0.84;
'characters': 0.84; 'subject: \n ': 0.84; 'replacing': 0.91
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1652804457; bh=GgkuCF08vvhItBzcq0Fd3mKdAPBu7QdOYW9cpDy/7SM=;
h=Date:Subject:To:References:From:In-Reply-To;
b=laVKIMGflV61byK8UvMIiCt1T/i2ytogApttqOvfGSJyO8HA506ZmyoHEcG6PWJtc
sn53bpaUCc1dbLpI0WEcY/CZQ03hITbaU8QuiKz6kUBve/uoRdvRjHb1QAriKOXwb5
IS0r/ussgwcGcoZYl2XhIFVCdQ+KPMdhR0Na+seA342hWYfbQhN6DlTppPrQ4AlE5e
3fQQ6AuwYnJ5B1cmbc1dyrlYxfdHz0wYWMv0AnCuAmTpb41uaxLfEtVTA5Qt3MWCCH
3v4eTgnCHOXzVhS4tEXZgosQG2vgK8+9uE+5tv86U3fYEWkTdAgDtuUo3stYRyczHc
P4qKWdh4nKG+g==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=FsUWQknq c=1 sm=1 tr=0 ts=6283cb69
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=hC5HBo6TjhWcO0eH:21 a=IkcTkHD0fZMA:10 a=pGLkceISAAAA:8
a=HtRiFKk7PDHvTgkQ9QkA:9 a=QEXdDO2ut3YA:10 a=buZdK5Zwu4izdfuwiaTv:22
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.0
Content-Language: en-GB
In-Reply-To: <20220517152129.3d2ns6e4yrdlnbrm@hjp.at>
X-CMAE-Envelope: MS4xfB79RCdChYi+ZfoEkOnl3QqVHNoT9gp2C5a5uwSIaMbJ6P7DR/hQ9nGie8CBdPbp2fyje2sLu/2hJ+u1F5J9aZ0mq1Q46UdbQTvXTu9wOZwMNltyMCgN
mERwqyShSylY9FeQ/aaUUe68LCNqhLd2npVtMVDcagJghbL7gYpiBE3vFtmUMyKEPBDmhxSMu06/vnjNH5jdUHDLcbsobas9myE=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <e043e637-499f-7da5-e8f6-5107fbacf73a@mrabarnett.plus.com>
X-Mailman-Original-References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com>
<d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com>
<27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com>
<20220517152129.3d2ns6e4yrdlnbrm@hjp.at>
 by: MRAB - Tue, 17 May 2022 16:20 UTC

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"
>> <hongyi.zhao@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("'", "")

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

<fjh88hhi9h1o3ugb3sf7auod4q0hehlepj@4ax.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18312&group=comp.lang.python#18312

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Tue, 17 May 2022 20:11:05 -0500
From: wlfr...@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a matrix representing elements in fractional form
Date: Tue, 17 May 2022 21:11:02 -0400
Organization: IISS Elusive Unicorn
Message-ID: <fjh88hhi9h1o3ugb3sf7auod4q0hehlepj@4ax.com>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com> <54r48hdpj8jov1on1f9lfi4t6vl08lk6md@4ax.com> <d82fbb30-28d7-459c-a1b6-ada167878a22n@googlegroups.com> <27r58hhbe2ldc4f0h9tmci3rallg6cc3va@4ax.com> <20220517152129.3d2ns6e4yrdlnbrm@hjp.at> <e043e637-499f-7da5-e8f6-5107fbacf73a@mrabarnett.plus.com> <mailman.435.1652804644.20749.python-list@python.org>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Lines: 22
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-pzJsiOK0wqXf9Kkymt4xoKnw3cKfSbzP48D6HMC+3SkpzEMBMkz4P6tvTd6+Xogezoii3cCAvqfUJNz!HdXN/ikC0Wd/1eqj3WXy/gtnFtNWgLKWQtN4vBPSuHotaWqW4nXsu6yP+QNSuJkbqkxGCNT+
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 2393
 by: Dennis Lee Bieber - Wed, 18 May 2022 01:11 UTC

On Tue, 17 May 2022 17:20:54 +0100, MRAB <python@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.

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

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

<t63o9c$mco$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18321&group=comp.lang.python#18321

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: cousinst...@gmail.com (Cousin Stanley)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
Date: Wed, 18 May 2022 14:26:02 -0700
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <t63o9c$mco$1@dont-email.me>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 18 May 2022 21:26:04 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="51d81de9d75c8af5c67ea16b497597ff";
logging-data="22936"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qp+dLFEmNal1a6PMMu5Lx1X/gzp6VA1g="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.8.0
Cancel-Lock: sha1:zG5cZXP6BvvEexhkGilNctxJOh0=
In-Reply-To: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
Content-Language: en-US
 by: Cousin Stanley - Wed, 18 May 2022 21:26 UTC

#!/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

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

<a7667c8b-5c33-4790-a0a7-d7c4f9f10118n@googlegroups.com>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18350&group=comp.lang.python#18350

 copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ad4:5962:0:b0:462:11fe:b67c with SMTP id eq2-20020ad45962000000b0046211feb67cmr7249630qvb.50.1653179879067;
Sat, 21 May 2022 17:37:59 -0700 (PDT)
X-Received: by 2002:a05:622a:100a:b0:2f3:c5db:70ef with SMTP id
d10-20020a05622a100a00b002f3c5db70efmr12548955qte.334.1653179878912; Sat, 21
May 2022 17:37:58 -0700 (PDT)
Path: i2pn2.org!i2pn.org!aioe.org!news.uzoreto.com!peer03.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Sat, 21 May 2022 17:37:58 -0700 (PDT)
In-Reply-To: <t63o9c$mco$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=203.175.13.156; posting-account=kF0ZaAoAAACPbiK5gldhAyX5qTd3krV2
NNTP-Posting-Host: 203.175.13.156
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com> <t63o9c$mco$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <a7667c8b-5c33-4790-a0a7-d7c4f9f10118n@googlegroups.com>
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
From: hongyi.z...@gmail.com (hongy...@gmail.com)
Injection-Date: Sun, 22 May 2022 00:37:59 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2619
 by: hongy...@gmail.com - Sun, 22 May 2022 00:37 UTC

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

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

<t6dirl$nq9$1@gioia.aioe.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18351&group=comp.lang.python#18351

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!aioe.org!hd26Lan9ki8ATcS0jNuNWQ.user.46.165.242.91.POSTED!not-for-mail
From: nos...@please.ty (jak)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
Date: Sun, 22 May 2022 16:54:45 +0200
Organization: Aioe.org NNTP Server
Message-ID: <t6dirl$nq9$1@gioia.aioe.org>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<t63o9c$mco$1@dont-email.me>
<a7667c8b-5c33-4790-a0a7-d7c4f9f10118n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="24393"; posting-host="hd26Lan9ki8ATcS0jNuNWQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.9.0
X-Notice: Filtered by postfilter v. 0.9.2
 by: jak - Sun, 22 May 2022 14:54 UTC

Il 22/05/2022 02:37, hongy...@gmail.com ha scritto:
> 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

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)

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

<t6gmpt$opi$1@dont-email.me>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18359&group=comp.lang.python#18359

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: cousinst...@gmail.com (Cousin Stanley)
Newsgroups: comp.lang.python
Subject: Re: Convert the decimal numbers expressed in a `numpy.ndarray` into a
matrix representing elements in fractional form
Date: Mon, 23 May 2022 12:20:29 -0700
Organization: A noiseless patient Spider
Lines: 49
Message-ID: <t6gmpt$opi$1@dont-email.me>
References: <5eb0b494-ec7a-4b6c-9966-078889dbc5aen@googlegroups.com>
<t63o9c$mco$1@dont-email.me>
<a7667c8b-5c33-4790-a0a7-d7c4f9f10118n@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 23 May 2022 19:20:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e965eb6e75bf3256ff55e3d31e33804d";
logging-data="25394"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+KQnJqsZfkpYcurq4BInfe2OAQgBrHuM4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101
Thunderbird/91.9.0
Cancel-Lock: sha1:LTaJZq6Bp/47qhlIldu2gI1vIxM=
In-Reply-To: <a7667c8b-5c33-4790-a0a7-d7c4f9f10118n@googlegroups.com>
Content-Language: en-US
 by: Cousin Stanley - Mon, 23 May 2022 19:20 UTC

> 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

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor