2020-10-19 21:08:55

This commit is contained in:
wizardforcel
2020-10-19 21:08:55 +08:00
parent 7f63048035
commit ab0caba1f0
140 changed files with 3982 additions and 3982 deletions

View File

@@ -4,7 +4,7 @@
In [1]:
```
```py
import numpy as np
import numpy.linalg
import scipy as sp
@@ -28,13 +28,13 @@ from scipy import linalg
In [2]:
```
```py
print "number of items in numpy.linalg:", len(dir(numpy.linalg))
print "number of items in scipy.linalg:", len(dir(scipy.linalg))
```
```
```py
number of items in numpy.linalg: 36
number of items in scipy.linalg: 115
@@ -56,7 +56,7 @@ number of items in scipy.linalg: 115
In [3]:
```
```py
A = np.mat("[1, 2; 3, 4]")
print repr(A)
@@ -65,7 +65,7 @@ print repr(A)
```
```
```py
matrix([[1, 2],
[3, 4]])
matrix([[1, 2],
@@ -77,13 +77,13 @@ matrix([[1, 2],
In [4]:
```
```py
print repr(A.I)
print repr(A.T)
```
```
```py
matrix([[-2\. , 1\. ],
[ 1.5, -0.5]])
matrix([[1, 3],
@@ -95,13 +95,13 @@ matrix([[1, 3],
In [5]:
```
```py
b = np.mat('[5; 6]')
print repr(A * b)
```
```
```py
matrix([[17],
[39]])
@@ -115,13 +115,13 @@ matrix([[17],
In [6]:
```
```py
A = np.array([[1,2], [3,4]])
print repr(A)
```
```
```py
array([[1, 2],
[3, 4]])
@@ -131,13 +131,13 @@ array([[1, 2],
In [7]:
```
```py
print repr(linalg.inv(A))
print repr(A.T)
```
```
```py
array([[-2\. , 1\. ],
[ 1.5, -0.5]])
array([[1, 3],
@@ -149,14 +149,14 @@ array([[1, 3],
In [8]:
```
```py
b = np.array([5, 6])
print repr(A.dot(b))
```
```
```py
array([17, 39])
```
@@ -165,12 +165,12 @@ array([17, 39])
In [9]:
```
```py
print repr(A * b)
```
```
```py
array([[ 5, 12],
[15, 24]])
@@ -188,7 +188,7 @@ array([[ 5, 12],
In [10]:
```
```py
A = np.array([[1,2],[3,4]])
print linalg.inv(A)
@@ -197,7 +197,7 @@ print A.dot(scipy.linalg.inv(A))
```
```
```py
[[-2\. 1\. ]
[ 1.5 -0.5]]
[[ 1.00000000e+00 0.00000000e+00]
@@ -213,7 +213,7 @@ print A.dot(scipy.linalg.inv(A))
In [11]:
```
```py
import time
A = np.array([[1, 3, 5],
@@ -241,7 +241,7 @@ print "solve: {} s".format(time.time() - tic)
```
```
```py
[-9.28 5.16 0.76]
[ 0.00000000e+00 -1.77635684e-15 -8.88178420e-16]
inv and dot: 0.0353579521179 s
@@ -263,7 +263,7 @@ solve: 0.0284671783447 s
In [12]:
```
```py
A = np.array([[1, 3, 5],
[2, 5, 1],
[2, 3, 8]])
@@ -272,7 +272,7 @@ print linalg.det(A)
```
```
```py
-25.0
```
@@ -287,7 +287,7 @@ print linalg.det(A)
In [13]:
```
```py
A = np.array([[1, 2],
[3, 4]])
@@ -303,7 +303,7 @@ print linalg.norm(A,np.inf) # L inf norm 最大行和
```
```
```py
5.47722557505
5.47722557505
6
@@ -350,7 +350,7 @@ $$ y_{i}=\sum_{j}c_{j}f_{j}\left(\mathbf{x}_{i}\right)+\epsilon_{i} $$
In [14]:
```
```py
c1, c2 = 5.0, 2.0
i = np.r_[1:11]
xi = 0.1*i
@@ -363,13 +363,13 @@ zi = yi + 0.05 * np.max(yi) * np.random.randn(len(yi))
In [15]:
```
```py
A = np.c_[np.exp(-xi)[:, np.newaxis], xi[:, np.newaxis]]
print A
```
```
```py
[[ 0.90483742 0.1 ]
[ 0.81873075 0.2 ]
[ 0.74081822 0.3 ]
@@ -387,14 +387,14 @@ print A
In [16]:
```
```py
c, resid, rank, sigma = linalg.lstsq(A, zi)
print c
```
```
```py
[ 4.87016856 2.19081311]
```
@@ -405,7 +405,7 @@ print c
In [17]:
```
```py
xi2 = np.r_[0.1:1.0:100j]
yi2 = c[0]*np.exp(-xi2) + c[1]*xi2
@@ -608,7 +608,7 @@ gIuIRNT/AXU6OlSJKzNiAAAAAElFTkSuQmCC
In [18]:
```
```py
A = np.array([[1, 5, 2],
[2, 4, 1],
[3, 6, 2]])
@@ -630,7 +630,7 @@ print linalg.norm(A.dot(v1)-l1*v1)
```
```
```py
[ 7.95791620+0.j -1.25766471+0.j 0.29974850+0.j]
[ 1\. 1\. 1.]
3.23301824835e-15
@@ -660,7 +660,7 @@ $M \times N$ 矩阵 $\mathbf A$ 的奇异值分解为: $$ \mathbf{A=U}\boldsym
In [19]:
```
```py
A = np.array([[1,2,3],[4,5,6]])
U, s, Vh = linalg.svd(A)
@@ -671,7 +671,7 @@ $\boldsymbol{\Sigma}$ 矩阵:
In [20]:
```
```py
M, N = A.shape
Sig = linalg.diagsvd(s,M,N)
@@ -679,7 +679,7 @@ print Sig
```
```
```py
[[ 9.508032 0\. 0\. ]
[ 0\. 0.77286964 0\. ]]
@@ -689,13 +689,13 @@ print Sig
In [21]:
```
```py
print A
print U.dot(Sig.dot(Vh))
```
```
```py
[[1 2 3]
[4 5 6]]
[[ 1\. 2\. 3.]
@@ -715,7 +715,7 @@ $\mathbf P$ 是 $M \times M$ 的单位矩阵的一个排列,$\mathbf L$ 是下
In [22]:
```
```py
A = np.array([[1,2,3],[4,5,6]])
P, L, U = linalg.lu(A)
@@ -728,7 +728,7 @@ print P.dot(L).dot(U)
```
```
```py
[[ 0\. 1.]
[ 1\. 0.]]
[[ 1\. 0\. ]
@@ -768,7 +768,7 @@ $\mathbf R$ 为上三角形矩阵,$\mathbf Q$ 是正交矩阵。
In [23]:
```
```py
A = np.mat('[1 3 2; 1 4 5; 2 3 6]')
print A
@@ -781,7 +781,7 @@ print Z.dot(T).dot(Z.T)
```
```
```py
[[1 3 2]
[1 4 5]
[2 3 6]]
@@ -814,14 +814,14 @@ print Z.dot(T).dot(Z.T)
In [24]:
```
```py
A = np.array([[1, 2], [3, 4]])
print linalg.expm3(A)
```
```
```py
[[ 51.96890355 74.73648784]
[ 112.10473176 164.07363531]]
@@ -835,12 +835,12 @@ print linalg.expm3(A)
In [25]:
```
```py
print linalg.expm2(A)
```
```
```py
[[ 51.9689562 74.73656457]
[ 112.10484685 164.07380305]]
@@ -852,12 +852,12 @@ print linalg.expm2(A)
In [26]:
```
```py
print linalg.expm(A)
```
```
```py
[[ 51.9689562 74.73656457]
[ 112.10484685 164.07380305]]
@@ -869,13 +869,13 @@ print linalg.expm(A)
In [27]:
```
```py
print A
print linalg.logm(linalg.expm(A))
```
```
```py
[[1 2]
[3 4]]
[[ 1\. 2.]