Lambdify order of variables

43 Views Asked by At

When I use lambdify to turn a sympy function into one that can be used numerically, I need to pass the variables in the function as the first argument. I know that the number of Arguments is important, but is the order also important? How do I know, which letter is assigned to which variable in the original equation. For example:

x, b , c , d , r , z , p = symbols('x b c d r z p')    
eq = x*b*c*d
la = lambdify((x, b, c, d), eq)

I have entered the right order with the variables used prior, but I can also use the following without getting an error:

la = lambdify((r, x, z, p), eq)

Now I don't know what is assigned to what. And when I try to solve the equation with la(...) I don't know in what order to put the values. Is x now the first or the second value?

1

There are 1 best solutions below

0
jared On

As per the documentation:

The list of variables should match the structure of how the arguments will be passed to the function. Simply enclose the parameters as they will be passed in a list.

(I bolded the text to highlight the important line.)

This means that you should call the lambdified result with the arguments in the same order as they were passed when you lambdified it. So, in your first version x should be the first argument and in the second it should be the second.

But keep in mind that in your second version, r, z, and q won't do anything since they don't appear in eq, but you will still have to pass something for those variables.

Also, you can call help(la) and it will show you the generated function.

>> help(la)
Help on function _lambdifygenerated:

_lambdifygenerated(x, b, c, d)
    Created with lambdify. Signature:
    
    func(x, b, c, d)
    
    Expression:
    
    b*c*d*x
    
    Source code:
    
    def _lambdifygenerated(x, b, c, d):
        return b*c*d*x
    
    
    Imported modules:

Here you can see that the function signature is (x, b, c, d).