Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you unpack a tuple of variable number of parameters (*args) inside a function?

What I have tried:

def df(t,x,*system_constants):
a,b,c,d,e,f,g,h,i,j,k,l,m = system_constants #unpack system constants
    #algebra with these constants to calculate something
    return something

This returns an error when called, saying:
"ValueError: not enough values to unpack (expected 13, got 0).

Also (regardless of this error), it might be better to use a short form unpacking with a for loop. What would this for loop look like?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

def df(t,x,*system_constants):

    for arg in system_constants:
        arg = system_constants
    #algebra with these constants to calculate something
    return something

The only objective here is to pass a tuple to a function without getting the above error, such that I can use the arguments without calling by index (i.e. without converting to a list)

The function is being called as an argument to the scipy.solve_ivp integrator:

sol = solve_ivp(df, [t0,tf], x0, dense_output = True)

The system_constants are stored in the same script just simply as:

system_constants = a,b,c,d,e,f,g,h,i,j,k,l,m

>Solution :

According to the documentation,

The calling signature is fun(t, y).

The function is only called with 2 arguments, you are expecting 15. That won’t work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading