# Math Programming

Templates can be created for mathematical programming. You can create basic math templates or add iterative program loops, format descriptors and conditional branches to create more complicated mathematical templates.

## Basic Templates

`The square root of 2 is approximately equal to 1.4.`

Because
there are no Templex statements, variables, or
expressions in the template, the content of the template is copied to the output
stream verbatim. This template generates the following
output:`The square root of 2 is approximately equal to 1.4.`

Expressions,
variables, and Templex statements must be placed between
braces, { }, in order for Templex to evaluate them.
Otherwise, they are treated as literal text and are not processed. The previous
template can be modified to solve for the square root of 2 by including the math
function,
sqrt.`The square root of 2 is approximately equal to {sqrt(2)}.`

The
template now calculates the square root of 2 and generates the following
output:`The square root of 2 is approximately equal to 1.414213.`

## Iterative Program Loops

```
{for (x=0; x<=5; x++)}
x={x}, Square root of x={sqrt(x)}
{endloop}
```

This
template produces the following
output:```
x=0, Square root of x = 0
x=1, Square root of x = 1
x=2, Square root of x = 1.41421
x=3, Square root of x = 1.73205
x=4, Square root of x = 2
x=5, Square root of x = 2.23607
```

## Format Descriptors

```
{for (x=0; x<=5; x++)}
x={x}, Square root of x={sqrt(x), %5.3f}
{endloop}
```

The
output from the template is now formatted to show three decimal
places:```
x=0, Square root of x = 0.000
x=1, Square root of x = 1.000
x=2, Square root of x = 1.414
x=3, Square root of x = 1.732
x=4, Square root of x = 2.000
x=5, Square root of x = 2.236
```

Any
characters on a line that follow a single quote ,‘, are a comment. No action is
taken on any function, operator, or expression that follows the quote on that line.
Text or expressions before the single quote are processed normally, but the items
following the single quote are considered comments and are not
processed.```
{'This template evaluates the square root of}
{'the numbers 0 through 5}
{for (x=0; x<=5; x++)}
x={x}, Square root of x={sqrt(x), %5.3f}
{endloop}
```

Note
that the output from the template does not include the commented
text:```
x=0, Square root of x = 0.000
x=1, Square root of x = 1.000
x=2, Square root of x = 1.414
x=3, Square root of x = 1.732
x=4, Square root of x = 2.000
x=5, Square root of x = 2.236
```

## Conditional Branches

```
{'This template evaluates the square root of the}
{'numbers 0 through 5 and prints the output when}
{'the value of the square root is less than or}
{'equal to 1.5}
{for (x=0; x<=5; x++)}
{if (sqrt(x)<=1.5)}
x={x}, Square root of x={sqrt(x), %5.3f}
{endif}
{endloop}
```

The
output from the template includes only the values less than or equal to
2:```
x=0, Square root of x = 0.000
x=1, Square root of x = 1.000
x=2, Square root of x = 1.414
```

## Multiple Output Files

```
{ofile = {"st_sqr.out", "st_sqrt.out", "st_sine.out"}}
{open ofile[0]}
{for (x=0; x<=5; x++)}
x={x}; Square of x= {x^2, %5.3f}
{endloop}
{close}
{open ofile[1]}
{for (x=0; x<=5; x++)}
x={x}; Square root of x= {sqrt(x), %5.3f}
{endloop}
{close}
{open ofile[2]}
{for (x=0; x<=5; x++)}
x={x}; Sine of x= {sin(x), %5.3f}
{endloop}
{close}
```

This
template creates three files called st_sqr.out,
st_sqrt.out, and st_sine.out with the
corresponding results.## Solving Linear Equations

The following template uses vector and matrix functions and operators to solve a linear system of equations.

```
3x + 4y + 9z = 29
5x - 3y + z = 16
x + y + z = 4
{x_coeffs = { 3, 5, 1 } }
{y_coeffs = { 4, -3, 1 } }
{z_coeffs = { 9, 1, 1 } }
{rhs = { 29, 16, 4 }' }
{lhs = {x_coeffs, y_coeffs, z_coeffs}' }
{format, %5.2f, matrix}
coefficient matrix:
{lhs}
right hand side:
{rhs}
determinant:
{determinant(lhs)}
inverse:
{inverse(lhs) , %7.4f}
inverse times original:
{inverse(lhs) * lhs}
answer:
{inverse(lhs) * rhs}
```

