Geometric backoff sequence w/ jitter
174543
def backoff_delays(start, stop, factor=2.0, jitter=False):
    
    cur = start
    while cur <= stop:
        if jitter:
            yield cur - (cur * random.random())
        else:
            yield cur
        cur = cur * factor
825973
@Override
    public void backward() {
        Tensor x = modInX.getOutput();
        Tensor w = modInW.getOutput();
        {
            Tensor tmp = new Tensor(yAdj); // copy
            tmp.elemDivide(w);
            correctForZeros(tmp);
            modInX.getOutputAdj().elemAdd(tmp);
        }
        {
            Tensor tmp = new Tensor(w); // copy
            tmp.fill(s.one());
            tmp.elemDivide(w);
            tmp.elemDivide(w);
            tmp.multiply(s.fromReal(-1));
            tmp.elemMultiply(yAdj);
            tmp.elemMultiply(x); 
            correctForZeros(tmp);
            modInW.getOutputAdj().elemAdd(tmp);
        }
    }
536047
def standard_backtrack(self):
        

        gradY = self.eval_grad()  # Given Y(f), this updates computes gradY(f)

        maxiter = self.L_maxiter

        iterBTrack = 0
        linesearch = 1
        while linesearch and iterBTrack < maxiter:

            self.proximal_step(gradY)  # Given gradY(f), L, this updates X(f)

            f = self.obfn_f(self.var_x())
            Dxy = self.eval_Dxy()
            Q = self.obfn_f(self.var_y()) + \
                self.eval_linear_approx(Dxy, gradY) + \
                (self.L / 2.) * np.linalg.norm(Dxy.flatten(), 2)**2

            if f <= Q:
                linesearch = 0
            else:
                self.L *= self.L_gamma_u

            iterBTrack += 1

        self.F = f
        self.Q = Q
        self.iterBTrack = iterBTrack
        # Update auxiliary sequence
        self.combination_step()
1326411
def staggered_positions(n_channels):
    """"""
    i = np.arange(n_channels - 1)
    x, y = (-1) ** i * (5 + i), 10 * (i + 1)
    pos = np.flipud(np.r_[np.zeros((1, 2)), np.c_[x, y]])
    return pos
117438
private void ssMergeBackward(int PA, int first, int middle, int last, int buf,
                                 int depth) {
        // PA, first, middle, last, buf are pointers in SA
        int p1, p2;// pointers in SA
        int a, b, c, bufend;// pointers in SA
        int t, r, x;

        bufend = buf + (last - middle) - 1;
        ssBlockSwap(buf, middle, last - middle);

        x = 0;
        if (SA[bufend] < 0) {
            p1 = PA + ~SA[bufend];
            x |= 1;
        } else {
            p1 = PA + SA[bufend];
        }
        if (SA[middle - 1] < 0) {
            p2 = PA + ~SA[middle - 1];
            x |= 2;
        } else {
            p2 = PA + SA[middle - 1];
        }
        for (t = SA[a = last - 1], b = bufend, c = middle - 1; ; ) {
            r = ssCompare(p1, p2, depth);
            if (0 < r) {
                if ((x & 1) != 0) {
                    do {
                        SA[a--] = SA[b];
                        SA[b--] = SA[a];
                    }
                    while (SA[b] < 0);
                    x ^= 1;
                }
                SA[a--] = SA[b];
                if (b <= buf) {
                    SA[buf] = t;
                    break;
                }
                SA[b--] = SA[a];
                if (SA[b] < 0) {
                    p1 = PA + ~SA[b];
                    x |= 1;
                } else {
                    p1 = PA + SA[b];
                }
            } else if (r < 0) {
                if ((x & 2) != 0) {
                    do {
                        SA[a--] = SA[c];
                        SA[c--] = SA[a];
                    }
                    while (SA[c] < 0);
                    x ^= 2;
                }
                SA[a--] = SA[c];
                SA[c--] = SA[a];
                if (c < first) {
                    while (buf < b) {
                        SA[a--] = SA[b];
                        SA[b--] = SA[a];
                    }
                    SA[a] = SA[b];
                    SA[b] = t;
                    break;
                }
                if (SA[c] < 0) {
                    p2 = PA + ~SA[c];
                    x |= 2;
                } else {
                    p2 = PA + SA[c];
                }
            } else {
                if ((x & 1) != 0) {
                    do {
                        SA[a--] = SA[b];
                        SA[b--] = SA[a];
                    }
                    while (SA[b] < 0);
                    x ^= 1;
                }
                SA[a--] = ~SA[b];
                if (b <= buf) {
                    SA[buf] = t;
                    break;
                }
                SA[b--] = SA[a];
                if ((x & 2) != 0) {
                    do {
                        SA[a--] = SA[c];
                        SA[c--] = SA[a];
                    }
                    while (SA[c] < 0);
                    x ^= 2;
                }
                SA[a--] = SA[c];
                SA[c--] = SA[a];
                if (c < first) {
                    while (buf < b) {
                        SA[a--] = SA[b];
                        SA[b--] = SA[a];
                    }
                    SA[a] = SA[b];
                    SA[b] = t;
                    break;
                }
                if (SA[b] < 0) {
                    p1 = PA + ~SA[b];
                    x |= 1;
                } else {
                    p1 = PA + SA[b];
                }
                if (SA[c] < 0) {
                    p2 = PA + ~SA[c];
                    x |= 2;
                } else {
                    p2 = PA + SA[c];
                }
            }
        }
    }
