Log in

View Full Version : Analyzing an expression?


Issues
10-11-2005, 15:06
This isn't related to FIRST, but is a matter of programming. I am trying to make a program that will take in information about multiple vectors and then take in an expression with both scalar and vector math and evaluate it. I was wondering what is the best approach for this. Lets say they have to enter in a string containing
(a,b,c,d,e,f)- letters representing vectors
(+,-,/,*,x) - operators
numbers and parenthesis

so like this (3a+4b)xc

what is the best way to evaluate the expression. The approach I am using uses like a million if statements and it is very hard to read =\ and slowwww. So, how does maple do it?

Timothy D. Ginn
10-11-2005, 16:40
Firstly, let me just say this subject goes a bit over my head at this point and I doubt this is something you'll get a simple answer to; but, I've been told of some open source thing which functions like Maple/Mathematica. (It may have been Maxima (http://maxima.sourceforge.net/index.shtml)). So, you might want to look at how they do it.

Depending on what you're doing converting to Reverse Polish Notation (http://www.calculator.org/rpn.html) may be useful, too.

Tristan Lall
10-11-2005, 18:25
The first thing you want to do is parse the string into terms. Given input like axb- c+dx e, you need to specify a symbolic convention, so that it is interpreted correctly; it could be interpreted as:a × b - c + d × e



or



a × x × b - c + dx × e



or as something else (with scalars in italics, vectors in boldface, and operators in plain type, and the × sign standing for cross-product or multiplication, as appropriate). Actually, this is the reason why math programs will force you to specify what's a vector, what's a variable, what's a constant, what's a function, and so on—there's far too much ambiguity. What you're going to need to do (though the implementation is flexible) is to force the user to add formatting to clarify their intent. Maybe enclose vectors in curly brackets (e.g. v = {v}), enclose scalars in square brackets (e.g. s = [s]), use plain letters and symbols for operators, and use nested parentheses for order of operations. The above expression would then be input as {a}x{b}-{c}+{d}x{e} if you wanted the first version, or [a]x{x}x{b}-{c}+d{x}x{e}, if you actually wanted the second version.


In fact, this is complicated significantly by the fact that there are many ways to represent a vector: Cartesian (e.g. v = xi + yj), polar (e.g. v = r cos θ + r sin θ) and normal-tangential (e.g. v = an + bt), and it's even messier for an arbitrary number of dimensions.

If that's not enough trouble, then you've got to decide what method of evaluation you're going to use, and what form the answer will take. The evaluation will probably have to take place in one of these co-ordinate systems, with one vector equation being split up into several components.

Now, you can solve the component equations. Do some quick research (http://www.google.ca/search?hl=en&rls=GGLD,GGLD:2003-35,GGLD:en&q=infix+postfix+stack&spell=1) into infix->postfix conversion. That's effectively what was mentioned earlier, except in computer-science terms (regular equations are infix, and postfix is RPN, but searching for RPN will get you stuff on HP calculators). From postfix, it's relatively straightforward to go about evaluating an expression of arbitrary complexity—but that doesn't mean it's simple. In any event, you probably want to output a postfix string (an equation), because it's very likely that you'll have symbols left over (variables, vectors, etc.), rather than a single numerical answer.

Once you've got your solutions solved down to the simplest forms, you then need to represent it as a vector equation again, rather than as several component equations (actually, component equations are a valid form of a vector, but assuming that you wanted it output the way that it was input, this step becomes necessary).

So, in short, for a general case, with an arbitrary number of variables, and with an arbitrary number of dimensions, and a significant subset of mathematical operators, evaluating a vector equation is really, really hard. I would very much recommend that you try something easier first, like arithmetic. Once you've got a feel for that, then try using scalar variables. Then, if it's not too much trouble, try simple cases of 2-D Cartesian vectors. You will run into all sorts of trouble, if you try to do it all at once!