在 scipy 或 numpy 中获取空空间作为干净整数

问题描述 投票:0回答:1

我正在用 scipy 计算矩阵的零空间:

from scipy.linalg import null_space

A = np.array([[1,2],[3,6]])

print(null_space(A))

这输出 [[-0.89442719] [ 0.4472136 ]],但有一个更清晰的整数解:[[-2, 1]]。当它存在时,我怎样才能得到一个干净的整数解?

python numpy scipy linear-algebra
1个回答
0
投票

考虑使用 SymPy 库:

>>> import numpy as np
>>> import sympy as sp
>>> A = np.array([[1, 2], [3, 6]])
>>> S = sp.Matrix(A)
>>> null_space = S.nullspace()
>>> integer_null_space = [int(i) for i in null_space[0]]
>>> integer_null_space
[-2, 1]
© www.soinside.com 2019 - 2024. All rights reserved.