# How to label a cube

I can't come up with a clever way to label a cube's faces, such that if I turned it in one of four directions, "Up", "Down", "Left", "Right", I'd know the resulting face by applying a function to the current face number, e.g., right(f) = f + 1, left(f) = f - 1. Any ideas?

Please excuse my terrible ASCII cube. I needn't label them 1 - 6.

Cube:

     ____
/ 5 /|   3 (back)
/___/_|
4  | 1 |2|
|___|/

6 (underside)

-
Well, neither of the suggestions you listed can work because they don't have any fixed points, but rotating a cube always fixes two faces. What kind of mathematical functions do you have in mind? Rotating a cube is already a pretty mathematical function and there are several mathematical ways to represent rotation. –  Qiaochu Yuan May 14 '11 at 9:26
I'm honestly not sure. I'm a programmer and this amusing thought just popped into my head. Obviously a simple list or map data structure can solve it, but I wonder if there's a more elegant way. –  ash May 14 '11 at 9:34
@Qiaochu: I agree that rotating a cube "up", down, left, right, always fixes two faces. When the axis of rotation is one of the three lines intersecting the centers of two opposing faces, then the two opposing faces through which the axis intersects are fixed. But not all rotations (rigid motions) of the cube fix two faces. For example, rotating a cube about any of the four long diagonals of a cube permutes all six faces, as does rotating a cube about any of the "axes" which intersects the midpoints of opposing edges of the cube. –  amWhy May 14 '11 at 17:02
@Amy: yes, I meant to restrict myself to the rotations being described in the OP. –  Qiaochu Yuan May 15 '11 at 3:20

Here's something that works, but it takes a little time to explain why it works. Label the top and bottom faces $\infty$ and $0$, respectively, and label the others $1, i, -1, -i$ in counterclockwise order looking down on the cube (where $i$ is the imaginary unit). If $z$ is the label of the current face at a particular location, then

• Right rotation is given by $z \mapsto -iz$,
• Left rotation is given by $z \mapsto iz$,
• Up rotation is given by $z \mapsto \frac{z + 1}{z - 1}$,
• Down rotation is given by $z \mapsto - \frac{z - 1}{z + 1}$.

If you know how to compute with complex numbers you should be able to verify all of this, although to really understand how this works you'll have to learn about the Riemann sphere, stereographic projection, and fractional linear transformations.

You can also explicitly write down the quaternions that represent the rotations you want. But really, none of this seems particularly useful for programming applications (in this special case; the quaternions are very useful for more general rotations).

More generally, you are asking a kind of group-theoretic question, asking whether there are elegant ways to describe the group action of the symmetry group of the cube on the faces (equivalently, on the centers of the faces). There are several ways to do this; one can use permutations, which is the standard way to describe group actions, but since our symmetries act by rotations we can also look at ways to represent the special orthogonal group $\text{SO}(3)$ of rotations. Above I make use of two such representations: one identifies it with the projective unitary group $\text{PU}(2)$, whereas another identifies it with the projective symplectic group $\text{PSp}(1)$ of unit quaternions modulo $-1$.

But again, I don't think any of this is particularly useful for a programming application.

-
@Qiaochu: I'm a little confused about your rotation functions. Say we start with z=1 as the face: then the upward rotation, as you've fined it, takes face z = 1 to where face <infinity> had been (on top). Likewise for "downward" rotation: face 1 goes to the bottom (replacing 0). However, you describe left rotation in terms of "counter-clockwise" (rotation), which moves 1 to the right, and right-rotation (meaning the rotation is clockwise?) moves 1 to the left. I guess it would be less confusing if "down" was framed as a forward rotation, "up" as a backward rotation. –  amWhy May 14 '11 at 17:10
The next mathematical questions would then be "is there a function over integers?" and then "is there a polynomial integer function (maybe with mod)?' –  Mitch May 14 '11 at 17:41
@Qiaochu: yes, that was my point about a table being a function; instead of a programming implementatino that would uses cases (e.g. "left(n) = if (n == 1) then return 6; else if (n==2) return 3;..."), you could encode it in an arithmetic function "left(n) = 6*[n==1] + 3*[n==2] + ..." where '[n==k]' is the indicator function for 'n==k' (something involving $e^{k \pi/6}$). –  Mitch May 15 '11 at 4:17
@Qiaochu: I'd guess that the OP really wants, -for any face-, a function that gives the label of the face to the left/right/etc. E.g., for the face labeled 0, the 'left' function should give -some- face to the left, presumably not the same face. Sure, the OP said '...if I turned it...' which translates to 'rotate', but I don't think that's what the OP really means. I think they want 4 functions, ranging over all the labels, such that $L(L(L(L(x)))) = x$, $R(R(R(R(x)))) = x$,$L(R(x))=x$ ..., and maybe $R(U(L(x)))=x$,$L(B(R(x)))=x$. I don't think there's a single group rotation operator here. –  Mitch May 15 '11 at 14:29
@Qiaochu: I think this kind of question is a fairly common request among non-mathematicians. Most people don't learn (or just forget) that a function is an arbitrary map and does not need to be specified by a single formula invoking only arithmetical operations and known special functions. This sort of thinking persists even when exposed to programming — somehow, a mathematical expression is regarded as simpler than a table lookup; whereas the truth is that a table lookup would probably be faster than any interpolation function you could dream up. –  Zhen Lin May 17 '11 at 7:20

This is a PDF of my attempt to express rotations of the cube in terms of permutations of the faces. Trying to reproduce it here would cost us both some time.

-
Please help with undeletion and reopen votes, e.g. here. If we don't reverse these then the site will soon no longer allow "elementary" questions. There are a long list of questions needing (single) undelete and reopen votes (see the 10K Tools, under Delete and Close). –  Bill Dubuque Aug 3 '14 at 16:53
@Bill Yes, most certainly! –  amWhy Aug 3 '14 at 17:14

A general function is simply a mapping, and the suggestion in your drawing can do that almost verbatim in any language:

$$\begin{eqnarray} R(1) &=& 2;\\ R(2) &=& 3;\\ R(3) &=& 4;\\ R(4) &=& 1;\\ R(5) &=& 2;\\ R(6) &=& 4;\\ \end{eqnarray}$$

That is, (in C style)

R(x) = (x==1) ? 2 : (x==2) ? 3 : (x==3) ? 4 : (x==4) ? 1 : (x==5) ? 2 : 4;


You could create a function for L, U, D, too (and presumably O for opposite), and this would work for any labeling. Note though that you should have a preferred orientation for each face/label (so that you can't rotate the cube keeping the same face forward). This you already knew.

What you are seeking is a labeling such that you can have a simpler function and corresponding labeling that fits, one that does not involve laboriously listing out the assigned values for each function and face. For some reason a case-by-case version is just not as elegant as a pre-calculus function. For example, to mimic the above function for $R$ and your labeling, you might use:

$$\begin{eqnarray} R(x) &=& (x \bmod 4) + 1\\ L(x) &=& (x-2 \bmod 4) + 1\\ U(x) &=& 5\\ D(x) &=& 6\\ \end{eqnarray}$$ (note that the range of mod is from 0 to 3).

These functions work very well for $x=1,2,3,4$ but not at all when $x=5,6$.

The difficulty as you've probably already noticed is how to assign labels and some other functions so that $U$ and $D$ are likewise easy to compute so that it will work for $x=5$ and $x=6$ too.

Again, one could use an arbitrary labeling and for each function come up with a polynomial that fits all 6 values. Suppose $R(5) = 2$ and $R(6) = 4$.

Lagrange Interpolation is a one possible technique that already does this. For $n$ $x,y$ points you fit an $n-1$ degree polynomial to get

$$\begin{eqnarray} R(x) &=& 2\cdot\frac{(x-2)(x-3)(x-4)(x-5)(x-6)}{(1-2)(1-3)(1-4)(1-5)(1-6)}\\ &+& 3\cdot\frac{(x-1)(x-3)(x-4)(x-5)(x-6)}{(2-1)(2-3)(2-4)(2-5)(2-6)}\\ &+& 4\cdot\frac{(x-1)(x-2)(x-4)(x-5)(x-6)}{(3-1)(3-2)(3-4)(3-5)(3-6)}\\ &+& 1\cdot\frac{(x-1)(x-2)(x-3)(x-5)(x-6)}{(4-1)(4-2)(4-3)(4-5)(4-6)}\\ &+& 2\cdot\frac{(x-1)(x-2)(x-3)(x-4)(x-6)}{(5-1)(5-2)(5-3)(5-4)(5-6)}\\ &+& 4\cdot\frac{(x-1)(x-2)(x-3)(x-4)(x-5)}{(6-1)(6-2)(6-3)(6-4)(6-5)}\\ \end{eqnarray}$$

(notice the cancellation of all terms to either 0 or 1) and after a lot of tedious calculation and simplification (or rather after Mathematica does it), you get:

$$R(x) = \frac{1}{120} (4800 - 10062 x - 7755 x^2 - 2635 x^3 - 405 x^4 - 23 x^5)$$

Using Zhen Lin's suggestion in the comment, one can take this mod 7 to get more manageable coefficients:

$$R(x) = (5 + 4 x + 6 x^2 + 4 x^3 + 6 x^4 + 5 x^5) \bmod 7$$

So two things to note...not only was that calculation tedious (just checking it is intractable by hand (more than it's worth)), but the resulting function is not particularly nice, and looks even more complex than just the plain old list.

At this point, maybe we could find a simpler function for a particular idiosyncratic labeling like Qiaochu's, using mod or some other simple functions, and it might even be simpler to do the finding, but I have no flash of insight right yet that it would allow it to work for all faces.

So I think the moral of the story is that, really, the easiest thing to code (and also quickest to execute, and easiest to understand later) will be the 'list-of-values' function.

The desire for a non-case based function is a natural one, like capturing a recurrence relation with a generating function, or fitting a function to a set of points. But in this case the result is as or more complex than the simple list.

-
We could probably take the polynomial modulo 7 to reduce the size of the coefficients and also get rid of the unpleasant division by 120. –  Zhen Lin May 17 '11 at 7:10