The documentation mentions that "the elements in SparseArray
need not be numeric", although "the individual elements of a sparse array cannot themselves be lists." I was wondering what type of objects can be put in a sparse array, as it also appears that sparse arrays themselves cannot be. That said, the documentation also says that "ArrayReshape
works with SparseArray
objects," which would suggest that sparse arrays should be able to contain sparse arrays (or lists or vice versa).
What I am trying to do is make a sparse block-banded (Toeplitz) matrix, where the block matrix has the same banding as the blocks (nested or fractal-like). Ideally, I would just be able to do:
Block[{k = 4, h},
h[ht_] :=
SparseArray[{Band[{1, 1}] -> ht, Band[{2, 1}] -> ht/4,
Band[{1, 2}] -> ht/4}, {k, k}];
ArrayFlatten[
h[h[1]]]]
However, it gives the error that "insufficient positions are available in the Band[<>]
to fit all the values SparseArray[<>]
..."
It seems I need some way to get pretend it is a scalar. It seems wrapping it in a Hold
allows it to form the sparse array h[Hold[h[1]]
, but then to actually ReleaseHold
requires converting the sparse array to a normal one. That is ReleaseHold[h[Hold[h[1]]]]
will not actually release the hold on the internal elements of the outer sparse array (and thus, ArrayFlatten
ing it doesn't give the desired result). So I need to ArrayFlatten[ReleaseHold[Normal[h[Hold[h[1]]]]]]
The trouble with using Normal
to unsparsify the matrix (so that ReleaseHold
will work) is that it isn't feasible for large k
. There are a few other approaches I tried (such as using an undefined f
or converting the inner sparse matrices ToString
s), however they all have the same trouble of requiring Normal
to undo this.
So to reiterate the question, can SparseArray
take sparse arrays as elements and how can I get it to treat it as a scalar (when used with Band
)?