MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Taking sums across indices of a

  • To: mathgroup at
  • Subject: [mg96060] Re: Taking sums across indices of a
  • From: "D. Grady" <D.C.Grady at>
  • Date: Wed, 4 Feb 2009 05:17:40 -0500 (EST)
  • References: <> <gm1dl7$3ns$>

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:

SparseTotal[a_?ArrayQ, {n_}] :=
   {\[Sigma] =
     (Append[#, n] &)@
      (Delete[#, n] &)@

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

SparseTotal[a_?ArrayQ, ns_List?(Length@# > 1 &)] :=
  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
means sum across index 1 and index 3 to yield a two-dimensional array,
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?


  • 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