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

needless quadratic time code in number_field.py #22681

Closed
culler opened this issue Mar 26, 2017 · 21 comments
Closed

needless quadratic time code in number_field.py #22681

culler opened this issue Mar 26, 2017 · 21 comments

Comments

@culler
Copy link

culler commented Mar 26, 2017

sage/src/sage/rings/number_field contains the following loop:

   7292             if both_maps and K.degree() == self.degree():
   7293                 g = K['x'](self.polynomial())
   7294                 v = g.roots()
   7295                 a = from_K(K.gen())
   7296                 for i in range(len(v)):
   7297                     r = g.roots()[i][0]
   7298                     to_K = self.hom([r])    # check=False here ??
   7299                     if to_K(a) == K.gen():
   7300                         break

Line 7297 should read r = v[i][0] .

This actually leads to a huge speed-up.

Component: number fields

Author: Frédéric Chapoton

Branch/Commit: 43d2b98

Reviewer: Vincent Delecroix

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

@culler culler added this to the sage-8.0 milestone Mar 26, 2017
@fchapoton
Copy link
Contributor

Branch: u/chapoton/22681

@fchapoton
Copy link
Contributor

Commit: fa361f6

@fchapoton
Copy link
Contributor

Author: Frédéric Chapoton

@fchapoton
Copy link
Contributor

New commits:

fa361f6trac 22681 cleanup

@videlec
Copy link
Contributor

videlec commented Mar 26, 2017

comment:2

Salut Frédéric,

If you don't care about multiplicity you can use

for root in g.roots(multiplicity=False):
    ...

(which would be cleaner than calling root[0])

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

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

34e7003trac 22681 one more enhancement

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

Changed commit from fa361f6 to 34e7003

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

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

3c95d38trac 22681 fix

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

Changed commit from 34e7003 to 3c95d38

@fchapoton
Copy link
Contributor

comment:5

Salut. Merci pour la suggestion. C'est fait.

@videlec
Copy link
Contributor

videlec commented Mar 26, 2017

comment:6
sage -t --long rings/number_field/number_field_rel.py
**********************************************************************
File "rings/number_field/number_field_rel.py", line 1771, in sage.rings.number_field.number_field_rel.NumberField_relative.roots_of_unity
Failed example:
    K.roots_of_unity()[:5]
Expected:
    [b*a + b, b^2*a, -b^3, a + 1, b*a]
Got:
    [b*a, -b^2*a - b^2, b^3, -a, b*a + b]
**********************************************************************

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

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

19b94b7trac 22681 fixing one doctest

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Mar 26, 2017

Changed commit from 3c95d38 to 19b94b7

@fchapoton
Copy link
Contributor

comment:8

ok, j'ai changé le test, après avoir vérifié que c'etait simplement un ordre different.

@videlec
Copy link
Contributor

videlec commented Mar 27, 2017

comment:9

The order is also different with this new version...

sage -t --long src/sage/rings/number_field/number_field_rel.py
**********************************************************************
File "src/sage/rings/number_field/number_field_rel.py", line 1771, in sage.rings.number_field.number_field_rel.NumberField_relative.roots_of_unity
Failed example:
    K.roots_of_unity()[:5]
Expected:
    [b*a, -b^2*a - b^2, b^3, -a, b*a + b]
Got:
    [-b^3*a - b^3, -b^2*a, b, a + 1, -b^3*a]
**********************************************************************

You can for example replace with

sage: r = K.roots_of_unity()
sage: len(r)
24
sage: a*b in r and b^2*(a-1) in r
True

(however it would be interesting to investigate why it is different each time now...)

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 2, 2017

Changed commit from 19b94b7 to 43d2b98

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 2, 2017

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

d9a388cMerge branch 'u/chapoton/22681' in 8.0.b0
43d2b98trac 22681 better doctest

@fchapoton
Copy link
Contributor

comment:11

I made a better doctest

@videlec
Copy link
Contributor

videlec commented Apr 2, 2017

comment:12

good!

@videlec
Copy link
Contributor

videlec commented Apr 2, 2017

Reviewer: Vincent Delecroix

@vbraun
Copy link
Member

vbraun commented Apr 5, 2017

Changed branch from u/chapoton/22681 to 43d2b98

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

4 participants