# Special matrices

### 5.44.2 Special matrices

#### Identity matrix: idn identity

The idn command finds identity matrices.

- idn takes one argument:
  n, a positive integer or
  A, a square matrix.
- idn(n) returns the n× n identity matrix.
- idn(A) returns the identity matrix the same size as A.

#### Examples.

- Input:
  idn(3)
  Output:

  ⎛
  ⎜
  ⎜
  ⎝

  1 | 0 | 0
  0 | 1 | 0
  0 | 0 | 1

  ⎞
  ⎟
  ⎟
  ⎠

- Input:
  idn([[2,3],[4,5]]
  Output:

  ⎛
  ⎜
  ⎝

  1 | 0
  0 | 1

  ⎞
  ⎟
  ⎠

#### Zero matrix: newMat

The newMat command creates a matrix of all 0s.

- newMat takes two arguments:
  n and p, two positive integers. newMat(n,p) returns the n× p zero matrix.

#### Example.

Input:

newMat(4,4)

Output:

⎡
⎢
⎢
⎢
⎣

0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0
0 | 0 | 0 | 0

⎤
⎥
⎥
⎥
⎦

#### Diagonals of matrices and diagonal matrices: BlockDiagonal diag

The diag command either creates a diagonal matrix or finds the diagonal elements of an existing matrix.
BlockDiagonal is a synonym for diag.

- diag takes one argument:
  L, a list or a square matrix.
- diag(L) (for a list L) returns the diagonal matrix with the entries of L on the diagonal.
- diag(L) (for a matrix L) returns a list consisting of the diagonal elements of L.

#### Examples.

- Input:
  diag([1,4])
  Output:

  ⎡
  ⎢
  ⎣

  1 | 0
  0 | 4

  ⎤
  ⎥
  ⎦

- Input:
  diag([[1,2],[3,4]])
  Output:

  ⎡ ⎣ | 1,4 | ⎤ ⎦

#### Jordan block: JordanBlock

The JordanBlock command creates a Jordan Block; i.e., a square matrix with the same value for all diagonal elements, 1s just above the diagonal, and 0s everyone else.

- JordanBlock takes two arguments:
  - a, an expression.
  - n, a positive integer.
  JordanBlock(a,n) returns the n× n matrix with as on the principal diagonal, 1s above this diagonal and 0s everywhere else.

#### Example.

Input:

JordanBlock(7,3)

Output:

⎡
⎢
⎢
⎣

7 | 1 | 0
0 | 7 | 1
0 | 0 | 7

⎤
⎥
⎥
⎦

#### Hilbert matrix: hilbert

A Hilbert matrix is a square matrix whose element in the ith row and jth column (recall the numbering starting at 0) is

aj,k=

1
j+k+1

The hilbert command finds Hilbert matrices.

- hilbert takes one argument:
  n, a positive integer.
- hilbert(n) returns the n× n Hilbert matrix.

#### Example.

Input:

hilbert(4)

Output:

⎛
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎜
⎝

1

1
2

1
3

1
4

1
2

1
3

1
4

1
5

1
3

1
4

1
5

1
6

1
4

1
5

1
6

1
7

⎞
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎟
⎠

#### Vandermonde matrix: vandermonde

A Vandermonde matrix is a square matrix where each row starts with a 1 and is in geometric progression. The vandermonde command finds a Vandermonde matrix.

- vandermonde takes one argument:
  X=[x0,…,xn−1], a vector.
- vandermonde(X) returns the corresponding Vandermonde matrix; namely, the k-th row of the matrix is the vector whose components are xik for i=0..n−1 and k=0..n−1.

Warning!
The indices of the rows and columns begin at 0 with Xcas.

#### Example.

Input:

vandermonde([a,2,3])

Output (if a is symbolic else purge(a)):

⎛
⎜
⎜
⎝

1 | a | a a
1 | 2 | 4
1 | 3 | 9

⎞
⎟
⎟
⎠