# 阵列编程

## 阵列的概念

Kenneth E. Iverson将阵列编程（实指APL）背后的原理描述如下[3]

[...]

## 语言

### 标量语言

```for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a[i][j] += b[i][j];
```

```a = a + b
```

```a(:,:) = a(:,:) + b(:,:)
```

### 阵列语言

```A := A + B;
```

#### APL

APL使用单一字符Unicode符号而没有语法糖

```A ← A + B
```

```A +← B
```

#### Analytica

```A := A + B;
```

#### BASIC

Dartmouth BASIC在其（1966年）第三版中拥有用于矩阵和数组操纵的MAT语句：

```DIM A(4),B(4),C(4)
MAT A = 1
MAT B = 2 * A
MAT C = A + B
MAT PRINT A,B,C
```

#### Mata

Stata的矩阵编程语言Mata支持阵列编程。下面例子展示了加法、乘法、一个矩阵和一个标量的加法、逐个元素的乘法、下标和Mata的多个逆矩阵之一：

```. 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
```

#### MATLAB

MATLAB中的实现允许同使用Fortran语言一样的经济型表达式：

```A = A + B;
```

GNU Octave语言是MATLAB语言的一种变体，它向最初的语言扩展了增广赋值

```A += B;
```

MATLAB和GNU Octave二者都固有的支持线性代数运算比如矩阵乘法、逆矩阵和一些线性方程组的数值解，甚至使用了摩尔－彭若斯广义逆[8][9]

``` a * b;
```

``` A(:)' * B(:);
```

#### rasql

Rasdaman光栅查询语言是面向数据库的阵列编程语言。例如，两个数组可以使用如下查询来相加：

```SELECT A + B
FROM   A, B
```

#### R

R语言缺省的支持阵列范型。下列例子展示计算两个矩阵的乘法和随后的一个标量（实际上是一个元素的向量）与一个向量的加法的过程：

```> 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
```

## 引用

1. ^ Stéfan van der Walt; S. Chris Colbert & Gaël Varoquaux. The NumPy array: a structure for efficient numerical computation. Computing in Science and Engineering (IEEE). 2011, 13 (2): 22–30. Bibcode:2011CSE....13b..22V. arXiv:1102.1523. doi:10.1109/mcse.2011.37.
2. ^ Michael Schidlowsky. Java and K. [2008-01-23]. （原始内容存档于2017-11-01）.
3. ^ Iverson, K. E. Notations as a Tool of Thought.. Communications of the ACM. 1980, 23 (8): 444–465 [2011-03-22]. doi:10.1145/358896.358899. （原始内容存档于2013-09-20）.
4. ^ Surana P. Meta-Compilation of Language Abstractions. (PDF). 2006 [2008-03-17]. （原始内容 (PDF)存档于2015-02-17）.
5. ^ Kuketayev. The Data Abstraction Penalty (DAP) Benchmark for Small Objects in Java.. [2008-03-17]. （原始内容存档于2009-01-11）.
6. ^ Chatzigeorgiou; Stephanides. Evaluating Performance and Power Of Object-Oriented Vs. Procedural Programming Languages. (编) Blieberger; Strohmeier. Proceedings - 7th International Conference on Reliable Software Technologies - Ada-Europe'2002. Springer. 2002: 367. ISBN 978-3-540-43784-0.
7. ^
8. ^ GNU Octave Manual. Arithmetic Operators.. [2011-03-19]. （原始内容存档于2010-12-25）.
9. ^ MATLAB documentation. Arithmetic Operators.. [2011-03-19]. （原始内容存档于2011-09-25）.