今天看Gilbert Strang的公开课,他提到了矩阵的应用中说了化学,
这种方法在我之前写kinetic模型的时候就用了,核心代码很少,就能把一连串基元反应配平并合并了哈哈哈。
来来来,上code(主要代码):

1
2
3
4
5
6
7
8
9
def null(A, eps=1e-10):
"get null space of transposition of site_matrix"
u, s, vh = np.linalg.svd(A, full_matrices=1, compute_uv=1)
null_space = np.compress(s <= eps, vh, axis=0)
return null_space.T
x = null(site_matrix.T) # basis of null space
if not x.any(): # x is not empty
raise ValueError('Failed to get basis of nullspace.')
x = map(abs, x.T.tolist()[0])

Comments