4. 行列
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)
A = Matrix(choice(X, (m, n)))
B = Matrix(choice(X, (m, n)))
print(f'{latex(A)} + {latex(B)} = ')
出力例:
code: python
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(Q:, 0, Q:, 1, s=4, color='red') plt.axis("equal"), plt.show()
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))
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())
x, y = zip(*Q)
plt.scatter(x, y, s=1)
plt.axis('equal'), plt.show()
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):
print(f'{X}\n{Y}\n= {X.dot(Y)}\n')
実行結果
code: python
code: mat_product1.py
from sympy import var, Matrix, sin, cos, eye
from sympy.abc import theta
D = C * B * A
E = (eye(2) + D) / 2
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.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()
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)
A = Matrix(choice(X, (m, el)))
B = Matrix(choice(X, (el, n)))
print(f'{latex(A)}{latex(B)} = ')
出力例
code: python
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
実行後の対話例
code: python
>> ans
{d: -2*f - 1, c: 2 - 2*e, b: f + 2, a: e - 3}
>> C = B.subs(ans); C
Matrix([
>> A * C
Matrix([
[]
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()
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):
for i in range(3):
plt.text(N-1, Ti-1, labeli, fontsize=18) plt.show()
https://gyazo.com/e8b67da16b028696aa3d1daa7ff0f42e