Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You have a strong appeal for members of your own sex.


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

SubjectAuthor
o Re: Relatively prime integers in NumPyOscar Benjamin

1
Subject: Re: Relatively prime integers in NumPy
From: Oscar Benjamin
Newsgroups: comp.lang.python
Date: Thu, 11 Jul 2024 21:22 UTC
References: 1 2
Path: eternal-september.org!news.eternal-september.org!feeder3.eternal-september.org!fu-berlin.de!uni-berlin.de!not-for-mail
From: oscar.j.benjamin@gmail.com (Oscar Benjamin)
Newsgroups: comp.lang.python
Subject: Re: Relatively prime integers in NumPy
Date: Thu, 11 Jul 2024 22:22:30 +0100
Lines: 34
Message-ID: <mailman.30.1720732964.2981.python-list@python.org>
References: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.com>
<CAHVvXxR31wYkF2dinUNaE+zHvXn1eDdSXJ9u82gUuvNzpb20ug@mail.gmail.com>
Mime-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
X-Trace: news.uni-berlin.de n4DXYmP8PfEcfKR2ktf6Fwge2ohDv1O4OmerTxRqM/hA==
Cancel-Lock: sha1:g8CdSV/H5gPqQlZVFH5/w6C3D2A= sha256:3NfYvams9dKvQsxJZ3wgIH4/hW4AINxqpS7zGZougGQ=
Return-Path: <oscar.j.benjamin@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=jrPSkf1i;
dkim-adsp=pass; dkim-atps=neutral
X-Spam-Status: OK 0.003
X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'this:': 0.03; 'jul': 0.04;
'math': 0.05; 'received:mail-lj1-x230.google.com': 0.05; 'row':
0.05; 'mechanism': 0.09; 'numpy': 0.09; 'import': 0.15; '2024':
0.16; '[1]:': 0.16; '[2]:': 0.16; 'prime': 0.16; 'relatively':
0.16; 'sounds': 0.16; 'time)': 0.16; 'wrote:': 0.16; 'thu,': 0.19;
'to:addr:python-list': 0.20; 'i.e.': 0.22; 'url:wiki': 0.23;
'function': 0.27; 'example,': 0.28; 'module': 0.31; 'python-list':
0.32; 'message-id:@mail.gmail.com': 0.32; 'header:In-Reply-To:1':
0.34; 'received:google.com': 0.34; 'from:addr:gmail.com': 0.35;
'those': 0.36; 'want': 0.40; 'provide': 0.60; 'dear': 0.62;
'identify': 0.64; 'imagine': 0.64; 'numbers': 0.67; 'skip:n 30':
0.67; 'factor': 0.69; 'url:wikipedia': 0.70; 'factors': 0.76;
'axis': 0.84; 'oscar': 0.84
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20230601; t=1720732962; x=1721337762; darn=python.org;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:from:to:cc:subject:date:message-id:reply-to;
bh=wQkbE3MmL/UUVt2aMAo0Z6upCfaBRFlStpBA+OqZ+8g=;
b=jrPSkf1iqh02rJdiWedszdssP8EvNnKi9f3Hr2Ctxb+hDmRjNS2m4xmBcq5i7aZQG6
dLc7THyhsGcyqmsRRhBvP7DjnoGkIYAb9KAStXNJWdsQ2NRJc2WO4Vs/CARFT4yNrFvJ
7o0kTugR0FAzqFPKWX9MiiSnO7bgTamY9Jsntly8d9tYK0LHVM2kZKB9Zog0ed0eG1nC
eQHvtIkrDFX4wyjQeKBu+aL9IrIZLXPKtfok47Khc4tc0yybNZhCovReLPAhk8eSEZMX
/uu+Lfy03ERndxXVD2FwtbJ2YuiOjNRzq35LS+ZjdRyq5Qy5cmSKFma7iOjtskFXbYrR
T6Fg==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=1e100.net; s=20230601; t=1720732962; x=1721337762;
h=to:subject:message-id:date:from:in-reply-to:references:mime-version
:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to;
bh=wQkbE3MmL/UUVt2aMAo0Z6upCfaBRFlStpBA+OqZ+8g=;
b=g65GP17tPUPBdnyjmN4zOAx91Dn9qKpoc7/6RzsDbnBR83qYvvg6yY2/QWqWO9giCf
LSzhvBGe0IifUcMHnFYK+6ISMu8+PmOe2v7oT/5IeGmImGjkYedJM7M0sdccbKFXHlbc
uTjqabqsvmIoXak5gYDs9rNTAhaeFEdvfitRk9Xg3DWPhR3W5yUu+rjSDJim9gO8ddzP
7XLpI077IUWcya5qns9j62FNSg4DUkf6XahSlAHH+uOJN6T5/7D5/RdeYDhxcJNmbOFp
I1PPI5yVMwCDZxr28b/WR/mssNukOAWVhn19djJ7hQwjQqDIMPVL1y7XkjtHY+8IZlSB
9qAA==
X-Gm-Message-State: AOJu0YyIgk+iqjMI+GcEbwNk3tG0Cdr+zLhn9w7ioczgmeAUwhFAj9qM
DIZqyw1PwNpSNdlG6NURtHfhprPfZ/0tg0ApZQAmAXuPRhRxxBK+G6SWhue5l61Dkd5oPHIS4wQ
eCzzLyXVBu3eXa/ldj/GjomUjlPbrSGwU
X-Google-Smtp-Source: AGHT+IECS3u3CEjHuMHOV5FaDFJkB0DbNiHB4/MD866t/c4z0dOBJmrySmFn/+ewpsi5pBpzQUT2NNe0bBkiL6GbLAM=
X-Received: by 2002:a2e:8750:0:b0:2ee:8ce9:5ad2 with SMTP id
38308e7fff4ca-2eeb3102696mr60603591fa.23.1720732961534; Thu, 11 Jul 2024
14:22:41 -0700 (PDT)
In-Reply-To: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.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: <CAHVvXxR31wYkF2dinUNaE+zHvXn1eDdSXJ9u82gUuvNzpb20ug@mail.gmail.com>
X-Mailman-Original-References: <SA0PR09MB6363F3E6B493202E73869DF4DBDA2@SA0PR09MB6363.namprd09.prod.outlook.com>
View all headers

(posting on-list this time)

On Thu, 11 Jul 2024 at 15:18, Popov, Dmitry Yu via Python-list
<python-list@python.org> wrote:
>
> 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.

It sounds like you want the gcd (greatest common divisor) of each row.
The math module can do this:

In [1]: a = [[1,5,8],
...: [2,4,8],
...: [3,3,9]]

In [2]: import math

In [3]: [math.gcd(*row) for row in a]
Out[3]: [1, 2, 3]

NumPy can also do it apparently:

In [10]: np.gcd.reduce(np.transpose(a))
Out[10]: array([1, 2, 3])

https://en.wikipedia.org/wiki/Greatest_common_divisor

--
Oscar

1

rocksolid light 0.9.8
clearnet tor