python - Vectoriced iterative fixpoint search -


i have function converge fixpoint, e.g. f(x)= (x-a)/2+a. have function find fixpoint through repetive invoking of function:

def find_fix_point(f,x):     while f(x)>0.1:         x = f(x)     return x 

which works fine, want vectoriced version;

def find_fix_point(f,x):     while (f(x)>0.1).any():         x = f(x)     return x 

however quite inefficient, if of instances need 10 iterations , 1 needs 1000. fast method remove `x have been found?

the code can use numpy or scipy.

one way solve use recursion:

def find_fix_point_recursive(f, x):     ind = x > 0.1     if ind.any():         x[ind] = find_fix_point_recursive(f, f(x[ind]))     return x 

with implementation, call f on points need updated.

note using recursion avoid having check x > 0.1 time, each call working on smaller , smaller arrays.

%timeit x = np.zeros(10000); x[0] = 10000; find_fix_point(f, x) 1000 loops, best of 3: 1.04 ms per loop  %timeit x = np.zeros(10000); x[0] = 10000; find_fix_point_recursive(f, x) 10000 loops, best of 3: 141 µs per loop 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -