Changeset 7522:c89495064898
- 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
r7519 r7522 269 269 import sage.functions.functions 270 270 271 #needed for converting from SymPy to SAGE 272 import sympy 273 271 274 is_simplified = False 272 275 … … 376 379 elif isinstance(x, MaximaElement): 377 380 return symbolic_expression_from_maxima_element(x) 381 # if "x" is a SymPy object, convert it to a SAGE object 382 elif isinstance(x, sympy.Basic): 383 return self(x._sage_()) 378 384 elif is_Polynomial(x) or is_MPolynomial(x): 379 385 if x.base_ring() != self: # would want coercion to go the other way … … 3705 3711 infixops[self._operator], 3706 3712 ops[1]._maxima_init_()) 3713 3714 def _sympy_(self): 3715 """Converts any expression to SymPy.""" 3716 3717 # Current implementation is fragile - it first converts the expression 3718 # to string, then preparses it, then gets rid of "Integer" and then 3719 # sympifies this string. 3720 3721 # In order to make this robust, one would have to implement _sympy_ 3722 # recursively in all expressions. But we want something now, instead of 3723 # tomorrow, so the following one-liner does the job for now. 3724 # Also all ugly things are concentrated in this line, everything else 3725 # (sympy.sympify, sage.all.SR, ...) is clean and robust. 3726 import sympy 3727 from sage.all import preparse 3728 s = sympy.sympify(preparse(repr(self)).replace("Integer","")) 3729 return s 3707 3730 3708 3731 def _sys_init_(self, system): -
sage/calculus/test_sympy.py
r7436 r7522 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
r7246 r7522 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.
