4. 行列
Pythonで学ぶ線形代数学
2019.10.11
問4.2
code: latex1.py
from numpy.random import randint, choice
from sympy import Matrix, latex
m, n = randint(2, 4, 2)
X = -3, -2, -1, 1, 2, 3, 4, 5
A = Matrix(choice(X, (m, n)))
B = Matrix(choice(X, (m, n)))
print(f'{latex(A)} + {latex(B)} = ')
ソースコード: latex1.py
出力例:
code: python
\left\begin{matrix}-2 & 4 & 4\\2 & -1 & 1\\-3 & 2 & 3\end{matrix}\right + \left\begin{matrix}-2 & 5 & 2\\2 & 5 & 5\\4 & 5 & 5\end{matrix}\right =
LaTeXによる出力例
https://gyazo.com/b23b1054aac38ba05dfc0a7da3d2851f
問4.3
code: random2d.py
import matplotlib.pyplot as plt
from numpy import array, random
A = array(1, 2], [2, 3)
B = array(1, 2], [2, 4)
P = random.normal(0, 1, (1000, 2))
plt.scatter(P:, 0, P:, 1, s=4)
Q = array(B.dot(p) for p in P)
plt.scatter(Q:, 0, Q:, 1, s=4, color='red')
plt.axis("equal"), plt.show()
ソースコード: random2d.py
https://gyazo.com/de7f9e69227fb98a572e925064a83a2f
code: random3d.py
from vpython import *
from numpy import array, random
A = array(1, 2, 3], 2, 3, 4, [3, 4, 5)
B = array(1, 2, 3], 2, 3, 1, [3, 1, 2)
canvas(background=color.white, foreground=color.black)
for u in 5, 0, 0], 0, 5, 0, [0, 0, 5:
v = vector(*u)
curve(pos=-v, v, color=v)
P = random.normal(0, 1, (1000, 3))
points(pos=vector(*p) for p in P, radius=4)
Q = array(A.dot(p) for p in P)
points(pos=vector(*q) for q in Q, radius=4, color=color.red)
ソースコード: random3d.py
https://gyazo.com/f4a46511c45461ddc05cb2ee0adb8fa1
回転行列
code: lena4.py
from numpy import array, pi, sin, cos
import matplotlib.pyplot as plt
t = pi / 4
A = array(cos(t), -sin(t)], [sin(t), cos(t))
with open('lena.txt', 'r') as fd:
P = eval(fd.read())
Q = A.dot(p) for p in P
x, y = zip(*Q)
plt.scatter(x, y, s=1)
plt.axis('equal'), plt.show()
ソースコード: lena4.py
https://gyazo.com/2e8f73a9b6c4616f8f45d189bea38347
問4.6
code: problems.py
from numpy import array
A = array(1, 2], [3, 4)
B = array(1, 2, 3], [4, 5, 6)
C = array(1, 2], 3, 4, [5, 6)
D = array(1, 2, 3], 4, 5, 6, [7, 8, 9)
for X in (A, B, C, D):
for Y in (A, B, C, D):
if X.shape1 == Y.shape0:
print(f'{X}\n{Y}\n= {X.dot(Y)}\n')
ソースコード: problems.py
実行結果
code: python
[1 2
3 4]
[1 2
3 4]
= [ 7 10
15 22]
[1 2
3 4]
[1 2 3
4 5 6]
= [ 9 12 15
19 26 33]
[1 2 3
4 5 6]
[1 2
3 4
5 6]
= [22 28
49 64]
[1 2 3
4 5 6]
[1 2 3
4 5 6
7 8 9]
= [30 36 42
66 81 96]
[1 2
3 4
5 6]
[1 2
3 4]
= [ 7 10
15 22
23 34]
[1 2
3 4
5 6]
[1 2 3
4 5 6]
= [ 9 12 15
19 26 33
29 40 51]
[1 2 3
4 5 6
7 8 9]
[1 2
3 4
5 6]
= [ 22 28
49 64
76 100]
[1 2 3
4 5 6
7 8 9]
[1 2 3
4 5 6
7 8 9]
= [ 30 36 42
66 81 96
102 126 150]
code: mat_product1.py
from sympy import var, Matrix, sin, cos, eye
from sympy.abc import theta
A = Matrix([cos(theta), sin(theta),
-sin(theta), cos(theta)])
B = Matrix([1, 0,
0, -1])
C = Matrix([cos(theta), -sin(theta),
sin(theta), cos(theta)])
D = C * B * A
E = (eye(2) + D) / 2
ソースコード: mat_product1.py
code: mat_product2.py
from numpy import matrix, sin, cos, tan, pi, eye
import matplotlib.pyplot as plt
t = pi / 6
A = matrix(cos(t), sin(t)], [-sin(t), cos(t))
B = matrix(1, 0], [0, -1)
C = matrix(cos(t), -sin(t)], [sin(t), cos(t))
D = C * B * A
E = (eye(2) + D) / 2
x = matrix(5], [5)
y = D * x
z = E * x
plt.plot(0, 10, 0, 10 * tan(t))
plt.plot([x0, 0, y0, 0], [x1, 0, y1, 0])
plt.plot([x0, 0, z0, 0], [x1, 0, z1, 0])
plt.text(x0, 0, x1, 0, 'x', fontsize=18)
plt.text(y0, 0, y1, 0, 'y', fontsize=18)
plt.text(z0, 0, z1, 0, 'z', fontsize=18)
plt.axis('scaled'), plt.xlim(0, 10), plt.ylim(0, 6), plt.show()
ソースコード: mat_product2.py
https://gyazo.com/50bbb0bafff3c35e8f3b072364065ac7
問4.7
code: latex2.py
from numpy.random import randint, choice
from sympy import Matrix, latex
m, el, n = randint(2, 4, 3)
X = -3, -2, -1, 1, 2, 3, 4, 5
A = Matrix(choice(X, (m, el)))
B = Matrix(choice(X, (el, n)))
print(f'{latex(A)}{latex(B)} = ')
ソースコード: latex2.py
出力例
code: python
\left\begin{matrix}5 & -1 & 2\\-2 & 3 & 3\\2 & 3 & -3\end{matrix}\right\left\begin{matrix}3 & 5 & 2\\1 & 2 & -1\\1 & 3 & -2\end{matrix}\right =
LaTeXによる出力
https://gyazo.com/cf2534b293adb93231cd5688b3f94b19
code: mat_product3.py
from sympy import Matrix, solve, eye
from sympy.abc import a, b, c, d, e, f
A = Matrix([1, 2, 3,
2, 3, 4])
B = Matrix([a, b,
c, d,
e, f])
ans = solve(A * B - eye(2), a, b, c, d, e, f)
ソースコード: mat_product3.py
実行後の対話例
code: python
>> ans
{d: -2*f - 1, c: 2 - 2*e, b: f + 2, a: e - 3}
>> C = B.subs(ans); C
Matrix([
e - 3, f + 2,
2 - 2*e, -2*f - 1,
e, f])
>> A * C
Matrix([
1, 0,
0, 1])
>> solve(B*A - eye(3), a, b, c, d, e, f)
[]
code: mat_product4.py
def matrix_multiply(A, B):
m, el, n = len(A), len(A0), len(B0)
C = [[sum([Aik * Bkj for k in range(el)])
for j in range(n)] for i in range(m)]
return C
if __name__ == '__main__':
from numpy.random import normal
import matplotlib.pyplot as plt
from time import time
N = range(10, 210, 10)
T = []
for n in N:
A = normal(0, 1, (n, n)).tolist()
t0 = time()
matrix_multiply(A, A)
t1 = time()
print(n, end=', ')
T.append(t1 - t0)
plt.plot(N, T), plt.show()
ソースコード: mat_product4.py
https://gyazo.com/14280443bd3a5a5fcd610889f415e4f5
code: mat_product5.py
from numpy.random import normal
from numpy.linalg import inv
import matplotlib.pyplot as plt
from time import time
N = range(100, 2100, 100)
T = ], [], [
for n in N:
t0 = time()
A = normal(0, 1, (n, n))
t1 = time()
A.dot(A)
t2 = time()
inv(A)
t3 = time()
print(n, end=', ')
t = (t0, t1, t2, t3)
for i in range(3):
Ti.append(ti + 1 - ti)
label = 'f(x)', 'g(x)', 'h(x)'
for i in range(3):
plt.plot(N, Ti)
plt.text(N-1, Ti-1, labeli, fontsize=18)
plt.show()
ソースコード: mat_product5.py
https://gyazo.com/e8b67da16b028696aa3d1daa7ff0f42e