Online Book Reader

Home Category

Beautiful Code [48]

By Root 5205 0
a dictionary also requires the hash value for each key, so caching the value saves having to rehash all the keys when resizing.

We don't keep track directly of the number of slots in the table, but derive it instead as needed from ma_mask. When looking up the entry for a key, slot = hash & mask is used to figure out the initial slot for a particular hash value. For instance, the hash function for the first entry generated a hash of –1549758592, and –1549758592 mod 31 is 0, so the entry is stored in slot 0.

Because the mask is needed so often, we store it instead of the number of slots. It's easy to calculate the number of slots by adding 1, and we never need to do so in the most speed-critical sections of code.

ma_fill and ma_used are updated as objects are added and deleted. ma_used is the number of keys present in the dictionary; adding a new key increases it by 1, and deleting a key decreases it by 1. To delete a key, we make the appropriate slot point to a dummy key; ma_fill therefore remains the same when a key is deleted, but may increase by 1 when a new key is added. (ma_fill is never decremented, but will be given a new value when a dictionary is resized.)

Multidimensional Iterators in NumPy > Key Challenges in N-Dimensional Array Operations

19. Multidimensional Iterators in NumPy

Travis E. Oliphant

Numpy is an optional package for the python language that provides a powerful N-dimensional array object. An N-dimensional array is a data structure that uses N integers, or indices, to access individual elements. It is a useful model for a wide variety of data processed by a computer.

For example, a one-dimensional array can store the samples of a sound wave, a two-dimensional array can store a grayscale image, a three-dimensional array can store a color image (with one of the dimensions having a length of 3 or 4), and a four-dimensional array can store the value of pressure in a room during a concert. Even higher-dimensional arrays are often useful.

NumPy provides an environment for the mathematical and structural manipulation of arrays of arbitrary dimensions. These manipulations are at the heart of much scientific, engineering, and multimedia code that routinely deals with large amounts of data. Being able to perform these mathematical and structural manipulations in a high-level language can considerably simplify the development and later reuse of these algorithms.

NumPy provides an assortment of mathematical calculations that can be done on arrays, as well as providing very simple syntax for structural operations. As a result, Python (with NumPy) can be successfully used for the development of significant and fast-performing engineering and scientific code.

One feature that allows fast structural manipulation is that any subregion of a NumPy array can be selected using the concept of slicing. In Python, a slice is defined by a starting index, a stopping index, and a stride, using the notation start:stop:stride (inside square brackets).

For example, suppose we want to crop and shrink a 656x498 image to a 160x120 image by selecting a region in the middle of the image. If the image is held in the NumPy array, im, this operation can be performed using:

im2=im[8:-8:4, 9:-9:4]

An important feature of NumPy, however, is that the new image selected in this way actually shares data with the underlying image. A copy is not performed. This can be an important optimization when calculating with large data sets where indiscriminate copying can overwhelm the computing resources.

19.1. Key Challenges in N-Dimensional Array Operations

In order to provide fast implementations of all mathematical operations, NumPy implements loops (in C) that work quickly over an array or several arrays of any number of dimensions. Writing such generic code that works quickly on arrays of arbitrary dimension can be a mind-stretching task. It may be easy to write a for loop to process all the elements of a one-dimensional array, or two nested for loops to process all the elements of a two-dimensional array. Indeed, if

Return Main Page Previous Page Next Page

®Online Book Reader