[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread William Stein

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)
> > 
> > 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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread didier deshommes

William,
Would you mind making this a blog post? This is a great reference of
how interfaces work in Sage.

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)
> 
> 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
>
> >
>

--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread William Stein

On Dec 13, 2007 11:47 AM, pgdoyle <[EMAIL PROTECTED]> wrote:
>
>
> > def math_bessel_K(nu,x):
> >return mathematica(nu).BesselK(x).N(20).sage()
> >
> > Use it:
> >
> > sage: math_bessel_K(2,I)
> > 0.180489972066962*I - 2.592886175491197
>
> This is incredibly cool!  I can't believe I missed this in the Sage
> reference manual, but there it is.  In wonder how it works?

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)

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

--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread pgdoyle


> def math_bessel_K(nu,x):
>return mathematica(nu).BesselK(x).N(20).sage()
>
> Use it:
>
> sage: math_bessel_K(2,I)
> 0.180489972066962*I - 2.592886175491197

This is incredibly cool!  I can't believe I missed this in the Sage
reference manual, but there it is.  In wonder how it works?

Cheers,

Peter
--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread William Stein

On Dec 13, 2007 11:54 AM, Timothy Clemans <[EMAIL PROTECTED]> wrote:
>
> I'm getting
>
> {{{id=2|
> def math_bessel_K(nu,x):
>return mathematica(nu).BesselK(x).N(20).sage()
> math_bessel_K(2,I)
> ///
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/home/tclemans/.sage/sage_notebook/worksheets/admin/5/code/
> 9.py", line 6, in 
> exec compile(ur'math_bessel_K(Integer(2),I)' + '\n', '', 'single')
>   File "/home/was/s/data/extcode/sage/", line 1, in 
>
>   File "/home/tclemans/.sage/sage_notebook/worksheets/admin/5/code/
> 9.py", line 5, in math_bessel_K
> return mathematica(nu).BesselK(x).N(Integer(20)).sage()
>   File "/home/was/s/local/lib/python2.5/site-packages/sage/interfaces/
> expect.py", line 1086, in sage
> return self._sage_()
>   File "/home/was/s/local/lib/python2.5/site-packages/sage/interfaces/
> expect.py", line 1079, in _sage_
> return sage.misc.sage_eval.sage_eval(repr(self))
>   File "/home/was/s/local/lib/python2.5/site-packages/sage/misc/
> sage_eval.py", line 112, in sage_eval
> raise SyntaxError, "%s\nError using SAGE to evaluate '%s'"%(msg,
> p)
> SyntaxError: invalid syntax (, line 1)
> Error using SAGE to evaluate '-
> RealNumber('2.592886175491196978167651322538251462935637034451900356688')
> +
>
> RealNumber('0.180489972066962026629620880838378650496225604668529521981')*I'
> }}}

That's coming from a newline, which appears in Mathematica 5 I guess,
but not 6.  Anyway,
the patch I justed posted at

  http://trac.sagemath.org/sage_trac/ticket/1491

fixes the problem.  Alternatively rewrite the function like this:

sage: def math_bessel_K(nu,x):
  return sage_eval(repr(mathematica(nu).BesselK(x).N(20)).replace('\n',''))

Important note: The conversion from Mathematica to Sage, i.e.,
mathematica_object.sage()
is very naive still -- all it is is basically sage_eval(repr(...))!
This could/would be improved
if somebody cared a lot.

William

--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread Timothy Clemans

I'm getting

{{{id=2|
def math_bessel_K(nu,x):
   return mathematica(nu).BesselK(x).N(20).sage()
math_bessel_K(2,I)
///
Traceback (most recent call last):
  File "", line 1, in 
  File "/home/tclemans/.sage/sage_notebook/worksheets/admin/5/code/
9.py", line 6, in 
exec compile(ur'math_bessel_K(Integer(2),I)' + '\n', '', 'single')
  File "/home/was/s/data/extcode/sage/", line 1, in 

  File "/home/tclemans/.sage/sage_notebook/worksheets/admin/5/code/
9.py", line 5, in math_bessel_K
return mathematica(nu).BesselK(x).N(Integer(20)).sage()
  File "/home/was/s/local/lib/python2.5/site-packages/sage/interfaces/
expect.py", line 1086, in sage
return self._sage_()
  File "/home/was/s/local/lib/python2.5/site-packages/sage/interfaces/
expect.py", line 1079, in _sage_
return sage.misc.sage_eval.sage_eval(repr(self))
  File "/home/was/s/local/lib/python2.5/site-packages/sage/misc/
sage_eval.py", line 112, in sage_eval
raise SyntaxError, "%s\nError using SAGE to evaluate '%s'"%(msg,
p)
SyntaxError: invalid syntax (, line 1)
Error using SAGE to evaluate '-
RealNumber('2.592886175491196978167651322538251462935637034451900356688')
+
 
RealNumber('0.180489972066962026629620880838378650496225604668529521981')*I'
}}}

On Dec 13, 11:21 am, "William Stein" <[EMAIL PROTECTED]> wrote:
> On Dec 13, 2007 11:08 AM, pgdoyle <[EMAIL PROTECTED]> wrote:
>
>
>
> > Let me simplify the question.
>
> > Is there a better way to get Mathematica to go off and compute a
> > Bessel function for me than this:
>
> > def math_bessel_K(nu,x):
> >   m=mathematica('N[BesselK['+str(mathematica(nu))
> > +','+str(mathematica(x))+'],20]')
> >   return m.sage()
>
> Yes there is, and you might even find this impressive:
>
> def math_bessel_K(nu,x):
>return mathematica(nu).BesselK(x).N(20).sage()
>
> Use it:
>
> sage: math_bessel_K(2,I)
> 0.180489972066962*I - 2.592886175491197
--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread William Stein

On Dec 13, 2007 11:08 AM, pgdoyle <[EMAIL PROTECTED]> wrote:
>
> Let me simplify the question.
>
> Is there a better way to get Mathematica to go off and compute a
> Bessel function for me than this:
>
> def math_bessel_K(nu,x):
>   m=mathematica('N[BesselK['+str(mathematica(nu))
> +','+str(mathematica(x))+'],20]')
>   return m.sage()

Yes there is, and you might even find this impressive:

def math_bessel_K(nu,x):
   return mathematica(nu).BesselK(x).N(20).sage()

Use it:

sage: math_bessel_K(2,I)
0.180489972066962*I - 2.592886175491197

--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---



[sage-support] Re: Subcontracting to Mathematica

2007-12-13 Thread pgdoyle

Let me simplify the question.

Is there a better way to get Mathematica to go off and compute a
Bessel function for me than this:

def math_bessel_K(nu,x):
  m=mathematica('N[BesselK['+str(mathematica(nu))
+','+str(mathematica(x))+'],20]')
  return m.sage()

Cheers,

Peter
--~--~-~--~~~---~--~~
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/
-~--~~~~--~~--~--~---