On Fri, Jun 21, 2013 at 5:48 PM, peibol <pabe...@gmail.com> wrote:
> Hi all
>
> I've been searching just to check if what I want to do is feasible with
> sympy. The thing is that I have an expression ('a/b+c/d') for which I want
> to give several sets of values and for each set render the latex without
> evaluating it. I don't know how to avoid automatic evaluation and
> simplification. Any help would be fantastic!
>
>     a,b,c,d = symbols('a b c d')
>     fvars = [a,b,c,d]
>     values = [1,4,2,4];  #example: 1/4+2/4
>     exp=sympify('a/b + c/d');  #here I define the algebraic expression
>     res=nsimplify(exp.evalf(subs = dict(zip(fvars,values))))  #here I get
> the result for later use

Why don't you just use subs here? Are your typical values floats
rather than ints?

>
>     straux=urllib.quote(printing.latex(exp))  #urllib is for making the
> expression URL friendly... the thing us that latex(exp)
>                                                              #I have tried
> exp.subs(a,1...) and eval (making a=1, b=4... and it gives 3/4, not 1/4 +
> 2/4
>
> strchart='http://chart.apis.google.com/chart?cht=tx&chs=140x50&chf=bg,s,FFFFFF00&chl='
> + straux
>
> Thanks!

The classical way to get these expressions to not evaluate is to use
Add(evaluate=False). If all you care about is printing, you can use
this trick

In [25]: (a/b + c/d).subs([(var, Symbol("%s" % i)) for var, i in
zip(fvars, values)])
Out[25]:
1   2
─ + ─
4   4

In other words, use a Symbol with the name of the function you want.
Unfortunately, this still won't preserve the input order, which is
currently impossible to do. I think for printing, though, otherwise
identical expressions are sorted alphabetically (more or less), so if
this is an issue, you could find a reliable pattern, and use the
symbol_names argument to latex(). You could also just use symbol_names
in the first place, instead of creating Symbol('1').

Aaron Meurer

>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+unsubscr...@googlegroups.com.
> To post to this group, send email to sympy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sympy+unsubscr...@googlegroups.com.
To post to this group, send email to sympy@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to