View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Extracting Terms from simple linear equation

some pseudo code. (since it uses split, it won't work in xl97) Assumes
spaces before and after the operators as shown.

dim ArrTerms() as String, ArrOps() as String
redim ArrTerms(0 to 20)
redim ArrOps(0 to 20)
Dim i as long, j as long
Dim b as Boolean
i = 0, j = 1
sSTr = -25 + 3*X + 15*Y - 4*X^2 + 8*X^2*Y^-2
v = Split(sSTr," ")
b = True
for k = lbound(v) to ubound(v)
if b then
ArrTerms(i) = v(k)
i = i + 1
else
ArrOps(j) = v(k)
j = j + 1
end if
b = not b
Next k
Redim preserve ArrTerms(0 to i - 1)
Redim preserve ArrOps(0 to j - 1)

-25 looks like a unary operator to me. Same as your negative exponent.

If you want it to be stored as a sign, you can pass through the ArrTerms and
look for this situation and move the sign to ArrOps. Not sure how you got
your final Plus sign, but it could be added it at end.

Another approach would be to replace ^- with a unique symbol. Then loop
through the string and separate on finding a plus or minus. Then after
separated, go through and change the unique symbol to ^-.


If you just need the answer to the formula, use Replace to replace the X's
and Y's with your values for each in the string, then use Evalute. No
parsing required.



--
Regards,
Tom Ogilvy


"William Benson" wrote in message
...
I have a basic linear equation of this form (Note, parentheses and grouped
terms will not matter):

-25 + 3*X + 15*Y - 4*X^2 + 8*X^2*Y^-2

Note: never this form -25 + 3*X + 15*Y - 4*X^2 (1 - 2 Y^-2)

Can someone please help me build a parsing routine to isolate terms, and
whether they are preceded by a + or a -. I am using two separate arrays
because I later have to work with each term to calculate a derivative.
Negative exponent must be treated differently than a minus sign. ArrTerms
and ArrOps() should look like this at the end of the above exercise, and

be
generalizable:

i ArrTerms( i ) ArrOps( i )
1 25 -
2 3*X +
3 15*Y +
4 4*X^2 -
5 8*X^2*Y^-2 +

I'd be very appreciative for help with the required (generalizable)

routine.
If I can clarify something, please e-mail me (w b e n s o n 1 @ n y c a p

..
r r . c o m) or post here.

Thanks!