In
mathematics
Mathematics is an area of knowledge that includes the topics of numbers, formulas and related structures, shapes and the spaces in which they are contained, and quantities and their changes. These topics are represented in modern mathematics ...
and
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, index notation is used to specify the elements of an array of numbers. The formalism of how indices are used varies according to the subject. In particular, there are different methods for referring to the elements of a list, a vector, or a matrix, depending on whether one is writing a formal mathematical paper for publication, or when one is writing a
computer program
A computer program is a sequence or set of instructions in a programming language for a computer to execute. Computer programs are one component of software, which also includes documentation and other intangible components.
A computer program ...
.
In mathematics
It is frequently helpful in mathematics to refer to the elements of an array using subscripts. The subscripts can be
integer
An integer is the number zero (), a positive natural number (, , , etc.) or a negative integer with a minus sign (−1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
s or
variables. The array takes the form of
tensors
In mathematics, a tensor is an algebraic object that describes a multilinear relationship between sets of algebraic objects related to a vector space. Tensors may map between different objects such as vectors, scalars, and even other tenso ...
in general, since these can be treated as multi-dimensional arrays. Special (and more familiar) cases are
vectors (1d arrays) and
matrices
Matrix most commonly refers to:
* ''The Matrix'' (franchise), an American media franchise
** ''The Matrix'', a 1999 science-fiction action film
** "The Matrix", a fictional setting, a virtual reality environment, within ''The Matrix'' (franchis ...
(2d arrays).
The following is only an introduction to the concept: index notation is used in more detail in mathematics (particularly in the representation and manipulation of
tensor operations). See the main article for further details.
One-dimensional arrays (vectors)
A vector treated as an array of numbers by writing as a
row vector
In linear algebra, a column vector with m elements is an m \times 1 matrix consisting of a single column of m entries, for example,
\boldsymbol = \begin x_1 \\ x_2 \\ \vdots \\ x_m \end.
Similarly, a row vector is a 1 \times n matrix for some n, c ...
or
column vector
In linear algebra, a column vector with m elements is an m \times 1 matrix consisting of a single column of m entries, for example,
\boldsymbol = \begin x_1 \\ x_2 \\ \vdots \\ x_m \end.
Similarly, a row vector is a 1 \times n matrix for some n, c ...
(whichever is used depends on convenience or context):
:
Index notation allows indication of the elements of the array by simply writing ''a
i'', where the index ''i'' is known to run from 1 to ''n'', because of n-dimensions.
For example, given the vector:
:
then some entries are
:
.
The notation can be applied to
vectors in mathematics and physics. The following
vector equation
:
can also be written in terms of the elements of the vector (aka components), that is
:
where the indices take a given range of values. This expression represents a set of equations, one for each index. If the vectors each have ''n'' elements, meaning ''i'' = 1,2,…''n'', then the equations are explicitly
:
Hence, index notation serves as an efficient shorthand for
#representing the general structure to an equation,
#while applicable to individual components.
Two-dimensional arrays
More than one index is used to describe arrays of numbers, in two or more dimensions, such as the elements of a matrix, (see also image to right);
:
The entry of a matrix A is written using two indices, say ''i'' and ''j'', with or without commas to separate the indices: ''a
ij'' or ''a
i,j'', where the first subscript is the row number and the second is the column number.
Juxtaposition
Juxtaposition is an act or instance of placing two elements close together or side by side. This is often done in order to compare/contrast the two, to show similarities or differences, etc.
Speech
Juxtaposition in literary terms is the showing ...
is also used as notation for multiplication; this may be a source of confusion. For example, if
:
then some entries are
:
.
For indices larger than 9, the comma-based notation may be preferable (e.g., ''a''
3,12 instead of ''a''
312).
Matrix equation
In mathematics, a matrix (plural matrices) is a rectangular array or table of numbers, symbols, or expressions, arranged in rows and columns, which is used to represent a mathematical object or a property of such an object.
For example,
\begin ...
s are written similarly to vector equations, such as
:
in terms of the elements of the matrices (aka components)
:
for all values of ''i'' and ''j''. Again this expression represents a set of equations, one for each index. If the matrices each have ''m'' rows and ''n'' columns, meaning and , then there are ''mn'' equations.
Multi-dimensional arrays
The notation allows a clear generalization to multi-dimensional arrays of elements: tensors. For example,
:
representing a set of many equations.
In tensor analysis, superscripts are used instead of subscripts to distinguish covariant from contravariant entities, see
covariance and contravariance of vectors
In physics, especially in multilinear algebra and tensor analysis, covariance and contravariance describe how the quantitative description of certain geometric or physical entities changes with a change of basis. In modern mathematical notation ...
and
raising and lowering indices In mathematics and mathematical physics, raising and lowering indices are operations on tensors which change their type. Raising and lowering indices are a form of index manipulation in tensor expressions.
Vectors, covectors and the metric
Math ...
.
In computing
In several programming languages, index notation is a way of addressing elements of an array. This method is used since it is closest to how it is implemented in
assembly language
In computer programming, assembly language (or assembler language, or symbolic machine code), often referred to simply as Assembly and commonly abbreviated as ASM or asm, is any low-level programming language with a very strong correspondence be ...
whereby the address of the first element is used as a base, and a multiple (the index) of the element size is used to address inside the array.
For example, if an array of integers is stored in a region of the computer's memory starting at the memory cell with address 3000 (the
base address
In computing, a base address is an address serving as a reference point ("base") for other addresses. Related addresses can be accessed using an ''addressing scheme''.
Under the ''relative addressing'' scheme, to obtain an absolute address, the r ...
), and each integer occupies four cells (bytes), then the elements of this array are at memory locations 0x3000, 0x3004, 0x3008, …, 0x3000 + 4(''n'' − 1) (note the
zero-based numbering
Zero-based numbering is a way of numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1 as is typical in everyday ''non-mathematical'' or ''non-programming'' circumstances. Under zero-based ...
). In general, the address of the ''i''th element of an array with
base address
In computing, a base address is an address serving as a reference point ("base") for other addresses. Related addresses can be accessed using an ''addressing scheme''.
Under the ''relative addressing'' scheme, to obtain an absolute address, the r ...
''b'' and element size ''s'' is .
Implementation details
In the
C programming language
''The C Programming Language'' (sometimes termed ''K&R'', after its authors' initials) is a computer programming book written by Brian Kernighan and Dennis Ritchie, the latter of whom originally designed and implemented the language, as well as ...
, we can write the above as (pointer form) or (array indexing form), which is exactly equivalent because the C standard defines the array indexing form as a transformation to pointer form. Coincidentally, since pointer addition is commutative, this allows for obscure expressions such as which is equivalent to .
[Programming with C++, J. Hubbard, Schaum's Outlines, McGraw Hill (USA), 1996, ]
Multidimensional arrays
Things become more interesting when we consider arrays with more than one index, for example, a two-dimensional table. We have three possibilities:
* make the two-dimensional array one-dimensional by computing a single index from the two
* consider a one-dimensional array where each element is another one-dimensional array, i.e. an array of arrays
* use additional storage to hold the array of addresses of each row of the original array, and store the rows of the original array as separate one-dimensional arrays
In C, all three methods can be used. When the first method is used, the programmer decides how the elements of the array are laid out in the computer's memory, and provides the formulas to compute the location of each element. The second method is used when the number of elements in each row is the same and known at the time the program is written. The programmer declares the array to have, say, three columns by writing e.g. . One then refers to a particular element of the array by writing . The compiler computes the total number of memory cells occupied by each row, uses the first index to find the address of the desired row, and then uses the second index to find the address of the desired element in the row. When the third method is used, the programmer declares the table to be an array of pointers, like in . When the programmer subsequently specifies a particular element , the compiler generates instructions to look up the address of the row specified by the first index, and use this address as the base when computing the address of the element specified by the second index.
Example
This function multiplies two 3×3 floating point matrices together.
void mult3x3f(float result[][3], const float A[][3], const float B[][3])
In other languages
In other programming languages such as Pascal, indices may start at 1, so indexing in a block of memory can be changed to fit a start-at-1 addressing scheme by a simple linear transformation – in this scheme, the memory location of the ''i''th element with
base address
In computing, a base address is an address serving as a reference point ("base") for other addresses. Related addresses can be accessed using an ''addressing scheme''.
Under the ''relative addressing'' scheme, to obtain an absolute address, the r ...
''b'' and element size ''s'' is .
References
* ''Programming with C++'', J. Hubbard, Schaum's Outlines, McGraw Hill (USA), 1996,
* ''Tensor Calculus'', D.C. Kay, Schaum's Outlines, McGraw Hill (USA), 1988,
* ''Mathematical methods for physics and engineering'', K.F. Riley, M.P. Hobson, S.J. Bence, Cambridge University Press, 2010,
{{DEFAULTSORT:Index Notation
Mathematical notation
Programming constructs