Hello everybody
I wrote the following :
++++++++++++++++++++++++++
#! /usr/bin/sage -python
# -*- coding: utf8 -*-
from sage.all import *
s = "x**2"
var('x')
f = sage_eval(s)
print f(2)
++++++++++++++++++++++++++
I would have expected the result to print 4.
Since I'm using Sage from yesterday, I'm not really surprised to get
an error :
Traceback (most recent call last):
File "./ecm.py", line 8, in <module>
f = sage_eval(s)
File "/usr/lib/python2.5/site-packages/sage/misc/sage_eval.py", line
110, in sage_eval
return eval(p, sage.all.__dict__, locals)
File "<string>", line 1, in <module>
NameError: name 'x' is not defined
My question is the following. I have a string (in the sense of python)
which contains some "x", and I want to convert it into a function on
which I can use the methods diff, integrate, eval, ...
How to do it ?
I also need to do the reverse : I have a function defined by
var('x')
f = x**2
and I want the string "x**2" on which I can perform all python
manipulations of strings.
[Context]
My aim is to write a script which produce the pstricks code for a
figure to be inserted in a LaTeX file. The script computes the
bounding box, intersections of curves, ...
Right now, my script uses Maxima as backend for the computations; I'm
doing ugly thinks like
commands.getoutput("maxima --batch-string=\"display2d:false; "+"diff
("+fx+",x)"+";\"")
and then, thousand of manipulations of string in order to extract the
result from the output of Maxima.
So now, I want to modernize my script and make no more explicit
appeals to Maxima and use the Sage's python module.
Up to now, I defined a class Fonction which is build from a string
like "sin(x)" and which allows me to produce pstricks code for plot
the function, put the axes, compute the bounding box, ...
In order to use Sage as backend, I need two thinks :
1. be able to convert a string into a function on which I can use Sage
2. from the results of Sage (especially from diff(f,x)), I need a
string that I can manipulate with Python (especially use the .replace
method in order to produce strings that are valid code for pstricks).
[/context]
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---