11-matrix (matrix)_square matrix_symmetrical matrix_identity matrix_diagonal matrix

Matrix

  • Vector is an extension of logarithm, a vector represents a set of numbers
  • A matrix is an extension of a vector, and a matrix represents a set of vectors
  • (

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    )

    \begin{pmatrix} 1 & amp; 2 & amp; 3 & amp;4 \ 5 & amp; 6 & amp; 7 & amp; 8\ 9 & amp; 10 & amp; 11 & amp;12 \ 13 & amp; 14 & amp; 15 & amp;16\end{pmatrix}

    ?15913?261014?371115?481216?
    ?

  • The next question is, for example, the above matrix, what kind of a set of vectors does it represent? How many vectors are there in this set of vectors? Who is each vector? In fact, we usually have different ways of looking at this question because we look at a matrix from a different perspective, or the application scenarios in which we apply the matrix are different. However, if we answer this question very broadly as a whole, it is also very helpful. easy. In short, we have two perspectives to look at such a matrix. One perspective is that we look at this matrix row by row, so this matrix is composed of four row vectors, Another perspective is the so-called column vector perspective. In this case, the matrix, which still contains four vectors

In most cases column vectors are more useful than row vectors

Or this application scenario where we apply the matrix. The situation is different, and we usually have different ways of looking at it, but overall it is very easy to answer this question in a very broad way. In short, we have two perspectives to see such a matrix, one perspective is us. Looking at this matrix row by row, this matrix is composed of four row vectors.
We will continue to deal with various matrices in the future. Abstractly speaking, what we are learning here is algebra, and algebra is to use letters to represent numbers. In the usual mathematical notation system, we use the uppercase letter A to represent a matrix, then for this matrix, each element in it is usually represented by a lowercase letter corresponding to this uppercase letter, and the subscript of this lowercase letter To indicate the number of rows and columns corresponding to this element. for example:

A

=

[

a

11

a

12

a

13

a

twenty one

a

twenty two

a

twenty three

a

31

a

32

a

33

]

A=\begin{bmatrix}a_{11} & amp; a_{12} & amp; a_{13} \a_{21} & amp; a_{22} & amp; a_{23} \ \a_{31} & a_{32} & a_{33} \\end{bmatrix}

A=
?a11?a21?a31a12?a22?a32a13?a23?a33
?

We represent a matrix in linear algebra the same way we represent a two-dimensional array in the computer world.

For example, when can we use a matrix? You must have seen various tables in your usual study, life and work. In fact, such a data table is a matrix. For example, I wrote a 3×5 matrix here.

Then each element in this matrix represents a grade, and the entire matrix represents the grades of several students in a class in different subjects. Here, you can see this matrix, which may represent the results of Zhang San, Li Si, and Wang Wu, which is the meaning of each row. At the same time, it shows the results of these three people in five different subjects: Chinese, mathematics, English, physics, and chemistry. This is what each column means.

Then, under such circumstances, everyone can naturally think that for this matrix, each row vector actually represents the grades of a certain person in each subject. This is when we look at this matrix from the perspective of row vectors. And each column vector represents the grades of different people in a certain subject. This is how we look at this matrix from the perspective of column vectors. Obviously, we can do some things in this dimension corresponding to the row vector, for example, to calculate the total score of each classmate’s exam, we only need to add up the scores of each row.

Of course, we can also do some things on the column vector, such as calculating the average score of each subject among the students in this class. In this case, we need to process these data column by column. For each column of data, calculate the sum and divide it by the number of rows, that is, divide it by the total number of people in this class. So of course, the example I gave here is a very intuitive and simple example. In fact, it is also an example of using matrices in most of the data science we show, so in most data science tasks, we get The data itself is usually such a matrix. We process data based on such a matrix. However, in the world of linear algebra, for a matrix, it can not only express a set of data in this way, but also express more Abstract content, for example, it can represent a set of transformations or a space. Personally, I think that using a matrix to represent these contents may be what we pay more attention to in linear algebra.

Matrices and their operations

[

a

11

?

a

1

no

?

?

?

a

m

1

?

a

m

no

]

