thk*_*ang 2 python linear-algebra
我有10,000个变量.对于他们中的100个,我确实知道确切的价值.
其他人给予如下:
a = 0.x_1 * b + 0.y_2 * c+ 0.z_1 * d + (1 - 0.x_1 - 0.y_1 - 0.z_1) * a
b = 0.x_2 * c + 0.y_2 * d+ 0.z_2 * e + (1 - 0.x_2 - 0.y_2 - 0.z_2) * b
...
q = 0.x_10000 * p + 0.y_10000 * r+ 0.z_10000 * s + (1 - 0.x_10000 - 0.y_10000 - 0.z_10000) * q
Run Code Online (Sandbox Code Playgroud)
是的我知道0.x_n,0.y_n,0.z_n的确切值......(作为前缀的点为0表示小于1而大于0)
我怎么能在python中解决这个问题?我真的很感激,如果你能给我一些例子,用这样简单的方程:
x - y + 2z = 5
y - z = -1
z = 3
Run Code Online (Sandbox Code Playgroud)
(使用numpy)如果我们重写线性方程组
x - y + 2z = 5
y - z = -1
z = 3
Run Code Online (Sandbox Code Playgroud)
作为矩阵方程
A x = b
Run Code Online (Sandbox Code Playgroud)
同
A = np.array([[ 1, -1, 2],
[ 0, 1, -1],
[ 0, 0, 1]])
Run Code Online (Sandbox Code Playgroud)
和
b = np.array([5, -1, 3])
Run Code Online (Sandbox Code Playgroud)
然后x可以找到使用np.linalg.solve:
import numpy as np
A = np.array([(1, -1, 2), (0, 1, -1), (0, 0, 1)])
b = np.array([5, -1, 3])
x = np.linalg.solve(A, b)
Run Code Online (Sandbox Code Playgroud)
产量
print(x)
# [ 1. 2. 3.]
Run Code Online (Sandbox Code Playgroud)
我们可以检查A x = b:
print(np.dot(A,x))
# [ 5. -1. 3.]
assert np.allclose(np.dot(A,x), b)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7599 次 |
| 最近记录: |