Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piecewise / Noncontiguous Ranges?

Is there any kind of object class for piecewise / noncontiguous ranges in Julia? For instance, I can create a regular range:

a = UnitRange(1:5)

But, if I wanted to combine this with other ranges:

b = UnitRange([1:5, 8:10, 4:7])

I cannot currently find an object or method. There is a PiecewiseIncreasingRanges module (https://github.com/simonster/PiecewiseIncreasingRanges.jl) that would be just what I want in this situation, except that it, as the name implies, requires the ranges be monotonically increasing.

The context for this is that I am looking for a way to create a compressed, memory efficient version of the SparseMatrixCSC type for sparse matrices with repeating rows. The RLEVectors module will work well to save space on the nonzerovalue vector in the sparse matrix class. Now though I am trying to find something to save space for the rowvalue vector that also defines the sparse matrix, since series of repeating rows will result in ranges of values in that vector (e.g. if the first 10 rows, or even certain columns in the first ten rows, of a sparse matrix are identical, then there will be a lot of 1:10 patterns in the row value vector).

More generally, I'd like a range such as the b object that I try to create above over which I could do an iterated loop, getting:

for (idx, item) in enumerate(hypothetical_object)
    println("idx: $idx, item: $item")
end

idx: 1, item: 1
idx: 2, item: 2
...
idx: 5, item: 5
idx: 6, item: 8
idx: 7, item: 9
idx: 8, item: 10
idx: 9, item: 4
idx: 10, item: 5
...

Update: One thing I'm considering, and will probably try implementing if I don't hear other suggestions here, will be to just create an array of PiecewiseIncreasingRange objects, one for each column in my sparse matrix. (I would probably also then break the nonzero value vector into an array of separate pieces, one for each column of my sparse matrix as well). This would at least be relatively simple to implement. I don't have a good sense off the bat how this would compare in terms of computational efficiency to the kind of object I am searching for in this question. I suspect that memory requirements would be about the same.

like image 997
Michael Ohlrogge Avatar asked Jan 31 '16 04:01

Michael Ohlrogge


1 Answers

To loop over a sequence of ranges (or other iterators), you can use the chain function in the Iterators.jl package.

For example: using Iterators b = chain(1:5, 8:10, 4:7) for i in b println(i) end outputs the elements of each range.

like image 74
SGJ Avatar answered Sep 29 '22 09:09

SGJ