The Natural Math Program

Stephen Montgomery-Smith

Tutor for Natural Math version 0.5 last modified July 21, 2001 Copyright 1999 Stephen J Montgomery-Smith. All rights reserved.

1 Introduction

Here we describe the Natural Math program. It is easy to use. Start with a file whose extension is .nat, for example, test.nat. This tutorial was created by the file tutor.nat.

Each line of your file xxx.nat is written in what we call “natural math,” that is, math written as you might naturally express it if you only had a simple typewriter. You will use numbers, letters, and symbols, although anything that can be expressed in symbols can also almost always be expressed in letters.

What the program will do is to convert the natural math file into a LaTEX file. You run it like this:

                      naturalmath xxx.nat

This will create a file xxx.tex.

Here is an example of lines of input, followed by the output that would be created.

integral from 0 to infinity of e ^(-x^2/2) dx
= sqrt (pi over 2)

∫_{0}^{∞} e^{−x^{2}/2} dx = \sqrt{\frac{π}{2}}

Each formula is created by a sequence of such lines, terminated by a a blank line. Let us first give an example, where we attempt to solve a homework problem. First we give the input, then the output.

2 Example

# Start the question
text Chapter 8.6 Question 25
text Evaluate the following sum
sum from n = 2 to infinity of 1 over (n^2 - 1)
text Answer: use partial fractions
n^2 - 1 = (n-1)(n+1)
1 over (n^2 - 1) = A over (n-1) + B over (n + 1)
= (A(n+1) + B (n-1)) over ((n-1)(n+1))
1 = A n + A + B n - B
text Equate coefficients
0 = A - B
1 = A + B
text add equations
1 = 2A
A = 1 over 2
B = - 1 over 2

1 over (n squared - 1) = 1 over (2(n-1)) - 1 over (2(n+1))
S _N = sum from n = 2 to N of 1 over (n^2 - 1)
=
(1 over 2 - 1 over 6) + (1 over 4 - 1 over 8) + (1 over 6 - 1 over 10)
+ (1 over 8 - 1 over 12)
+ ... +
+ (1 over (2(N-3)) - 1 over (2(N-1)) )
+ (1 over (2(N-2)) - 1 over (2N) )
+ (1 over (2(N-1)) - 1 over (2(N+1)) )
=
1 over 2 + 1 over 4 - 1 over (2N) - 1 over (2(N+1))
limit as N to infinity of S_N = 3 over 4

Chapter 8.6 Question 25 Evaluate the following sum

∑_{n=2}^{∞} \frac{1}{n^{2} − 1}

Answer: use partial fractions

n^{2} − 1 = (n − 1) (n + 1)

\frac{1}{n^{2} − 1} = \frac{A}{n − 1} + \frac{B}{n + 1}

= \frac{A (n + 1) + B (n − 1)}{(n − 1) (n + 1)}

1 = An + A + Bn − B

Equate coefficients

0 = A − B

1 = A + B

add equations

1 = 2A

A = \frac{1}{2}

B = −\frac{1}{2}

\frac{1}{n^{2} − 1} = \frac{1}{2 (n − 1)} − \frac{1}{2 (n + 1)}

S_{N} = ∑_{n=2}^{N} \frac{1}{n^{2} − 1}

= (\frac{1}{2} − \frac{1}{6}) + (\frac{1}{4} − \frac{1}{8}) + (\frac{1}{6} − \frac{1}{10}) + (\frac{1}{8} − \frac{1}{12}) + … +

+ (\frac{1}{2 (N − 3)} − \frac{1}{2 (N − 1)}) + (\frac{1}{2 (N − 2)} − \frac{1}{2N} )

+ (\frac{1}{2 (N − 1)} − \frac{1}{2 (N + 1)})

= \frac{1}{2} + \frac{1}{4} − \frac{1}{2N} − \frac{1}{2 (N + 1)}

lim_{N→∞} S_{N} = \frac{3}{4}

3 The basic commands

