Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically build a lambda function in python

Tags:

python

scipy

Supose that I want to generate a function to be later incorporated in a set of equations to be solved with scipy nsolve function. I want to create a function like this:

xi + xi+1 + xi+3 = 1

in which the number of variables will be dependent on the number of components. For example, if I have 2 components:

 f = lambda x: x[0] + x[1] - 1

for 3:

 f = lambda x: x[0] + x[1] + x[2] - 1

I specify the components as an array within the arguments of the function to be called:

 def my_func(components):
        for component in components:
        .....
        .....
        return f

I can't just find a way of doing this. I've to be able to make it this way as this function and other functions need to be solved together with nsolve:

 x0 = scipy.optimize.fsolve(f, [0, 0, 0, 0 ....])

Any help would be appreciated

Thanks!


Since I'm not sure which is the best way of doing this I will fully explain what I'm trying to do:

-I'm trying to generate this two functions to be later nsolved:

enter image description here

enter image description here

So I want to create a function teste([list of components]) that can return me this two equations (Psat(T) is a function I can call depending on the component and P is a constant(value = 760)).

Example:

  teste(['Benzene','Toluene'])

would return:

xBenzene + xToluene = 1

xBenzenePsat('Benzene') + xToluenePsat('Toluene') = 760

in the case of calling:

   teste(['Benzene','Toluene','Cumene'])

it would return:

xBenzene + xToluene + xCumene = 1

xBenzenePsat('Benzene') + xToluenePsat('Toluene') + xCumene*Psat('Cumene') = 760

All these x values are not something I can calculate and turn into a list I can sum. They are variables that are created as a function ofthe number of components I have in the system...

Hope this helps to find the best way of doing this

like image 467
user2574761 Avatar asked Jul 12 '13 01:07

user2574761


People also ask

How do you create a dynamic function in Python?

Python Code can be dynamically imported and classes can be dynamically created at run-time. Classes can be dynamically created using the type() function in Python. The type() function is used to return the type of the object. The above syntax returns the type of object.

How do you pass a dynamic argument in Python?

Passing arguments to the dynamic function is straight forward. We simply can make solve_for() accept *args and **kwargs then pass that to func() . Of course, you will need to handle the arguments in the function that will be called. Currently, none of the do_ methods in our example accept arguments.

Are lambdas faster than functions Python?

Lambda functions are inline functions and thus execute comparatively faster.


2 Answers

A direct translation would be:

f = lambda *x: sum(x) - 1

But not sure if that's really what you want.

like image 125
Jon Clements Avatar answered Oct 05 '22 21:10

Jon Clements


You can dynamically build a lambda with a string then parse it with the eval function like this:

a = [1, 2, 3]

s = "lambda x: "
s += " + ".join(["x[" + str(i) + "]" for i in xrange(0, 3)]) # Specify any range
s += " - 1"

print s

f = eval(s)
print f(a)
like image 38
cheeyos Avatar answered Oct 05 '22 21:10

cheeyos