Interactive Fourier Series with SAGE math

Earlier today I was chatting to a lecturer over coffee about various mathematical packages that he might use for an upcoming Masters course  (note – offer me food or drink and I’m happy to talk about pretty much anything). He was mainly interested in Mathematica and so we spent most of our time discussing that but it is part of my job to make sure that he considers all of the alternatives – both commercial and open source. The course he was planning on running (which I’ll keep to myself out of respect for his confidentiality) was definitely a good fit for Mathematica but I felt that SAGE might suite him nicely as well.

“Does it have nice, interactive functionality like Mathematica’s Manipulate function?” he asked

Oh yes! Here is a toy example that I coded up in about the same amount of time that it took to write the introductory paragraph above (but hopefully it has no mistakes). With just a bit of effort pretty much anyone can make fully interactive mathematical demonstrations using completely free software. For more examples of SAGE’s interactive functionality check out their wiki.

Interactive Fourier Series

Here’s the code:

def ftermSquare(n):

def ftermSawtooth(n):

def ftermParabola(n):
 return((-1)^n/n^2 * cos(n*x))

def fseriesSquare(n):
 return(4/pi*sum(ftermSquare(i) for i in range (1,2*n,2)))

def fseriesSawtooth(n):
 return(1/2-1/pi*sum(ftermSawtooth(i) for i in range (1,n)))

def fseriesParabola(n):
 return(pi^2/3 + 4*sum(ftermParabola(i) for i in range(1,n)))

def plotFourier(n=slider(1, 30,1,10,'Number of terms')
,plotpoints=('Value of plot_points',[100,500,1000]),Function=['Saw Tooth','Square Wave','Periodic Parabola']):
    if Function=='Saw Tooth':
    if Function=='Square Wave':
    if Function=='Periodic Parabola':
  1. Swulf
    November 18th, 2009 at 06:15
    Reply | Quote | #1

    Neat. I run Sage under Ubuntu and have not yet ventured into scripting. Assuming I save your program as a file, where do I save it and how do I invoke it?

  2. November 18th, 2009 at 11:03
    Reply | Quote | #2


    You could just copy and paste it into the notebook interface – that’s all I’m doing at the moment.


  3. November 18th, 2009 at 11:43
    Reply | Quote | #3

    @Swulf: you can copy/paste the entire code into one cell in the notebook and execute it. that’s all. it doesn’t work on command line, since this is based on html.

  4. Vincent
    November 18th, 2009 at 15:03
    Reply | Quote | #4

    Plot[FourierSeries[fun[w] // N, w, m], {w, 0,
    2}, Evaluated -> True], {m, {10, 20, 30}}, {fun, {SquareWave,

    Gives you something similar, although much slower in Mathematica. The slowness comes from computing the fourier series all the time, rather then writing explicitly what it is like in the example above. you could of cause write the series as above and it would be instant like I asume the sage example is.

    Personally I much prefer mathematica because it becomes possible to write code which exactly resembles the formula in the book. Usually students have a sort of code-chock when they have to translate mathematics into a program, with mathematica most the time this translation isn’t needed.

  5. November 18th, 2009 at 15:25
    Reply | Quote | #5


    It’s always nice to see someone else using sage. This is a very concise way to show Fourier series too! Excellent code.

  6. November 18th, 2009 at 15:57
    Reply | Quote | #6

    @Clinton – glad you like it. I am always slightly nervous when posting code because if I make a mistake then I quickly get jumped on. It’s a good way to learn though :)

    @Vincent – Yep, that works but you are right – it is VERY slow. Far too slow to be interactive I think. If I were to code up a Mathematica version of this SAGE interaction then it would end up looking very similar to the Python code in the main post and the performance would be similar.

  7. Alex, Host Color
    November 18th, 2009 at 18:45
    Reply | Quote | #7

    Hi Mike,

    Thanks to you post I found SAGE and I have to say I like it. Can you please contact me on the email I have provided. I would like to ask you something about the blog.


  8. Vincent
    November 18th, 2009 at 23:15
    Reply | Quote | #8

    Just got home and took a quick stab at it, and it’s similar in function as the sage code, but has a higher resemblance to textbook math. (note that it looks even simpler in 2d-math mode).

    saw = 1/n Sin[n x Pi/3];
    square = 1/(2 n – 1) Sin[(2 n – 1) x Pi/3];
    polynomial = (-1)^n/n^2*Cos[n x];

    Plot[Sum[fn, {n, 1, nmax}], {x, 0, 10}, Evaluated -> True], {nmax, 1,30, 1},
    {{fn, saw}, {saw -> “Saw”, square -> “Square”, polynomial -> “Polynomial”}}]

    Also I found myself surprised that Mathematica actually returned a resulting expression for the sum
    Sum[1/n Sin[n x Pi/3],{n,1,nmax}]
    before I specified nmax. It’s a three term thing using the LerchPhi function so I won’t write it. Sometimes you sorta feel like your dealing with a hyperintelligen autistic boy on speed when dealing with Mathematica.

  9. December 23rd, 2009 at 05:14
    Reply | Quote | #9

    Inspired by your Sage @interact posts, I decided to try my hand at one. I’ve made one, and thought a fun way to share it would be the ‘Publish’ option at However, while the code shows up, the @interact-ivity doesn’t run. Have you used, and successfully published an @interact-ive worksheet, that I might look at and see what I’m doing wrong?

  10. December 23rd, 2009 at 10:13

    Hi Nick

    I have never used but I suggest that you send a message to the guys at sage-support asking for advice

    They are a friendly bunch and have helped me out a number of times.

    Looking forward to seeing your interaction.

  11. December 23rd, 2009 at 13:20

    Thanks @Mike. I did dig around on those groups a little, and found that @interact-ive bits are turned off, on purpose, for published worksheets. Understandable, from a resources and probably security standpoint, but too bad, nonetheless. Apparently it’s a goal to get these things changed, so maybe someday…

  12. marius_darie
    March 3rd, 2010 at 12:07


    I was locking for a language / software for implement my theoretic model.
    Now, I think I found it.
    I’m very impressed about sage, and @interact.

    Best wishes for all,

  13. Slawomir P K Wojcik
    December 13th, 2019 at 12:33

    just paste it into a cell of sage/python notebook and press shift+enter

    (a good way of learning programming is to comment other people’s programs)