## Computer-aided Mathematical Calculation [1]

The advent of electronic calculators made it easy to do arithmetic by computer, and made logarithm tables and the like obsolete. Now computer hardware is becoming powerful enough to be able to do not only arithmetic, but all kinds of conventional mathematics. But to realize this capability a computer language is needed. The language should be as close as possible to conventional mathematics, and should be able to incorporate as much mathematical knowledge as possible. Through its use, mathematical handbooks and tables of integrals should eventually become as obsolete as logarithm tables.

The language should be general, so that it can handle all kinds of mathematics. It should be interactive so that simple calculations are quick to do. It should be extensible, so that it can learn new mathematics. It should be efficient and heavy duty, so that it is able to do big calculations. And finally, it should be portable, and be able to run without significant change on many different kinds of computers.

There are numerical computer languages, such as FORTRAN, BASIC, APL and C. The intrinsic data types in these languages are numbers, or arrays of numbers. But mathematical calculations involve higher-level objects, such as algebraic formulae (say ). So to be able to handle many mathematical calculations one needs a language capable of symbolic manipulation of such objects.

Several ''computer algebra'' systems have been constructed. The prime examples are REDUCE and MACSYMA. The emphasis of these systems is on algebraic manipulation, involving calculations with polynomials and other algebraic objects. But to handle much of the mathematics that arises in practice, one needs a system with a more general basic structure. One needs a true general programming language for mathematical computation.

It was with this in mind that I designed the SMP language. SMP was first implemented in 1979--1981, in about 120000 lines of C code. It represents a general interactive system for mathematical computation. It incorporates a core of fundamental mathematical knowledge; new objects and operations can be defined in the SMP programming language. In addition, the SMP system includes such facilities as graphics and numerical C and FORTRAN code generation. There is also an expanding library of external files containing programs for specific applications.

Figure 1 shows an example of a dialogue with SMP, in which SMP is used much like a symbolic mathematical calculator. The SMP language is designed to be as close to conventional mathematical notation and usage as possible. It is strongly based on pattern matching.

The command a:x-1 assigns the symbolic expression x-1 to be the value of the symbol or ''variable'' a. So wherever a appears, it is replaced by the expression x-1. Now the assignments v[2]:x and v[3]:y define values for components of the list v. With these assignments the object v, which can be considered as a vector or array, is defined to have value {[3]:y,[2]:x}. Making the assignment v[p]:x+2 specifies a value for the element of v with symbolic index p. The value will be used whenever the literal expression v[p] appears. But no definition has been given for an expression like v[q]. To define a value for all ''projections'' of v, one makes the assignment v[\$x]:\$x^2. This specifies that the value v when indexed with any expression represented by the ''generic symbol'' \$x is the square of that expression. The assignment can thus be considered a definition for the ''function'' v. The notation makes it clear that a function can be viewed as an array with a continuous index. SMP always uses more specific definitions for a particular expression in preference to more general ones. Thus v[p] is replaced by x+2, but v[q] becomes q^2.

SMP includes a sophisticated pattern matcher, which makes it possible to implement complex transformation rules for patterns. So for example the definition Acos[Sqrt[1-\$x^2]]: Asin[\$x] specifies a simplification rule for inverse trigonometric functions. In general one can specify rules for mathematical functions and so on much as they are given in books of tables. Figure 2 shows an example.

The core of SMP incorporates a substantial body of mathematical knowledge and techniques. At the simplest level, it includes over 200 mathematical functions, covering for example all the standard special functions of mathematical physics (hypergeometric functions, elliptic functions, and so on). It incorporates many standard mathematical operations, including polynomial manipulation (expansion, factorization, etc.), equation solving (linear, polynomial, etc.), calculus (differentiation, integration, etc.) and series expansion (power series, rational approximations, continued fractions). A central part of many of these mathematical operations is simplification of expressions to canonical form. So for example both y+x+x+a and x+y+a+x are transformed to the canonical form a+2x+y. The derivative D[f[x],x] is transformed to the canonical form D[f[#1],{#1,1,x}] which displays explicitly the single differentiation with respect to a dummy variable #1 followed by evaluation at #1:x. This canonical form makes possible immediate definition of symbolic derivatives. So for example D[f[\$x],{\$x,1,\$y}]: g[\$y] defines the derivative of f to be g. With this definition, the standard pattern matching process transforms D[f[x^2]+f[3x],x] to 2x g[x] + 3g[x].

To allow for general mathematical operations, SMP is able to perform not only standard mathematical operations, but also purely structural operations on symbolic expressions. Expressions can be treated either as symbolic tree structures, or can be manipulated by essentially graphical means. SMP includes many list manipulation primitives, covering for example the capabilities of APL. For example, Ar[list specification,element specification] creates lists of particular structure and with particular elements. Thus Ar[4,f] yields the four-element list {f[1],f[2],f[3],f[4]}, while Ar[{2,2},g] yields the matrix {{g[1,1],g[1,2]},{g[2,1],g[2,2]}}. SMP includes a wide range of matrix manipulation functions. Ar can also be used to create higher rank tensors. So for example a general definition for the -dimensional Levi-Civita (totally antisymmetric) tensor can be given simply as Levi[\$n_=Natp[\$n]]: Ar[Ar[\$n,\$n],Sig], where \$n_=Natp[\$n] represents only expressions that satisfy the natural number predicate, and Sig signifies the signature function.

The definition of Levi just given is an example of a program in SMP. SMP has a wide range of programming constructs: its symbolic nature makes it substantial richer than numerical languages. SMP programs are usually built up interactively, with individual functions tested before being combined with others. This is made possible by the fact that any intermediate data corresponds to a legal SMP expression, which can be input or output like any other. And the powerful primitive functions of SMP are arranged to have expressions input and output in very standard forms, making them easy to string together into programs. Typical SMP programs make extensive use of pattern matching. Rather than having extensive conditional statements, they consist of a sequence of definitions to be used when they apply - a form much closer to that found in mathematical handbooks.

The input and output syntax of SMP can be defined by the user so as to be as close as possible to conventional notation. Two- and three-dimensional graphical output can also be obtained. The graphs are stored internally as symbolic expressions, and so can be manipulated for example by the same structural operations as are used for standard mathematical expressions.

Another capability of SMP, not directly linked to symbolic manipulation, but extremely significant in practice, is the ability to convert functions defined in SMP into C or FORTRAN programs which can then in turn be loaded into an SMP job. This capability makes it possible to write symbolic programs which can create programs needed for efficient numerical computation.

The kernel of SMP contains a core of basic mathematical knowledge. But the ultimate power of SMP lies in the ability to create programs for a wide range of mathematical problems. There is an expanding library of external files which contain definitions for particular applications. These files are indexed with a database system which uses keywords based on standard English language phrases. There are already several hundred external files, which serve for example to define new mathematical functions, specify relations and simplification rules for functions, catalogue mathematical, physical and other data, and to implement a wide variety of mathematical definitions and algorithms. With time, one can expect that much of the knowledge now found in mathematical handbooks can be put in this form, and accessed through SMP.