This template produces the following output.

```
3x + 4y + 9z = 29
5x - 3y + z = 16
x + y + z = 4
```

coefficient
matrix:```
| 3.00 4.00 9.00 |
| 5.00 -3.00 1.00 |
| 1.00 1.00 1.00 |
```

right
hand
side:```
| 29.00 |
| 16.00 |
| 4.00 |
```

determinant:`44.00`

```
| -0.0909 0.1136 0.7045 |
| -0.0909 -0.1364 0.9545 |
| 0.1818 0.0227 -0.6591 |
```

inverse
times
original:```
| 1.00 0.00 0.00 |
| 0.00 1.00 0.00 |
| 0.00 0.00 1.00 |
```

answer:```
| 2.00 |
| -1.00 |
| 3.00 |
```

Multiple statements can be placed between a single set of braces, { }. Additionally, multiple statements can be placed on the same line by separating the statements with semi-colons, ;. A semi-colon is not required at the end of the line.

Placing multiple statements on one line or several statements between one set of braces gives you flexibility in the way you write your templates.

```
{
pt1 = {10.1, 20.2, 30.3}; pt2 = {11.1, 21.2, 33.3};
diff = pt1 - pt2
dist = sqrt(diff[0]^2+diff[1]^2+diff[2]^2)
}
The distance between pt1 and pt2 is {dist}.
```

This
templates yields the following
output:`The distance between pt1 and pt2 is 3.31662.`

Templates
can be extended to solve advanced mathematical problems. The following template uses
different integration techniques to solve a first order differential
equation:```
{
'Solution to the First Order Differential Equation,
'Initial Value Problem: df/dx= -f + x + 1 where f(0)= 1
'using Euler, Modified Euler and Runge Kutta explicit
'integration techniques.
}
{
n=10
x = array(n+1)
Fexact = array(n+1)
Feu = array(n+1)
Fme = array(n+1)
Frk = array(n+1)
Fexact[0] = 1; Feu[0] = 1; Fme[0] = 1; Frk[0] = 1;
h=1/n
}
-X- -Fexact- -Feuler- -Fmodeuler- -Frungkutta-
{for(i=0; i<10; i++)}
{
x[i+1] = (i+1) * h
'Exact Solution
Fexact[i+1] = x[i+1] + exp(-x[i+1])
'Euler Explicit Integration
g = -Feu[i] + x[i] + 1
Feu[i+1] = Feu[i] + h*g
'Modified Euler Integration
g_me1 = -Fme[i] + x[i] + 1
F_me = Fme[i] + 0.5*h*g_me1
g_me2 = -F_me + (x[i]+0.5*h) + 1
Fme[i+1] = Fme[i] + h*g_me2
'Runge Kutta
k1 = -Frk[i] + x[i] + 1
k2 = -(Frk[i] + 0.5*h*k1) + x[i] + 0.5*h + 1
k3 = -(Frk[i] + 0.5*h*k2) + x[i] + 0.5*h + 1
k4 = -(Frk[i] + h*k3) + x[i] + h + 1
Frk[i+1] = Frk[i] + (h/6)*(k1+2*k2+2*k3+k4)
}
{endloop}
{
table(x, Fexact, Feu, Fme, Frk, "%4.2f %9.6f %9.6f %9.6f %9.6f", 0, 10)
}
```

The
template yields the following
output:```
-X- -Fexact- -Feuler- -Fmodeuler- -Frungkutta-
0.00 1.000000 1.000000 1.000000 1.000000
0.10 1.004837 1.000000 1.005000 1.004838
0.20 1.018731 1.010000 1.019025 1.018731
0.30 1.040818 1.029000 1.041218 1.040818
0.40 1.070320 1.056100 1.070802 1.070320
0.50 1.106531 1.090490 1.107076 1.106531
0.60 1.148812 1.131441 1.149404 1.148812
0.70 1.196585 1.178297 1.197210 1.196586
0.80 1.249329 1.230467 1.249975 1.249329
0.90 1.306570 1.287420 1.307228 1.306570
1.00 1.367879 1.348678 1.368541 1.367880
```