This comparison of programming languages (array) compares the features of
array data structure
In computer science, an array is a data structure consisting of a collection of ''elements'' ( values or variables), each identified by at least one ''array index'' or ''key''. An array is stored such that the position of each element can be c ...
s or
matrix
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'' (franchi ...
processing for various computer
programming language
A programming language is a system of notation for writing computer programs. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language.
The description of a programming l ...
s.
Syntax
Array dimensions
The following list contains
syntax examples of how to determine the dimensions (index of the first element, the last element or the size in elements).
Note particularly that some languages index from zero while others index from one.
Indexing
The following list contains syntax examples of how to access a single element of an array.
Slicing
The following list contains syntax examples of how a range of element of an array can be accessed.
In the following table:
* – the index of the first element in the slice
* – the index of the last element in the slice
* – one more than the index of last element in the slice
* – the length of the slice (= end - first)
* – the number of array elements in each (default 1)
Array system cross-reference list
Vectorized array operations
Some compiled languages such as
Ada
Ada may refer to:
Places
Africa
* Ada Foah, a town in Ghana
* Ada (Ghana parliament constituency)
* Ada, Osun, a town in Nigeria
Asia
* Ada, Urmia, a village in West Azerbaijan Province, Iran
* Ada, Karaman, a village in Karaman Province, Tu ...
and
Fortran, and some scripting languages such as
IDL,
MATLAB
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementa ...
, and
S-Lang
The S-Lang programming library is a software library for Unix, Windows, VMS, OS/2, and Mac OS X. It provides routines for embedding an interpreter for the S-Lang scripting language, and components to facilitate the creation of text-based applica ...
, have native support for vectorized operations on arrays. For example, to perform an element by element sum of two arrays, and to produce a third , it is only necessary to write
c = a + b
In addition to support for vectorized arithmetic and relational operations, these languages also vectorize common mathematical functions such as sine. For example, if is an array, then
y = sin (x)
will result in an array whose elements are sine of the corresponding elements of the array .
Vectorized index operations are also supported. As an example,
even = x(2::2);
odd = x(::2);
is how one would use
Fortran to create arrays from the even and odd entries of an array. Another common use of vectorized indices is a filtering operation. Consider a clipping operation of a sine wave where amplitudes larger than 0.5 are to be set to 0.5. Using
S-Lang
The S-Lang programming library is a software library for Unix, Windows, VMS, OS/2, and Mac OS X. It provides routines for embedding an interpreter for the S-Lang scripting language, and components to facilitate the creation of text-based applica ...
, this can be done by
y = sin(x);
y
0.5)">here(abs(y)>0.5)= 0.5;
Mathematical matrix operations
,
,
,
,
,
,
, -
,
Mathematica
Wolfram Mathematica is a software system with built-in libraries for several areas of technical computing that allow machine learning, statistics, symbolic computation, data manipulation, network analysis, time series analysis, NLP, optimi ...
/
Wolfram Language
The Wolfram Language ( ) is a general multi-paradigm programming language developed by Wolfram Research. It emphasizes symbolic computation, functional programming, and rule-based programming and can employ arbitrary structures and data. It is ...
,
,
,
,
,
,
,
, -
,
MATLAB
MATLAB (an abbreviation of "MATrix LABoratory") is a proprietary multi-paradigm programming language and numeric computing environment developed by MathWorks. MATLAB allows matrix manipulations, plotting of functions and data, implementa ...
/
GNU Octave
GNU Octave is a high-level programming language primarily intended for scientific computing and numerical computation. Octave helps in solving linear and nonlinear problems numerically, and for performing other numerical experiments using a lang ...
,
,
,
,
,
,
,
, -
,
NumPy
,
,
,
,
,
,
,
, -
,
R
, or
,
,
,
,
,
,
, -
,
S-Lang
The S-Lang programming library is a software library for Unix, Windows, VMS, OS/2, and Mac OS X. It provides routines for embedding an interpreter for the S-Lang scripting language, and components to facilitate the creation of text-based applica ...
,
,
,
,
,
,
,
, -
,
SymPy
,
,
,
,
,
,
,
, -
References
{{Reflist, refs=
[The index may be a negative number, indicating the corresponding number of places before the end of the array.]
[Slices for multidimensional arrays are also supported and defined similarly.]
[Slices of the type ]''first'':''last'':''step''
are also supported.
[More generally, for 1-d arrays ]Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
and S-Lang
The S-Lang programming library is a software library for Unix, Windows, VMS, OS/2, and Mac OS X. It provides routines for embedding an interpreter for the S-Lang scripting language, and components to facilitate the creation of text-based applica ...
allow slices of the form''array'' 'indices''/code>, where ''indices''
can be a range such mentioned in footnote 2 or an explicit list of indices, e.g., ' ,9,3,4/code>', or a mix of both, e.g., A [0:3,7,9,[11:2:-3">:3">[0:3,7,9,[11:2:-3">:3<_a>,7,9,[11:2:-3.html" ;"title=":3">[0:3,7,9,[11:2:-3">:3">[0:3,7,9,[11:2:-3/code>.
[{{Mono, ''last'' or {{Mono, ''end'' may be a negative number, indicating to stop at the corresponding number of places before the end of the array.]
[C# 8.0 proposed feature ({{as of, 2019, 08, 29, lc=y)]
[{{cite web , title=Ranges - C# 8.0 language proposals , url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-8.0/ranges , website=Microsoft Docs , publisher=Microsoft , accessdate=29 August 2019]
[Size can only be chosen on initialization after which it is fixed.]
[Allows arrays of arrays which can be used to emulate most—but not all—aspects multi-dimensional arrays]
[Size can only be chosen on initialization when memory is allocated on the heap, as distinguished from when it is allocated on the stack. This note need not be made for a language that always allocates arrays on the heap.]
[C99 allows for variable size arrays; however there is almost no compiler available to support this new feature]
[This list is strictly comparing language features. In every language (even assembler) it is possible to provide improved array handling via add on libraries. This language has improved array handling as part of its standard library]
[The class Array is fixed-size, but OrderedCollection is dynamic]
[The indexing base can be 0 or 1 as per the System Variable {{Mono, ⎕IO. This value may apply to the whole "workspace", or be localized to a user-defined function or a single primitive function by use of the Variant operator ({{Mono, ⍠).]
[At least 2 dimensions (scalar numbers are 1×1 arrays, vectors are 1×n or n×1 arrays).]
[Allows creating fixed-size arrays in "unsafe" code, allowing enhanced interoperability with other language]
[Varies by implementation. Newer implementations (FreePascal, Object Pascal (Delphi)) allow heap-based dynamic arrays.]
[Behaviour can be tuned via compiler switches. As in DMD 1.0 bounds are checked in debug mode and unchecked in release mode for efficiency]
[Almost all Fortran implementations offer bounds checking options via compiler switches. However by default, bounds checking is usually turned off for efficiency]
[Many implementations (Turbo Pascal, Object Pascal (Delphi), FreePascal) allow the behaviour to be changed by compiler switches and in-line directives]
[COBOL provides a way to specify that the usable size of an array is variable, but this can never be greater than the declared maximum size, which is also the allocated size]
[Most Common Lisp implementations allow checking to be selectively disabled]
[The index type can be a freely chosen integer type, ]enumerated type
In computer programming, an enumerated type (also called enumeration, enum, or factor in the R programming language, and a categorical variable in statistics) is a data type consisting of a set of named values called ''elements'', ''members'', ' ...
, or character type. For arrays with non-compact index types see: Associative array
In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection. In mathematical terms an ...
[The default base index is the lowest value of the index type used]
[Standard ]Perl
Perl is a family of two High-level programming language, high-level, General-purpose programming language, general-purpose, Interpreter (computing), interpreted, dynamic programming languages. "Perl" refers to Perl 5, but from 2000 to 2019 it ...
array data types do not support vectorized operations as defined here. However, the Perl Data Language
Perl Data Language (abbreviated PDL) is a set of free software array programming extensions to the Perl programming language. PDL extends the data structures built into Perl, to include large multidimensional arrays, and adds functionality to m ...
extension adds array objects with this ability.
[The standard Python array type, ]list
, does not support vectorized operations as defined here. However, the numpy extension adds array objects with this ability
[By specifying a base index, arrays at an arbitrary base can be created. However, by default, Lua's length operator does not consider the base index of the array when calculating the length. This behavior can be changed via metamethods.]
[FreeBASIC supports both variable array lengths and fixed length arrays. Arrays declared with no index range are created as variable-length arrays, while arrays with a declared range are created as fixed-length arrays]
[In these languages, one can access or write to an array index greater than or equal to the length of the array, and the array will implicitly grow to that size. This may appear at first as if the bounds are not checked; however, the bounds are checked in order to decide to grow the array, and you do not have unsafe memory access like you do in C.]
[PHP's "arrays" are associative arrays. You can use integers and strings as the keys (indexes); floats can also be used as the key but are truncated to integers. There is not really any "base index" or "bounds"]
[Haskell arrays (Data.Array) allow using any type which is an instance of Ix as index type. So a custom type can be defined and used as an index type as long as it instances Ix. Also, tuples of Ix types are also Ix types; this is commonly used to implement multi-dimensional arrays]
[ALGOL 68 arrays must be subscripted (and sliced) by type {{Mono, INT. However a hash function could be used to convert other types to {{Mono, INT. e.g. {{Mono, name ''hash("string")'']
[Because C does not bound-check indices, a pointer to the interior of any array can be defined that will symbolically act as a pseudo-array that accommodates negative indices or any integer index origin.]
[COBOL arrays may be indexed with "INDEX" types, distinct from integer types]
[While COBOL only has arrays-of-arrays, array elements can be accessed with a multi-dimensional-array-like syntax, where the language automatically matches the indexes to the arrays enclosing the item being referenced]
[While Golang's Array type is not dynamically sized, the data typ]
Slice
is dynamically-sized and is much more common in use than arrays.
[The base can be changed when initializing with {{Mono, System.Array.CreateInstance (which returns {{Mono, System.Array), but not when using the language syntax. Arrays with non-zero base indices are not the same type as those with zero base indices and cannot be manipulated using language syntax (the {{Mono, GetValue and {{Mono, SetValue methods must be used instead) or downcast to a specific type ({{Mono, T[] in C#, or {{Mono, T() in VB.NET), preventing breakage of code assuming base indices of zero.]
[XPath/XQuery has two kinds of arrays. ''Sequences'' {{Mono, (1,2,3) which cannot nest and in the XPath/XQuery 3.1 version ''arrays'' {{Mono, array { 1,2,3 } or {{Mono, ],2,3
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
} which can.
Programming language comparison
*Array