Skip to content Skip to sidebar Skip to footer

Evolving Functions In Python

Updated Question Following from my original post, with the use of @Attack68 's code, I have created a program that successfully evolved the function with a choice of multiplicat

Solution 1:

How about this:

classMyFunction:def__init__(self):
        deff1(x):
            return1.0self.functions = [f1]

    defappend_function(self, fn):
        self.functions.append(fn)

    def__call__(self, x):
        product = 1.0for f inself.functions:
            product *= f(x)
        return product

This object starts off as simply returning 1.0. Later you add more functions and it returns the product of all of them.

Solution 2:

Your description suggests your iterated values are combined through a product and are not in fact a composition of functions. A simple way of recording these is to have a set of base functions:

import numpy as np
import scipy.integrate asintdeftwo(x):
    return x*2definv(x):
    return1/x

base = [two, inv]

funcs = np.random.choice(base, size=10)

defapply(x, funcs):
    y = 1for func in funcs:
        y *= func(x)
    return y

print('function value at 1.5 ', apply(1.5, funcs))

answer = int.quad(apply, 1, 2, args=(funcs,))
print('integration over [1,2]: ', answer)

Post a Comment for "Evolving Functions In Python"