B
    c‹dl  ã               @   sn   d 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dlmZ dd
d„ZdS )zX
This module implements a method to find
Euler-Lagrange Equations for given Lagrangian.
é    )Úcombinations_with_replacement)Ú
DerivativeÚFunctionÚdiff)ÚEq)ÚS)ÚSymbol)Úsympify)Úiterable© c       
   
      s„  t ˆ ƒrtˆ ƒnˆ f‰ ˆ s*t|  t¡ƒ‰ n$x"ˆ D ]}t|tƒs0td| ƒ‚q0W t |ƒr^t|ƒn|f}|stˆ d j}ntdd„ |D ƒƒ}tdd„ |D ƒƒs¤td| ƒ‚x&ˆ D ]}||jksªtd||f ƒ‚qªW t	‡ fdd	„|  t
¡D ƒdg ƒ}g }xŒˆ D ]„}t| |ƒ}xVtd
|d
 ƒD ]D}x<t||ƒD ].}|tj| t| t|f|žŽ f|žŽ   }q&W qW t|dƒ}	t|	tƒrø| |	¡ qøW |S )a5  
    Find the Euler-Lagrange equations [1]_ for a given Lagrangian.

    Parameters
    ==========

    L : Expr
        The Lagrangian that should be a function of the functions listed
        in the second argument and their derivatives.

        For example, in the case of two functions $f(x,y)$, $g(x,y)$ and
        two independent variables $x$, $y$ the Lagrangian has the form:

            .. math:: L\left(f(x,y),g(x,y),\frac{\partial f(x,y)}{\partial x},
                      \frac{\partial f(x,y)}{\partial y},
                      \frac{\partial g(x,y)}{\partial x},
                      \frac{\partial g(x,y)}{\partial y},x,y\right)

        In many cases it is not necessary to provide anything, except the
        Lagrangian, it will be auto-detected (and an error raised if this
        cannot be done).

    funcs : Function or an iterable of Functions
        The functions that the Lagrangian depends on. The Euler equations
        are differential equations for each of these functions.

    vars : Symbol or an iterable of Symbols
        The Symbols that are the independent variables of the functions.

    Returns
    =======

    eqns : list of Eq
        The list of differential equations, one for each function.

    Examples
    ========

    >>> from sympy import euler_equations, Symbol, Function
    >>> x = Function('x')
    >>> t = Symbol('t')
    >>> L = (x(t).diff(t))**2/2 - x(t)**2/2
    >>> euler_equations(L, x(t), t)
    [Eq(-x(t) - Derivative(x(t), (t, 2)), 0)]
    >>> u = Function('u')
    >>> x = Symbol('x')
    >>> L = (u(t, x).diff(t))**2/2 - (u(t, x).diff(x))**2/2
    >>> euler_equations(L, u(t, x), [t, x])
    [Eq(-Derivative(u(t, x), (t, 2)) + Derivative(u(t, x), (x, 2)), 0)]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Euler%E2%80%93Lagrange_equation

    zFunction expected, got: %sr   c             s   s   | ]}t |ƒV  qd S )N)r	   )Ú.0Úvarr   r   úa/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/sympy/calculus/euler.pyú	<genexpr>V   s    z"euler_equations.<locals>.<genexpr>c             s   s   | ]}t |tƒV  qd S )N)Ú
isinstancer   )r   Úvr   r   r   r   X   s    z!Variables are not symbols, got %sz"Variables %s do not match args: %sc                s    g | ]}|j ˆ krt|jƒ‘qS r   )ÚexprÚlenÚ	variables)r   Úd)Úfuncsr   r   ú
<listcomp>_   s    z#euler_equations.<locals>.<listcomp>é   )r
   ÚtupleZatomsr   r   Ú	TypeErrorÚargsÚallÚ
ValueErrorÚmaxr   r   Úranger   r   ZNegativeOner   Úappend)
ÚLr   ÚvarsÚfÚorderZeqnsÚeqÚiÚpZnew_eqr   )r   r   Úeuler_equations   s6    :






4

r(   N)r   r   )Ú__doc__Ú	itertoolsr   Zsympy.core.functionr   r   r   Zsympy.core.relationalr   Zsympy.core.singletonr   Zsympy.core.symbolr   Zsympy.core.sympifyr	   Zsympy.utilities.iterablesr
   r(   r   r   r   r   Ú<module>   s   