K iV(dZgdZddlmZmZmZmZmZddlm Z ddl m Z ddl m Z ddlmZddlmZmZmZmZmZmZdd lmZdd lmZdd lmZdd lmZdd lm Z ddl!m"Z"ddl#m$Z$ddl%m&Z&dZ'ddZ(dZ)ddZ*dZ+dZ,ddZ-d dZ.d dZ/dZ0dZ1y)!z **Contains** * refraction_angle * fresnel_coefficients * deviation * brewster_angle * critical_angle * lens_makers_formula * mirror_formula * lens_formula * hyperfocal_distance * transverse_magnification ) refraction_angle deviationfresnel_coefficientsbrewster_anglecritical_anglelens_makers_formulamirror_formula lens_formulahyperfocal_distancetransverse_magnification)FloatIoopizoo)S)Symbolsympify)sqrt)acosasinatan2cossintan)Matrix)cancel)Limit)Ray3D) intersection)Plane) is_sequence)MediumcXt|tr|j}|St|}|S)zG Helper function that returns refractive index, given a medium ) isinstancer%refractive_indexr)mediumns `/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/physics/optics/utils.pyrefractive_index_of_mediumr,,s0&&!  # # H FO HNct|}t|} t|} t||}|d|| t dd|cxkrt dzkst dt d|r||kDr t dt |t|z|z Sd} | | t dt|tsHt|r t|} n3t|trt|j} n td |} |Wt|ts td t|trd } |j|d } t|j} nt|tst|r t|} not|trRt|j} t|tr:t||} t!| d k(r t d d } | d } n td|} ||z } t#t%d| D}t#t%d| D}| |z} | |z} | j'|  }d| dzd|dzz zz }|j(rt*j,S| | z| |zt#|z | zz}||z}| s|St |S#t$rd}YwxYw#ttf$rd}YwxYw)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 anglegg?(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 sequencec3&K|] }|dz ywN.0is r+ z#refraction_angle..s4QAqD4c3&K|] }|dz ywr2r4r5s r+r8z#refraction_angle..s01!Q$0r9r$r3)direction_ratio)r,float TypeErrorr ValueErrorrrrr'rr#r r;r"r! normal_vectorlenrsumdot is_negativerZero)incidentmedium1medium2normalplanen1n2angle_of_incidencecritical_angle_ return_ray _incidentintersection_pt_normaleta mag_incident mag_normalc1cs2drss r+rr7sF $G ,B #G ,B""8_(':%  !2OP P(12c61GH H2GH H 1OCFG GBs-..r122J V/@AA h ' x x(I % (x778IAC C  %'QR R h &J#00:1=O,,-&&)6" .FE* !7!78h.&28V&DO?+q0(LNN&* *9!*<CEEG R%C4)445Lc0001JI zG -- B c1fa"a%i C vv i-3r6DI-w6 6C l C  _c::o "!"  "s" J$ J6$ J32J36K  K c dd|zcxkrtkstdtdt|}t|}t|t |z|z } t ||}|||kDrt ||z  t ||zz }t ||z t ||zz }dt |zt|zt ||zz } dt |zt|zt ||zt||z zz } ||| | gS||z } tt|ttt |dz| dzz zz t|ttt |dz| dzz zzz }t| dzt|zttt |dz| dzz zz | dzt|zttt |dz| dzz zzz }||gS#t$rd}YwxYw)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 r3r/N) rr>r,rrrrrrrr) rLrFrGrJrKangle_of_refraction(angle_of_total_internal_reflection_onsetR_sR_pT_sT_pr*s r+rrspb $$ )r )CDD *CDD #G ,B #G ,Br#&8"99"<=83A"b3I007,/AA%(;;<<'*==>?$'::;'*==>?'((-?)@@'*==>?'((-?)@@(+>>?'*==>?@S#s## rEc,-$s-.1AqD899:()$s-.1AqD899:;<ad3122$s-.1AqD899:Q$s-..$s-.1AqD899:;<Sz3 83708s G99 HHct|||||} t|}|t||z S|dk7rt |t rt |j}t |t sHt|r t |}n3t |t rt |j}n td|}|[t |t sHt|r t |}nIt |t rt |j}n#td|}nt |j}ttd|D} ttd|D} ttd|D} || z}|| z}|| z}t|j|} t|j|} | | z Sy#t$rd}YwxYw) 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 )rHrINr r0z-normal should be a Matrix, Ray3D, or sequencec3&K|] }|dz ywr2r4r5s r+r8zdeviation..s818r9c3&K|] }|dz ywr2r4r5s r+r8zdeviation..s4qad4r9c3&K|] }|dz ywr2r4r5s r+r8zdeviation..s 9!A 9r9)rr r=r<r'r rr;r#r?rrArrB)rErFrGrHrI refractedrLrOrQrSrT mag_refractedr7rs r+rr0sN!!(!((.', .I ""8_%Y"444A~ i 'y889I(F+8$"8, He,"8#;#;< EGG!I =ff-v&$VnG.$V%;%;> G  G cFt|}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,rrFrGrJrKs r+rrs$, $G ,B #G ,B R=r-cjt|}t|}||kDr tdt||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>rrgs r+rrs9, $G ,B #G ,B BwKLLBrE{r-c@t|tr |j}n t|}t|tr |j}n t|}t|}d||z |z d|z d|z z ||z |z||z|zz zzz }|tk(rt 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(rrrInfinity)n_lensn_surrr1r2d focal_lengths r+rrsB&&!((&&!(( Av&0!B$2+6F?VWBW\beg\gjl\lAm2nopLszz r-c|r|r |r tdt|}t|}t|}|tur td}|tur td}|tur td}||tur?|tur7t t z||zz |t|tj S|tur(t |z||zz |tj S|tur(t |z||zz |tj S||z||zz S||tur?|tur7t t z||z z |t|tj S|tur(t |z||z z |tj S|tur(t |z||z z |tj S||z||z z S||tur?|tur7t t z||z z |t|tj S|tur(t |z||z z |tj S|tur(t |z||z z |tj S||z||z z Sy)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 parametersuvfNr>rrrrdoitrprsrt_u_v_fs r+rrsk>a=>><(L A ABw C[Bw C[r C[ 7qBwr"ub2gB7R@EEG G 72q2vB/446 6 7ArAvB/446 6sAE{y 7|r)r"ub2gB7R@EEG G 7L"|*;>> 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) rrrsrtruNrvrxs r+r r >sk>a=>><(L A ABw C[Bw C[r C[ 7qBwr"ub2gB7R@EEG G 72rAvB/446 6 7Aq2vB/446 6sAE{y 7|r)r"ub2gB7R@EEG G 7L,*;>> from sympy.physics.optics import hyperfocal_distance >>> round(hyperfocal_distance(f = 0.5, N = 8, c = 0.0033), 2) 9.47 r$r3r)ruNcs r+r r s6.  A A A q1uI1 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)sisos r+r r s"0 B BeHr-)NN)r )NNN)2__doc____all__sympy.core.numbersr rrrrsympy.core.singletonrsympy.core.symbolrsympy.core.sympifyr(sympy.functions.elementary.miscellaneousr(sympy.functions.elementary.trigonometricrrrrrrsympy.matrices.densersympy.polys.polytoolsrsympy.series.limitsrsympy.geometry.liner sympy.geometry.utilr!sympy.geometry.planer"sympy.utilities.iterablesr#r)r%r,rrrrrrrr r r r4r-r+rs  76"$&9WW'(%%,&1 `;FSlYx6@/dB1JB1H:r-