Pick is a widely used function in Mathematica. A minor enhancement was made to this function in the recent release; it can now work directly with BitVector datastructure. In this article, let us go over these two with some examples.
Let us start with Pick first. It allows us to select from a list of elements based on some criterion. Look at these examples:
The first example selects week days based on a list of seven {True, False} elements. In the second example we use randomly generated five {0, 1} integers and use Pick to select those elements from {a, b, c, d, e} where the corresponding integer is 1 in the integer list.
The third example generates a Cyclic object based on another Cyclic object.
BitVector
BitVector is one of the several built-in data structures in Mathematica. These data structures are highly optimized in terms of space and operations.
There are standard operations that are defined on BitVector, for example, Setting, Clearing, and Inverting bits.
The function “Normal[]” is convenient for converting the BitVector object to a list of {True, False} elements for easy decoding. As you can guess, “True” corresponds to “1” inside the BitVector and “False” corresponds to “0”.
We can even “visualize” the BitVector object thus:
Using BitVector with Pick
Now that we know about both Pick and BitVector, let us see how we can combine both. How do we “Pick” those elements from a list based on the contents of a BitVector? Here are some examples:
Being able to use a BitVector object directly with Pick, instead of converting the object to a list of Boolean values is definitely cleaner and more efficient. This enhancement in the new release of Mathematica is welcome indeed!
Have a great weekend!
Recent Comments