244709
def _run_sequence(self, sequence):
        '''
        
        '''
        self._check_pause()
        self._pre_test()
        session_data = self.target.get_session_data()
        self._test_info()
        resp = None
        for edge in sequence:
            if edge.callback:
                edge.callback(self, edge, resp)
            session_data = self.target.get_session_data()
            node = edge.dst
            node.set_session_data(session_data)
            resp = self._transmit(node)
        return self._post_test()
154216
def _one_iteration(self, F, Ybus, V, Vm, Va, pv, pq, pvpq):
        
        J = self._build_jacobian(Ybus, V, pv, pq, pvpq)

        # Update step.
        dx = -1 * spsolve(J, F)
#        dx = -1 * linalg.lstsq(J.todense(), F)[0]

        # Update voltage vector.
        npv = len(pv)
        npq = len(pq)
        if npv > 0:
            Va[pv] = Va[pv] + dx[range(npv)]
        if npq > 0:
            Va[pq] = Va[pq] + dx[range(npv, npv + npq)]
            Vm[pq] = Vm[pq] + dx[range(npv + npq, npv + npq + npq)]

        V = Vm * exp(1j * Va)
        Vm = abs(V) # Avoid wrapped round negative Vm.
        Va = angle(V)

        return V, Vm, Va
1080530
def _jitter(c, magnitude:uniform):
    ""
    c.flow.add_((torch.rand_like(c.flow)-0.5)*magnitude*2)
    return c
659844
def hinit(func, x, t, pos_neg, f0, iord, hmax, rtol, atol, args):
    
    sk = atol + rtol * np.fabs(x)
    dnf = np.sum(np.square(f0 / sk), axis=0)
    dny = np.sum(np.square(x / sk), axis=0)

    h = np.sqrt(dny / dnf) * 0.01

    h = np.min([h, np.fabs(hmax)])
    h = custom_sign(h, pos_neg)

    # perform an explicit Euler step
    xx1 = x + h * f0
    f1 = np.array(func(xx1, t[0] + h, *args))

    # estimate the second derivative of the solution
    der2 = np.sum(np.square((f1 - f0) / sk), axis=0)
    der2 = np.sqrt(der2) / h

    # step size is computed such that h ** iord * max_d(norm(f0), norm(der2)) = 0.01
    der12 = np.max([np.fabs(der2), np.sqrt(dnf)])
    h1 = np.power(0.01 / der12, 1.0 / iord)

    h = np.min([100.0 * np.fabs(h), np.min([np.fabs(h1), np.fabs(hmax)])])

    return custom_sign(h, pos_neg), f0, f1, xx1
