
    wgV                     (   d Z g dZddlmZmZ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mZmZ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l!m"Z" ddl#m$Z$ ddl%m&Z& d Z'ddZ(d Z)ddZ*d Z+d Z,ddZ-d dZ.d dZ/d Z0d Z1y)!z
**Contains**

* refraction_angle
* fresnel_coefficients
* deviation
* brewster_angle
* critical_angle
* lens_makers_formula
* mirror_formula
* lens_formula
* hyperfocal_distance
* transverse_magnification
)
refraction_angle	deviationfresnel_coefficientsbrewster_anglecritical_anglelens_makers_formulamirror_formulalens_formulahyperfocal_distancetransverse_magnification    )FloatIoopizoo)S)Symbolsympify)sqrt)acosasinatan2cossintan)Matrix)cancel)Limit)Ray3D)intersection)Plane)is_sequence   )Mediumc                 X    t        | t              r| j                  }|S t        |       }|S )zG
    Helper function that returns refractive index, given a medium
    )
isinstancer%   refractive_indexr   )mediumns     _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/physics/optics/utils.pyrefractive_index_of_mediumr,   ,   s0     &&!## H FOH    Nc                    t        |      }t        |      }	 t        |       }	 t        ||      }|d||t	        d      d|cxk  rt
        dz  k  st	        d       t	        d      |r||kD  rt	        d      t        |t        |      z  |z        S d}	||t	        d      t        | t              sHt        |       rt        |       }
n3t        | t              rt        | j                        }
nt        d	      | }
|Wt        |t              st        d
      t        | t              rd}	|j                  |       d   }t        |j                        }nt        |t              st        |      rt        |      }not        |t              rRt        |j                        }t        | t              r:t        | |      }t!        |      dk(  rt	        d      d}	|d   }nt        d      |}||z  }t#        t%        d |
D                    }t#        t%        d |D                    }|
|z  }
||z  }|
j'                  |       }d|dz  d|dz  z
  z  z
  }|j(                  rt*        j,                  S ||
z  ||z  t#        |      z
  |z  z   }||z  }|	s|S t        |      S # t        $ r d}Y w xY w# t        t        f$ r d}Y w xY w)a	  
    This function calculates transmitted vector after refraction at planar
    surface. ``medium1`` and ``medium2`` can be ``Medium`` or any sympifiable object.
    If ``incident`` is a number then treated as angle of incidence (in radians)
    in which case refraction angle is returned.

    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, sequence or a number
        Incident vector or angle of incidence
    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.

    Returns
    =======

    Returns an angle of refraction or a refracted ray depending on inputs.

    Examples
    ========

    >>> from sympy.physics.optics import refraction_angle
    >>> from sympy.geometry import Point3D, Ray3D, Plane
    >>> from sympy.matrices import Matrix
    >>> from sympy import symbols, pi
    >>> 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)))
    >>> round(refraction_angle(pi/6, 1.2, 1.5), 5)
    0.41152
    Nz0Normal/plane not allowed if incident is an angleg        g      ?(Angle of incidence not in range [0:pi/2)z'Ray undergoes total internal reflectionFz%Either plane or normal is acceptable./incident should be a Matrix, Ray3D, or sequencez3plane should be an instance of geometry.plane.PlaneTr   z.Normal isn't concurrent with the incident ray.z-Normal should be a Matrix, Ray3D, or sequencec              3   &   K   | ]	  }|d z    yw   N .0is     r+   	<genexpr>z#refraction_angle.<locals>.<genexpr>   s     4QAqD4   c              3   &   K   | ]	  }|d z    ywr2   r4   r5   s     r+   r8   z#refraction_angle.<locals>.<genexpr>   s     01!Q$0r9   r$   r3   )direction_ratio)r,   float	TypeErrorr   
ValueErrorr   r   r   r'   r   r#   r    r;   r"   r!   normal_vectorlenr   sumdotis_negativer   Zero)incidentmedium1medium2normalplanen1n2angle_of_incidencecritical_angle_
return_ray	_incidentintersection_pt_normaletamag_incident
mag_normalc1cs2drss                      r+   r   r   7   s   F 
$G	,B	#G	,B""8_(': %!2OPP(12c61GHH 2GHH1OCFGGBs-..r122 JV/@AAh'x x(I%(x778IAC C 	 %'QRR h&J#00:1=O,,-&&)6" .FE* !7!78h.&28V&DO?+q0(LN N &*
*9!*<CE E G
R%C4)445Lc0001J IzG
--
 	 B
c1fa"a%i 
 C
vv
i-3r6DI-w6
6C
l
C
_c::o  "!"
 	" s"   J$ J6 $J32J36K
