Matrices with Numpy

In linear algebra, various types of matrices play a central role. These include the zero matrix, identity matrix, null matrix, diagonal matrix, and other matrices. Numpy provides a straightforward way to create and manipulate these matrices.

Bernd Fischer

Bernd Fischer

11. Oct 2024 · 3 min read

Matrices with Numpy

1. The Zero Matrix

The zero matrix is a matrix in which all entries are equal to 0. In Numpy, it can be created using the function numpy.zeros.

Example: Creating a 3x3 Zero Matrix

import numpy as np

# Eine 3x3-Nullmatrix erstellen
null_matrix = np.zeros((3, 3))

# Visualisierung der Nullmatrix
print("Nullmatrix (3x3):\n", null_matrix)

Output:

Nullmatrix (3x3):
 [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

In this example, a 3x3 matrix is created with all entries having the value 0.

2. The Identity Matrix

The identity matrix is a square matrix where all entries along the main diagonal are 1 and all other entries are 0. It is often referred to as the identity matrix. In Numpy, it can be created with numpy.eye.

Example: Creating a 4x4 Identity Matrix

# Eine 4x4-Einheitsmatrix (Identitätsmatrix) erstellen
identity_matrix = np.eye(4)

# Visualisierung der Einheitsmatrix
print("Einheitsmatrix (4x4):\n", identity_matrix)

Output:

Einheitsmatrix (4x4):
 [[1. 0. 0. 0.]
  [0. 1. 0. 0.]
  [0. 0. 1. 0.]
  [0. 0. 0. 1.]]

Here, a 4x4 identity matrix is created, which is commonly used as the identity matrix in calculations.

3. Diagonal Matrix

A diagonal matrix is a matrix in which all elements outside the main diagonal are zero. The elements of the main diagonal can take any values. In Numpy, a diagonal matrix can be created using the function numpy.diag.

Example: Creating a Diagonal Matrix with Specific Values on the Main Diagonal

# Werte für die Hauptdiagonale definieren
diagonal_values = [10, 20, 30]

# Eine Diagonalmatrix erstellen
diagonal_matrix = np.diag(diagonal_values)

# Visualisierung der Diagonalmatrix
print("Diagonalmatrix:\n", diagonal_matrix)

Output:

Diagonalmatrix:
 [[10  0  0]
  [ 0 20  0]
  [ 0  0 30]]

In this example, a diagonal matrix is created where the main diagonal contains the values 10, 20, and 30.

4. Triangular Matrices

A triangular matrix is a square matrix where either all elements below or above the main diagonal are zero. There are two types of triangular matrices: upper and lower triangular matrices.

Example: Creating a Lower Triangular Matrix

# Eine 3x3-Matrix erstellen
matrix = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])

# Untere Dreiecksmatrix erstellen
lower_triangular = np.tril(matrix)

# Visualisierung der unteren Dreiecksmatrix
print("Untere Dreiecksmatrix:\n", lower_triangular)

Output:

Untere Dreiecksmatrix:
 [[1 0 0]
  [4 5 0]
  [7 8 9]]

Example: Creating an Upper Triangular Matrix

# Obere Dreiecksmatrix erstellen
upper_triangular = np.triu(matrix)

# Visualisierung der oberen Dreiecksmatrix
print("Obere Dreiecksmatrix:\n", upper_triangular)

Output:

Obere Dreiecksmatrix:
 [[1 2 3]
  [0 5 6]
  [0 0 9]]

In these examples, the lower and upper triangular matrices are extracted from an existing matrix.

5. Symmetric Matrix

A symmetric matrix is a square matrix where the elements are symmetric with respect to the main diagonal. This means the entries above the main diagonal have the same values as the entries below the main diagonal.

Example: Creating a Symmetric Matrix

# Eine symmetrische Matrix erstellen
sym_matrix = np.array([[1, 2, 3], 
                       [2, 4, 5], 
                       [3, 5, 6]])

# Visualisierung der symmetrischen Matrix
print("Symmetrische Matrix:\n", sym_matrix)

Output:

Symmetrische Matrix:
 [[1 2 3]
  [2 4 5]
  [3 5 6]]

This is a symmetric matrix where the values above and below the main diagonal are equal.

6. Matrix with Random Values

A matrix with random values can be used in linear algebra to simulate stochastic processes or random events. Numpy offers the function numpy.random.rand to create matrices with random values.

Example: Creating a 3x3 Matrix with Random Values

# Eine 3x3-Matrix mit Zufallswerten erstellen
random_matrix = np.random.rand(3, 3)

# Visualisierung der Zufallsmatrix
print("Matrix mit Zufallswerten (3x3):\n", random_matrix)

Output (Sample Values):

Matrix mit Zufallswerten (3x3):
 [[0.123456 0.654321 0.987654]
  [0.345678 0.876543 0.234567]
  [0.567890 0.123987 0.876543]]

The created matrix contains random values between 0 and 1.