Ask Your Question

Revision history [back]

First, the * I(x*i^(-1)) part in your Moebius function will only add some useless computation since it is a multiplication by 1.

Then, you can define the composition of your Moebius function by itself as follows:

M2 = lambda x : Moebius(Moebius(x))

If you want the ^2 notation to work, you have to define a Python class since ^2 calls the __pow__ method of your object, this will require some more work around the following lines:

class MyOperator():
    def __init__(self, func):
        self.func = func
    def __pow__(self, exponent):
        def iterfunc(x):
            result = x
            for i in range(exponent):
                result = self.func(result)
            return result
        return MyOperator(iterfunc)
    def __call__(self, x):
        return self.func(x)

Then you can do:

sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0

First, the * I(x*i^(-1)) part in your Moebius function will only add some useless computation since it is a multiplication by 1.

Then, you can define the composition of your Moebius function by itself as follows:

M2 = lambda x : Moebius(Moebius(x))

If you want the ^2 notation to work, you have to define a Python class since ^2 calls the __pow__ method of your object, this will require some more work around the following lines:

class MyOperator():
    def __init__(self, func):
        self.func = func
    def __pow__(self, exponent):
        def iterfunc(x):
            result = x
            for i in range(exponent):
                result = self.func(result)
            return result
        return MyOperator(iterfunc)
    def __call__(self, x):
        return self.func(x)

Then you can do:

sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0

Or similarly:

sage: inc = lambda x : x+1
sage: f = MyOperator(inc)
sage: f(14)
15
sage: g = f^12
sage: g(14)
26

First, the * I(x*i^(-1)) part in your Moebius function will only add some useless computation since it is a multiplication by 1.

Then, you can define the composition of your Moebius function by itself as follows:

M2 = lambda x : Moebius(Moebius(x))

If you want the ^2 notation to work, you have to define a Python class since ^2 calls the __pow__ method of your object, this will require some more work around the following lines:

class MyOperator():
    def __init__(self, func):
        self.func = func
    def __pow__(self, exponent):
        def iterfunc(x):
            result = x
            for i in range(exponent):
                result = self.func(result)
            return result
        return MyOperator(iterfunc)
    def __call__(self, x):
        return self.func(x)

Then you can do:

sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0

Or similarly:

sage: inc = lambda x : x+1
sage: f = MyOperator(inc)
sage: f(14)
15
sage: g = f^12
sage: g(14)
26
sage: h = f^0
sage: h(100)
100