Matrices in Mathematica can be constructed from all the different types of objects that Mathematica holds. They can contain machine-precision real and complex floating-point numbers, arbitrary-precision real and complex floating-point numbers, integers, rational numbers, and general symbolic quantities. This tutorial considers the different types of matrices that Mathematica supports.
Numbers in Mathematica
In order to understand the different types of matrices that Mathematica can work with, it will help to get an overview of the different types of numbers that are supported. This section only gives an overview of numbers in Mathematica; further information is found in "Numbers".
Mathematica provides an arithmetic system that unifies all these different types of numbers. For many purposes it is not necessary to know that there are two different types of integer because both types flow together. For example, the integer is represented by machine hardware. If this is raised to the tenth power, the result is represented by software:
Another example shows how a computation of the exponential of a machine-precision floating-point number returns an arbitrary-precision number:
These examples demonstrate the general point that Mathematica arithmetic traps overflows and underflows so that it can switch from machine to software numbers. Despite this it tries to use machine hardware functionality whenever possible, so that Mathematica computations can be fast.
Exact versus Approximate Numbers
One important issue for Mathematica is that it makes a distinction between exact and approximate numbers. This is done to support the symbolic side of Mathematica. This example compares the computation of the Sin function for an integer and a real argument:
This demonstrates that mathematical functions, such as Sin, maintain the nature of their input. If they get an exact argument, the result will be exact. If they get an approximate argument the result is approximate. This feature of Mathematica is essential to maintain the system as a symbolic system, but it stands in contrast to the operation of a purely numerical environment. For example, in many languages such as C or Java the equivalent computation would coerce the integer to a double and the result would be an approximate number.
This distinction means that when you work with numbers you need to be more aware of their types than is the case in many other languages.
You can use N to compute a numerical approximation for an exact number:
Mixed Mode Arithmetic
Another important issue concerns the operation of functions that take more than one argument. In this case Mathematica takes the lowest precision of the input. For example, the following mixed computations all return approximate results:
The same principle applies if the input includes an exact integer represented by software:
Matrices in Mathematica
Matrices in Mathematica can contain many different types of elements.
For example, here is a matrix where each element is a machine-precision approximate real number:
This matrix contains complex numbers with real and imaginary parts that are machine-precision approximate real numbers:
This matrix contains approximate numbers with 20 digits of precision:
This matrix contains integers:
This matrix contains rational numbers:
This matrix contains complex numbers with real and imaginary parts that are integers:
This matrix contains polynomials:
These different types of matrices can be put into categories according to the types of technique used for computing with them.
These three different categories are briefly reviewed.
Standard Numerical Techniques
In Mathematica, matrix computations involving machine-precision Real and machine-precision approximate Complex numbers are carried out with standard numerical techniques. In many cases computations involve optimized libraries, many of which are described in "Software References".
An important goal for many of these computations is to match and surpass the performance of any software package that is dedicated to machine-precision numbers. This is in keeping with the design goals of Mathematica, as described under "Design Principles of Mathematica". In the case of linear algebra computations, Mathematica makes use of a considerable amount of sophisticated technology, some of which is described under "Performance of Linear Algebra Computation".
Arbitrary-Precision Numerical Techniques
In Mathematica, matrix computations involving arbitrary-precision Real and arbitrary-precision approximate Complex numbers are carried out with special numerical libraries. These libraries are adapted from standard libraries so they can work for arbitrary-precision computations.
Matrix computations involving exact numbers and general symbolic techniques are carried out with computer algebra techniques.
All computations provided for numerical matrices are also available for symbolic matrices. This meets the design goal of integrating symbolic and numerical computation.
Mixed Mode Matrices
This section describes what happens if the input matrix contains a mixture of different types of entries.
If the matrix contains anything that is not a number (or something that cannot be converted into a number), the matrix will be treated as a symbolic matrix and the computation will proceed with symbolic techniques. In the following example, the presence of the x makes the matrix symbolic and the result is a symbolic matrix:
If the matrix only contains numbers (or things that can be converted into numbers), the computation will be done according to the lowest precision. Thus in the following example, the presence of the machine-precision approximate number, 1., means that the input matrix will be converted to machine-precision numbers and the computation is done with machine-precision techniques:
In this example, the lowest precision is a number with 20 digits of precision. Thus, the input matrix will be converted to precision 20 numbers and the computation done with arbitrary-precision techniques:
The resulting matrix has entries with 20 digits of precision:
Note that machine-precision counts as lower precision than any arbitrary-precision numbers. In this example, because there is a machine-precision number, the input matrix will be converted to machine-precision numbers and the computation done with machine-precision techniques:
The result is a machine-precision matrix:
It is important to be aware of these distinctions. For example, if the input matrix consists of integers, the computation will use symbolic algebra techniques and return an exact precision result:
In some other computation systems an input matrix of integers would be converted to a floating-point matrix. Mathematica does not do this, so that it can support symbolic computation. However, it means that users need to be aware of the differences. This is because the time required for a symbolic technique can be quite different from a machine-precision technique; in many cases the symbolic computation will take much longer.
Complex matrices in Mathematica are defined in a way that is completely consistent with other types of matrices such as those that involve real numbers. All the functions that work for real matrices work for complex matrices.
One special detail about complex matrices is that certain linear algebra computations that involve a transpose for real matrices involve a conjugate transpose for complex matrices. An example of this is the Cholesky decomposition. For a real symmetric positive definite matrix it is defined as shown:
For a complex Hermitian positive definite matrix the definition is shown as follows:
Mathematica arbitrary-precision computations support a form of arithmetic known as significance arithmetic. The fundamental idea of significance arithmetic is that a number is seen as an approximation with an error specified by its precision. When a number is used in a computation the precision of the result may be different according to the properties of the function. For example, here is a number with 30 digits of precision:
Now compute the value of the Sin function for this number:
The result is seen to be less than 30 digits:
In Mathematica, linear algebra computations generally do not use significance arithmetic. They are carried out in what is known as fixed precision, where computations are done with fixed numbers of digits with no attempt to track the error in individual operations. Error tracking is done on an algorithmic level, issuing warnings, for example, when the solution of a linear system may not be correct:
The inverse matrix that is computed consists entirely of numbers with precision 20: