Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Array programming
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Array languages=== In array languages, operations are generalized to apply to both scalars and arrays. Thus, ''a''+''b'' expresses the sum of two scalars if ''a'' and ''b'' are scalars, or the sum of two arrays if they are arrays. An array language simplifies programming but possibly at a cost known as the ''abstraction penalty''.<ref>{{cite thesis|author=Surana P |title=Meta-Compilation of Language Abstractions. |year=2006 |url=https://dl.acm.org/doi/book/10.5555/1195058}}</ref><ref>{{cite web |last= Kuketayev |title= The Data Abstraction Penalty (DAP) Benchmark for Small Objects in Java. |url= http://www.adtmag.com/joop/article.aspx?id=4597 |access-date= 2008-03-17 |archive-url= https://web.archive.org/web/20090111091710/http://www.adtmag.com/joop/article.aspx?id=4597 |archive-date= 2009-01-11 |url-status= dead }}</ref><ref>{{Cite book |last1= Chatzigeorgiou |last2= Stephanides |editor-last= Blieberger |editor2-last= Strohmeier |contribution= Evaluating Performance and Power Of Object-Oriented Vs. Procedural Programming Languages |title= Proceedings - 7th International Conference on Reliable Software Technologies - Ada-Europe'2002 |year= 2002 |pages= 367 |publisher= Springer |url= https://books.google.com/books?id=QMalP1P2kAMC&q=%22abstraction+penalty%22 |isbn= 978-3-540-43784-0 }}</ref> Because the additions are performed in isolation from the rest of the coding, they may not produce the optimally most [[algorithmic efficiency|efficient]] code. (For example, additions of other elements of the same array may be subsequently encountered during the same execution, causing unnecessary repeated lookups.) Even the most sophisticated [[optimizing compiler]] would have an extremely hard time amalgamating two or more apparently disparate functions which might appear in different program sections or sub-routines, even though a programmer could do this easily, aggregating sums on the same pass over the array to minimize [[Computational overhead|overhead]]). ====Ada==== The previous C code would become the following in the [[Ada (programming language)|Ada]] language,<ref>[http://www.adaic.org/standards/05rm/html/RM-TTL.html Ada Reference Manual]: [http://www.adaic.org/resources/add_content/standards/05rm/html/RM-G-3-1.html G.3.1 Real Vectors and Matrices]</ref> which supports array-programming syntax. <syntaxhighlight lang="ada"> A := A + B; </syntaxhighlight> ====APL==== APL uses single character Unicode symbols with no syntactic sugar. <syntaxhighlight lang="apl"> A ← A + B </syntaxhighlight> This operation works on arrays of any rank (including rank 0), and on a scalar and an array. Dyalog APL extends the original language with [[augmented assignment]]s: <syntaxhighlight lang="apl"> A +← B </syntaxhighlight> ====Analytica==== Analytica provides the same economy of expression as Ada. <pre> A := A + B; </pre> ====BASIC==== [[Dartmouth BASIC]] had MAT statements for matrix and array manipulation in its third edition (1966). <syntaxhighlight lang="basic"> DIM A(4),B(4),C(4) MAT A = 1 MAT B = 2 * A MAT C = A + B MAT PRINT A,B,C </syntaxhighlight> ====Mata==== [[Stata]]'s matrix programming language Mata supports array programming. Below, we illustrate addition, multiplication, addition of a matrix and a scalar, element by element multiplication, subscripting, and one of Mata's many inverse matrix functions. <syntaxhighlight lang="stata"> . mata: : A = (1,2,3) \(4,5,6) : A 1 2 3 +-------------+ 1 | 1 2 3 | 2 | 4 5 6 | +-------------+ : B = (2..4) \(1..3) : B 1 2 3 +-------------+ 1 | 2 3 4 | 2 | 1 2 3 | +-------------+ : C = J(3,2,1) // A 3 by 2 matrix of ones : C 1 2 +---------+ 1 | 1 1 | 2 | 1 1 | 3 | 1 1 | +---------+ : D = A + B : D 1 2 3 +-------------+ 1 | 3 5 7 | 2 | 5 7 9 | +-------------+ : E = A*C : E 1 2 +-----------+ 1 | 6 6 | 2 | 15 15 | +-----------+ : F = A:*B : F 1 2 3 +----------------+ 1 | 2 6 12 | 2 | 4 10 18 | +----------------+ : G = E :+ 3 : G 1 2 +-----------+ 1 | 9 9 | 2 | 18 18 | +-----------+ : H = F[(2\1), (1, 2)] // Subscripting to get a submatrix of F and : // switch row 1 and 2 : H 1 2 +-----------+ 1 | 4 10 | 2 | 2 6 | +-----------+ : I = invsym(F'*F) // Generalized inverse (F*F^(-1)F=F) of a : // symmetric positive semi-definite matrix : I [symmetric] 1 2 3 +-------------------------------------------+ 1 | 0 | 2 | 0 3.25 | 3 | 0 -1.75 .9444444444 | +-------------------------------------------+ : end </syntaxhighlight> ====MATLAB==== The implementation in [[MATLAB]] allows the same economy allowed by using the Fortran language. <syntaxhighlight lang="matlab"> A = A + B; </syntaxhighlight> A variant of the MATLAB language is the [[GNU Octave]] language, which extends the original language with augmented assignments: <syntaxhighlight lang="octave"> A += B; </syntaxhighlight> Both MATLAB and GNU Octave natively support [[linear algebra]] operations such as matrix multiplication, [[matrix inversion]], and the numerical solution of [[system of linear equations]], even using the [[Moore–Penrose pseudoinverse]].<ref>{{cite web |title= GNU Octave Manual. Arithmetic Operators. |url= https://www.gnu.org/software/octave/doc/interpreter/Arithmetic-Ops.html |access-date= 2011-03-19}}</ref><ref>{{cite web |title= MATLAB documentation. Arithmetic Operators. |url= http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html |access-date= 2011-03-19 |archive-date= 2010-09-07 |archive-url= https://web.archive.org/web/20100907074906/http://www.mathworks.com/help/techdoc/ref/arithmeticoperators.html |url-status= dead }}</ref> The [[Nial]] example of the inner product of two arrays can be implemented using the native matrix multiplication operator. If <code>a</code> is a row vector of size [1 n] and <code>b</code> is a corresponding column vector of size [n 1]. a * b; By contrast, the [[entrywise product]] is implemented as: a .* b; The inner product between two matrices having the same number of elements can be implemented with the auxiliary operator <code>(:)</code>, which reshapes a given matrix into a column vector, and the [[transpose]] operator <code>'</code>: A(:)' * B(:); ====rasql==== The [[Rasdaman#Raster Query Language|rasdaman query language]] is a database-oriented array-programming language. For example, two arrays could be added with the following query: <syntaxhighlight lang="sql"> SELECT A + B FROM A, B </syntaxhighlight> ====R==== The R language supports [[array paradigm]] by default. The following example illustrates a process of multiplication of two matrices followed by an addition of a scalar (which is, in fact, a one-element vector) and a vector: <syntaxhighlight lang="rout"> > A <- matrix(1:6, nrow=2) # !!this has nrow=2 ... and A has 2 rows > A [,1] [,2] [,3] [1,] 1 3 5 [2,] 2 4 6 > B <- t( matrix(6:1, nrow=2) ) # t() is a transpose operator !!this has nrow=2 ... and B has 3 rows --- a clear contradiction to the definition of A > B [,1] [,2] [1,] 6 5 [2,] 4 3 [3,] 2 1 > C <- A %*% B > C [,1] [,2] [1,] 28 19 [2,] 40 28 > D <- C + 1 > D [,1] [,2] [1,] 29 20 [2,] 41 29 > D + c(1, 1) # c() creates a vector [,1] [,2] [1,] 30 21 [2,] 42 30 </syntaxhighlight> ====Raku==== Raku supports the array paradigm via its Metaoperators.<ref>{{cite web |url=https://docs.raku.org/language/operators#Metaoperators |title=Metaoperators section of Raku Operator documentation}}</ref> The following example demonstrates the addition of arrays @a and @b using the Hyper-operator in conjunction with the plus operator. <syntaxhighlight lang="raku"> [0] > my @a = [[1,1],[2,2],[3,3]]; [[1 1] [2 2] [3 3]] [1] > my @b = [[4,4],[5,5],[6,6]]; [[4 4] [5 5] [6 6]] [2] > @a »+« @b; [[5 5] [7 7] [9 9]] </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)