Skip to content Skip to sidebar Skip to footer

Product Of Two Functions

I have two functions, f and g. Both have the same signature: (x). I want to create a new function, z, with the same signature: def z(x): return f(x) * g(x) except that I'd like

Solution 1:

Something close is possible:

z = lambda x: f(x) * g(x)

Personally, I find this way more intuitive than z = f * g, because mathematically, multiplying functions doesn't mean anything. Depending on the interpretation of the * operator, it may mean composition so z(x) = f(g(x)), but definitely not multiplication of the results of invocation. On the other hand, the lambda above is very explicit, and frankly requires just a bit more characters to write.


Update: Kudos to JBernardo for hacking it together. I was imagining it would be much more hacky than in turned out. Still, I would advise against using this in real code.

Solution 2:

The funny thing is that it is quite possible. I made a project some days ago to do things like that.

Here it is: FuncBuilder

By now you can only define variables, but you can use my metaclass with the help of some other functions to build a class to what you want.

Problems:

  • It's slow
  • It's really slow
  • You think you want that but describing functions the way they meant to be described is the right way.

You should use your first code.

Just as a proof of concept:

from funcbuilder import OperatorMachinery

classFuncOperations(metaclass=OperatorMachinery):
     def__init__(self, function):
          self.func = function
     def__call__(self, *args, **kwargs):
          return self.func(*args, **kwargs)

deffunc(self, *n, oper=None):
    ifnot n:
        returntype(self)(lambda x: oper(self.func(x)))
    returntype(self)(lambda x: oper(self.func(x), n[0](x)))

FuncOperations.apply_operators([func, func])

Now you can code like that:

@FuncOperations
def f(x):
    return x + 1@FuncOperations
def g(x):
    return x + 2

And the desired behavior is:

>>>z = f * g>>>z(3)
20

I added a better version of it on the FuncBuilder project. It works with any operation between a FuncOperation object and another callable. Also works on unary operations. :D

You can play with it to make functions like:

z = -f + g * h

Solution 3:

I can be done with the exact syntax you intended (though using lambda might be better), by using a decorator. As stated, functions don't have operators defined for them, but objects can be made to be callable just like functions in Python -- So the decorator bellow just wraps the function in an object for which the multiplication for another function is defined:

classmultipliable(object):def__init__(self, func):
        self.func = func
    def__call__(self, *args, **kw):
        returnself.func(*args, **kw)
    def__mul__(self, other):
        @multipliabledefnew_func(*args, **kw):
            returnself.func(*args, **kw) * other(*args, **kw)
        return new_func

@multipliabledefx():
    return2

(tested in Python 2 and Python 3)

def y():
    return 3

z = x * y
z()

Post a Comment for "Product Of Two Functions"