[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-11-01 Thread Dom Grigonis
Ok, I think I didn’t do a full justice. So actually, numba is fairly fast, given everything is precompiled. Predicates still make things slower, but not as much as I initially thought. However, the way your code is structured, it has to re-compile both predicate and target function (with new pr

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-11-01 Thread Dom Grigonis
Your comparisons do not paint correct picture. a) Most of time here is spent for array allocation and the actual time of the loop gets swallowed in the noise a) You do not test early stop - your benchmarks simply test full loop as condition is almost never hit - 5 standard deviations... Here is

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-11-01 Thread Neal Becker
Thanks Juan, this is really great! I plan to make use of this right away. On Wed, Nov 1, 2023 at 8:13 AM Juan Nunez-Iglesias wrote: > Have you tried timing things? Thankfully this is easy to test because the > Python source of numba-jitted functions is available at jitted_func.py_func. > > In [

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-11-01 Thread Juan Nunez-Iglesias
Have you tried timing things? Thankfully this is easy to test because the Python source of numba-jitted functions is available at jitted_func.py_func. In [23]: @numba.njit ...: def _first(arr, pred): ...: for i, elem in enumerate(arr): ...: if pred(elem): ...:

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-31 Thread Dom Grigonis
This results in a very slow code. The function calls of _pred = numba.njit(pred) are expensive and this sort of approach will be comparable to pure python functions. This is only recommended for sourcing functions that are not called frequently, but rather have a large computational content wit

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-31 Thread Bill Ross
Could a sub-python level spin up extra threads (processes)? Search could benefit easily. I switched back to Java for number crunching because one gets to share memory without using OS-supplied shared memory. Maybe put a JVM behind python, or do python in the JVM? Bill -- Phobrain.com On 202

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-31 Thread Juan Nunez-Iglesias
If you add a layer of indirection with Numba you can get a *very* nice API: @numba.njit def _first(arr, pred): for i, elem in enumerate(arr): if pred(elem): return i def first(arr, pred): _pred = numba.njit(pred) return _first(arr, _pred) This even works with lamb

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-31 Thread Lev Maximov
I've implemented such functions in Cython and packaged them into a library called numpy_illustrated It exposes the following functions: find(a, v) # returns the index of the first occurrence of v in a first_above(a, v) # returns the index of the fi

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-30 Thread Dom Grigonis
I juggled a bit and found pretty nice solution using numba. Which is probably not very robust, but proves that such thing can be optimised while retaining flexibility. Check if it works for your use cases and let me know if anything fails or if it is slow compared to what you used. first_true_s

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-30 Thread rosko37
An example with a 1-D array (where it is easiest to see what I mean) is the following. I will follow Dom Grigonis's suggestion that the range not be provided as a separate argument, as it can be just as easily "folded into" the array by passing a slice. So it becomes just: idx = first_true(arr, con

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-26 Thread Dom Grigonis
If such issue is at numpy level, eg xor, which tests for number truth value is equal to n: xor([1, 1, 0], 2) == True xor([1, 0, 0], 2) == False I try to use builtin iterator functions for efficiency, such as combination of filter + next. If, however, the problem is at numpy level, I find `numba

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-26 Thread Ilhan Polat
It's typically called short-circuiting or quick exit when the target condition is met. if you have an array a = np.array([-1, 2, 3, 4, , 1]) and you are looking for a true/false result whether anything is negative or not (a < 0).any() will generate a bool array equal to and then check all

[Numpy-discussion] Re: Function that searches arrays for the first element that satisfies a condition

2023-10-26 Thread Dom Grigonis
Could you please give a concise example? I know you have provided one, but it is engrained deep in verbose text and has some typos in it, which makes hard to understand exactly what inputs should result in what output. Regards, DG > On 25 Oct 2023, at 22:59, rosko37 wrote: > > I know this que