def transmit2x2(fvec_in, cmat, fmatout=None, tmatin=None, tmatout=None, fvec_out=None): """Transmits field vector using 2x2 method. This functions takes a field vector that describes the input field and computes the output transmited field using the 2x2 characteristic matrix. """ b = np.broadcast(fvec_in[..., 0][..., None, None], cmat) if fmatout is None: fmatout = f_iso(1, 0, 0) if fvec_out is not None: fvec_out[...] = 0 else: fvec_out = np.zeros(b.shape[:-2] + (4, ), fvec_in.dtype) if tmatin is not None: evec = dotmv(tmatin, fvec_in[..., ::2], out=fvec_out[..., ::2]) else: evec = fvec_in[..., ::2] eout = dotmv(cmat, evec, out=fvec_out[..., ::2]) if tmatout is not None: eout = dotmv(tmatout, eout, out=fvec_out[..., ::2]) e2h = E2H_mat(fmatout, mode=+1) hout = dotmv(e2h, eout, out=fvec_out[..., 1::2]) return fvec_out
def transmitted_field_direct(field, beta, phi, n=1.): ev = refind2eps([n] * 3) ea = np.zeros_like(ev) alpha, fmat = alphaf(beta, phi, ev, ea) pmat = projection_mat(fmat) field0 = np.empty_like(field) dotmv(pmat, transpose(field), out=transpose(field0)) return field0
def field4(fmat, jones=(1, 0), amplitude=1., mode=+1, out=None): """Build field vector form a given polarization state, amplitude and mode. Numpy broadcasting rules apply.""" jones = np.asarray(jones) amplitude = np.asarray(amplitude) c, s = jones[..., 0], jones[..., 1] b = np.broadcast(fmat[..., 0, 0], c, amplitude) shape = b.shape + (4, ) fmat = normalize_f(fmat) fvec = np.zeros(shape, CDTYPE) if mode == +1: fvec[..., 0] = c fvec[..., 2] = s elif mode == -1: fvec[..., 1] = c fvec[..., 3] = s else: raise ValueError("Unknown propagation mode.") fvec = dotmv(fmat, fvec, out=out) a = np.asarray(amplitude)[..., None] out = np.multiply(fvec, a, out=fvec) return out
def _reflect2d(fvec_in, fmat_in, rmat, fmat_out, fvec_out=None): """Transmits/reflects field vector using 4x4 method. This functions takes a field vector that describes the input field and computes the output transmited field and also updates the input field with the reflected waves. """ fmat_ini = inv(fmat_in) avec = dotmv(fmat_ini, fvec_in) a = np.zeros(avec.shape, avec.dtype) a[..., 0::2] = avec[..., 0::2] if fvec_out is not None: fmat_outi = inv(fmat_out) bvec = dotmv(fmat_outi, fvec_out) a[..., 1::2] = bvec[..., 1::2] else: bvec = np.zeros_like(avec) av = a.reshape(a.shape[:-2] + (a.shape[-2] * a.shape[-1], )) out = dotmv(rmat, av).reshape(a.shape) avec[..., 1::2] = out[..., 1::2] bvec[..., ::2] = out[..., ::2] dotmv(fmat_in, avec, out=fvec_in) return dotmv(fmat_out, bvec, out=out)
def jonesvec(pol, phi=0., out=None): """Returns a normalized jones vector from an input length 2 vector., Additionaly, you can use this function to view the jones vector in rotated coordinate frame, defined with a rotation angle phi. Numpy broadcasting rules apply. Parameters ---------- pol : (...,2) array Input jones vector. Does not need to be normalized. phi : float or (...,1) array If jones vector must be view in the rotated frame, this parameter defines the rotation angle. out : ndarray Output array in which to put the result; if provided, it must have a shape that the inputs broadcast to. Returns ------- vec : ndarray Normalized jones vector Example ------- >>> jonesvec((1,1j)) #left-handed circuar polarization array([0.70710678+0.j , 0. +0.70710678j]) In rotated frame >>> j1,j2 = jonesvec((1,1j), (np.pi/2,np.pi/4)) >>> np.allclose(j1, (0.70710678j,-0.70710678)) True >>> np.allclose(j2, (0.5+0.5j,-0.5+0.5j)) True """ pol = np.asarray(pol, CDTYPE) phi = np.asarray(phi) if pol.shape[-1] != 2: raise ValueError("Invalid input shape") norm = (pol[..., 0] * pol[..., 0].conj() + pol[..., 1] * pol[..., 1].conj())**0.5 pol = pol / norm[..., np.newaxis] pol = np.asarray(pol, CDTYPE) r = rotation_matrix2(-phi) return dotmv(r, pol, out)
def transmit(fvec_in, cmat, fmatin=None, fmatout=None, fmatini=None, fmatouti=None, fvec_out=None): """Transmits field vector using 4x4 method. This functions takes a field vector that describes the input field and computes the output transmited field and also updates the input field with the reflected waves. """ b = np.broadcast(fvec_in[..., None], cmat, fmatin, fmatout) if fmatini is None: if fmatin is None: fmatin = f_iso(1, 0, 0) fmatini = inv(fmatin) if fmatin is None: fmatin = inv(fmatini) if fmatouti is None: if fmatout is None: fmatout = fmatin fmatouti = fmatini else: fmatouti = inv(fmatout) if fmatout is None: fmatout = inv(fmatouti) smat = system_mat(cmat, fmatini=fmatini, fmatout=fmatout) avec = dotmv(fmatini, fvec_in) a = np.zeros(b.shape[:-1], avec.dtype) a[..., 0::2] = avec[..., 0::2] avec = a.copy() #so that it broadcasts if fvec_out is not None: bvec = dotmv(fmatouti, fvec_out) a[..., 1::2] = bvec[..., 1::2] else: bvec = np.zeros_like(avec) r = reflection_mat(smat) out = dotmv(r, a, out=fvec_out) avec[..., 1::2] = out[..., 1::2] bvec[..., ::2] = out[..., ::2] dotmv(fmatin, avec, out=fvec_in) return dotmv(fmatout, bvec, out=out)
def apply_jonesmat(pmat, field, out=None): """Multiplies a (2x2) or (4x4) jones matrix with field vector Parameters ---------- pmat : ndarray A 4x4 jones matrix of shape (...,4,4) for field data or 2x2 jones matrix of shape (...,2,2) for E-field data or field : array Field vector array of shape (...,4) or (...,2). out : array, optional If specified, the results are written here. Returns ------- out : ndarray Computed field array of shape (...,4). """ return dotmv(pmat, field, out=out)
def jonesvec(pol, phi=0.): """Returns a normalized jones vector from an input length 2 vector., Additionaly, you can use this function to view the jones vactor in rotated coordinate frame, defined with a rotation angle phi. Numpy broadcasting rules apply. Example ------- >>> jonesvec((1,1j)) #left-handed circuar polarization array([0.70710678+0.j , 0. +0.70710678j]) """ pol = np.asarray(pol, CDTYPE) assert pol.shape[-1] == 2 norm = (pol[..., 0] * pol[..., 0].conj() + pol[..., 1] * pol[..., 1].conj())**0.5 pol = pol / norm[..., np.newaxis] pol = np.asarray(pol, CDTYPE) r = rotation_matrix2(-phi) return dotmv(r, pol)