[pythran] impressive!

  • From: Neal Becker <ndbecker2@xxxxxxxxx>
  • To: "pythran@xxxxxxxxxxxxx" <pythran@xxxxxxxxxxxxx>
  • Date: Fri, 1 Mar 2019 08:27:01 -0500

I'm really impressed that this version of the code works:  Here I'm using
'find_first', passing it a lambda expression
as the 2nd argument:

import numpy as np

#pythran export empirical(float[:], float, float)
def empirical(ds, alpha, x):
    sds = np.sort(ds)
    ds_to_the_alpha = sds**alpha
    fractions = ds_to_the_alpha / sum (ds_to_the_alpha)
    thresholds = np.cumsum(fractions)
    i = find_first (thresholds, lambda u: x < u)
    #i = np.searchsorted (thresholds, x)
    return i, thresholds, fractions

#pthran export find_first(float[:], bool (float))
def find_first (seq, pred):
    for i,x in enumerate (seq):
        if pred(x):
            return i
    return None

Other related posts: