Ticket #13110: trac_13110-python_complex_input-ts.patch

File trac_13110-python_complex_input-ts.patch, 4.6 KB (added by tscrim, 9 years ago)
  • sage/rings/complex_number.pyx

    # HG changeset patch
    # User Travis Scrimshaw <tscrim@ucdavis.edu>
    # Date 1360442508 28800
    # Node ID 8936b6e83f88f7fc4480cf00f0859e2795463a1f
    # Parent c1748d6b8bfb71e1c75f468b2ad94c4ccec3744c
    Trac #13110 - allow python complex as input to ComplexNumber.
    
    diff --git a/sage/rings/complex_number.pyx b/sage/rings/complex_number.pyx
    a b def create_ComplexNumber(s_real, s_imag= 
    23532353
    23542354    INPUT:
    23552355
    2356     - ``s_real`` -- a string that defines a real number
    2357       (or something whose string representation defines a number)
    2358 
    2359     - ``s_imag`` -- a string that defines a real number
    2360       (or something whose string representation defines a number)
    2361 
    2362     - ``pad`` -- an integer at least 0.
     2356    - ``s_real``, ``s_imag`` -- Strings that defines a real number
     2357      (or something whose string representation defines a number).
     2358      Alternatively since :trac:`13110`, the first argument can be a complex
     2359      number.
     2360
     2361    - ``pad`` -- an integer at least 0
    23632362
    23642363    - ``min_prec`` -- number will have at least this many bits of precision,
    2365       no matter what.
     2364      no matter what
    23662365
    23672366    EXAMPLES::
    23682367
    def create_ComplexNumber(s_real, s_imag= 
    23842383        sage: sage.rings.complex_number.create_ComplexNumber(s_real=2,s_imag=1)
    23852384        2.00000000000000 + 1.00000000000000*I
    23862385
     2386    We can create a :class:`ComplexNumber` from other types of a complex number
     2387    (see :trac:`13110`)::
     2388
     2389        sage: ComplexNumber(complex(2,5))
     2390        2.00000000000000 + 5.00000000000000*I
     2391        sage: ComplexNumber(ComplexNumber(2,5))
     2392        2.00000000000000 + 5.00000000000000*I
     2393        sage: ComplexNumber(CDF(2,5))
     2394        2.00000000000000 + 5.00000000000000*I
     2395
     2396    This can also take a variety of string representations of a complex
     2397    number::
     2398
     2399        sage: ComplexNumber('2+5j')
     2400        2.00000000000000 + 5.00000000000000*I
     2401        sage: ComplexNumber('2.0+5.0*I')
     2402        2.00000000000000 + 5.00000000000000*I
     2403        sage: ComplexNumber('+2.0+5.0*I')
     2404        2.00000000000000 + 5.00000000000000*I
     2405        sage: ComplexNumber('+2.0-5.0*I')
     2406        2.00000000000000 - 5.00000000000000*I
     2407        sage: ComplexNumber('-2.0-5.0*I')
     2408        -2.00000000000000 - 5.00000000000000*I
     2409        sage: ComplexNumber('-2.0+5.0*I')
     2410        -2.00000000000000 + 5.00000000000000*I
     2411        sage: ComplexNumber('2.0+5.0I')
     2412        2.00000000000000 + 5.00000000000000*I
     2413
    23872414    TESTS:
    23882415
    23892416    Make sure we've rounded up ``log(10,2)`` enough to guarantee
    def create_ComplexNumber(s_real, s_imag= 
    23952422        sage: sage.rings.complex_number.create_ComplexNumber(0,s).imag()-1 == 0
    23962423        False
    23972424
     2425    Check that converting a real to a python complex does not alter the final
     2426    result (part of the changes in :trac:`13110`)::
     2427
     2428        sage: ComplexNumber('2') == ComplexNumber(2)
     2429        True
    23982430    """
    23992431    if s_imag is None:
    2400         s_imag = 0
     2432        if isinstance(s_real, complex):
     2433            s_imag = s_real.imag
     2434            s_real = s_real.real
     2435        elif isinstance(s_real, (ComplexNumber, ComplexDoubleElement)):
     2436            s_imag = s_real.imag()
     2437            s_real = s_real.real()
     2438        elif isinstance(s_real, str):
     2439            s_real = s_real.strip().lower()
     2440            if s_real[-1] == 'j':
     2441                s_real = complex(s_real)
     2442                s_imag = s_real.imag
     2443                s_real = s_real.real
     2444            elif s_real[-1] == 'i':
     2445                minus = s_real[0] == '-'
     2446                if minus or s_real[0] == '+':
     2447                    s_real = s_real[1:]
     2448                s_plus = s_real.split('+')
     2449                s_minus = s_real.split('-')
     2450                if len(s_plus) + len(s_minus) == 2:
     2451                    s_imag = 0
     2452                elif len(s_plus) + len(s_minus) == 3:
     2453                    if len(s_plus) == 2:
     2454                        s_real = s_plus[0]
     2455                        s_imag = s_plus[1]
     2456                    else:
     2457                        s_real = s_minus[0]
     2458                        s_imag = '-' + s_minus[1]
     2459                    if minus:
     2460                        s_real = '-' + s_real
     2461                    split = s_imag.split('*')
     2462                    if len(split) == 1:
     2463                        s_imag = s_imag[:-1]
     2464                    elif len(split) == 2:
     2465                        s_imag = split[0]
     2466            else:
     2467                s_imag = 0
     2468        else:
     2469            s_imag = 0
     2470    elif isinstance(s_real, (complex, ComplexNumber, ComplexDoubleElement)):
     2471        raise ValueError("Cannot pass a complex number and an imaginary part")
    24012472
    24022473    if not isinstance(s_real, str):
    24032474        s_real = str(s_real).strip()