ó
¡¼™\c           @   s  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
 m Z m Z d  d l m Z m Z m Z m Z d  d l m Z d  d l m Z d e d	 „ Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z  d „  Z! d S(   iÿÿÿÿ(   t
   CoordSys3D(   t   Del(   t
   BaseScalar(   t   Vectort
   BaseVector(   t   gradientt   curlt
   divergence(   t   difft	   integratet   St   simplify(   t   sympify(   t   Dyadicc      	   C   sð  |  d k s |  t  j k r |  St | t ƒ s= t d ƒ ‚ n  t |  t  ƒ rw| d k	 rg t d ƒ ‚ n  | rý g  } x< |  j t t	 ƒ D]( } | j
 | k r† | j | j
 ƒ q† q† Wt | ƒ } i  } x$ | D] } | j | j | ƒ ƒ qË W|  j | ƒ }  n  t  j } |  j ƒ  }	 x^ |	 D]V } | | k ra| j | ƒ |	 | j | ƒ }
 | t |
 | ƒ 7} q| |	 | 7} qW| St |  t ƒ r9| d k r›| } n  t | t ƒ s¹t d ƒ ‚ n  t j } | } xj |  j j ƒ  D]Y \ } } | t | | d | ƒt | j d | d | ƒt | j d | d | ƒB7} qØW| S| d k	 rTt d ƒ ‚ n  | rèt g  ƒ } t |  ƒ }  x9 |  j t ƒ D]( } | j
 | k r‚| j | j
 ƒ q‚q‚Wi  } x$ | D] } | j | j | ƒ ƒ q»W|  j | ƒ S|  Sd S(   sK  
    Global function for 'express' functionality.

    Re-expresses a Vector, Dyadic or scalar(sympyfiable) in the given
    coordinate system.

    If 'variables' is True, then the coordinate variables (base scalars)
    of other coordinate systems present in the vector/scalar field or
    dyadic are also substituted in terms of the base scalars of the
    given system.

    Parameters
    ==========

    expr : Vector/Dyadic/scalar(sympyfiable)
        The expression to re-express in CoordSys3D 'system'

    system: CoordSys3D
        The coordinate system the expr is to be expressed in

    system2: CoordSys3D
        The other coordinate system required for re-expression
        (only for a Dyadic Expr)

    variables : boolean
        Specifies whether to substitute the coordinate variables present
        in expr, in terms of those of parameter system

    Examples
    ========

    >>> from sympy.vector import CoordSys3D
    >>> from sympy import Symbol, cos, sin
    >>> N = CoordSys3D('N')
    >>> q = Symbol('q')
    >>> B = N.orient_new_axis('B', q, N.k)
    >>> from sympy.vector import express
    >>> express(B.i, N)
    (cos(q))*N.i + (sin(q))*N.j
    >>> express(N.x, B, variables=True)
    B.x*cos(q) - B.y*sin(q)
    >>> d = N.i.outer(N.i)
    >>> express(d, B, N) == (cos(q))*(B.i|N.i) + (-sin(q))*(B.j|N.i)
    True

    i    s>   system should be a CoordSys3D                         instancesJ   system2 should not be provided for                                 VectorssC   system2 should be a CoordSys3D                             instancet	   variablesi   N(   R   t   zerot
   isinstanceR    t	   TypeErrort   Nonet
   ValueErrort   atomsR   R   t   systemt   appendt   sett   updatet
   scalar_mapt   subst   separatet   rotation_matrixt	   to_matrixt   matrix_to_vectorR   t
   componentst   itemst   expresst   argsR   t   add(   t   exprR   t   system2R   t   system_listt   xt	   subs_dictt   ft   outvect   partst   tempt   outdyadt   vart   kt   vt
   system_set(    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyR!      sl    0	 		#c         C   s3  d d l  m } | |  ƒ } t | ƒ d k rt t | ƒ ƒ } t |  | d t ƒ}  | j ƒ  \ } } } | j ƒ  \ } } }	 t	 j
 | | ƒ t |  | ƒ }
 |
 t	 j
 | | ƒ t |  | ƒ 7}
 |
 t	 j
 | | ƒ t |  |	 ƒ 7}
 |
 d k rt |  t	 ƒ rt	 j }
 n  |
 St |  t	 ƒ r%t	 j St d ƒ Sd S(   sé  
    Returns the directional derivative of a scalar or vector field computed
    along a given vector in coordinate system which parameters are expressed.

    Parameters
    ==========

    field : Vector or Scalar
        The scalar or vector field to compute the directional derivative of

    direction_vector : Vector
        The vector to calculated directional derivative along them.


    Examples
    ========

    >>> from sympy.vector import CoordSys3D, directional_derivative
    >>> R = CoordSys3D('R')
    >>> f1 = R.x*R.y*R.z
    >>> v1 = 3*R.i + 4*R.j + R.k
    >>> directional_derivative(f1, v1)
    R.x*R.y + 4*R.x*R.z + 3*R.y*R.z
    >>> f2 = 5*R.x**2*R.z
    >>> directional_derivative(f2, v1)
    5*R.x**2 + 30*R.x*R.z

    iÿÿÿÿ(   t   _get_coord_sys_from_expri    R   N(   t   sympy.vector.operatorsR2   t   lent   nextt   iterR!   t   Truet   base_vectorst   base_scalarsR   t   dotR   R   R   R
   (   t   fieldt   direction_vectorR2   t	   coord_syst   it   jR/   R'   t   yt   zt   out(    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   directional_derivative€   s     ##c         C   sQ   t  ƒ  } |  j r8 t t |  ƒ ƒ t t |  ƒ ƒ j ƒ  S| j | |  ƒ ƒ j ƒ  S(   s!  
    Return the laplacian of the given field computed in terms of
    the base scalars of the given coordinate system.

    Parameters
    ==========

    expr : SymPy Expr or Vector
        expr denotes a scalar or vector field.

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, laplacian
    >>> R = CoordSys3D('R')
    >>> f = R.x**2*R.y**5*R.z
    >>> laplacian(f)
    20*R.x**2*R.y**3*R.z + 2*R.y**5*R.z
    >>> f = R.x**2*R.i + R.y**3*R.j + R.z**4*R.k
    >>> laplacian(f)
    2*R.i + 6*R.y*R.j + 12*R.z**2*R.k

    (   R   t	   is_VectorR   R   R   t   doitR:   (   R$   t   delop(    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt	   laplacian±   s    		&c         C   sJ   t  |  t ƒ s t d ƒ ‚ n  |  t j k r1 t St |  ƒ j ƒ  t j k S(   sŸ  
    Checks if a field is conservative.

    Parameters
    ==========

    field : Vector
        The field to check for conservative property

    Examples
    ========

    >>> from sympy.vector import CoordSys3D
    >>> from sympy.vector import is_conservative
    >>> R = CoordSys3D('R')
    >>> is_conservative(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
    True
    >>> is_conservative(R.z*R.j)
    False

    s   field should be a Vector(   R   R   R   R   R7   R   R   (   R;   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   is_conservativeÐ   s
    c         C   sM   t  |  t ƒ s t d ƒ ‚ n  |  t j k r1 t St |  ƒ j ƒ  t d ƒ k S(   s—  
    Checks if a field is solenoidal.

    Parameters
    ==========

    field : Vector
        The field to check for solenoidal property

    Examples
    ========

    >>> from sympy.vector import CoordSys3D
    >>> from sympy.vector import is_solenoidal
    >>> R = CoordSys3D('R')
    >>> is_solenoidal(R.y*R.z*R.i + R.x*R.z*R.j + R.x*R.y*R.k)
    True
    >>> is_solenoidal(R.y * R.j)
    False

    s   field should be a Vectori    (   R   R   R   R   R7   R   R   R
   (   R;   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   is_solenoidalñ   s
    c         C   s	  t  |  ƒ s t d ƒ ‚ n  |  t j k r4 t d ƒ St | t ƒ sR t d ƒ ‚ n  t |  | d t	 ƒ}  | j
 ƒ  } | j ƒ  } t |  j | d ƒ | d ƒ } xc t | d ƒ D]Q \ } } t | | | d ƒ } |  j | ƒ | } | t | | | d ƒ 7} q° W| S(   sÃ  
    Returns the scalar potential function of a field in a given
    coordinate system (without the added integration constant).

    Parameters
    ==========

    field : Vector
        The vector field whose scalar potential function is to be
        calculated

    coord_sys : CoordSys3D
        The coordinate system to do the calculation in

    Examples
    ========

    >>> from sympy.vector import CoordSys3D
    >>> from sympy.vector import scalar_potential, gradient
    >>> R = CoordSys3D('R')
    >>> scalar_potential(R.k, R) == R.z
    True
    >>> scalar_field = 2*R.x**2*R.y*R.z
    >>> grad_field = gradient(scalar_field)
    >>> scalar_potential(grad_field, R)
    2*R.x**2*R.y*R.z

    s   Field is not conservativei    s   coord_sys must be a CoordSys3DR   i   (   RH   R   R   R   R
   R   R    R   R!   R7   R8   R9   R	   R:   t	   enumerateR   (   R;   R=   t
   dimensionst   scalarst   temp_functionR>   t   dimt   partial_diff(    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   scalar_potential  s    
 c         C   s  t  | t ƒ s t d ƒ ‚ n  t  |  t ƒ r? t |  | ƒ } n |  } | j } t | j | ƒ | d t ƒ} t | j | ƒ | d t ƒ} i  } i  }	 | j	 ƒ  }
 xN t
 | j ƒ  ƒ D]: \ } } | j | ƒ | |
 | <| j | ƒ |	 |
 | <qµ W| j |	 ƒ | j | ƒ S(   s0  
    Returns the scalar potential difference between two points in a
    certain coordinate system, wrt a given field.

    If a scalar field is provided, its values at the two points are
    considered. If a conservative vector field is provided, the values
    of its scalar potential function at the two points are used.

    Returns (potential at point2) - (potential at point1)

    The position vectors of the two Points are calculated wrt the
    origin of the coordinate system provided.

    Parameters
    ==========

    field : Vector/Expr
        The field to calculate wrt

    coord_sys : CoordSys3D
        The coordinate system to do the calculations in

    point1 : Point
        The initial Point in given coordinate system

    position2 : Point
        The second Point in the given coordinate system

    Examples
    ========

    >>> from sympy.vector import CoordSys3D, Point
    >>> from sympy.vector import scalar_potential_difference
    >>> R = CoordSys3D('R')
    >>> P = R.origin.locate_new('P', R.x*R.i + R.y*R.j + R.z*R.k)
    >>> vectfield = 4*R.x*R.y*R.i + 2*R.x**2*R.j
    >>> scalar_potential_difference(vectfield, R, R.origin, P)
    2*R.x**2*R.y
    >>> Q = R.origin.locate_new('O', 3*R.i + R.j + 2*R.k)
    >>> scalar_potential_difference(vectfield, R, P, Q)
    -2*R.x**2*R.y + 18

    s   coord_sys must be a CoordSys3DR   (   R   R    R   R   RP   t   originR!   t   position_wrtR7   R9   RJ   R8   R:   R   (   R;   R=   t   point1t   point2t	   scalar_fnRQ   t	   position1t	   position2t
   subs_dict1t
   subs_dict2RL   R>   R'   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   scalar_potential_differenceE  s"    -			c         C   sH   t  j } | j ƒ  } x, t |  ƒ D] \ } } | | | | 7} q" W| S(   sà  
    Converts a vector in matrix form to a Vector instance.

    It is assumed that the elements of the Matrix represent the
    measure numbers of the components of the vector along basis
    vectors of 'system'.

    Parameters
    ==========

    matrix : SymPy Matrix, Dimensions: (3, 1)
        The matrix to be converted to a vector

    system : CoordSys3D
        The coordinate system the vector is to be defined in

    Examples
    ========

    >>> from sympy import ImmutableMatrix as Matrix
    >>> m = Matrix([1, 2, 3])
    >>> from sympy.vector import CoordSys3D, matrix_to_vector
    >>> C = CoordSys3D('C')
    >>> v = matrix_to_vector(m, C)
    >>> v
    C.i + 2*C.j + 3*C.k
    >>> v.to_matrix(C) == m
    True

    (   R   R   R8   RJ   (   t   matrixR   R*   t   vectsR>   R'   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyR   Š  s
     	c         C   s  |  j  | j  k r9 t d t |  ƒ d t | ƒ ƒ ‚ n  g  } | } x) | j d k	 rp | j | ƒ | j } qH W| j | ƒ t | ƒ } g  } |  } x& | | k r¾ | j | ƒ | j } q™ Wt | ƒ } | j | ƒ } x+ | d k r| j | | ƒ | d 8} qÝ W| | f S(   s¿   
    Calculates the 'path' of objects starting from 'from_object'
    to 'to_object', along with the index of the first common
    ancestor in the tree.

    Returns (index, list) tuple.
    s!   No connecting path found between s    and i    i   N(	   t   _rootR   t   strt   _parentR   R   R   R4   t   index(   t   from_objectt	   to_objectt
   other_patht   objt
   object_sett	   from_pathR`   R>   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   _path±  s*    	$c          O   sì   | j  d t ƒ } t d „  |  Dƒ ƒ s7 t d ƒ ‚ n  g  } x€ t |  ƒ D]r \ } } x/ t | ƒ D]! } | | | j |  | ƒ 8} qc Wt | ƒ j t	 j
 ƒ r¯ t d ƒ ‚ n  | j | ƒ qJ W| rè g  | D] } | j ƒ  ^ qÍ } n  | S(   sŠ  
    Takes a sequence of independent vectors and orthogonalizes them
    using the Gram - Schmidt process. Returns a list of
    orthogonal or orthonormal vectors.

    Parameters
    ==========

    vlist : sequence of independent vectors to be made orthogonal.

    orthonormal : Optional parameter
                  Set to True if the vectors returned should be
                  orthonormal.
                  Default: False

    Examples
    ========

    >>> from sympy.vector.coordsysrect import CoordSys3D
    >>> from sympy.vector.vector import Vector, BaseVector
    >>> from sympy.vector.functions import orthogonalize
    >>> C = CoordSys3D('C')
    >>> i, j, k = C.base_vectors()
    >>> v1 = i + 2*j
    >>> v2 = 2*i + 3*j
    >>> orthogonalize(v1, v2)
    [C.i + 2*C.j, 2/5*C.i + (-1/5)*C.j]

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Gram-Schmidt_process

    t   orthonormalc         s   s   |  ] } t  | t ƒ Vq d  S(   N(   R   R   (   t   .0t   vec(    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pys	   <genexpr>÷  s    s#   Each element must be of Type Vectors#   Vector set not linearly independent(   t   gett   Falset   allR   RJ   t   ranget
   projectionR   t   equalsR   R   R   R   t	   normalize(   t   vlistt   kwargsRh   t   ortho_vlistR>   t   termR?   Rj   (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   orthogonalizeÒ  s    #"N("   t   sympy.vector.coordsysrectR    t   sympy.vector.deloperatorR   t   sympy.vector.scalarR   t   sympy.vector.vectorR   R   R3   R   R   R   t   sympyR   R	   R
   R   t
   sympy.coreR   t   sympy.vector.dyadicR   R   Rl   R!   RC   RG   RH   RI   RP   RZ   R   Rg   Rv   (    (    (    s5   lib/python2.7/site-packages/sympy/vector/functions.pyt   <module>   s"   "u	1		!	!	3	E	'	!