Home > Mobile >  How to set a class of mathematical functions in Python?
How to set a class of mathematical functions in Python?

Time:11-08

I would like to create a math-visualisation class in Python where I can visualise optimisation algorithms. This is what I have so far:

class function_visualisation:
    def objective1n1(x):
        return x**5.0 - 2*x**4 - 0.5*x**3   4*x**2 - x

    def objective2n1(x,y):
        return x**5.0 - 2*y**4 - 15*x**3   4*y**2 - x

    def __init__(self, input = 0):
        self.fct_1 = self.objective1n1(x)
        self.fct_2 = self.objective2n1(x,y)

Where right now I have 2 polynomials; the first taking one and the second two inputs. Eventually I would like the class to have several adjustable function types and plotting methods. The problem is that when trying to instanciate Python says

    self.fct_1 = self.objective1n1(x)
NameError: name 'x' is not defined

which I can understand.

In various posts I saw people use an intuitive technique to define functions with numpy; e.g. :

x = np.linspace(−4*np.pi,4*np.pi,50)

y = np.linspace(−4*np.pi,4*np.pi,50)

z = x**2   y**2

Is there a good way to get something similar such that I don't have to give the function arguments in advance?

Edit: Thanks to both of you.

but as I commented, when I initialise and insert a concrete value

fcts = function_visualisation()

print(fcts.fct_1(5))

I get

    print(fcts.fct_1(5))
TypeError: objective1n1() takes 1 positional argument but 2 were given

The reason I do want a class like this is to package different plotting methods for different dimensions together and make it easy to change the function one contemplates. So how could I fix that?

CodePudding user response:

When you have a function, such as

def objective1n1(x):
    return x**5.0 - 2*x**4 - 0.5*x**3   4*x**2 - x

Calling objective1n1(x) will calculate this function at the input x, thus x has to be defined in advance (just like in your example with np.linspace).

If you simply want to assign a function object to another variable, use:

self.fct_1 = self.objective1n1

After that, you can call self.fct_1(x) for any x later in your code.

CodePudding user response:

self.objective1n1(x) is the call of the function self.objective1n1. If you simply want to change which function is behind self.fct_1, you need

self.fct_1 = self.objective1n1

If you don't need the self-reference, you can use the @staticmethod decorator. However, you might want to think about not using a class for this in the first place.

CodePudding user response:

To combine the answers of Martin Thoma, Mikhail Genkin and the comment of kindall:

Calling objective1n1(x) will calculate this function at the input x, thus x has to be defined in advance (just like in the example with np.linspace).

If one simply wants to assign a function object to another variable, use:

self.fct_1 = self.objective1n1

To be able to call the function like this

fcts = function_visualisation()

print(fcts.fct_1(5))

one has to define it either with a @staticmethod decorator

@staticmethod
def objective1n1(x):
    return x**5.0 - 2*x**4 - 0.5*x**3   4*x**2 - x

or with a self reference

def objective1n1(self,x):
    return x**5.0 - 2*x**4 - 0.5*x**3   4*x**2 - x
  • Related