Kc           
         dd| z  cxk  rt         k  st        d       t        d      t        |      }t        |      }t        |t	        |       z  |z        }	 t        ||      }||| kD  rt	        | |z
         t	        | |z         z  }t        | |z
        t        | |z         z  }dt	        |      z  t        |       z  t	        | |z         z  }	dt	        |      z  t        |       z  t	        | |z         t        | |z
        z  z  }
|||
|	gS ||z  }t        t        |       t        t        t	        |       dz  |dz  z
        z  z
  t        |       t        t        t	        |       dz  |dz  z
        z  z   z        }t        |dz  t        |       z  t        t        t	        |       dz  |dz  z
        z  z
  |dz  t        |       z  t        t        t	        |       dz  |dz  z
        z  z   z        }||gS # t        $ r d}Y w xY w)ab  
    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
    =======

    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
    ==========

    .. [1] https://en.wikipedia.org/wiki/Fresnel_equations
    r   r3   r/   N)r   r>   r,   r   r   r   r   r   r   r   r   )rL   rF   rG   rJ   rK   angle_of_refraction(angle_of_total_internal_reflection_onsetR_sR_pT_sT_pr*   s               r+   r   r      sp   b $$)r)CDD *CDD	#G	,B	#G	,Br#&8"99"<=83A"b3I0 07,/AA%(;;<<'*==>?$'::;'*==>?'((-?)@@'*==>?'((-?)@@(+>>?'*==>?@ S#s##rEc,-$s-.1AqD899:()$s-.1AqD899:; < ad3122$s-.1AqD899:Q$s-..$s-.1AqD899:; < Sz3  83708s   G9 9HHc                    t        | ||||      }	 t        |       }|t        |      |z
  S |dk7  rt	        |t
              rt        |j                        }t	        | t              sHt        |       rt        |       }n3t	        | t
              rt        | j                        }nt        d      | }|[t	        |t              sHt        |      rt        |      }nIt	        |t
              rt        |j                        }n#t        d      |}nt        |j                        }t        t        d |D                    }	t        t        d |D                    }
t        t        d |D                    }||	z  }||
z  }||z  }t        |j                  |            }t        |j                  |            }||z
  S y# t        $ r d}Y w xY w)	a  
    This function calculates the angle of deviation of a ray
    due to refraction at planar surface.

    Parameters
    ==========

    incident : Matrix, Ray3D, sequence or float
        Incident vector or angle of incidence
    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.

    Returns angular deviation between incident and refracted rays

    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)
    >>> round(deviation(0.1, 1.2, 1.5), 5)
    -0.02005
    )rH   rI   Nr   r0   z-normal should be a Matrix, Ray3D, or sequencec              3   &   K   | ]	  }|d z    ywr2   r4   r5   s     r+   r8   zdeviation.<locals>.<genexpr>  s     818r9   c              3   &   K   | ]	  }|d z    ywr2   r4   r5   s     r+   r8   zdeviation.<locals>.<genexpr>  s     4qad4r9   c              3   &   K   | ]	  }|d z    ywr2   r4   r5   s     r+   r8   zdeviation.<locals>.<genexpr>  s      9!A 9r9   )r   r   r=   r<   r'   r    r   r;   r#   r?   r   rA   r   rB   )rE   rF   rG   rH   rI   	refractedrL   rO   rQ   rS   rT   mag_refractedr7   rs                 r+   r   r   0  s   N !!(!((.',	.I
""8_ %Y"444A~i'y889I(F+8$"8,	He,"8#;#;<	EG G !I=ff-v&$VnG.$V%;%;<G#GI I !U001GC8i889#4G445
S 9y 99:\!	:]"	w'(w'(1uK   "!"s   F> >GGc                 F    t        |       }t        |      }t        ||      S )a  
    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

    )r,   r   rF   rG   rJ   rK   s       r+   r   r     s$    , 
$G	,B	#G	,BR=r-   c                 j    t        |       }t        |      }||kD  rt        d      t        ||z        S )a  
    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

    z0Total internal reflection impossible for n1 < n2)r,   r>   r   rg   s       r+   r   r     s9    , 
$G	,B	#G	,B	BwKLLBrE{r-   c                 @   t        | t              r| j                  } nt        |       } t        |t              r|j                  }nt        |      }t        |      }d| |z
  |z  d|z  d|z  z
  | |z
  |z  | |z  |z  z  z   z  z  }|t        k(  rt
        j                  S |S )a9  
    This function calculates focal length of a 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.
    d : sympifiable, optional
        Thickness of lens, default value is 0.

    Examples
    ========

    >>> from sympy.physics.optics import lens_makers_formula
    >>> from sympy import S
    >>> lens_makers_formula(1.33, 1, 10, -10)
    15.1515151515151
    >>> lens_makers_formula(1.2, 1, 10, S.Infinity)
    50.0000000000000
    >>> lens_makers_formula(1.33, 1, 10, -10, d=1)
    15.3418463277618

    r$   )r'   r%   r(   r   r   r   Infinity)n_lensn_surrr1r2dfocal_lengths         r+   r   r     s    B &&!((&&!((
