这有数学方面和编程方面。在数学方面,需要注意的是,如果 ax=b 有多个解,那么这些解是 {y + b1 * t1 + b_2 * t_2 + ... + bN * tN | t1, ..., tN in the real numbers} 其中 y 是 ax=b 的任意解(例如最小二乘解),b1, ..., bN 是 a 的零空间的基向量。在编程方面,np.linalg.lstsq 获得最小二乘解,scipy.linalg.null_space 获得空空间。您可以将它们组合在一起以获得类似于您想要的输出的一种方法如下。
import numpy as np
import scipy.linalg
import sys
def print_parameterized_form(a, b):
one_solution = np.linalg.lstsq(a, b, rcond=None)[0]
null_space_basis = scipy.linalg.null_space(a)
for i in range(a.shape[1]):
sys.stdout.write('x{} = {}'.format(i, one_solution[i]))
for j in range(null_space_basis.shape[1]):
sys.stdout.write(' + ({}) * t{}'.format(null_space_basis[i, j], j))
sys.stdout.write('\n')
a = np.array([[1,2,4,1,0,2],[0,1,2,0,0,1],[0,0,0,2,2,0],[0,0,0,0,14,4],[0,0,0,0,0,-2]])
b = np.array([3,0,1,0,14])
print_parameterized_form(a, b)
这应该给你这样的东西:
x0 = 4.500000000000011 + (-3.5160449919006082e-15) * t0
x1 = 1.4000000000000128 + (0.8944271909999162) * t0
x2 = 2.7999999999999887 + (-0.4472135954999573) * t0
x3 = -1.499999999999997 + (9.065580383436411e-17) * t0
x4 = 2.0000000000000004 + (4.62652890306841e-18) * t0
x5 = -6.999999999999999 + (1.86607760441072e-16) * t0