[PYTHON] I wrote a demo program for linear transformation of a matrix

at first

Mathematical Girl's Secret Note "What the Procession Draws" Demonstration of linear transformation performed by "Lisa" in Chapter 4 Transform I went with Python

Operating environment

Python3

Implementation example and result

Below, they are listed in the order of description in this manual.

Show dots

Displayed at point (2, 1) on the graph

import matplotlib.pyplot as plt
import numpy as np

p21 = np.array([2, 1])
plt.plot(p21[0], p21[1], marker='.')

plt.show()
Screen Shot 2020-04-28 at 11.27.52.png

Move points

Matrix points (2, 1)

\begin{pmatrix}
2 & 0 \\
0 & 2 
\end{pmatrix}

Linearly transform with to move to point (4, 2)

Screen Shot 2020-04-28 at 11.24.38.png
import matplotlib.pyplot as plt
import numpy as np

p21 = np.array([2, 1])
p21to42 = np.array([[2, 0], [0, 2]]) @ p21

fig = plt.figure()
ax = fig.add_subplot(111)
ax.annotate('', xy=p21to42,
                xytext=p21,
                arrowprops=dict(shrink=0, width=1, headwidth=8))
ax.set_xlim([0, 5])
ax.set_ylim([0, 5])

plt.plot(p21[0], p21[1], marker='.')
plt.plot(p21to42[0], p21to42[1], marker='.')

plt.show()

Move multiple points

Matrix multiple points

\begin{pmatrix}
2 & 0 \\
0 & 2 
\end{pmatrix}

Linear transformation with

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(111)

for x in np.linspace(-1, 1, 11):
    for y in np.linspace(-1, 1, 11):
        marker = '8'
        if x > 0 and y > 0:
            marker = '$1$'
        elif x < 0 and y > 0:
            marker = '$2$'
        elif x < 0 and y < 0:
            marker = '$3$'
        elif x > 0 and y < 0:
            marker = '$4$'

        pOrg = np.array([x, y])
        pTra = np.array([[2, 0], [0, 2]]) @ pOrg

        ax.annotate('', xy=pTra,
                        xytext=pOrg,
                        arrowprops=dict(shrink=0.1, width=1, headwidth=3))

        plt.plot(pOrg[0], pOrg[1], marker = marker)
        plt.plot(pTra[0], pTra[1], marker = marker)

ax.set_xlim([-2, 2])
ax.set_ylim([-2, 2])
plt.show()
Screen Shot 2020-04-28 at 13.07.29.png
\begin{pmatrix}
1/2 & 0 \\
0 & 1/2 
\end{pmatrix}

When linearly transformed with Screen Shot 2020-04-28 at 13.15.13.png

\begin{pmatrix}
3 & 0 \\
0 & 2 
\end{pmatrix}

When linearly transformed with

Screen Shot 2020-04-28 at 13.17.21.png
\begin{pmatrix}
2 & 1 \\
1 & 3 
\end{pmatrix}

When linearly transformed with Screen Shot 2020-04-28 at 13.20.23.png

Synthesis of linear transformations

Matrix of figures surrounded by (0, 0), (1, 0), (1, 1), (0, 1)

\begin{pmatrix}
2 & 1 \\
1 & 3 
\end{pmatrix}

Matrix after conversion with

\begin{pmatrix}
0 & -1 \\
1 & 0 
\end{pmatrix}

Convert with

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)

original = np.array([[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]])
trans1 = np.array([[0, 0]])
trans2 = np.array([[0, 0]])
for ele in original:
    _ele1 = np.array([[2, 1], [1, 3]]) @ ele
    _ele2 = np.array([[0, -1], [1, 0]]) @ _ele1
    trans1 = np.vstack((trans1, np.array([_ele1])))
    trans2 = np.vstack((trans2, np.array([_ele2])))

ax1.plot(original[:,0], original[:,1], marker = ".", label='original')
ax2.plot(trans1[:,0], trans1[:,1], marker = ".", label='trans1')
ax3.plot(trans2[:,0], trans2[:,1], marker = ".", label='trans2')

ax1.legend(loc = 'upper center')
ax2.legend(loc = 'upper center')
ax3.legend(loc = 'upper center')
ax1.set_xlim([-4, 4])
ax1.set_ylim([-4, 4])
ax2.set_xlim([-4, 4])
ax2.set_ylim([-4, 4])
ax3.set_xlim([-4, 4])
ax3.set_ylim([-4, 4])
plt.show()
Screen Shot 2020-04-28 at 18.19.26.png

If the conversion order is different as shown in the figure below, the figure will also be different. You can see that ABx = BAx does not hold like a normal formula

Screen Shot 2020-04-28 at 18.31.12.png

Recommended Posts

I wrote a demo program for linear transformation of a matrix
I wrote a program quickly to study DI with Python ①
Convenient for training newcomers? I wrote a Telnet practice server
I searched for a similar card of Hearthstone with Deep Learning
I made a lot of files for RDP connection with Python
I touched PyAutoIt for a moment
The story of writing a program
Latin learning for the purpose of writing a Latin sentence analysis program (Part 1)
AtCoder writer I wrote a script to aggregate the contests for each writer
I wrote a corpus reader that reads the results of MeCab analysis
I made a program to check the size of a file in Python
I made a dash docset for Holoviews
I made a payroll program in Python!
Impressions of using Flask for a month
I wrote unit tests for various languages
I wrote the code for Gibbs sampling
I took a benchmark of h5py compression
I tried 3D detection of a car
I made a library for actuarial science
I wrote a graph like R glmnet in Python for sparse modeling in Lasso
[Fundamental Information Technology Engineer Examination] I wrote a linear search algorithm in Python.
I tried a TensorFlow tutorial (MNIST for beginners) on Cloud9-Classification of handwritten images-
I wrote an animation that degenerates a linear system with deadly dirty code
I want to add silence to the beginning of a wav file for 1 second