Additional functionality related to this tutorial has been introduced in subsequent versions of Mathematica. For the latest information, see Matrices and Linear Algebra.

Matrix Types

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".

machine-sizeIntegerintegers represented by machine hardware
Integerarbitrarylength exact integer
Rational (fractions) in lowest terms
machine-sizeRealapproximate real number represented by machine hardware
Realapproximate real number, with any specified precision
Complexcomplex number of the form number+numberI
AlgebraicNumberroot of an algebraic equation
exact numerical expressioncomposition of numbers and mathematical functions
infinities and exceptionsvarious special numbers

Intrinsic types of numbers in Mathematica.

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.
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
Another example shows how a computation of the exponential of a machine-precision floating-point number returns an arbitrary-precision number.
In[3]:=
Click for copyable input
Out[3]=

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.
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=

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.
In[3]:=
Click for copyable input
Out[4]=

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.
In[1]:=
Click for copyable input
Out[1]=
In[2]:=
Click for copyable input
Out[2]=
The same principle applies if the input includes an exact integer represented by software.
In[3]:=
Click for copyable input
Out[3]=

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.
In[1]:=
Click for copyable input
Out[1]//MatrixForm=
This matrix contains complex numbers with real and imaginary parts that are machine-precision approximate real numbers.
In[2]:=
Click for copyable input
Out[2]//MatrixForm=
This matrix contains approximate numbers with 20 digits of precision.
In[3]:=
Click for copyable input
Out[3]//MatrixForm=
This matrix contains integers.
In[4]:=
Click for copyable input
Out[4]//MatrixForm=
This matrix contains rational numbers.
In[5]:=
Click for copyable input
Out[5]//MatrixForm=
This matrix contains complex numbers with real and imaginary parts that are integers.
In[6]:=
Click for copyable input
Out[6]//MatrixForm=
This matrix contains polynomials.
In[7]:=
Click for copyable input
Out[7]//MatrixForm=

These different types of matrices can be put into categories according to the types of technique used for computing with them.

machine-precisionRealstandard numerical techniques
machine-precisionComplex
arbitrary-precisionRealarbitraryprecision numerical techniques
arbitrary-precisionComplex
Integersymbolic algebra techniques
Rational
exactComplex
symbolic

Different types of matrices in Mathematica.

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.

More information can be found in the section "Arbitrary-Precision Matrices".

Symbolic Algebra Techniques

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 makes the matrix symbolic and the result is a symbolic matrix.
In[1]:=
Click for copyable input
Out[2]=
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[3]:=
Click for copyable input
Out[4]=
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.
In[5]:=
Click for copyable input
Out[6]=
The resulting matrix has entries with 20 digits of precision.
In[7]:=
Click for copyable input
Out[7]=
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.
In[8]:=
Click for copyable input
Out[9]=
The result is a machine-precision matrix.
In[10]:=
Click for copyable input
Out[10]=
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[11]:=
Click for copyable input
Out[12]=

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

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.
In[1]:=
Click for copyable input
Out[3]//MatrixForm=
For a complex Hermitian positive definite matrix the definition is shown as follows.
In[4]:=
Click for copyable input
Out[6]//MatrixForm=

Arbitrary-Precision Matrices

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.
In[1]:=
Click for copyable input
Out[1]=
Now compute the value of the Sin function for this number.
In[2]:=
Click for copyable input
Out[2]=
The result is seen to be less than 30 digits.
In[3]:=
Click for copyable input
Out[3]=
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.
In[4]:=
Click for copyable input
Out[5]=
The inverse matrix that is computed consists entirely of numbers with precision 20.
In[6]:=
Click for copyable input
Out[6]=