Say I want to get Mathematica to compute some function that Sage can't compute for me. What is the best way to pipe the arguments into Mathematica, and then get the answer back into the world of sage?
Here's what I tried: sage: def math_bessel_K(nu,x): ... m=mathematica('N[BesselK['+str(mathematica(nu)) +','+str(mathematica(x))+'],20]') ... return m.sage() ... This seems to work: See the appended output. But I wonder if it's really necessary to do the conversion to a string and then the string concatenations. Maybe there is a more direct way to get the arguments into Mathematica? Also, I wonder about the type when the result is a complex number: Do I really want the result to have type <class 'sage.calculus.calculus.SymbolicArithmetic'>? I presume I want to get the result into a standard Sage type, rather than having it maintain its connection with Mathematica. But would I be better off converting it to some other format, e.g. some kind of complex number that takes account of the the precision that the result carries? But, maybe there is already a general way of subcontracting a computation like this to Mathematica, or Maple, or Matlab, or whatever? Finally, note that Sage's bessel_K gets very close to computing the result for me. Cheers, Peter ------------ sage: def bessel_comp(nu,x): ... m=math_bessel_K(nu,x) ... print m ... print type(m) ... print ... p=pari(nu).besselk(x) ... print p ... print type(p) ... print ... s=bessel_K(nu,x,100) ... print ... print type(s) sage: bessel_comp(2,3) 0.061510458471742037657 <type 'sage.rings.real_mpfr.RealNumber'> 0.06151045847174203765682007145288 <type 'sage.libs.pari.gen.gen'> 0.061510458471742037656820071453 <type 'sage.rings.real_mpfr.RealNumber'> sage: bessel_comp(2,I) 0.180489972066962 I - 2.592886175491197 <class 'sage.calculus.calculus.SymbolicArithmetic'> -2.592886175491196978167660649170 + 0.1804899720669620266296208813982*I <type 'sage.libs.pari.gen.gen'> Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 88.py", line 4, in <module> exec compile(ur'bessel_comp(Integer(2),I)' + '\n', '', 'single') File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ sage/", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 86.py", line 16, in bessel_comp s=bessel_K(nu,x,Integer(100)) File "/usr/local/sage/local/lib/python2.5/site-packages/sage/ functions/special.py", line 526, in bessel_K b = RR(pari(nu).besselk(z)) File "real_mpfr.pyx", line 244, in sage.rings.real_mpfr.RealField.__call__ File "real_mpfr.pyx", line 673, in sage.rings.real_mpfr.RealNumber._set TypeError: Unable to convert x (='-2.592886175491196978167660649170+0.1804899720669620266296208813982*I') to real number. sage: bessel_comp(2,3+I) 0.0152759650615147 - 0.0554717495270565 I <class 'sage.calculus.calculus.SymbolicArithmetic'> 0.01527596506151469555940906153650 - 0.05547174952705649444323133156426*I <type 'sage.libs.pari.gen.gen'> Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 89.py", line 4, in <module> exec compile(ur'bessel_comp(Integer(2),Integer(3)+I)' + '\n', '', 'single') File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ sage/", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 86.py", line 16, in bessel_comp s=bessel_K(nu,x,Integer(100)) File "/usr/local/sage/local/lib/python2.5/site-packages/sage/ functions/special.py", line 526, in bessel_K b = RR(pari(nu).besselk(z)) File "real_mpfr.pyx", line 244, in sage.rings.real_mpfr.RealField.__call__ File "real_mpfr.pyx", line 673, in sage.rings.real_mpfr.RealNumber._set TypeError: Unable to convert x (='0.01527596506151469555940906153650-0.05547174952705649444323133156426*I') to real number. sage: bessel_comp(2+I,3) 0.0289192946582081 I + 0.0455907718407551 <class 'sage.calculus.calculus.SymbolicArithmetic'> 0.04559077184075505871203211093879 + 0.02891929465820812820828883525761*I <type 'sage.libs.pari.gen.gen'> Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 90.py", line 4, in <module> exec compile(ur'bessel_comp(Integer(2)+I,Integer(3))' + '\n', '', 'single') File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ sage/", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 86.py", line 16, in bessel_comp s=bessel_K(nu,x,Integer(100)) File "/usr/local/sage/local/lib/python2.5/site-packages/sage/ functions/special.py", line 526, in bessel_K b = RR(pari(nu).besselk(z)) File "real_mpfr.pyx", line 244, in sage.rings.real_mpfr.RealField.__call__ File "real_mpfr.pyx", line 673, in sage.rings.real_mpfr.RealNumber._set TypeError: Unable to convert x (='0.04559077184075505871203211093879+0.02891929465820812820828883525761*I') to real number. sage: bessel_comp(2+I,3+I) 0.0431928272695873 - 0.0400595380665329 I <class 'sage.calculus.calculus.SymbolicArithmetic'> 0.04319282726958726692927951697209 - 0.04005953806653287581272798713877*I <type 'sage.libs.pari.gen.gen'> Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 91.py", line 4, in <module> exec compile(ur'bessel_comp(Integer(2)+I,Integer(3)+I)' + '\n', '', 'single') File "/usr/local/sage-2.8.15-ubuntu32bit-i686-Linux/data/extcode/ sage/", line 1, in <module> File "/home/doyle/.sage/sage_notebook/worksheets/admin/5/code/ 86.py", line 16, in bessel_comp s=bessel_K(nu,x,Integer(100)) File "/usr/local/sage/local/lib/python2.5/site-packages/sage/ functions/special.py", line 526, in bessel_K b = RR(pari(nu).besselk(z)) File "real_mpfr.pyx", line 244, in sage.rings.real_mpfr.RealField.__call__ File "real_mpfr.pyx", line 673, in sage.rings.real_mpfr.RealNumber._set TypeError: Unable to convert x (='0.04319282726958726692927951697209-0.04005953806653287581272798713877*I') to real number. --~--~---------~--~----~------------~-------~--~----~ 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/ -~----------~----~----~----~------~----~------~--~---