1. Introduction

I am using an array of dimension 3 (might become more) to store some values. I would like to implement a function that takes as argument the array and a couple of numbers smaller than the array dimension and returns a standard matrix (dim=2) which is obtained by reduction or "projection" (I don't really know how to call it) of the array along all the "other" dimensions. First a quick example with an array of dimension/depth 3

2. Definitions

Let $d_i \in \mathbb{N}, \quad i=1,\dots,N\quad$ be the sublengths of each of the dimensions.

What I will call in the following the projection of the array along the dimension $q$ is the following surjective function :
$$
\textrm{For } q\leq N \in \mathbb{N}_0,\quad P(M,q) : \mathbb{R}^{\Pi d_i} \rightarrow\mathbb{R}^{\frac{\Pi d_i}{d_q}} : M(n_1,\dots,n_N) \mapsto \sum_{j=1}^{n_q}M(n_1,\dots,n_{q-1},j,n_{q+1},\dots,n_N)
$$

3. Goal

The goal is to write a function that takes any multidimensional array as well as a list of all the dimensions that will be "kept" in the final array. For the sake of any representation, the obtained array should be of dimension $\leq$ 2 but I would like to stay as general as I (we) could.

The dimensions ordering should simply be the one used to implement the array, and the code should be consistent in order to keep the dimensions in the same order at any time.

4. Implementation

ProjectedColumns[len_,cols_]:= Complement[Range[len],cols]

ProjectedColumns is a function that returns a list of the dimensions to project, where cols is the list of dimensions to be kept and len is the dimension of the array.

CompoundProjection is a function that will project all the dimensions sequentially in order to arrive to the final result (The projection is commutative).

SingleProjection[data_, dimnumber_] := ???

Now what I need and I don't manage to get is the function that will actually perfom a one-dimensional projection. In my mind, I would need to have a number of Do loops which equals the dimension of the array.

In that case, I simply parse through the array and do the summation over the one I'm interested in. Is it possible to set up such a structure with all the commands that Mathematica offers and that I probably don't know ?

5. In summary

Has anyone followed this nonsense ?

Is there a Mathematica command that makes what I want directly ?

(If Yes then No to the previous questions, then I'm happy)

Is there a way to improve the correctness or elegance of what I did ?

Is there a way to intricate a dynamic number of Do loops ?

Or, in general, is there another way to achieve the goal described in 3.

Reading this I though initially that you could actually get all the desired functionality out of just Total and complement, however you run into trouble with it if you want to remove for example dimension 1 and 4 in a 6 dimensional structure, and then you need to reorder the arguments, or call Total continually while recalculating the dimensions on each pass.
–
jVincentJul 27 '12 at 18:27

@jVincent Yes, for that it's necessary to call Total two times.
–
SzabolcsJul 27 '12 at 19:03

1

Total seems to be a good tool indeed. I've come up with a way of using it recursively. You don't need to relaculate anything in the process. You just apply the reduction beginning by the deepest dimensions and then it works fine.
–
PschoofsJul 30 '12 at 9:08

I may be misunderstanding what you need, however consider this. If you have an structure of nested lists such as your data, then summing across the deepest list is easily accomplished using Map[Total,data,{-2}]. So, as long as you want to remove dimensions from "the back" you are good to go. And if we need to remove for example the second dimension, then you can just transpose it to the back, and then remove it:

Map[Total,Transpose[data,{1,3,2}],{-2}]

Summing out more then one dimension would then just be pushing them all to the back, and applying Total at a higher level and summing all the way down. Which can be done as:

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.