Go to Post It's up to veteran teams and mentors to help keep the original spirit of FIRST alive. - It's up to us to make these boards exemplify the FIRST principals in every post. - Ben Mitchell [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
 
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 10-11-2005, 15:06
Issues Issues is offline
Registered User
FRC #0525
 
Join Date: Nov 2005
Location: Iowa
Posts: 11
Issues is on a distinguished road
Analyzing an expression?

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?
  #2   Spotlight this post!  
Unread 10-11-2005, 16:40
Timothy D. Ginn's Avatar
Timothy D. Ginn Timothy D. Ginn is offline
I check here maybe once a year.
no team
 
Join Date: Apr 2003
Rookie Year: 2002
Location: Port Perry, ON. Canada
Posts: 247
Timothy D. Ginn is a name known to allTimothy D. Ginn is a name known to allTimothy D. Ginn is a name known to allTimothy D. Ginn is a name known to allTimothy D. Ginn is a name known to allTimothy D. Ginn is a name known to all
Send a message via ICQ to Timothy D. Ginn Send a message via AIM to Timothy D. Ginn Send a message via MSN to Timothy D. Ginn Send a message via Yahoo to Timothy D. Ginn
Re: Analyzing an expression?

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). So, you might want to look at how they do it.

Depending on what you're doing converting to Reverse Polish Notation may be useful, too.
__________________
Alumni of FRC Team 1006
Former mentor of Full Lego Alchemist (FLL 5621) - Sempar School / Computing Students' Association of Queen's University
  #3   Spotlight this post!  
Unread 10-11-2005, 18:25
Tristan Lall's Avatar
Tristan Lall Tristan Lall is offline
Registered User
FRC #0188 (Woburn Robotics)
 
Join Date: Aug 2001
Rookie Year: 1999
Location: Toronto, ON
Posts: 2,484
Tristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond reputeTristan Lall has a reputation beyond repute
Re: Analyzing an expression?

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 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!

Last edited by Tristan Lall : 10-11-2005 at 18:29.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Individual bits in C Noah Programming 7 08-01-2004 15:53


All times are GMT -5. The time now is 04:35.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi