Yes, I think it is useful. I would model it off functions like fourier_transform, which have unevaluated counterparts if they cannot be computed.
There are a few other issues with the code as well. The best way to comment on them would be if you submitted a pull request, though, so I'll wait for that. As for where it should go, I'm not sure. This is more related to integration than series expansion, in the sense that the series module is more about taking finite series expansions and limits (which use series expansions). This is a full series expansion. I think it would be useful to have several of these (e.g., it would also be nice to have a function that could return the full summation taylor expansion for an expression). So I'm not sure where it should go. I guess start a new file in sympy/series. We can figure out if it belongs somewhere else later. Aaron Meurer On Sun, Oct 27, 2013 at 11:34 AM, Pablo Puente <[email protected]> wrote: > Hi, > > I have written 2 functions to support Fourier sine and cosine series. See > code below. > > I have these questions: > > 1. Do you think is worth to add it to Sympy? > 2. Should it be a part of series/series.py or a new file > series/series_fourier.py ? > 3. When testing it I tried to add it to series_py. The import for integrate > causes an Import Error. Any hints how to avoid it? > from .series import series > File "/home/pape/sympy/sympy/series/series.py", line 15, in <module> > from sympy.integrals import integrate > File "/home/pape/sympy/sympy/integrals/__init__.py", line 12, in <module> > from .integrals import integrate, Integral, line_integrate > File "/home/pape/sympy/sympy/integrals/integrals.py", line 3, in <module> > from sympy.concrete.expr_with_limits import AddWithLimits > File "/home/pape/sympy/sympy/concrete/__init__.py", line 1, in <module> > from .products import product, Product > File "/home/pape/sympy/sympy/concrete/products.py", line 11, in <module> > from sympy.polys import quo, roots > ImportError: cannot import name roots > > Thanks, > Pablo > ----- > x = symbols('x', real=True) > L= symbols('L', real=True, positive=True) > pprint(series_fourier(x, x, -L, L)) > > ∞ > _____ > ╲ > ╲ n ⎛π⋅n⋅x⎞ > ╲ -2⋅(-1) ⋅L⋅sin⎜─────⎟ > ╲ ⎝ L ⎠ > ╱ ────────────────────── > ╱ π⋅n > ╱ > ╱ > ‾‾‾‾‾ > n = 1 > > pprint(series_fourier_expansion(x/(2*L), x, 4, 0, 2*L)) > > ⎛π⋅x⎞ ⎛2⋅π⋅x⎞ ⎛3⋅π⋅x⎞ ⎛4⋅π⋅x⎞ > sin⎜───⎟ sin⎜─────⎟ sin⎜─────⎟ sin⎜─────⎟ > ⎝ L ⎠ ⎝ L ⎠ ⎝ L ⎠ ⎝ L ⎠ 1 > - ──────── - ────────── - ────────── - ────────── + ─ > π 2⋅π 3⋅π 4⋅π 2 > > ----- > > from __future__ import print_function, division > > from sympy.core.add import Add > from sympy.integrals import integrate > from sympy.core.singleton import S > from sympy.core.compatibility import xrange > from sympy.functions.elementary.trigonometric import cos, sin > from sympy.core.symbol import Symbol > from sympy.concrete.summations import Sum > > > def series_fourier_expansion(expr, x, n, interval_inf, interval_sup): > """ Returns a truncated Fourier sine and cosine series expansion > up to n+1 terms (sin(nx)/cos(nx)) over the indicated interval > """ > # http://mathworld.wolfram.com/FourierSeries.html > if interval_sup < interval_inf: > raise Exception('interval_sup cannot be less than interval_inf') > L = interval_sup - interval_inf > l = list() > integrate_tuple = (x, interval_inf, interval_sup) > l.append(integrate(expr, integrate_tuple)/L) # A0/2 > for m in xrange(1, n+1): > cos_expr = cos(2*m*S.Pi*x/L) > sin_expr = sin(2*m*S.Pi*x/L) > An = 2*integrate(expr*cos_expr, integrate_tuple)/L > Bn = 2*integrate(expr*sin_expr, integrate_tuple)/L > l.append(An*cos_expr) > l.append(Bn*sin_expr) > return Add(*l) > > > def series_fourier(expr, x, interval_inf, interval_sup): > """ Returns the unevaluated Fourier sine and cosine series calculated > over the indicated interval > """ > # http://mathworld.wolfram.com/FourierSeries.html > if interval_sup < interval_inf: > raise Exception('interval_sup cannot be less than interval_inf') > n = Symbol('n', integer=True, positive=True) > L = interval_sup - interval_inf > integrate_tuple = (x, interval_inf, interval_sup) > result = integrate(expr, integrate_tuple)/L # A0/2 > cos_expr = cos(2*n*S.Pi*x/L) > sin_expr = sin(2*n*S.Pi*x/L) > An = (2*integrate(expr*cos_expr, integrate_tuple)/L).simplify() > Bn = (2*integrate(expr*sin_expr, integrate_tuple)/L).simplify() > if An != S.Zero: > result += Sum(An*cos_expr, (n, 1, S.Infinity)) > if Bn != S.Zero: > result += Sum(Bn*sin_expr, (n, 1, S.Infinity)) > return result > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sympy. > For more options, visit https://groups.google.com/groups/opt_out. -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. For more options, visit https://groups.google.com/groups/opt_out.
