ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 26 May 2017 07:51:21 -0500Periodic functionhttp://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.sokingFri, 26 May 2017 07:51:21 -0500http://ask.sagemath.org/question/37699/Defining a periodic function.http://ask.sagemath.org/question/10539/defining-a-periodic-function/Hi,
I am a new sage user. I'd like to define simple periodic maps over \R which plot and integrate correctly (eg. a square signal (which of course is discontinuous but which I would still like to be able to plot in a way that makes it clear that the function is not multivalued at discontinuity points)).
I tried different approaches none of which gave satisfactory results.
Any hint on how to do that nicely (or what would be the obstacles)?
Thank youlcSat, 14 Sep 2013 23:57:16 -0500http://ask.sagemath.org/question/10539/defining periodic functionshttp://ask.sagemath.org/question/7799/defining-periodic-functions/I'm trying to plot approximations to McCarthy's continuous nowhere differentiable function [(PDF file)](http://www-formal.stanford.edu/jmc/weierstrass.pdf). The definition is like this: first, define a function $g(x)$ to be a triangle wave:
$$g(x) = 1+x \ \text{ if } -2 \leq x \leq 0, \quad g(x) = 1-x \ \text{ if } 0 \leq x \leq 2$$
and then require $g$ to be periodic with period 4. Then McCarthy's function is
$$f(x) = \sum_{n=1}^{\infty} 2^{-n} g(2^{2^{n}} x).$$
How should I set this up in Sage? If I define g(x) by
def g(x):
if -2 <= x and x <= 0:
return 1+x
elif 0 < x and x <= 2:
return 1-x
elif x > 2:
return g(x-4)
return g(x+4)
and then try to plot the 4th partial sum for $f(x)$, I get an error about "maximum recursion depth exceeded". I get the same error if I try `plot(g, (x, 10000, 10010))`. Is there a better way of defining a periodic function like $g$? I guess I can do something like `while x>2: x = x-4`, etc., but my real question is, can I define such a function symbolically rather than as a Python function?
Edit: my current fastest version of this function is as follows:
%cython
def g(float x):
x = abs(x)
x = 4.0*(x/4.0 - int(x/4.0))
if x <= 2:
return 1-x
return -3+x
Even if it's not possible to write this symbolically, how can I speed this up?John PalmieriWed, 08 Dec 2010 06:36:18 -0600http://ask.sagemath.org/question/7799/