\begin{bmatrix} a_{11} & amp; \cdots & amp; a_{1n} \ \cdots & amp; \cdots & amp; \cdots \ a_{m1} & amp; \cdots & amp; a_{mn} \ \end{bmatrix}

?a11am1?a1namn
?

A matrix is a two-dimensional array. It is an m-by-n matrix. It has m rows and n columns. Each row and column has an element above it. Each element has a row label i and a column label j. The corresponding element is

a

i

j

a_{ij}

aij?
A matrix is a common data structure that consists of a stack of one-dimensional arrays (also known as row vectors). A matrix is a two-dimensional array consisting of rows and columns. It is commonly used for data processing and representation in fields such as mathematics, computer science, and statistics.

A matrix can be composed of a bunch of row vectors, which is called a matrix of row vectors; it can also be composed of a bunch of column vectors, called a matrix of column vectors. The number of rows of the matrix indicates the height of the matrix, and the number of columns indicates the width of the matrix. A matrix of row vectors is composed of row vectors arranged horizontally, while a matrix of column vectors is composed of column vectors arranged vertically.

For example, here is a matrix of row vectors:

[1 2 3]
[4 5 6]
[7 8 9]

Here is a matrix of column vectors:

[1 4 7]
[2 5 8]
[3 6 9]

Matrix has a wide range of applications in linear algebra, image processing, neural network and other fields. In computers, matrices are also the basis of many data processing algorithms, such as matrix multiplication, matrix transposition and other operations.

Square matrix, symmetric matrix, identity matrix, diagonal

Square

The following introduces several special matrices. If m is equal to n, it is called a square matrix

[

1

0

0

0

2

0

0

0

3

]

\begin{bmatrix} 1 & amp; 0 & amp; 0 \ 0 & amp; 2 & amp; 0 \ 0 & amp; 0 & amp; 3 \ \end{bmatrix }

?100?020?003?
?

Symmetric matrix

The shape and value of the original matrix will not be changed after the symmetric matrix is transposed.
definition is

a

i

j

a_{ij}

aij?
equal to

a

j

i

a_{ji}

aji? Then it is a symmetric matrix (the elements along the main diagonal (the line dividing the upper left corner to the lower right corner) are symmetrical, and the symmetry here is that the elements on both sides of the main diagonal are equal), and it must be a square matrix

[

1

1

2

1

2

3

2

3

3

]

\begin{bmatrix} 1 & amp; 1 & amp; 2 \ 1 & amp; 2 & amp; 3 \ 2 & amp; 3 & amp; 3 \ \end{bmatrix }

?112?123?233?
?

Identity matrix

The main diagonal is all 1, and the other positions are 0. This is called the unit matrix. The unit matrix is written as I, which must be a square matrix, which is equivalent to the 1 in the number.

[

1

?

0

0

0

?

1

?

0

0

0

?

?

?

0

0

0

?

?

?

0

0

0

?

1

]

\begin{bmatrix} 1 & amp; \cdots & amp; 0 & amp; 0 & amp; 0\ \cdots & amp; 1 & amp; \cdots & amp; 0 & amp; 0 \ 0 & amp; \cdots & amp; \cdots & amp; \cdots & amp; 0\ 0 & amp; 0 & amp; \cdots & amp; ; \cdots\ 0 & amp; 0 & amp; 0 & amp; \cdots & amp; 1\ \end{bmatrix}

?1?0001?00?0?0?00000?1?
?

The method corresponding to the identity matrix is the identity and eye methods in the numpy module. numpy.identity returns an identity matrix, that is, a square matrix with 1 on the diagonal and 0 in other positions.

identity method

parameter:

  • n: An integer indicating the number of rows and columns of the output identity matrix.
  • dtype: an optional parameter, used to specify the data type of the output array, the default is the float type.

return value:

  • out: A NumPy array of size n x n with 1s on the main diagonal and 0s on the remaining elements.

eye method

The numpy.eye method is used to create an identity matrix, also known as the identity matrix or identity matrix. The identity matrix is a square matrix with all 1s on its main diagonal and 0s on the rest.

Function signature:

numpy.eye(N, M=None, k=0, dtype=float, order='C')

parameter:

  • N: The number of rows of the generated matrix (also the number of columns, because it is a square matrix).
  • M: optional parameter, the number of columns of the generated matrix, the default is N, because it is a square matrix, generally does not need to be specified.
  • k: optional parameter, the offset of the diagonal, the default is 0, which means that the element on the main diagonal is 1, and the other elements are 0; if k If it is a positive integer, the main diagonal is above the matrix; if it is a negative integer, the main diagonal is below the matrix, and the diagonal elements are 1.
  • dtype: an optional parameter, specifying the data type of the generated matrix, the default is float.
  • order: optional parameter, specifying the memory layout of the array, 'C' means C order, 'F' means Fortran order , the default is 'C'.

return value:

  • Returns an identity matrix with shape (N, M).

Example:

import numpy as np

# create a 3x3 identity matrix
eye_matrix = np.eye(3)
print(eye_matrix)
# output:
# [[1.0.0.]
# [0.1.0.]
# [0.0.1.]]

# Create a 4x4 identity matrix with a main diagonal offset of 1
eye_matrix_shifted = np.eye(4, k=1)
print(eye_matrix_shifted)
# output:
# [[0. 1. 0. 0.]
# [0.0.1.0.]
# [0. 0. 0. 1.]
# [0.0.0.0.]]

The numpy.eye function is very useful, especially in linear algebra and matrix operations.

The difference between identity and eye methods

The numpy.eye function and the numpy.identity function are both used to create the identity matrix, but they have some differences:

  1. The parameters are different:

    • numpy.eye(N, M=None, k=0, dtype=float): The eye function can create an identity matrix of any size, where N indicates the number of rows, M indicates the number of columns, by default M is equal to N, k indicates the diagonal The offset of the line, the default is 0, which is the main diagonal.
    • numpy.identity(n, dtype=None): The identity function creates a square matrix (the number of rows is equal to the number of columns), and only needs an integer n is used as a parameter to indicate the number of rows and columns of the output unit matrix.
  2. The defaults are different:

    • The default data type of numpy.eye is float, and other data types can be specified through the dtype parameter.
    • The default data type of numpy.identity is also float, and other data types can be specified through the dtype parameter.

Example:

import numpy as np

# Use eye to create a 3x4 identity matrix with a diagonal offset of 1
eye_matrix = np.eye(3, 4, k=1, dtype=int)
print(eye_matrix)
# output:
# [[0 1 0 0]
# [0 0 1 0]
# [0 0 0 1]]

# Use identity to create a 3x3 identity matrix, the default data type is float
identity_matrix = np. identity(3, dtype=int)
print(identity_matrix)
# output:
# [[1 0 0]
# [0 1 0]
# [0 0 1]]

In general, the eye function is more flexible than the identity function. It can create a non-square identity matrix and specify the offset of the diagonal, while the identity function is specifically used to create the identity matrix of a square matrix.

Diagonal array

Diagonal matrix, that is, the main diagonal is not 0, and other positions are 0

Application of dot method in matrix

The numpy.dot method is used to compute the matrix multiplication (dot product) of two arrays. In matrix operations, matrix multiplication is a common operation that involves the process of multiplying and summing the elements of two matrices.

Function signature:

numpy.dot(a, b, out=None)

parameter:

  • a: Array or matrix to perform matrix multiplication.
  • b: Array or matrix to perform matrix multiplication.
  • out: an optional parameter, specifying an array of output results. If not specified, the result will be a new array created.

return value:

  • Returns the result of matrix multiplication of two arrays.

Precautions:

  1. The dimensions of the two arrays must meet the conditions for matrix multiplication, that is, the number of columns in the first array must be equal to the number of rows in the second array.
  2. If two arrays are one-dimensional, the dot function will calculate their dot product (inner product).
  3. If two arrays are two-dimensional, the dot function will calculate their matrix multiplication.

Example:

import numpy as np

# Dot product (inner product) of one-dimensional array
a = np.array([1, 2, 3])
b = np. array([4, 5, 6])
dot_product = np. dot(a, b)
print(dot_product) # output: 32

# Matrix multiplication of two-dimensional arrays
matrix_a = np.array([[1, 2], [3, 4]])
matrix_b = np.array([[5, 6], [7, 8]])
matrix_product = np.dot(matrix_a, matrix_b)
print(matrix_product)
# output:
# [[19 22]
# [43 50]]