       Re: Taking sums across indices of a

• To: mathgroup at smc.vnet.net
• Subject: [mg96060] Re: Taking sums across indices of a
• Date: Wed, 4 Feb 2009 05:17:40 -0500 (EST)
• References: <200901301042.FAA06408@smc.vnet.net> <gm1dl7\$3ns\$1@smc.vnet.net>

```Right, I should really read all the comments before asking silly
questions.  Thanks very much Carl and Bobby, this was a big help.  I
spent way too much time wrapping these suggestions up into a function,
and here it is for anyone else who might need it:

ClearAll[SparseTotal];
SparseTotal[a_?ArrayQ, {n_}] :=
With[
{\[Sigma] =
(Append[#, n] &)@
(Delete[#, n] &)@
Range@Length@Dimensions@a},

Transpose[a, Ordering@\[Sigma]].
SparseArray[_ -> 1, Dimensions[a][[n]]]
];

SparseTotal[a_?ArrayQ, ns_List?(Length@# > 1 &)] :=
Fold[
SparseTotal,
a,
List /@ (Reverse@Sort@ns)
]

This thing works almost exactly like Total, EXCEPT that Total uses
standard level specifications and SparseTotal just takes a list of
indices to sum across.  So for example, if W is a four-dimensional
sparse array, then
SparseTotal[W,{1,3}]
means sum across index 1 and index 3 to yield a two-dimensional array,
and
Total[W,{1,3}]
means sum across levels 1, 2, and 3 to yield a one-dimensional array.
This should probably be changed since it could be confusing, but it's
good enough for me.

The solution I posted initially takes about 30 seconds to run on my
data; this solution takes half a second.  Again, thanks very much.

Now, not to sound annoyed, but it would have been the work of ten
minutes to export this array, do the calculation in Octave, and
reimport it to Mathematica.  Conveniently, Mathematica can neither
read nor write Octave's sparse array format, so instead I end up
spending two days figuring out a way to perform this basic computation
without overflowing my computer's memory.  I know that Total can work
on ragged arrays, and in that sense it's more general than Octave's
'sum' function, but it seems ridiculous that Total will expand its
argument to a normal array in every case.  If I try to evaluate
Total[ SparseArray[...], {2}]
there's no need whatsoever to turn that in to a normal array.  Does
anyone have any idea why Total doesn't work with SparseArray?

-Daniel

```

• Prev by Date: Re: symbolic matrix manipulation
• Next by Date: Re: Bug in HypergeometricPFQ[{}, {1/2, 3/4, 3/4}, x]]
• Previous by thread: Split Screen Redux
• Next by thread: Re: Re: Taking sums across indices of a SparseArray