473,404 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to "reduce" a numpy array using a costum binary function

I know there must be a simple method to do this.

I have implemented this function for calculating a checksum based on a
ones complement addition:

def complement_ones_checksum(ints):
"""
Returns a complements one checksum based
on a specified numpy.array of dtype=uint16
"""
result = 0x0
for i in ints:
result += i
result = (result + (result >16)) & 0xFFFF
return result

It works, but is of course inefficient. My prfiler syas this is the
99.9% botteleneck in my applicaiton.

What is the efficient numpy way to do this?

No need to dwelve into fast inlining of c-code or Fortran and stuff
like that although that may give further performance imporevements.
Nov 13 '08 #1
3 4203
It is always good to ask yourself a question.
I had forgooten about the reduce function

I guess this implementation

from numpy import *

def compl_add_uint16(a, b):
c = a + b
c += c >16
return c & 0xFFFF

def compl_one_checksum(uint16s):
return reduce(compl_add_uint16, uint16s, 0x0000)

is somewhat better?

But is it the best way to do it with numpy?

In [2]: hex(compl_add_uint16(0xF0F0, 0x0F0F))
Out[2]: '0xffff'

In [3]: hex(compl_add_uint16(0xFFFF, 0x0001))
Out[3]: '0x1'

In [5]: hex(compl_one_checksum(array([], dtype=uint16)))
Out[5]: '0x0'

In [6]: hex(compl_one_checksum(array([0xF0F0, 0x0F0F, 0x0001],
dtype=uint16)))
Out[6]: '0x1L'
Nov 13 '08 #2
Slaunger wrote:
It is always good to ask yourself a question.
I had forgooten about the reduce function

I guess this implementation

from numpy import *

def compl_add_uint16(a, b):
c = a + b
c += c >16
return c & 0xFFFF

def compl_one_checksum(uint16s):
return reduce(compl_add_uint16, uint16s, 0x0000)

is somewhat better?

But is it the best way to do it with numpy?
It's not too bad, if you only have 1D arrays to worry about (or you are only
concerned with reducing down the first axis). With a Python-implemented
function, there isn't much that will get you faster.

My coworker Ilan Schnell came up with a neat way to use PyPy's RPython->C
translation scheme and scipy.weave's ad-hoc extension module-building
capabilities to generate new numpy ufuncs (which have a .reduce() method)
implemented in pure RPython.

http://conference.scipy.org/proceedi.../full_text.pdf
http://svn.scipy.org/svn/scipy/branches/fast_vectorize/

If you have more numpy questions, please join us on the numpy mailing list.

http://www.scipy.org/Mailing_Lists

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Nov 13 '08 #3
On 13 Nov., 22:48, Robert Kern <robert.k...@gmail.comwrote:
Slaunger wrote:
It is always good to ask yourself a question.
I had forgooten about the reduce function
I guess this implementation
from numpy import *
def compl_add_uint16(a, b):
* * c = a + b
* * c += c >16
* * return c & 0xFFFF
def compl_one_checksum(uint16s):
* * return reduce(compl_add_uint16, uint16s, 0x0000)
is somewhat better?
But is it the best way to do it with numpy?

It's not too bad, if you only have 1D arrays to worry about (or you are only
concerned with reducing down the first axis). With a Python-implemented
function, there isn't much that will get you faster.
Yes, I only have 1D arrays in this particular problem.
>
My coworker Ilan Schnell came up with a neat way to use PyPy's RPython->C
translation scheme and scipy.weave's ad-hoc extension module-building
capabilities to generate new numpy ufuncs (which have a .reduce() method)
implemented in pure RPython.

* *http://conference.scipy.org/proceedi.../full_text.pdf
* *http://svn.scipy.org/svn/scipy/branches/fast_vectorize/
OK. Thanks. I am still a rather inexperienced SciPy and Python
programmer, and I must admit
that right now this seems to be in the advanced end for me. But, now
that you
mention weave I have given it a thought to reimplement my binary compl
add function
shown above using weave - if my profiler says that is where I should
be spending my
time optimizing.
If you have more numpy questions, please join us on the numpy mailing list.

* *http://www.scipy.org/Mailing_Lists
Thank you for directing me to that numpy specific mailing list.
I have been on scipy.org many times, but apparently overlooked
that very prominent link to mailing lists.

Slaunger
--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
* that is made terrible by our own mad attempt to interpret it as though it had
* an underlying truth."
* *-- Umberto Eco- Skjul tekst i anførselstegn -

- Vis tekst i anførselstegn -
Nov 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
42
by: Alan McIntyre | last post by:
Hi all, I have a list of items that has contiguous repetitions of values, but the number and location of the repetitions is not important, so I just need to strip them out. For example, if my...
8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
8
by: CoolPint | last post by:
Is there any way I can reduce the size of internal buffer to store characters by std::string? After having used a string object to store large strings, the object seems to retain the large...
43
by: M-One | last post by:
See subject: how do I calloc (and free the memory, if that's not free(my_bytes);) this? TIA!
2
by: José Joye | last post by:
Hello, When openning a solution, all projects are expended in the Solution Explorer, is there a magic key to "reduce" all the project? Thanks, José
3
by: boeledi | last post by:
Dear All, (First of all this is not a c# piece of code but it does not really matter). I would really appreciate if someone could help me. I am developing an ASP.NET web site and I have to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.