Mmm, I'm not sure if you got the idea. In your code, I'm talking about
removing 'func2', and passing to 'cuadrature' an instance of MyFunc
(and implement __call__ in MyFunc), like this

cdef class MyFunc:
       cdef double (*f)(double)
       def __call__(self, a):
             return array([self.f(x) for x in a])

myfunc = MyFunc()
myfunc.set_f(f)
val, err = quadrature(myfunc, a, b)

See the attached files for a working example. Just run test_functor.py
(uses pyximport, no need to build yourself functor.pyx -> functor.so)
to make sure all is is working, then look at the fine code ;-).


On Fri, Oct 17, 2008 at 5:05 AM, Ondrej Certik <[EMAIL PROTECTED]> wrote:
> On Fri, Oct 17, 2008 at 5:28 AM, Lisandro Dalcin <[EMAIL PROTECTED]> wrote:
>> Ondrej, perhaps I'm missing something, but... Why not to implement
>> __call__ on MyFunc cdef class? IMHO, this way your code would looks
>> much better. Moreover, it would be faster.
>
> Because __call__ cannot be a cdef function and def function cannot return 
> void*.
>
> Ondrej
> _______________________________________________
> Cython-dev mailing list
> [email protected]
> http://codespeak.net/mailman/listinfo/cython-dev
>



-- 
Lisandro Dalcín
---------------
Centro Internacional de Métodos Computacionales en Ingeniería (CIMEC)
Instituto de Desarrollo Tecnológico para la Industria Química (INTEC)
Consejo Nacional de Investigaciones Científicas y Técnicas (CONICET)
PTLC - Güemes 3450, (3000) Santa Fe, Argentina
Tel/Fax: +54-(0)342-451.1594

Attachment: functor.pyx
Description: Binary data

import pyximport; pyximport.install()

import numpy as np
from functor import Functor

mysin = Functor('sin')
mycos = Functor('cos')
mytan = Functor('tan')

x = np.linspace(0,np.pi/4,50)

y1 = mysin(x)
y2 = mycos(x)
y3 = mytan(x)

assert isinstance(y1, np.ndarray)
assert isinstance(y2, np.ndarray)
assert isinstance(y3, np.ndarray)

assert y1.dtype == np.float64
assert y1.dtype == np.float64
assert y1.dtype == np.float64

assert np.allclose(y1, np.sin(x))
assert np.allclose(y2, np.cos(x))
assert np.allclose(y3, np.tan(x))
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev

Reply via email to