# `densematrix`

Create a matrix or a vector

MuPAD® notebooks will be removed in a future release. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```densematrix(`Array`)
densematrix(`List`)
densematrix(`ListOfRows`)
densematrix(`Matrix`)
densematrix(`m`, `n`)
densematrix(`m`, `n`, `Array`)
densematrix(`m`, `n`, `List`)
densematrix(`m`, `n`, `ListOfRows`)
densematrix(`m`, `n`, `f`)
densematrix(`m`, `n`, `List`, Diagonal)
densematrix(`m`, `n`, `g`, Diagonal)
densematrix(`m`, `n`, `List`, Banded)
densematrix(`1`, `n`, `Array`)
densematrix(`1`, `n`, `List`)
densematrix(`m`, `1`, `Array`)
densematrix(`m`, `1`, `List`)
```

## Description

```densematrix(m, n, [[a11, a12, ...], [a21, a22, ...], ...])``` returns the m×n matrix .

`densematrix(n, 1, [a1, a2, ...])` returns the n×1 column vector .

`densematrix(1, n, [a1, a2, ...])` returns the n row vector .

`densematrix` creates matrices and vectors. A vector with n entries is either an n×1 matrix (a column vector) or a n matrix (a row vector).

Matrix and vector components must be arithmetical expressions. For specific component domains, refer to the help page of `Dom::DenseMatrix`.

Arithmetical operations with matrices can be performed by using the standard arithmetical operators of MuPAD®.

E.g., if `A` and `B` are two matrices defined by `densematrix`, then ```A + B``` computes the sum and `A * B` computes the product of the two matrices, provided that the dimensions are correct.

Similarly, `A^(-1)` or `1/A` computes the inverse of a square matrix `A` if it exists. Otherwise, `FAIL` is returned.

See Example 1.

Many system functions accept matrices as input, such as `map`, `subs`, `has`, `zip`, `conjugate` to compute the complex conjugate of a matrix, `norm` to compute matrix norms, or even `exp` to compute the exponential of a matrix. See Example 4.

Most of the functions in the MuPAD linear algebra package linalg work with matrices. For example, the command `linalg::gaussJordan(A)` performs Gauss-Jordan elimination on `A` to transform `A` to its reduced row echelon form. See Example 2.

See the help page of linalg for a list of available functions of this package.

`densematrix` is an abbreviation for the domain `Dom::DenseMatrix``()`. You find more information about this data type for matrices on the corresponding help page.

Matrix components can be extracted by the usual index operator `[ ]`, which also works for lists, arrays, hfarrays, and tables. The call ```A[i, j]``` extracts the matrix component in the ith row and the jth column.

Assignments to matrix components are performed similarly. The call `A[i, j] := c` replaces the matrix component in the ith row and the jth column of A by `c`.

If one of the indices is not in its valid range, then an error message is issued.

The index operator also extracts submatrices. The call ```A[r1..r2, c1..c2]``` creates the submatrix of A comprising the rows with the indices r1, r1 + 1, …, r2 and the columns with the indices c1, c1 + 1, …, c2 of A.

See  Example 3 and Example 5.

`densematrix(Array)` or `densematrix(Matrix)` create a new matrix with the same dimension and the components of `Array` or `Matrix`, respectively. The array must not contain any uninitialized entries. If `Array` is one-dimensional, then the result is a column vector. Cf. Example 7.

`densematrix(List)` creates an m×1 column vector with components taken from the nonempty list, where m is the number of entries of `List`. See Example 5.

`densematrix(ListOfRows)` creates an m×n matrix with components taken from the nested list`ListOfRows`, where m is the number of inner lists of `ListOfRows`, and n is the maximal number of elements of an inner list. Each inner list corresponds to a row of the matrix. Both m and n must be non-zero.

If an inner list has less than n entries, then the remaining components in the corresponding row of the matrix are set to zero. See Example 6.

It might be a good idea first to create a two-dimensional array from that list before calling `densematrix`. This is due to the fact that creating a matrix from an array is the fastest way one can achieve. However, in this case the sublists must have the same number of elements.

The call `densematrix(m, n)` returns the m×n zero matrix.

The call `densematrix(m, n, Array)` creates an m×n matrix with components taken from `Array`, which must be an array or an hfarray. `Array` must have mn operands. The first m operands define the first row, the next m operands define the second row, etc. The formatting of the array is irrelevant. E.g., any array with 6 elements can be used to create matrices of dimension 1 ×6, or 2×3, or 3×2, or 6 ×1.

`densematrix(m, n, List)` creates an m×n matrix with components taken row after row from the non-empty list. The list must contain mn entries. Cf. Example 6.

`densematrix(m, n, ListOfRows)` creates an m×n matrix with components taken from the list `ListOfRows`.

If m ≥ 2 and n ≥ 2, then `ListOfRows` must consist of at most `m` inner lists, each having at most `n` entries. The inner lists correspond to the rows of the returned matrix.

If an inner list has less than `n` entries, then the remaining components of the corresponding row of the matrix are set to zero. If there are less than `m` inner lists, then the remaining lower rows of the matrix are filled with zeroes. See Example 6.

`densematrix(m, n, f)` returns the matrix whose (i, j)th component is `f(i,j)`. The row index i runs from 1 to m and the column index j from 1 to n. See Example 8.

`densematrix(m, 1, Array)` returns the m×1 column vector with components taken from `Array`. The `array` or `hfarray` `Array` must have `m` entries.

`densematrix(m, 1, List)` returns the m×1 column vector with components taken from `List`. The list `List` must have at most `m` entries. If there are fewer entries, then the remaining vector components are set to zero. See Example 5.

`densematrix(1, n, Array)` returns the 1 ×n row vector with components taken from `Array`. The `array` or `hfarray` `Array` must have `n` entries.

`densematrix(1, n, List)` returns the 1 ×n row vector with components taken from `List`. The list `List` must have at most `n` entries. If there are fewer entries, then the remaining vector components are set to zero. See Example 5.

### Note

The components of a matrix are no longer evaluated after the creation of the matrix, i.e., if they contain free identifiers they will not be replaced by their values.

## Examples

### Example 1

We create the 2×2 matrix by passing a list of two rows to `densematrix`, where each row is a list of two elements, as follows:

`A := densematrix([[1, 5], [2, 3]])`
` `

In the same way, we generate the following 2 ×3 matrix:

`B := densematrix([[-1, 5/2, 3], [1/3, 0, 2/5]])`
` `

We can do matrix arithmetic using the standard arithmetical operators of MuPAD. For example, the matrix product AB, the 4th power of A, and the scalar multiplication of A by are given by:

`A * B, A^4, 1/3 * A`
` `

Since the dimensions of the matrices A and B differ, the sum of A and B is not defined and MuPAD returns an error message:

`A + B`
```Error: Dimensions do not match. [(Dom::DenseMatrix(Dom::ExpressionField()))::_plus] ```

To compute the inverse of A, enter:

`1/A`
` `

If a matrix is not invertible, then the result of this operation is `FAIL`:

`C := densematrix([[2, 0], [0, 0]])`
` `
`C^(-1)`
` `

### Example 2

In addition to standard matrix arithmetic, the library linalg offers a lot of functions handling matrices. For example, the function `linalg::rank` determines the rank of a matrix:

`A := densematrix([[1, 5], [2, 3]])`
` `
`linalg::rank(A)`
` `

The function `linalg::eigenvectors` computes the eigenvalues and the eigenvectors of `A`:

`linalg::eigenvectors(A)`
` `

To determine the dimension of a matrix use the function `linalg::matdim`:

`linalg::matdim(A)`
` `

The result is a list of two positive integers, the row and column number of the matrix.

Use `info(linalg)` to obtain a list of available functions, or enter `?linalg` for details about this library.

### Example 3

Matrix entries can be accessed with the index operator `[ ]`:

`A := densematrix([[1, 2, 3, 4], [2, 0, 4, 1], [-1, 0, 5, 2]])`
` `
`A[2, 1] * A[1, 2] - A[3, 1] * A[1, 3]`
` `

You can redefine a matrix entry by assigning a value to it:

`A[1, 2] := a^2: A`
` `

The index operator can also be used to extract submatrices. The following call creates a copy of the submatrix of A comprising the second and the third row and the first three columns of A:

`A[2..3, 1..3]`
` `

The index operator does not allow to replace a submatrix of a given matrix by another matrix. Use `linalg::substitute` to achieve this.

### Example 4

Some system functions can be applied to matrices. For example, if you have a matrix with symbolic entries and want to have all entries in expanded form, simply apply the function `expand`:

```delete a, b: A := densematrix([ [(a - b)^2, a^2 + b^2], [a^2 + b^2, (a - b)*(a + b)] ])```
` `
`expand(A)`
` `

You can differentiate all matrix components with respect to some indeterminate:

`diff(A, a)`
` `

The following command evaluates all matrix components at a given point:

`subs(A, a = 1, b = -1)`
` `

Note that the function `subs` does not evaluate the result of the substitution. For example, we define the following matrix:

`A := densematrix([[sin(x), x], [x, cos(x)]])`
` `

Then we substitute x = 0 in each matrix component:

`B := subs(A, x = 0)`
` `

You see that the matrix components are not evaluated completely: for example, if you enter `sin(0)` directly, it evaluates to zero.

The function `eval` can be used to evaluate the result of the function `subs`. However, `eval` does not operate on matrices directly, and you must use the function `map` to apply the function `eval` to each matrix component:

`map(B, eval)`
` `

The function `zip` can be applied to matrices. The following call combines two matrices A and B by dividing each component of A by the corresponding component of B:

```A := densematrix([[4, 2], [9, 3]]): B := densematrix([[2, 1], [3, -1]]): zip(A, B, `/`)```
` `

### Example 5

A vector is either an m×1 matrix (a column vector) or a n matrix (a row vector). To create a vector with `densematrix`, pass the dimension of the vector and a list of vector components as argument to `densematrix`:

```row_vector := densematrix(1, 3, [1, 2, 3]); column_vector := densematrix(3, 1, [1, 2, 3])```
` `
` `

If the only argument of `densematrix` is a non-nested list or a one-dimensional array, then the result is a column vector:

`densematrix([1, 2, 3])`
` `

For a row vector `r`, the calls ```r[1, i]``` and `r[i]` both return the ith vector component of `r`. Similarly, for a column vector `c`, the calls `c[i, 1]` and `c[i]` both return the ith vector component of `c`.

For example, to extract the second component of the vectors `row_vector` and `column_vector`, we enter:

`row_vector, column_vector`
` `

Use the function `linalg::vecdim` to determine the number of components of a vector:

`linalg::vecdim(row_vector), linalg::vecdim(column_vector)`
` `

The number of components of a vector can also be determined directly by the call `nops(vector)`.

The dimension of a vector can be determined as described above in the case of matrices:

```linalg::matdim(row_vector), linalg::matdim(column_vector)```
` `

See the linalg package for functions working with vectors, and the help page of `norm` for computing vector norms.

### Example 6

In the following examples, we illustrate various calls of `densematrix` as described above. We start by passing a nested list to `densematrix`, where each inner list corresponds to a row of the matrix:

`densematrix([[1, 2], ])`
` `

The number of rows of the created matrix is the number of inner lists, namely m = 2. The number of columns is determined by the maximal number of entries of an inner list. In the example above, the first list is the longest one, and hence n = 2. The second list has only one element, and therefore the second entry in the second row of the returned matrix was set to zero.

In the following call, we use the same nested list, but in addition pass two dimension parameters to create a 4×4 matrix:

`densematrix(4, 4, [[1, 2], ])`
` `

In this case, the dimension of the matrix is given by the dimension parameters. As before, missing entries in an inner list correspond to zero, and in addition missing rows are treated as zero rows.

If the dimension m×n of the matrix is stated explicitly, the entries may also be specified by a plain list with mn elements. The matrix is filled with these elements row by row:

`densematrix(2, 3, [1, 2, 3, 4, 5, 6])`
` `
`densematrix(3, 2, [1, 2, 3, 4, 5, 6])`
` `

### Example 7

A one- or two-dimensional array of arithmetical expressions, such as:

```a := array(1..3, 2..4, [[ 1, 1/3 , 0 ], [-2, 3/5 , 1/2], [-3/2, 0 , -1 ]])```
` `