56941
def ystep(self):
        
        """

        if self.opt['NonNegCoef'] or self.opt['NoBndryCross']:
            Y1 = self.block_sep1(self.Y)
            if self.opt['NonNegCoef']:
                Y1[Y1 < 0.0] = 0.0
            if self.opt['NoBndryCross']:
                for n in range(0, self.cri.dimN):
                    Y1[(slice(None),)*n +
                       (slice(1-self.D.shape[n], None),)] = 0.0
            self.block_sep1(self.Y)[:] = Y1
193206
def backward(self, y):
        
        if self.squeeze:
            y = y[None, :, :]
        ind = np.tril_indices(self.N)
        return np.vstack([y_i[ind] for y_i in y])
751781
def backoff(error_class, retry_limit=9, &block)
      next_sleep = BACKOFF_INITIAL_SLEEP
      begin
        yield
      rescue error_class
        raise if retry_limit == 0
        sleep(next_sleep)
        next_sleep *= 2
        retry_limit -= 1
        retry
      end
    end
1015523
def _add_jitter(self, vec):
        
        if self.scatterchartdata.jitter == 0 or self.scatterchartdata.jitter is None:
            return vec
        return vec + np.random.rand(1, len(vec))[0] * self.scatterchartdata.jitter
483798
function sqSegSegDist(x0, y0, x1, y1, x2, y2, x3, y3) {
    var ux = x1 - x0;
    var uy = y1 - y0;
    var vx = x3 - x2;
    var vy = y3 - y2;
    var wx = x0 - x2;
    var wy = y0 - y2;
    var a = ux * ux + uy * uy;
    var b = ux * vx + uy * vy;
    var c = vx * vx + vy * vy;
    var d = ux * wx + uy * wy;
    var e = vx * wx + vy * wy;
    var D = a * c - b * b;

    var sc, sN, tc, tN;
    var sD = D;
    var tD = D;

    if (D === 0) {
        sN = 0;
        sD = 1;
        tN = e;
        tD = c;
    } else {
        sN = b * e - c * d;
        tN = a * e - b * d;
        if (sN < 0) {
            sN = 0;
            tN = e;
            tD = c;
        } else if (sN > sD) {
            sN = sD;
            tN = e + b;
            tD = c;
        }
    }

    if (tN < 0.0) {
        tN = 0.0;
        if (-d < 0.0) sN = 0.0;
        else if (-d > a) sN = sD;
        else {
            sN = -d;
            sD = a;
        }
    } else if (tN > tD) {
        tN = tD;
        if ((-d + b) < 0.0) sN = 0;
        else if (-d + b > a) sN = sD;
        else {
            sN = -d + b;
            sD = a;
        }
    }

    sc = sN === 0 ? 0 : sN / sD;
    tc = tN === 0 ? 0 : tN / tD;

    var cx = (1 - sc) * x0 + sc * x1;
    var cy = (1 - sc) * y0 + sc * y1;
    var cx2 = (1 - tc) * x2 + tc * x3;
    var cy2 = (1 - tc) * y2 + tc * y3;
    var dx = cx2 - cx;
    var dy = cy2 - cy;

    return dx * dx + dy * dy;
}
316204
@Override
    public void backward() {
        Tensor x = modInX.getOutput();
        Tensor tmp = new Tensor(yAdj); // copy
        tmp.elemDivide(x);
        modInX.getOutputAdj().elemAdd(tmp);
    }
909652
public static int cudnnSoftmaxBackward(
        cudnnHandle handle, 
        int algo, 
        int mode, 
        Pointer alpha, 
        cudnnTensorDescriptor yDesc, 
        Pointer y, 
        cudnnTensorDescriptor dyDesc, 
        Pointer dy, 
        Pointer beta, 
        cudnnTensorDescriptor dxDesc, 
        Pointer dx)
    {
        return checkResult(cudnnSoftmaxBackwardNative(handle, algo, mode, alpha, yDesc, y, dyDesc, dy, beta, dxDesc, dx));
    }
375053
private void backpropagate() {
        for (int l = net.length; --l > 0;) {
            backpropagate(net[l], net[l - 1]);
        }
    }