Differences between revisions 5 and 6
 ⇤ ← Revision 5 as of 2006-01-06 20:57:27 → Size: 9639 Editor: RogerHui Comment: format fiddling ← Revision 6 as of 2008-12-08 10:45:37 → ⇥ Size: 9639 Editor: anonymous Comment: converted to 1.6 markup Deletions are marked like this. Additions are marked like this. Line 5: Line 5: [[TableOfContents]] <> Line 454: Line 454: [[BR]] <
>

An Idiosyncratic Introduction to J

## Introduction

J is executable mathematical notation.

```   2 + 3
5
x=: 2
y=: 3
x + y
5```

## Functions

There is a rich set of primitives.

```   2 + 3
5
2 - 3
_1
2 * 3
6
2 % 3
0.666667
2 ^ 3
8
2 ^ 0.5
1.41421
_2 ^ 0.5
0j1.41421
2 ^. 3
1.58496```

## Arrays

Functions apply to arrays.

```   2 + 5 6 7
7 8 9
2 3 4 * 5 6 7
10 18 28
2 3 4 - 5 6 7
_3 _3 _3
2 3 4 % 5 6 7
0.4 0.5 0.571429
2 3 % 5 6 7       NB. error because arguments do not match
|length error
|   2 3    %5 6 7```

## Arrays (ctd)

Some functions 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
0 1 2 3 4 5 6
1 + i.7
1 2 3 4 5 6 7
2 ^ i.7
1 2 4 8 16 32 64
(i.7) ^ 0.5
0 1 1.41421 1.73205 2 2.23607 2.44949
3 5 \$ 3 1 4 2
3 1 4 2 3
1 4 2 3 1
4 2 3 1 4
x=: 3 5 \$ 3 1 4 2
x * x
9  1 16 4  9
1 16  4 9  1
16  4  9 1 16```

## Assignment

An array or function can be assigned a name using =: , i.e. copula.

```   i. 7
0 1 2 3 4 5 6
x=: i. 7
x
0 1 2 3 4 5 6
x ^ 2
0 1 4 9 16 25 36
pow=: ^
x pow 2
0 1 4 9 16 25 36
x pow 0.5
0 1 1.41421 1.73205 2 2.23607 2.44949```

Functions can be monadic or dyadic, depending on whether there is an argument on the right or on the left and right.

```   2 % 3
0.666667
% 3
0.333333
2 ^ 3
8
^ 3
20.0855```

## 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
8 16 32 64
2 ^ 0.5 3 _5
1.41421 8 0.03125
_2 ^ 0.5 6 7
0j1.41421 64 _128

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

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

## 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
1 2 3 4 5 6 7
+/ x
28
*/ x
5040
>./ x
7
<./ x
1
+./ x
1
*./ x
420```

## 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
0 1 2 3 4 5 6 7 8
x +/ x
0 1  2  3  4  5  6  7  8
1 2  3  4  5  6  7  8  9
2 3  4  5  6  7  8  9 10
3 4  5  6  7  8  9 10 11
4 5  6  7  8  9 10 11 12
5 6  7  8  9 10 11 12 13
6 7  8  9 10 11 12 13 14
7 8  9 10 11 12 13 14 15
8 9 10 11 12 13 14 15 16
x */ x
0 0  0  0  0  0  0  0  0
0 1  2  3  4  5  6  7  8
0 2  4  6  8 10 12 14 16
0 3  6  9 12 15 18 21 24
0 4  8 12 16 20 24 28 32
0 5 10 15 20 25 30 35 40
0 6 12 18 24 30 36 42 48
0 7 14 21 28 35 42 49 56
0 8 16 24 32 40 48 56 64
x </ x
0 1 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 0 0 0 0 0 0 0
0 1 0 1 0 1 0 1 0
0 1 2 0 1 2 0 1 2
0 1 2 3 0 1 2 3 0
0 1 2 3 4 0 1 2 3
0 1 2 3 4 5 0 1 2
0 1 2 3 4 5 6 0 1
0 1 2 3 4 5 6 7 0```

## Table (ctd)

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
0 1 2 3  4  5  6
1 2 3 4  5  6  7
2 3 4 5  6  7  8
3 4 5 6  7  8  9
4 5 6 7  8  9 10
5 6 7 8  9 10 11
6 7 8 9 10 11 12
% 1 + x +/x
1      0.5 0.333333     0.25       0.2  0.166667  0.142857
0.5 0.333333     0.25      0.2  0.166667  0.142857     0.125
0.333333     0.25      0.2 0.166667  0.142857     0.125  0.111111
0.25      0.2 0.166667 0.142857     0.125  0.111111       0.1
0.2 0.166667 0.142857    0.125  0.111111       0.1 0.0909091
0.166667 0.142857    0.125 0.111111       0.1 0.0909091 0.0833333
0.142857    0.125 0.111111      0.1 0.0909091 0.0833333 0.0769231

y=: i.7x
H=: % 1 + y +/ y
H
1 1r2 1r3  1r4  1r5  1r6  1r7
1r2 1r3 1r4  1r5  1r6  1r7  1r8
1r3 1r4 1r5  1r6  1r7  1r8  1r9
1r4 1r5 1r6  1r7  1r8  1r9 1r10
1r5 1r6 1r7  1r8  1r9 1r10 1r11
1r6 1r7 1r8  1r9 1r10 1r11 1r12
1r7 1r8 1r9 1r10 1r11 1r12 1r13

~. q: % -/ .* H
2 3 5 7 11 13```

