Skip to content

ENH: Integer gcd function #8772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
brsr opened this issue Mar 11, 2017 · 1 comment
Closed

ENH: Integer gcd function #8772

brsr opened this issue Mar 11, 2017 · 1 comment

Comments

@brsr
Copy link
Contributor

brsr commented Mar 11, 2017

Essentially, math.gcd, but operating on numpy arrays with dtype= something in the int family.

>>> a = np.arange(4)
>>> b = np.arange(10,14)
>>> np.gcd(a, b)
array([10, 1, 2, 1])

Would support broadcasting:

>>> c = 2
>>> np.gcd(b, c)
array([2, 1, 2, 1])

Might want to have a version that operates on multiple values (more than 2) on an axis of a multivariable array:

>>> d = np.arange(9).reshape((3, 3))
>>> np.gcd_along_axis(d, axis=1)
array([3, 1, 1])

(This is slightly related to #4829.)

@eric-wieser
Copy link
Member

eric-wieser commented Mar 11, 2017

Sounds like you're after a gufunc for gcd (and why not lcm as well?)

Although lcm could be implemented as a regular ufunc here and then you could use np.lcm.reduce(d, axis=...) for your second case. That wouldn't work for gcd, although you've made me thing about ways it could be made to work (#8773). - wait, turns out I'm an idiot, and gcd is associative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants