473,394 Members | 1,821 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,394 software developers and data experts.

Numarray - Using Integers Efficiently for Boolean Values

Below is the code to/from Boolean arrays and Unsigned integers. On my
Pentium 4, functions such as "bitwise_and" are 32 times faster when run
on 32-bit integers instead of the entire-byte-consuming-Boolean.

Good luck all:-)

uint32Mask =
numarray.array([0x00000001,0x00000002,0x00000004,0x00000008, \

0x00000010,0x00000020,0x00000040,0x00000080, \

0x00000100,0x00000200,0x00000400,0x00000800, \

0x00001000,0x00002000,0x00004000,0x00008000, \

0x00010000,0x00020000,0x00040000,0x00080000, \

0x00100000,0x00200000,0x00400000,0x00800000, \

0x01000000,0x02000000,0x04000000,0x08000000, \

0x10000000,0x20000000,0x40000000,0x80000000], numarray.UInt32)
uint32MaskInner = numarray.copy.deepcopy(uint32Mask)
uint32MaskInner.shape = [32,1]
uint32MaskOuter = numarray.copy.deepcopy(uint32Mask)
uint32MaskOuter.shape = [1,32]

def BoolToUInt32(myArr):
if myArr.size()%32 != 0:
print "Size is: ", myArr.size()
return
numarray.matrixmultiply(numarray.reshape(myArr,[myArr.size()/32,32]),uint32MaskInner).flat

def UInt32ToBool(myArr,destination=None):
if destination == None:
destination = numarray.zeros([myArr.size()*32],numarray.Bool)
#return
numarray.bitwise_and(numarray.reshape(myArr,[myArr.size(),1]),uint32MaskOuter).flat
#else:
destination.shape = [myArr.size(),32]

numarray.bitwise_and(numarray.reshape(myArr,[myArr.size(),1]),uint32MaskOuter,destination)
destination.shape = [destination.size()]
return destination

Test of code:
import numarray
gram.UInt32ToBool(ni) array([1, 0, 0, ..., 0, 0, 0], type=Bool) numarray.all(numarray.equal(n,gram.UInt32ToBool(gr am.BoolToUInt32(n)))) 1 n array([1, 0, 0, ..., 0, 0, 0], type=Bool) n.shape (1024,) numarray.all(numarray.equal(n,gram.UInt32ToBool(gr am.BoolToUInt32(n))))

1

Feb 16 '06 #1
0 1221

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

Similar topics

7
by: 2mc | last post by:
I am finding out all kinds of ways to do things in NumPy through the many suggestions I have received. It's exciting. Thanks to all who have replied on my other threads. I'm still having...
11
by: grv | last post by:
So it is supposed to be very fast to have an array of say 5 million integers stored in a binary file and do a = numarray.fromfile('filename', (2, 2, 2)) numarray.add(a, 9, a) but how is that...
6
by: Michael Drumheller | last post by:
(If you're not interested in NumArray, please skip this message.) I am new to NumArray and I wonder if someone can help me with array-indexing. Here's the basic situation: Given a rank-2 array...
3
by: Mizrandir | last post by:
I'd like to subclass numarray's array. I'd like to add some methods and override others like __init__. I don't know how to do this and haven't found help searching the manual or the web, can...
5
by: Edward C. Jones | last post by:
Suppose arr is a two dimensional numarray array. Suppose we do the following: def rowsort(arr): a = arr.tolist() a.sort() return numarray.array(a) Can this sort be done efficiently in...
13
by: Jeff Melvaine | last post by:
I note that I can write expressions like "1 << 100" and the result is stored as a long integer, which means it is stored as an integer of arbitrary length. I may need to use a large number of...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
6
by: comp.lang.php | last post by:
I'm involved in a rather nasty debate involving a strange issue (whereby the exasperated tell me to RTFM even after my having done so), where this is insanely possible: print_r(is_int('1'));...
1
by: andrewfelch | last post by:
I need to be able to store thousands of one-million-length bit arrays. The first operations use the & operator. But eventually I must perform an inner product with a one-million-length integer...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
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...

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.