Renormalizes fern.numP to avoid overflow
160946
public void renormalizeP() {
		int targetMax = maxP/20;

		for( int i = 0; i < managers.length; i++ ) {
			TldFernManager m = managers[i];
			for( int j = 0; j < m.table.length; j++ ) {
				TldFernFeature f = m.table[j];
				if( f == null )
					continue;
				f.numP = targetMax*f.numP/maxP;
			}
		}
		maxP = targetMax;
	}
1209908
private void computeDir(double[] dir, double[] fg, QNInfo qn)
      throws SurpriseConvergence {
    System.arraycopy(fg, 0, dir, 0, fg.length);

    int mmm = qn.size();
    double[] as = new double[mmm];

    for (int i = mmm - 1; i >= 0; i--) {
      as[i] = qn.getRho(i) * ArrayMath.innerProduct(qn.getS(i), dir);
      plusAndConstMult(dir, qn.getY(i), -as[i], dir);
    }

    // multiply by hessian approximation
    qn.applyInitialHessian(dir);

    for (int i = 0; i < mmm; i++) {
      double b = qn.getRho(i) * ArrayMath.innerProduct(qn.getY(i), dir);
      plusAndConstMult(dir, qn.getS(i), as[i] - b, dir);
    }

    ArrayMath.multiplyInPlace(dir, -1);
  }
1189229
private void forward(TrellisNode[][] trellis, double[] scaling) {
        int n = trellis.length; // length of sequence

        TrellisNode[] t0 = trellis[0];
        for (int i = 0; i < numClasses; i++) {
            TrellisNode t0i = t0[i];
            TreePotentialFunction pi = potentials[i];
            if (numFeatures <= 0) {
                for (int k = t0i.age; k < pi.trees.size(); k++) {
                    t0i.scores[0] += pi.eta * pi.trees.get(k).predict(t0i.samples[0]);
                }
            } else {
                for (int k = t0i.age; k < pi.trees.size(); k++) {
                    t0i.scores[0] += pi.eta * pi.trees.get(k).predict(t0i.sparseSamples[0]);
                }                
            }

            t0i.expScores[0] = Math.exp(t0i.scores[0]);
            t0i.alpha = t0i.expScores[0];
            t0i.age = pi.trees.size();
        }

        // Normalize alpha values since they increase quickly
        scaling[0] = 0.0;
        for (int i = 0; i < numClasses; i++) {
            scaling[0] += t0[i].alpha;
        }
        for (int i = 0; i < numClasses; i++) {
            t0[i].alpha /= scaling[0];
        }

        for (int t = 1; t < n; t++) {
            TrellisNode[] tt = trellis[t];
            TrellisNode[] tt1 = trellis[t-1];
            for (int i = 0; i < numClasses; i++) {
                TrellisNode tti = tt[i];
                TreePotentialFunction pi = potentials[i];
                tti.alpha = 0.0;
                for (int j = 0; j < numClasses; j++) {
                    if (numFeatures <= 0) {
                        for (int k = tti.age; k < pi.trees.size(); k++) {
                            tti.scores[j] += pi.eta * pi.trees.get(k).predict(tti.samples[j]);
                        }
                    } else {
                        for (int k = tti.age; k < pi.trees.size(); k++) {
                            tti.scores[j] += pi.eta * pi.trees.get(k).predict(tti.sparseSamples[j]);
                        }
                    }

                    tti.expScores[j] = Math.exp(tti.scores[j]);
                    tti.alpha += tti.expScores[j] * tt1[j].alpha;
                }
                tti.age = pi.trees.size();
            }

            // Normalize alpha values since they increase quickly
            scaling[t] = 0.0;
            for (int i = 0; i < numClasses; i++) {
                scaling[t] += tt[i].alpha;
            }
            for (int i = 0; i < numClasses; i++) {
                tt[i].alpha /= scaling[t];
            }
        }
    }
668687
def _nanstd(array, axis=None, ddof=0):
    """"""

    if isinstance(axis, tuple):
        array = _move_tuple_axes_first(array, axis=axis)
        axis = 0
    return bottleneck.nanstd(array, axis=axis, ddof=ddof)
103476
function maxOrMin( args, method ) {
            var m, n,
                i = 0;

            if ( isArray( args[0] ) ) args = args[0];
            m = new BigNumber( args[0] );

            for ( ; ++i < args.length; ) {
                n = new BigNumber( args[i] );

                // If any number is NaN, return NaN.
                if ( !n.s ) {
                    m = n;
                    break;
                } else if ( method.call( m, n ) ) {
                    m = n;
                }
            }

            return m;
        }
10341
function rnbinom(size, prob) {
      var rg;

      if (size <= 0 || prob <= 0 || prob > 1) {
         return function() { return NaN; };
      }
      if (prob === 1) { return function() { return 0; }; }
      rg = rgamma(size, (1 - prob) / prob);
      return function() { return rpois(rg())(); };
   }
635508
def rolling_fltr(dem, f=np.nanmedian, size=3, circular=True, origmask=False):
    
    print("Applying rolling filter: %s with size %s" % (f.__name__, size))
    dem = malib.checkma(dem)
    #Convert to float32 so we can fill with nan
    dem = dem.astype(np.float32)
    newshp = (dem.size, size*size)
    #Force a step size of 1
    t = malib.sliding_window_padded(dem.filled(np.nan), (size, size), (1, 1))
    if circular:
        if size > 3:
            mask = circular_mask(size)
            t[:,mask] = np.nan
    t = t.reshape(newshp)
    out = f(t, axis=1).reshape(dem.shape)
    out = np.ma.fix_invalid(out).astype(dem.dtype)
    out.set_fill_value(dem.fill_value)
    if origmask:
        out = np.ma.array(out, mask=np.ma.getmaskarray(dem))
    return out
26651
public void learnFern(boolean positive, ImageRectangle r) {

		float rectWidth = r.getWidth();
		float rectHeight = r.getHeight();

		float c_x = r.x0+(rectWidth-1)/2f;
		float c_y = r.y0+(rectHeight-1)/2f;

		for( int i = 0; i < ferns.length; i++ ) {

			// first learn it with no noise
			int value = computeFernValue(c_x, c_y, rectWidth, rectHeight,ferns[i]);
			TldFernFeature f = managers[i].lookupFern(value);
			increment(f,positive);
		}
	}
169905
def _slow_calcbinflux(len_binwave, i_beg, i_end, avflux, deltaw):
    
    binflux = np.empty(shape=(len_binwave, ), dtype=np.float64)
    intwave = np.empty(shape=(len_binwave, ), dtype=np.float64)

    # Note that, like all Python striding, the range over which
    # we integrate is [first:last).
    for i in range(len(i_beg)):
        first = i_beg[i]
        last = i_end[i]
        cur_dw = deltaw[first:last]
        intwave[i] = cur_dw.sum()
        binflux[i] = np.sum(avflux[first:last] * cur_dw) / intwave[i]

    return binflux, intwave
127666
private void calcNoOfNodes(Node node) {
        if (node instanceof Parent) {
            if (((Parent) node).getChildrenUnmodifiable().size() != 0) {
                ObservableList<Node> tempChildren = ((Parent) node).getChildrenUnmodifiable();
                noOfNodes += tempChildren.size();
                for (Node n : tempChildren) { calcNoOfNodes(n); }
            }
        }
    }
1374683
def _call(self, x):
        """"""

        sum_constr = abs(x.ufuncs.sum() / self.diameter - 1) <= self.sum_rtol

        nonneq_constr = x.ufuncs.greater_equal(0).asarray().all()

        if sum_constr and nonneq_constr:
            return 0
        else:
            return np.inf
448991
def log_(t, alpha=1, beta=1):
    """"""
    return beta * np.log(alpha * np.maximum(0, t - 10 * beta) + 1)
1344977
def _renorm(args: Dict[str, Any]):
    """"""
    state = _state_shard(args)
    # If our gate is so bad that we have norm of zero, we have bigger problems.
    state /= np.sqrt(args['norm_squared'])
1035759
def correct(self, temp, we_t):
        
        if not PIDTempComp.in_range(temp):
            return None

        n_t = self.cf_t(temp)

        if n_t is None:
            return None

        we_c = we_t * n_t

        return we_c
450137
private void performDynamicStep() {
        // initially look for singular values of zero
        if( findingZeros ) {
            if( steps > 6 ) {
                findingZeros = false;
            } else {
                double scale = computeBulgeScale();
                performImplicitSingleStep(scale,0,false);
            }
        } else {
            // For very large and very small numbers the only way to prevent overflow/underflow
            // is to have a common scale between the wilkinson shift and the implicit single step
            // What happens if you don't is that when the wilkinson shift returns the value it
            // computed it multiplies it by the scale twice, which will cause an overflow
            double scale = computeBulgeScale();
            // use the wilkinson shift to perform a step
            double lambda = selectWilkinsonShift(scale);

            performImplicitSingleStep(scale,lambda,false);
        }
    }
805756
def _symm_current(C):
    """"""
    nans = np.isnan(C)
    C[nans] = np.einsum('klij', C)[nans]
    return C
405717
def IntGreaterThanZero(n):
    """"""
    try:
        n = int(n)
    except:
        raise ValueError("%s is not an integer" % n)
    if n <= 0:
        raise ValueError("%d is not > 0" % n)
    else:
        return n