Skip to content
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

passing a generator to gcd() fails #32314

Closed
yyyyx4 opened this issue Jul 30, 2021 · 12 comments
Closed

passing a generator to gcd() fails #32314

yyyyx4 opened this issue Jul 30, 2021 · 12 comments

Comments

@yyyyx4
Copy link
Member

yyyyx4 commented Jul 30, 2021

The following use of gcd() fails:

sage: gcd(x for x in [123,456,789])
# ...
TypeError: object of type 'generator' has no len()

At the same time, lcm() works fine with generators:

sage: lcm(x for x in [123,456,789])
4917048

The proposed fix is to make sure we consume the generator only once in gcd() and pass it to GCD_list() as a list.

Component: basic arithmetic

Keywords: gcd, generator, list

Author: Lorenz Panny

Branch/Commit: e6a3c5d

Reviewer: Kwankyu Lee

Issue created by migration from https://trac.sagemath.org/ticket/32314

@yyyyx4 yyyyx4 added this to the sage-9.4 milestone Jul 30, 2021
@kwankyu
Copy link
Collaborator

kwankyu commented Aug 6, 2021

comment:2

It seems this

     if seq.universe() is ZZ:
-        return GCD_list(a)
+        return GCD_list(seq)
     else:
         return __GCD_sequence(seq, **kwargs)

is enough to fix the bug, and enforcing the type in GCD_list() is not necessary. Is it?

@yyyyx4
Copy link
Member Author

yyyyx4 commented Aug 6, 2021

comment:3

You're right. I moved the list cast in GCD_list to an earlier time because GCD_list was also failing on generators due to the len() call, but that could be considered intended. I can change it back if you prefer (I agree that there might be a tiny performance impact in some cases, like when you pass a massive tuple full of Integers).

@kwankyu
Copy link
Collaborator

kwankyu commented Aug 6, 2021

comment:4

Replying to @yyyyx4:

I can change it back if you prefer (I agree that there might be a tiny performance impact in some cases, like when you pass a massive tuple full of Integers).

I prefer it because of the performance degradation.

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 6, 2021

Changed commit from 231862d to e6a3c5d

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 6, 2021

Branch pushed to git repo; I updated commit sha1. New commits:

e6a3c5dundo earlier list cast in GCD_list

@yyyyx4
Copy link
Member Author

yyyyx4 commented Aug 6, 2021

comment:6

Done.

@yyyyx4

This comment has been minimized.

@kwankyu
Copy link
Collaborator

kwankyu commented Aug 7, 2021

comment:8

LGTM

@kwankyu
Copy link
Collaborator

kwankyu commented Aug 7, 2021

Reviewer: Kwankyu Lee

@fchapoton
Copy link
Contributor

comment:9

what about the necessary new doctest ?

@kwankyu
Copy link
Collaborator

kwankyu commented Aug 7, 2021

comment:10

Replying to @fchapoton:

what about the necessary new doctest ?

Feel free to set it back to needs work if you think a doctest is required.

@mkoeppe mkoeppe modified the milestones: sage-9.4, sage-9.5 Aug 22, 2021
@vbraun
Copy link
Member

vbraun commented Oct 19, 2021

Changed branch from public/fix_gcd_on_generators to e6a3c5d

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

5 participants