can be converted into a matrix as follows:

`A := densematrix(a)`
` `

Arrays serve, for example, as an efficient structured data type for programming. However, arrays do not have any algebraic meaning, and no mathematical operations are defined for them. If you convert an array into a matrix, you can use the full functionality defined for matrices as described above. For example, let us compute the matrix 2 A - A2 and the Frobenius norm of A:

`2*A - A^2, norm(A, Frobenius)`
` `

Note that an array may contain uninitialized entries:

`b := array(1..4): b := 2: b := 0: b`
` `

`densematrix` cannot handle arrays that have uninitialized entries, and responds with an error message:

`densematrix(b)`
```Error: Unable to define a matrix over 'Dom::ExpressionField()'. [(Dom::DenseMatrix(Dom::ExpressionField()))::new] ```

We initialize the remaining entries of the array `b` and convert it into a matrix, or more precisely, into a column vector:

`b := 0: b := -1: densematrix(b)`
` `

### Example 8

We show how to create a matrix whose components are defined by a function of the row and the column index. The entry in the ith row and the jth column of a Hilbert matrix (see also `linalg::hilbert`) is . Thus the following command creates a 2×2 Hilbert matrix:

`densematrix(2, 2, (i, j) -> 1/(i + j - 1))`
` `

The following two calls produce different results. In the first call, `x` is regarded as an unknown function, while it is a constant in the second call:

```delete x: densematrix(2, 2, x), densematrix(2, 2, (i, j) -> x)```
` `

### Example 9

Diagonal matrices can be created by passing the option `Diagonal` and a list of diagonal entries:

`densematrix(3, 4, [1, 2, 3], Diagonal)`
` `

Hence, you can generate the 3×3 identity matrix as follows:

`densematrix(3, 3, [1 \$ 3], Diagonal)`
` `

Equivalently, you can use a function of one argument:

`densematrix(3, 3, i -> 1, Diagonal)`
` `

Since the integer `1` also represents a constant function, the following shorter call creates the same matrix:

`densematrix(3, 3, 1, Diagonal)`
` `

### Example 10

Banded Toeplitz matrices (see above) can be created with the option `Banded`. The following command creates a matrix of bandwidth 3 with all main diagonal entries equal to 2 and all entries on the first sub- and superdiagonal equal to - 1:

`densematrix(4, 4, [-1, 2, -1], Banded)`
` `

## Parameters

 `Array` A one- or two-dimensional array of type `DOM_ARRAY` or `DOM_HFARRAY` `List` `ListOfRows` A nested list of rows, each row being a list of arithmetical expressions `Matrix` A matrix, i.e., an object of a data type of category `Cat::Matrix` `m` The number of rows: a positive integer `n` The number of columns: a positive integer `f` A function or a functional expression of two arguments `g` A function or a functional expression of one argument

## Options

 `Diagonal` Create a diagonal matrix With the option `Diagonal`, diagonal matrices can be created with diagonal elements taken from a list, or computed by a function or a functional expression. `densematrix(m, n, List, Diagonal)` creates the m×n diagonal matrix whose diagonal elements are the entries of `List`. See Example 9. `List` must have at most min(m, n) entries. If it has fewer elements, then the remaining diagonal elements are set to zero. `densematrix(m, n, g, Diagonal)` returns the matrix whose ith diagonal element is `g(i, i)`, where the index i runs from 1 to min(m, n). See Example 9. `Banded` Create a banded Toeplitz matrix A banded matrix has all entries zero outside the main diagonal and some of the adjacent sub- and superdiagonals. `densematrix(m, n, List, Banded)` creates an m×n banded Toeplitz matrix with the elements of `List` as entries. The number of entries of `List` must be odd, say 2 h + 1, and must not exceed `n`. The bandwidth of the resulting matrix is at most h. All elements of the main diagonal of the created matrix are initialized with the middle element of `List`. All elements of the ith subdiagonal are initialized with the (h + 1 - i)th element of `List`. All elements of the ith superdiagonal are initialized with the (h + 1 + i)th element of `List`. All entries on the remaining sub- and superdiagonals are set to zero. See Example 10.

## Return Values

Matrix of the domain type`Dom::DenseMatrix``()`.