Formula Translation


This article has been rewritten.
Also, the full Delphi project with source code and
an excerciser program are made available.
Click here



Introduction
This article describes the translation of a mathematical formula into a sequence of basic operations.
The method was developed for Graphics-Explorer, a versatile program to investigate
various types of functions and equations.
(Click here to see specifications or to download Graphics-Explorer)

Graphics-Explorer may handle up to 9 different equations of 4 different types simultaniously:

typeexample
1functiony = 3sin(2x)
2inverse functionx = 5cos(y) - 2sin(3y)
3parametric functiony = 3sin(2v) ; x = 5cos(7v)
4implicit functionx^2 + y^2 = 81

The same translation proces is used for all types of equations.
The final result of the translation is the 'director-table', a list of
basic calculations in the order of their priority.
In the following description, the formula y = 3.2 + 1.6(x-0.5) + 0.22x^2
will be translated.

Multiply operator insertion
Before the translation starts, the multiply operator '*' is inserted where needed.
This changes the formula to y = 3.2 + 1.6*(x-0.5) + 0.22*x^2
Also, any blank characters are removed.

Priority
Because of different priorities of the operators + * ^, the value of y cannot be
calculated simply in the 'left to right' sequence.

The priority of an operation is the sum of two values:

1. the biaspriority:
    initially, this value is 0. Occurrence of '(' in the formula string increases the
    biaspriority by 10, where occurrence of ')' decreases this priority by 10.

2. the operator priority:

7function (sin, cos, tan, root, etc.)
6^ power
5unairy - as in -17.5 or -sin(2x)
4* / , multiplication and division
3+ - , addition and subtraction
2=
1; the separator of parametric functions
0end of table

Constants and variables
In the formula above, constants 3.2 , 1.6 , 0.5 and 0.22 are noticed as well as variables x and y.
These values are stored in an array called Freg[1..250] (floating point registers) as follows:

Fregcontains
[1..19]intermediate results
[20..39]constants formula 1
[40..59]constants formula 2
[60..199]constants formulas 3..9
[200]x
[201]y
[202]v
[203]A
[204]B
[205]C
[206]pi 3.14159...
[207]e 2.71828...
[210..212]formula 1: A,B,C after substitution
[213..215]formula 2: same as above
[216..236]formulas 3..9: same as above

After scanning above formula (as number 1), Freg will look like:

Fregvalue
203.2
211.6
220.5
230.22
242

The constants are stored in the sequence of their occurrence.

The Director table
This table is the final result of the translation.
Each row describes a basic arithmetic operation or function and has the form:
    operation....destination....source1....source2
Consider the following table as a result of translated y = 3.2 + 1.6(x-0.5) + 0.22x^2:

operationdestinationsource1source2
1-1x22
2^2x24
3*1211
4*2232
5+1201
6+112
7=ynone1

For clarity, x and y are printed instead of 200 and 201.
Also, operators +,-,*,^ are printed instead of the numeric code, indicating that operator.
The meaning of row 1 is:
    subtract contents of Freg[22] from x (Freg[200]) and place this difference in Freg[1].
The value of an equation can quickly be calculated by executing the rows
of the director table from top to bottom.
After executing row 7, the final result is hold by Freg[201] (y).

The Priority Table
This table is an intermediate during formula-string to Director Table translation.
A row of the priority table has the form:
    constant or variable ....operation or function.....priority
Numbers in the table refer to the location of the variable or constant in the Freg array.

constant or
variable
operationpriority
1y=2
220+3
321*4
4x-13
522+3
623*4
7x^6
824end0

The first 2 colums, red row by row, resemble the formula string with constants
and variables replaced by their reference to the Freg[] array.
The third column indicates the priority of the operation and is the addition of operator- and bias priority.
Notice, that row 4 has the priority 13, not 3, because a '(' parenthesis has been passed.

The translation
The Director Table is built while eliminating the Priority Table row by
row, always taking the operation with the highest priority first.

constant or
variable
operationpriority
1y=2
220+3
321*4
4x-13
522+3
623*4
7x^6
824end0
operationdestinationsource1source2
1-1x22


The operation of row 4 is removed from the priority table and added to the director table.
Since registers x and 22 may not be modified, the first register for intermediate results, 1, receives the difference.
Now, row 6 has the highest priority.

constant or
variable
operationpriority
1y=2
220+3
321*4
41+3
523*4
6x^6
724end0
operationdestinationsource1source2
1-1x22
2^2x24


The result is stored in register 2, the next available register for intermediate results.

constant or
variable
operationpriority
1y=2
220+3
321*4
41+3
523*4
62end0
operationdestinationsource1source2
1-1x22
2^2x24
3*1211


Register 1 is overwritten, not to waste registers 1..19 continuing the process:

constant or
variable
operationpriority
1y=2
220+3
31+3
423*4
52end0
operationdestinationsource1source2
1-1x22
2^2x24
3*1211
4*2232


Continuing:

constant or
variable
operationpriority
1y=2
220+3
31+3
42end0
operationdestinationsource1source2
1-1x22
2^2x24
3*1211
4*2232
5+1201


And after the next step:

constant or
variable
operationpriority
1y=2
21+3
32end0
operationdestinationsource1source2
1-1x22
2^2x24
3*1211
4*2232
5+1201
6+112


And after the last step:

constant or
variable
operationpriority
1y=2
21end0
operationdestinationsource1source2
1-1x22
2^2x24
3*1211
4*2232
5+1201
6+112
7=ynone1


This completes the translation.

Additional notes
Functions like sin, cos, abs are treated the same way as operators +,- etc. however the source1 operand is ignored.

The separator character ';' for parametric functions (type 3) is regarded as a dummy operator with lowest priority.
This prevents interaction between values of x and y.
The calculated values for x and y are available in Freg[200] and Freg[201] after processing the director table.

Intrinsic functions (type 4) treat the '=' operator as a '-' operator.
An intrinsic function f(x) = g(x) is transformed to v = f(x) - g(x).
A dot (x,y) is plotted if v = 0. If v changes sign, the dot (x,y) with the lowest absolute value is plotted.

Graphics-Explorer contains debug code for examining the tables.
This code is activated/deactivated by typing ,d. (type d, while holding down the shift and control key)
The unairy '-' operator is shown as (-).

David E. Dirkse, May 2004, the Netherlands
david@davdata.nl