0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

線形代数の基礎中の基礎をpythonに落とし込んだ

Last updated at Posted at 2021-10-26

列ベクトル

```php # ベクトル import numpy as np x=np.array([2,1]) print(x) x=x.reshape(-1,1) print(x) #[2 1] #[[2],[1]] ```

ベクトルの計算

```php # ベクトルの計算 x=np.array([2,1]).reshape(-1,1) y=np.array([1,3]).reshape(-1,1) print(x) print(y) print(x+y) print(x-y) print(2*x) print(-y) print('ベクトルの内積',x*y) # ベクトルの内積 print('ベクトル同士の内積はスカラーになる', np.dot(x.T,y)) #x:[[2],[1]] #y:[[1],[3]] #x+y[[3],[4]] #x-y[[ 1],[-2]] #2*x:[[4],[2]] #-y[[-1],[-3]] #ベクトルの内積 [[2],[3]] #ベクトル同士の内積はスカラーになる [[5]] ```

ノルム

```php # ノルム x=np.array([4,-3]).reshape(-1,1) print(np.linalg.norm(x)) print('L1ノルム:',np.linalg.norm(x,1)) # L1ノルム print('L2ノルム:',np.linalg.norm(x,2)) # L2ノルム print('L∞ノルム:',np.linalg.norm(x,np.inf)) #L∞ノルム #5.0 #L1ノルム: 7.0 #L2ノルム: 5.0 #L∞ノルム: 4.0 ```

距離

```php # 距離 x=np.array([4,-3]).reshape(-1,1) y=np.array([2,4]).reshape(-1,1) print(np.linalg.norm(x-y)) from scipy.spatial import distance print('ユークリッド距離:',distance.euclidean(x,y)) # ユークリッド距離 print('マンハッタン距離:',distance.cityblock(x,y))# マンハッタン距離 print('チェビシェフ距離:',distance.chebyshev(x,y)) # チェビシェフ距離 #7.280109889280518 #ユークリッド距離: 7.280109889280518 #マンハッタン距離: 9 #チェビシェフ距離: 7 ```

内積

```php # 内積 x=np.array([4,-3]).reshape(-1,1) y=np.array([2,4]).reshape(-1,1) print(np.dot(x.T,y))#ベクトル同士の内積はスカラーになるが、Pythonでは要素が一つだけのNumpyのarrayが返ってくる print(np.dot(x.T,y)[0,0]) #[[-4]] #-4 ```

行列式

```php # 行列式 A = np.array([ [ 4., -7., 4], [ 1., 1., -1.], [ 2., 5., -8.], ]) print('Aの行列式',np.linalg.det(A))#Aの行列式 print('Aの逆行列',np.linalg.inv(A)) #Aの行列式 -42.00000000000001 #Aの逆行列 [[ 0.07142857 0.85714286 -0.07142857] # [-0.14285714 0.95238095 -0.19047619] # [-0.07142857 0.80952381 -0.26190476]] ```

方程式の解を求める

```php # 解を求める A = np.array([ [ 4., -7., 4], [ 1., 1., -1.], [ 2., 5., -8.], ]) b = np.array([1., 6., 3.]).reshape(-1,1) np.dot(np.linalg.inv(A),b) #array([[5.], # [5.], # [4.]]) ```

ランク

```php #ランク A = np.array([ [ 2., 1., -1], [ 1., -1., 3.], [ -1., 5., 2.], ]) np.linalg.matrix_rank(A) #3 ```

行列の演算

```php # 行列の演算 A = np.array([ [ 4., -7., 4], [ 1., 1., -1.], [ 2., 5., -8.], ]) B = np.array([ [ 1., 2., -5.], [ 2., 3., -7.], [ 4., -1., 7.], ]) k=10 print(A+B) print(A-B) print(k*A) print((1/k)*A) #[[ 5. -5. -1.] # [ 3. 4. -8.] # [ 6. 4. -1.]] #[[ 3. -9. 9.] # [ -1. -2. 6.] # [ -2. 6. -15.]] #[[ 40. -70. 40.] # [ 10. 10. -10.] # [ 20. 50. -80.]] #[[ 0.4 -0.7 0.4] # [ 0.1 0.1 -0.1] # [ 0.2 0.5 -0.8]] ```

行列とベクトルの積

```php # 行列とベクトルの積 A = np.array([ [ 4., -7., 4], [ 1., 1., -1.], [ 2., 5., -8.], ]) B = np.array([ [ 1., 2., -5.], [ 2., 3., -7.], ]) x = np.array([1., 2., 3.]).reshape(-1,1) print(np.dot(A,x)) print(np.dot(B,x)) #[[ 2.],[ 0.],[-12.]] #[[-10.],[-13.]] ```

行列と行列の積

```php # 行列と行列の積 A = np.array([ [ 2., 1.], [ 1., 3.], [ 1., -1.], ]) B = np.array([ [ 1., -1., 2.], [ 1., 2., 3.], ]) print(np.dot(A,B)) print(np.dot(B,A)) #[[ 3. 0. 7.] # [ 4. 5. 11.] # [ 0. -3. -1.]] #[[ 3. -4.] # [ 7. 4.]] ```

線形写像

```php #線形写像 A = np.array([ [ 1., 2., -3.], [ 2., -1., 1.], [ -1., 3., -2.], ]) x = np.array([4., 2., 3.]).reshape(-1,1) print(A) print(np.dot(A,x)) #[[ 1. 2. -3.] # [ 2. -1. 1.] # [-1. 3. -2.]] #[[-1.],[ 9.],[-4.]]) ```

合成写像

```php # 合成写像 A = np.array([ [ 1., 2.], [ 3., -1.], [ 4., 5.], ])

B = np.array([
[ -2., 2., -1.,],
[ 3., 1., 2.,],
])
print(np.dot(B,A))
print(np.dot(A,B))
#[[ 0. -11.],[ 14. 15.]]
#[[ 4. 4. 3.],[-9. 5. -5.],[ 7. 13. 6.]]

<h1>行列の連結</h1>
```php
# 行列の連結
A = np.array([
    [ 1., 2.],
    [ 3., -4.],
])
x1 = np.array([2., 3.]).reshape(-1,1)
x2 = np.array([1., 1.]).reshape(-1,1)
X=np.concatenate([x1, x2], 1)
X
#array([[2., 1.],
#       [3., 1.]])

github

https://github.com/miyagawa-toshiki/Basics-of-Linear-Algebra/
0
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?