ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 18 Jan 2015 04:56:44 -0600How to create a loop with two variables?http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/I have defined the following functions:
def I(x):
return 1
def Moebius(x):
return sum(moebius(i) * I(x*i^(-1)) for i in range(1,x+1))
The output is of course Mertens function. How do I turn `Moebius(x)` into an operator, so that I can define `Moebius^2(x)` as the Moebius transform of the output?
Sat, 03 Jan 2015 05:55:13 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/Answer by tmonteil for <p>I have defined the following functions: </p>
<pre><code>def I(x):
return 1
def Moebius(x):
return sum(moebius(i) * I(x*i^(-1)) for i in range(1,x+1))
</code></pre>
<p>The output is of course Mertens function. How do I turn <code>Moebius(x)</code> into an operator, so that I can define <code>Moebius^2(x)</code> as the Moebius transform of the output? </p>
http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?answer=25384#post-id-25384First, 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
Sat, 03 Jan 2015 06:29:08 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?answer=25384#post-id-25384Comment by Kevin Smith for <p>First, the <code>* I(x*i^(-1))</code> part in your <code>Moebius</code> function will only add some useless computation since it is a multiplication by <code>1</code>.</p>
<p>Then, you can define the composition of your <code>Moebius</code> function by itself as follows:</p>
<pre><code>M2 = lambda x : Moebius(Moebius(x))
</code></pre>
<p>If you want the <code>^2</code> notation to work, you have to define a Python class since <code>^2</code> calls the <code>__pow__</code> method of your object, this will require some more work around the following lines:</p>
<pre><code>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)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0
</code></pre>
<p>Or similarly:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25550#post-id-25550Excuse my absolute ignorance of programming- though I am now certain this is complete nonsense from sage's point of view, in an algebraic sense, I want the function I(x) to be something in the domain of the operator that I name Moebius. The reason I set I(x) to be constant initially was merely to demonstrate that I get the right output in this case. What I really want to do is take the output as an input to further applications of Moebius, so that I get the higher order Mertens functions, that is, the Mellin transforms of the negative powers of the Riemann zeta function. Of course there ought to be another way to do this involving extracting the number of times each primes divides a given integer, but that looked rather more difficult at first. It's equally likely that I am mistaken about that too though.Sun, 18 Jan 2015 04:56:44 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25550#post-id-25550Comment by tmonteil for <p>First, the <code>* I(x*i^(-1))</code> part in your <code>Moebius</code> function will only add some useless computation since it is a multiplication by <code>1</code>.</p>
<p>Then, you can define the composition of your <code>Moebius</code> function by itself as follows:</p>
<pre><code>M2 = lambda x : Moebius(Moebius(x))
</code></pre>
<p>If you want the <code>^2</code> notation to work, you have to define a Python class since <code>^2</code> calls the <code>__pow__</code> method of your object, this will require some more work around the following lines:</p>
<pre><code>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)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0
</code></pre>
<p>Or similarly:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25410#post-id-25410You could just write
def Moebius(x):
return sum(moebius(i) for i in range(1,x+1))
Which other input than `x` would you like ?Tue, 06 Jan 2015 01:07:12 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25410#post-id-25410Comment by Kevin Smith for <p>First, the <code>* I(x*i^(-1))</code> part in your <code>Moebius</code> function will only add some useless computation since it is a multiplication by <code>1</code>.</p>
<p>Then, you can define the composition of your <code>Moebius</code> function by itself as follows:</p>
<pre><code>M2 = lambda x : Moebius(Moebius(x))
</code></pre>
<p>If you want the <code>^2</code> notation to work, you have to define a Python class since <code>^2</code> calls the <code>__pow__</code> method of your object, this will require some more work around the following lines:</p>
<pre><code>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)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0
</code></pre>
<p>Or similarly:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25387#post-id-25387Without the presence of a function such as I(x), I don't see how Moebius even has an input ( other than x). Can you explain how your solution had another input please? Thank youSat, 03 Jan 2015 08:37:26 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25387#post-id-25387Comment by Kevin Smith for <p>First, the <code>* I(x*i^(-1))</code> part in your <code>Moebius</code> function will only add some useless computation since it is a multiplication by <code>1</code>.</p>
<p>Then, you can define the composition of your <code>Moebius</code> function by itself as follows:</p>
<pre><code>M2 = lambda x : Moebius(Moebius(x))
</code></pre>
<p>If you want the <code>^2</code> notation to work, you have to define a Python class since <code>^2</code> calls the <code>__pow__</code> method of your object, this will require some more work around the following lines:</p>
<pre><code>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)
</code></pre>
<p>Then you can do:</p>
<pre><code>sage: f = MyOperator(Moebius)
sage: f(10)
-1
sage: g = f^2
sage: g(10)
0
sage: f(f(10))
0
</code></pre>
<p>Or similarly:</p>
<pre><code>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
</code></pre>
http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25385#post-id-25385Thank you very much for such a prompt and clear reply. The I function was there because I'd hoped to have it as non-trivial input to the function.Sat, 03 Jan 2015 06:51:31 -0600http://ask.sagemath.org/question/25383/how-to-create-a-loop-with-two-variables/?comment=25385#post-id-25385