[SciPy-user] evaluating a really long, multiline expression
David M. Cooke
cookedm at physics.mcmaster.ca
Wed Nov 23 16:48:32 CST 2005
Ryan Krauss <ryanlists at gmail.com> writes:
> I have a really long symbolic expression that is being generated by
> Maxima and output to a text file. I need to turn this into a python
> function that I will use in optimize.newton and optimize.fmin. It is
> almost 9000 lines and the first 10 lines look like this :
>
> bv3 = (s^5*(abeam^2*betabeam^2*c3beam
> *(ml2*(rl2*(Kact*kj2
> *(ml1
> *((ml0
> *(2*kbase*Lbeam*Ll0*rl0
> -2*kbase*Lbeam*rl0^2)
> -2*Izl0*kbase*Lbeam-2*cbase*cj1*Lbeam)
> *rl1^2
> +Ll1
> *((ml0
> *(2*kbase*Lbeam*rl0^2
> -2*kbase*Lbeam*Ll0*rl0)
> +2*Izl0*kbase*Lbeam+2*cbase*cj1*Lbeam)
>
> Aside from replacing the ^ with ** and needing to get rid of the
> un-python-ish spacing at the front, any thoughts on how to make python
> evalute this expression quickly (assuming that all the variables are
> assigned numeric values by the function). I am a little nervous that
> such a long expression will take a really long time to evaluate. But
> I haven't tried it yet. Any thoughts on parsing this thing? am I
> better off leaving it a 9000 line statement or making one really long
> line (I think about 300,000 characters)? Is there any speed trade off
> with multiline commands?
There might be a slight difference in time in loading the file between
multiline and one line, but there would be no time difference in
evaluating the expression. Editors deal better with things on multiple
lines, though.
Does Maxima have some method of rewriting expressions in terms of a
sequence of expressions, pulling out common subcomputations? Maple has
this:
> ex := expand((x+y)^5);
5 4 3 2 2 3 4 5
ex := x + 5 x y + 10 x y + 10 x y + 5 x y + y
> codegen[optimize](ex);
2 2 2 2
t1 = x , t2 = t1 , t7 = y , t13 = t7 ,
t17 = t2 x + 5 t2 y + 10 t1 x t7 + 10 t1 t7 y + 5 x t13 + t13 y
Something like that would *really* help. Also, speedwise, you might
think about putting it into a Pyrex file, and compiling it as a C module.
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/
|cookedm at physics.mcmaster.ca
More information about the SciPy-user
mailing list