31

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.

11 Answers11

77

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)
Greg Hewgill
  • 10,201
50

You don't need to imagine in high spatial dimensions, just think of it as a fern leaf. fern leaf

The main stalk is your first array, with each branch being an item that it is storing. If we look at a branch this is your second dimension. It has a similar structure of smaller branches coming of it representing its data. These in turn have their own small branches which continues until we get to the tiny leaves representing the data of the inner most or highest dimension array.

You can see this building up if you declare each level with its own name. Here I am reusing each level varible to minimise the code:

leaf = 2;
tinyBranch = [leaf, leaf, leaf];
middleBranch = [tinyBranch, tinyBranch, tinyBranch];
bigBranch = [middleBranch, middleBranch, middleBranch];
mainBranch = [bigBranch, bigBranch, bigBranch];
n00begon
  • 609
45

The dimensions are whatever you want to be, the 4th dimension doesn't necessarily have to be time. If you think of three dimensions as a cube, you can think of 4 dimensions as a row of cubes. 5 dimensions, a grid of cubes, and so on.

You could also have a 3d collection of voxels, with a 4th dimension being color, or density, or some other property.

When you allocate the memory for your multidimensional array, it just simply allocates the product of each dimensions maximum for your data type. If you have a 3d array or 'cube' of 10 elements in each dimension, you'll have 1,000 elements allocated. If you make that a 4d array with 10 elements in the 4th dimension, the computer will just allocate 10,000. Bump it up to 5 dimensions, and it will allocate 100,000.

The computer doesn't care about any sort of meaning about what each dimension represents. To select where in the list of elements a single point is, it's just multiplication to select a memory address.

whatsisname
  • 27,703
26

Imagine doing R&D on some new medical device, a series of sensors that you put along a patient's arms. You have seven volunteers lined up for testing. Each sensor reports low-frequency, mid-frequency, and high-frequency readings, which you take once every 100ms for about a minute.

How to store all that data in memory for analysis and plotting?

An array, obviously. It would look like this (using made-up generic pseudocode):

npatients = 7
nsensors = 4     // number of sensors on an arm
nchannels = 3
nsamples = 60.0 / 0.1
sensordata = Array[ npatients, nsensors, 2, nchannels, nsamples ]

That's a five dimensional array, and there's nothing tricky, mysterious or baffling about it. There is no reason to try to associate it with 5-dimensional Euclidean space. To obtain any one data value, we use an expression like

x = sensordata[6, 5, 1, 2, 338)

It just like querying a relational database where you have a record for each data value, with five columns holding patient id, sensor id and so on, and a column with the value. To get one data point you use five terms in the WHERE: SELECT value FROM SensorData WHERE (patientid=6) and (sensorid=5) and (arm="left") and (channel="midfreq") and (sampleindex=338).

There is nothing mystical about a database table with five or more columns, is there?

