Pre-note: this is in regard to a Polynomial Math utility, that I am revising. The older version utilized a string-parsing algorithm that was painfully slow, in that it manually pulled out every character without prejudice and figured out the coefficients that way.
Obviously, I'd rather not have the same algorithm for this in the new version. I came up with some ideas for a faster parser that I'd like to run by whomever has the time to comment.
1. For breaking the string into terms in the polynomial...
Code:
2. For getting the coefficient out:
** Set X equal to 1, then do ' expr(String) ' on that part. 1 to any power evaluates to 1. 1 times the coefficient is the coefficient. Is that pretty fool-proof?
3. For getting the exponents out:
** Not exactly sure what to do, but can't I set X > 1, evaluate ax^b, then do some sort of log( on that to get the exponent?
4. The last thing I need is a way of compensating for a term that is omitted. Like if we go from x^4 straight to x^2, we need a way of putting in a 0 for the coefficient of x^3. Not sure how to do this, using the above system.
Obviously, I'd rather not have the same algorithm for this in the new version. I came up with some ideas for a faster parser that I'd like to run by whomever has the time to comment.
1. For breaking the string into terms in the polynomial...
Code:
inString (Str0, "+") --> LPLUS
inString (Str0, "-") --> LMINUS
augment (LPLUS, LMINUS) --> LSIGNS
// LSIGNS is the positions of all + or - signs
// dim (LSIGNS) + 1 == number of terms, not including terms that have a value of 0 (and are thus omitted in the typed function)
SortA (LSIGNS) --> LSIGNS
augment( {1}, LSIGNS ) --> LSIGNS
augment( LSIGNS, length (Str0) ) --> LSIGNS
** we should now have a list consisting of the first spot in the string, all +'s or -'s, and the last term. We can use that to parse the string.
2. For getting the coefficient out:
** Set X equal to 1, then do ' expr(String) ' on that part. 1 to any power evaluates to 1. 1 times the coefficient is the coefficient. Is that pretty fool-proof?
3. For getting the exponents out:
** Not exactly sure what to do, but can't I set X > 1, evaluate ax^b, then do some sort of log( on that to get the exponent?
4. The last thing I need is a way of compensating for a term that is omitted. Like if we go from x^4 straight to x^2, we need a way of putting in a 0 for the coefficient of x^3. Not sure how to do this, using the above system.