"""
MikoshiLang Extended Functions - Set 26
Optics, photonics, lasers, and electromagnetic waves
"""
import sympy as sp
from sympy import symbols, sqrt, sin, cos, tan, exp, log, pi, atan2

def get_rules():
    """Extended set 26: Optics and photonics (150 functions)"""
    n, theta, lambda_ = symbols('n theta lambda')
    
    return [
        # ===== GEOMETRIC OPTICS (35 functions) =====
        ('SnellsLawOptics[n1_, n2_, theta1_]', lambda n1, n2, th1: sp.asin(n1*sin(th1)/n2)),
        ('CriticalAngle[n1_, n2_]', lambda n1, n2: sp.asin(n2/n1)),
        ('TotalInternalReflection[theta_, theta_c_]', lambda th, thc: th > thc),
        ('BrewsterAngle[n1_, n2_]', lambda n1, n2: atan2(n2, n1)),
        ('FresnelReflectionS[n1_, n2_, theta1_, theta2_]', lambda n1, n2, th1, th2: ((n1*cos(th1) - n2*cos(th2))/(n1*cos(th1) + n2*cos(th2)))**2),
        ('FresnelReflectionP[n1_, n2_, theta1_, theta2_]', lambda n1, n2, th1, th2: ((n1*cos(th2) - n2*cos(th1))/(n1*cos(th2) + n2*cos(th1)))**2),
        ('FresnelTransmissionS[n1_, n2_, theta1_, theta2_]', lambda n1, n2, th1, th2: (2*n1*cos(th1)/(n1*cos(th1) + n2*cos(th2)))**2*(n2*cos(th2)/(n1*cos(th1)))),
        ('FresnelTransmissionP[n1_, n2_, theta1_, theta2_]', lambda n1, n2, th1, th2: (2*n1*cos(th1)/(n1*cos(th2) + n2*cos(th1)))**2*(n2*cos(th2)/(n1*cos(th1)))),
        ('ThinLensEquation[f_, do_, di_]', lambda f, do, di: 1/f - 1/do - 1/di),
        ('LensmakerEquation[n_, R1_, R2_, d_]', lambda n, R1, R2, d: (n - 1)*(1/R1 - 1/R2 + (n - 1)*d/(n*R1*R2))),
        ('Magnification[di_, do_]', lambda di, do: -di/do),
        ('AngularMagnification[theta_i_, theta_o_]', lambda thi, tho: thi/tho),
        ('OpticalPower[f_]', lambda f: 1/f),  # Diopters
        ('FocalLength[R_, n_]', lambda R, n: R/(2*(n - 1))),
        ('ImageDistance[f_, object_distance_]', lambda f, do: f*do/(do - f)),
        ('NewtonianFormLens[x_, x_prime_, f_]', lambda x, xp, f: x*xp - f**2),
        ('SphericalMirrorEquation[f_, do_, di_]', lambda f, do, di: 1/f - 1/do - 1/di),
        ('MirrorFocalLength[R_]', lambda R: R/2),
        ('ParaxialApproximation[theta_]', lambda th: sin(th) == th),
        ('SphericalAberration[n_, R_, h_]', lambda n, R, h: h**4/(8*R**3)),
        ('ComaAberration[h_, theta_]', lambda h, th: h*th**2),
        ('AstigmatismAberration[f_s_, f_t_]', lambda fs, ft: fs - ft),
        ('FieldCurvature[f_, n_]', lambda f, n: f/n),
        ('Distortion[radial_, tangential_]', lambda rad, tan: rad + tan),
        ('ChromaticAberration[f_blue_, f_red_]', lambda fb, fr: fb - fr),
        ('AxialChromaticAberration[delta_f_]', lambda df: df),
        ('LateralChromaticAberration[delta_m_]', lambda dm: dm),
        ('AbbeNumber[nd_, nF_, nC_]', lambda nd, nF, nC: (nd - 1)/(nF - nC)),
        ('DispersivePower[delta_n_, n_]', lambda dn, n: dn/(n - 1)),
        ('RayleighCriterion[lambda_, D_]', lambda lam, D: 1.22*lam/D),
        ('OpticalResolution[lambda_, NA_]', lambda lam, NA: 0.61*lam/NA),
        ('NumericalAperture[n_, theta_]', lambda n, th: n*sin(th)),
        ('FNumber[f_, D_]', lambda f, D: f/D),
        ('DepthOfField[f_, N_, c_]', lambda f, N, c: 2*N*c*f**2/sp.Symbol('s')**2),
        ('HyperfocalDistance[f_, N_, c_]', lambda f, N, c: f**2/(N*c)),
        
        # ===== WAVE OPTICS (35 functions) =====
        ('YoungDoubleSlitInterference[m_, lambda_, d_]', lambda m, lam, d: m*lam == d*sin(sp.Symbol('theta'))),
        ('FringeSpacing[lambda_, L_, d_]', lambda lam, L, d: lam*L/d),
        ('MichelsonInterferometerPathDifference[d_]', lambda d: 2*d),
        ('FabryPerotResonance[m_, lambda_, d_, n_]', lambda m, lam, d, n: 2*n*d == m*lam),
        ('FinesseParameter[FSR_, delta_lambda_]', lambda FSR, dlam: FSR/dlam),
        ('FreeSpectralRange[lambda_, d_, n_]', lambda lam, d, n: lam**2/(2*n*d)),
        ('MachZehnderInterferometer[delta_phi_]', lambda dphi: dphi),
        ('SagnacEffect[A_, omega_, lambda_]', lambda A, omega, lam: 4*pi*A*omega/(sp.Symbol('c')*lam)),
        ('NewtonRings[m_, lambda_, R_]', lambda m, lam, R: sqrt(m*lam*R)),
        ('ThickFilmInterference[m_, lambda_, d_, n_]', lambda m, lam, d, n: 2*n*d*cos(sp.Symbol('theta')) == m*lam),
        ('AntireflectionCoating[n_film_, n_substrate_]', lambda nf, ns: sqrt(nf*ns)),
        ('QuarterWaveThickness[lambda_, n_]', lambda lam, n: lam/(4*n)),
        ('HalfWaveThickness[lambda_, n_]', lambda lam, n: lam/(2*n)),
        ('MultiLayerInterference[d_, n_, lambda_]', lambda d, n, lam: 2*n*d/lam),
        ('BraggReflectorBandgap[n1_, n2_, lambda_]', lambda n1, n2, lam: lam*(n1 - n2)),
        ('DiffractionGrating[m_, lambda_, d_]', lambda m, lam, d: m*lam == d*sin(sp.Symbol('theta'))),
        ('BlazedGratingEfficiency[theta_b_]', lambda thb: sin(thb)**2),
        ('FresnelDiffraction[u_, v_]', lambda u, v: (u, v)),
        ('FresnelIntegralC[x_]', lambda x: sp.integrate(cos(pi*sp.Symbol('t')**2/2), (sp.Symbol('t'), 0, x))),
        ('FresnelIntegralS[x_]', lambda x: sp.integrate(sin(pi*sp.Symbol('t')**2/2), (sp.Symbol('t'), 0, x))),
        ('FraunhoferDiffraction[a_, lambda_, f_]', lambda a, lam, f: lam*f/a),
        ('SingleSlitDiffraction[m_, lambda_, a_]', lambda m, lam, a: a*sin(sp.Symbol('theta')) == m*lam),
        ('CircularApertureDiffraction[D_, lambda_]', lambda D, lam: 1.22*lam/D),
        ('AiryDiskRadius[lambda_, f_, D_]', lambda lam, f, D: 1.22*lam*f/D),
        ('BesselBeamProfile[J0_, r_]', lambda J0, r: J0**2),
        ('GaussianBeamWaist[w0_]', lambda w0: w0),
        ('GaussianBeamDivergence[lambda_, w0_]', lambda lam, w0: lam/(pi*w0)),
        ('RayleighRange[w0_, lambda_]', lambda w0, lam: pi*w0**2/lam),
        ('BeamQualityFactor[M2_]', lambda M2: M2),
        ('Gouy PhaseShift[z_, zR_]', lambda z, zR: atan2(z, zR)),
        ('HermiteGaussianMode[m_, n_]', lambda m, n: (m, n)),
        ('LaguerreGaussianMode[p_, l_]', lambda p, l: (p, l)),
        ('OrbitalAngularMomentum[l_]', lambda l: l*sp.Symbol('hbar')),
        ('VortexBeam[l_, phi_]', lambda l, phi: exp(sp.I*l*phi)),
        ('BesselBeam[kz_, kr_]', lambda kz, kr: sqrt(kz**2 + kr**2)),
        
        # ===== POLARIZATION (25 functions) =====
        ('LinearPolarization[E0_, theta_]', lambda E0, th: E0*sp.Matrix([cos(th), sin(th)])),
        ('CircularPolarization[E0_, handedness_]', lambda E0, h: E0/sqrt(2)*sp.Matrix([1, sp.I*h])),
        ('EllipticalPolarization[a_, b_, delta_]', lambda a, b, delta: (a, b, delta)),
        ('JonesVector[Ex_, Ey_]', lambda Ex, Ey: sp.Matrix([Ex, Ey])),
        ('JonesMatrix[element_]', lambda elem: sp.Matrix([[1, 0], [0, 1]])),  # Identity placeholder
        ('LinearPolarizerJones[theta_]', lambda th: sp.Matrix([[cos(th)**2, cos(th)*sin(th)], [cos(th)*sin(th), sin(th)**2]])),
        ('WavePlateJones[delta_]', lambda delta: sp.Matrix([[1, 0], [0, exp(sp.I*delta)]])),
        ('QuarterWavePlate[]', lambda: sp.Matrix([[1, 0], [0, sp.I]])),
        ('HalfWavePlate[]', lambda: sp.Matrix([[1, 0], [0, -1]])),
        ('RotationMatrixJones[theta_]', lambda th: sp.Matrix([[cos(th), sin(th)], [-sin(th), cos(th)]])),
        ('StokesParameter[S0_, S1_, S2_, S3_]', lambda S0, S1, S2, S3: [S0, S1, S2, S3]),
        ('DegreeOfPolarization[S1_, S2_, S3_, S0_]', lambda S1, S2, S3, S0: sqrt(S1**2 + S2**2 + S3**2)/S0),
        ('PoincareSphere[S1_, S2_, S3_]', lambda S1, S2, S3: (S1, S2, S3)),
        ('MuellerMatrix[M_]', lambda M: M),
        ('BirefringenceIndex[ne_, no_]', lambda ne, no: ne - no),
        ('OpticalActivity[alpha_, l_]', lambda alpha, l: alpha*l),
        ('FaradayRotation[V_, B_, l_]', lambda V, B, l: V*B*l),
        ('VerdetConstant[theta_, B_, l_]', lambda th, B, l: th/(B*l)),
        ('KerrEffect[n_, E_]', lambda n, E: n + sp.Symbol('K')*E**2),
        ('PockelsEffect[delta_n_, E_]', lambda dn, E: dn == sp.Symbol('r')*E),
        ('ElectroopticCoefficient[r_]', lambda r: r),
        ('BirefringentRetardation[delta_]', lambda delta: delta),
        ('CircularDichroism[A_L_, A_R_]', lambda AL, AR: AL - AR),
        ('OpticalRotatoryDispersion[alpha_, lambda_]', lambda alpha, lam: alpha/lam**2),
        ('CottonEffect[CD_, wavelength_]', lambda CD, lam: CD),
        
        # ===== LASERS (30 functions) =====
        ('LaserGain[g_, l_]', lambda g, l: exp(g*l)),
        ('PopulationInversion[N2_, N1_]', lambda N2, N1: N2 - N1),
        ('EinsteinACoefficient[A21_]', lambda A: A),  # Spontaneous emission
        ('EinsteinBCoefficient[B21_]', lambda B: B),  # Stimulated emission
        ('GainCoefficient[sigma_, delta_N_]', lambda sig, dN: sig*dN),
        ('StimulatedEmissionCrossSection[sigma_]', lambda sig: sig),
        ('LaserThreshold[R1_, R2_, alpha_, g_]', lambda R1, R2, alpha, g: -log(R1*R2)/(2*sp.Symbol('L')) + alpha),
        ('OutputCoupling[T_]', lambda T: T),
        ('CavityLosses[alpha_, L_]', lambda alpha, L: alpha*L),
        ('RoundTripGain[g_, L_]', lambda g, L: exp(2*g*L)),
        ('QSwitching[tau_]', lambda tau: tau),
        ('ModeLocking[delta_t_]', lambda dt: dt),
        ('PulseWidth[delta_nu_]', lambda dnu: 1/dnu),
        ('RepetitionRate[cavity_length_]', lambda L: sp.Symbol('c')/(2*L)),
        ('PeakPower[energy_, pulse_width_]', lambda E, dt: E/dt),
        ('AveragePower[peak_power_, duty_cycle_]', lambda Ppeak, dc: Ppeak*dc),
        ('LaserFluence[energy_, area_]', lambda E, A: E/A),
        ('LaserIntensity[power_, area_]', lambda P, A: P/A),
        ('SaturationIntensity[I_sat_]', lambda Isat: Isat),
        ('RabiFrequency[mu_, E_]', lambda mu, E: mu*E/sp.Symbol('hbar')),
        ('DopplerBroadening[nu0_, T_]', lambda nu0, T: nu0*sqrt(8*sp.Symbol('k_B')*T*log(2)/(sp.Symbol('m')*sp.Symbol('c')**2))),
        ('PressureBroadening[gamma_]', lambda gamma: gamma),
        ('HomogeneousBroadening[delta_nu_]', lambda dnu: dnu),
        ('InhomogeneousBroadening[delta_nu_]', lambda dnu: dnu),
        ('LaserLinewidth[delta_nu_]', lambda dnu: dnu),
        ('SchawlowTownesLimit[delta_nu_]', lambda dnu: dnu),
        ('FrequencyDoubling[omega_]', lambda omega: 2*omega),  # SHG
        ('ThirdHarmonicGeneration[omega_]', lambda omega: 3*omega),
        ('SumFrequencyGeneration[omega1_, omega2_]', lambda om1, om2: om1 + om2),
        ('DifferenceFrequencyGeneration[omega1_, omega2_]', lambda om1, om2: om1 - om2),
        
        # ===== NONLINEAR OPTICS (25 functions) =====
        ('SecondHarmonicGeneration[I_, chi2_]', lambda I, chi2: chi2*I**2),
        ('ThirdOrderSusceptibility[chi3_]', lambda chi3: chi3),
        ('TwoPhotonAbsorption[I_, beta_]', lambda I, beta: beta*I**2),
        ('OpticalKerrEffect[n2_, I_]', lambda n2, I: n2*I),
        ('SelfPhaseModulation[n2_, I_, L_]', lambda n2, I, L: n2*I*L),
        ('CrossPhaseModulation[n2_, I1_, I2_]', lambda n2, I1, I2: 2*n2*I1*I2),
        ('FourWaveMixing[omega1_, omega2_, omega3_]', lambda om1, om2, om3: om1 + om2 - om3),
        ('StimulatedRamanScattering[omega_, omega_v_]', lambda om, omv: om - omv),
        ('StimulatedBrillouinScattering[omega_]', lambda om: om),
        ('OpticalParametricAmplification[signal_, pump_]', lambda sig, pump: sig*pump),
        ('OpticalParametricOscillation[threshold_]', lambda th: th),
        ('PhaseMatching[k1_, k2_, k3_]', lambda k1, k2, k3: k1 + k2 - k3),
        ('QuasiPhaseMatching[period_]', lambda period: 2*pi/period),
        ('CoherenceLength[delta_k_]', lambda dk: pi/dk),
        ('ParametricGain[gamma_, P_]', lambda gamma, P: gamma*P),
        ('OpticalSoliton[beta2_, gamma_, P_]', lambda beta2, gamma, P: sqrt(abs(beta2)/(gamma*P))),
        ('SolitonPeriod[LD_]', lambda LD: pi*LD/2),
        ('DispersionLength[beta2_, T0_]', lambda beta2, T0: T0**2/abs(beta2)),
        ('NonlinearLength[gamma_, P0_]', lambda gamma, P0: 1/(gamma*P0)),
        ('SelfSteepening[shock_parameter_]', lambda s: s),
        ('IntrapulsRamanScattering[TR_]', lambda TR: TR),
        ('SupercontinuumGeneration[spectrum_]', lambda spec: spec),
        ('ModulationalInstability[beta2_, gamma_, P_]', lambda beta2, gamma, P: beta2*gamma*P < 0),
        ('RogueWave[amplitude_]', lambda amp: amp),
        ('Soliton FissionLength[N_]', lambda N: sp.Symbol('LD')/N),
    ]


def register():
    """Register all extended26 rules"""
    from .extended_helper import convert_rules
    return convert_rules(get_rules())