3.1 Numbers and Variables

Natural Math allows numbers and one letter variables written in the usual way. There is also the complete collection of greek letters, both lower and upper case, and infinity. There are also the dots.

30.45 , x , pi, Pi, Phi, phi, infinity, ...,dots

30.45, x, π, Π, Φ, ϕ, ∞, …, …

3.2 Operations

Natural Math supports a large set of operations from mathematics. The arithmetic operators:

a + b, a plus b, a - b, a minus b,

a + b, a + b, a − b, a − b,

a * b, a times b, a / b, a divide b,

a × b, a × b, a/b, a ÷ b,

a ^b, a power b, a . b, a dot b

a^{b}, a^{b}, a · b, a · b

The fraction operator, and the implicit multiplication operator: (in the case of the implicit multiplication operator, the space between the two quantities can be crucual if they are both letter variables or numbers):

a over b, a b

\frac{a}{b}, ab

The relational operators:

a = b, a eq b, a <= b, a le b, a >= b, a > b,

a = b, a = b, a ≤ b, a ≤ b, a ≥ b, a > b,

a <> b, a ne b, a < b, a lt b, a > b, a gt b

a ≠ b, a ≠ b, a < b, a < b, a > b, a > b

Other operators (the last one tells you that the comma is considered as an operator):

a _b, a sub b, a subst b, a -> b, a to b, a tendsto b,
a , b, a comma b

a_{b}, a_{b}, a|_{b} , a → b, a → b, a → b, a, b, a, b

The plus and minus can also appear at the beginning of some expressions:

a * (-b) , a^+b

a × (−b) , a^{+b}

Operations can appear right at the beginning of the formula, like comma, the relational operators, and plus/minus.

= a + b

= a + b

Also, an operation can be left ‘dangling’ at the end of input:

a+

a+

The value of these last two allowable activities is to let long formulae range over several lines. This is illustrated in the long example given is Section 2.

Finally, the operations plus and minus may be used in a contex where they are treated as quantities. This allows expressions like

a to 4^+ , b = 3_-

a → 4^{+}, b = 3_{−}

3.3 Order of Operations and Brackets

Natural Math does a careful analysis, pulling apart the expressions so as to figure out what comes first. So in the following example, the division is done before the addition.

a + b over c

a + \frac{b}{c}

You can change the order of operations: in this example the addition is done before the division.

(a + b) over c

\frac{a + b}{c}

Here is another example.

(a+b) times c

(a + b) × c

In this last example, the brackets appeared when typeset. Usually, brackets written in will appear as you wrote them:

(a b)c

(ab) c

In a few cases, brackets are needed to change the natural order of doing operations, but it would not be right to typeset them. This happens with fractions (as above), and also with powers and subscripts. (It also happens with the square root and absolute value, and with limits of integration, and with the substitution operator — see below.) However, you can always force the brackets to appear in this situation by adding an extra pair of unneccesary brackets:

a^(b+c), a_(b+c)

a^{b+c}, a_{b+c}

((x+y)) over ((x^2 - y^2)) , f^((2))(x)

\frac{(x + y)}{(x^{2} − y^{2})}, f^{(2)} (x)

There are also square brackets:

[ x over y ]

[\frac{x}{y}]

What is the order in which natural math would evaluate the operations without brackets? First powers and subscripts. Then fractions. Then multiplication and division. Then addition and subtraction. Then the ‘tends to’. Then the ‘comma’. Finally the relational operators. Otherwise the operations are performed left to right, except that the power and subscript operators are performed from right to left.

a^b^c^d , a_b_c_d

a^{b^{c^{d}}}, a_{b_{c_{d}}}

Here is an example with the substitution operator. Notice that in this case, a rather large number of brackets is needed. Natural Math has its limitations!

((df^-1(x)) over dx) subst (x=f(a))
=
1 over (((df(x)) over dx) subst (x = a))

\frac{df^{−1} (x)}{dx}|_{x=f(a)} = \frac{1}{\frac{df(x)}{dx} |_{x=a}}

3.4 Functions

Natural Math supports a range of functions

sqrt a, abs a, |a|, a squared, a !, a factorial

\sqrt{a}, |a| , |a| , a^{2}, a!, a!

and the trig and hyperbolic functions:

sin, cos, tan, sec, csc, cot,
arcsin, arccos, arctan,
sinh, cosh, tanh, coth

sin, cos, tan, sec, csc, cot, arcsin, arccos, arctan, sinh, cosh, tanh, coth

and functions that you can create yourself, either by using quotes, or by using the “textsymb” command:

"sech"(x) = textsym sech(x) = 2 over (e^x + e^-x)

sech (x) = sech (x) = \frac{2}{e^{x} + e^{−x}}

Some of these functions interact with brackets in interesting ways:

sqrt(a+b) , sqrt((a+b)) , abs(a+b) , abs((a+b))

\sqrt{a + b}, \sqrt{(a + b)}, |a + b| , |(a + b)|

The absolute value construction is even more interesting, and there is a potential for ambiguity: does |a|b|c| represent abs(a abs(b) c), or abs(a) b abs(c)? Natural Math will use the second interpretation, but this can be changed using brackets:

|x over y| 5 |x over y|,
|(x over y| 5 |x over y)|

|\frac{x}{y}| 5 |\frac{x}{y}| , |\frac{x}{y} |5| \frac{x}{y}|

Finally, the trig functions can be raised to powers:

sin^2 x + cos^2 x = 1,
sin^-1 (sqrt3 over 2) = pi over 3

sin^{2}x + cos^{2}x = 1, sin^{−1} (\frac{\sqrt{3}}{2}) = \frac{π}{3}

3.5 Integrals, Sums and Limits

Here is the integral symbol

integral

∫

Here is the integral sign used: note the optional use of ‘of’. Also ‘dx’ is a single symbol. It can be expressed as two separate symbols, but the use of the single symbol slightly improves the typesetting.

integral x^3 dx,
integral of x^3 dx,
integral of x^3 d x

∫ x^{3} dx, ∫ x^{3} dx, ∫ x^{3}dx

Definite integrals are a little trickier, because Natural Math has to figure out what should be in the limits. It will make an intelligent guess, but sometimes it needs help. This can be provided, either with brackets, or with ‘of’.

integral from 1 to a + b x^3 dx,
integral from 1 to a b x^3 dx,
integral from 1 to (a b) x^3 dx,
integral from 1 to a b of x^3 dx

∫_{1}^{a+b} x^{3} dx, ∫_{1}^{a} bx^{3} dx, ∫_{1}^{ab} x^{3} dx, ∫_{1}^{ab} x^{3} dx

Sums are exactly the same. The following example shows that you don’t need to use both of the ‘from’ and ‘to’ quantifiers.

sum from n <= 20 of a_n

∑_{n≤20} a_{n}

Limits are similar: we have the ‘as’ quanitifier:

rho = lim as n to infinity | a_(n+1) over a_n | ,
rho = lim as n to infinity of | a_(n+1) over a_n | ,

ρ = lim_{n→∞} |\frac{a_{n+1}}{a_{n}} | , ρ = lim_{n→∞} |\frac{a_{n+1}}{a_{n}} | ,

‘From’ and ‘to’ may also be used with brackets (both round and square), although instead of ‘of’ you can use ‘end’.

[ x^3 ] from 0 to 6 / a end ,
[ x^3 ] from 0 to 6 / a ,
[ x^3 ] from 0 to (6 / a)

[x^{3}]_{0}^{6/a} , [x^{3}]_{0}^{6} /a, [x^{3}]_{0}^{6/a}

More examples:

integral u dv over dx dx = u v - integral v du over dx dx

∫ u \frac{dv}{dx} dx = uv − ∫ v \frac{du}{dx} dx

