LABTITLE=: 'An Idiosyncratic Introduction to J'
LABAUTHOR=: 'Roger Hui'
LABERRORS=: 1
LABFOCUS=: 0

NB. =========================================================
Lab Section Introduction
J is executable mathematical notation.
)
2 + 3
x=: 2
y=: 3
x + y

NB. =========================================================
Lab Section Functions
There is a rich set of primitives.
)
2 + 3
2 - 3
2 * 3
2 % 3
2 ^ 3
2 ^ 0.5
_2 ^ 0.5
2 ^. 3

NB. =========================================================
Lab Section Arrays
Functions apply to arrays.
)

2 + 5 6 7
2 3 4 * 5 6 7
2 3 4 - 5 6 7
2 3 4 % 5 6 7
2 3 % 5 6 7       NB. error because arguments do not match

NB. =========================================================
Lab Section
Some function make arrays.

i.n is a list of the integers from 0 to n-1.

s $ v makes an array of shape s using the elements v.
)

i. 7
1 + i.7
2 ^ i.7
(i.7) ^ 0.5
3 5 $ 3 1 4 2
x=: 3 5 $ 3 1 4 2
x * x

NB. =========================================================
Lab Section Assignment
An array or function can be assigned a name using =:, i.e. copula.
)
i. 7
x=: i. 7
x
x ^ 2
pow=: ^
x pow 2
x pow 0.5

NB. =========================================================
Lab Section Monadic or Dyadic
A function can be monadic or dyadic, depending on whether there is an argument on the right or on the left and right.
)

2 % 3
% 3
2 ^ 3
^ 3

NB. =========================================================
Lab Section More Primitives
The primitives functions include + - * % as well as ^ (power), ^. (log), > (greater than), >. (maximum), +. (or/GCD), *. (and/LCM), | (residue or modulo), etc.

And all of these can be monadic or dyadic, and apply to arrays.
)

2 ^ 3 4 5 6
2 ^ 0.5 3 _5
_2 ^ 0.5 6 7

0 0 1 1 +. 0 1 0 1
2 3 4 5 +. 10 20 30 40
2 3 4 5 *. 10 20 30 40

2 3 4 5 | 10 20 30 40
2 3 4 5 <. 10 20 30 40
2 3 4 5 >. 10 20 30 40

NB. =========================================================
Lab Section Insert
Adverbs modify verbs 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.
)

x=: 1 + i. 7
x
+/ x
*/ x
>./ x
<./ x
+./ x
*./ x

NB. =========================================================
Lab Section Table
f/ is a verb, and like other verbs can be monadic or dyadic.  The monadic meaning is "insert".  The dyadic meaning is "table", i.e. function table.

Function tables are a good way to organize systematic experimentation on unfamiliar functions.
)

x=:i.9
x
x +/ x
x */ x
x </ x
x >/ x
x >./ x
x <./ x
x +./ x
x *./ x
x | / x

NB. =========================================================
Lab Section
The Hilbert matrix is a simple function on the addition table.

The patterns are more apparent in the extended (rational) domain.  The reciprocal determinant of the Hilbert matrix of order n, has prime factors that are the primes less than 2*n.
)

x=: i.7
x +/ x
% 1 + x +/x

y=: i.7x
H=: % 1 + y +/ y
H

~. q: % -/ .* H

NB. =========================================================
Lab Section
The "triangle" of Pascal is an example of a function table, using the binomial coefficient function ! .

An advantage of looking at it as a table rather than as a triangle, is that one can then apply matrix operations to it, such as matrix inverse.
)
x=: i.7
x !/x

m=: x !/x
%. m

NB. =========================================================
Lab Section Prefix
Prefix is another adverb.  f\ applies f to the prefixes of the argument.

The monad < (box) is helpful in elucidating prefix.
)

x=: 1+i.7
x
+/\ x
<\ x

*/\ x
<./\x
>./\x
+./\x
*./\x

NB. =========================================================
Lab Section Permutations
The dyadic function x{y indexes y by x.  If p is a permutation, p{y permutes y by p.
)

p=: 22 ?. 22    NB. a random permutation
p
p { p
p { p { p

3 22$p
{/3 22$p
{/\3 22$p

NB. =========================================================
Lab Section
{/ (m,#p) $ p inserts { between m copies of the permutation, and computes the m-th power of p.

The corresponding prefixes, {/\ (m,#p) $ p, are the successive powers of p.
)


p=: 22 ?. 22    NB. a random permutation
p
p { p { p

#p

(3,#p)$p
{/(3,#p)$p
{/\(3,#p)$p

NB. =========================================================
Lab Section
C. p computes the cycles of permutation p.  The LCM of the cycle lengths, is the order of the subgroup generated by p.
)
p=: 22 ?. 22
C. p
#&> C. p
*./ #&> C. p

# ~. {/\ (200,#p) $ p