Changeset 7571:d691d0e61066


Ignore:
Timestamp:
11/13/07 07:06:23 (6 years ago)
Author:
Ondrej Certik <ondrej@…>
Branch:
default
Message:

SymPy? and SAGE objects can now be mixed

Location:
sage
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sage/calculus/calculus.py

    r7556 r7571  
    271271import sage.functions.functions 
    272272 
     273#needed for converting from SymPy to SAGE 
     274import sympy 
     275 
    273276is_simplified = False 
    274277 
     
    378381        elif isinstance(x, MaximaElement): 
    379382            return symbolic_expression_from_maxima_element(x) 
     383        # if "x" is a SymPy object, convert it to a SAGE object 
     384        elif isinstance(x, sympy.Basic): 
     385            return self(x._sage_()) 
    380386        elif is_Polynomial(x) or is_MPolynomial(x): 
    381387            if x.base_ring() != self:  # would want coercion to go the other way 
     
    38453851                             infixops[self._operator], 
    38463852                             ops[1]._maxima_init_()) 
     3853 
     3854    def _sympy_(self): 
     3855        """Converts any expression to SymPy.""" 
     3856 
     3857        # Current implementation is fragile - it first converts the expression 
     3858        # to string, then preparses it, then gets rid of "Integer" and then 
     3859        # sympifies this string. 
     3860 
     3861        # In order to make this robust, one would have to implement _sympy_ 
     3862        # recursively in all expressions. But we want something now, instead of 
     3863        # tomorrow, so the following one-liner does the job for now. 
     3864        # Also all ugly things are concentrated in this line, everything else 
     3865        # (sympy.sympify, sage.all.SR, ...) is clean and robust. 
     3866        import sympy 
     3867        from sage.all import preparse 
     3868        s = sympy.sympify(preparse(repr(self)).replace("Integer","")) 
     3869        return s 
    38473870 
    38483871    def _sys_init_(self, system): 
  • sage/calculus/test_sympy.py

    r7525 r7571  
    1241248651*x^8/13440 + 241*x^6/240 + 11*x^4/8 + 3*x^2/2 + 1 
    125125 
     126 
     127 
     128Mixing SymPy with SAGE: 
     129sage: import sympy 
     130sage: sympy.sympify(var("y"))+sympy.Symbol("x") 
     131x + y 
     132sage: o = var("omega") 
     133sage: s = sympy.Symbol("x") 
     134sage: t1 = s + o 
     135sage: t2 = o + s 
     136sage: print type(t1) 
     137<class 'sympy.core.add.Add'> 
     138sage: print type(t2) 
     139<class 'sage.calculus.calculus.SymbolicArithmetic'> 
     140sage: print t1, t2 
     141omega + x                                    x + omega 
     142sage: e=sympy.sin(var("y"))+sage.all.cos(Symbol("x")) 
     143sage: print type(e) 
     144<class 'sympy.core.add.Add'> 
     145sage: print e 
     146cos(x) + sin(y) 
     147sage: e=e._sage_() 
     148sage: print type(e) 
     149<class 'sage.calculus.calculus.SymbolicArithmetic'> 
     150sage: print e 
     151                                sin(y) + cos(x) 
     152sage: e = sage.all.cos(var("y")**3)**4+var("x")**2 
     153sage: e = e._sympy_() 
     154sage: print e 
     155    x**2 + cos(y**3)**4 
    126156""" 
  • sage/structure/coerce.pyx

    r7553 r7571  
    328328            return _verify_canonical_coercion_c(x,y) 
    329329             
     330        try: 
     331            if PY_TYPE_CHECK(xp, type) or PY_TYPE_CHECK(yp, type): 
     332                x = x._sage_() 
     333                y = y._sage_() 
     334                return self.canonical_coercion_c(x, y) 
     335        except AttributeError: 
     336            pass 
     337 
    330338        raise TypeError, "no common canonical parent for objects with parents: '%s' and '%s'"%(xp, yp) 
    331339         
Note: See TracChangeset for help on using the changeset viewer.