integral from 0 to 10 of theta^3 dtheta
=
[x^4 over 4] from 0 to 10
=
10^4 over 4 - 0^4 over 4
=
1000 over 4

∫_{0}^{10} θ^{3} dθ = [\frac{x^{4}}{4} ]_{0}^{10} = \frac{10^{4}}{4} − \frac{0^{4}}{4} = \frac{1000}{4}

integral from -1 to 1 1 over x^(2/3) dx
=
limit as a to 0^- of
integral from -1 to a 1 over x^(2/3) dx
+
limit as b to 0^+ of
integral from b to 1 1 over x^(2/3) dx

∫_{−1}^{1} \frac{1}{x^{2/3}} dx = lim_{a→0^{−}} ∫_{−1}^{a} \frac{1}{x^{2/3}} dx + lim_{b→0^{+}} ∫_{b}^{1} \frac{1}{x^{2/3}} dx

=
limit as a to 0^- of
[3 x^(1/3) ] from -1 to a
+
limit as b to 0^+ of
[3 x^(1/3) ] from b to 1

= lim_{a→0^{−}} [3x^{1/3}]_{−1}^{a} + lim_{b→0^{+}} [3x^{1/3}]_{b}^{1}

Note that in the last example, the use of the ‘of’s is rather crucial. See what happens if we don’t use them:

=
limit as a to 0^-
[3 x^(1/3) ] from -1 to a
+
limit as b to 0^+
[3 x^(1/3) ] from b to 1

= lim_{a→0^{−[3x^{1/3}]{a \atop −1}}+lim_{b→0^{+[3x^{1/3}]{1 \atop b}}}}

3.6 Inserting Text

To put a paragraph of text in your output, start the line with the word text. The text following, and the following non-blank lines will be inserted directly into the LaTEX file. Indeed, if you know LaTEX, you can even use LaTEX commands. (We won’t illustrate that here, but that is how this document was created.)

text Here are some lines
of text.  How do they look?

Here are some lines of text. How do they look?

You can also insert single words into formulae as follows:

1 over x to 0 text as x to infinity

\frac{1}{x} → 0 as x → ∞

or several words

x over (x^2 + 1) text (grows at the same rate as) 1 over x

\frac{x}{x^{2} + 1} grows at the same rate as \frac{1}{x}

You can also do this using quotes (note the spaces between the quotes and the words — it will look different if they are not there):

x over (x^2 + 1) " grows at the same rate as " 1 over x

\frac{x}{x^{2} + 1} grows at the same rate as \frac{1}{x}

These commands are very fussy - they must have only letter based text in them. Otherwise you get an error message, which brings us to the next topic.

3.7 Error Messages

If Natural Math finds an error, it will spit out the part of the lines it was able to process, and then follow it with a kind of descriptive error message, including a rough idea of which line number it was in the .nat file where the error happened. This same error will also be written on the command line at which you ran naturalmath.

Here are examples:

These errors were inserted deliberately.

These errors were inserted deliberately.
^
Error: what's this: `These' just before line 614
16 * x -
1 + 2 over (x + yy) - 11.2235 +
24 / 13
1 + 2 over (x + yy) - 11.2235 +
                ^
Error: what's this: `yy' just before line 619
(1+2
(1+2
    ^
Error: right bracket missing just before line 622

3.8 Debug and Newpage and Comments

Finally, if you want to see how you wrote the command along with the typeset version, put the word debug at the beginning of your math. That is how this tutorial was created.

debug x + y

will produce

x + y

x + y

To put comments in the .nat file, note that any line beginning with # will not be processed by Natural Math.

To start a new page, issue the one line command (followed by a blank line)

newpage

4 Want More Features? Bugs to report?

Obviously, if you want to make really complicated math formulae, or have more control over how it looks, you should learn the TEX, AMSTEX or LaTEX programs.

Otherwise email Stephen Montgomery-Smith at

                  stephen@math.missouri.edu.

Same if you have bug reports. Also, if you solve bugs, or made improvements, please, please tell me about it.