Hi Viny,
Sage's mathematica support at present seems to be geared towards
sending data to mathematica and printing the results to the screen,
rather than getting mathematica's results back into sage for data
manipulation. So the going may be a little rough.
Sage can convert its own arrays into mathematica's notation, e.g.
Jason's example
sage: b=mathematica([1,2,3])
will create a mathematica variable representing the array {1,2,3},
accessible from sage. I'm not sure whether you can send mathematica
more complicated things such as functions in this fashion.
Once you've got your variables into mathematica, you perform
mathematica functions on them like so:
sage: x = mathematica(1)
sage: mathematica.eval('myfunc[x_,y_]:=x+y')
sage: x.myfunc(2)
3
It's hard to get your mathematica results back into sage for use. As
Jason noted below, mathematica arrays with {} don't get converted back
into sage arrays with []. There is a ticket related to the array
problem <http://trac.sagemath.org/sage_trac/ticket/4948>, but there
seems to be no progress on it.
I gave up and rolled my own mathematica list to numpy array conversion
- you might find it useful. I believe that using eval() is frowned
upon for security reasons. If you want sage arrays rather than numpy
arrays, then use sage_eval() (which is considerably slower for large
arrays) and strip out the numpy stuff. Comments on the below code are
welcome.
import numpy
import re
def mma_to_numpy(mma_arr):
"""Convert mathematica array to numpy array."""
# Convert the mathematica object to a string
string = repr(mma_arr)
# Convert mathematica-style {} to python style []
string = string.replace('{','[').replace('}',']')
# Replace mathematica's crazy exponent notation with the standard
one
string = string.replace('*^', 'e')
# Delete new lines (both \n and \r\n appear sporadically)
string = string.replace('\n', ' ').replace('\r', ' ')
# Detect characters that should not be in an array
if re.findall(r'[a-df-zA-HJ-Z]+', string) != []:
raise TypeError, "Mathematica returns non-array: %s" % string
# Read the string into numpy, interpreting I as j
return eval(string, {'I': numpy.complex(0,1)})
You use it as such:
sage: b = mathematica([1.5,2.+3.j,3])
sage: b
{1.5, 2. + 3.*I, 3}
sage: numpy.array(mma_to_numpy(b), dtype='D')
array([ 1.5+0.j, 2.0+3.j, 3.0+0.j])
Hope that this helps - there isn't much documentation on this stuff.
Cheers,
Felix
On Aug 13, 8:26 pm, Viny <[email protected]> wrote:
> On 13 août, 10:47, Viny <[email protected]> wrote:
>
> > Hi,
>
> > I have a problem of memory into maxima when it performs a symbolic
> > expression of an hessian matrix. This symbolic expression of the
> > hessian seems to be too big. My sage program is written using python.
> > And i want to compute the hessian matrix with mathematica 6.0 in my
> > sage file.
> > I need to compute symbolic expresstion of the gradient and the hessian
> > with mathematica and after compute the mathématica LinearSolve
> > function to solve hessian^-1*grad.
> > How to make this interface in my .sage?
> > Please help me!!!
>
> > I have mathematica 6.0 and sage4.0.2 in my linux 64bit system.
>
> This is an example of the function to compute the gradient in
> my .sage, but it doesn't work.
> def grad(f,v):
> return mathematica ( f ).D( [ ' v ',1] )
> The result is {0,0} . I have another problem of format, i don't know
> the command to change { } in () in the mathematica result {0,0}
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
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://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---