Functional programming style - readability - question ...

Is there a way (different use of operators, for example) to make the following more readable?

Total[Transpose[aDataStructure],{3,1,2}],2]

My concern is that I anticipate the 1st argument to Total become more complex and then the farther away the second argument moves, visually. For readability purposes I’d like the 2nd argument to Total not to “fall off the end”.

Yes, I could repackage this use of Total in my own function, but it’s going to be an inner, inner tunction call, and I prefer to make the Mathematica functions as explicit as possible. And, various uses of Total[#,2]& seem not right either.

The good news is that this all works great, so it’s just the readability I’m concerned about.

Thank you,
--Mark

Your input seems to be incomplete, can you please fix it? I assume you mean Total[ Transpose[array, {3,1,2}], 2 ] where ‘array’ is a 3D array. Is this correct?

If this is indeed what you mean, the simpler version is

Total[a, {2, 3}]

Yes, thank you.

But, I’m looking at a 4D array, next, etc.

-- Mark

Ah, hadn’t explored arguments for Total sufficiently. Cool!
-- Mark

Generally, for an N-D tensor, Total[tensor, {k}] will sum along the kth index.

I remember now why I didn’t use Total the way you suggest.

Total[array, {2,3}] and Total[array,{1,2}] do what I need, but Total[array,{1,3}] sums over everything when I need it to sum over levesl 1 and 3, only. I’m tryting to write the code “homogeneously”, and so don’t want to handle the latter case as an exception. Thus, Total[Transpose[ the three pairs of levels],2] does the right thing.

Any more good ideas?

It’s very good to be reminded of this functionality.

Thank you,
--Mark