Ticket #4892 (closed defect: fixed)

Opened 4 years ago

Last modified 4 years ago

[with patch, positive review] Changing precision of a Complex can convert it to a real

Reported by: cremona Owned by: rlm
Priority: major Milestone: sage-3.3
Component: basic arithmetic Keywords: real complex precision
Cc: Work issues:
Report Upstream: Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

georg.grafendorfer reported this to sage-support:

sage: a = CC(-5).n(prec=100)
sage: b = ComplexField(100)(-5)
sage: a == b
True
sage: type(a) == type(b)
False
sage: ln(a)
NaN
sage: ln(b)
1.6094379124341003746007593332 + 3.1415926535897932384626433833*I

The issue is that both a and b are equal to -5 (exactly, to 100 bit precision) but a is a Real while b is a Complex. This happens because

Looking at the code for numerical_approx() in sage.misc.functional,
this happens because the attempt to coerce z into RealField(100)
succeeds.  To fix this (if it is agreed that it is a bug) that
function would need to test the type of the input and return something
of the same type (real/complex).

The suggested fix is that the numerical_approx function should always return a complex number to the appropriate precsion if the input has type complex, even if the coercion into a real succeeded.

Attachments

trac_4892-complex_approx.patch Download (1.5 KB) - added by rlm 4 years ago.
trac_4892_1.patch Download (1.2 KB) - added by AlexGhitza 4 years ago.
apply after the other patch

Change History

Changed 4 years ago by rlm

comment:1 Changed 4 years ago by rlm

  • Owner changed from somebody to rlm
  • Status changed from new to assigned
  • Summary changed from Changing precision of a Complex can convert it to a real to [with patch, needs review] Changing precision of a Complex can convert it to a real

comment:2 Changed 4 years ago by AlexGhitza

  • Summary changed from [with patch, needs review] Changing precision of a Complex can convert it to a real to [with patch, positive review] Changing precision of a Complex can convert it to a real

Looks good to me.

comment:3 Changed 4 years ago by mabshoff

  • Summary changed from [with patch, positive review] Changing precision of a Complex can convert it to a real to [with patch, needs work] Changing precision of a Complex can convert it to a real

This patch causes the following doctest failure:

mabshoff@geom:/scratch/mabshoff/sage-3.3.alpha2$ ./sage -t -long devel/sage/sage/modules/vector_double_dense.pyx
sage -t -long "devel/sage/sage/modules/vector_double_dense.pyx"
**********************************************************************
File "/scratch/mabshoff/sage-3.3.alpha2/devel/sage/sage/modules/vector_double_dense.pyx", line 531:
    sage: _.parent()
Expected:
    Vector space of dimension 3 over Real Field with 53 bits of precision
Got:
    Vector space of dimension 3 over Complex Field with 53 bits of precision
**********************************************************************
File "/scratch/mabshoff/sage-3.3.alpha2/devel/sage/sage/modules/vector_double_dense.pyx", line 535:
    sage: _.parent()
Expected:
    Vector space of dimension 3 over Real Field with 75 bits of precision
Got:
    Vector space of dimension 3 over Complex Field with 75 bits of precision
**********************************************************************

Given that this is vector_double_dense.pyx it seems odd.

Cheers,

Michael

comment:4 Changed 4 years ago by AlexGhitza

  • Summary changed from [with patch, needs work] Changing precision of a Complex can convert it to a real to [with patch, needs review] Changing precision of a Complex can convert it to a real

Oops I should have tested modules/ during the review of rlm's patch.

But the doctests were indeed wrong to start with, the vector spaces *should* be complex. I've added a trivial patch that fixes this.

Changed 4 years ago by AlexGhitza

apply after the other patch

comment:5 Changed 4 years ago by rlm

  • Summary changed from [with patch, needs review] Changing precision of a Complex can convert it to a real to [with patch, positive review] Changing precision of a Complex can convert it to a real

+1 to the second patch

comment:6 Changed 4 years ago by mabshoff

  • Status changed from assigned to closed
  • Resolution set to fixed
  • Milestone changed from sage-3.4.1 to sage-3.3

Merged both patches in Sage 3.3.alpha3.

Cheers,

Michael

Note: See TracTickets for help on using tickets.