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

implement round for number field elements with real embedding #21935

Closed
videlec opened this issue Nov 22, 2016 · 20 comments
Closed

implement round for number field elements with real embedding #21935

videlec opened this issue Nov 22, 2016 · 20 comments

Comments

@videlec
Copy link
Contributor

videlec commented Nov 22, 2016

Real embedded element has a floor and ceil but no round (= nearest integer)

sage: a = QuadraticField(2).gen()
sage: a.floor()
1
sage: a.ceil()
2
sage: a.round()
Traceback (most recent call last):
...
AttributeError: 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic' object has no attribute 'round'

Depends on #22095

CC: @mkoeppe @seblabbe

Component: number fields

Keywords: days79

Author: Vincent Delecroix

Branch/Commit: 720f040

Reviewer: Jean-Philippe Labbé, Sébastien Labbé

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

@videlec videlec added this to the sage-7.5 milestone Nov 22, 2016
@videlec
Copy link
Contributor Author

videlec commented Nov 22, 2016

New commits:

720f04021935: implement round for nf element

@videlec
Copy link
Contributor Author

videlec commented Nov 22, 2016

Branch: u/vdelecroix/21935

@videlec
Copy link
Contributor Author

videlec commented Nov 22, 2016

Commit: 720f040

@jplab
Copy link

jplab commented Nov 23, 2016

comment:2

Hi Vincent,

This looks good to me. All test passed on Sage7.5.b3. Up to the reaction of the bot, you can set it to positive review.

JP

@jplab
Copy link

jplab commented Nov 23, 2016

Author: Vincent Delecroix

@jplab
Copy link

jplab commented Nov 23, 2016

Reviewer: Jean-Philippe Labbé

@videlec
Copy link
Contributor Author

videlec commented Nov 24, 2016

comment:3

patchbot is happy ;-)

@vbraun
Copy link
Member

vbraun commented Nov 25, 2016

comment:4

On OSX

5772sage -t --long src/sage/rings/number_field/number_field_element_quadratic.pyx
5773**********************************************************************
5774File "src/sage/rings/number_field/number_field_element_quadratic.pyx", line 1997, in sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round
5775Failed example:
5776    for _ in range(100):
5777       a = QQ.random_element(1000,20)
5778       b = QQ.random_element(1000,20)
5779       assert round(a) == round(K2(a)), a
5780       assert round(a) == round(K3(a)), a
5781       assert round(a) == round(K5(a)), a
5782       assert round(a+b*sqrt(2.)) == round(a+b*sqrt2), (a, b)
5783       assert round(a+b*sqrt(3.)) == round(a+b*sqrt3), (a, b)
5784       assert round(a+b*sqrt(5.)) == round(a+b*sqrt5), (a, b)
5785Exception raised:
5786    Traceback (most recent call last):
5787      File "/Users/buildslave-sage/slave/sage_git/build/local/lib/python2.7/site-packages/sage/doctest/forker.py", line 498, in _run
5788        self.compile_and_execute(example, compiler, test.globs)
5789      File "/Users/buildslave-sage/slave/sage_git/build/local/lib/python2.7/site-packages/sage/doctest/forker.py", line 861, in compile_and_execute
5790        exec(compiled, globs)
5791      File "<doctest sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round[8]>", line 5, in <module>
5792        assert round(a) == round(K3(a)), a
5793    AssertionError: 9/11
5794**********************************************************************
57951 item had failures:
5796   1 of  10 in sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round
5797    [472 tests, 1 failure, 21.51 s]

@seblabbe
Copy link
Contributor

comment:6

I can reproduce the doctest failure.

But I can not reproduce the problem on the command line:

sage: a = 9/11
sage: round(a)
1
sage: round(K3(a))
1

@seblabbe
Copy link
Contributor

comment:7

With

