[Solved] ValueError when defining a lambda function in python


You have a bunch of 1000 element arrays:

In [8]: p.shape
Out[8]: (1000,)
In [9]: K.shape
Out[9]: (1000,)
In [10]: R.shape
Out[10]: (1000,)
In [11]: np.minimum.reduce([p, K, R]).shape
Out[11]: (1000,)
In [12]: Vl(p).shape
Out[12]: (1000,)
In [8]: p.shape
Out[8]: (1000,)
In [9]: K.shape
Out[9]: (1000,)
In [10]: R.shape
Out[10]: (1000,)
In [11]: np.minimum.reduce([p, K, R]).shape
Out[11]: (1000,)
In [12]: Vl(p).shape
Out[12]: (1000,)

But integrate.quad is calling Vl with a scalar, an integration variable rangine from 0 to pbar. The nature of the integration is to evaluate Vl at a bunch of points, and sum the values appropriately.

Vl(0) produces this error because it is

In [15]: np.minimum.reduce([0, K, R])    
ValueError: setting an array element with a sequence.

So you need to change Vl to work with a scalar p, or perform your sum directly on the array.

Writing

Vl = lambda x: np.minimum.reduce([x, K, R])

might have clued you into the difference. Vl does not work with x different from the global p. K and R are globals, x is local to the lambda.

1

solved ValueError when defining a lambda function in python