Ticket #4948: trac-4948-mathematica_lists.patch
File trac-4948-mathematica_lists.patch, 8.4 KB (added by , 10 years ago) |
---|
-
sage/interfaces/expect.py
# HG changeset patch # User Felix Lawrence <felix@physics.usyd.edu.au> # Date 1250918481 -36000 # Node ID dc4f1f143c24315eed97c64eadf3e7fba0aee061 # Parent faa0164492450842fad46876f70659e312e3b902 Implemented support for converting mathematica lists and large GP and mathematica floats (i.e. numbers with an exponent) into native sage objects. diff -r faa016449245 -r dc4f1f143c24 sage/interfaces/expect.py
a b 11 11 12 12 - William Stein (2006-03-01): got rid of infinite loop on startup if 13 13 client system missing 14 15 - Felix Lawrence (2009-08-21): edited ._sage_() to support lists and float exponents in foreign notation. 14 16 """ 15 17 16 18 #***************************************************************************** … … 1137 1139 (operator.lt, self._lessthan_symbol()), (operator.le, "<="), 1138 1140 (operator.gt, self._greaterthan_symbol()), (operator.ge, ">=")]) 1139 1141 1142 def _exponent_symbol(self): 1143 """ 1144 Return the symbol used to denote *10^ in floats, e.g 'e' in 1.5e6 1145 """ 1146 return 'e' 1147 1140 1148 ############################################################ 1141 1149 # Functions for working with variables. 1142 1150 # The first three must be overloaded by derived classes, … … 1443 1451 yield self[i] 1444 1452 1445 1453 def __len__(self): 1446 raise NotImplementedError 1454 """ 1455 Call self.sage() and return the length of that sage object. 1456 1457 This approach is inefficient - each interface should override 1458 this method with one that calls the external program's length 1459 function. 1460 1461 EXAMPLES:: 1462 1463 sage: len(gp([1,2,3])) 1464 3 1465 1466 AUTHORS: 1467 1468 - Felix Lawrence (2009-08-21) 1469 """ 1470 return len(self.sage()) 1447 1471 1448 1472 def __reduce__(self): 1449 1473 return reduce_load, (self.parent(), self._reduce()) … … 1536 1560 #print msg 1537 1561 pass 1538 1562 1563 def _sage_repr(self): 1564 """ 1565 Return a sage-friendly string representation of the object. 1566 1567 Some programs use different notation to Sage, e.g. Mathematica 1568 writes lists with {} instead of []. This method calls repr(self) 1569 then converts the foreign notation into Sage's notation. 1570 1571 OUTPUT: 1572 1573 A string representation of the object that is ready for 1574 sage_eval(). 1575 1576 EXAMPLES:: 1577 1578 sage: repr(mathematica([1,2,3])) # optional - mathematica 1579 '{1, 2, 3}' 1580 sage: mathematica([1,2,3])._sage_repr() # optional - mathematica 1581 '[1, 2, 3]' 1582 1583 :: 1584 1585 sage: gp(10.^80)._sage_repr() 1586 '1.0000000000000000000000000000000000000e80' # 64-bit 1587 sage: mathematica('10.^80')._sage_repr() # optional - mathematica 1588 '1.e80' 1589 1590 AUTHORS: 1591 1592 - Felix Lawrence (2009-08-21) 1593 """ 1594 #TO DO: this could use file transfers when self.is_remote() 1595 1596 string = repr(self).replace('\n',' ').replace('\r', '') 1597 # Translate the external program's list formatting to Sage's 1598 lld = self.parent()._left_list_delim() 1599 if '[' != lld: string = string.replace(lld, '[') 1600 rld = self.parent()._right_list_delim() 1601 if ']' != rld: string = string.replace(rld, ']') 1602 # Translate the external program's exponent formatting 1603 expl = self.parent()._exponent_symbol() 1604 if 'e' != expl: string = string.replace(expl, 'e') 1605 return string 1606 1539 1607 def _sage_(self): 1540 1608 """ 1541 1609 Attempt to return a Sage version of this object. … … 1548 1616 1/2 1549 1617 sage: _.parent() 1550 1618 Rational Field 1619 1620 AUTHORS: 1621 1622 - William Stein 1623 1624 - Felix Lawrence (2009-08-21) 1551 1625 """ 1552 #TO DO: this could use file transfers when self.is_remote()1553 1626 try: 1554 return sage.misc.sage_eval.sage_eval( repr(self).replace('\n',''))1627 return sage.misc.sage_eval.sage_eval(self._sage_repr()) 1555 1628 except: 1556 1629 raise NotImplementedError 1557 1630 -
sage/interfaces/gp.py
diff -r faa016449245 -r dc4f1f143c24 sage/interfaces/gp.py
a b 553 553 """ 554 554 return '==' 555 555 556 def _exponent_symbol(self): 557 """ 558 Returns the symbol to denote the exponent of a number in GP. 559 560 EXAMPLES:: 561 562 sage: gp._exponent_symbol() 563 ' E' 564 565 :: 566 567 sage: repr(gp(10.^80)).replace(gp._exponent_symbol(), 'e') 568 '1.0000000000000000000000000000000000000e80' # 64-bit 569 """ 570 return ' E' 571 556 572 def help(self, command): 557 573 r""" 558 574 Returns GP's help for ``command``. -
sage/interfaces/mathematica.py
diff -r faa016449245 -r dc4f1f143c24 sage/interfaces/mathematica.py
a b 179 179 180 180 sage: n = mathematica(2434500) # optional - mathematica 181 181 sage: n.FactorInteger() # optional - mathematica 182 {{2, 2}, {3, 2}, {5, 3}, {541, 1}} # optional - mathematica182 {{2, 2}, {3, 2}, {5, 3}, {541, 1}} 183 183 sage: n = mathematica(2434500) # optional - mathematica 184 184 sage: F = n.FactorInteger(); F # optional - mathematica 185 185 {{2, 2}, {3, 2}, {5, 3}, {541, 1}} … … 234 234 ... return mathematica(nu).BesselK(x).N(20).sage() 235 235 ... 236 236 sage: math_bessel_K(2,I) # optional - mathematica 237 0.180489972066962*I - 2.592886175491197 237 0.180489972066962*I - 2.592886175491197 # 32-bit 238 -2.592886175491196978 + 0.1804899720669620266*I # 64-bit 239 240 :: 241 242 sage: slist = [[1, 2], 3., 4 + I] 243 sage: mlist = mathematica(slist); mlist # optional - mathematica 244 {{1, 2}, 3., 4 + I} 245 sage: slist2 = list(mlist); slist2 # optional - mathematica 246 [{1, 2}, 3., 4 + I] 247 sage: slist2[0] # optional - mathematica 248 {1, 2} 249 sage: slist2[0].parent() # optional - mathematica 250 Mathematica 251 sage: slist3 = mlist.sage(); slist3 # optional - mathematica 252 [[1, 2], 3.00000000000000, I + 4] 253 254 :: 255 256 sage: mathematica('10.^80') # optional - mathematica 257 1.*^80 258 sage: mathematica('10.^80').sage() # optional - mathematica 259 1.00000000000000e80 238 260 239 261 AUTHORS: 240 262 241 263 - William Stein (2005): first version 242 264 243 265 - Doug Cutrell (2006-03-01): Instructions for use under Cygwin/Windows. 266 267 - Felix Lawrence (2009-08-21): Added support for importing Mathematica lists and floats with exponents. 244 268 """ 245 269 246 270 #***************************************************************************** … … 460 484 def _assign_symbol(self): 461 485 return ":=" 462 486 487 def _exponent_symbol(self): 488 """ 489 Returns the symbol used to denote the exponent of a number in 490 Mathematica. 491 492 EXAMPLES:: 493 494 sage: mathematica._exponent_symbol() # optional - mathematica 495 '*^' 496 497 :: 498 499 sage: bignum = mathematica('10.^80') # optional - mathematica 500 sage: repr(bignum) # optional - mathematica 501 '1.*^80' 502 sage: repr(bignum).replace(mathematica._exponent_symbol(), 'e').strip() # optional - mathematica 503 '1.e80' 504 """ 505 return "*^" 506 463 507 def _object_class(self): 464 508 return MathematicaElement 465 509 … … 513 557 P = self._check_valid() 514 558 return P.get(self._name, ascii_art=True) 515 559 560 def __len__(self): 561 """ 562 Return the object's length, evaluated by mathematica. 563 564 EXAMPLES:: 565 566 sage: len(mathematica([1,1.,2])) # optional - mathematica 567 3 568 569 AUTHORS: 570 - Felix Lawrence (2009-08-21) 571 """ 572 return self.Length() 573 516 574 def show(self, filename=None, ImageSize=600): 517 575 r""" 518 576 Show a mathematica expression or plot in the Sage notebook.