numpy:具有特定条件的线性系统.没有负面的解决方案 [英] Numpy: Linear system with specific conditions. No negative solutions
问题描述
我正在使用numpy编写Python代码.在我的代码中,我使用"linalg.solve"来求解n个变量中n个方程的线性系统.当然,解决方案可以是肯定的,也可以是否定的.我需要做的是始终具有正解或至少等于0.为此,我首先要让软件以这种形式求解我的线性方程组
x=np.linalg.solve(A,b)
,其中x是具有n个变量(按特定顺序(x1,x2,x3 ..... xn))的数组, A是n维方阵,b是n维数组. 现在我想这样做:
-求解方程组
-检查每个x是否为正值
-如果不是,则每个负数x我都希望它们为= 0(例如x2 = -2 ----> x2 = 0)
-泛型xn = 0想要消除n维平方矩阵A中的n行和n列(我将获得另一个平方矩阵A1),并消除b中的n元素以获得b1. /p>
-再次用矩阵A1和b1求解系统
-重复直到每个x为正或为零
-最后构建一个由n个元素组成的最终数组,在其中将放置最后的迭代解决方案以及每个等于零的变量(我需要按顺序排列它们,因为它没有任何孤立性,因此如果在迭代过程中是x2 = 0 -----> xfinal = [x1,0,x3,.....,xn]
认为它可以工作,但不知道如何在python中进行操作.
希望我很清楚.真的无法弄清楚!
您有一个最小化问题,即
min ||Ax - b||
s.t. x_i >= 0 for all i in [0, n-1]
您可以使用Scipy中的优化"模块
import numpy as np
from scipy.optimize import minimize
A = np.array([[1., 2., 3.],[4., 5., 6.],[7., 8., 10.]], order='C')
b = np.array([6., 12., 21.])
n = len(b)
# Ax = b --> x = [1., -2., 3.]
fun = lambda x: np.linalg.norm(np.dot(A,x)-b)
# xo = np.linalg.solve(A,b)
# sol = minimize(fun, xo, method='SLSQP', constraints={'type': 'ineq', 'fun': lambda x: x})
sol = minimize(fun, np.zeros(n), method='L-BFGS-B', bounds=[(0.,None) for x in xrange(n)])
x = sol['x'] # [2.79149722e-01, 1.02818379e-15, 1.88222298e+00]
使用您的方法,我得到x = [ 0.27272727, 0., 1.90909091]
.
如果您仍要使用算法,则在下面
n = len(b)
x = np.linalg.solve(A,b)
pos = np.where(x>=0.)[0]
while len(pos) < n:
Ap = A[pos][:,pos]
bp = b[pos]
xp = np.linalg.solve(Ap, bp)
x = np.zeros(len(b))
x[pos] = xp
pos = np.where(x>=0.)[0]
但是我不建议您使用它,您应该使用最小化选项.
I'm writing a Python code using numpy. In my code I use "linalg.solve" to solve a linear system of n equations in n variables. Of course the solutions could be either positive or negative. What I need to do is to have always positive solutions or at least equal to 0. To do so I first want the software to solve my linear system of equations in this form
x=np.linalg.solve(A,b)
in which x is an array with n variables in a specific order (x1, x2, x3.....xn), A is a n dimensional square matrix and b is a n-dimensional array. Now I thought to do this:
-solve the system of equations
-check if every x is positive
-if not, every negative x I'll want them to be =0 (for example x2=-2 ---->x2=0)
-with a generic xn=0 want to eliminate the n-row and the n-coloumn in the n dimensional square matrix A (I'll obtain another square matrix A1) and eliminate the n element in b obtaining b1.
-solve the system again with the matrix A1 and b1
-re-iterate untill every x is positive or zero
-at last build a final array of n elements in which I'll put the last iteration solutions and every variable which was equal to zero ( I NEED THEM IN ORDER AS IT WOULD HAVE BEEN NO ITERATIONS so if during the iterations it was x2=0 -----> xfinal=[x1, 0 , x3,.....,xn]
Think it 'll work but don't know how to do it in python.
Hope I was clear. Can't really figure it out!
You have a minimization problem, i.e.
min ||Ax - b||
s.t. x_i >= 0 for all i in [0, n-1]
You can use the Optimize module from Scipy
import numpy as np
from scipy.optimize import minimize
A = np.array([[1., 2., 3.],[4., 5., 6.],[7., 8., 10.]], order='C')
b = np.array([6., 12., 21.])
n = len(b)
# Ax = b --> x = [1., -2., 3.]
fun = lambda x: np.linalg.norm(np.dot(A,x)-b)
# xo = np.linalg.solve(A,b)
# sol = minimize(fun, xo, method='SLSQP', constraints={'type': 'ineq', 'fun': lambda x: x})
sol = minimize(fun, np.zeros(n), method='L-BFGS-B', bounds=[(0.,None) for x in xrange(n)])
x = sol['x'] # [2.79149722e-01, 1.02818379e-15, 1.88222298e+00]
With your method I get x = [ 0.27272727, 0., 1.90909091]
.
In the case you still want to use your algorithm, it is below
n = len(b)
x = np.linalg.solve(A,b)
pos = np.where(x>=0.)[0]
while len(pos) < n:
Ap = A[pos][:,pos]
bp = b[pos]
xp = np.linalg.solve(Ap, bp)
x = np.zeros(len(b))
x[pos] = xp
pos = np.where(x>=0.)[0]
But I don't recommend you to use it, you should use the minimize option.
这篇关于numpy:具有特定条件的线性系统.没有负面的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!