1. 程式人生 > >千里之行始於足下

千里之行始於足下

– Start

import numpy as np

# 什麼是矩陣(matrix)?
a = np.array([ [1, 2],
               [3, 4] ])

b = np.array([ [-1, -2],
               [-3, -4] ])

# 矩陣加法
c = a + b
print(f'a + b = {c}')

# 矩陣減法
c = a - b
print(f'a - b = {c}')
import numpy as np

# 定義矩陣
a = np.array([ [1, 2],
               [3, 4] ])

b = np.array([ [-1, -2],
               [-3, -4] ])

# 矩陣乘法 (行*列)
# [[(1*-1 + 2*-3), (1*-2 + 2*-4)],
# [(3*-1 + 4*-3), (3*-2 + 4*-4)]]
c = a @ b
print(c)
import numpy as np

# 單位矩陣(identity matrix)
a = np.identity(3)
b = np.eye(3)
print('單位矩陣')
print(a)


# 逆矩陣(inverse matrix)
c = np.array([ [1, 2],
               [3, 4] ])
# d = 1 / (1*4 - 2*3) * np.array([ [4, -2],
#                                  [-3, 1] ])
d = np.linalg.inv(c)
print('矩陣 c')
print(c)
print('c 的逆矩陣')
print(d)


# 矩陣 * 逆矩陣 = 單位矩陣
e = c @ d
e = np.round(e, 2)
print('矩陣 * 逆矩陣')
print(e)

– 聲 明:轉載請註明出處 – Last Updated on 2018-10-22 – Written by ShangBo on 2018-10-22 – End