9

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.

Ponkadoodle
  • 5,777
  • 5
  • 38
  • 62

4 Answers4

10

Edit> Expression reduced form 700 to 20 chars below

Try to use FullSimplify in Wolfram Alpha, or Mathematica.

WolframAlpha FullSimplify(x^2+2 x +1)

Edit ->

Thinking again, Mathematica does not need to simplify your one var equation to solve it ... the Solve command (or FindRoot, or FindInstance ...) will do it.

Try for example

WolframAlpha Solve(x^2+2*x+1=0 , x)

EDIT -> Just to make the answer free of dependencies from ideone.com, your 700 char equation after some simplifications becomes

   t= -((E*A+B*F+ Sqrt(2*A*E*F*B+ A^2*(I^2-F^2) + B^2*(I^2-E^2))) /(A^2 + B^2))

Where

   E = e - g
   A = a - c
   B = b - d
   F = f - h
   I = i + j

Please check if the Sqrt argument is a perfect square, based on other "geometrical" considerations ... it barks and has a tail ... is it a dog?

EDIT -> Guesswork:

I don't have any proof, but the symmetry of the equation suggests that in your problem

  E^2 = (I^2-F^2)  => (e-g)^2 = (i+j)^2 - (f-h)^2

If so is the case (please verify it), your equation becomes

  t= -((E*A+B*F+ Abs(E*A+B*F)) /(A^2 + B^2))

If AE+BF > 0 (and I guess it is so, because if not t===0)

  +-----------------------------------+
  ¦  Your 700 chars equation comes to ¦
  ¦                                   ¦
  ¦ t= -2 * (A*E + B*F) / (A^2 + B^2) ¦
  ¦                                   ¦
  +-----------------------------------+

short and sweet ... :)

Dr. belisarius
  • 60,527
  • 15
  • 115
  • 190
  • 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 :-( – Ponkadoodle Jun 25 '10 at 00:54
  • Post it and I'll try it in Mathematica – Dr. belisarius Jun 25 '10 at 00:55
  • It's too long! I'll upload it to somewhere... hang on. [edit, here @belisarius: http://ideone.com/lV3Sx] – Ponkadoodle Jun 25 '10 at 01:05
  • ah, the bracket isn't supposed to be part of the url. So here it is again: ideone.com/lV3Sx – Ponkadoodle Jun 25 '10 at 01:15
  • @wallacoloo Please specify the independent var and the other side of the equation – Dr. belisarius Jun 25 '10 at 01:30
  • @belisarius oh, well it's t=... Where all the other variables are known ( http://ideone.com/oF9kT ) – Ponkadoodle Jun 25 '10 at 01:38
5

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
  • 40,059
  • 14
  • 91
  • 135
3

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.

Community
  • 1
  • 1
Niki
  • 15,662
  • 5
  • 48
  • 74
  • 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. – Ponkadoodle Jun 25 '10 at 21:08
  • @wallacoloo You stated that "This is performance critical code" and not "I want the cheapest solution" :D – Dr. belisarius Jun 25 '10 at 21:48
  • @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. – Ponkadoodle Jun 25 '10 at 22:14
2

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.

Community
  • 1
  • 1
Akusete
  • 10,704
  • 7
  • 57
  • 73
  • Personally I prefer matlab, but then again I am not the one paying for the licence :) – Akusete Jun 25 '10 at 01:03
  • @belisarius: If you enjoy pictures of mass murdering communists, I guess you have a point re. the avatar... – Pierreten Jun 25 '10 at 02:08
  • @belisarius, wow the pope? I dont know how you got there :) Which pope may I ask? – Akusete Jun 25 '10 at 05:34
  • 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 Jun 25 '10 at 13:30