1

How can I turn

def square_error(f1,f2, u1,v1,sigu1,sigv1,rho1, u2,v2,sigu2,sigv2,rho2, u3,v3,sigu3,sigv3,rho3):

into:

def square_error(x):
#     x = [0.2,0.5,
#           1,1,1,1,0.3,
#           1,1,1,1,0.3,
#           1,1,1,1,0.3],
    f1,f2, u1,v1,sigu1,sigv1,rho1, u2,v2,sigu2,sigv2,rho2, u3,v3,sigu3,sigv3,rho3 = x

The scipy.minimize function only allow 1 argument for the target function, so I need to turn the variables into a single argument.

3
  • possible duplicate of *args and **kwargs? Commented Sep 14, 2015 at 2:43
  • np.array(x)? I'm not clear what you're asking. Commented Sep 14, 2015 at 2:48
  • @Alexander, I've update my question, could you see it again? Commented Sep 14, 2015 at 2:50

1 Answer 1

2

Yes you can do that, but you will have to the * syntax which will unpack the array into the parameters of the function.

For example:

def square_error( x,y,z ):
    print x,y,z

arr = [ 1, 2, 3 ]
square_error( *arr )

will unpack the values 1,2,3 into the parameters x,y,z


Or if you wanted to unpck the values into variables within the function, use sequence unpacking:

def square_error( arr ):
    x,y,z = arr
    print x,y,z

arr = [ 1, 2, 3 ]
square_error( arr )
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry, I may not be cleaer enough. I need to turn def square_error(x,y,z): into def square_error(x):, and within the function, unpack the x into x,y,z. How can I do that? The scipy.minimize function seems only allow for 1 argument.
Ah in that case, you don't need to do anything special, I've edited my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.