Hi,

I am having problems understanding how "I" (complex unit) in Sage:

sage: type(I)
<type 'sage.symbolic.expression.Expression'>

somehow becomes a NumberFieldElement_quadratic and that fails to
convert to the sympy's I:

ond...@raven:~/repos/sympy(pu)$ MPMATH_NOSAGE=yes sage -python
bin/test sympy/test_external/test_sage.py --pdb
============================= test process starts ==============================
executable:   
/home/ondrej/ext/sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/bin/python
 (2.6.4-final-0)

sympy/test_external/test_sage.py[13] .XXX <type
'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>
I
E> /home/ondrej/repos/sympy/sympy/core/sympify.py(185)_sympify()
-> raise SympifyError("%r is NOT a valid SymPy expression" % (a,))
(Pdb) a
a = I
(Pdb) type(a)
<type 
'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>



Here is the full traceback:

________________ sympy/test_external/test_sage.py:test_complex _________________
  File "/home/ondrej/repos/sympy/sympy/test_external/test_sage.py",
line 61, in test_complex
    check_expression("I*y", "y")
  File "/home/ondrej/repos/sympy/sympy/test_external/test_sage.py",
line 47, in check_expression
    assert sympy.S(e_sage) == e_sympy
  File "/home/ondrej/repos/sympy/sympy/core/sympify.py", line 86, in sympify
    v = meth()
  File "expression.pyx", line 932, in
sage.symbolic.expression.Expression._sympy_
(sage/symbolic/expression.cpp:5466)
  File 
"/home/ondrej/ext/sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py",
line 214, in __call__
    return self.arithmetic(ex, operator)
  File 
"/home/ondrej/ext/sage-4.3.3-linux-64bit-ubuntu_9.10-x86_64-Linux/local/lib/python2.6/site-packages/sage/symbolic/expression_conversions.py",
line 587, in arithmetic
    return sympy.Mul(*ops)
  File "/home/ondrej/repos/sympy/sympy/core/cache.py", line 85, in wrapper
    func_cache_it_cache[k] = r = func(*args, **kw_args)
  File "/home/ondrej/repos/sympy/sympy/core/operations.py", line 35, in __new__
    c_part, nc_part, order_symbols = cls.flatten(map(_sympify, args))
  File "/home/ondrej/repos/sympy/sympy/core/sympify.py", line 185, in _sympify
    raise SympifyError("%r is NOT a valid SymPy expression" % (a,))
SympifyError: SympifyError: I is NOT a valid SymPy expression


The relevant code is in sage/symbolic/expression_conversions.py around
the line 587:

    def arithmetic(self, ex, operator):
        """
        EXAMPLES::

            sage: from sage.symbolic.expression_conversions import
SympyConverter
            sage: s = SympyConverter()
            sage: f = x + 2
            sage: s.arithmetic(f, f.operator())
            2 + x
        """
        import sympy
        operator = arithmetic_operators[operator]
        ops = [self(a) for a in ex.operands()]
        if operator == "+":
            return sympy.Add(*ops)
        elif operator == "*":
            return sympy.Mul(*ops)


where the "self(a)" thing converts an expression from Sage to sympy,
and thus "ops" in the line:

            return sympy.Mul(*ops)

are sympy symbols and thus everything works. Inluding "I" alone, as
these tests in sympy pass just fine:

    check_expression("I", "")
    check_expression("23+I*4", "x")

This test fails however:

    check_expression("I*y", "y")


what happens is that "ops" contains

[y, I]

where "y" is a sympy symbol (ok), but I is of a type
NumberFieldElement_quadratic and sympy then fails to convert it to a
sympy expression in Mul.

What I understood is that Sage tries to optimize this expressions
somehow using NumberFieldElement_quadratic, but I wasn't able to
reproduce it:

sage: var("y")
y
sage: type(I*y)
<type 'sage.symbolic.expression.Expression'>


Does anyone understand this code in Sage? What is happening there?
Should I implement _sympy_ methods in NumberFieldElement_quadratic, or
what is the way to fix this?

Thanks,
Ondrej

-- 
To post to this group, send an email to sage-devel@googlegroups.com
To unsubscribe from this group, send an email to 
sage-devel+unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

To unsubscribe from this group, send email to 
sage-devel+unsubscribegooglegroups.com or reply to this email with the words 
"REMOVE ME" as the subject.

Reply via email to