# Introduction to Linear Algebra in Wolfram Language

Tensors and Arrays | Expression Input and Output |

Matrices as Wolfram Language Expressions | Design Principles of Wolfram Language |

Wolfram Language has a broad range of functions to support linear algebra operations and to integrate them into the system. It can work with vectors, matrices, and tensors that can contain machine-precision floating-point numbers, arbitrary-precision floating-point numbers, complex floating-point numbers, integers, rational numbers, and general symbolic quantities. Linear algebra operations are supported for matrices that contain all these different types of entry.

Wolfram Language supports both dense and sparse matrices. Several tutorials concentrate on dense matrices. Sparse matrices are documented under "Working with Sparse Arrays". In general, all the operations that work on dense matrices work on sparse matrices in an equivalent way.

Matrices are represented in Wolfram Language with lists. They can be entered directly with the { } notation that Wolfram Language provides for lists.

They can be created programmatically with Table.

You can extract size information.

It is also possible to test if something is a matrix.

MatrixForm helps to make the structure of the matrix clearer.

Matrices are important in many areas of computation because they are an efficient way to represent linear systems of equations. Many computer applications can work with matrices because they can work efficiently with arrays of numbers. However, Wolfram Language can also work directly with the systems of linear equations the matrices represent. Here, a matrix is multiplied by a vector of the unknowns and a system of equations is formed.

These equations can be solved with the algebraic equation solver, Solve.

Alternatively, the matrix representation of the equations can be solved directly with the LinearSolve command.

Conversion between matrices and the linear systems they represent is often useful in order to understand the underlying principles. Using one form or the other can also be a very useful way to set up particular problems.

## Tensors and Arrays

A matrix has two indices that label each entry. By convention the values of these indices label the row and column when the matrix is written in a tabular form. A generalization of matrices can have less than or more than two indices. Typically an object with no indices is called a scalar, and one with one index is called a vector. An object with more than two indices is called a tensor, although a scalar, vector, or matrix can also be called a tensor.

Wolfram Language works with tensors that have arbitrary numbers of indices with the same list structure that is used for matrices. The number of levels of lists represents the number of indices. For example, the following is a vector.

The following is a tensor with 3 indices, with lengths 2, 3, and 4, respectively. The number of indices required to label a tensor can be called the rank of the tensor.

It should be noted that the notion of a tensor as used in physics has additional properties beyond the labeling of elements in generalized matrices. This is discussed more in the description of tensors given in MathWorld. In Wolfram Language the term tensor is used to refer to generalized matrices.

Many of the Wolfram Language functions that operate on matrices are also generalized to work for vectors and tensors. This important principle will be demonstrated many times in this document, and is covered under "Matrix and Tensor Operations".

## Matrices as Wolfram Language Expressions

One important feature of matrices in Wolfram Language is that they are Wolfram Language expressions. This means that all the Wolfram Language commands that operate on Wolfram Language expressions work on matrices. The same principle applies to vectors and tensors. The fact that all objects in Wolfram Language have a common expression structure greatly enhances the expressive nature of Wolfram Language programming. This principle, sometimes called the fundamental principle of Wolfram Language, may seem abstract, uninteresting, or obvious to people who have a background in purely numerical programming, but users who have a background in object-oriented programming will recognize the benefits of a common parent object. Some examples of this principle will be discussed in this section.

As demonstrated earlier, a matrix can be entered with a list notation.

One important way to understand Wolfram Language expressions is through the function FullForm. This shows the literal details of how the expression is arranged in Wolfram Language. In this case, the tree structure of the matrix is seen. There is an outer node, which is a List; in Wolfram Language this is called having a head of List. The outer node has two arguments, each of which has the head List; each of these has three arguments that are integers.

The head of the matrix expression can be inspected with the function Head.

Length returns the number of arguments of the matrix expression.

Part can be used to extract elements of an expression.

Wolfram Language operations such as FullForm, Head, Length, and Part will work for any Wolfram Language expression. This is very convenient because it means that a common set of programming constructs will work for programming in many different applications, and not only for linear algebra. The same techniques apply to many other areas in Wolfram Language, for example, graphics programming, document programming, and symbolic algebra programming. These operations are often called structural operations because they work on the structure of Wolfram Language expressions. They can be contrasted with other operations that are specific to linear algebra. For example, the function MatrixQ is specific to matrix computation.

## Expression Input and Output

Wolfram Language contains commands for I/O on expressions. It also has many commands for I/O with specially formatted data. The commands that can work with formatted data are reviewed under "Import and Export of Matrices". In this section the Wolfram Language commands for I/O on expressions are reviewed.

The concept of the Wolfram Language FullForm is completely equivalent to that of serializable objects in Java or C#. The FullForm of any Wolfram Language expression is a complete specification of the expression. Saving the FullForm into a file and reading it back again will re-create the expression.

You can save a Wolfram Language expression into a file with the command Put, entered using >> as a shortcut notation.

The contents of the file can be inspected with FilePrint.

The file can be read back into Wolfram Language with Get, entered using << as a shortcut notation.

This restores the matrix from the file so that it can be used for further computation.

Another way to do I/O on matrices in Wolfram Language is to use the Wolfram Symbolic Transfer Protocol (WSTP). This is an interprocess communication mechanism that is particularly useful for communicating between different Wolfram Language sessions.

Using Put and Get to store matrices in files is most useful if you want to save work from Wolfram Language to be restored later. If you want to exchange matrices with other applications, data saved in general formats is more useful; this is described under "Import and Export of Matrices".

## Design Principles of Wolfram Language

The support for linear algebra demonstrates a number of important Wolfram Language design principles.

- It provides machine-precision computation that meets or surpasses the speed of dedicated machine-precision numerical applications.