ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 08 Jun 2017 16:23:41 +0200Periodic functionhttps://ask.sagemath.org/question/37699/periodic-function/ HI all,
I want to write in sage a `2pi` periodic even function defined by `f(t) = -t + pi`, for `t` in `[0, pi)`. I already checked [Defining a periodic function](https://ask.sagemath.org/question/10539/defining-a-periodic-function/) and [defining periodic functions](https://ask.sagemath.org/question/7799/defining-periodic-functions/) on this plateform, but none of the provided solutions works for me. My main issue is that I need a way to transform any real number `x` into its unique representative in the interval `[-pi, pi)`. For that I used `frac` and `%` but they both raise errors. Could anyone help me out?
Thanks.Fri, 26 May 2017 14:51:21 +0200https://ask.sagemath.org/question/37699/periodic-function/Answer by ndomes for <p>HI all, </p>
<p>I want to write in sage a <code>2pi</code> periodic even function defined by <code>f(t) = -t + pi</code>, for <code>t</code> in <code>[0, pi)</code>. I already checked <a href="https://ask.sagemath.org/question/10539/defining-a-periodic-function/">Defining a periodic function</a> and <a href="https://ask.sagemath.org/question/7799/defining-periodic-functions/">defining periodic functions</a> on this plateform, but none of the provided solutions works for me. My main issue is that I need a way to transform any real number <code>x</code> into its unique representative in the interval <code>[-pi, pi)</code>. For that I used <code>frac</code> and <code>%</code> but they both raise errors. Could anyone help me out?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/37699/periodic-function/?answer=37700#post-id-37700EDITED
f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
UPDATE 2 --- a symbolic function:
f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)Fri, 26 May 2017 16:14:27 +0200https://ask.sagemath.org/question/37699/periodic-function/?answer=37700#post-id-37700Comment by dan_fulea for <p>EDITED </p>
<pre><code>f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
</code></pre>
<p>UPDATE 2 --- a symbolic function: </p>
<pre><code>f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37701#post-id-37701Well, `frac` is a misleading name in this case, since
sage: ( -1.5 ).frac()
-0.500000000000000
Or just plot:
plot(h,-3*pi,3*pi)Fri, 26 May 2017 17:35:13 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37701#post-id-37701Comment by ndomes for <p>EDITED </p>
<pre><code>f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
</code></pre>
<p>UPDATE 2 --- a symbolic function: </p>
<pre><code>f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37712#post-id-37712Thanks for the hint. I tried to improve my answer.Fri, 26 May 2017 22:39:21 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37712#post-id-37712Comment by soking for <p>EDITED </p>
<pre><code>f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
</code></pre>
<p>UPDATE 2 --- a symbolic function: </p>
<pre><code>f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37717#post-id-37717thanks, ndomes for your answer, bu the function should be even, yours is not :(. By the way are we obliged to rescale the function from `[0, pi)` to `[0,1)`? Is it not possible to solve this question directly (I mean without working on the interval `[0,1)`)?Sat, 27 May 2017 09:20:35 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37717#post-id-37717Comment by ndomes for <p>EDITED </p>
<pre><code>f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
</code></pre>
<p>UPDATE 2 --- a symbolic function: </p>
<pre><code>f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37722#post-id-37722Sorry for my misunderstanding. I ignored the 'even' because the function you provided isn't even. The easiest way to build a periodic function is to start with a periodic function. The start interval depends on the function you use and therefore we may need axis transformations to get the desired result. One more suggestion:
f(x) = abs(arccos(cos(x))-pi)Sun, 28 May 2017 09:33:39 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37722#post-id-37722Comment by soking for <p>EDITED </p>
<pre><code>f(t) = -t + pi # interval [0, pi]
g(t) = 1/2*f(t*pi) # transformed to interval [-1, 1]
# h = lambda x: 2*g(RR(x/pi).frac()) # including retransformation
h = lambda x: (heaviside(x)-1)*pi + 2*g(RR(x/pi).frac())
plot(h,-3*pi,3*pi)
</code></pre>
<p>UPDATE 2 --- a symbolic function: </p>
<pre><code>f(x) = 1/2*(sign(sin(2*x))*arccos(cos(2*x-pi)) + pi)
plot(f,-3*pi,3*pi,aspect_ratio=1)
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37723#post-id-37723I agree with you, that's why I initially started with this piece of code
v(x) = piecewise([([0, pi], -x + pi), ((pi, 2*pi), x - pi)])
where `v` is the function I want to duplicate all over the real line. But when I write
f(x) = v(T*RR(abs(x)/T).frac())
it raises an error, and it seems that the issue is coming from `T*RR(abs(x)/T).frac()`.
Even though your answer is correct and defines the function I wanted, I still want to find a way to do the same with the piecewise function I defined earlier, can you help me with it?Sun, 28 May 2017 18:33:03 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37723#post-id-37723Answer by dom for <p>HI all, </p>
<p>I want to write in sage a <code>2pi</code> periodic even function defined by <code>f(t) = -t + pi</code>, for <code>t</code> in <code>[0, pi)</code>. I already checked <a href="https://ask.sagemath.org/question/10539/defining-a-periodic-function/">Defining a periodic function</a> and <a href="https://ask.sagemath.org/question/7799/defining-periodic-functions/">defining periodic functions</a> on this plateform, but none of the provided solutions works for me. My main issue is that I need a way to transform any real number <code>x</code> into its unique representative in the interval <code>[-pi, pi)</code>. For that I used <code>frac</code> and <code>%</code> but they both raise errors. Could anyone help me out?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/37699/periodic-function/?answer=37769#post-id-37769For mapping one real interval to another interval, you have the numpy function interp() (linear approximation).
Example :
import numpy as np
from numpy import interp
prec = 200
xp = np.linspace(0.0, 99.0, prec)
yp = np.linspace(-1.0, 1.0, prec)
print interp([52.0],xp,yp)
The printed value is near 0.0 (middle of interval [-1,1]) because 52.0 is near 50.0 (middle of interval [0,99])
Fri, 02 Jun 2017 21:42:41 +0200https://ask.sagemath.org/question/37699/periodic-function/?answer=37769#post-id-37769Answer by dan_fulea for <p>HI all, </p>
<p>I want to write in sage a <code>2pi</code> periodic even function defined by <code>f(t) = -t + pi</code>, for <code>t</code> in <code>[0, pi)</code>. I already checked <a href="https://ask.sagemath.org/question/10539/defining-a-periodic-function/">Defining a periodic function</a> and <a href="https://ask.sagemath.org/question/7799/defining-periodic-functions/">defining periodic functions</a> on this plateform, but none of the provided solutions works for me. My main issue is that I need a way to transform any real number <code>x</code> into its unique representative in the interval <code>[-pi, pi)</code>. For that I used <code>frac</code> and <code>%</code> but they both raise errors. Could anyone help me out?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/37699/periodic-function/?answer=37702#post-id-37702It is, i think, the following function `f`...
def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
In a picture...
plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
Fri, 26 May 2017 17:46:19 +0200https://ask.sagemath.org/question/37699/periodic-function/?answer=37702#post-id-37702Comment by dan_fulea for <p>It is, i think, the following function <code>f</code>...</p>
<pre><code>def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
</code></pre>
<p>In a picture...</p>
<pre><code>plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37703#post-id-37703... done in the spirit of the previous post.Fri, 26 May 2017 17:47:09 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37703#post-id-37703Comment by soking for <p>It is, i think, the following function <code>f</code>...</p>
<pre><code>def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
</code></pre>
<p>In a picture...</p>
<pre><code>plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37718#post-id-37718Yeah that's the plot I wanted, but I don't understand the procedure. Can you explain it please?Sat, 27 May 2017 09:21:57 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37718#post-id-37718Comment by dan_fulea for <p>It is, i think, the following function <code>f</code>...</p>
<pre><code>def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
</code></pre>
<p>In a picture...</p>
<pre><code>plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37732#post-id-37732I tried to use the already implemented functions, combined in a suitable way.
First of all, we need an even function,
so it is natural to go through $x\to |x|$.
This explains the inner most
`abs`. Now we need to construct the shape of the function only for $x\ge 0$.
(This already removes the problem in the first answer, i could have also easily fallen in the trap
of the implemented `frac` function, which is not a periodic one, not the one from maths. People that
did some computations with the Riemann $\zeta$--function may be highly confused.)
Then we need a periodic function, let us use `frac` on the positive real halfline. Notation $x\to\{x\}$.
(The last notation is not the one element set, as my teacher joked each time he could...)
It has jumps at integer arguments, but the...Tue, 30 May 2017 00:42:32 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37732#post-id-37732Comment by dan_fulea for <p>It is, i think, the following function <code>f</code>...</p>
<pre><code>def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
</code></pre>
<p>In a picture...</p>
<pre><code>plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37733#post-id-37733...but the slope is almost right, so let us make it continuous somehow.
We consider then step by step:
$$ x\to \{x\}\ , $$
$$ x\to \{x\}-\frac 12\ , $$
$$ x\to \left|\ \{x\}-\frac 12\ \right|\ , $$
now it is continuous, and it remains a small step, rescaling arguments, and values of the last function.
Note: I have no idea how to markdown + latex {x}. Abve i tried to display the functions: {x}, then {x}-1/2, finally | {x} - 1/2 |Tue, 30 May 2017 00:43:25 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37733#post-id-37733Comment by soking for <p>It is, i think, the following function <code>f</code>...</p>
<pre><code>def g(x): return abs( 2*RR( abs(x)/2 ).frac() - 1 )
def f(x): return pi * g(x/pi)
</code></pre>
<p>In a picture...</p>
<pre><code>plot( g, -3, 3, aspect_ratio=1 )
plot( f, -3*pi, 3*pi, aspect_ratio=1 )
</code></pre>
https://ask.sagemath.org/question/37699/periodic-function/?comment=37846#post-id-37846OK, thanks for the explanation. I didn't get everything, but I'll take sometime to read and understand each step.Thu, 08 Jun 2017 16:23:41 +0200https://ask.sagemath.org/question/37699/periodic-function/?comment=37846#post-id-37846