## Table (ctd)

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
1 1 1 1 1  1  1
0 1 2 3 4  5  6
0 0 1 3 6 10 15
0 0 0 1 4 10 20
0 0 0 0 1  5 15
0 0 0 0 0  1  6
0 0 0 0 0  0  1

m=: x !/x
%. m
1 _1  1 _1  1  _1   1
0  1 _2  3 _4   5  _6
0  0  1 _3  6 _10  15
0  0  0  1 _4  10 _20
0  0  0  0  1  _5  15
0  0  0  0  0   1  _6
0  0  0  0  0   0   1```

## Prefix

Prefix is another adverb. f\ applies f to the prefixes of the argument.

```   x=: 1+i.7
x
1 2 3 4 5 6 7
+/\ x
1 3 6 10 15 21 28
<\ x
┌─┬───┬─────┬───────┬─────────┬───────────┬─────────────┐
│1│1 2│1 2 3│1 2 3 4│1 2 3 4 5│1 2 3 4 5 6│1 2 3 4 5 6 7│
└─┴───┴─────┴───────┴─────────┴───────────┴─────────────┘

*/\ x
1 2 6 24 120 720 5040
<./\x
1 1 1 1 1 1 1
>./\x
1 2 3 4 5 6 7
+./\x
1 1 1 1 1 1 1
*./\x
1 2 6 12 60 60 420```

## Permutations

The dyadic function x{y indexes y by x . If x is a permutation, p{y permutes y by p .

```   p=: 23 ?. 23    NB. a random permutation
p
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12
p{p
18 12 6 19 2 11 8 0 4 10 15 9 1 21 13 5 7 17 16 14 22 3 20
p{p{p
2 20 7 5 16 13 0 4 18 3 19 21 22 10 9 14 8 17 6 11 12 15 1```

## Permutations (ctd)

{/ (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=: 23 ?. 23
p
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12
# p
23

(3,#p) \$ p
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12
{/ (3,#p) \$ p
2 20 7 5 16 13 0 4 18 3 19 21 22 10 9 14 8 17 6 11 12 15 1
{/\ (3,#p) \$ p
4 22 16 15 18 14 7 8  0 21  3 13 20  9 11 19 6 17  2  5  1 10 12
18 12  6 19  2 11 8 0  4 10 15  9  1 21 13  5 7 17 16 14 22  3 20
2 20  7  5 16 13 0 4 18  3 19 21 22 10  9 14 8 17  6 11 12 15  1```

## Permutations (ctd)

C. p computes the cycles of permutation p . The LCM of the cycle lengths, is the order of the subgroup generated by p .

```   p=: 23 ?. 23
p
4 22 16 15 18 14 7 8 0 21 3 13 20 9 11 19 6 17 2 5 1 10 12

C. p
┌──┬─────────────────┬──────────────────────────┬──────────┐
│17│18 2 16 6 7 8 0 4│21 10 3 15 19 5 14 11 13 9│22 12 20 1│
└──┴─────────────────┴──────────────────────────┴──────────┘
#&> C. p
1 8 10 4
*./ #&> C. p
40
# ~. {/\ (200,#p) \$ p
40```

Contributed by RogerHui.

Studio/IdiosyncraticIntroduction (last edited 2008-12-08 10:45:37 by anonymous)