1

I have made a class which has a function as one of its attributes. The function is created based on parameters a,b, then stored in the class.

A write_derivatives method of the class takes the function and differentiates with sympy - it then spits out another lambda function.

However I can't seem to store the functions in the class.

class rosenbrock:
    def __init__(self, a, b):
        self.f_ros = lambda x0,x1:(a-x0)**2+b*(x1-x0**2)**2
        self._write_derivatives()

    def _write_derivatives(self):
        self.df_ros_dx0 = partial_diff(self.f_ros(x0,x1),x0) #creates another lambda function
        self.df_ros_dx1 = partial_diff(self.f_ros(x0,x1),x1)
        self.d2f_ros_dx0x0 = partial_diff(self.df_ros_dx0(x0,x1),x0)
        #etc.

Taking self.f_ros as an example; if I try to save it as I have written here, then I get an error when I call it in _write_derivatives

unsupported operand type(s) for -: 'int' and 'tuple'

Wrapping it in staticMethod as per this question allows me to save it, but then I can't use it without another error

'staticmethod' object is not callable

But then unwrapping it with __func__ as here leads back to the first error

self.df_ros_dx0 = partial_diff(self.f_ros.__func__(x0,x1),x0)

How can I re-jig my class so that I can store all these functions and use them within the class?

Greedo
  • 4,967
  • 2
  • 30
  • 78

0 Answers0