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
data=Table[10 (i - 1) + 3 (j - 1) + k, {i, 2}, {j, 3}, {k, 4}]
--> data = {{{1, 2, 3, 4}, {4, 5, 6, 7}, {7, 8, 9, 10}},
{{11, 12, 13, 14}, {14, 15, 16, 17}, {17, 18, 19, 20}}}
My desired function would do, for example, the following
Myfunction[data,{1,2}] = {{10,22,34},{50,62,74}}
After this brief introduction, let me explain where I've arrived. First let's generalize a little bit, as it is my goal, eventually.
2. Definitions
Let $M \in \mathbb{R}^{\Pi d_i}$ be an array of dimension $N \in \mathbb{N}$.
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[data_, cols_] :=
Module[{len, vect},
len = Depth[data] - 1;
vect = ProjectedColumns[len, cols];
Do[
SingleProjection[data, vect[[i]]],
{i, Length[vect]}
]
]
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.
Map[Total , data, {2}]
, which will do what you seek withMyfunction
$\endgroup${1,2}
parameter in your first code snippet $\endgroup$