diff --git a/src/sage/rings/number_field/number_field_element_quadratic.pyx b/src/sage/rings/number_field/number_field_element_quadratic.pyx
index 83f5495..fad31df 100644
--- a/src/sage/rings/number_field/number_field_element_quadratic.pyx
+++ b/src/sage/rings/number_field/number_field_element_quadratic.pyx
@@ -2054,7 +2054,8 @@ cdef class NumberFieldElement_quadratic(NumberFieldElement_absolute):
             ....:    a = QQ.random_element(1000,20)
             ....:    b = QQ.random_element(1000,20)
             ....:    assert round(a) == round(K2(a)), a
-            ....:    assert round(a) == round(K3(a)), a
+            ....:    if not round(a) == round(K3(a)): 
+            ....:        print "left={}, right={}, a={}".format(round(a), round(K3(a)), a)
             ....:    assert round(a) == round(K5(a)), a
             ....:    assert round(a+b*sqrt(2.)) == round(a+b*sqrt2), (a, b)
             ....:    assert round(a+b*sqrt(3.)) == round(a+b*sqrt3), (a, b)

I get:

$ sage -tp src/sage/rings/number_field/number_field_element_quadratic.pyx
Running doctests with ID 2016-12-23-15-11-57-2aaeb3bd.
Git branch: 21935
Using --optional=4ti2,atlas,ccache,latte_int,meataxe,mpir,pandoc_attributes,pandocfilters,python2,rst2ipynb,sage
Doctesting 1 file using 4 threads.
sage -t --warn-long 40.9 src/sage/rings/number_field/number_field_element_quadratic.pyx
**********************************************************************
File "src/sage/rings/number_field/number_field_element_quadratic.pyx", line 2053, in sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round
Failed example:
    for _ in range(100):
       a = QQ.random_element(1000,20)
       b = QQ.random_element(1000,20)
       assert round(a) == round(K2(a)), a
       if not round(a) == round(K3(a)): 
           print "left={}, right={}, a={}".format(round(a), round(K3(a)), a)
       assert round(a) == round(K5(a)), a
       assert round(a+b*sqrt(2.)) == round(a+b*sqrt2), (a, b)
       assert round(a+b*sqrt(3.)) == round(a+b*sqrt3), (a, b)
       assert round(a+b*sqrt(5.)) == round(a+b*sqrt5), (a, b)
Expected nothing
Got:
    left=1, right=0, a=9/11
    left=0, right=1, a=1/13
**********************************************************************
1 item had failures:
   1 of  10 in sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic.round
    [485 tests, 1 failure, 7.31 s]
----------------------------------------------------------------------
sage -t --warn-long 40.9 src/sage/rings/number_field/number_field_element_quadratic.pyx  # 1 doctest failed
----------------------------------------------------------------------

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

comment:8

Can someone can give me ssh access to an OSX computer? This really needs to be fixed!!

@seblabbe
Copy link
Contributor

comment:9

Replying to @videlec:

Can someone can give me ssh access to an OSX computer? This really needs to be fixed!!

I was using my desktop at work when I reproduced the error earlier today which runs Ubuntu.

Don't you get the same error on your machine?

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

comment:10

Indeed, I am able to reproduce it and has nothing to do with the current branch

Failed example:
    K3(9/11) - 0
Expected:
    9/11
Got:
    -1/2*sqrt3 + 9/11

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

comment:11

Even better ;-)

Failed example:
    K3.zero()
Expected:
    0
Got:
    1/2*sqrt3

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

comment:12

#22095

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

comment:13

test passes for me when #22095 applied.

@videlec
Copy link
Contributor Author

videlec commented Dec 23, 2016

Dependencies: #22095

@seblabbe
Copy link
Contributor

Changed reviewer from Jean-Philippe Labbé to Jean-Philippe Labbé, Sébastien Labbé

@seblabbe
Copy link
Contributor

comment:14

I do not have access anymore to my Ubuntu machine to test the problem I had earlier.

All tests passed on my OSX. Positive review.

@vbraun
Copy link
Member

vbraun commented Jan 18, 2017

Changed branch from u/vdelecroix/21935 to 720f040

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