Comparison of programming languages (array)

From Infogalactic: the planetary knowledge core
Jump to: navigation, search

Lua error in package.lua at line 80: module 'strict' not found. Lua error in package.lua at line 80: module 'strict' not found.

This comparison of programming languages (array) compares the features of array data structures or matrix processing for over 48 various computer programming languages.

Syntax

Array dimensions

The following list contains syntax examples of how to determine the dimensions (index of the first element, the last element and/or the size in elements).

Note particularly that some languages index from zero while others index from one. At least since Dijkstra's famous essay,[1] zero-based indexing has been seen as superior, and new languages tend to use it.

Languages Size First Last
Ada name'Length name'First name'Last
ALGOL 68 UPB name - LWB name+1
2 UPB name - 2 LWB name+1
etc.
LWB name
2 LWB name
etc.
UPB name

2 UPB name
etc.

AWK length 1 asorti
C#, Visual Basic .NET, Windows PowerShell, F# name.Length name.GetLowerBound(dimension) name.GetUpperBound(dimension)
CFML arrayLen(name)
name.len()
1 name.len()
Ch max(shape(name)) 0 max(shape(name))-1
Cobra name.length 0 name.length-1
Common Lisp (length name) 0 (1- (length name))
Fortran SIZE(name) LBOUND(name) UBOUND(name)
Go len(name) 0 len(name) - 1
Haskell rangeSize (bounds name) fst (bounds name) snd (bounds name)
Haxe name.length 0 name.length - 1
ISLISP (length name) 0 (1- (length name))
Java, JavaScript, D, Scala name.length 0 name.length - 1
J #name 0 <:@#name
Lua #name 1 #name
Mathematica Length[name] 1
First[name]
-1
Last[name]
MATLAB, GNU Octave, Julia length(name) 1 end
Object Pascal Length(name) 0
low(name)
Length(name)-1
high(name)
Objective-C (NSArray * only) [name count] 0 [name count] - 1
OCaml Array.length name 0 Array.length name - 1
Perl scalar(@name) $[ $#name
Perl 6 @name.elems 0 @name.end
PHP count($name) 0 count($name) - 1
Python len(name) 0 -1
len(name) - 1
Ruby name.size 0
name.first
-1
name.size - 1
name.last
Rust name.len() 0 name.len() - 1
S-Lang length(name) 0 -1
length(name)-1
Scheme (vector-length vector) 0 (- (vector-length vector) 1)
Smalltalk name size 1
name first
name size
name last
Visual Basic UBound(name)-LBound(name)+1 LBound(name) UBound(name)
Wolfram Language Length[name] 1
First[name]
-1
Last[name]
Xojo UBound(name) 0 UBound(name)

Indexing

The following list contains syntax examples of how to access a single element of an array.

Format Languages
name[index] or name[index1, index2] etc. ALGOL 68, AWK, Pascal, Object Pascal, C#, S-Lang[2]
name[index] ActionScript, C, CFML, Ch, Cobra, C++, D, Go, Haxe, Java, JavaScript, Julia, Lua, Objective-C (NSArray *), Perl,[2] Python,[2] Ruby,[2] Rust, Swift
$name[index] Perl,[2] PHP, Windows PowerShell[2]
@name[index] Perl 6
name(index) Ada, BASIC, COBOL, Fortran, RPG, GNU Octave, MATLAB, Scala, Visual Basic, Visual Basic .NET, Xojo
name.(index) OCaml
name.[index] F#
name ! index Haskell
(vector-ref name index) Scheme
(aref name index) Common Lisp
(elt name index) ISLISP
name [[index]] Mathematica,[2] Wolfram Language
name at:index Smalltalk
[name objectAtIndex:index] Objective-C (NSArray * only)
index{name J
name.item(index) or name @ index[3] Eiffel

Slicing

The following list contains syntax examples of how a range of element of an array can be accessed.

In the following table:

  • first - the index of the first element in the slice
  • last - the index of the last element in the slice
  • end - one more than the index of last element in the slice
  • len - the length of the slice (= end - first)
  • step - the number of array elements in each (default 1)
Format Languages
name[first:last] ALGOL 68,[4] Julia
name[first:end:step] Python,[5][6] Go
name[first..last] Pascal, Object Pascal, Delphi
$name[first..last] Windows PowerShell
@name[first..last] Perl[7]
name[first..last]
name[first...end]
name[first, len]
Ruby[6]
name(first..last) Ada[4]
name(first:last) Fortran,[4][5] GNU Octave, MATLAB[4][7]
name[[first;;last]] Mathematica,[4][5][6] Wolfram Language
name[[first:last]] S-Lang[4][5][7]
name.[first..last] F#
name.slice(first, end) Haxe, JavaScript, Scala
name.slice(first, len) CFML
array_slice(name, first, len) PHP[6]
(subseq name first end) Common Lisp
(subseq name first end) ISLISP
Array.sub name first len OCaml
[name subarrayWithRange:NSMakeRange(first, len)]    Objective-C (NSArray * only)
(first([+i.@(-~)end){name J
name[first..<end]
name[first...last]
Swift
name copyFrom: first to:last
name copyFrom: first count:len
Smalltalk
name[first..end] D, Rust
name[first:end] Cobra

Array system cross-reference list

Programming language Default base index Specifiable index type[8] Specifiable base index Bound check Multidimensional Dynamically-sized Vectorized operations
Ada index type[9] yes yes checked yes init[10] some, others definable[11]
ALGOL 68 1 no[12] yes varies yes yes user definable
APL  ?  ? 0 or 1[13] checked yes init[10] yes
AWK 1 yes, implicitly no unchecked yes, as delimited string yes, rehashed no
BASIC 0  ? no checked no init[10]  ?
C 0 no no[14] unchecked yes, also array of array[15] init,[10][16] heap[17] no
Ch 0 no no checked yes, also array of array[15] init,[10][16] heap[17] yes
C++[11] 0 no no[14] unchecked yes, also array of array[15] heap[17] no
C# 0 no no checked yes heap[17][18] yes (LINQ select)
CFML 1 no no checked yes, also array of array[15] yes no
COBOL 1 no[19] no checked array of array[15][20] no[21] some intrinsics
Cobra 0 no no checked array of array[15] heap  ?
Common Lisp 0  ? no checked[22] yes yes yes (map or map-into)
D 0 yes[23] no varies[24] yes yes  ?
F# 0 no no checked yes heap[17][18] yes (map)
FreeBASIC 0 no yes checked yes init,[10] init[25]  ?
Fortran 1 yes yes varies[26] yes yes yes
FoxPro 1  ? no checked yes yes  ?
Go 0 no no checked array of array[15] no no
Haskell 0 yes[27] yes checked yes, also array of array[15] init[10]  ?
IDL 0  ? no checked yes yes yes
ISLISP 0  ? no checked yes init[10] yes (map or map-into)
J 0  ? no checked yes init[10] yes
Java[11] 0 no no checked array of array[15] init[10]  ?
JavaScript 0 no no checked[28] array of array[15] yes yes
Julia 1 yes no checked yes yes yes
Lua 1  ? partial[29] checked array of array[15] yes  ?
Mathematica 1 no no checked yes yes yes
MATLAB 1  ? no checked yes[30] yes yes
Oberon 0  ? no checked yes no  ?
Oberon-2 0  ? no checked yes yes  ?
Objective-C[11] 0 no no checked array of array[15] yes no
OCaml 0 no no checked by default array of array[15] init[10]  ?
Pascal, Object Pascal index type[9] yes yes varies[31] yes varies[32] some
Perl 0 no yes ($[) checked[28] array of array[15] yes no[33]
Perl 6 0 no no checked[28] yes yes yes
PHP 0 yes[34] yes[34] checked[34] yes yes yes
PL/I 1  ? yes checked yes no  ?
Python 0 no no checked array of array[15] yes no[35]
RPG 1 no no  ? no no  ?
Ruby 0 no no checked[28] array of array[15] yes  ?
S-Lang 0  ? no checked yes yes yes
Scala 0 no no checked array of array[15] init[10] yes (map)
Scheme 0  ? no checked array of array[15] init[10] yes (map)
Smalltalk[11] 1  ? no checked array of array[15] yes[36]  ?
Swift 0 no no checked array of array[15] yes  ?
Visual Basic 0 no yes checked yes yes  ?
Visual Basic .NET 0 no no checked yes yes yes (LINQ select)
Wolfram Language 1 no no checked yes yes yes
Windows PowerShell 0 no no checked yes heap  ?
Xojo 0 no no checked yes yes no
XPath 1 no no checked no yes yes
Programming language Default base index Specifiable index type[8] Specifiable base index Bound check Multidimensional Dynamically-sized Vectorized operations

Vectorized array operations

Some compiled languages such as Ada and Fortran, and some scripting languages such as IDL, MATLAB, and S-Lang, have native support for vectorized operations on arrays. For example, to perform an element by element sum of two arrays, a and b to produce a third c, 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 x is an array, then

    y = sin (x)

will result in an array y whose elements are sine of the corresponding elements of the array x.

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, this may accomplished by

    y = sin(x);
    y[where(abs(y)>0.5)] = 0.5;

Mathematical matrix operations

References

  1. https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html
  2. 2.0 2.1 2.2 2.3 2.4 2.5 2.6 The index may be a negative number, indicating the corresponding number of places before the end of the array.
  3. http://smarteiffel.loria.fr/libraries/api/lib.d/storage.d/loadpath.se.d/collection.d/ARRAY/ANY.html
  4. 4.0 4.1 4.2 4.3 4.4 4.5 Slices for multidimensional arrays are also supported and defined similarly.
  5. 5.0 5.1 5.2 5.3 Slices of the type first:last:step are also supported.
  6. 6.0 6.1 6.2 6.3 last or end may be a negative number, indicating to stop at the corresponding number of places before the end of the array.
  7. 7.0 7.1 7.2 More generally, for 1-d arrays Perl and S-Lang permit slices of the formarray[indices], where indices can be a range such mentioned in footnote 2 or an explicit list of indices, e.g., '[0,9,3,4]', as well as a combination of the two, e.g., A[[[0:3]],7,9,[11:2:-3]]].
  8. 8.0 8.1 The index type can be a freely chosen integer type, enumerated type, or character type. For arrays with non-compact index types see: Associative array
  9. 9.0 9.1 The default base index is the lowest value of the index type used
  10. 10.00 10.01 10.02 10.03 10.04 10.05 10.06 10.07 10.08 10.09 10.10 10.11 10.12 Size can only be chosen on initialization after which it is fixed
  11. 11.0 11.1 11.2 11.3 11.4 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
  12. ALGOL 68 arrays must be subscripted (and sliced) by type INT. However a hash function could be used to convert other types to INT. e.g. name[hash("string")]
  13. The indexing base can be 0 or 1, but is set for a whole "workspace"
  14. 14.0 14.1 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
  15. 15.00 15.01 15.02 15.03 15.04 15.05 15.06 15.07 15.08 15.09 15.10 15.11 15.12 15.13 15.14 15.15 15.16 15.17 15.18 15.19 Allows arrays of arrays which can be used to emulate most—but not all—aspects multi-dimensional arrays
  16. 16.0 16.1 C99 allows for variable size arrays; however there is almost no compiler available to support this new feature
  17. 17.0 17.1 17.2 17.3 17.4 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
  18. 18.0 18.1 Allows creation of fixed-size arrays in "unsafe" code, allowing for enhanced interoperability with other language
  19. COBOL arrays may be indexed with "INDEX" types, distinct from integer types
  20. 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
  21. 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
  22. Most Common Lisp implementations allow checking to be selectively disabled
  23. Associative Arrays - D Programming Language
  24. Behaviour can be tuned using compiler switches. As in DMD 1.0 bounds are checked in debug mode and unchecked in release mode for efficiency reasons
  25. 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
  26. Almost all Fortran implementations offer bounds checking options via compiler switches. However by default, bounds checking is usually turned off for efficiency reasons
  27. 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
  28. 28.0 28.1 28.2 28.3 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
  29. 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
  30. At least 2 dimensions (scalar numbers are 1×1 arrays, vectors are 1×n or n×1 arrays)
  31. Many implementations (Turbo Pascal, Object Pascal (Delphi), FreePascal) allow the behaviour to be changed by compiler switches and in-line directives
  32. Varies by implementation. Newer implementations (FreePascal, Object Pascal (Delphi)) permit heap-based dynamic arrays
  33. Standard Perl array data types do not support vectorized operations as defined here. However, the Perl Data Languageextension adds array objects with this ability
  34. 34.0 34.1 34.2 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"
  35. The standard Python array type, list, does not support vectorized operations as defined here. However, the numpy extension adds array objects with this ability
  36. The class Array is fixed-size, but OrderedCollection is dynamic
  37. Ch numerical features
Language/
Library
Create Determinant Transpose Element Column Row Eigenvalues
Fortran m = RESHAPE([x11, x12, ...], SHAPE(m)) TRANSPOSE(m) m(i,j) m(:,j) m(i,:)
Ch [37] m = {...} determinant(m) transpose(m) m[i-1][j-1] shape(m,0) shape(m,1) eigen(output, m, NULL)
Mathematica m = {{x11, x12, ...}, ...} Det[m] Transpose[m] m[[i,j]] m[[;;,j]] m[[i]] Eigenvalues[m]
MATLAB /
GNU Octave
m = [...] det(m) m' m(i,j) m(:,j) m(i,:) eig(m)
NumPy m = mat(...) linalg.det(m) m.T m[i-1,j-1] m[:,i-1] m[i-1,:] linalg.eigvals(m)
S-Lang m = reshape([x11, x12, ...], [new-dims]) m = transpose(m) m[i,j] m[*,j] m[j,*]
SymPy m = Matrix(...) m.T m[i-1,j-1]
Wolfram Language m = {{x11, x12, ...}, ...} Det[m] Transpose[m] m[[i,j]] m[[;;,j]] m[[i]] Eigenvalues[m]