Opened 9 years ago

Last modified 6 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:


Came across this working on #1173:

sage: version()
'Sage Version 4.7.2.alpha3, Release Date: 2011-09-28'
sage: import mpmath
sage: z = mpmath.mpf(2)
sage: z
sage: RR(z)
sage: CC(z)
sage: z = mpmath.mpc(2,3)
sage: z
mpc(real='2.0', imag='3.0')
sage: complex(z)
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 9 years ago by leif

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

comment:2 Changed 9 years ago by leif

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

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

  • Milestone sage-4.7.3 deleted

Milestone sage-4.7.3 deleted

comment:5 Changed 6 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.