(I'm using 1-based indexing though in real life, 0-based is much more common.)

Note that I'm a bad boy due to hard-coding the number of arms. If I'm ever given funding to investigate these sensors on an octopus, I'm in trouble!

DarenW
  • 4,463
20

An array is only a block of continous memory. Memory addressing is one-dimensional, you can either go forward or backward. So assuming you have an array with 5 elements, 5 memory blocks will be reserved. If you have a 2-dimensional array with 5 elements in each dimension, 25 memory blocks will be reserved.

Zillolo
  • 300
18

...or I'd be asking it on MathSO...

Well, as a matter of fact mathematicians would never (or at least not usually) associate a fourth dimension with anything like time. Nor would they associate the first three ones with anything space like: mathematicians simply define dimension as an abstract property of, typically, a vector space (often this will be generalised to manifolds or even metric spaces). And this abstract definition doesn't care about how many dimensions the physical space we happen to move in has. The concept of dimensions applies to spaces that don't even resemble the physical space. In fact mathematicians (and indeed physicists) very often use infinite-dimensional spaces, such as the Hilbert spaces of quantum mechanics.

With that clarified, let's talk arrays – you don't need to understand vector spaces, since the abstract definition is actually much simpler here.

An (ℓ0 × ℓ1 × ℓ2 × ... × ℓn−1)-sized array (i.e. of dimension n) is simply a collection of ℓ0ℓ1 ⋅ ... ⋅ ℓn−1 numbers (or whatever type of object populates the array). The only difference to a one-dimensional array of that length is that you have a particular useful way of indexing the dimensions seperately, namely

ilin = in−1 + ℓn−1 ⋅ (in−2 + ℓn−1 ⋅ ( ... ℓ2 ⋅ (i1 + ℓ1i0)...))

14

In programming, arrays are quite easy to implement, but maybe not to understand.

Generally, each level of arrays means to have the content n-fold. That means

  • int x[4] are 4 blocks, each of them containing an int.
  • int x[5][4] are 5 blocks, each of them containing an int[4].
  • int x[3][5][4] are 3 blocks, each of them containing an int[5][4].
  • int x[2][3][5][4] are 2 blocks, each of them containing an int[3][5][4].

How you are referring to them is up to you, but for better understanding, you have something like

  • COLUMN for the last one
  • ROW for the second-last one
  • PAGE for the third-last one

Till here, I read it somewhere. In order to stay here, we can as well define

  • BOOK for the fourth-last one
  • and maybe SHELF for the fifth-last one. (Or, if you prefer, SHELFROW so that we can continue.)

That said, I never saw array with more than 4 or maybe 5 dimensions in "wild life".

This way, you can define and imagine int x[6][2][3][5][4] as a collection of 6 "shelves", each having 2 books, each having 3 pages, each having 5 rows, each having 4 columns.

glglgl
  • 250
13

Think of a one-dimensional array like a chest of drawers:

chest of drawers

Each drawer is an index of the array. You can put whatever you want in each drawer, and for many purposes, each drawer will only contain a single item (that's a one-dimensional array).

This chest of drawers is magical though, so it's not limited by physical space. That means that you can put another chest of drawers inside each drawer of the first chest of drawers. The inner chests of drawers can then contain whatever you want. That's a two-dimensional array.

So you can say something like "open up the top drawer of the first chest of drawers, get the chest of drawers out of that drawer, then open up the bottom drawer of that second chest of drawers". That would be like accessing an index of a 2D array: myArray[0][3];

And of course, the chests of drawers inside the outer-most chest of drawers can themselves contain chests of drawers. That's a three-dimensional array.

So, your question is: what's a four-dimensional array? It's a chest of drawers of chests of drawers of chests of drawers of chests of drawers, of course!

It's drawers all the way down.

Kevin Workman
  • 668
  • 4
  • 10
5

Most of the aspects of this question have already been considered, but I think it will help if you consider the nature of a dimension. Not all dimensions are spatial. A dimension is a context for measurement. Here are some examples:

  • Frequency - colour or pitch
  • Mass
  • Valence
  • Colour (up quark, down quark, strange quark, charmed quark etc)
  • Spin direction
  • Angle
  • Loudness
  • Hotness (of chili)

The "fourth" dimension is only fourth because there are three spatial dimensions. Space and time loom large because, well, they loom large. Very much in-your-face. But any quantifiable, measurable quality can be a dimension if you measure it.

For example, brassieres have three dimensions: cup size, chest size and interstitial (I don't know what you girls call it but I mean the distance between cups).

Peter
  • 51
4

In physics, we assume each spatial dimension to be infinite, which makes finding space for new dimensions pretty difficult.

When dealing with finite arrays, it's easy to find space.

Imagine a sheet of paper with a grid printed on it; you can write some information in each cell of the grid. That's a 2D array: row and column.

Put several of those sheets of paper in a file folder; that's a 3D array: page, row, and column.

Put several of those folders in a file box. 4D array: folder, page, row, column.

Arrange boxes in a rectangular grid on a wooden pallet. 6D array: box-row, box-column, folder, page, row, column.

Stack more grids of boxes on top of those. 7D array: box-depth, box-row, box-column, folder, page, row, column.

Start cramming pallets into a shipping container: 9D array. (Assuming each stack is as tall as the inside of the container, so you can only get 2 more dimensions here.)

Stack up shipping containers on the deck of a container ship: 12D array.

Your fleet of container ships is now a 13D array.

3

In the Cartesian coordinate system, you have the x and y axes on a plane. You can represent any number on the plane as (x,y).

In three-"space" (otherwise known as a cube), you can have the x, y, and z axes. You can represent any element of the cube as (x,y,z).

In multivariate space, you can have the x, y, z and, w axes (where the w axis is "imaginary"). You can represent any element of that space as (x, y, z, w).

All of these points in space are denoted by vectors. In four-space, you can have two vectors, where v1 =(x1, y1, z1, w1), and v2 =(x2, y2, z2, w2). Then you manipulate these vectors as you would numbers. For instance, the sum of the two vectors, v1+ v2 would be (x1, y1, z1, w1)+ (x2, y2, z2, w2). Then you add these vectors term by term as you would numbers, to get: (x1+x2, y1+y2, z1+z2, w1+w2).

Your program will define the vectors using appropriate arrays, and then perform arithmetic operations on them in the appropriate order.

Tom Au
  • 893