In [ ]:
2+3
In [ ]:
x=: 2
In [ ]:
y=:3
In [ ]:
x+y
In [ ]:
2 + 3
In [ ]:
2 - 3
In [ ]:
2 * 3
In [ ]:
2 % 3
In [ ]:
2 ^ 3
In [ ]:
2 ^ 0.5
In [ ]:
_2 ^ 0.5
In [ ]:
2 ^. 3
In [ ]:
2 + 5 6 7
In [ ]:
2 3 4 * 5 6 7
In [ ]:
2 3 4 - 5 6 7
In [ ]:
2 3 4 % 5 6 7
In [ ]:
2 3 % 5 6 7 NB. error because arguments do not match
In [ ]:
i. 7
In [ ]:
1 + i. 7
In [ ]:
2 ^ i. 7
In [ ]:
(i. 7) ^ 0.5
In [ ]:
3 5 $ 3 1 4 2
In [ ]:
x=:3 5 $ 3 1 4 2
In [ ]:
x * x
In [ ]:
i. 7
In [ ]:
x=:i. 7
In [ ]:
x
In [ ]:
x ^ 2
In [ ]:
power=: ^
In [ ]:
x power 2
In [ ]:
x power 0.5
In [ ]:
2 - 3 NB. Subtraction (dyadic)
In [ ]:
-3 NB. Negation (monadic)
In [ ]:
2 % 3 NB. Division
In [ ]:
% 3 NB. Reciprocal
In [ ]:
2 ^ 3 NB. Power
In [ ]:
^0 1 2 NB. Exponential
In [ ]:
2 ^ 3 4 5 6
In [ ]:
2 ^ 0.5 3 _5
In [ ]:
_2 ^ 0.5 6 7
In [ ]:
0 0 1 1 +. 0 1 0 1
In [ ]:
2 3 4 5 +. 10 20 30 40
In [ ]:
2 3 4 5 *. 10 20 30 40
In [ ]:
2 3 4 5 | 10 20 30 40
In [ ]:
2 3 4 5 <. 10 20 30 40
In [ ]:
2 3 4 5 >. 10 20 30 40
Adverbs modify verbs (functions) to produce new verbs.
For example, the adverb / inserts its verb argument between the items of its argument.
Thus, +/ is summation and */ is product. These are the "big sigma" and "big pi" of conventional notation. Moreover, / can be applied to any function: >./ is maximum, <./ is minimum, +./ is or or GCD, *./ is and or LCM, etc.
In [ ]:
x=: 1 + i. 7
In [ ]:
x
In [ ]:
+/ x
In [ ]:
*/ x
In [ ]:
>./ x
In [ ]:
<./ x
In [ ]:
+./ x
In [ ]:
*./ x
If f is a verb (function), then f/ is a verb, and, like any other verb, it can be monadic or dyadic. The monadic meaning is "insert". The dyadic meaning is "table", that is, a function table.
Function tables are a good way to organize systematic experimentation on unfamiliar functions.
In [ ]:
x=:i.9
In [ ]:
x
In [ ]:
x +/ x
In [ ]:
x */ x
In [ ]:
x </ x
In [ ]:
x >/ x
In [ ]:
x >./ x
In [ ]:
x <./ x
In [ ]:
x +./ x
In [ ]:
x *./ x
In [ ]:
x |/ x
The Hilbert matrix is a simple function on the addition table. As shown below, its pattern is more apparent in the extended precision (rational) domain.
Although Hilbert matrices are notoriously unstable under inversion, the matrix inverse (%.) gives an exact integer result (as shown below):
In [ ]:
x=:i.7
In [ ]:
x +/ x
In [ ]:
% 1 + x +/ x
In [ ]:
y=:i.7x NB. The final x gives extended precision
In [ ]:
H=:% 1 + y +/ y
In [ ]:
H
In [ ]:
%. H
In [ ]:
x=: i. 7
In [ ]:
x !/ x
In [ ]:
m=: x !/ x
In [ ]:
%. m NB. The alternating binomial coefficients
In [ ]:
+/ m
In [ ]:
+/ %. m
In [ ]:
x=: 1+i.7
In [ ]:
x
In [ ]:
+/\ x NB. Subtotals
In [ ]:
<\ x
In [ ]:
*/\ x NB. Progressive products
In [ ]:
<./\ x
In [ ]:
>./\ x
In [ ]:
+./\ x
In [ ]:
*./\ x
In [ ]:
p=:9 ?. 9 NB. A random permutation
In [ ]:
p
In [ ]:
p { p
In [ ]:
p { p { p
In [ ]:
6 9$p
In [ ]:
{/ 6 9$p
In [ ]:
{/\ 6 9$p
In [ ]:
p
In [ ]:
p{p
In [ ]:
p{p{p
In [ ]:
{/ 6 9 $ p
In [ ]:
{/\6 9 $ p
In [ ]:
p
In [ ]:
C. p
In [ ]:
#&> C. p
In [ ]:
*./ #&> C. p
In [ ]:
require 'plot viewmat'
In [ ]:
color =: viewmat
In [ ]:
x=:i. 5
In [ ]:
x +/ x
In [ ]:
color x +/ x
In [ ]:
x -/ x NB. Subtraction table
In [ ]:
color x -/ x
In [ ]:
pn=: i: _3
In [ ]:
pn
In [ ]:
pn */ pn
In [ ]:
color pn */ pn
In [ ]:
pn
In [ ]:
*pn
In [ ]:
* pn */ pn
In [ ]:
color * pn */ pn
In [ ]:
y=: 1 + i. 6
In [ ]:
y |/ y
In [ ]:
color y |/ y
In [ ]:
2 | (i.8) +/ (i.8)
In [ ]:
color 2 | (i.8) +/ (i.8)
In [ ]:
2 | (i.2) !/ (i.2)
In [ ]:
color 2 | (i.2) !/ (i.2)
In [ ]:
2 | (i.4) !/ (i.4)
In [ ]:
color 2 | (i.4) !/ (i.4)
In [ ]:
2 | (i.16) !/ (i.16)
In [ ]:
color 2 | (i.16) !/ (i.16)
In [ ]:
3 | (i.16) !/ (i.16)
In [ ]:
color 3 | (i.16) !/ (i.16)
In [ ]:
require'plot'
In [ ]:
x=: i.5
In [ ]:
y=: *: x
In [ ]:
x;y
In [ ]:
plot x;y
In [ ]:
PLOT=: 'stick,line'&plot
In [ ]:
PLOT x;y
In [ ]:
z=: %: x NB. Square root
In [ ]:
x;>y;z
In [ ]:
PLOT x;>y;z
In [ ]:
sin=: 1&o.
In [ ]:
cos=: 2&o.
In [ ]:
x=: 0.1 * i: 21
In [ ]:
x
In [ ]:
PLOT x;>(sin x);(cos x)
In [ ]:
] x=:i. 8 NB. The identity function ] displays x
In [ ]:
] y=: x ^ 2
In [ ]:
] z=: 2 ^ x
In [ ]:
}. y NB. Drop first of y
In [ ]:
}: y NB. Drop last of y
In [ ]:
(}. y) - (}: y) NB. Subtraction of adjacent items of y
In [ ]:
(}. z) % (}: z) NB. Ratio of adjacent items of z
In [ ]:
dif=: }. - }:
In [ ]:
rat=: }. % }:
In [ ]:
dif y
In [ ]:
dif dif y
In [ ]:
rat z
In [ ]:
rat rat z
In [ ]:
dif rat z
In [ ]:
c=:x*x*x NB. cube
In [ ]:
dif c
In [ ]:
dif dif c
In [ ]:
dif dif dif c
In [ ]:
q=:x*x*x*x*x
In [ ]:
q
In [ ]:
dif dif q
In [ ]:
dif ^:2 q
In [ ]:
dif ^: 0 1 2 3 4 5 6 q
In the list a=: 1 3 5 7 9 11, the expression 2{a selects the item with the value 5, and 2 is said to be its "index".
The function f=: >:@+: gives each element of a as a function of its index, and is said to define the "series" a. This is true only for arguments in the list i.#a, but if we assume (or assert) that f applies to every index, then it is said to define an "infinite" series.
In [ ]:
a=: 1 3 5 7 9 11
In [ ]:
2{a
In [ ]:
7{a
In [ ]:
f=:>:@+: NB. >: is increment, and +: is double
In [ ]:
f 2
In [ ]:
f 7
In [ ]:
f i.20
Using the list b=: 0 _
2 _
4 0 16 50 108 we can define a
function for the corresponding series. We will use
repeated application of the difference function
dif=:}.-}: to obtain the coefficients for the
falling factorial polynomial ffp=:p.!._
1. These matters will be discussed more fully in a subsequent chapter on polynomials.
In [ ]:
b=:0 _2 _4 0 16 50 108
In [ ]:
dif=:}.-}:
In [ ]:
dif^:0 1 2 3 4 b
In [ ]:
c=:0 _2 0 6 0 NB. First column of table of differences
In [ ]:
]ncoeff=: c % !i.#c NB. Normalized coefficients
In [ ]:
ffp=:p.!._1 NB. Falling factorial polynomial
In [ ]:
ncoeff ffp i.7 NB. Reproduces b
In [ ]:
f=: ncoeff&ffp NB. A function that defines (and extends) the series
In [ ]:
f i. 15
In [ ]:
b=: 3+(i.7)^5
In [ ]:
b
In [ ]:
dif^:(i.7) b
In [ ]:
c=: {."1 dif^:(i.7) b
In [ ]:
c
In [ ]:
(c%!i.#c) ffp i.10
In [ ]:
x=:5
In [ ]:
x^3
In [ ]:
2*x^3
In [ ]:
x=:0 1 2 3 4 5
In [ ]:
2*x^3
In [ ]:
c=:3 1 4 2
In [ ]:
c p. x
In [ ]:
(3*x^0)+(1*x^1)+(4*x^2)+(2*x^3)
Polynomials are important for a number of reasons:
In [ ]:
c=:1 3 3 1
In [ ]:
d=:4 3 2 1
In [ ]:
e=:1 2 1
In [ ]:
(c p. x)+(d p. x)
In [ ]:
(c+d) p. x
In [ ]:
(c p. x)*(e p. x)
In [ ]:
1 5 10 10 5 1 p. x
In [ ]:
(1%!i.10) p. x
In [ ]:
^ x
In [ ]:
c+(e,0)
In [ ]:
((e,0) p. x) = (e p. x)
In [ ]:
c */ e
In [ ]:
</. c */ e NB. Box diagonals
In [ ]:
+//. c */ e NB. Sum diagonals
In [ ]:
(1 5 10 10 5 1 p. x) = ((c p. x) * (e p. x))
In [ ]:
c
In [ ]:
i.#c
In [ ]:
c * i. # c
In [ ]:
der=:}. c * i. # c NB. Coefficients of derivative
In [ ]:
der
In [ ]:
require 'plot'
In [ ]:
PLOT=:'stick,line'&plot
In [ ]:
PLOT x;>(c p. x);(der p. x)
In [ ]:
c
In [ ]:
1+i.#c
In [ ]:
c % 1+i.#c
In [ ]:
int=: 20 , c % 1+i.#c
In [ ]:
int
In [ ]:
PLOT x;>(c p. x);(int p. x)
In [ ]:
DER=:}.@(]*i.@#)
In [ ]:
INT=:[,(] % >:@i.@#@]) NB. >: is the "increment" function
In [ ]:
DER c
In [ ]:
20 INT c
In [ ]:
DER 20 INT c
In [ ]:
ce=:1 % ! i. 7
In [ ]:
ce
In [ ]:
ce p. 0 1 2 3 4
In [ ]:
^ 0 1 2 3 4
In [ ]:
DER ce
In [ ]:
DER DER ce
In [ ]:
1 INT ce
In [ ]:
x=:5
In [ ]:
e=:4
In [ ]:
x^e
In [ ]:
x*x*x*x
In [ ]:
ff=: ^!._1 NB. Falling factorial function
In [ ]:
x ff e
In [ ]:
(x-0)*(x-1)*(x-2)*(x-3)
In [ ]:
*/(x-i.e)
In [ ]:
^ t. 0 NB. Leading item of Taylor series for the exponential
In [ ]:
^ t.i.7 NB. First seven items of TS for ^
In [ ]:
sin=: 1&o.
In [ ]:
cos=: 2&o.
In [ ]:
sin t. i.7
In [ ]:
cos t. i.7
In [ ]:
i:3
In [ ]:
(cos t. i.7) p. i:3
In [ ]:
cos i:3
In [ ]:
! 20 NB. Approximation in "scientific notation"
In [ ]:
! x: 20
In [ ]:
2%3
In [ ]:
2%x: 3
In [ ]:
]a=:% 1 + i. x:6 NB. Reciprocals in rational form
In [ ]:
a +/ a NB. Addition table
In [ ]:
a
In [ ]:
a */ a
In [ ]:
a %/ a
In [ ]:
^ t. i. x: 10 NB. Taylor series in rational form
In [ ]:
>((^t.);(sin t.);(cos t.)) i. x:10
In [ ]:
]bct=: (i.7) !/ (i.7)
In [ ]:
]abct=: %. bct NB. Inverse gives alternating binomial coefficients
In [ ]:
+/bct NB. Column sums
In [ ]:
2^i.7
In [ ]:
+/abct NB. Not all zeros
In [ ]:
mp=: +/ . * NB. Matrix product function
In [ ]:
bct mp abct
In [ ]:
bct mp bct
In [ ]:
bct mp bct mp bct
In [ ]:
bct4=:bct mp bct mp bct mp bct
In [ ]:
bct4 NB. What is the developing pattern?
In [ ]:
bct4 % bct NB. pattern clarified by element-by-element division
In [ ]:
]st=: - (i.7) -/ (x: i.7)
In [ ]:
4 ^ st
In [ ]:
bct * 4 ^ st NB. Enter bct4 to compare with this result
In [ ]:
]tri=:>0 0;7 4;3 0 NB. Base length 3 and altitude 4
In [ ]:
tri,.0.5 NB. Border with column of halves
In [ ]:
det=:-/ . * NB. Determinant function
In [ ]:
det tri,.0.5 NB. Signed area (<0 vertices in clockwise order)
In [ ]:
area=: det@(,.&0.5)NB. Define area function
In [ ]:
area tri
Analysis of this use of the determinant for triangles (and tetrahedrons) may be found in Klein "Elementary Mathematics From an Advanced Viewpoint".
Because areas of triangles are signed, the area of any polygon can be obtained by adding lines to triangulate it, and summing the areas.
In [ ]:
]cctri=:0 2 1{tri NB. Counterclockwise triangle
In [ ]:
area cctri
In [ ]:
cos=:2&o.
In [ ]:
sin=:1&o.
In [ ]:
]pent=:(cos,.sin) 2r5p1 * i. 5
In [ ]:
require 'plot'
In [ ]:
|:pent NB. Transpose table
In [ ]:
;/|: pent NB. Box rows (columns of pent)
In [ ]:
(fix=: ;/@|:) pent
In [ ]:
plot fix pent
In [ ]:
0 1 2 3 4 0{pent
In [ ]:
plot fix 0 1 2 3 4 0{pent
In [ ]:
0 2 1 3 4 0{pent
In [ ]:
plot fix 0 2 1 3 4 0{pent
In [ ]:
plot fix 0 1 2 3 4 0 2 0 3 0 4{pent
In [ ]:
]bt=: (0 1 2{pent);(0 2 3{pent);(0 3 4{pent)
In [ ]:
area&> bt
In [ ]:
+/ area&> bt NB. Total area of pentagon
Although the function area gives signed results according to the clockwise or counter-clockwise order of the vertices, these areas are all of the same sign.
This occurs because pent is a convex polygon, and the significance of signed areas is not apparent. We will make it so by using a concave polygon, obtained by multiplying the second vertex of pent by zero.
In [ ]:
]concave=: 1 0 1 1 1*pent
In [ ]:
plot fix 0 1 2 3 4 0{concave
In [ ]:
]btc=: (0 1 2{concave);(0 2 3{concave);(0 3 4{concave)
In [ ]:
area&> btc
In [ ]:
+/area&> btc
In [ ]:
tri
In [ ]:
1 |. tri
In [ ]:
(1 |. tri)-tri
In [ ]:
disp=: 1&|. - ]
In [ ]:
disp tri
In [ ]:
length=: %:@(+/)@(]*])"1
In [ ]:
]sides=:length disp tri
In [ ]:
sp=: %&2 @ (+/)
In [ ]:
sp sides
In [ ]:
(sp , sp - ]) sides
In [ ]:
*/ (sp , sp - ]) sides
In [ ]:
%: */(sp , sp - ]) sides
In [ ]:
area tri NB. Compare with signed area
In [ ]:
Heron=: %: @ (*/) @ (sp , sp - ])
In [ ]:
Heron sides
The rate of change of a function is an important attribute: for example, the rate of change of the sine is the cosine, the rate of change of the exponential is itself, and the rate of change of the nth power is n times the (n-1)th power.
Although the instantaneous rate of change at a point may be difficult to determine, the average rate of change over a non-zero interval i is straightforward.
In [ ]:
f=: 3:*]*] NB. three times the square
In [ ]:
x=:0 1 2 3 4 5
In [ ]:
i=:0.1
In [ ]:
f x
In [ ]:
f x+i
In [ ]:
(f x+i)-(f x) NB. Rise over interval
In [ ]:
((f x+i)-(f x))%i NB. Average rate of change
In [ ]:
require'plot'
In [ ]:
PLOT=:'stick,line'&plot
In [ ]:
i*i.20
In [ ]:
f i*i.20
In [ ]:
PLOT f i*i.20
In [ ]:
smi=:0.001
In [ ]:
PLOT f smi*i.100
The derivative (the slope AT a point) should be given by a zero interval, but this is the meaningless division of a zero rise by a zero interval.
However, the case of a power function can be analyzed to give a simple meaningful result. For example, the cube function applied to x+i gives
```(x*x*x)+(3*x*x*i)+(3*x*i*i)+(i*i*i)```
```Sutraction of the cube of x gives:```
```(3*x*x*i)+(3*x*i*i)+(i*i*i)```
```which can be divided by i to yield:```
```(3*x*x)+(3*x*i)+(i*i)```
```Since i is zero, the last two terms can be dropped to yield```
```3*x*x (that is, three x squared, or formally, 3:*]*]) for the slope with zero interval.```
Similar analysis for other powers gives n*x^(n-1) as the derivative of x^n Moreover, the derivative of a multiple of a function is the same multiple of the derivative, and the derivative of a sum of functions is the sum of their derivatives.
These observations extend the analysis to polynomials, with the simple consequence that the derivative of the polynomial c&p. is the polynomial d&p., where d=:}. c*i.#c.
In [ ]:
]c=:1&o. t. i. x:8 NB. Taylor series for sin
In [ ]:
c*i.#c
In [ ]:
]d=:}.c*i.#c NB. Coefficients for derivative (cos)
In [ ]:
d&p. y=:0.1*i:30 NB. Range from about -pi to pi
In [ ]:
PLOT y;>(c&p. y);(d&p. y)
The foregoing shows that the essential notion of the calculus (that is, the derivative) can be treated without resort to the general analysis of limits, provided that attention is restricted to polynomials or to rational functions obtained as the ratio of polynomials.
Since polynomials and rationals can be used to approximate all the functions of interest in an introductory course in calculus, this is not a serious restriction. This approach to the calculus will be pursued in the next chapter.
In [ ]:
DER=: }. @ (] * i.@#) NB. Gives coefficients of derivative
In [ ]:
INT=: [ , (] % >:@i.@#@]) NB. Gives coefficients of integral
In [ ]:
c=: 1 3 3 1
In [ ]:
DER c
In [ ]:
4 INT c NB. Constant of integration 4
In [ ]:
DER 4 INT c
In [ ]:
(DER c) p. x=:i.7 NB. Evaluation of derivative polynomial
In [ ]:
c&p. d.1 x NB. First derivative of c&p.
In [ ]:
c&p. x
In [ ]:
c&p. d.1 x
In [ ]:
c&p. d.2 x
In [ ]:
c&p. d._1 x
In [ ]:
c&p. d._2 x
In [ ]:
c&p. d._1 d._1 x
In [ ]:
c&p. d._1 d.1 x
In [ ]:
c&p. d.1 t. i.7
In [ ]:
c&p. d.2 t. i.7
In [ ]:
c&p. d._1 t. i.7
In [ ]:
(4: + c&p. d._1) t. i.7
In [ ]:
L=: +/@i. NB. Sum of integers
In [ ]:
R=: (] * ] - 1:) % 2:
In [ ]:
T=: L -: R
In [ ]:
(T ; L ; R ; i.) 6
In [ ]:
n=: ]
In [ ]:
R=: (n*n-1:)%2:
In [ ]:
R 6
A proof is a sequence of equivalent expressions that lead in justifiable steps from a left limb to a right. We will write one expression below another to assert that it is equivalent to the one above it, possibly annotating it with the justification to provide a formal proof:
```L NB. Theorem 1```
```+/@i. NB. Definition of L```
The foregoing proof can be illuminated by entering each expression followed by an argument, and observing that each gives the same result. Any mis-step in a proof will likely show an anomolous result.
An expression can be conveniently entered by moving the cursor up to it, pressing enter to bring it to the input area, modifying it by inserting the argument, and then pressing enter. Moreover, partial expressions may be entered to observe their results. Thus:
In [ ]:
L 6
In [ ]:
+/@i. 6
In [ ]:
+/@|.@i.6 NB. Sum is associative and commutative (|. reverses)
In [ ]:
(((+/@i.)+(+/@|.@i.)) % 2:)6 NB. Half sum of equal quantities
In [ ]:
R 6
In [ ]:
(i. + |.@i.) 6 NB. To show that this is indeed a list of n-1
The following is a similar proof that the sum of the first n odd integers equals the square of n:
```(odds=: 1: + 2: \* i.) NB. First odd integers```
```+/@(1: + 2: \* i.) NB. Sum of odds```
```(n + +/@(2: \* i.)) NB. Sum of n ones is n```
```(n + 2: \* +/@i.) NB. Sum of twice is twice sum```
```(n + 2: \* (n \* n - 1:) % 2:) NB. Result of preceding theorem```
```(n + n \* n - 1:) NB. Simple algebra```
```(n \* n) NB. Simple algebra```
```*: NB. Definition of square```
If a function recurs in the expression that defines it, the function is said to be recursively defined. Such a definition must be supplemented by a definition for some specific argument, using an expression that does not make use of the function being defined.
For example, the factorial of the argument j may be defined by j * f j-1 (or more formally by ] * f@<:), supplemented by the definition 1: (the constant function 1) for the case j=: 0. Thus:
In [ ]:
f=: 1:`(]*f@<:) @. *
In [ ]:
f 5
In [ ]:
f"0 i. 6 NB. The function f is applied to each rank-0 cell
In the foregoing definition, the signum function * yields 0 if the argument is zero, and 1 if it is greater than zero.
Consequently, the agenda @. chooses the last element of the gerund 1:`(]*f@<:) each time until the argument (repeatedly decremented by <:) becomes zero, in which case it chooses the constant function 1:, thus terminating the process.
Alternatively, the imposition of zero rank could be incorporated in the recursive definition:
In [ ]:
f=: 1:`(]*f@<:) @. * " 0
In [ ]:
f i. 6
In [ ]:
1:`(]*$:@<:) @. * " 0 i. 6
In [ ]:
factorial=: 1:`(]*$:@<:) @. * " 0
In [ ]:
factorial i. 6
Quicksort is an interesting example of the use of recursion: the basic procedure is the segregation of the items of an argument list according to whether they are less than the leading item, equal to it, or greater than it.
This procedure is applied recursively to the segregated lists, repeating as long as the argument has more than one item.
In [ ]:
y=: 52 9 65 41 70 91 52 76 26 4 73 32 63
In [ ]:
less=: ([ < {.) # ]
In [ ]:
equal=: ([ = {.) # ]
In [ ]:
greater=: ([ > {.) # ]
In [ ]:
less y
In [ ]:
(less;equal;greater) y
In [ ]:
qs=: ]`($:@less, equal, $:@greater) @. (# > 1:)
In [ ]:
qs y
In [ ]:
seg=: less;equal;greater
In [ ]:
y
In [ ]:
seg y
In [ ]:
> {. seg y NB. Select and open first (less-than)box
In [ ]:
seg > {. seg y NB. Segregate "less-than" list
In [ ]:
; seg > {. seg y NB. Raze the boxed list
In [ ]:
> {: seg y NB. Select and open last (greater-than) list
In [ ]:
seg > {: seg y
In [ ]:
seg y
In [ ]:
> {. seg y
In [ ]:
seg > {. seg y
In [ ]:
> {. seg > {. seg y
In [ ]:
seg > {. seg > {. seg y
In [ ]:
; seg > {. seg > {. seg y
In [ ]:
w=:'ART'
In [ ]:
2 A. w
In [ ]:
4 A. w
In [ ]:
!# w NB. The number of anagrams (permutations) of w
In [ ]:
i.!#w NB. The indices of all !3 anagrams of w
In [ ]:
(i.!#w) A. w
In [ ]:
]top=: (i.6)A.0 1 2 NB. Table of permutations
In [ ]:
top { 'ART'
In [ ]:
]boxedlist=:'zero';'one';'two'
In [ ]:
top{boxedlist
In [ ]:
(top { boxedlist) ,. a: ,. ((i.6) A. boxedlist)
(i.!4) A. 0 1 2 3 (or (i.!4) A. 'ABCD') produces a complete permutation table of order 4. Coloring this table gives a pattern that "shows why" there are 432*1 (that is, !4) permutations of order 4.
The color window is better if expanded (by clicking in the middle tiny square at upper right of the window).
In [ ]:
require 'viewmat'
In [ ]:
color=:viewmat
In [ ]:
color (i.!4) A. 'ABCD'
A list (such as p=: 3 0 4 1 2 5) that is a permutation of its indices is called a permutation vector, and p&{ is the corresponding permutation function.
This permutation can also be prescribed by stating that position 3 receives the item from position 1, that that position receives from position 0, and that (completing the cycle) receives from position 3; further that position 4 receives from 2, which receives from 4; and position 5 (a cycle of one) remains in place.
A boxed list of this cycle representation is given by the function C. .
In [ ]:
p=: 3 0 4 1 2 5
In [ ]:
p&{ 'ABCDEF'
In [ ]:
C. p
In [ ]:
(C. p)&C. 'ABCDEF'
In [ ]:
10 {. p4=: (i.!4) A. i.4
In [ ]:
|: 10 {. (<"1 p4=: (i.!4) A. i.4) ,. (C. p4)
In [ ]: