APL2 Quick Reference Sheet

Complex Conjugate+X+23 0 ¯31 1J1 ⇔ 23 0 ¯31 1J¯1 AddA+B75 3 46 + 5 ¯2 880 1 54
Negate-X-1 ¯17 44.8¯1 17 ¯44.8 SubtractA-B75 3 46 - 5 ¯2 870 5 38
Signum×X×56.2 ¯1.4 0 0J¯31 ¯1 0 0J¯1 MultiplyA×B75 3 46 × 5 ¯2 8375 ¯6 368
Reciprocal÷X÷1 2 3 41 0.5 0.3333333333 0.25 DivideA÷B75 3 46 ÷ 5 ¯2 815 ¯1.5 5.75
Magnitude|X|23 0 ¯31 1J123 0 31 1.414213562 Residue (Remainder)A|B3|15.4 ¯21 ¯23 9 80.4 0 1 0 2
Exponential (eX)*X*1 22.718281828 7.389056099 PowerA*B3 7 16 * 3 2 0.527 49 4
Natural Logarithm⍟X⍟2.718281828 101 2.302585093 Logarithm (Base A)A⍟B2⍟10239.99859043
Ceiling⌈X⌈ ¯2.8 ¯1.1 0 1.1 2.5¯2 ¯1 0 2 3 MaximumA⌈B4 17 ¯2 ⌈ 3 64.8 14 64.8 1
Floor⌊X⌊ ¯2.8 ¯1.1 0 1.1 2.5¯3 ¯2 0 1 2 MinimumA⌊B4 17 ¯2 ⌊ 3 64.8 13 17 ¯2
Factorial!X!1487178291200 Binomial CoefficientA!BNumber of combinations of B items taken A at a time
Pi Times○X○÷1800.01745329252 Circular FunctionsA○BSee table below
Roll?XRandomly choose an item from ⍳X DealA?BRandomly choose A items from ⍳B without replacement
 Base ValueA⊥B2⊥0 1 0 0 0 117
 RepresentationA⊤B2 2 2 2 2 2 ⊤ 170 1 0 0 0 1
Grade Up⍋XList of indices that will sort X in ascending order Grade UpA⍋BAlphabetic sort: A gives the collating sequence
Grade Down⍒XList of indices that will sort X in descending order Grade DownA⍒BAlphabetic sort: A gives the collating sequence
Matrix Inverse⌹X Matrix DivideA⌹BMultiply A by the inverse of matrix B
Execute⍎XEvaluate APL2 expression given as character vector X 
Format⍕X⍕¯17.5'¯17.5' FormatA⍕B0 3 4 0 6 ¯2⍕ ¯12.14 30 17.1'¯12.140  30 1.7E1'
'06/06/0006' ⍕ 7 20 1969'07/20/1969'
 ExpandA\B1 0 0 1 1 0 1\3 1 4 23 0 0 1 4 0 2
 ExpandA⍀BExpand along the first axis of B
 ReplicateA/B2 0 ¯5 3 1/3 1 4 23 3 0 0 0 0 0 4 4 4 2
 ReplicateA⌿BReplicate along the first axis of B

Boolean Functions

Less ThanA<B1 2 3 < 3 2 11 0 0
Less Than or Equal ToA≤B1 2 3 ≤ 3 2 11 1 0
Equal ToA=B1 2 3 = 3 2 10 1 0
Not Equal ToA≠B1 2 3 ≠ 3 2 11 0 1
Greater Than or Equal ToA≥B1 2 3 ≥ 3 2 10 1 1
Greater ThanA>B1 2 3 > 3 2 10 0 1
Not~X~0 11 0
AndA∧B1 1 0 0 ∧ 1 0 1 01 0 0 0
OrA∨B1 1 0 0 ∨ 1 0 1 01 1 1 0
NandA⍲B1 1 0 0 ⍲ 1 0 1 00 1 1 1
NorA⍱B1 1 0 0 ⍱ 1 0 1 00 0 0 1
MatchA≡B1 if A and B have the same dimensions, nesting levels, and contents, 0 otherwise
MembershipA∈B'ace'∈'A Programming Language'1 0 1
FindA⍷B'ISSI'⍷'MISSISSIPPI'0 1 0 0 1 0 0 0 0 0 0

Circular Functions

0○X(1-X*2)*0.5  0○X(1-X*2)*0.5
1○XSine ¯1○XArcsine
2○XCosine ¯2○XArccosine
3○XTangent ¯3○XArctangent
4○X(1+X*2)*0.5 ¯4○X(¯1+X*2)*0.5
5○XHyperbolic Sine ¯5○XInverse Hyperbolic Sine
6○XHyperbolic Cosine ¯6○XInverse Hyperbolic Cosine
7○XHyperbolic Tangent ¯7○XInverse Hyperbolic Tangent
8○X (¯1-X*2)*0.5 for X<0
-(¯1-X*2)*0.5 for X≥0
 ¯8○X-8○X
9○XReal part of X ¯9○XX
10○X|X ¯10○X+X (complex conjugate)
11○XImaginary part of X ¯11○X0J1×X
12○XPhase of X ¯12○X*0J1×X

Note: one way to get the arctangent of Y÷X in the proper quadrant (-π to +π) is 12○X+11○Y

Structural Functions

 IndexA⌷BSelect items from B using indices A
Index Generator⍳X⍳81 2 3 4 5 6 7 8 Index OfA⍳B'HELLO'⍳'ELP'2 3 6
Not (see Boolean Functions) WithoutA~B(⍳8)~1 4 52 3 6 7 8
Shape ofρXList of dimensions of X ReshapeAρB
3 4ρ'ABCDEFGHIJKL' ⇔ 
ABCD
EFGH
IJKL
Reverse⌽X⌽⍳88 7 6 5 4 3 2 1 RotateA⌽B3⌽⍳84 5 6 7 8 1 2 3
Reverse Vertically⊖X
⊖3 4ρ'ABCDEFGHIJKL' ⇔ 
IJKL
EFGH
ABCD
 Rotate VerticallyA⊖B
1⊖3 4ρ'ABCDEFGHIJKL' ⇔ 
EFGH
IJKL
ABCD
Transpose⍉X
⍉3 4ρ'ABCDEFGHIJKL' ⇔ 
AEI
BFJ
CGK
DHL
 TransposeA⍉BThe elements of A give the new order for the axes of B
First↑XThe first element of X TakeA↑B¯3↑⍳86 7 8
  DropA↓B3↓⍳84 5 6 7 8
Enclose⊂XRepresent array X as a scalar PartitionA⊂B1 0 1 1 2 2 2 3 ⊂ 'ABCDEFGH'
┌→─────────────────┐
│┌→┐ ┌→─┐ ┌→──┐ ┌→┐│
││A│ │CD│ │EFG│ │H││
│└─┘ └──┘ └───┘ └─┘│
└∊─────────────────┘
Disclose⊃XRemove one level of nesting from X PickA⊃BIndices A select an item from within nested array B
Depth≡XDeepest level of nesting in X Match (see Boolean Functions)
Ravel,XChange X to a vector, preserving depth CatenateA,BConnect two arrays along their last axis
Enlist∈XChange X to a vector of depth 1 Membership (see Boolean Functions)

Operators

Eachf¨XApply function f to each item of X
Reductionf/XApply function f along the last axis (rows) of X
Reductionf⌿XApply function f along the first axis of X
N-Wise ReductionA f/BApply function f along the last axis (rows) of B with a moving window of size A
N-Wise ReductionA f⌿BApply function f along the first axis of B with a moving window of size A
Scanf\XApply function f along the last axis (rows) of X, displaying intermediate results
Scanf⍀XApply function f along the first axis of X, displaying intermediate results
Inner ProductA f.g BApply function g between rows of A and corresponding columns of B, then apply f along each result
Outer ProductA ∘.f BApply function f between every combination of one element from A and one element from B

Note: A+.×B is the standard matrix multiplication operation for matrices A and B.

Scans of boolean vectors are often useful:

∧\XMake everything 0 after the first 0
<\XMake everything 0 after the first 1
≤\XMake everything 1 after the first 0
∨\XMake everything 1 after the first 1
X∨≠\XMake everything 1 between odd and even 1s