r"""Calculate Pu+, Pu-, Pd+, Pd-.

    This is a modified version of empymod.kernel.fields(). See the original
    version for more information.
772091
def fields(depth, Rp, Rm, Gam, lrec, lsrc, zsrc, TM):
    

    """
    # Booleans if src in first or last layer; swapped if up=True
    first_layer = lsrc == 0
    last_layer = lsrc == depth.size-1

    # Depths; dp and dm are swapped if up=True
    if lsrc != depth.size-1:
        ds = depth[lsrc+1]-depth[lsrc]
        dp = depth[lsrc+1]-zsrc
    dm = zsrc-depth[lsrc]

    # Rm and Rp; swapped if up=True
    Rmp = Rm
    Rpm = Rp

    # Boolean if plus or minus has to be calculated
    if TM:
        plus = False
    else:
        plus = True

    # Sign-switches
    pm = 1     # + if plus=True, - if plus=False
    if not plus:
        pm = -1

    # Calculate down- and up-going fields
    for up in [False, True]:

        # No upgoing field if rec is in last layer or below src
        if up and (lrec == depth.size-1 or lrec > lsrc):
            Puu = np.full_like(Gam[:, :, lsrc, :], 0+0j)
            Pud = np.full_like(Gam[:, :, lsrc, :], 0+0j)
            continue
        # No downgoing field if rec is in first layer or above src
        if not up and (lrec == 0 or lrec < lsrc):
            Pdu = np.full_like(Gam[:, :, lsrc, :], 0+0j)
            Pdd = np.full_like(Gam[:, :, lsrc, :], 0+0j)
            continue

        # Swaps if up=True
        if up:
            dp, dm = dm, dp
            Rmp, Rpm = Rpm, Rmp
            first_layer, last_layer = last_layer, first_layer

        # Calculate Pu+, Pu-, Pd+, Pd-; rec in src layer; Eqs  81/82, A-8/A-9
        iGam = Gam[:, :, lsrc, :]
        if last_layer:  # If src/rec are in top (up) or bottom (down) layer
            Pd = Rmp*np.exp(-iGam*dm)
            Pu = np.full_like(Gam[:, :, lsrc, :], 0+0j)
        else:           # If src and rec are in any layer in between
            Ms = 1 - Rmp*Rpm*np.exp(-2*iGam*ds)
            Pd = Rmp/Ms*np.exp(-iGam*dm)
            Pu = Rmp/Ms*pm*Rpm*np.exp(-iGam*(ds+dp))

        # Store P's
        if up:
            Puu = Pu
            Pud = Pd
        else:
            Pdu = Pd
            Pdd = Pu

    # Return fields (up- and downgoing)
    return Puu, Pud, Pdu, Pdd
652344
def as_df():
    """"""
    records = []
    for sym, ele in vars(_this).items():
        if sym not in ["Element", "Isotope"] and not sym.startswith("_"):
            for k, v in vars(ele).items():
                if k.startswith("_") and k[1].isdigit():
                    records.append({kk: vv for kk, vv in vars(v).items() if not kk.startswith("_")})
    return _DF.from_records(records)
407111
def _compute_inter_event_std(self, C, C_PGA, pga1100, mag, vs30):
        
        tau_0 = self._compute_std_0(C['s3'], C['s4'], mag)
        tau_b_pga = self._compute_std_0(C_PGA['s3'], C_PGA['s4'], mag)
        delta_amp = self._compute_partial_derivative_site_amp(C, pga1100, vs30)

        std_inter = np.sqrt(tau_0 ** 2 + (delta_amp ** 2) * (tau_b_pga ** 2) +
                            2 * delta_amp * tau_0 * tau_b_pga * C['rho'])

        return std_inter
458019
def mod_categorical_expval(p):
    
    p = extend_dirichlet(p)
    return np.sum([p * i for i, p in enumerate(p)])
837860
def mod2pi(ts):
    
    return np.pi - np.mod(np.pi - ts, 2*np.pi)
114008
def obfn_reg(self):
        
        """

        rl1 = np.linalg.norm((self.wl1 * self.obfn_gvar()).ravel(), 1)
        rl21 = np.sum(np.sqrt(np.sum(self.obfn_gvar()**2,
                                     axis=self.cri.axisC)))
        return (self.lmbda*rl1 + self.mu*rl21, rl1, rl21)
1113853
def _c0(self):
        ""
        h_df = self.df / 2
        p, S = self._p, self.S

        return h_df * (logdet(S) + p * logtwo) + lpgamma(p, h_df)
907779
def multifield(self):
        """"""
        return bool(lib.EnvDeftemplateSlotMultiP(
            self._env, self._tpl, self._name))
853591
private void computeDefaultSerializableFields(DocEnv env,
                                                  ClassSymbol def,
                                                  ClassDocImpl cd) {
        for (Symbol sym : def.members().getSymbols(NON_RECURSIVE)) {
            if (sym != null && sym.kind == VAR) {
                VarSymbol f = (VarSymbol)sym;
                if ((f.flags() & Flags.STATIC) == 0 &&
                    (f.flags() & Flags.TRANSIENT) == 0) {
                    //### No modifier filtering applied here.
                    FieldDocImpl fd = env.getFieldDoc(f);
                    //### Add to beginning.
                    //### Preserve order used by old 'javadoc'.
                    fields.prepend(fd);
                }
            }
        }
    }
370120
def _get_delta(self, stds, dists):
        
        delta = np.maximum((0.1-0.001*dists.repi), np.zeros_like(dists.repi))
        return delta
1062815
def _multiply(self, x1, x2, out):
        """"""
        for spc, xp, yp, outp in zip(self.spaces, x1.parts, x2.parts,
                                     out.parts):
            spc._multiply(xp, yp, outp)
142692
def Rx(rads: Union[float, sympy.Basic]) -> XPowGate:
    """"""
    pi = sympy.pi if protocols.is_parameterized(rads) else np.pi
    return XPowGate(exponent=rads / pi, global_shift=-0.5)
823629
def p_obj_fields(self, p):
        
        p[0] = dict([p[1]] if len(p) == 2 else p[1] + [p[3]])
227706
def sum_combined_sp_values(self, other):
        
        if self.values is None:
            ret = IdValues()
        else:
            ret = IdValues({'0': sum(int(x) for x in
                                     {k: int(v) * int(other.values[k]) for k, v
                                      in self.values.items()}.values())})
        return ret
23758
def value(self):
    """"""
    return self._sum / tf.cast(self._count, self._dtype)
592423
def calc_uv(self, x, y, prev=False):
        """"""
        assert len(x) == self.N
        assert len(y) == self.N
        u = np.zeros(self.N, self.x.dtype)
        v = np.zeros(self.N, self.y.dtype)
        for n in xrange(self.N):
            # don't include self interaction
            if prev:
                x0 = self.xprev[np.r_[:n,n+1:self.N]]
                y0 = self.yprev[np.r_[:n,n+1:self.N]]
            else:
                x0 = self.x[np.r_[:n,n+1:self.N]]
                y0 = self.y[np.r_[:n,n+1:self.N]]
            s0 = self.s[np.r_[:n,n+1:self.N]]
            u0, v0 = self.uv_at_xy(x[n], y[n], x0, y0, s0)
            u[n] = u0.sum()
            v[n] = v0.sum()
        return u, v
220724
def _pdist(p):
        
        index, ref, ampl, cutoff, beta = p[:5]
        if cutoff == 0.0:
            pdist = models.PowerLaw(
                ampl * 1e30 * u.Unit("1/eV"), ref * u.TeV, index
            )
        else:
            pdist = models.ExponentialCutoffPowerLaw(
                ampl * 1e30 * u.Unit("1/eV"),
                ref * u.TeV,
                index,
                cutoff * u.TeV,
                beta=beta,
            )
        return pdist