
    EdeY                        d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	 ddl
mZ ddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ d Zd Zd Z d Z! G d de          Z" G d de"          Z#ddZ$dS )zFourier Series    )oopi)Wild)Expr)Add)Tuple)S)DummySymbol)sympify)sincossinc)
SeriesBase)
SeqFormula)Interval)is_sequencec                 V   ddl m} |d         |d         |d         z
  }}t          d|z  t          z  |z  |z            }d|z   || |z  |          z  |z  }|                    |t
          j                  dz  }|t          d|z   || |z  |          z  |z  |dt          f          fS )z,Returns the cos sequence in a Fourier seriesr   	integrate      )	sympy.integralsr   r   r   subsr	   Zeror   r   )	funclimitsnr   xLcos_termformulaa0s	            4lib/python3.11/site-packages/sympy/series/fourier.pyfourier_cos_seqr%      s    ))))))!9fQi&)+qA1Q3r6!8a<  H(lYYth???!CG	a	 	 1	$Bz!h,4(?F)K)KK !1bz+ + + +    c                     ddl m} |d         |d         |d         z
  }}t          d|z  t          z  |z  |z            }t	          d|z   || |z  |          z  |z  |dt
          f          S )z,Returns the sin sequence in a Fourier seriesr   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r    sin_terms          r$   fourier_sin_seqr)      s    ))))))!9fQi&)+qA1Q3r6!8a<  Ha(lYYth%G%GGq":' ' 'r&   c                    d }d\  }}}| ||           t            t           }}}t          |t                    r=t          |          dk    r|\  }}}n#t          |          dk    r ||           }|\  }}t	          |t
                    r||t          dt          |          z            t          j	        t          j
        g}||v s||v rt          d          t          |||f          S )a  
    Limits should be of the form (x, start, stop).
    x should be a symbol. Both start and stop should be bounded.

    Explanation
    ===========

    * If x is not given, x is determined from func.
    * If limits is None. Limit of the form (x, -pi, pi) is returned.

    Examples
    ========

    >>> from sympy.series.fourier import _process_limits as pari
    >>> from sympy.abc import x
    >>> pari(x**2, (x, -2, 2))
    (x, -2, 2)
    >>> pari(x**2, (-2, 2))
    (x, -2, 2)
    >>> pari(x**2, None)
    (x, -pi, pi)
    c                     | j         }t          |          dk    r|                                S |st          d          S t	          d| z            )Nr   kz specify dummy variables for %s. If the function contains more than one free symbol, a dummy variable should be supplied explicitly e.g. FourierSeries(m*n**2, (n, -pi, pi)))free_symbolslenpopr
   
ValueError)r   frees     r$   _find_xz _process_limits.<locals>._find_x=   s`     t99> 		88:: 	::P   r&   )NNNN   r   zInvalid limits given: %sz.Both the start and end value should be bounded)r   r   r   r.   
isinstancer   r0   strr	   NegativeInfinityInfinityr   )r   r   r2   r   startstop	unboundeds          r$   _process_limitsr;   &   s#   .   &NAud 0 R$565!! !v;;! 	!#NAudd[[A 	!A KE4a   CE CT C3c&kkABBB#QZ0I	 KTY. KIJJJAud#$$$r&   c                    d }fd}ddl m}m}m}  | | ||                               }|                                }	t          dd d g          t          d	fd
g          |	d         D ]B}
|
                                d         }|D ]#} ||          s |||          sd| fc c S $Cd|fS )Nc                     || j         vS Nr-   )exprsr   s     r$   check_fxzfinite_check.<locals>.check_fx`   s    ***r&   c                     t          | t          t          f          r7| j        d         }|                    t
          |z  z  |z  z             dS dS d S )Nr   TF)r4   r   r   argsmatchr   )_exprr   r    sincos_argsabs       r$   check_sincosz"finite_check.<locals>.check_sincosc   s^    ec3Z(( 	*Q-K  BqD!a00 tu	 	r&   r   )TR2TR1sincos_to_sumrG   c                     | j         S r>   
