matrix - How to efficiently store and manipulate sparse binary matrices in Octave? -
matrix - How to efficiently store and manipulate sparse binary matrices in Octave? -
i'm trying manipulate sparse binary matrices in gnu octave, , it's using way more memory expect, , relevant sparse-matrix functions don't behave way want them to. see this question higher-than-expected sparse-matrix storage in matlab, suggests matrix should consume even more memory, helped explain (only) part of situation.
for sparse, binary matrix, can't figure out way octave not store array of values (they're implicitly 1
, need not stored). can done? octave seems consume memory values array.
a trimmed-down illustration demonstrating situation: create random sparse matrix, turn "binary":
mys=spones(sprandn(1024,1024,.03)); nnz(mys), whos mys
shows situation. consumed size consistent storage mechanism outlined in aforementioned answer , expanded below, if spones()
creates array of storage-class double
, if indices 32-bit (i.e., totalstoragesize - rowindices - columnindices == numnonzero*sizeof(double) -- unnecessarily storing these values (all 1
s double
s) on half of total memory consumed 3%-sparse object.
after messing (for long) while composing question, discovered partial workarounds, i'm going "self-answer" (only) part of question continuity (hopefully), didn't figure out adequate reply main question:
how create efficiently-stored ("no-/implicit-values") binary matrix in octave?
additional background on storage format follows...
the octave docs storage format sparse matrices uses format compressed sparse column (csc). seems imply storing next arrays (expanding on aforementioned answer, canonical yale format labels
, tweaks column-major order):
a
), number-of-nonzeros (nnz) entries of storage-class size; row numbers (ia
), nnz entries of index size (hopefully int64
maybe int32
); start of each column (ja
), number-of-columns-plus-1 entries of index size) in case, binary-only storage, hope there's way avoid storing array (a
), can't figure out.
full disclosure: noted above, composing question, discovered workaround cut down memory usage, i'm "self-answering" part of here, still isn't satisfying, i'm still listening improve actual reply storage of sparse binary matrix without trivial, bloated, unnecessary values array...
to binary-like value out of number-like value , reduce memory usage in case, utilize "logical" storage, created logical(x)
. example, building above,
logicalmys = logical(mys);
creates sparse bool matrix
, takes less memory (1-byte logical
rather 8-byte double
values array).
adding more info whos
info using whos_line_format
helps illuminate situation: default string includes 5 of 7 properties (see docs more). i'm using format string
whos_line_format(" %a:4; %ln:6; %cs:16:6:1; %rb:12; %lc:8; %e:10; %t:20;\n")
to add together display of "elements", , "type" (which distinct "class").
with that, whos mys logicalmys
shows
attr name size bytes class elements type ==== ==== ==== ===== ===== ======== ==== mys 1024x1024 391100 double 32250 sparse matrix logicalmys 1024x1024 165350 logical 32250 sparse bool matrix
so shows distinction between sparse matrix
, sparse bool matrix
. however, total memory consumed logicalmys
consistent storing array of nnz booleans (1-byte) -- is:
in numbers,
165350 - 32250*4 - 1025*4 == 32250
. so we're still storing 32250 elements, of 1
. further, if set 1 of 1
-elements zero, reduces reported storage! time, try: pick nonzero element, e.g., (42,1)
, 0 it: logicalmys(42,1) = 0;
whos
it!
my hope correct, , clarifies things might interested. comments, corrections, or actual answers welcome!
matrix octave sparse-matrix binary-matrix
Comments
Post a Comment