Hi,

It seems that the only way to compute with Sage the Fourier series of a 
periodic real function is through the methods fourier_series_* of 
piecewise-defined 
functions 
<http://doc.sagemath.org/html/en/reference/functions/sage/functions/piecewise.html>.
 
Let us take a trivial example: the Fourier series of the cosine function:

sage: f = piecewise([((0,2*pi), cos(x))])

To get the cosine coefficient of order n of the Fourier series, one has to 
call the method fourier_series_cosine_coefficient with n and the 
half-period as argument:

sage: f.fourier_series_cosine_coefficient(1, pi)
1

So far so good. Now

sage: f.fourier_series_partial_sum(1, pi)
0

This is quite surprising at first glance and is actually the offset issue 
already discussed in #8603 <https://trac.sagemath.org/ticket/8603>. The 
expect result is obtained via

sage: f.fourier_series_partial_sum(2, pi)
cos(x)

To understand why one has to provide 2 as the first argument, let us have a 
look at the documentation:

sage: f.fourier_series_partial_sum?
Signature:      f.fourier_series_partial_sum(*args, **kwds)
Docstring:     
   Returns the partial sum

      f(x) sim frac{a_0}{2} + sum_{n=1}^N [a_ncos(frac{npi
      x}{L}) + b_nsin(frac{npi x}{L})],

   as a string.

At the very least, this is very incomplete: there is no explicit relation 
between N and *args, nor between L and *args. One has to infer them from 
the provided examples. Moreover the specified output type is wrong: it is 
not a string but a symbolic expression:

sage: type(f.fourier_series_partial_sum(2, pi))
<type 'sage.symbolic.expression.Expression'>

The documentation of fourier_series_cosine_coefficient is better because it 
has an INPUT section:

sage: f.fourier_series_cosine_coefficient?
Signature:      f.fourier_series_cosine_coefficient(*args, **kwds)
Docstring:     
   Returns the n-th Fourier series coefficient of cos(npi x/L), a_n.

   INPUT:

   * "self" - the function f(x), defined over -L x L

   * "n" - an integer n=0

   * "L" - (the period)/2

   OUTPUT: a_n = frac{1}{L}int_{-L}^L f(x)cos(npi x/L)dx

However , it  needs some correction: n=0 looks like a typo here. 

Besides the documentation, another issue is the relation between the period 
and the piecewise function domain. Since one has to provide the half-period 
L as an argument to the fourier_series_* methods, this suggests that L is 
independent from the domain provided to piecewise. Let us then define

sage: f = piecewise([((0,4*pi), cos(x))])

This is the same cosine function as before but defined on a larger domain. 
Let us ask for the Fourier coefficient of order 1  by providing pi as the 
half-period, as above:

sage: f.fourier_series_cosine_coefficient(1, pi)
2

Boom!
Having a look at the code of fourier_series_cosine_coefficient, one sees 
clearly why there is such an error:

sage: f.fourier_series_cosine_coefficient??
            from sage.all import cos, pi
            x = SR.var('x')
            result = 0
            for domain, f in parameters:
                for interval in domain:
                    a = interval.lower()
                    b = interval.upper()
                    result += (f*cos(pi*x*n/L)/L).integrate(x, a, b)
            return SR(result).simplify_trig()

The integral is taken over all the domain, while it should be taken over a 
single period only. 
A solution would be to deduce the half-period L from the domain (instead of 
getting it as an argument)  by writing something like

L = (self.domain().sup() - self.domain().inf())/2

I volunteer to write a ticket to improve the documentation and possibly the 
code. Any comment/suggestion prior to do so is welcome.

Eric. 

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to