]>
Axiom provides you with a very large library of predefined operations and objects to compute with. You can use the Axiom library of constructors to create new objects dynamically of quite arbitrary complexity. For example, you can make lists of matrices of fractions of polynomials with complex floating point numbers as coefficients. Moreover, the library provides a wealth of operations that allow you to create and manipulate these objects.
For many applications, you need to interact with the interpreter and write some Axiom programs to tackle your application. Axiom allows you to write functions interactively, function thereby effectively extending the system library. Here we give a few simple examples, leaving the details to Chapter ugUser .
We begin by looking at several ways that you can define the ``factorial'' function in Axiom. The first way is to give a function:piece-wise definition piece-wise definition of the function. piece-wise function definition This method is best for a general recurrence relation since the pieces are gathered together and compiled into an efficient iterative function. Furthermore, enough previously computed values are automatically saved so that a subsequent call to the function can pick up from where it left off.
Define the value of fact at .
Define the value of fact(n) for general .
Ask for the value at . The resulting function created by Axiom computes the value by iteration.
A second definition uses an if-then-else and recursion.
This function is less efficient than the previous version since each iteration involves a recursive function call.
A third version directly uses iteration.
This is the least space-consumptive version.
A final version appears to construct a large list and then reduces over it with multiplication.
In fact, the resulting computation is optimized into an efficient iteration loop equivalent to that of the third version.
The library version uses an algorithm that is different from the four above because it highly optimizes the recurrence relation definition of factorial.
You are not limited to one-line functions in Axiom. If you place your function definitions in .input files file:input (see ugInOutIn ), you can have multi-line functions that use indentation for grouping.
Given elements, diagonalMatrix creates an by matrix with those elements down the diagonal. This function uses a permutation matrix that interchanges the th and th rows of a matrix by which it is right-multiplied.
This function definition shows a style of definition that can be used in .input files. Indentation is used to create blocks: sequences of expressions that are evaluated in sequence except as modified by control statements such as if-then-else and return.
This creates a four by four matrix that interchanges the second and third rows.
Create an example matrix to permute.
Interchange the second and third rows of m.
A function can also be passed as an argument to another function, which then applies the function or passes it off to some other function that does. You often have to declare the type of a function that has functional arguments.
This declares t to be a two-argument function that returns a Float. The first argument is a function that takes one Float argument and returns a Float.
This is the definition of t.
We have not defined a cos in the workspace. The one from the Axiom library will do.
Here we define our own (user-defined) function.
Pass this function as an argument to t.
Axiom also has pattern matching capabilities for simplification simplification pattern matching of expressions and for defining new functions by rules. For example, suppose that you want to apply regularly a transformation that groups together products of radicals: Note that such a transformation is not generally correct. Axiom never uses it automatically.
Give this rule the name groupSqrt.
Here is a test expression.
The rule groupSqrt successfully simplifies the expression.