On Dec 13, 2007 5:28 PM, didier deshommes <[EMAIL PROTECTED]> wrote: > > William, > Would you mind making this a blog post? This is a great reference of > how interfaces work in Sage.
Great idea! Done: http://sagemath.blogspot.com/ > > didier > > 2007/12/13, William Stein <[EMAIL PROTECTED]>: > > > I figured out a clean way to have this sort of notation work in > > Mathematica etc. rom Sage when > > I was hiking with my wife in Sedona to Vultee Arch a couple years ago. > > Basically when you do say > > s = mathematica(nu) > > Sage converts nu into a Mathematica-readable string by calling > > nu._mathematica_init_(): > > > > sage: nu = sqrt(2) + I*sin(3) > > sage: nu._mathematica_init_() > > '(Sqrt[2]) + ((I) * (Sin[3]))' > > > > Note that _mathematica_init_ on the nu in the above example calls > > other _mathematica_init_'s, > > e.g., sqrt(2) has a mathematica_init_ method that calls that > > _mathematica_init_ method for 2, etc. > > Mathematica itself is not used at all yet. Incidentally, just like > > you can latex expressions, e.g., > > > > sage: latex(nu) > > {i \cdot \sin \left( 3 \right)} + \sqrt{ 2 } > > > > for inclusion in a paper or something, you can also mathematica them > > as above, to share > > with mathematica users... > > > > Anyway Sage sends this string to mathematica: > > > > 'sage0 := (Sqrt[2]) + ((I) * (Sin[3]))' > > > > Mathematica evaluates it and Sage creates a Python wrapper object, > > which knows the variable > > name 'sage0': > > > > sage: s = mathematica(nu) > > sage: s.name( ) > > 'sage0' > > sage: type(s) > > <class 'sage.interfaces.mathematica.MathematicaElement'> > > sage: s > > Sqrt[2] + I*Sin[3] > > > > Now there is a running instance of mathematica, and it has an object > > defined in it > > called sage0, which is equal to our nu: > > > > sage: mathematica.eval('sage0') > > Sqrt[2] + I Sin[3] > > > > > > You can even play around with that copy of Mathematica more directly > > from the command > > line (or with %mathematica in a cell in the Sage notebook): > > > > sage: %mathematica > > > > --> Switching to Mathematica <-- > > > > '' > > mathematica: sage0 > > Sqrt[2] + I*Sin[3] > > mathematica: Sqrt[sage0] > > Sqrt[Sqrt[2] + I*Sin[3]] > > mathematica: N[sage0] > > 1.4142135623730951 + 0.1411200080598672*I > > > > [[Note this is like the Mathematica command line but better since it > > has readline support, etc.]] > > > > mathematica: %sage > > --> Exiting back to SAGE <-- > > > > Finally, if you want to call a function and give the version > > of nu that is in Mathematica as an argument, you just call > > the function on it using Pythonic notation: > > > > sage: s.Sqrt() > > Sqrt[Sqrt[2] + I*Sin[3]] > > sage: s.N() > > 1.4142135623730951 + 0.1411200080598672*I > > > > What happens here is that say s.Sqrt creates a Python class that > > wraps the Mathematica function Sqrt with argument s not yet evaluated, > > so even this works: > > > > sage: s.Sqrt? > > Docstring: > > Sqrt[z] or Sqrt[z] gives the square root of z. > > > > When we did s.Sqrt? Sage queries Mathematica for how Sqrt works. > > > > Everything above applies equally to the Maple, Matlab, Maxima, PARI, > > Singular, etc., interfaces. There is *much* that can be done to make > > the Mathematica interface in particular even more cool. Ideas appreciated! > > > > By the way, it snowed very nicely on the hike, and my wife and I had a > > great time. > > > > -- William > > > > > > > > > > > -- William Stein Associate Professor of Mathematics University of Washington http://wstein.org --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://sage.math.washington.edu/sage/ and http://sage.scipy.org/sage/ -~----------~----~----~----~------~----~------~--~---