简体   繁体   English

在 python 中寻找仿射空间的基础?

[英]Finding basis of affine space in python?

Suppose I am given an affine space as some conjunction of equalities say:假设我有一个仿射空间,正如一些等式的合取所说:

x + y + z = 2 && x - 3z = 4

which I represent in python as:我在 python 中表示为:

[ [1,1,1,2] , [1,0,-3,4] ]

I would like to find the basis of this affine set.我想找到这个仿射集的基础。 Does python have any such library? python有这样的图书馆吗?

Little bit of mathematics :一点点数学

Let the affine space be given by the matrix equation Ax = b.让仿射空间由矩阵方程 Ax = b 给出。 Let the k vectors {x_1, x_2, .. x_k } be the basis of the nullspace of A ie the space represented by Ax = 0. Let y be any particular solution of Ax = b.设 k 个向量 {x_1, x_2, .. x_k } 是 A 的零空间的基础,即 Ax = 0 表示的空间。设 y 是 Ax = b 的任何特定解。 Then the basis of the affine space represented by Ax = b is given by the (k+1) vectors {y, y + x_1, y + x_2, .. y + x_k }.然后由 Ax = b 表示的仿射空间的基础由 (k+1) 个向量 {y, y + x_1, y + x_2, .. y + x_k } 给出。 If there is no particular solution for Ax = b, then return some error message, as the set represented is empty.如果 Ax = b 没有特定的解决方案,则返回一些错误消息,因为表示的集合是空的。

For the above equation the matrix equation is:对于上述方程,矩阵方程为:

Ax = b where Ax = b 其中

A = [[1, 1, 1], [1, 0, -3]] A = [[1, 1, 1], [1, 0, -3]]

x = [x, y, z]^T x = [x, y, z]^T

b = [2, 4]^T b = [2, 4]^T

If you are looking for a numerical (ie approximate) solution then you can try this:如果您正在寻找数值(即近似)解决方案,那么您可以试试这个:

import numpy as np
import scipy

A = np.array([[1,1,1] , [1,0,-3]])
b = np.array([2, 4])

null_sp = scipy.linalg.null_space(A)
x0 = np.linalg.lstsq(A, b, rcond=None)[0][..., None]
aff_basis = np.c_[np.zeros(A.shape[1])[..., None], x] + x0
print(aff_basis) 

It gives:它给:

[[ 1.69230769  1.10395929]
 [ 1.07692308  1.86138762]
 [-0.76923077 -0.9653469 ]]

You find two points on the line by adding two constraints and solving for the remaining unknowns:通过添加两个约束并求解剩余的未知数,您可以在线上找到两个点:

x = 1 -> (1, 2, -1)
z = 0 -> (4, -2, 0)

This requires the resolution of two easy 2x2 systems, no need for a library.这需要两个简单的 2x2 系统的分辨率,不需要库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM