Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You are standing on my toes.


comp / comp.lang.python / RE: Relatively prime integers in NumPy

SubjectAuthor
* RE: Relatively prime integers in NumPy<avi.e.gross
`- Re: Relatively prime integers in NumPyPaul Rubin

1
Subject: RE: Relatively prime integers in NumPy
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Date: Thu, 11 Jul 2024 23:26 UTC
References: 1 2 3 4
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: <avi.e.gross@gmail.com>
Newsgroups: comp.lang.python
Subject: RE: Relatively prime integers in NumPy
Date: Thu, 11 Jul 2024 19:26:30 -0400
Lines: 180
Message-ID: <mailman.31.1720740394.2981.python-list@python.org>
References: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.com>
<00e801dad3bf$473daed0$d5b90c70$@gmail.com>
<DM8PR09MB63603191F5509E5013D1BEDCDBA52@DM8PR09MB6360.namprd09.prod.outlook.com>
<014701dad3e9$c2eddc60$48c99520$@gmail.com>
Mime-Version: 1.0
Content-Type: text/plain;
charset="utf-8"
Content-Transfer-Encoding: base64
X-Trace: news.uni-berlin.de dNQxVUnj5T65aBWwHH2CzQl/w08goBiUHKsW6ncQe58Q==
Cancel-Lock: sha1:6bTnsE7wfSLxWikZZwtXjZhyoK8= sha256:m/xFPPZ8nvj5kUaRuBxmwt/Cd47eVZXoHza1bYWRBAA=
Return-Path: <avi.e.gross@gmail.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=gmail.com header.i=@gmail.com header.b=UWetey20;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.000
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'looks': 0.02; 'fairly':
0.05; 'architecture': 0.07; 'explanation': 0.07; 'loop': 0.07;
'matches': 0.07; 'modules': 0.07; 'attempts': 0.09; 'dataframe':
0.09; 'matrix': 0.09; 'mechanism': 0.09; 'numpy': 0.09; 'routine':
0.09; 'skip:z 20': 0.09; 'something,': 0.09; 'url-
ip:13.107.246.67/32': 0.09; 'url-ip:13.107.246/24': 0.09; '&gt;':
0.14; 'url:mailman': 0.15; '2024': 0.16; '3:10': 0.16;
'algorithms': 0.16; 'calculations': 0.16; 'computation': 0.16;
'dictionary,': 0.16; 'division': 0.16; 'divisors': 0.16;
'efficiently.': 0.16; 'evaluating': 0.16; 'examples.': 0.16;
'explicit': 0.16; 'far,': 0.16; 'fourth': 0.16; 'integer': 0.16;
'loops': 0.16; 'loops,': 0.16; 'mathematical': 0.16; 'nested':
0.16; 'numpy.': 0.16; 'numpy?': 0.16; 'ok.': 0.16; 'operations,':
0.16; 'patterns.': 0.16; 'prime': 0.16; 'primes': 0.16;
'procedure': 0.16; 'prompts': 0.16; 'received:mail-
qk1-x734.google.com': 0.16; 'refactor': 0.16; 'relatively': 0.16;
'see\xc2\xa0': 0.16; 'skip:0 210': 0.16; 'so)': 0.16; 'sorry.':
0.16; 'url-ip:3.215/16': 0.16; 'url:urldefense': 0.16; 'url:v3':
0.16; 'using.': 0.16; 'vectorized': 0.16; '\xc2\xa0this': 0.16;
'problem': 0.16; 'python': 0.16; 'larger': 0.17; 'values': 0.17;
'probably': 0.17; 'message-id:@gmail.com': 0.18; 'solve': 0.19;
'uses': 0.19; 'calls': 0.19; 'implement': 0.19; 'to:addr:python-
list': 0.20; 'language': 0.21; 'written': 0.22; 'creates': 0.22;
'i.e.': 0.22; 'maybe': 0.22; 'ran': 0.22; 'returns': 0.22; 'code':
0.23; 'lines': 0.23; 'idea': 0.24; '(and': 0.25; 'skip:- 10':
0.25; 'url:listinfo': 0.25; 'cannot': 0.25; '11,': 0.26; 'object':
0.26; 'recording': 0.26; 'suspect': 0.26; 'else': 0.27; 'bit':
0.27; 'function': 0.27; 'done': 0.28; 'expect': 0.28; 'mostly':
0.28; 'purpose': 0.28; 'thinking': 0.28; 'email
addr:python.org&gt;': 0.28; 'example,': 0.28; 'goes': 0.28;
'suggest': 0.28; 'it,': 0.29; 'code,': 0.31; 'takes': 0.31;
'comment': 0.31; 'looked': 0.31; 'module': 0.31; 'saved': 0.31;
'enclosed': 0.69; 'end,': 0.69; 'factor': 0.69; 'latter': 0.69;
'result,': 0.69; 'sequence': 0.69; 'skip:\xe2 20': 0.69; 'times':
0.69; 'url:us': 0.69; '8bit%:38': 0.70; 'conditions': 0.70;
'depending': 0.70; 'instead,': 0.70; 'speed': 0.71; '8bit%:89':
0.75; '8bit%:92': 0.75; '8bit%:94': 0.75; '8bit%:78': 0.76;
'factors': 0.76; 'need,': 0.76; 'sent:': 0.78; 'highly': 0.78;
'0in': 0.81; 'more.': 0.82; '8bit%:95': 0.84; '8bit%:76': 0.84;
'8bit%:97': 0.84; 'axis': 0.84; 'divisions': 0.84; 'email name:&lt
;python-list': 0.84; 'integral': 0.84; 'lot.': 0.84; 'luck': 0.84;
'parts.': 0.84; 'popov': 0.84; 'skip:1 70': 0.84; 'skip:z 30':
0.84; 'want.': 0.84; '8bit%:98': 0.91; 'skip:\xd0 10': 0.91;
'two.': 0.91; 'interest.': 0.93
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1720740391; x=1721345191; darn=python.org;
h=content-language:thread-index:mime-version:message-id:date:subject
:in-reply-to:references:to:from:from:to:cc:subject:date:message-id
:reply-to; bh=SA70ewc/UHfnMPBG0WCTbIsPhMw+ygJe7hFlvg+I3DI=;
b=UWetey20SdjNz39xrZd/gE3gMmhTlwd35X6QSZLznqqgMPhDWjP9XkkKwEVEzSqnnx
8PkbpgRFXmQX5K5nQhqamjMkx0nyDD+SAW72bggReCkkKoKtJYujKCTM4M93FPIgQmwG
3jpSRId6weboW+0cEkF5h+9eUoiEys20KT0AxRmIZ6dyMNFCpEjEK98gFHGSCeU9D6w+
zPaLU3MmTpNEIPUXZRCC7fuGRSE6NqFtyYJVi5lerqO3xetPFVkc0OoC6Zq84aovdDVu
iXUbFups06fPf8BsBype/UITyM99DORIF/CFIrLdG2vN4MKiYblA4qQ/XU45IDmTiigk
olgg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1720740391; x=1721345191;
h=content-language:thread-index:mime-version:message-id:date:subject
:in-reply-to:references:to:from:x-gm-message-state:from:to:cc
:subject:date:message-id:reply-to;
bh=SA70ewc/UHfnMPBG0WCTbIsPhMw+ygJe7hFlvg+I3DI=;
b=gbouIAH2z/S9b5Y8gHorcawlE0MNMApX3tyhKOEMEZmnV4fXvgK5cWOCV4FAoyVovn
OaToY8j2v1jBUzAkfe2pKehPRHVCgE7iM66ssVyjOcLccutm58OYWuwkqK4bX1M0uUJf
A1ZE4/L9J01qFEx1AMZwcYUpgQqBm0NIsUN+ccSy+Ddl8SF8xeMWeha9jqfozt51qKUF
Xg9HAsMUmcyBTvfEDADTLXrh07eh4XVLf0GAvm4YEeguOm8Rx2L8UuNbGQSYGORGvqFX
mgpu4lViRwV4F4m2UYtlEfYl8TjwshPZgDqyCnulbRs7lyK1B4g3GALJw45NeavoGodx
M+5Q==
X-Forwarded-Encrypted: i=1;
AJvYcCVG8S7ql7QZKumch33XO0AFT/YOxB48HpnWnD3d1nQjuUIb3RDkdvSsi3+JxP0qnns6Bqvjfvz3KlSGKUDzsoU2pSqtkYh2
X-Gm-Message-State: AOJu0YzzMWjdVsFKZvwv47n3ntRkBPphX7Py4TIosHXoOWRJ7wcDOn43
X5+72jhwcuWwRzTh20HptsVH+qC1lu33c57+HLkPpVFQXwRLmIq+tPVEzCjf
X-Google-Smtp-Source: AGHT+IErn4weqHsV1fuIE09DovoxKM0v9OUJJ0/9iCJMVTcCgAp7HjjKei/qXXJbpMy3ETnhWY7wNA==
X-Received: by 2002:a05:622a:1647:b0:447:c7e4:6b42 with SMTP id
d75a77b69052e-447fa8eafa5mr117767051cf.22.1720740391097;
Thu, 11 Jul 2024 16:26:31 -0700 (PDT)
In-Reply-To: <DM8PR09MB63603191F5509E5013D1BEDCDBA52@DM8PR09MB6360.namprd09.prod.outlook.com>
X-Mailer: Microsoft Outlook 16.0
Thread-Index: AQHaqdYQj6Isi+wDzs9EemRqPkPTEQJmM6ydAUtEEoqx1OhGIA==
Content-Language: en-us
X-Content-Filtered-By: Mailman/MimeDel 2.1.39
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: <014701dad3e9$c2eddc60$48c99520$@gmail.com>
X-Mailman-Original-References: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.com>
<00e801dad3bf$473daed0$d5b90c70$@gmail.com>
<DM8PR09MB63603191F5509E5013D1BEDCDBA52@DM8PR09MB6360.namprd09.prod.outlook.com>
View all headers

OK. That explains a bit more.

If I understand what you are looking for is a fast implementation and quite often in Pyhon it means using code written in another language such as C that is integrated carefully in a moule or two. Another tack is to replace many explicit loops with often much faster vectorized operations. Numpy provides advantages like the above if you use it as intended.

Of course there are other techniques in how code is refactored or the order of operations, or doing things in parallel.

Just as an example, your inner loop ear the top is operating one at a time or numbers between 0 and max_l and hen creates variables initialized and then possibly changed in chvec and maxmult. It uses various conditions to change those variables then goes on to do more things included in a fourth nested loop.

What would happen if, instead, you used two objects with the same names that were each a numpy array, or perhaps combined into a dataframe type object?

Using numpy (and perhaps pandas) you could have code that initialized one such array to hold the initial 1 or 2 as needed in an object whose length was max_l+1 and then the next operations, using numpy notation would be along the lines of replace the corresponding value depending on external variables you call h or k and so on.

There would be several invisible loops, perhaps chained in some way, but probably running way faster than the explicit loop.

I am not going to write any specific code, but suggest you read some documentation on how to use numpy for some of the operations you want when operating on larger clusters of info. You can gain some speed even by changing a few parts. To refactor the entire thing would take more thought and if you come up with the idea of operating on a multidimensional array, might take some care.

But consider what would happen if you looked at your loops which are currently of a fixed size and created a 3-D matrix with dimensions of max_h+1, max_k+1, and max_l+1 and simply initialized it with all possible initial values and then ran an algorithm to manipulate it, often asking numpy for various slices or whatever works for you as in axes. This architecture may not work for ou but is an example of the kind of thinking it an take to make a problem use algorithms more efficiently.

I note the code did not actually help me understand what mathematical operation you want to perform. I assumed I might see some operations like division and t may be other parts of your code that implement what you want.

But if this is a common enough need, I suspect you may want to see if something similar enough is out there. Your code may be more complex and more like the sieve of Eratosthenes that attempts to test every possibility.

One algorithm I have seen simply takes the numbers you are evaluating and in a loop of the first N primes (or an open-ended generator) simply does an integer division by 2, as many times as it returns an integral result, then as many divisions by 3 then 5 and 7 and so on. It aborts when it has been chopped down to size, or the prime being used is large enough (square root or so) ad at the end, you should have some sequence of divisors, or just and the number if it is prime. Some such algorithm can be fairly fast and perhaps can even be done vectorized.

One last comment is about memoization. If your data is of a nature where a relatively few numbers come up often, then you an use something, like perhaps a dictionary, to store the results of a computation like getting a list of prime factors for a specific number, or just recording whether it is prime or composite. Later calls to do calculations would always check if the result has already been saved and skip recalculating it.

Good Luck


From: Popov, Dmitry Yu <dpopov@anl.gov>
Sent: Thursday, July 11, 2024 3:26 PM
To: avi.e.gross@gmail.com; 'Popov, Dmitry Yu via Python-list' <python-list@python.org>
Subject: Re: Relatively prime integers in NumPy

Thank you for your interest. My explanation is too concise indeed, sorry. So far, I have used Python code with three enclosed 'for' loops for this purpose which is pretty time consuming. I'm trying to develop a NumPy based code to make this procedure faster. This routine is kind of 'heart' of the algorithm to index of X-ray Laue diffraction patterns. In our group we have to process huge amount of such patterns. They are collected at a synchrotron radiation facility. Faster indexation routine would help a lot.

This is the code I'm currently using. Any prompts how to implement it in NumPy would be highly appreciated.

for h in range(0, max_h):
      for k in range(0, max_k):
            for l in range(0, max_l):
                  chvec=1
                  maxmult=2
                  if h > 1:                     
                        maxmult=h
                  if k > 1:
                        maxmult=k
                  if l > 1:
                        maxmult=l
                  if h > 1:
                        if maxmult > h:
                              maxmult=h
                  if k > 1:
                        if maxmult > k:
                              maxmult=k
                  if l > 1:
                        if maxmult > l:
                              maxmult=l
                  maxmult=maxmult+1
                  for innen in range(2, maxmult):
                        if h in range(0, (max_h+1), innen):
                              if k in range(0, (max_k+1), innen):
                                    if l in range(0, (max_l+1), innen):
                                          chvec=0
                  if chvec==1:
                        # Only relatively prime integers h,k,l pass to this block of the code


_____
From: avi.e.gross@gmail.com <mailto:avi.e.gross@gmail.com> <avi.e.gross@gmail.com <mailto:avi.e.gross@gmail.com> >
Sent: Thursday, July 11, 2024 1:22 PM
To: Popov, Dmitry Yu <dpopov@anl.gov <mailto:dpopov@anl.gov> >; 'Popov, Dmitry Yu via Python-list' <python-list@python.org <mailto:python-list@python.org> >
Subject: RE: Relatively prime integers in NumPy

Дмитрий, You may think you explained what you wanted but I do not see what result you expect from your examples. Your request is a bit too esoteric to be a great candidate for being built into a module like numpy for general purpose se but
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization.

ZjQcmQRYFpfptBannerEnd
Дмитрий,

You may think you explained what you wanted but I do not see what result you
expect from your examples.

Your request is a bit too esoteric to be a great candidate for being built
into a module like numpy for general purpose se but I can imagine it could
be available in modules build on top of numpy.

Is there a reason you cannot solve this mostly outside numpy?

It looks like you could use numpy to select the numbers you want to compare,
then call one of many methods you can easily search for to see how to use
python to make some list or other data structure for divisors of each number
involved and then use standard methods to compare the lists and exact common
divisors. If needed, you could then put the results back into your original
data structure using numpy albeit the number of matches can vary.

Maybe a better explanation is needed as I cannot see what your latter words
about -1 and 1 are about. Perhaps someone else knows.




-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org <mailto:python-list-bounces+avi.e.gross=gmail.com@python.org> > On
Behalf Of Popov, Dmitry Yu via Python-list
Sent: Monday, July 8, 2024 3:10 PM
To: Popov, Dmitry Yu via Python-list <python-list@python.org <mailto:python-list@python.org> >
Subject: Relatively prime integers in NumPy

Dear Sirs.

Does NumPy provide a simple mechanism to identify relatively prime integers,
i.e. integers which don't have a common factor other than +1 or -1? For
example, in case of this array:
[[1,5,8],
[2,4,8],
[3,3,9]]
I can imagine a function which would return array of common factors along
axis 0: [1,2,3]. Those triples of numbers along axis 1 with the factor of1
or -1 would be relatively prime integers.

Regards,
Dmitry Popov

Argonne, IL
USA

--
https://urldefense.us/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!G_uCfscf7eWS!ZGK1ZXYgmC6cpNa1xTXVTNklhunjYiinwaDe_xE3sJyVs4ZcVgUB_v2FKvDzDspx7IzFCZI7JpFsiV5iH58P$ <https://urldefense.us/v3/__https:/mail.python.org/mailman/listinfo/python-list__;!!G_uCfscf7eWS!ZGK1ZXYgmC6cpNa1xTXVTNklhunjYiinwaDe_xE3sJyVs4ZcVgUB_v2FKvDzDspx7IzFCZI7JpFsiV5iH58P$>


Click here to read the complete article
Subject: Re: Relatively prime integers in NumPy
From: Paul Rubin
Newsgroups: comp.lang.python
Organization: A noiseless patient Spider
Date: Fri, 12 Jul 2024 01:51 UTC
References: 1 2 3 4 5
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.python
Subject: Re: Relatively prime integers in NumPy
Date: Thu, 11 Jul 2024 18:51:57 -0700
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <87sewfml6q.fsf@nightsong.com>
References: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.com>
<00e801dad3bf$473daed0$d5b90c70$@gmail.com>
<DM8PR09MB63603191F5509E5013D1BEDCDBA52@DM8PR09MB6360.namprd09.prod.outlook.com>
<014701dad3e9$c2eddc60$48c99520$@gmail.com>
<mailman.31.1720740394.2981.python-list@python.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Fri, 12 Jul 2024 03:51:57 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b8cd35497cb037778df3a7faa6e18225";
logging-data="2863171"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//d0zFU4G2ERmzzXDYisB9"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:mmpobCo85Mge/tqqWV3jwU2z+Ao=
sha1:zww78lZUWrAnOIppp0qEmmKJT/M=
View all headers

<avi.e.gross@gmail.com> writes:
> make some list or other data structure for divisors of each number
> involved and then use standard methods to compare the lists and exact
> common divisors.

You don't need to find divisors (factor the numbers) to find the gcd.
Factorization can be very slow. Gcd on the other hand is very fast,
using the Euclidean GCD algorithm. You should be able to find info
about this easily. Factorization would be a terrible way to compute
gcd's.

1

rocksolid light 0.9.8
clearnet tor