Opened 8 years ago

Last modified 5 years ago

#11962 new defect

mpmath mpc won't coerce to ComplexNumber

Reported by: dsm Owned by: robertwb
Priority: major Milestone: sage-6.8
Component: coercion Keywords:
Cc: leif Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
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 (5)

comment:1 Changed 8 years ago by leif

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

comment:2 Changed 8 years ago by leif

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

comment:3 Changed 8 years 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 8 years ago by jdemeyer

  • Milestone sage-4.7.3 deleted

Milestone sage-4.7.3 deleted

comment:5 Changed 5 years ago by jdemeyer

  • Component changed from interfaces to coercion
  • Milestone set to sage-6.8
  • Owner changed from was to robertwb
Note: See TracTickets for help on using tickets.