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

sqrt(4) returns a SymbolicComposition instead of the number 2! #4425

Closed
williamstein opened this issue Nov 2, 2008 · 10 comments
Closed

sqrt(4) returns a SymbolicComposition instead of the number 2! #4425

williamstein opened this issue Nov 2, 2008 · 10 comments

Comments

@williamstein
Copy link
Contributor

In Sage-3.1.4 we have this, which I consider wrong:

sage: n = 4
sage: type(sqrt(n))
<class 'sage.calculus.calculus.SymbolicComposition'>
sage: type(n.sqrt())
<type 'sage.rings.integer.Integer'>

I think sqrt(foo) should first check if foo has a sqrt method, and
if so call it. I realize there is a subtle problem here, because
the integer sqrt function calls the symbolic calculus one! So we
need some sort of architecture to fix this right. This isn't trivial.

Component: basic arithmetic

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

@kcrisman
Copy link
Member

kcrisman commented Nov 4, 2008

comment:1

This patch takes care of the problem as stated, passes tests for integer.pyx and rational.pyx; test for calculus.py times out (as it always does on my computer, not just for this patch).

I am fairly certain it also does not introduce some new endless loops or other weird bugs, but that would be worth checking out by any reviewer.

@kcrisman kcrisman self-assigned this Nov 4, 2008
@kcrisman
Copy link
Member

kcrisman commented Nov 4, 2008

Based on 3.2.alpha0

@mwhansen
Copy link
Contributor

mwhansen commented Nov 5, 2008

comment:2

Attachment: sqrt-nonsymbolic.patch.gz

I think that the .sqrt() method in Integer and Rational should call sqrt._do_sqrt instead of creating the SymbolicComposition themselves. For example, see

sage: sqrt._do_sqrt(4)
2
sage: type(_)
<class 'sage.calculus.calculus.SymbolicComposition'>
sage: sqrt._do_sqrt(5)
sqrt(5)
sage: sqrt._do_sqrt(5, all=True)
[sqrt(5), -sqrt(5)]

@mwhansen
Copy link
Contributor

mwhansen commented Nov 6, 2008

comment:3

Attachment: trac_4425.patch.gz

I've attached a patch which makes the change I suggested. What are your thoughts?

@kcrisman
Copy link
Member

kcrisman commented Nov 6, 2008

Attachment: sqrt-nonsymbolic-1.patch.gz

@kcrisman
Copy link
Member

kcrisman commented Nov 6, 2008

comment:4

This is fine; in the meantime I did get around to implementing this. However, I also moved the prec evaluation to the _do_sqrt function as well, since it seemed consonant with your review.

After thinking about it, it also makes more sense do that because if for some reason something lands in sqrt with prec which isn't RR, Rational, Integer, etc. then _do_sqrt() should still make the square root be in RR if x is positive; the previous behavior was to automatically put it in CC.

Does this seem okay?

@williamstein
Copy link
Contributor Author

comment:5

I attached a trivial followup patch to sqrt-nonsymbolic-1.patch which adds a docstring for the _do_sqrt function (which had no docstring) and some doctests to that function. The new docstring also clarifies that the extend option to _do_sqrt is completely ignored, and why that is the right behavior.

The original patch sqrt-nonsymbolic-1.patch and that patch plus the attached followup sqrt-nonsymblic-2.patch together pass all doctests for me in rings and calculus.

@williamstein
Copy link
Contributor Author

Attachment: sqrt-nonsymbolic-2.patch.gz

mabshoff -- apply this and sqrt-nonsymbolic-1.patch; don't apply anything else

@kcrisman
Copy link
Member

kcrisman commented Nov 6, 2008

comment:6

Replying to @williamstein:

I attached a trivial followup patch to sqrt-nonsymbolic-1.patch which adds a docstring for the _do_sqrt function (which had no docstring) and some doctests to that function. The new docstring also clarifies that the extend option to _do_sqrt is completely ignored, and why that is the right behavior.

This seems okay; I didn't have time to merge it but the new tests behave correctly. So this means that the following is the desired behavior for when to extend and when not to extend?

sage: sqrt._do_sqrt(Integer(3),extend=False)
sqrt(3)
sage: a=3; type(a)
<type 'sage.rings.integer.Integer'>
sage: a.sqrt(extend=False)
---------------------------------------------------------------------------
ValueError: square root of 3 not an integer

If so, then let's finally let the square root of 4 be 2!

@sagetrac-mabshoff
Copy link
Mannequin

sagetrac-mabshoff mannequin commented Nov 8, 2008

comment:7

Merged sqrt-nonsymbolic-1.patch and sqrt-nonsymbolic-2.patch in Sage 3.2.rc0

@sagetrac-mabshoff sagetrac-mabshoff mannequin closed this as completed Nov 8, 2008
@sagetrac-mabshoff sagetrac-mabshoff mannequin modified the milestones: sage-3.2.1, sage-3.2 Nov 8, 2008
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

3 participants