Benjamin C. Ide

New Package: GrayCodeIterator.jl

I’ve just published GrayCodeIterator.jl to the Julia package registry. It provides an iterator for all weight \(k\) binary vectors of length \(n\). The algorithm is based on this paper. This is useful for things like the Brouwer-Zimmermann algorithm for finding the minimum distance of a linear error correcting code. We’ve (myself and Eric Sabo) been using the package lately while developing a minimum distance finding function for a coding theory library that we’ve been working on.

You can install it from the Pkg repl, or via

1
2
using Pkg
Pkg.add("GrayCodeIterator")

and then the package can be loaded as normal: using GrayCodeIterator. Basic usage looks like the following where we’re looping over length 20 vectors of weight 6:

1
2
3
for g in GrayCode(20, 6)
    # do something with the vector g
end

If you aren’t planning to modify g in the loop, then on Julia version 1.8 or higher, you can get a a big speedup by letting the iterator mutate g in place:

1
2
3
for g in GrayCode(20, 6, mutate = true)
    # do something with the vector g, but don't mutate it
end

You can also give it a prefix vector. This allows you to only give vectors starting from a chosen node of the tree in Figure 1 of this paper. For example, collect(GrayCode(4, 2, [0, 1])) == [[0,1,0,1], [0,1,1,0]].