[pythran] speaking of dispatch...

  • From: Neal Becker <ndbecker2@xxxxxxxxx>
  • To: pythran@xxxxxxxxxxxxx
  • Date: Thu, 23 Oct 2014 07:21:48 -0400

I just tried the following, which attempt to create a polymorphic function
'mag_sqr' that will dispatch to 'mag_sqr1' or 'mag_sqr2' depending on
whether array is real or complex.

It caused a huge c++ error trace, too large to include here
(gcc-4.8.3-7.fc20.x86_64)

import numpy as np

def conj (x):
    if x.dtype == complex:
        return x.real - 1j*x.imag
    else:
        return x

#pythran export mag_sqr1(float64[])
def mag_sqr1 (x):
    return x * x

#pythran export mag_sqr2(complex128[])
def mag_sqr2 (x):
    return x.real**2 + x.imag**2
    ## return np.real(x)**2 + np.imag(x)**2

#pythran export mag_sqr(float64[])
#pythran export mag_sqr(complex128[])
def mag_sqr (x):
    if x.dtype == np.complex:
        return mag_sqr2 (x)
    elif x.dtype == np.double:
        return mag_sqr1 (x)


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

Other related posts: