Hello everybody!
Thanks for your help.
I'm realizing that i'm definitely really bad in sage, perhaps i'll be
best in another life!!lol!
This is a piece of a code for changing a symbolic mathematica list
expression to a sage list expression:
def mmatosage(mma_list):
# Convert the mathematica object to a string
string = repr(mma_list)
# Convert mathematica-style {} to python style []
string = string.replace('{','[').replace('}',']')
# Replace mathematica's crazy exponent notation with the standard
one
string = string.replace('^', '**')
# Delete new lines (both \n and \r\n appear sporadically)
string = string.replace('\n', ' ').replace('\r', ' ')
sage_stuff = sage_eval(string,locals =
{'E':'E','x':'x','y':'y'},preparse=False,)
return sage_stuff
I get the folowing error when I apply it to this mathematica
expression {((1 + E^x^2)*x)/(1 + E^x^2 + 2*E^x^2*x^2),
((-1 + E^y^2)*y)/(-1 + E^y^2 + 2*E^y^2*y^2)}
---> 41 sage_stuff = sage_eval(string,locals =
{'E':'E','x':'x','y':'y'},preparse=False)
42 return sage_stuff
/usr/local/src/sage-4.0.2/local/lib/python2.5/site-packages/sage/misc/
sage_eval.pyc in sage_eval(source, locals, cmds, preparse)
197 return locals['_sage_eval_returnval_']
198 else:
--> 199 return eval(source, sage.all.__dict__, locals)
200
201
/usr/local/src/sage-4.0.2/local/lib/python2.5/site-packages/sage/
all.pyc in <module>()
TypeError: unsupported operand type(s) for ** or pow(): 'str' and
'int'
I have any idea.
Thankx for your help!
On 15 août, 03:09, Marshall Hampton <[email protected]> wrote:
> Oh right, sorry. I always do that and then remember after the syntax
> error...
>
> On Aug 14, 6:35 pm, William Stein <[email protected]> wrote:
>
> > On Fri, Aug 14, 2009 at 4:32 PM, Marshall Hampton<[email protected]>
> > wrote:
>
> > > You probably have to pass in the "dx" as a local variable, i.e. do
> > > something like
>
> > > sage_stuff = sage_eval(expr, locals = {'dx' = 'dx'})
>
> > It's {'dx':'dx'} or something like that. Definitely not 'dx' = 'dx'.
>
> > > where expr is your mathematica expression.
>
> > > -Marshall Hampton
>
> > > On Aug 14, 1:49 pm, Viny <[email protected]> wrote:
> > >> Thanks for your help Felix, with your ideas i made progresses. But the
> > >> problem isn't fixe yet. Since i have some symbolic variable such 'dx'
> > >> in my expressions, the
> > >> code's line --> 500 return eval(string, {'I': numpy.complex(0,1)})
> > >> generate this folowing error
> > >> NameError: name 'dx' is not defined
> > >> Even when i put in the begining of my function var ("dx"), the problem
> > >> stay.
>
> > >> Thanks again
>
> > >> On 14 août, 04:04, thelamecamel <[email protected]> wrote:
>
> > >> > 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}
>
> > --
> > William Stein
> > Associate Professor of Mathematics
> > University of Washingtonhttp://wstein.org
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---