Changeset 7571:d691d0e61066
- Timestamp:
- 11/13/07 07:06:23 (6 years ago)
- Branch:
- default
- Location:
- sage
- Files:
-
- 3 edited
-
calculus/calculus.py (modified) (3 diffs)
-
calculus/test_sympy.py (modified) (1 diff)
-
structure/coerce.pyx (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
sage/calculus/calculus.py
r7556 r7571 271 271 import sage.functions.functions 272 272 273 #needed for converting from SymPy to SAGE 274 import sympy 275 273 276 is_simplified = False 274 277 … … 378 381 elif isinstance(x, MaximaElement): 379 382 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_()) 380 386 elif is_Polynomial(x) or is_MPolynomial(x): 381 387 if x.base_ring() != self: # would want coercion to go the other way … … 3845 3851 infixops[self._operator], 3846 3852 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 3847 3870 3848 3871 def _sys_init_(self, system): -
sage/calculus/test_sympy.py
r7525 r7571 124 124 8651*x^8/13440 + 241*x^6/240 + 11*x^4/8 + 3*x^2/2 + 1 125 125 126 127 128 Mixing SymPy with SAGE: 129 sage: import sympy 130 sage: sympy.sympify(var("y"))+sympy.Symbol("x") 131 x + y 132 sage: o = var("omega") 133 sage: s = sympy.Symbol("x") 134 sage: t1 = s + o 135 sage: t2 = o + s 136 sage: print type(t1) 137 <class 'sympy.core.add.Add'> 138 sage: print type(t2) 139 <class 'sage.calculus.calculus.SymbolicArithmetic'> 140 sage: print t1, t2 141 omega + x x + omega 142 sage: e=sympy.sin(var("y"))+sage.all.cos(Symbol("x")) 143 sage: print type(e) 144 <class 'sympy.core.add.Add'> 145 sage: print e 146 cos(x) + sin(y) 147 sage: e=e._sage_() 148 sage: print type(e) 149 <class 'sage.calculus.calculus.SymbolicArithmetic'> 150 sage: print e 151 sin(y) + cos(x) 152 sage: e = sage.all.cos(var("y")**3)**4+var("x")**2 153 sage: e = e._sympy_() 154 sage: print e 155 x**2 + cos(y**3)**4 126 156 """ -
sage/structure/coerce.pyx
r7553 r7571 328 328 return _verify_canonical_coercion_c(x,y) 329 329 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 330 338 raise TypeError, "no common canonical parent for objects with parents: '%s' and '%s'"%(xp, yp) 331 339
Note: See TracChangeset
for help on using the changeset viewer.
