views:

236

answers:

5

I had a pretty big equation that I needed to use to solve for a given variable. So I used an online tool that was capable of rewriting an equation in terms of a given variable. It gave me some huge 700 character equation. I tested it, and it does work.

I can see some pretty obvious redundancies in the equation where it's recomputing a value that could be saved as a temporary variable instead. I could go through the entire equation and optimize it myself, but I'm likely to have to do this with many more equations, so I'd like to automate the process instead.

What are some good tools that will help optimize mathematical redundancies?
(It's just for a personal project, so I'd really prefer something free)

To all those people who I know will ask about this really being necessary: This is performance critical code, and from my experience, the AS3 compiler will not do these kind of optimizations on it's own. Removing redundancies will also make the code more readable.

+1  A: 

Look into memoization.

http://en.m.wikipedia.org/wiki/Memoization

Pierreten
+10  A: 
belisarius
Great idea. But the equation is so large that the input box won't accept it. So I encoded it directly into the URL to get around that (using python's urllib.urlencode). But it simplified it to something that is incorrect. Maybe it'll work right in Mathematica, but I don't think it's free software :-(
Wallacoloo
Post it and I'll try it in Mathematica
belisarius
It's too long! I'll upload it to somewhere... hang on. [edit, here @belisarius: http://ideone.com/lV3Sx]
Wallacoloo
ah, the bracket isn't supposed to be part of the url. So here it is again: ideone.com/lV3Sx
Wallacoloo
@wallacoloo Please specify the independent var and the other side of the equation
belisarius
@belisarius oh, well it's t=... Where all the other variables are known ( http://ideone.com/oF9kT )
Wallacoloo
+2  A: 

As belisarius suggested, putting the equation into a mathematical programming language like matlab, mathematica or maple would allow you to use their simplify and reduction tools to help you.

Here is a list of free matlab like programs http://www.dspguru.com/dsp/links/matlab-clones if you dont want to fork out the high price for a matlab licence.

Akusete
+1 for the free software list and the avatar
belisarius
+1 for clones. Octave is the best
phwd
Personally I prefer matlab, but then again I am not the one paying for the licence :)
Akusete
@belisarius: If you enjoy pictures of mass murdering communists, I guess you have a point re. the avatar...
Pierreten
@Pierreten OMG ... I thought it was the Pope ..
belisarius
@belisarius, wow the pope? I dont know how you got there :) Which pope may I ask?
Akusete
For symbolic math, you'll want to use Maxima. Matlab and friends are for numerical work. Maxima has a number of functions for factoring and simplifying expressions. It can even do simplifications of trigonometric expressions.
phkahler
+5  A: 

I've used wxMaxima. It's fairly easy to make it do substitutions, and it's free. I had to crank a lot of massive Laplace transforms, with partial fraction expansions. Once I learned how to use it, it was pretty quick.

Mike Dunlavey
+1 Maxima is a great free tool for symbolic math.
phkahler
+3  A: 

Maxima has a useful function called optimize:

Function: optimize (expr)

Returns an expression that produces the same value and side effects as expr but does so more efficiently by avoiding the recomputation of common subexpressions. optimize also has the side effect of "collapsing" its argument so that all common subexpressions are shared. Do example (optimize) for examples.

It would simplify the expression you uploaded to Ideone to:

block(
[%1,%2,%3,%4,%5,%6,%7,%8,%9,%10,%11,%12,%13,%14],
  %1:a^2,
  %2:b^2,
  %3:c^2,
  %4:d^2,
  %5:-%4+2*b*d-%2,
  %6:-%3+2*a*c-%1,
  %7:2*a-2*c,
  %8:2*c-2*a,
  %9:
  %8*d+b*%7,
  %10:%7*d+b*%8,
  %11:i^2,
  %12:j^2,
  %13:-2*%12-4*i*j-2*%11,
  %14:%12+2*i*j+%11,(-sqrt(%4*%14+%3*%14+%2*%14+%1*%14+b*d*%13+a*c*%13+%6*h^2+    (%9*g+2*%3-4*a*c+2*%1)*f+%10*e)*h+%5*g^2+f*(%10*g+%9*e)+(2*%4-4*b*d+2*%2)*e*g+%6*f^2+%5*e^2)-(d-b)*h-(c-a)*g-(b-d)*f-(a-)*e)/(%4-2*b*d+%3-2*a*c+%2+%1))

Not neccessarily more readable, but it contains no more common subexpressions.

nikie
+1 Bingo! This should be the accepted answer.
Mike Dunlavey
Although Mathematica seems to be able to simplify equations much better, Maxima is *free*, and very easy to use. So I accepted this as an answer.
Wallacoloo
@wallacoloo You stated that "This is performance critical code" and not "I want the cheapest solution" :D
belisarius
@belisarius that is true, I'll go clarify it now ;-) I am not going to profit off of the end product, so I'd prefer not to spend any cash on a tool, no matter how amazing it is.
Wallacoloo