Bekaso
Bekaso

Reputation: 145

Exclude some arguments from the minimization

If I have a function func(x1,x2,x3), can I use the minimize function scipy.optimize.minimize with excluding x3 from the optimization process, where x3 is defined as numpy array. How can I define the argument in this case?. I'm supposed to get an array containing the minimum values of func for each value of x3.

For example:

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] 

I want to minimize it where c2 = linspace(-1,1,10) and the angles belong to (0,2pi)

Upvotes: 0

Views: 143

Answers (2)

joni
joni

Reputation: 7157

As an alternative to jimmie's answer, you can use a lambda function and the unpacking operator *:

minimize(lambda x: func(*x, linspace(-1,1,10)), x0=x0, ...)

will minimize the function func for the variables thet1,phai1,thet2,phai2 with given c2=linspace(-1,1,10).

Upvotes: 0

jimmie_roggers
jimmie_roggers

Reputation: 173

Maybe you could use something like this:

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] 

Then when you call the function:

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.

Upvotes: 1

Related Questions