[sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-15 Thread Nils Bruin
On May 15, 8:58 am, Nils Bruin  wrote:
> sage: zeta.simplify_radical()
> -1

Sorry, that was supposed to be

sage: (zeta^3).simplify_radical()
-1

my apologies.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-15 Thread Nils Bruin
On May 15, 3:25 am, man...@gmx.net wrote:
> It does not work because "abs( )" returns an "Expression" which
> will cause find_local_minimum to cause errors.

The problem here is that 1.0+I*1.0 is a symbolic expression rather
than a complex number (which one would you want? python complex? CDF?
some multi-precision complex floats?), but it's a bit of a crystal
ball problem:

as far as sage is concerned, I is a "symbolic expression" (the one
representing a square root of -1). Multiplying an expression with a
float (of pretty much any kind) results in a symbolic expression,
because that's often the only thing it can do. Think

1.2*cos(x)

In this particular case, you would have preferred if "I" had read as a
kind of (complex) float. However, when someone types

zeta = 1/2*(1+sqrt(3)*I)

they are probably going to be a bit upset if sage decides to just
compute with float approximations rather than the exact expression.
See:

sage: zeta.simplify_radical()
-1

The solution in your case is to work with the appropriate I, i.e.,

cI= CDF(0,1) # or CDF(I) for that matter

and then there's no ambiguity.

The main problem here is that pretty much everything can be
interpreted as a "symbolic expression", so once you've forced sage to
consider it as such, it's as if it vanished into a black hole: its
original type information is not readily accessible.

Unless you really do want to work with "symbolic expressions", don't
let them touch your work! They're infectious, as you've experienced,
and they are so by design.

It does mean you have to think about the type of object you DO want to
work with (in your case that seems to be easy: floats or float
complex).

If a routine returns a float when fed an RDF element or other sage
implementation of floats, it's probably a bug, so those are definitely
worth reporting. It may be a bit overwhelming that there are multiple
implementations of floats in sage, but they do have their purposes.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-15 Thread manday
I think you shouldn't blame it all on the user. I understand and
appreciate that Sage makes a clear distinction between different types
and handles them differently. But that doesn't explain the numerous
errors which are caused by SAGE's funtions which should work with a
specific type, but simply refuse to.

Here is another instance: http://alpha.sagenb.org/home/pub/701/

It does not work because "abs( )" returns an "Expression" which
will cause find_local_minimum to cause errors.

This is one instance of dozens I've encountered. Other examples include
functions returning floats while yet others cause errors when called
with floats instead of Reals, etc...

On Mon, May 13, 2013 at 05:22:07PM -0700, rjf wrote:
>On Monday, May 13, 2013 2:11:59 PM UTC-7, ManDay wrote:
> 
>  Could anyone shed a bit of light on this?
> 
>  The more I try, the more SAGE's types get in my way.
> 
>It is pretty much understood by some people in the computer algebra
>system-building community
>that "types" or "categories" or similar concepts imposed on users who
>understand some mathematics (but maybe not much modern algebra)
> and not much about programming language design and numerical computation,
>will fall into holes.
> 
>See what happened to Scratchpad / Axiom. It tries and maybe does as
>good a job as we know how to do, but users don't go for it.
> 
>It is not too surprising to find people falling into holes in the Sage
>type system.
> 
>Sorry, no easy solution to everything along these lines has emerged.  So
>far
>as I can tell, Python offers no step forward, and maybe a step backward.
> 
>RJF
> 
>--
>You received this message because you are subscribed to the Google Groups
>"sage-devel" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to sage-devel+unsubscr...@googlegroups.com.
>To post to this group, send email to sage-devel@googlegroups.com.
>Visit this group at [1]http://groups.google.com/group/sage-devel?hl=en.
>For more options, visit [2]https://groups.google.com/groups/opt_out.
> 
> 
> 
> References
> 
>Visible links
>1. http://groups.google.com/group/sage-devel?hl=en
>2. https://groups.google.com/groups/opt_out

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-14 Thread Ralf Hemmecke
> does Axiom have Coercion?

You surely have never looked into AXIOM.

http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.132.4943&rep=rep1&type=pdf

> How does it handle this?
> http://www.sagemath.org/doc/reference/coercion/sage/structure/coerce.html

It depends on how much automatic coercions you want to allow. For
library programming I personally don't like automatic coercions, since
they might hide a bug in the code. But working in an interactive
session, AXIOM has some smartness in the interpreter that allows
"coerce" (and sometimes also "convert" and "retract" functions) to be
applied.

Ralf

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-14 Thread Harald Schilly
On Tuesday, May 14, 2013 2:22:07 AM UTC+2, rjf wrote:
>
> So far
> as I can tell, Python offers no step forward, and maybe a step backward.
>

Sage != Python. Apart from that, does Axiom have Coercion? How does it 
handle this?

 http://www.sagemath.org/doc/reference/coercion/sage/structure/coerce.html

H

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-13 Thread manday
Thank you John and the others, too. That clarifies it. I never used
complex numbers in C (personally, I consider them, just like wchars, out
of place but that's another subject...), if I had, I'd likely have
known.

It's not apparently clear how to solve this problem consistently (I'm
just performing a handbook Gauss-Jacobi Quadrature for a complex
Schwarz-Christoffel Integral in my code and wouldn't really know where
to deviate from the straight-forward implementation by which pow(
-m,alpha ) arises) but I'll hopefully figure out


On Mon, May 13, 2013 at 02:56:28PM -0700, John H Palmieri wrote:
>On Monday, May 13, 2013 2:11:59 PM UTC-7, ManDay wrote:
> 
>  Could anyone shed a bit of light on this?
> 
>  The more I try, the more SAGE's types get in my way. It turns out to be
>  an unpredictable element. Unless I make sure *every* involved function
>  really returns one specific type, calculations are becoming practically
>  impossible, because the results are completely random (malfunctions).
> 
>  Here is just the last and most annoying of my encounters I can't make
>  heads or tails of, enjoy:
> 
>  [1]http://alpha.sagenb.org/home/pub/701/
> 
>The issue is, if you define a number a by
> 
>   sage: a = -CDF(2.4995)
> 
>then the imaginary part of a is "-0.0", and it is treated as negative.
>When a**(1/2) is computed, it is computed (according to the documentation)
>as exp(log(a)*(1/2)), and since the imaginary part of a is negative, its
>log has imaginary part approximately equal to -pi. This choice of branch
>cut is documented: type "log?" On the other hand:
> 
>   sage: b = CDF(-2.4995)
> 
>Then b has imaginary part which is 0.0, treated as positive, so log(b) has
>imaginary part approximately equal to +pi.
> 
>Maybe "sqrt(a)" would give you more consistent results.
> 
>   sage: sqrt(a)
>   1.58098070829*I
>   sage: sqrt(b)
>   1.58098070829*I
> 
>--
>John
> 
>--
>You received this message because you are subscribed to the Google Groups
>"sage-devel" group.
>To unsubscribe from this group and stop receiving emails from it, send an
>email to sage-devel+unsubscr...@googlegroups.com.
>To post to this group, send email to sage-devel@googlegroups.com.
>Visit this group at [2]http://groups.google.com/group/sage-devel?hl=en.
>For more options, visit [3]https://groups.google.com/groups/opt_out.
> 
> 
> 
> References
> 
>Visible links
>1. http://alpha.sagenb.org/home/pub/701/
>2. http://groups.google.com/group/sage-devel?hl=en
>3. https://groups.google.com/groups/opt_out

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-13 Thread rjf


On Monday, May 13, 2013 2:11:59 PM UTC-7, ManDay wrote:
>
> Could anyone shed a bit of light on this? 
>
> The more I try, the more SAGE's types get in my way.
>

It is pretty much understood by some people in the computer algebra 
system-building community
that "types" or "categories" or similar concepts imposed on users who
understand some mathematics (but maybe not much modern algebra)
 and not much about programming language design and numerical computation,
will fall into holes.

See what happened to Scratchpad / Axiom. It tries and maybe does as
good a job as we know how to do, but users don't go for it.

It is not too surprising to find people falling into holes in the Sage type 
system.

Sorry, no easy solution to everything along these lines has emerged.  So far
as I can tell, Python offers no step forward, and maybe a step backward.

RJF


-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-13 Thread Stephen Montgomery-Smith
On 05/13/2013 04:56 PM, John H Palmieri wrote:
> On Monday, May 13, 2013 2:11:59 PM UTC-7, ManDay wrote:
> 
> Could anyone shed a bit of light on this?
> 
> The more I try, the more SAGE's types get in my way. It turns out to be
> an unpredictable element. Unless I make sure *every* involved function
> really returns one specific type, calculations are becoming practically
> impossible, because the results are completely random (malfunctions).
> 
> Here is just the last and most annoying of my encounters I can't make
> heads or tails of, enjoy:
> 
> http://alpha.sagenb.org/home/pub/701/
> 
> 
> 
> The issue is, if you define a number a by
> 
>sage: a = -CDF(2.4995)
> 
> then the imaginary part of a is "-0.0", and it is treated as negative.
> When a**(1/2) is computed, it is computed (according to the
> documentation) as exp(log(a)*(1/2)), and since the imaginary part of a
> is negative, its log has imaginary part approximately equal to -pi. This
> choice of branch cut is documented: type "log?" On the other hand:
> 
>sage: b = CDF(-2.4995)
> 
> Then b has imaginary part which is 0.0, treated as positive, so log(b)
> has imaginary part approximately equal to +pi.

Also, this not restricted to sage.  Try this little C program (using C99
standard, so don't try it on Visual Studio):

#include 
#include 
#include 

int main() {
  complex a = 2.4995, b = -2.4995;
  printf("%g %g\n", cimag(csqrt(-a)), cimag(csqrt(b)));
}

This really is what the C99 standard requires.

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.




[sage-devel] Re: SQRT of negative number is negative? Or positive? Or what?

2013-05-13 Thread John H Palmieri
On Monday, May 13, 2013 2:11:59 PM UTC-7, ManDay wrote:
>
> Could anyone shed a bit of light on this? 
>
> The more I try, the more SAGE's types get in my way. It turns out to be 
> an unpredictable element. Unless I make sure *every* involved function 
> really returns one specific type, calculations are becoming practically 
> impossible, because the results are completely random (malfunctions). 
>
> Here is just the last and most annoying of my encounters I can't make 
> heads or tails of, enjoy: 
>
> http://alpha.sagenb.org/home/pub/701/ 
>

The issue is, if you define a number a by

   sage: a = -CDF(2.4995)

then the imaginary part of a is "-0.0", and it is treated as negative. When 
a**(1/2) is computed, it is computed (according to the documentation) as 
exp(log(a)*(1/2)), and since the imaginary part of a is negative, its log 
has imaginary part approximately equal to -pi. This choice of branch cut is 
documented: type "log?" On the other hand:

   sage: b = CDF(-2.4995)

Then b has imaginary part which is 0.0, treated as positive, so log(b) has 
imaginary part approximately equal to +pi.

Maybe "sqrt(a)" would give you more consistent results.

   sage: sqrt(a)
   1.58098070829*I
   sage: sqrt(b)
   1.58098070829*I

-- 
John

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at http://groups.google.com/group/sage-devel?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.