is_Integerr,   s    r$   <lambda>zfinite_check.<locals>.<lambda>p   s     r&   c                 "    | t           j        k    S r>   r	   r   rP   s    r$   rQ   zfinite_check.<locals>.<lambda>p   s    QV r&   
propertiesrH   c                     | j         vS r>   r?   r,   r   s    r$   rQ   zfinite_check.<locals>.<lambda>q   s    (? r&   r   FT)sympy.simplify.furJ   rK   rL   as_coeff_addr   as_coeff_mul)fr   r    rA   rI   rJ   rK   rL   rE   	add_coeffs
mul_coeffstrG   rH   s    `           @@r$   finite_checkr`   ^   s@   + + +      :999999999M##cc!ff++&&E""$$IS446K6KNOOOAS????BCCCAq\    ^^%%a(
 	  	 AHQNN  ll1a&;&;  ax	  ;r&   c                   X   e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zed	             Zed
             Zed             Zed             Zed             Zd ZddZddZd Zd Zd Zd ZddZd Zd Zd Zd ZdS )FourierSeriesa9  Represents Fourier sine/cosine series.

    Explanation
    ===========

    This class only represents a fourier series.
    No computation is performed.

    For how to compute Fourier series, see the :func:`fourier_series`
    docstring.

    See Also
    ========

    sympy.series.fourier.fourier_series
    c                 P    t          t          |          }t          j        | g|R  S r>   )mapr   r   __new__)clsrC   s     r$   re   zFourierSeries.__new__   s)    7D!!|C'$''''r&   c                     | j         d         S Nr   rC   selfs    r$   functionzFourierSeries.function   s    y|r&   c                 (    | j         d         d         S Nr   r   ri   rj   s    r$   r   zFourierSeries.x       y|Ar&   c                 N    | j         d         d         | j         d         d         fS )Nr   r   ri   rj   s    r$   periodzFourierSeries.period   s!    	!Q1a11r&   c                 (    | j         d         d         S )Nr   r   ri   rj   s    r$   r#   zFourierSeries.a0   ro   r&   c                 (    | j         d         d         S )Nr   r   ri   rj   s    r$   anzFourierSeries.an   ro   r&   c                 (    | j         d         d         S )Nr   ri   rj   s    r$   bnzFourierSeries.bn   ro   r&   c                 ,    t          dt                    S rh   )r   r   rj   s    r$   intervalzFourierSeries.interval   s    2r&   c                     | j         j        S r>   )rx   infrj   s    r$   r8   zFourierSeries.start       }  r&   c                     | j         j        S r>   )rx   suprj   s    r$   r9   zFourierSeries.stop   r{   r&   c                     t           S r>   )r   rj   s    r$   lengthzFourierSeries.length   s    	r&   c                 X    t          | j        d         | j        d         z
            dz  S )Nr   r   r   )absrq   rj   s    r$   r    zFourierSeries.L   s&    4;q>DKN233a77r&   c                 B    | j         }|                    |          r| S d S r>   )r   has)rk   oldnewr   s       r$   
_eval_subszFourierSeries._eval_subs   s*    F771:: 	K	 	r&   r3   c                     |t          |           S g }| D ]:}t          |          |k    r n$|t          j        ur|                    |           ;t          | S )a  
        Return the first n nonzero terms of the series.

        If ``n`` is None return an iterator.

        Parameters
        ==========

        n : int or None
            Amount of non-zero terms in approximation or None.

        Returns
        =======

        Expr or iterator :
            Approximation of function expanded into Fourier series.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x, (x, -pi, pi))
        >>> s.truncate(4)
        2*sin(x) - sin(2*x) + 2*sin(3*x)/3 - sin(4*x)/2

        See Also
        ========

        sympy.series.fourier.FourierSeries.sigma_approximation
        )iterr.   r	   r   appendr   )rk   r   termsr_   s       r$   truncatezFourierSeries.truncate   sl    @  	:: 	  	 A5zzQ   QE{r&   c                 \    fdt          | d                   D             }t          | S )a  
        Return :math:`\sigma`-approximation of Fourier series with respect
        to order n.

        Explanation
        ===========

        Sigma approximation adjusts a Fourier summation to eliminate the Gibbs
        phenomenon which would otherwise occur at discontinuities.
        A sigma-approximated summation for a Fourier series of a T-periodical
        function can be written as

        .. math::
            s(\theta) = \frac{1}{2} a_0 + \sum _{k=1}^{m-1}
            \operatorname{sinc} \Bigl( \frac{k}{m} \Bigr) \cdot
            \left[ a_k \cos \Bigl( \frac{2\pi k}{T} \theta \Bigr)
            + b_k \sin \Bigl( \frac{2\pi k}{T} \theta \Bigr) \right],

        where :math:`a_0, a_k, b_k, k=1,\ldots,{m-1}` are standard Fourier
        series coefficients and
        :math:`\operatorname{sinc} \Bigl( \frac{k}{m} \Bigr)` is a Lanczos
        :math:`\sigma` factor (expressed in terms of normalized
        :math:`\operatorname{sinc}` function).

        Parameters
        ==========

        n : int
            Highest order of the terms taken into account in approximation.

        Returns
        =======

        Expr :
            Sigma approximation of function expanded into Fourier series.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x, (x, -pi, pi))
        >>> s.sigma_approximation(4)
        2*sin(x)*sinc(pi/4) - 2*sin(2*x)/pi + 2*sin(3*x)*sinc(3*pi/4)/3

        See Also
        ========

        sympy.series.fourier.FourierSeries.truncate

        Notes
        =====

        The behaviour of
        :meth:`~sympy.series.fourier.FourierSeries.sigma_approximation`
        is different from :meth:`~sympy.series.fourier.FourierSeries.truncate`
        - it takes all nonzero terms of degree smaller than n, rather than
        first n nonzero ones.

        References
        ==========

        .. [1] https://en.wikipedia.org/wiki/Gibbs_phenomenon
        .. [2] https://en.wikipedia.org/wiki/Sigma_approximation
        c                 l    g | ]0\  }}|t           j        ut          t          |z  z            |z  1S  )r	   r   r   r   ).0ir_   r   s      r$   
<listcomp>z5FourierSeries.sigma_approximation.<locals>.<listcomp>0  sL     % % %$!QQVO%b1fqj!!A% % % %r&   N)	enumerater   )rk   r   r   s    ` r$   sigma_approximationz!FourierSeries.sigma_approximation   sE    D% % % %)D!H2E2E % % %E{r&   c                     t          |          | j        }}||j        v rt          d|d|          | j        |z   }| j        |z   }|                     || j        d         || j        | j	        f          S )a  
        Shift the function by a term independent of x.

        Explanation
        ===========

        f(x) -> f(x) + s

        This is fast, if Fourier series of f(x) is already
        computed.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x**2, (x, -pi, pi))
        >>> s.shift(1).truncate()
        -4*cos(x) + cos(2*x) + 1 + pi**2/3
        '' should be independent of r   )
r   r   r-   r0   r#   rl   r   rC   rt   rv   )rk   r]   r   r#   sfuncs        r$   shiftzFourierSeries.shift4  s~    * qzz461 	J*111aaHIIIWq[!yy	!r47DG.DEEEr&   c                 r   t          |          | j        }}||j        v rt          d|d|          | j                            |||z             }| j                            |||z             }| j                            |||z             }|                     || j	        d         | j
        ||f          S )a  
        Shift x by a term independent of x.

        Explanation
        ===========

        f(x) -> f(x + s)

        This is fast, if Fourier series of f(x) is already
        computed.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x**2, (x, -pi, pi))
        >>> s.shiftx(1).truncate()
        -4*cos(x + 1) + cos(2*x + 2) + pi**2/3
        r   r   r   r   r   r-   r0   rt   r   rv   rl   r   rC   r#   rk   r]   r   rt   rv   r   s         r$   shiftxzFourierSeries.shiftxS      * qzz461 	J*111aaHIIIW\\!QU##W\\!QU##""1a!e,,yy	!twB.?@@@r&   c                 P   t          |          | j        }}||j        v rt          d|d|          | j                            |          }| j                            |          }| j        |z  }| j        d         |z  }| 	                    || j        d         |||f          S )a  
        Scale the function by a term independent of x.

        Explanation
        ===========

        f(x) -> s * f(x)

        This is fast, if Fourier series of f(x) is already
        computed.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x**2, (x, -pi, pi))
        >>> s.scale(2).truncate()
        -8*cos(x) + 2*cos(2*x) + 2*pi**2/3
        r   r   r   r   )
r   r   r-   r0   rt   	coeff_mulrv   r#   rC   r   )rk   r]   r   rt   rv   r#   r   s          r$   scalezFourierSeries.scales  s    * qzz461 	J*111aaHIIIWq!!Wq!!Wq[	!q yy	!r2rl;;;r&   c                 r   t          |          | j        }}||j        v rt          d|d|          | j                            |||z            }| j                            |||z            }| j                            |||z            }|                     || j	        d         | j
        ||f          S )a  
        Scale x by a term independent of x.

        Explanation
        ===========

        f(x) -> f(s*x)

        This is fast, if Fourier series of f(x) is already
        computed.

        Examples
        ========

        >>> from sympy import fourier_series, pi
        >>> from sympy.abc import x
        >>> s = fourier_series(x**2, (x, -pi, pi))
        >>> s.scalex(2).truncate()
        -4*cos(2*x) + cos(4*x) + pi**2/3
        r   r   r   r   r   s         r$   scalexzFourierSeries.scalex  r   r&   Nr   c                 4    | D ]}|t           j        ur|c S d S r>   rS   )rk   r   logxcdirr_   s        r$   _eval_as_leading_termz#FourierSeries._eval_as_leading_term  s7     	 	A 	 	r&   c                     |dk    r| j         S | j                            |          | j                            |          z   S rh   )r#   rt   coeffrv   )rk   pts     r$   
_eval_termzFourierSeries._eval_term  s<    7 	7Nw}}R  47==#4#444r&   c                 ,    |                      d          S )N)r   rj   s    r$   __neg__zFourierSeries.__neg__  s    zz"~~r&   c                    t          |t                    r| j        |j        k    rt          d          | j        |j        }}| j        |j                            ||          z   }| j        |j        vr|S | j        |j        z   }| j	        |j	        z   }| j
        |j
        z   }|                     || j        d         |||f          S t          | |          S )N(Both the series should have same periodsr   )r4   rb   rq   r0   r   rl   r   r-   rt   rv   r#   r   rC   r   )rk   otherr   yrl   rt   rv   r#   s           r$   __add__zFourierSeries.__add__  s    e]++ 	C{el* M !KLLL657qA}u~':':1a'@'@@HvX22  58#B58#B58#B99Xty|b"b\BBB4r&   c                 .    |                      |           S r>   )r   )rk   r   s     r$   __sub__zFourierSeries.__sub__  s    ||UF###r&   )r3   rh   )__name__
__module____qualname____doc__re   propertyrl   r   rq   r#   rt   rv   rx   r8   r9   r   r    r   r   r   r   r   r   r   r   r   r   r   r   r   r&   r$   rb   rb   |   s+         ( ( (   X   X 2 2 X2   X   X   X   X ! ! X! ! ! X!   X 8 8 X8  
* * * *XD D D DLF F F>A A A@< < <BA A A@   
5 5 5
       &$ $ $ $ $r&   rb   c                   b    e Zd ZdZd Zed             Zed             Zd Zd Z	d Z
d Zd	 Zd
S )FiniteFourierSeriesa  Represents Finite Fourier sine/cosine series.

    For how to compute Fourier series, see the :func:`fourier_series`
    docstring.

    Parameters
    ==========

    f : Expr
        Expression for finding fourier_series

    limits : ( x, start, stop)
        x is the independent variable for the expression f
        (start, stop) is the period of the fourier series

    exprs: (a0, an, bn) or Expr
        a0 is the constant term a0 of the fourier series
        an is a dictionary of coefficients of cos terms
         an[k] = coefficient of cos(pi*(k/L)*x)
        bn is a dictionary of coefficients of sin terms
         bn[k] = coefficient of sin(pi*(k/L)*x)

        or exprs can be an expression to be converted to fourier form

    Methods
    =======

    This class is an extension of FourierSeries class.
    Please refer to sympy.series.fourier.FourierSeries for
    further information.

    See Also
    ========

    sympy.series.fourier.FourierSeries
    sympy.series.fourier.fourier_series
    c           	      B   t          |          }t          |          }t          |          }t          |t                    rt          |          dk    s|                                \  }}ddlm |t          fd|D              z   }|                    dddd                                          \  }}|d         t          |d         |d         z
            dz  }	t          d	d
 d g          }
t          dfdg          }i }i }|D ]}|                    |t          |
t          |	z  z  z            z            }|                    |t          |
t          |	z  z  z            z            }|r9||         |                    ||
         t           j                  z   |||
         <   |r9||         |                    ||
         t           j                  z   |||
         <   ||z  }t          |||          }t%          j        | |||          S )Nr3   r   )TR10c                 &    g | ]} |          S r   r   )r   r   r   s     r$   r   z/FiniteFourierSeries.__new__.<locals>.<listcomp>  s!    111!dd1gg111r&   F)trig
power_base	power_explogr   r   rG   c                     | j         S r>   rN   rP   s    r$   rQ   z-FiniteFourierSeries.__new__.<locals>.<lambda>  s     r&   c                     | t           j        uS r>   rS   rP   s    r$   rQ   z-FiniteFourierSeries.__new__.<locals>.<lambda>  s    QRQW r&   rT   rH   c                     | j         vS r>   r?   rW   s    r$   rQ   z-FiniteFourierSeries.__new__.<locals>.<lambda>  s    0G r&   )r   r4   r   r.   rY   rX   r   r   expandr   r   rD   r   r   r   getr	   r   r   re   )rf   r[   r   r@   cerexprr#   exp_lsr    rG   rH   rt   rv   pr_   qr   r   s                    @@r$   re   zFiniteFourierSeries.__new__  s.   AJJ5%(( 	&SZZ1_ 	&%%''DAq......1111q11122E5UeY^__llnnJBq	AF1Iq	)**Q.AS&<&<>W>W%Z[[[AS&G&G&G&G%JKKKABB   GGAAaL1$4 5 5566GGAAaL1$4 5 5566  tbffQqT16&:&::BqtHH  tbffQqT16&:&::BqtHH!GBB"b"%%E|CFE222r&   c           	         | j         rdnd}|t          t          | j                                                                      t          | j                                                                      dz   z  }t          d|          S rn   )r#   maxsetrt   keysunionrv   r   )rk   _lengths     r$   rx   zFiniteFourierSeries.interval#  sk    w%!!A3s47<<>>**00TW\\^^1D1DEEFFJJ7###r&   c                      | j         | j        z
  S r>   )r9   r8   rj   s    r$   r   zFiniteFourierSeries.length)  s    y4:%%r&   c                 @   t          |          | j        }}||j        v rt          d|d|          |                                                     |||z             }| j                            |||z             }|                     || j        d         |          S Nr   r   r   	r   r   r-   r0   r   r   rl   r   rC   rk   r]   r   rE   r   s        r$   r   zFiniteFourierSeries.shiftx-      qzz461 	J*111aaHIII$$QA..""1a!e,,yy	!e444r&   c                     t          |          | j        }}||j        v rt          d|d|          |                                 |z  }| j        |z  }|                     || j        d         |          S r   )r   r   r-   r0   r   rl   r   rC   r   s        r$   r   zFiniteFourierSeries.scale8  sx    qzz461 	J*111aaHIII!#!yy	!e444r&   c                 @   t          |          | j        }}||j        v rt          d|d|          |                                                     |||z            }| j                            |||z            }|                     || j        d         |          S r   r   r   s        r$   r   zFiniteFourierSeries.scalexC  r   r&   c                 V   |dk    r| j         S | j                            |t          j                  t          |t          | j        z  z  | j        z            z  | j	                            |t          j                  t          |t          | j        z  z  | j        z            z  z   }|S rh   )r#   rt   r   r	   r   r   r   r    r   rv   r   )rk   r   _terms      r$   r   zFiniteFourierSeries._eval_termN  s    7 	7NB''#bBK.@46.I*J*JJ'++b!&))Cb46k0BTV0K,L,LLMr&   c                    t          |t                    r5|                    t          | j        | j        d         d                    S t          |t                    r|| j        |j        k    rt          d          | j	        |j	        }}| j        |j        
                    ||          z   }| j	        |j        vr|S t          || j        d                   S d S )Nr   F)finiter   )r   )r4   rb   r   fourier_seriesrl   rC   r   rq   r0   r   r   r-   )rk   r   r   r   rl   s        r$   r   zFiniteFourierSeries.__add__V  s    e]++ 	A==ty|7<"> "> "> ? ? ?233 
	A{el* M !KLLL657qA}u~':':1a'@'@@HvX22  !(49Q<@@@@
	A 
	Ar&   N)r   r   r   r   re   r   rx   r   r   r   r   r   r   r   r&   r$   r   r     s        $ $L"3 "3 "3H $ $ X$
 & & X&	5 	5 	5	5 	5 	5	5 	5 	5  A A A A Ar&   r   NTc                    t          |           } t          | |          }|d         }|| j        vr| S |rHt          |d         |d         z
            dz  }t	          | ||          \  }}|rt          | ||          S t          d          }|d         |d         z   dz  }|j        r|                     ||           }	| |	k    r?t          | ||          \  }
}t          ddt          f          }t          | ||
||f          S | |	 k    rHt          j        }
t          ddt          f          }t          | ||          }t          | ||
||f          S t          | ||          \  }
}t          | ||          }t          | ||
||f          S )a`  Computes the Fourier trigonometric series expansion.

    Explanation
    ===========

    Fourier trigonometric series of $f(x)$ over the interval $(a, b)$
    is defined as:

    .. math::
        \frac{a_0}{2} + \sum_{n=1}^{\infty}
        (a_n \cos(\frac{2n \pi x}{L}) + b_n \sin(\frac{2n \pi x}{L}))

    where the coefficients are:

    .. math::
        L = b - a

    .. math::
        a_0 = \frac{2}{L} \int_{a}^{b}{f(x) dx}

    .. math::
        a_n = \frac{2}{L} \int_{a}^{b}{f(x) \cos(\frac{2n \pi x}{L}) dx}

    .. math::
        b_n = \frac{2}{L} \int_{a}^{b}{f(x) \sin(\frac{2n \pi x}{L}) dx}

    The condition whether the function $f(x)$ given should be periodic
    or not is more than necessary, because it is sufficient to consider
    the series to be converging to $f(x)$ only in the given interval,
    not throughout the whole real line.

    This also brings a lot of ease for the computation because
    you do not have to make $f(x)$ artificially periodic by
    wrapping it with piecewise, modulo operations,
    but you can shape the function to look like the desired periodic
    function only in the interval $(a, b)$, and the computed series will
    automatically become the series of the periodic version of $f(x)$.

    This property is illustrated in the examples section below.

    Parameters
    ==========

    limits : (sym, start, end), optional
        *sym* denotes the symbol the series is computed with respect to.

        *start* and *end* denotes the start and the end of the interval
        where the fourier series converges to the given function.

        Default range is specified as $-\pi$ and $\pi$.

    Returns
    =======

    FourierSeries
        A symbolic object representing the Fourier trigonometric series.

    Examples
    ========

    Computing the Fourier series of $f(x) = x^2$:

    >>> from sympy import fourier_series, pi
    >>> from sympy.abc import x
    >>> f = x**2
    >>> s = fourier_series(f, (x, -pi, pi))
    >>> s1 = s.truncate(n=3)
    >>> s1
    -4*cos(x) + cos(2*x) + pi**2/3

    Shifting of the Fourier series:

    >>> s.shift(1).truncate()
    -4*cos(x) + cos(2*x) + 1 + pi**2/3
    >>> s.shiftx(1).truncate()
    -4*cos(x + 1) + cos(2*x + 2) + pi**2/3

    Scaling of the Fourier series:

    >>> s.scale(2).truncate()
    -8*cos(x) + 2*cos(2*x) + 2*pi**2/3
    >>> s.scalex(2).truncate()
    -4*cos(2*x) + cos(4*x) + pi**2/3

    Computing the Fourier series of $f(x) = x$:

    This illustrates how truncating to the higher order gives better
    convergence.

    .. plot::
        :context: reset
        :format: doctest
        :include-source: True

        >>> from sympy import fourier_series, pi, plot
        >>> from sympy.abc import x
        >>> f = x
        >>> s = fourier_series(f, (x, -pi, pi))
        >>> s1 = s.truncate(n = 3)
        >>> s2 = s.truncate(n = 5)
        >>> s3 = s.truncate(n = 7)
        >>> p = plot(f, s1, s2, s3, (x, -pi, pi), show=False, legend=True)

        >>> p[0].line_color = (0, 0, 0)
        >>> p[0].label = 'x'
        >>> p[1].line_color = (0.7, 0.7, 0.7)
        >>> p[1].label = 'n=3'
        >>> p[2].line_color = (0.5, 0.5, 0.5)
        >>> p[2].label = 'n=5'
        >>> p[3].line_color = (0.3, 0.3, 0.3)
        >>> p[3].label = 'n=7'

        >>> p.show()

    This illustrates how the series converges to different sawtooth
    waves if the different ranges are specified.

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> s1 = fourier_series(x, (x, -1, 1)).truncate(10)
        >>> s2 = fourier_series(x, (x, -pi, pi)).truncate(10)
        >>> s3 = fourier_series(x, (x, 0, 1)).truncate(10)
        >>> p = plot(x, s1, s2, s3, (x, -5, 5), show=False, legend=True)

        >>> p[0].line_color = (0, 0, 0)
        >>> p[0].label = 'x'
        >>> p[1].line_color = (0.7, 0.7, 0.7)
        >>> p[1].label = '[-1, 1]'
        >>> p[2].line_color = (0.5, 0.5, 0.5)
        >>> p[2].label = '[-pi, pi]'
        >>> p[3].line_color = (0.3, 0.3, 0.3)
        >>> p[3].label = '[0, 1]'

        >>> p.show()

    Notes
    =====

    Computing Fourier series can be slow
    due to the integration required in computing
    an, bn.

    It is faster to compute Fourier series of a function
    by using shifting and scaling on an already
    computed Fourier series rather than computing
    again.

    e.g. If the Fourier series of ``x**2`` is known
    the Fourier series of ``x**2 - 1`` can be found by shifting by ``-1``.

    See Also
    ========

    sympy.series.fourier.FourierSeries

    References
    ==========

    .. [1] https://mathworld.wolfram.com/FourierSeries.html
    r   r   r   r   )r   r;   r-   r   r`   r   r
   is_zeror   r%   r   r   rb   r	   r   r)   )r[   r   r   r   r    	is_finiteres_fr   centerneg_fr#   rt   rv   s                r$   r   r   g  s   H 	

AQ''Fq	A  9q	F1I%&&*'1a00	5 	9&q&%888c

AQi&)#q(F~ 
:q1": 	:$Q22FBA2w''B FRRL9995&[ 	:BA2w''B FA..B FRRL999Q**FB	FA	&	&BFRRL111r&   )NT)%r   sympy.core.numbersr   r   sympy.core.symbolr   sympy.core.exprr   sympy.core.addr   sympy.core.containersr   sympy.core.singletonr	   r
   r   sympy.core.sympifyr   (sympy.functions.elementary.trigonometricr   r   r   sympy.series.series_classr   sympy.series.sequencesr   sympy.sets.setsr   sympy.utilities.iterablesr   r%   r)   r;   r`   rb   r   r   r   r&   r$   <module>r      s     ' ' ' ' ' ' ' ' " " " " " "                   ' ' ' ' ' ' " " " " " " + + + + + + + + & & & & & & C C C C C C C C C C 0 0 0 0 0 0 - - - - - - $ $ $ $ $ $ 1 1 1 1 1 1+ + +' ' '5% 5% 5%p  <Y$ Y$ Y$ Y$ Y$J Y$ Y$ Y$x
LA LA LA LA LA- LA LA LA^A2 A2 A2 A2 A2 A2r&   