We're trying to find a way to get sage to generate latex
representation of numeric expressions without fully evaluating them.
For example this would allow us to do something like render 1+5 in
latex as $1+5$ instead of 6.
Can someone tell us if it's possible to achieve this goal ? Maybe by
doing something like
var('a')
var('b')
sage_exp("a + b", locals={'a': 1, 'b': 5})
This may require adding new code.
I prefer to discuss design upfront. I am new to the project.
I've been looking a bit at the sage.symbolic.expression class.
Pointers appreciated.
Cheers,
Jerome
Our background:
*******************
We've been experimenting with an exercise generator for math students.
Target age: <=18 years old at start.
Our first version of the program uses a custom language and a ply (lex/
yacc) parser. Our exercises are dynamic with some randomness for both
variables and operations. Example of an exercise:
id0
"Compute"
a (1;5)
b (-5;-1)
s \sign1
solve a s (b)
solution \res {a s (b)}
(add or substract 2 numbers)
or
id21
"Write the answer in standardform"
a, c, e, g \d(1;10)
b, d, f, h (-4;4)\0
solve \frac {\res {\decform{a*10^b}} * \res {\decform {c*10^d}}}{\res
{\decform{e*10^f}} * \res {\decform{g*10^h}}}
solution \stdform {\res {\frac {\res {a*10^b} * \res {c*10^d}}{\res
{e*10^f} * \res {g*10^h}}}}
For our second prototype, we've been exploring alternative designs, in
particular using Domain Specific Languages instead of a parser. Here
comes sage.
Sage seems great for everything that is advanced maths. But is harder
to use when it comes to simple numeric expressions. As the expressions
are evaluated right away, I am unable to render 1+5 in latex. It gets
printed as 6 instead of $1+5$
Other example: 2/4 + 1/4 should be $\frac{2}{4} + \frac{1}{4}$ and not
$\frac{1}{2} + \frac{1}{4}
On Mon, 8 Feb 2010 02:51:31 -0800 (PST)
Jerome Lacoste <jerome....@gmail.com> wrote:
> Hi all,
>
> We're trying to find a way to get sage to generate latex
> representation of numeric expressions without fully evaluating them.
>
> For example this would allow us to do something like render 1+5 in
> latex as $1+5$ instead of 6.
> Can someone tell us if it's possible to achieve this goal ? Maybe by
> doing something like
>
> var('a')
> var('b')
> sage_exp("a + b", locals={'a': 1, 'b': 5})
>
> This may require adding new code.
>
> I prefer to discuss design upfront. I am new to the project.
> I've been looking a bit at the sage.symbolic.expression class.
>
> Pointers appreciated.
I have a patch which allows you to "hold" automatic evaluation of
most symbolic expressions. It doesn't work for addition and
multiplication yet though. This would let you do
sage: SR(1).add(1, hold=True)
1 + 1
or when we implement contexts
sage: with hold:
....: t = SR(1) + 1; t # SR(1) is not necessary if we decide to
....: # make everything a symbolic expression
....: # in the hold context
....:
sage: t
1 + 1
Unfortunately, the library we use for the symbolics, pynac, has the
automatic evaluation of add and mul objects built into the class
constructors [1, 2]. Changing this behavior means adding a parameter to
the constructor to delay this evaluation/normalization. If you're only
going to use these expressions for printing, this should be enough.
However, if you compute with them further, there will be lots of subtle
problems, since basic assumptions about the data structure is violated.
[1] http://pynac.sagemath.org/hg/file/9ff767fb0c18/ginac/add.cpp#l63
[2]
http://pynac.sagemath.org/hg/file/9ff767fb0c18/ginac/expairseq.cpp#l801
One can overcome this limitation by keeping a flag and performing the
auto evaluation when necessary. I haven't had time to implement this
yet. I would be glad to rebase my patch to the latest Sage version and
make it available so you can build on it, if you think it will be
helpful. I don't think I'll have the time to make the necessary changes
in pynac myself at least for the next 2 weeks.
Cheers,
Burcin
What about a syntax like:
sage: var('a,b', hold=True)
sage: (a+b).subs(a=5,b=1)
1+5
where the "hold" attribute on a variable indicates that no
simplifications should be done with that variable, until some special
compute function like evaluate or something is called that specifically
ignores the holds.
(a+b).subs(a=5,b=1).evaluate()
6
Thanks,
Jason
--
Jason Grout
Just for the record: you might be interested in (or already be aware
of ) WIMS, which is an web-based interactive exercise server (with
randomness, and a huge collection of exercises):
Best,
Nicolas
--
Nicolas M. Thi�ry "Isil" <nth...@users.sf.net>
http://Nicolas.Thiery.name/