Suppose I have two large packed arrays a
and b
, that respectively contain the position of the elements of a square matrix (i.e. a
has the explicit form {{1,2},{2,3},{3,4},...}) and the value for each matrix element (i.e. b
has the explicit form {0.2938,0.091,-0.4984,...}).
Now, is there a way to build a sparse array from a
and b
without having unpacking in the internal process of creating the sparse array ?
Refining the question + example below: The usual SparseArray[a->b]
command seems have some unpacking in the process although a and b are unaffected. Really, what I'm concerned about is : suppose you have memA=ByteCount[a]
and memB=ByteCount[b]
.
In the process of constructing c=SparseArray[a->b]
I do not want my kernel to suck up all my memory by unpacking a or b (or doing some other unpacking in the internal code), i.e. the kernel memory required during the process should not exceed something like memA+memB, which seems like a very natural requirement to me... In eldo's answer below, it seems there is no unpacking since the arrays considered are too small. Considering larger arrays will lead to the above mentioned problem.
Is there a simple solution (or not so simple) to this issue ?
*Specific example:
On["Packing"]
a=Table[{i, i + 1}, {i, 1, 10000000}]; (* Position of the elements *)
b=Table[RandomReal[], {i, 1, 10000000}]; (* Value of the elements *)
c=SparseArray[{a -> b}]; (* Generating the sparse array *)
ByteCount[a]
ByteCount[b]
ByteCount[c]
80000168
80000168
160000952
Plus you get the following messages :
FromPackedArray::punpack: Unpacking array with dimensions {10000000,2} in call to Rule. >> FromPackedArray::punpack: Unpacking array with dimensions {10000000} in call to Rule. >> FromPackedArray::punpack1: Unpacking array with dimensions {10000000}.>>
So it seems there is some unpacking in the internal process. In this specific examples the kernel memory during the process goes up by 1 GB, although the final results takes only 160 MB...
$Version
8.0 for Mac OS X x86 (64-bit) (October 5, 2011)
On["Packing"]
is incorrect. It just switches on unpacking warnings. It does not AFAIK influence packing itself. $\endgroup$