Professional Documents
Culture Documents
An Introduction To APL For Scientists and Engineers PDF
An Introduction To APL For Scientists and Engineers PDF
K. E. IVERSON
IBM PHILADELPHIA SCIENTIFIC CENTER TECHNICAL REPORT NO. 320-3019 MARCH 1973
AN INTRODUCTION TO APL
K. E. IVERSON
EXPERIMENTATION 2
SYSTEMATIC EXPERIMENTATION 3
MONADIC FUNCTIONS 10
ORDER OF EXECUTION 11
EVALUATION OF SERIES 12
FUNCTION DEFINITION 13
HIGHER-DIMENSIONAL ARRAYS 14
INNER PRODUCT 15
LINEAR EQUATIONS 16
CURVE FITTING 17
SELECTION FUNCTIONS 18
ITERATION 19
IDENTITIES 20
PROOFS 21
REFERENCE MATERIAL 22
REFERENCES
EXPERIMENTATION
A. Simple expressions:
.. * r
3-4
1
-3
SYSTEMATIC EXPERIMENTATION
A. On single quantities:
1
Vary one argument systematically.
o
B. On lists of numbers:
311 2 3 4 5 6 7
1 2 0 1 2 a 1
25
8+1 2 3 4 5 6 7
31 s
1 2 a 1 2 0 1
8*3
1 8 27 64 125 216 343
SxS
1 4 9 16 25 36 49
5+1 2 3 4 5 6 7
So . xS So. +5
1 2 3 4 5 6 7 2 3 4 5 6 7 8
2 4 6 8 10 12 14 3 4 5 6 7 8 9
3 69 12 15 18 21 1.+ 5 6 7 8 9 10
4 8 12 16 20 24 28 5 6 7 8 9 10 11
5 10 15 20 25 30 35 6 7 8 10 11 12
9
6 12 18 24 30 36 42 7 8 9 10 11 12 13
7 14 21 28 35 42 49 8 9 10 11 12 13 14
B+2 3
Bo . xS 2 3 0 .+5
2 4 6 10 12 14
8 31.+567 8 9
3 6 9 12 15 18 21 4 567 8 9 10
c. Examine the tables for patterns and try to see why each
function generates the particular pattern.
E. The Qyt~r Q~Qg~Qt (0.+ and o.x and o.r I etc.) applies to
higher-dimensional arrays in an obvious way. Try, for
example:
Q+l 2 3 4
Qo.xQo.XQ
( LOCK
SHIFT
SET [
-5
A. Graph of a parabola:
X+l 2 3 4 5 6 7
V+(X-3)x(X-5)
V
8 3 0 1 0 3 8
R+8 7 6 5 4 3 2 1 0 1
Ra • =V
1 0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 1 0 0 a 1 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 0 1 0 0
0 0 0 1 0 0 0
B. Bar chart:
Ro. :-.;V
1 0 0 a a 0 1
1 0 0 0 0 0 1
1 a a 0 0 0 1
1 0 a 0 0 0 1
1 0 0 0 0 0 1
1 1 0 0 0 1 1
1 1 0 0 0 1 1
1 1 a 0 0 1 1
1 1 1 1 0 1 1
1 1 1 1 1 1 1
1 5
.l X 16 Pressing the attention button will
11000 interrupt any activity of the computer.
-6
A. Indexing:
X+2 3 5 7 11
X[4J
7
X[1 2 3J
235
X[5 4 3 2 1J
11 7 5 3 2
X[4 1 3J
7 2 5
B. Characters:
'ABCDEFGHI '[8 5 1 4 10 3 8 9 5 6J
HEAD CHIEF
, *'[2 1 2 2 1 2 2 1 2J
* ** ** *
c. Plotting:
X+1 2 3 4 5 6 7
V+(X-3)x(X-5)
R+8 7 6 5 4 3 2 1 0 1
, *'[l+(Ro.=V)]
-7
A. Negation:
X+3
-x
3
P+l 2 3 4 5 6 7
Q+P-4
R+P+2
-p
1 2 3 4 5 6 7
-Q
3 2 1 a 1 2 3
-R
5 1 1. 5 2 2.5 3 3.5
I r *
[Note that each of these symbols denotes either a
function of two arguments (as in X+Y) or of one argument
(as in +Y) just as the symbol denotes either
§Ybtra~tiQn (as in X-Y) or ngggtiQn (as in -1) in
conventional notation.]
To. xT
xTo . xT
' +'[2+x(To.xT)J
X+-7
(X-3)x(X-5)
8
'iJZ+-F X
[1J Z+-(X-3)x(X-5) 'iJ
F 1 2 3 4 5 6 7
8 3 0 1 a 3 8
V Z+D X
[ 1 ] Z+-O=(7!X) v
D 13
0
D 868
1
D 6 7 8 9 10 11 12 13 14
0 1 0 0 0 0 0 0 1
c. A plotting function.
'VZ+-PLOT T
[lJ Z+' *'[1+TJ 'V
R+-8 7 6 5 4 3 2 1 0 1
PLOT Ro.=F 1 2 3 4 5 6 7
-9
REDUCTION
MONADIC FUNCTIONS
y-;-X
2 0.5 0 0.25 0.4
-;-X
1 0.5 0.3333333333 0.25 0.2
ORDER OF EXECUTION
PARENTHESES
Ix 2 1 1 1 2
21012 ~ - ! I X
o .5 1 1 1 o. 5
15 10
3x4+S
27 o
The main advantage of the hierarchy of +, x, and * in
conventional notation is in writing polynomials. However, a
polynomial can be written in terms of its vector of
coefficients and vector of exponents as follows:
X+-5
+/3 1 4 2xX*O 1 2 3
358
EVALUATION OF SERIES
(X*K)f!K
0.02083333333
(X*K)f!K
1 0.5 0.125 002083333333 0.002604166667
*-x
0.6065306597
SxAS
[ LOCK
1.000223796
SET [
~HIFT
C+2xK
C
02468
FUNCTION DEFINITION
.5 TERM 3
0.02083333333
.5 TERM 0 1 2 3 4
1 0.5 0.125 0.02083333333 0.002604166667
IJ Z+-X SUM K
[lJ Z++/X TERM K IJ
.5 SUM 1+15
1.6484375
'VZ+COSH X
[lJ Z+X SUM 2x-l+128 IJ
COSH 3
10.067662
tVZ+-X HYP Y
[lJ Z+((X*2)+(Y*2»*.5 IJ
3 5 6 HYP 4 12 8
5 13 10
HIGHER-DIMENSIONAL ARRAYS
FORMATION
13 14 15 16
17 18 19 20
21 22 23 24
SHAPE
INNER PRODUCT
M+(14)o.~14
N+4 4P116
M N
1 0 0 0 1 2 3 4
1 100 567 8
1 110 9 10 11 12
1 1 1 1 13 14 15 16
M+. xN
1 2 3 4
6 8 10 12
15 18 21 24
28 32 36 40
1 2 3 4+.xM
10 9 7 4
M+.xl 2 3 4
1 3 6 10
LINEAR EQUATIONS
A-+-?4 4p9
X-+-3 2 5 7
A
6 4 8 5
9 4 6 7
8 4 2 7
781 7
B-+-A+.xX
51 91 117 82
~A Inverse of A.
0.16091954023 0.09195402299 0.17241379310 0.08045977011
0.09195402299 0.19540229885 0.24137931034 0.04597701149
0.19827586207 0.17241379310 0.19827586207 0.27586206897
0.13936781609 0.06321839080 0.30603448276 0.36781609195
(ffiA)+.xB
3 2 5 7
-17
CURVE FITTING
Y+X*3
Y
1 8 27 64
Xo.*0,12
111
124
139
1 4 16
C+Yff]Xo.*O,12
C
10.5 16.7 7.5
(Xo.*O,12)+.xC
1.3 7.1 27.9 63.7
YH:!Xo.*0,t3
1.372E-14 -2.422E-14 1.232E-14 1
OTHER FUNCTIONS
Xo .x14
1 2 3 4
2 4 6 8
3 6 9 12
4 8 12 16
SELECTION FUNCTIONS
INDEXING
P+2 3 5 7 11
M+3 4Pl12
1 2 34
5 6 78
9 10 11 12
P[3J
5
P[2 3 4J
357
P[4 1 5 2 3J Permutation.
7 2 11 3 5
765
11 10 9
M[ 2 ; J Entire row.
567 8
M[ ; 3 J Entire column.
3 7 11
M[ ; 3 2 J Entire columns.
3 2
7 6
11 10
2tP 3tP
2 3 5 7 11
2i-P 2 3tM
5 7 11 123
567
ITERATION
BRANCHING
F 7
7
10
13
16
CONDITIONAL BRANCH
\j Z+BIN N
rl] 2+-1
[2J Z+-(2,0)+0,2
[3J -+2xN~pZ \J
BIN 4
1 4 641
TRACING
T6BIN+-2 3
Z+-BIN 2
BIN~2J 1 1
BIN[3] 2
BIN[2] 1 2 1
BIN[3] 0
T6BIN+O
BIN 2
121
-20
IDENTITIES
(r/A)=-L/-A (A/L)=~V/~L
(L/A)=-r/-A (~/L)=~=/~L
ASSOCIATIVITY
A/,«M+.xN)+.xP)=M+.x(N+.xP)
A/,«CV.AD)V.AE)=CV.A(Dv.AE)
A/,«ML.+N)L.+P)=ML.+(NL.+P)
DISTRIBUTIVITY
A/,(M+.x(N+P»)=(M+.xN)+(M+.xP)
A/,(CV.A(DvE)=(CV.AD)V(CV.AE)
A/,(ML.+(NLP»=(ML.+N)LCML.+P)
PARTITIONING
A/,(M+.xN)=«U/M)+.xU/[lJN)+(C~U)/M)+.x(~U)/[lJN
A/,(CV.AD)=«U/C)v.AU/[lJD)+«~U)/C)V.A(~U)/[lJD
PROOFS
Thrn 1: +/SxW
Sx+/W Distributivity of x over +
Thm 2: +/V o . xW
Vx+/W ThIn 1 applied to each element of V
ThIn 3: +/+/Vo.xW
+/ vx ( +/ W) ThIn 2
(+/V)x(+/W) ThIn 1 (with +/W for Sand V for W)
CC P X)xCD P X)
Ct/CXX*-ltlpC)X(t/DxX*-ltlpD) Definition of P
t/t/(CXX*-ltlpC)O.xCDxX*-ltlpD) Thm 3
t/t/(Co .xD)x(X*-ltlpC)O .x(X*-ltlpD) Thrn 4
+/+/(Co .xD)xX*C-l+1pC)O .tC-ltlpD) Thrn 5
-22
REFERENCE MATERIAL
CORRECTIONS
'JBIN
[4J [OJ
V Z+-BIN N
[1 ] Z+-1
[2J Z+(Z,O)+o,Z
[3 ] -+2 x N?:.pZ
'V
[4 ]
[2] Z+(Z,O)-O,Z
SCALAR FUNCTIONS
generator3
10 +-+ an empty vector