Ticket #11962 (new defect)

Opened 19 months ago

Last modified 19 months ago

mpmath mpc won't coerce to ComplexNumber

Reported by: dsm Owned by: was
Priority: major Milestone:
Component: interfaces Keywords:
Cc: leif Work issues:
Report Upstream: N/A Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

Came across this working on #1173:

sage: version()
'Sage Version 4.7.2.alpha3, Release Date: 2011-09-28'
sage: import mpmath
sage: 
sage: z = mpmath.mpf(2)
sage: z
mpf('2.0')
sage: RR(z)
2.00000000000000
sage: CC(z)
2.00000000000000
sage: 
sage: z = mpmath.mpc(2,3)
sage: z
mpc(real='2.0', imag='3.0')
sage: complex(z)
(2+3j)
sage: CC(z)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
[...]
TypeError: unable to coerce to a ComplexNumber: <type 'sage.libs.mpmath.ext_main.mpc'>

Change History

comment:1 Changed 19 months ago by leif

  • Cc leif added
  • Owner changed from tbd to was
  • Component changed from PLEASE CHANGE to interfaces

comment:2 Changed 19 months ago by leif

  • Summary changed from mpc won't coerce to ComplexNumber to mpmath mpc won't coerce to ComplexNumber

comment:3 Changed 19 months ago by leif

A trivial fix would be:

  • sage/rings/complex_number.pyx

    diff --git a/sage/rings/complex_number.pyx b/sage/rings/complex_number.pyx
    a b  
    148148            elif isinstance(real, list) or isinstance(real, tuple): 
    149149                re, imag = real 
    150150                real = re 
    151             elif isinstance(real, complex): 
    152                 real, imag = real.real, real.imag 
    153151            else: 
    154                 imag = 0 
     152                try: 
     153                    real, imag = real.real, real.imag 
     154                except AttributeError: 
     155                    imag = 0 
    155156        try: 
    156157            R = parent._real_field() 
    157158            rr = R(real) 

But perhaps there are better ways.

comment:4 Changed 19 months ago by jdemeyer

  • Milestone sage-4.7.3 deleted

Milestone sage-4.7.3 deleted

Note: See TracTickets for help on using tickets.