从最小化中排除一些参数
Exclude some arguments from the minimization
如果我有函数 func(x1,x2,x3)
,我可以使用最小化函数 scipy.optimize.minimize
并从优化过程中排除 x3
,其中 x3
定义为 numpy array
。
在这种情况下,我如何定义参数?对于 x3
.
的每个值,我应该得到一个包含 func
最小值的数组
例如:
def func(thet1,phai1,thet2,phai2,c2):
RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])
w, v = np.linalg.eig(RhoABC)
return w[1]
我想在 c2 = linspace(-1,1,10)
和角度属于 (0,2pi)
的地方将其最小化
也许你可以使用这样的东西:
def func(thet1,phai1,thet2,phai2,*args, c2 = []):
#considering c2 to be x3 in the above post
RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])
w, v = np.linalg.eig(RhoABC)
return w[1]
然后调用函数时:
retVal = func(thet1,phai1,thet2,phai2, c2=c2)
#you have to specify c2 first and then equate it to the value since this is an optional argument.
作为 jimmie 答案的替代方法,您可以使用 lambda 函数和解包运算符 *
:
minimize(lambda x: func(*x, linspace(-1,1,10)), x0=x0, ...)
将根据给定的 c2=linspace(-1,1,10)
.
最小化变量 thet1,phai1,thet2,phai2
的函数 func
如果我有函数 func(x1,x2,x3)
,我可以使用最小化函数 scipy.optimize.minimize
并从优化过程中排除 x3
,其中 x3
定义为 numpy array
。
在这种情况下,我如何定义参数?对于 x3
.
func
最小值的数组
例如:
def func(thet1,phai1,thet2,phai2,c2):
RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])
w, v = np.linalg.eig(RhoABC)
return w[1]
我想在 c2 = linspace(-1,1,10)
和角度属于 (0,2pi)
也许你可以使用这样的东西:
def func(thet1,phai1,thet2,phai2,*args, c2 = []):
#considering c2 to be x3 in the above post
RhoABC = np.array([[1,0,thet1,0,0,0,0,c1],[0,1,0,0,phai2,0,c2,0],[0,0,1,0,0,c2,thet2,0],[0,0,0,1,c2,0,0,0],[0,phai1,0,c2,1,0,0,0],[0,0,c2,0,0,1,0,thet2],[0,c2,0,0,0,0,1,0],[c1,0,0,phai1,0,0,0,1]])
w, v = np.linalg.eig(RhoABC)
return w[1]
然后调用函数时:
retVal = func(thet1,phai1,thet2,phai2, c2=c2)
#you have to specify c2 first and then equate it to the value since this is an optional argument.
作为 jimmie 答案的替代方法,您可以使用 lambda 函数和解包运算符 *
:
minimize(lambda x: func(*x, linspace(-1,1,10)), x0=x0, ...)
将根据给定的 c2=linspace(-1,1,10)
.
thet1,phai1,thet2,phai2
的函数 func