[pythran] dimension-independent functions?

  • From: Neal Becker <ndbecker2@xxxxxxxxx>
  • To: pythran@xxxxxxxxxxxxx
  • Date: Fri, 5 Sep 2014 07:01:27 -0400

What is the best way to write a dimension-independent function?  For
example,
suppose I want to apply the function "sqr" element-wise to an array of any
number
of dimensions N.

I see one way is to explicitly instantiate the function for each desired
number of dimensions:

import numpy as np
#pythran export sqr(float64[])
#pythran export sqr(float64[][])
def sqr (x):
    size = x.shape[0]
    y = np.empty_like (x)
    for i in range (size):
        y[i] = x[i] * x[i]
    return y

Is there a better way?

-- 
*Those who don't understand recursion are doomed to repeat it*

Other related posts: