473,760 Members | 10,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4221
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_uint1 6(a, b):
c = a + b
c += c >16
return c & 0xFFFF

def compl_one_check sum(uint16s):
return reduce(compl_ad d_uint16, uint16s, 0x0000)

is somewhat better?

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

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

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

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

In [6]: hex(compl_one_c hecksum(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_uint1 6(a, b):
c = a + b
c += c >16
return c & 0xFFFF

def compl_one_check sum(uint16s):
return reduce(compl_ad d_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...@gm ail.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_uint1 6(a, b):
* * c = a + b
* * c += c >16
* * return c & 0xFFFF
def compl_one_check sum(uint16s):
* * return reduce(compl_ad d_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
2867
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 Vainio http://www.students.tut.fi/~vainio24
42
2620
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 original list is , I want to end up with . Here is the way I'm doing this now: def straightforward_collapse(myList):
8
2341
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 number 2. I actually wrote a java function that did this a couple of years ago, in school, as an excersise in "binary trees". I lost it, and most of my programming knowledge, but I figured perhaps there is a way to do this easily in python? It...
8
2997
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 buffer as I found out calling by capacity(). What if I do not need all that buffer space again want to reduce the size to a smaller one? I tried resize() passing a smaller size than the
43
3418
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
1273
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
2204
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 deal with images upload. When the user is selecting an image, I save it as such together with a thumbnail version.
0
9521
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9945
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.