15-Extension of Matrix Transpose

Transpose of matrix?

Foreword

In many cases, the data we get may not arrange the coordinates of the points in the direction of the column. For us humans, the more convenient way is to arrange the coordinates of the point in the direction of the row. We are more familiar with Think of the matrix as a data. Here, the P matrix is actually regarded as a data matrix.

T

=

(

1.5

0

0

2

)

T=\begin{pmatrix} 1.5 & amp;0 \ 0 & amp;2\end{pmatrix}

T=(1.50?02?)

P

=

(

0

0

4

0

5

3

)

P=\begin{pmatrix} 0 & amp;0 \ 4 & amp;0 \ 5 & amp;3\end{pmatrix}

P =
?045?003?
?

In this case, we are still accustomed to calling each row a sample in data science. In other words, each row is a point, and each column is called a feature or attribute in data science. For the data of point coordinates, each column represents the value of a certain dimension. The first column is the x-axis Or the value in the x direction, and the second dimension is the value in the y axis or y direction, but everyone must know that this matrix T cannot be multiplied by this matrix P, because a 2×2 matrix cannot be multiplied by a 3 ×2 matrices, but what should we do if the data we get is such a matrix? The answer is very simple, we need to transpose the matrix P, the so-called transposition is to make the rows become columns, and let the columns become OK

P

T

=

(

0

4

5

0

0

3

)

P^T=\begin{pmatrix} 0 & amp; 4 & amp;5 \ 0 & amp; 0 & amp;3\end{pmatrix}

PT=(00?40?53?)

it’s here

P

T

P^T

PT is called the transpose matrix of P

From the point of view of the elements in the matrix, then

A

=

(

a

i

j

)

A=(a_{ij})

A=(aij?) 、

A

T

=

(

a

j

i

)

A^T=(a_{ji})

AT=(aji?)
Let’s talk about one. If no special declaration is made, the vector refers to a column vector

The properties of matrix transposition

  • (

    A

    T

    )

    T

    =

    A

    (A^T)^T=A

    (AT)T=A

  • (

    A

    +

    B

    )

    T

    =

    A

    T

    +

    B

    T

    (A + B)^T=A^T + B^T

    (A + B)T=AT + BT

  • (

    k

    ?

    A

    )

    T

    =

    k

    ?

    A

    T

    (k·A)^T=k·A^T

    (k?A)T=k?AT

  • Next, note that the transpose of matrix multiplication is a bit different.

    (

    A

    B

    )

    T

    =

    B

    T

    ?

    A

    T

    (AB)^T=B^T·A^T

    (AB)T=BT?AT, we can look at and understand it like this

In the previous article, we have given the project structure and program code of vectors and matrices in linear algebra. Here we follow the project code in the previous article to add the transpose method of the matrix in the Matrix class. The project code is at the end of this article14-Matrix multiplication and its algorithm

Actual Combat 1 – Transpose method of matrix class and its matrix object in Numpy

├───Linear_Algebra
│ │ main_matrix.py
| | main_numpy_Matrix.py
│ │
│ │
│ ├───playLA
│ │ │ Matrix.py
│ │ │ Vector.py
│ │ │ _globals.py
│ │ │ __init__.py
| | |

Next, we add the transpose method to the Matrix class in the Matrix.py program

def T(self):
        """Returns the transpose of the matrix"""
        return Matrix([[e for e in self. col_vector(i)] for i in range(self. col_num())])

Then we test in main_matrix.py

# coding:utf-8

from playLA.Matrix import Matrix
from playLA.Vector import Vector

if __name__ == '__main__':
    matrix = Matrix([[1, 2], [3, 4]])
    print(matrix)
    print('matrix. shape=', matrix. shape())
    print("matrix. size=", matrix. size())
    print("len(matrix)=", len(matrix))
    print("matrix[0][0]=", matrix[0, 0])

    matrix2 = Matrix([[5, 6], [7, 8]])
    print(matrix + matrix2)
    print(matrix - matrix2)
    print(matrix * 2)
    print(2 * matrix)
    print(Matrix. zero(2, 3))

    T = Matrix([[1.5, 0], [0, 2]])
    p = Vector([5, 3])
    print(T. dot(p))

    P = Matrix([[0, 4, 5], [0, 0, 3]])
    print(T. dot(P))

    print(matrix.dot(matrix2), matrix2.dot(matrix)) # Verify that matrix multiplication follows the commutative law

    print(P.T()) # Test whether the program transpose function is realized

Then let’s take a look at the matrix object in Numpy and its related operations
main_numpy_Matrix.py

# coding:utf-8

import numpy as np

if __name__ == '__main__':
    """The matrices and vectors in Numpy are encapsulated in a class"""
    # matrix creation
    A = np.array([[1, 2], [3, 4]])
    print(A)

    # Properties of the matrix
    print(A. shape)
    print(A.T) # transpose of the matrix

    # get the elements of the matrix
    print(A[1, 1])
    print(A[0]) # In the computer, the matrix is stored in the form of rows by default
    print(A[:,0]) # Get the vector of the first column of matrix A
    print(A[1, :]) # Get the second row element of the matrix

    # Basic operations on matrices
    B = np.array([[5, 6, ], [7, 8, ]])
    print(A + B)
    print(A - B)
    print(A * 10)
    print(10*A)
    print(A * B) # Note that it is not a matrix multiplication, it is a multiplication of the corresponding elements of the matrix
    print(A.dot(B)) # matrix multiplication

    # operations on matrices and vectors
    p = np.array([10, 100])
    print(A + p) # The addition of a matrix and a vector is actually adding each row of the matrix to the vector
    print(A + 1) # This is actually a broadcast mechanism for high-dimensional arrays
    print(A. dot(p))