Skip to content

ENH: Integer gcd function #8772

Closed
Closed

Description

@brsr

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

Activity

  1. eric-wieser commented on Mar 11, 2017

    @eric-wieser
    Member

    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

  2. self-assigned this
    on Mar 12, 2017
  3. added a commit that references this issue on Dec 13, 2017
    7ab0f15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions