How can I use fabs from math.h in my cython code.

789 views
Skip to first unread message

Vishal

unread,
Feb 18, 2011, 1:11:03 AM2/18/11
to cython-users
Hello,

I have some code where I need to get the absolute value of a double
precision number. Currently, the Python builtin abs is used. Which
makes it a little slower. I would like to use the fabs() function as
defined in math.h.
To do this I tried to add the signature of this function to math.pxd
in libc folder of Cython, as th last indented line of the pxd file,
like so:

double fabs(double x)

then in my pyx file I did the following:

from libc.math cimport fabs

This line starts giving the following error when i want to see the
annotated output.

AttributeError: 'UnspecifiedType' object has no attribute
'create_from_py_utility_code'

if I remove that line, everything goes fine?

Where to go from here?


Thanks and best regards,
Vishal Sapre

Vishal

unread,
Feb 18, 2011, 1:23:48 AM2/18/11
to cython-users
Hello,

I just found out that I had the following already present:
from math import log
and on removing this, everything went well.

Thanks and best regards,
Vishal Sapre
--
Thanks and best regards,
Vishal Sapre
---
"So say...Day by day, in every way, I am getting better, better and better !!!"
"A Strong and Positive attitude creates more miracles than anything else."
"Life is 10% how you make it, and 90% how you take it"
"Diamond is another piece of coal that did well under pressure”
"May we do good and not evil. May we find forgiveness for ourself and forgive others. May we share freely, never taking more than we give."
"बहुजन हिताय, बहुजन सुखाय (Benefit for most people, Happiness for most people.)"

Stefan Behnel

unread,
Feb 18, 2011, 1:26:39 AM2/18/11
to cython...@googlegroups.com
Vishal, 18.02.2011 07:11:

> Hello,
>
> I have some code where I need to get the absolute value of a double
> precision number. Currently, the Python builtin abs is used. Which
> makes it a little slower. I would like to use the fabs() function as
> defined in math.h.

Hmm, right, math.pxd is actually lacking a lot of declarations:

http://en.wikipedia.org/wiki/Math.h


> To do this I tried to add the signature of this function to math.pxd
> in libc folder of Cython, as th last indented line of the pxd file,
> like so:
>
> double fabs(double x)

Sure, should work.


> then in my pyx file I did the following:
>
> from libc.math cimport fabs
>
> This line starts giving the following error when i want to see the
> annotated output.
>
> AttributeError: 'UnspecifiedType' object has no attribute
> 'create_from_py_utility_code'

Hmm, I assume that's not all you put into your .pyx, is it? That certainly
works for me.


> if I remove that line, everything goes fine?
>
> Where to go from here?

Could you provide some example code that fails for you? Try to strip down
your code to a short example, that will usually allow you to see what goes
wrong.

Stefan

Stefan Behnel

unread,
Feb 18, 2011, 1:39:06 AM2/18/11
to cython...@googlegroups.com
Stefan Behnel, 18.02.2011 07:26:
> Vishal, 18.02.2011 07:11:

>> I have some code where I need to get the absolute value of a double
>> precision number. Currently, the Python builtin abs is used. Which
>> makes it a little slower. I would like to use the fabs() function as
>> defined in math.h.
>
> Hmm, right, math.pxd is actually lacking a lot of declarations:
>
> http://en.wikipedia.org/wiki/Math.h

BTW, I think it shouldn't make your code slower to use the Python
functions. Applying math.abs() to a typed C floating point value should
call fabs().

http://trac.cython.org/cython_trac/ticket/662

(we've discussed this idea on cython-dev ages ago, but I couldn't find a
ticket for it)

Stefan

Reply all
Reply to author
Forward
0 new messages