How the Fourth Dimension Works with Arrays

arraytheory

Abstract:

So, as I understand it (although I have a very limited understanding), there are three dimensions that we (usually) work with physically:

The 1st would be represented by a line.
The 2nd would be represented by a square.
The 3rd would be represented by a cube.

Simple enough until we get to the 4th — It is kinda hard to draw in a 3D space, if you know what I mean… Some people say that it has something to do with time.

The Question:

Now, though that doesn't all make much sense, that is all great with me. My question isn't about this, or I'd be asking it on MathSO or PhysicsSO. My question is: How does the computer handle this with arrays?

I know that you can create 4D, 5D, 6D, etc… arrays in many different programming languages, but I want to know how that works.

Best Answer

Fortunately, programs aren't limited by the physical constraints of the real world. Arrays aren't stored in physical space, so the number of dimensions of the array doesn't matter. They are flattened out into linear memory. For example, a single dimensional array with two elements might be laid out as:

(0) (1)

A 2x2 dimensional array might then be:

(0,0) (0,1) (1,0) (1,1)

A three dimensional 2x2x2 array might be:

(0,0,0) (0,0,1) (0,1,0) (0,1,1) (1,0,0) (1,0,1) (1,1,0) (1,1,1)

You can hopefully see where this is going. Four dimensions might be:

(0,0,0,0) (0,0,0,1) (0,0,1,0) (0,0,1,1) (0,1,0,0) (0,1,0,1) (0,1,1,0) (0,1,1,1)
(1,0,0,0) (1,0,0,1) (1,0,1,0) (1,0,1,1) (1,1,0,0) (1,1,0,1) (1,1,1,0) (1,1,1,1)