ó
~9­\c        
   @   sj  d  Z  d d l m Z d d d d d d d	 d
 d d g
 Z d d l m Z m Z m Z m Z m	 Z	 m
 Z
 m Z m Z m Z m Z m Z m Z m Z m Z d d l m 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 d „ Z d „  Z  d d d „ Z! d „  Z" d „  Z# d „  Z$ d d d d „ Z% d d d d „ Z& d „  Z' d „  Z( d S(   sÎ   
**Contains**

* refraction_angle
* fresnel_coefficients
* deviation
* brewster_angle
* critical_angle
* lens_makers_formula
* mirror_formula
* lens_formula
* hyperfocal_distance
* transverse_magnification
iÿÿÿÿ(   t   divisiont   refraction_anglet	   deviationt   fresnel_coefficientst   brewster_anglet   critical_anglet   lens_makers_formulat   mirror_formulat   lens_formulat   hyperfocal_distancet   transverse_magnification(   t   Symbolt   sympifyt   sqrtt   Matrixt   acost   oot   Limitt   atan2t   asint   cost   sint   tant   It   cancel(   t   is_sequence(   t   Ray3Dt   Point3D(   t   intersection(   t   Planei   (   t   Mediumc         C   sï  t  } | d
 k	 r- | d
 k	 r- t d ƒ ‚ n  t |  t ƒ s‡ t |  ƒ rW t |  ƒ } q t |  t ƒ rx t |  j ƒ } q t d ƒ ‚ n |  } | d
 k	 rô t | t	 ƒ s· t d ƒ ‚ n  t |  t ƒ râ t
 } | j |  ƒ d } n  t | j ƒ } n² t | t ƒ s t | ƒ rt | ƒ } q¦t | t ƒ r‘t | j ƒ } t |  t ƒ rt |  | ƒ } t | ƒ d k r{t d ƒ ‚ qŽt
 } | d } qq¦t d ƒ ‚ n | } d \ }	 }
 t | t ƒ rÍ| j }	 n t | ƒ }	 t | t ƒ rô| j }
 n t | ƒ }
 |	 |
 } t t g  | D] } | d ^ qƒ ƒ } t t g  | D] } | d ^ q@ƒ ƒ } | | } | | } | j | ƒ } d | d d | d } | j r§d S| | | | t | ƒ | } | | } | sÛ| St | d	 | ƒSd
 S(   s1  
    This function calculates transmitted vector after refraction at planar
    surface. `medium1` and `medium2` can be `Medium` or any sympifiable object.

    If `incident` is an object of `Ray3D`, `normal` also has to be an instance
    of `Ray3D` in order to get the output as a `Ray3D`. Please note that if
    plane of separation is not provided and normal is an instance of `Ray3D`,
    normal will be assumed to be intersecting incident ray at the plane of
    separation. This will not be the case when `normal` is a `Matrix` or
    any other sequence.
    If `incident` is an instance of `Ray3D` and `plane` has not been provided
    and `normal` is not `Ray3D`, output will be a `Matrix`.

    Parameters
    ==========

    incident : Matrix, Ray3D, or sequence
        Incident vector
    medium1 : sympy.physics.optics.medium.Medium or sympifiable
        Medium 1 or its refractive index
    medium2 : sympy.physics.optics.medium.Medium or sympifiable
        Medium 2 or its refractive index
    normal : Matrix, Ray3D, or sequence
        Normal vector
    plane : Plane
        Plane of separation of the two media.

    Examples
    ========

    >>> from sympy.physics.optics import refraction_angle
    >>> from sympy.geometry import Point3D, Ray3D, Plane
    >>> from sympy.matrices import Matrix
    >>> from sympy import symbols
    >>> n = Matrix([0, 0, 1])
    >>> P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
    >>> r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
    >>> refraction_angle(r1, 1, 1, n)
    Matrix([
    [ 1],
    [ 1],
    [-1]])
    >>> refraction_angle(r1, 1, 1, plane=P)
    Ray3D(Point3D(0, 0, 0), Point3D(1, 1, -1))

    With different index of refraction of the two media

    >>> n1, n2 = symbols('n1, n2')
    >>> refraction_angle(r1, n1, n2, n)
    Matrix([
    [                                n1/n2],
    [                                n1/n2],
    [-sqrt(3)*sqrt(-2*n1**2/(3*n2**2) + 1)]])
    >>> refraction_angle(r1, n1, n2, plane=P)
    Ray3D(Point3D(0, 0, 0), Point3D(n1/n2, n1/n2, -sqrt(3)*sqrt(-2*n1**2/(3*n2**2) + 1)))

    s%   Either plane or normal is acceptable.s/   incident should be a Matrix, Ray3D, or sequences3   plane should be an instance of geometry.plane.Planei    s.   Normal isn't concurrent with the incident ray.s-   Normal should be a Matrix, Ray3D, or sequencei   i   t   direction_ratioN(   NN(   t   Falset   Nonet
   ValueErrort
   isinstanceR   R   R   R   t	   TypeErrorR   t   TrueR   t   normal_vectort   lenR   t   refractive_indexR   R   t   sumt   dott   is_negative(   t   incidentt   medium1t   medium2t   normalt   planet
   return_rayt	   _incidentt   intersection_ptt   _normalt   n1t   n2t   etat   it   mag_incidentt
   mag_normalt   c1t   cs2t   drs(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR   '   sl    ;
))

	 
c         C   s8  t  | t ƒ r | j } n t | ƒ } t  | t ƒ rB | j } n t | ƒ } t | t |  ƒ | ƒ } y t | | ƒ } Wn t k
 r” d } n X| d k s­ | |  k rXt |  | ƒ t |  | ƒ } t	 |  | ƒ t	 |  | ƒ } d t | ƒ t
 |  ƒ t |  | ƒ }	 d t | ƒ t
 |  ƒ t |  | ƒ t
 |  | ƒ }
 | | |
 |	 g S| | } t t
 |  ƒ t t t |  ƒ d | d ƒ t
 |  ƒ t t t |  ƒ d | d ƒ ƒ } t | d t
 |  ƒ t t t |  ƒ d | d ƒ | d t
 |  ƒ t t t |  ƒ d | d ƒ ƒ } | | g Sd S(   sB  
    This function uses Fresnel equations to calculate reflection and
    transmission coefficients. Those are obtained for both polarisations
    when the electric field vector is in the plane of incidence (labelled 'p')
    and when the electric field vector is perpendicular to the plane of
    incidence (labelled 's'). There are four real coefficients unless the
    incident ray reflects in total internal in which case there are two complex
    ones. Angle of incidence is the angle between the incident ray and the
    surface normal. ``medium1`` and ``medium2`` can be ``Medium`` or any
    sympifiable object.

    Parameters
    ==========

    angle_of_incidence : sympifiable

    medium1 : Medium or sympifiable
        Medium 1 or its refractive index

    medium2 : Medium or sympifiable
        Medium 2 or its refractive index

    Returns a list with four real Fresnel coefficients:
    [reflection p (TM), reflection s (TE),
    transmission p (TM), transmission s (TE)]
    If the ray is undergoes total internal reflection then returns a
    list of two complex Fresnel coefficients:
    [reflection p (TM), reflection s (TE)]

    Examples
    ========

    >>> from sympy.physics.optics import fresnel_coefficients
    >>> fresnel_coefficients(0.3, 1, 2)
    [0.317843553417859, -0.348645229818821,
            0.658921776708929, 0.651354770181179]
    >>> fresnel_coefficients(0.6, 2, 1)
    [-0.235625382192159 - 0.971843958291041*I,
             0.816477005968898 - 0.577377951366403*I]

    References
    ==========

    https://en.wikipedia.org/wiki/Fresnel_equations
    i   N(   R#   R   R(   R   R   R   R   R"   R!   R   R   R   R   R   (   t   angle_of_incidenceR-   R.   R5   R6   t   angle_of_refractiont(   angle_of_total_internal_reflection_onsett   R_st   R_pt   T_st   T_pt   n(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR   ³   s>    /
($
 	' 'c         C   sø  t  |  | | d | d | ƒ} | d k rôt | t ƒ rK t | j ƒ } n  t |  t ƒ s¥ t |  ƒ ru t |  ƒ } q« t |  t ƒ r– t |  j ƒ } q« t d ƒ ‚ n |  } | d k rt | t ƒ st | ƒ rá t | ƒ } qt | t ƒ rt | j ƒ } qt d ƒ ‚ q)| } n t | j ƒ } t	 t
 g  | D] } | d ^ q6ƒ ƒ }	 t	 t
 g  | D] } | d ^ q_ƒ ƒ }
 t	 t
 g  | D] } | d ^ qˆƒ ƒ } | |	 } | |
 } | | } t | j | ƒ ƒ } t | j | ƒ ƒ } | | Sd S(   s1  
    This function calculates the angle of deviation of a ray
    due to refraction at planar surface.

    Parameters
    ==========

    incident : Matrix, Ray3D, or sequence
        Incident vector
    medium1 : sympy.physics.optics.medium.Medium or sympifiable
        Medium 1 or its refractive index
    medium2 : sympy.physics.optics.medium.Medium or sympifiable
        Medium 2 or its refractive index
    normal : Matrix, Ray3D, or sequence
        Normal vector
    plane : Plane
        Plane of separation of the two media.

    Examples
    ========

    >>> from sympy.physics.optics import deviation
    >>> from sympy.geometry import Point3D, Ray3D, Plane
    >>> from sympy.matrices import Matrix
    >>> from sympy import symbols
    >>> n1, n2 = symbols('n1, n2')
    >>> n = Matrix([0, 0, 1])
    >>> P = Plane(Point3D(0, 0, 0), normal_vector=[0, 0, 1])
    >>> r1 = Ray3D(Point3D(-1, -1, 1), Point3D(0, 0, 0))
    >>> deviation(r1, 1, 1, n)
    0
    >>> deviation(r1, n1, n2, plane=P)
    -acos(-sqrt(-2*n1**2/(3*n2**2) + 1)) + acos(-sqrt(3)/3)

    R/   R0   i    s/   incident should be a Matrix, Ray3D, or sequences-   normal should be a Matrix, Ray3D, or sequencei   N(   R   R#   R   R   R   R   R$   R!   R&   R   R)   R   R*   (   R,   R-   R.   R/   R0   t	   refractedR2   R4   R8   R9   R:   t   mag_refractedt   r(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR     sF    $		)))


c         C   sg   d \ } } t |  t ƒ r' |  j } n t |  ƒ } t | t ƒ rN | j } n t | ƒ } t | | ƒ S(   s­  
    This function calculates the Brewster's angle of incidence to Medium 2 from
    Medium 1 in radians.

    Parameters
    ==========

    medium 1 : Medium or sympifiable
        Refractive index of Medium 1
    medium 2 : Medium or sympifiable
        Refractive index of Medium 1

    Examples
    ========

    >>> from sympy.physics.optics import brewster_angle
    >>> brewster_angle(1, 1.33)
    0.926093295503462

    N(   NN(   R!   R#   R   R(   R   R   (   R-   R.   R5   R6   (    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR   \  s    c         C   s‡   d \ } } t |  t ƒ r' |  j } n t |  ƒ } t | t ƒ rN | j } n t | ƒ } | | k ru t d ƒ ‚ n t | | ƒ Sd S(   sÑ  
    This function calculates the critical angle of incidence (marking the onset
    of total internal) to Medium 2 from Medium 1 in radians.

    Parameters
    ==========

    medium 1 : Medium or sympifiable
        Refractive index of Medium 1
    medium 2 : Medium or sympifiable
        Refractive index of Medium 1

    Examples
    ========

    >>> from sympy.physics.optics import critical_angle
    >>> critical_angle(1.33, 1)
    0.850908514477849

    s0   Total internal reflection impossible for n1 < n2N(   NN(   R!   R#   R   R(   R   R"   R   (   R-   R.   R5   R6   (    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR     s    c         C   s†   t  |  t ƒ r |  j }  n t |  ƒ }  t  | t ƒ rB | j } n t | ƒ } t | ƒ } t | ƒ } d |  | | d | d | S(   sD  
    This function calculates focal length of a thin lens.
    It follows cartesian sign convention.

    Parameters
    ==========

    n_lens : Medium or sympifiable
        Index of refraction of lens.
    n_surr : Medium or sympifiable
        Index of reflection of surrounding.
    r1 : sympifiable
        Radius of curvature of first surface.
    r2 : sympifiable
        Radius of curvature of second surface.

    Examples
    ========

    >>> from sympy.physics.optics import lens_makers_formula
    >>> lens_makers_formula(1.33, 1, 10, -10)
    15.1515151515151

    i   (   R#   R   R(   R   (   t   n_lenst   n_surrt   r1t   r2(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR   §  s    c         C   sÔ  |  r! | r! | r! t  d ƒ ‚ n  t |  ƒ }  t | ƒ } t | ƒ } | t k r` t d ƒ } n  | t k r{ t d ƒ } n  |  t k r– t d ƒ } n  |  d k rT| t k rè | t k rè t t | | | | | t ƒ | t ƒ j ƒ  S| t k rt | | | | | t ƒ j ƒ  S| t k rDt | | | | | t ƒ j ƒ  S| | | | S| d k r| t k r¦|  t k r¦t t | | | | | t ƒ | t ƒ j ƒ  S| t k rÔt | |  | |  | t ƒ j ƒ  S|  t k rt | | | | | t ƒ j ƒ  S| |  | |  S| d k rÐ| t k rd|  t k rdt t | | | | | t ƒ | t ƒ j ƒ  S| t k r’t | |  | |  | t ƒ j ƒ  S|  t k rÀt | | | | | t ƒ j ƒ  S| |  | |  Sd S(   sÍ  
    This function provides one of the three parameters
    when two of them are supplied.
    This is valid only for paraxial rays.

    Parameters
    ==========

    focal_length : sympifiable
        Focal length of the mirror.
    u : sympifiable
        Distance of object from the pole on
        the principal axis.
    v : sympifiable
        Distance of the image from the pole
        on the principal axis.

    Examples
    ========

    >>> from sympy.physics.optics import mirror_formula
    >>> from sympy.abc import f, u, v
    >>> mirror_formula(focal_length=f, u=u)
    f*u/(-f + u)
    >>> mirror_formula(focal_length=f, v=v)
    f*v/(-f + v)
    >>> mirror_formula(u=u, v=v)
    u*v/(u + v)

    s"   Please provide only two parameterst   ut   vt   fN(   R"   R   R   R   R!   R   t   doit(   t   focal_lengthRM   RN   t   _ut   _vt   _f(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR   Ï  sF    .""."".""c         C   sÔ  |  r! | r! | r! t  d ƒ ‚ n  t |  ƒ }  t | ƒ } t | ƒ } | t k r` t d ƒ } n  | t k r{ t d ƒ } n  |  t k r– t d ƒ } n  |  d k rT| t k rè | t k rè t t | | | | | t ƒ | t ƒ j ƒ  S| t k rt | | | | | t ƒ j ƒ  S| t k rDt | | | | | t ƒ j ƒ  S| | | | S| d k r| t k r¦|  t k r¦t t | | | | | t ƒ | t ƒ j ƒ  S| t k rÔt | |  |  | | t ƒ j ƒ  S|  t k rt | | | | | t ƒ j ƒ  S| |  |  | S| d k rÐ| t k rd|  t k rdt t | | | | | t ƒ | t ƒ j ƒ  S| t k r’t | |  | |  | t ƒ j ƒ  S|  t k rÀt | | | | | t ƒ j ƒ  S| |  | |  Sd S(   s×  
    This function provides one of the three parameters
    when two of them are supplied.
    This is valid only for paraxial rays.

    Parameters
    ==========

    focal_length : sympifiable
        Focal length of the mirror.
    u : sympifiable
        Distance of object from the optical center on
        the principal axis.
    v : sympifiable
        Distance of the image from the optical center
        on the principal axis.

    Examples
    ========

    >>> from sympy.physics.optics import lens_formula
    >>> from sympy.abc import f, u, v
    >>> lens_formula(focal_length=f, u=u)
    f*u/(f + u)
    >>> lens_formula(focal_length=f, v=v)
    f*v/(f - v)
    >>> lens_formula(u=u, v=v)
    u*v/(u - v)

    s"   Please provide only two parametersRM   RN   RO   N(   R"   R   R   R   R!   R   RP   (   RQ   RM   RN   RR   RS   RT   (    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR     sF    .""."".""c         C   s8   t  |  ƒ }  t  | ƒ } t  | ƒ } d | | |  d S(   sš  

    Parameters
    ==========
    f: sympifiable
    Focal length of a given lens

    N: sympifiable
    F-number of a given lens

    c: sympifiable
    Circle of Confusion (CoC) of a given image format

    Example
    =======
    >>> from sympy.physics.optics import hyperfocal_distance
    >>> from sympy.abc import f, N, c
    >>> round(hyperfocal_distance(f = 0.5, N = 8, c = 0.0033), 2)
    9.47
    i   i   (   R   (   RO   t   Nt   c(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR	   X  s    c         C   s!   t  |  ƒ }  t  | ƒ } |  | S(   su  

    Calculates the transverse magnification, which is the ratio of the
    image size to the object size.

    Parameters
    ==========
    so: sympifiable
    Lens-object distance

    si: sympifiable
    Lens-image distance

    Example
    =======
    >>> from sympy.physics.optics import transverse_magnification
    >>> transverse_magnification(30, 15)
    -2

    (   R   (   t   sit   so(    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyR
   t  s    N()   t   __doc__t
   __future__R    t   __all__t   sympyR   R   R   R   R   R   R   R   R   R   R   R   R   R   t   sympy.core.compatibilityR   t   sympy.geometry.lineR   R   t   sympy.geometry.utilR   t   sympy.geometry.planeR   t   mediumR   R!   R   R   R   R   R   R   R   R   R	   R
   (    (    (    s9   lib/python2.7/site-packages/sympy/physics/optics/utils.pyt   <module>   s6   	^Œ	XQ	#	(	(ED	