views:

718

answers:

9

How to convert an equation into formulas for individual variables? I am thinking about a math equations like:

c^2 = a^2 + b^2

I would like to have a function that could process any formula, and give me the individual variable formulas. The above equation would produce the following:

a = (c^2 - b^2)^0.5
b = (c^2 - a^2)^0.5
c = (a^2 + b^2)^0.5

I would also like to start with:

a = (c^2 - b^2)^0.5

and output:

b = (c^2 - a^2)^0.5
c = (a^2 + b^2)^0.5

I have looked at expression trees, but I am having trouble visualizing how this will work. I would like a .NET (C#, VB.NET, or F#) solution. Any ideas?

Something like:

public string[] GetFormulas(string equation)
{
   ...
}

Thanks.

A: 

Since c^2 = a^2 + b^2 is not an expression in C#, you're off on the wrong track.

Forget about .NET expression trees and create your own. You need to describe the algorithms required to do these transformations, and the data necessary to describe an equation. You'll find that what you wind up with is quite different from a .NET expression tree.

John Saunders
+1  A: 

This is something for which you'll probably want to look at using a mathematical library. For .NET, Math.NET seems to be the most complete option (not sure how stable it is, but it's certainly very complete). The library for doing symbolic manipulation should be able to handle the specific problem you have posed here.

To be honest, writing this from scratch shouldn't be too difficult, but if you're not very familiar with expression trees and wouldn't know how to approach the task, I would still recommend using an existing maths library, either Math.NET or any other decent one that does symbolic algebra.

Noldorin
Also, note that simply rearranging a given arbitrary equation is a *lot* easier (though it can be reasonably tricky) than actually solving it. If you want to solve complex equations, you're going to need something like the Mathematica package in general.
Noldorin
I might finally suggest that you use a language like F# if you're familiar with it and are going to write the important code yourself (rather than using a library). Functional languages are widely considered to be particularly well-suited to mathematical and scientific applications.
Noldorin
Math.NET will not work for me because of the GPL.
Bobby Ortiz
Oh right, fair enough. In that case, you may want to specify the legal/licensing requirement in your question...
Noldorin
+4  A: 

This is a non-trivial problem you're trying to solve... I don't think you'll have much luck trying to solve it on your own. Better to find some kind of third-party app or library that does it. There are a number of programs that can do the operation you're talking about, such as Matlab and Maple. Also, a TI-89 graphing calculator can do it. You may be able to get the algorithms you need from Octave, which is essentially an open-source implementation of Matlab.

rmeador
+1  A: 

Dealing with equations in a symbolic and non-numeric way is definitely not an easy task. I think the easiest way for you would be to simply use Mathematica, Maple or similar behind the scenes and let them do the hard work for you.

Joey
+6  A: 

Symbolic equation solving is a complex problem and there is no closed solution for many equations. Writing your own Computer Algebra System is none trivial, but you may be able to write a program for simple equation.

You will have to build a expression tree of the input string and define transformation rules for manipulating the expression tree. To solve for a variable you might then perform a search (guided by good heuristics to keep the runtime acceptable) on the space of expression tree that can be derived from the orginal tree by multiple aplications of transformation rules.

Daniel Brückner
+2  A: 

Your only choice is to brute force it by applying all known techniques. In simple algebra equations like you have above, that might be sufficient, but more complex problems will require increasingly complex solutions. In brief, it won't be easy.

Once you figure out how to parse text into symbols, it might be easy enough to create an app that can determine that

c^2 = a^2 + b^2

can be substituted as

c = (a^2 + b^2)^.5

but, what about

cos(c) = sin(a^2/b) - b^(a/sin(b))

Worse yet, you have unsolvable integrations, and abstract algebra... You'll have to draw the line of complexity somewhere, or else you'll just end up building another Maple.

Michael Meadows
I agree. Most of the equations I will be using will be basic Algebra and basic Geometry. Some Trig, but very little.
Bobby Ortiz
A: 

In addition to anything that has already been said, you can have a look at numerical methods.

There are algorithms to apriximate the solution(s) of an equation. Because most of them won't be that easy (or even imposible) to solve exactly.

Gamecat
A: 

A nasty problem in general. For low order polynomial expressions, this is not too terribly hard. For linear problems, you just need a parser and a little bit of post-processing. But even a simple to write expression might be less than trivial. For example, what will you do with

x^5 + y^5 - xy + 1 = 0

Solving for either of x or y in terms of the other means you must solve for the roots of a non-constant coefficient polynomial of order 5. This will be impossible to do in general.

Worse, introduce trig functions or any special function into the mix, and it will force you to re-write Mathematica before you are done.

woodchips
A: 

Not a proposed solution but... as an intersting aside...I have a Toshiba M400 Tablet PC that lets me write with a pen formulas on my screen which it then coverts into programatic friendly notation...useful ;P

MaSuGaNa