Thanks for the hint. I thought about Cython myself but I was unable to
get even the slightest speed gain out of it.
Here is the equivalent Cython code with the timing and setup.py. I typed
(I think). Am I missing something obvious?
Cheers
Robert
On 25.07.2011 01:38, Joon Ro wrote:
> For those cases where you cannot vectorize the operation, numpy is
> usually does not help much.
> Try using Cython. You will be able to compile the part of the code and
> the loop will be much faster (can be more than 100 times faster).
>
> http://docs.cython.org/
>
> -Joon
>
>
> On Sun, 24 Jul 2011 18:10:14 -0500, Robert Elsner
> <[email protected]> wrote:
>
>> Boiled it down a bit more to only include code that actually takes time.
>> First time around I found the other variant more instructive because it
>> shows the discrepancy between the DCT and the loop but might be
>> confusing. Thus here the bare minimum that correctly calculates the
>> coefficients of the first derivative from the coefficients of the
>> Chebyshev polynomials.
>>
>> Cheers
>> Robert
>>
>> On 25.07.2011 00:43, Robert Elsner wrote:
>>> Hey Everybody,
>>>
>>> I am approximating the derivative of nonperiodic functions on [-1,1]
>>> using Chebyshev polynomials. The implementation is straightforward and
>>> works well but is painfully slow. The routine wastes most of its
>>> time on
>>> a trivial operation (see comment in the code)
>>> Unfortunately the spectral coefficients are calculated recursively and
>>> thus I haven't found a way to speed up the code. I am aware of list
>>> comprehensions, the speed advantage of calling native numpy functions
>>> etc. But no luck yet finding an alternate formulation that speeds up
>>> the
>>> calculation. I attached a stripped down variant of the code. I am very
>>> grateful for tips or directions where to look for a solution (well I
>>> know writing a small C extension might be the way to go but Id love to
>>> have speedy Python)
>>>
>>> Note: The DCT takes almost no time compared to the statement in the
>>> loop.
>>>
>>> Cheers
>>> Robert
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> NumPy-Discussion mailing list
>>> [email protected]
>>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>
>
from optimization import chebyshev_derivative_gc
import timeit
# Timing code
t = timeit.Timer( chebyshev_derivative_gc )
print t.repeat( 3, 1000 )
import numpy as np
cimport numpy as np
# Fixing datatypes
DTYPE = np.float
ctypedef np.float_t DTYPE_t
def chebyshev_derivative_gc():
""" Calculate the first derivative of a function
"""
cdef int m = 6
cdef int N = 64
cdef np.ndarray[DTYPE_t, ndim = 2] a = np.random.random( ( m, N ) )
cdef np.ndarray[DTYPE_t, ndim = 2] b = np.zeros( (m, N), dtype = DTYPE )
cdef int l,g,h,j
l = 2
g = 1
h = 0
b[:, N - l] = l * ( N - g ) * a[:, N - g]
for j in range( N - l, h, -g ):
# The next line is the offender - try by commenting out.
b[:, j - g] = ( l * j ) * a[:, j] + b[:, j + g]
"""
Created on 25.07.2011
@author: robert
"""
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
ext_modules = [Extension( "optimization", ["optimization.pyx"] )]
setup(
name = 'Testing Cython speedups',
cmdclass = {'build_ext': build_ext},
ext_modules = ext_modules
)
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion