<-- Previous || Up || Next -->

Infix To Basic Function
Math Symbolic Class

Public Function InfixToBasic( _
    ByVal vExpr As Variant _
    ) As Variant

"Derive Expression to VB Expression" or
"Infix Expression To Basic Language Expression"

Summary: The purpose of this function is to take algebraic expressions recognized by a symbolic math program named Derive (by Soft Warehouse, Honolulu, Hawaii; http://www.derive.com/derive.htm) and to translate them into Visual Basic language programs. This function accepts the Derive expressions as they appear within the saved Derive-format "MTH" files (with one minor exception: Within MTH files, expressions are limited to 80 or so characters and the line continuation character is a tilde ("~"). This function assumes that any such continued lines have been rejoined and the line continuation character has been removed.)
We used this function to help implement some of the more complicated solutions within Geometric calculator functions. This program turns the Derive-compatible algebraic expressions into a series of sub-calculations. Most repetitive sub-expressions are replaced by 1) a single calculations and variable assignment, and 2) two or more references to the value of that variable.
Example:
    ? InfixToBasic("2*SQRT(b^2-3*a*c)*COS(ATAN(SQRT(3)*(27*a^2*d-9*a*b*c+2*b^3)/(9*a*(b^2-3*a*c)^(3/2)*SQRT((27*a^2*d^2+2*a*c*(2*c^2-9*b*d)+b^2*(4*b*d-c^2))/(3*a*c-b^2)^3)))/3+pi/6)/(3*ABS(a))-b/(3*a)")
    Dim N() As Variant
    ReDim N(1 To 54)
    N(1) = Square(b)
    N(2) = Mult(3,a)
    N(3) = Mult(N(2),c)
    N(4) = Subtract(N(1),N(3))
    N(8) = Square(a)
    N(9) = Mult(27,N(8))
    N(11) = Mult(9,a)
    N(26) = Square(c)
    Subtract(Div(Mult(Mult(2,Sqrt(N(4))),Cosine(Addit(Div(ATan(Div(Mult(Sqrt(3),Addit(Subtract(Mult(N(9),d),Mult(Mult(N(11),b),c)),Mult(2,Power(b,3)))),Mult(Mult(N(11),Power(N(4),Div(3,2))),Sqrt(Div(Addit(Addit(Mult(N(9),Square(d)),Mult(Mult(Mult(2,a),c),Subtract(Mult(2,N(26)),Mult(Mult(9,b),d)))),Mult(N(1),Subtract(Mult(Mult(4,b),d),N(26)))),Power(Subtract(N(3),N(1)),3)))))),3),Div(pi,6)))),Mult(3,Absolute(a))),Div(b,N(2)))
See also:
    InfixToPrefix Function
    InfixToPostfix Function
    BreakParentheses Function
    RationalComplexToVBExpr Function
vExpr: String containing the Derive-compatible algebraic expression which is to be translated into Visual Basic code for insertion into a Function or Subroutine. Function returns Null if vExpr is Null or cannot be fixed up to a String.

Note: This translation task is necessarily complicated, and the implementation of this function is not the best.
Note: Function has a basic problem in that some unused variables within the calculation sequence are not reused. For example, after several "assignment calculation to variable" code lines, one or more of those variables may no longer be needed for future calculations, but they are no reused.
Note: This algorithm has a basic problem in that it does not produce an optimized set of calculations. It produces an accurate translation (as far as we know), but that translation does not represent the absolute minimum number of calculations required to evaluate the expression.
Note: Function will return an error in the form of a string like "Error: Expected one operand." if the algebraic (infix) expression is ill-formed. The possible errors include:
Error: Expected one operand. (Function has encountered a function call so it needs an operand but the operand stack is empty.)
Error: Expected two operands. (Function has encountered a binary operator so it needs two operands but the operand stack has less than two operands.)
Error: Expected end of expression. (Function has encountered the end of the input expression but there are still operands on the stack.)

Note: This function creates a one-dimensional array of Variant values (within the result string) which it uses to store the intermediate values (the results of the redundant sub-expression calculations).

Translations: As well as attempting to break-out the repeated sub-expressions, this function performs some other translations on various Derive constants, operators, and functions. These translations are detailed in the following tables.

Constants:

    From     To
    --------------
    PI       Pi
    #e       e
    #i       "0|1"
Operators:
    From     To
    --------------
    +        Addit
    -        Subtract
    *        Mult
    /        Div
    ^        Power
Functions:
    From     To
    --------------
    ABS(     Absolute(
    ASIN(    ASin(
    ATAN(    ATan(
    COS(     Cosine(
    LN(      Logarithm(
    SIGN(    Sign(
    SINE(    Sine(
    SQRT(    Sqrt(
    TAN(     Tangent(

Copyright 1996-1999 Entisoft
Entisoft Tools is a trademark of Entisoft.