Av&0!B$2+6F?VWBW\beg\gjl\lAm2nopLszzr-   c                    | r|r|rt        d      t        |       } t        |      }t        |      }|t        u rt        d      }|t        u rt        d      }| t        u rt        d      }| |t        u r?|t        u r7t	        t	        z  ||z   z  |t              |t              j                         S |t        u r(t	        |z  ||z   z  |t              j                         S |t        u r(t	        |z  ||z   z  |t              j                         S ||z  ||z   z  S ||t        u r?| t        u r7t	        t	        z  ||z
  z  |t              |t              j                         S |t        u r(t	        | z  || z
  z  |t              j                         S | t        u r(t	        |z  ||z
  z  |t              j                         S || z  || z
  z  S ||t        u r?| t        u r7t	        t	        z  ||z
  z  |t              |t              j                         S |t        u r(t	        | z  || z
  z  |t              j                         S | t        u r(t	        |z  ||z
  z  |t              j                         S || z  || z
  z  S y)a  
    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)

    "Please provide only two parametersuvfNr>   r   r   r   r   doitrp   rs   rt   _u_v_fs         r+   r   r     sk   > a=>><(L
A
ABwC[BwC[rC[7qBwr"ub2gB7R@EEGG72q2vB/44667ArAvB/4466sAE{y7|r)r"ub2gB7R@EEGG7L"|*;<b"EJJLL22q2vB/4466~q</00y7|r)r"ub2gB7R@EEGG7L"|*;<b"EJJLL22q2vB/4466~q</00 r-   c                    | r|r|rt        d      t        |       } t        |      }t        |      }|t        u rt        d      }|t        u rt        d      }| t        u rt        d      }| |t        u r?|t        u r7t	        t	        z  ||z
  z  |t              |t              j                         S |t        u r(t	        |z  ||z
  z  |t              j                         S |t        u r(t	        |z  ||z
  z  |t              j                         S ||z  ||z
  z  S ||t        u r?| t        u r7t	        t	        z  ||z
  z  |t              |t              j                         S |t        u r(t	        | z  | |z
  z  |t              j                         S | t        u r(t	        |z  ||z
  z  |t              j                         S || z  | |z
  z  S ||t        u r?| t        u r7t	        t	        z  ||z   z  |t              |t              j                         S |t        u r(t	        | z  || z   z  |t              j                         S | t        u r(t	        |z  ||z   z  |t              j                         S || z  || z   z  S y)a  
    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)

    rr   rs   rt   ru   Nrv   rx   s         r+   r	   r	   >  sk   > a=>><(L
A
ABwC[BwC[rC[7qBwr"ub2gB7R@EEGG72rAvB/44667Aq2vB/4466sAE{y7|r)r"ub2gB7R@EEGG7L,*;<b"EJJLL22rAvB/4466~|a/00y7|r)r"ub2gB7R@EEGG7L"|*;<b"EJJLL22q2vB/4466~q</00 r-   c                 `    t        |       } t        |      }t        |      }d||z  z  | dz  z  S )a  

    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
    >>> round(hyperfocal_distance(f = 0.5, N = 8, c = 0.0033), 2)
    9.47
    r$   r3   r   )ru   Ncs      r+   r
   r
     s6    . 	
A
A
Aq1uI1r-   c                 :    t        |       } t        |      }| |z   S )a  

    Calculates the transverse magnification upon reflection in a mirror,
    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   )sisos     r+   r   r     s"    0 
B	BeHr-   )NN)r   )NNN)2__doc____all__sympy.core.numbersr   r   r   r   r   sympy.core.singletonr   sympy.core.symbolr   sympy.core.sympifyr   (sympy.functions.elementary.miscellaneousr   (sympy.functions.elementary.trigonometricr   r   r   r   r   r   sympy.matrices.denser   sympy.polys.polytoolsr   sympy.series.limitsr   sympy.geometry.liner    sympy.geometry.utilr!   sympy.geometry.planer"   sympy.utilities.iterablesr#   r)   r%   r,   r   r   r   r   r   r   r   r	   r
   r   r4   r-   r+   <module>r      s   
 7 6 " $ & 9 W W ' ( % % , & 1 `;FSlYx6@/dB1JB1H:r-   