32

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.

Questionmark
  • 471
  • 1
  • 5
  • 9
  • 67
    If 3 dimensions can be visualized as a cube, then 4 dimensions can be visualized as a bunch of cubes in a line. 5 dimensions can be visualized as a grid where each cell in the grid contains... a cube! And so on... The "Fourth Dimension" has nothing to do with time (whatever *that* is), unless you define it as such in the context of the semantics of your program. – FrustratedWithFormsDesigner Jul 02 '14 at 21:35
  • 16
    In general, you can get over this conceptual hump by trying to avoid thinking of dimensions as strictly physical constructs. For example, some machine learning problems can have a dimensionality in the hundreds of thousands, where each dimension is a feature of the dataset. – Steven Evers Jul 03 '14 at 00:42
  • 5
    (1) [Vector space](http://en.wikipedia.org/wiki/Vector_space) (2) [Multi-dimensional arrays](http://en.wikipedia.org/wiki/Array_data_structure#Multidimensional_arrays) – rwong Jul 03 '14 at 01:16
  • 7
    Further to Steve Evers's comment, think of a common data type: an RGB color. This has three dimensions, so you can consider the RGB "color space". Now add an alpha component. You have four dimensions. – jscs Jul 03 '14 at 02:29
  • 26
    Remember that the computer doesn't care about the idea of geometric dimensions--those are just a device for human convenience. If you allocate a 5x5x5x5 array, the computer just allocates an array of 625 elements and does math with your indices accordingly. – David Zhang Jul 03 '14 at 02:58
  • 2
    While you should avoid linking "dimensions" with physical space, for data visualisation this is often impossible, but yes, it gets very very confusing when you start talking about whether an nth dimensional output set can be linearly separated by a hyperplane. Don't think about that stuff if you can avoid it. – Phoshi Jul 03 '14 at 08:56
  • 1
    I think of 4th dimension from a mathematical point of view. A 3 dimensional function returns a 4th dimensional point, right? I just think of that 4th "dimension" just as a function of a point in 3 dimensions. For example, you have some object in 3 space which has some different density at each point. Bam, 4 dimensions - X, Y, Z, and Density. – jaredready Jul 03 '14 at 13:25
  • http://math.stackexchange.com/questions/87601/techniques-for-visualising-n-dimension-spaces#comment206408_87601 – Ming-Tang Jul 04 '14 at 18:59
  • 1
    @Questionmark In this case "Tell us what you've tried" means you could for example explain how the second or third dimension works with arrays and why the fourth dimension is any different. – Robert Jul 06 '14 at 06:01
  • Ever heard of an [hypercube](http://en.wikipedia.org/wiki/Hypercube) ? – ereOn Jul 06 '14 at 08:36
  • 1
    @Questionmark Just replace the `fr` in the URL with `en`. But anyway, I still don't understand where the original question came from. Why would an array with 4 subscripts be more difficult than one with 3 subscripts? I mean, why 3 versus 4? The question would have made more sense to me if you asked how 2-dimensional arrays work; computer memory is 1-dimensional after all. – Mr Lister Jul 07 '14 at 06:41

11 Answers11

78

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,181
  • 1
  • 46
  • 45
  • 10
    Computer arrays are not limited by human comprehension or visualization, but they're limited by physical constraints, e.g. an array of d dimensions each of length n will take n^d, or more generally with different length dimensions, n1 × n2 × … × nd. – acelent Jul 02 '14 at 23:25
  • 1
    Greg, I was under the impression that the dimensions in arrays referred to their nesting. E.g. 1d `(1,2)`, 2d `(1, (A,B))`, 3d `(1, (A, ($, %)))`, etc. What is the correct term for this sort of phenomena? Nesting level 3? – Colton Allen Jul 03 '14 at 00:11
  • 6
    @ColtonAllen: I'm not quite sure what you're referring to. The [definition of dimension](http://mathworld.wolfram.com/Dimension.html) says "Roughly speaking, it is the number of coordinates needed to specify a point on the object." An array declared in C as `int a[2][2][2];` would be a 3-dimensional array. – Greg Hewgill Jul 03 '14 at 00:23
  • @ColtonAllen: I'm not aware of any term for that. That just sort of begins to be conceptualized as a hierarchical data structure rather than an array. – whatsisname Jul 03 '14 at 00:24
  • @ColtonAllen: Could you ask that as an independent question? – David Cary Jul 03 '14 at 03:14
  • 1
    @ColtonAllen: Nesting structures are technically trees. Though in some languages like lisp where data structures are created recursively nesting can be equivalent to arrays. – slebetman Jul 03 '14 at 06:30
  • 1
    Should a two dimensional 2x2 matrix not be `((0,0),(0,0),(0,0),(0,0))` and a 3 dimentional 2x2x2 be `(((0,0),(0,0),(0,0),(0,0)),((0,0),(0,0),(0,0),(0,0)))`. I'm not 100% sure what your brackets represent – Richard Tingle Jul 03 '14 at 09:17
  • 1
    @RichardTingle: Sorry, perhaps my notation wasn't clear. Each tuple represents an array element, and the numbers within the tuple represent the coordinate of that element. It looks like you're thinking of a notation for representing the actual array contents. – Greg Hewgill Jul 03 '14 at 09:19
  • @GregHewgill Ahh, that makes sense, thats probably a better way to represent it – Richard Tingle Jul 03 '14 at 09:20
  • @GregHewgill It makes a lot more sense now that you put it in terms of coordinates. – Colton Allen Jul 03 '14 at 12:34
  • 4
    *"Fortunately, programs aren't limited by the physical constraints of the real world"* So thats why all we have processors at 4.5THz, and no one cares about the memory hierarchy, isn't? This is really the most funny answer I have readed.... – Manu343726 Jul 03 '14 at 15:32
  • 24
    @Manu343726 He's referring to the fact that we live in (theoretically) limited dimensions of space and time, but arrays in computer memory have "pretend" dimensions - it's all stored in a linearly contiguous space in memory - so they are not limited to the number of dimensions we live in. Don't troll >:( – Blackhawk Jul 03 '14 at 16:38
  • 2
    @Blackhawk I know what he meant, note that I didn't downvoted him. Its only that when first reading the answer the sentences where so funny. – Manu343726 Jul 03 '14 at 16:43
  • 15
    @Manu343726: I carefully said *Programs*, not *Computers*. That's one of the great things about software. – Greg Hewgill Jul 03 '14 at 17:27
  • Since we don't know the actual physical constraints in terms of dimensions, only our own perception, I suggest you reword the answer, something like replacing "the physical constraints of the real world" with "the human constraints on understanding further dimensions". Another one, "Arrays aren't stored in physical space", they are, so "Arrays aren't stored in as many physical dimensions as their own" would make a whole lot more sense. – acelent Jul 03 '14 at 22:47
  • I think your first sentence is actually backwards. Programs are limited by the physical constraints of the hardware they run on. And memory is _flat_. Programming _languages_ may allow arrays to be specified in terms of arbitrarily many dimensions, but really all that happens is the provided numbers are multiplied together and you get a flat list of however many things you actually asked for. Some math is used to handle addressing into the flat list based upon multi-dimensional indices, but it's still a flat list. The dimensions are entirely illusory. – aroth Jul 04 '14 at 08:00
  • @aroth - you can write an infinite loop that allocates new memory on each run and the program will be *correct*. What happens when you "port" this program to a theoretical computer with infinite resources and run it? – Leonardo Herrera Jul 04 '14 at 14:39
  • This discussion seems to be leading toward [Cantor's diagonal argument](http://en.wikipedia.org/wiki/Cantor's_diagonal_argument). Would you like to move the discussion to the endless chat? – rwong Jul 05 '14 at 10:49
  • At least in C, a 2-dimensional array is nothing more or less than an array of 1-dimensional arrays. An N-dimensional array is an array of N-1-dimensional arrays. Other languages define these terms differently, but the semantics are similar (though the ordering may vary). – Keith Thompson Aug 15 '14 at 19:17
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
  • 4
  • 5
  • 1
    Wouldn't this visualization more closely represent a jagged array? – Matt Johnson-Pint Jul 03 '14 at 22:25
  • 2
    @MattJohnson Formally yes, but this example does explain/visualize multi-dimensional arrays as well. – M.Mimpen Jul 04 '14 at 08:14
  • 4
    +1 this is not only a neat visualization, but closer to the truth than the linear explanation for many array implementations. Whether or not most people here would restrict the term "array" to contiguous memory, "multidimensional *array*" is certainly the term used for such arrangements in the literature for many languages. – DeveloperInDevelopment Jul 05 '14 at 21:17
46

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,463
  • 14
  • 73
  • 93
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,433
  • 3
  • 22
  • 43
  • 3
    +1 Excellent example demonstrating that dimensions can be any data that you require. – Mike Jul 03 '14 at 02:19
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
  • 1
  • 4
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)...))

leftaroundabout
  • 1,557
  • 11
  • 12
  • To be clear, you only need an array with 3 elements to describe 3 dimensions, and an N element array describes N dimensions. However, detailing *every* vector is a different story. Often times, it's done by showing an image (`imshow` in Python) -- it can show two spatial dimensions as well as a third color dimension. – Scott Jul 07 '14 at 03:23
  • @Scott: I agree the notion of "dimension of an array" is unfortunate because it means something unrelated to the dimensionality of a space whose vectors you might represent by the arrays. (However I also think it's not such a good idea to represent vectors by plain, unabstracted arrays in the first place.) A better name might be the _rank_ of an array, in analogy to [tensors](http://en.wikipedia.org/wiki/Tensors). – leftaroundabout Jul 07 '14 at 09:44
13

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
  • 242
  • 1
  • 7
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
  • 1
  • 1
    "Not all dimensions are spatial." For arrays, all dimensions *are* spatial. – Rhymoid Jul 05 '14 at 17:03
  • 2
    @Rhymoid: For arrays, *no* dimensions are inherently spatial in the way we think about space. :P We define them to represent whatever we want. – cHao Jul 06 '14 at 16:37
  • @cHao Maybe if you look at the semantics of the data they store. But on the representational/syntactic/implementation side of things, all array dimensions are inherently spatial. It's actually what you depend on when using arrays as part of an algorithm. – Rhymoid Jul 06 '14 at 16:42
  • @Rhymoid: That is the same thought process that led to this question being asked in the first place. A dimension being enumerable doesn't make it spatial. Implementationwise, there is no space. There is only memory, and memory is one-dimensional as far as a program knows/sees/cares. – cHao Jul 06 '14 at 16:59
  • @cHao: implementationwise, there is space, because there is also time. The term 'space leak' (as an alternative for 'memory leak', found in the Haskell community) is no coincidence. The fact that memory is described as one-dimensional is a heritage from BCPL. – Rhymoid Jul 06 '14 at 18:13
  • @Rhymoid: Memory is described as one-dimensional because in every common CPU, a process can enumerate every addressable byte of memory by incrementing a single number over and over. That being the very definition of "one dimensional", any other description would seem incorrect. – cHao Jul 06 '14 at 18:49
  • @cHao: That's undoubtedly how Martin Richards got his ideas for BCPL's memory model too. But don't forget that formal descriptions of algorithms aren't limited by what we can build, because they are inherently distinct: what we can build, like CPUs, is physical, while formal descriptions, like arrays, are ideal. For instance, using the description `[[Integer]]` (a list of lists of integers) in Haskell as a two-dimensional 'array' is inherently two-dimensional, even though it's all thunks and pointers when you compile it for a physical computer. – Rhymoid Jul 06 '14 at 18:57
  • @Rhymoid: I can't help that Haskell is hopelessly detached from reality. In the real world, that list of lists is a list of lists, and the only difference between `a[1]` and `a[1][2][3][4][5]` is the type of the expression. – cHao Jul 06 '14 at 22:19
  • @cHao My remark on dimensionality in Haskell was *exactly* about types. – Rhymoid Jul 06 '14 at 22:42
  • @Rhymoid: That typing is entirely a feature of the language, though. Once you get into implementation, everything becomes one-dimensional. A 3D array's size would not be measured in cubic bytes. – cHao Jul 07 '14 at 01:51
  • @Rhymoid: And if we want to complicate things from the language side, consider that in any language where pointers, references, and-or copy-on-write are a thing (which includes basically all OO languages these days), any of those entries can be a reference to the same array as another -- meaning all the stuff within that array effectively costs zero bytes. And if we throw in dynamic typing (again very common), it's possible to have an array that contains itself -- which renders it effectively ∞-dimensional, but doesnt keep it from fitting in a finite amount of memory. – cHao Jul 07 '14 at 03:08
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.

  • "we assume each spatial dimension to be infinite" infinite isn't the biggest deal here actually, _continuous_ is the "real" problem (i.e. over-countably infinite, and we need a homeomorphic mapping so it's physically meaningful). – leftaroundabout Jul 04 '14 at 11:01
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
  • 7
  • 17