2
$\begingroup$

Is there a way of having arrays which have, so to speak, two different depths $m,n$? From the point of view of memory usage, it would be the same as an array of depth $m+n$, but I would like to obtain and treat the two depths and the respective dimension lists separately.

To use a mathematical analogy: a tensor (in geometry) of type $(1,2)$ is different than a tensor of type $(0,3)$, it has the same number of components, but the operations defined on the two differ.

How can I do this in Mathematica? Is there such a thing as a "doubly-deep" array? Or can I make a data structure, C-like, in which there is the actual array, plus a couple of int variables to keep track of the two different depths?

Any advice would be welcome.

Update: Here is an example. Let $X$, $Y$, $Z$ be binary sets (= of 2 elements each). A transition matrix is a matrix whose rows sum to one. A transition matrix $A:X\times Y\to Z$ and a transition matrix $B:X\to Y\times Z$ would be then cubical arrays with the same number of entries (8), same depths (3) and dimensions (2,2,2), but different normalization. In particular: $$ \sum_{x,y}A(x,y,z)=1 \quad\forall z\;, $$ but: $$ \sum_{x}B(x,y,z)=1 \quad\forall y,z\;. $$ For example I would like to define a command, say "CheckNormal", that checks if $A$ and $B$ are correctly normalized. But the normalizations differ. It's as if $A$ had "depth" (2,1), and "dimensions" ((2,2),(2)), and $B$ had depth (1,2), and dimensions ((2),(2,2)). In both cases, I want the sums to be executed only over the first set of indices, and I would like that information (= how many indices are in the first set) to be encoded in $A$ and $B$ (not in the function CheckNormal).

$\endgroup$
  • $\begingroup$ ArrayReshape[ ]? $\endgroup$ – Dr. belisarius Oct 6 '15 at 14:16
  • $\begingroup$ @belisarius No, that works, for example, for turning a 2x3 matrix into a 3x2 one, or 6x1. This is different, as matrices they would just look the same. $\endgroup$ – geodude Oct 6 '15 at 14:20
  • $\begingroup$ Could you give us a practical example of the structure of an array that would fit your requirements? ArrayReshape as suggested by @belisarius is not limited to two dimensions... $\endgroup$ – MarcoB Oct 6 '15 at 14:37
  • $\begingroup$ Transpose works in any number of dimensions, and allows any permutation of the different levels. $\endgroup$ – bill s Oct 6 '15 at 14:57
  • 1
    $\begingroup$ My feeling is that the best way to implement this is via the operations on the arrays, not in the arrays themselves; you would need different operations (CheckNormalTypeA, CheckNormalTypeB). Alternatively, use customs Heads typeA and tybeB that wrap your (equal-dimensional) arrays, and then overload the definition of CheckNormal using something like CheckNormal[a_typeA] := ..., etc. However, it certainly seems like ArrayReshape is the way to go, based on your ((2,2),(2) to ((2),(2,2)) example. $\endgroup$ – march Oct 6 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.