# Studio/TasteofJPart1

## A Taste of J - Part 1

### First Steps

You can use J as a calculator - just type sentences into the window and press Enter.

J is ready for input when the message in the status bar at the foot of the screen displays Ready.

Typically, the cursor is indented 3 spaces to distinguish user entries from the response of the computer, which are shown aligned to the left.

For example, add 5 to the three numbers 10 20 30 :

```   5 + 10 20 30
15 25 35
```

You are encouraged to experiment when going through labs. You can type in your own sentences, or re-enter sentences by moving the cursor up to the line, pressing Enter, then making any modifications.

Try this now with the sentence 5 + 10 20 30 shown above.

The standard mathematical functions are "+ - * % ^ ^." , i.e. plus, minus, times, divide, power and log:

```   5 + 10 20 30
15 25 35

5 - 10 20 30
_5 _15 _25

5 * 10 20 30
50 100 150

5 % 10 20 30
0.5 0.25 0.166667

5 ^ 10 20 30
9.76563e6 9.53674e13 9.31323e20

5 ^. 10 20 30
1.43068 1.86135 2.11328
```

Note that the symbol for log "^." has two characters. In general, J functions are written with either a single character as in power "^", or with two or three characters, where the additional characters may be a period "." or colon ":".

For example, "*:" squares its argument:

```   *: 10 20 30
100 400 900
```

Use "=:" to assign names.

No result is shown if a sentence concludes with an assignment; otherwise the result is displayed.

Note that character strings are entered between single quotes (apostrophes):

```   a=: 5

b=: 10 20 30

log=: ^.

plus=: +

square=: *:

sum=: +/

text=: 'hello world'
```

We can now use the assigned names:

```   a plus b
15 25 35

square b
100 400 900

sum b
60

c=: a plus b

sum c
75

text
hello world
```

### Data Manipulation

J excels at manipulating data. Let us try some examples.

Define a character string t as follows:

```   t=: 'earl of chatham'

t
earl of chatham
```

Count of t, i.e. number of characters in the string:

```   # t
15
```

Nub of t, i.e. unique characters:

```   ~. t
earl ofchtm
```

Count of nub of t :

```   # ~. t
11
```

Sort t in ascending order. "sort" is a predefined utility. The sort order is the standard ASCII alphabet, where blanks are sorted before other characters:

```   sort t
aaacefhhlmort
```

Reverse t :

```   |. t
mahtahc fo lrae
```

Duplicate each letter:

```   2 # t
eeaarrll  ooff  cchhaatthhaamm
```

Make 3 copies of t :

```   3 # ,: t
earl of chatham
earl of chatham
earl of chatham
```

Chop t into words.

Note J puts each word in a box - when something is in a box it is treated as a single item, even though it may contain several numbers or characters:

```   ;: t
+----+--+-------+
|earl|of|chatham|
+----+--+-------+
```

Sort words in t :

```   sort ;: t
+-------+----+--+
|chatham|earl|of|
+-------+----+--+
```

The utility adverb "each" applies a function to each boxed item. For example, count the length of each word:

```   # each ;: t
+-+-+-+
|4|2|7|
+-+-+-+
```

### Numbers

The above examples apply equally well to numbers.

Other facilities are defined just for numbers. For example, the integer function (i.) generates numbers:

```   i.10           NB. first 10 counting numbers
0 1 2 3 4 5 6 7 8 9

i.4 3          NB. first 12 numbers in a 4 by 3 table
0  1  2
3  4  5
6  7  8
9 10 11

i.3 4 5        NB. first 60 numbers in a 3 by 4 by 5 table
0  1  2  3  4
5  6  7  8  9
10 11 12 13 14
15 16 17 18 19

20 21 22 23 24
25 26 27 28 29
30 31 32 33 34
35 36 37 38 39

40 41 42 43 44
45 46 47 48 49
50 51 52 53 54
55 56 57 58 59
```

The function "+/" sums its argument. It is made up of "+" (add) with "/" (insert), meaning insert "+" between each item of the argument.

So +/ 10 20 30 means: 10 + 20 + 30 .

Similarly */ is "multiply insert", i.e. multiply all elements together.

```   +/ 10 20 30
60

*/ 10 20 30
6000
```

Add up the first 10,000 positive integers:

```   +/i.10001  NB. 10001 - since J starts counting at 0
50005000
```

```   +/i.3 4 5
60  63  66  69  72
75  78  81  84  87
90  93  96  99 102
105 108 111 114 117
```

"power insert" of 10 20 30 is a big number! It exceeds the representation used by the machine, so J returns infinity.

```   ^/ 10 20 30
_
```

Can you figure out the other examples below?

(Try 3 ^ 4 , then take 2 to the power of this number.)

```   ^/ 2 3 4
2.41785e24
```

The 'x' used below means "extended" and instructs J to use extended precision in performing the calculations, instead of converting to floating point.

```   ^/ 2 3 4x
2417851639229258349412352
```

Note the "e" notation used in 2.41785e24, meaning:

2.41785 * 10^24

J has other number notations that use letters:

```   3b102     NB. base (base-3 notation for 11, or 10b11)
11

3r5       NB. ratio (3 % 5)
3r5

3j5       NB. complex number (3 + 0j5)
3j5

3p5       NB. Pi (3 * Pi ^ 5)
918.059

3x5       NB. Exponentional (3 * 2.71828... ^ 5)
445.239
```

Examples:

```   1p1 2p1     NB. Pi, 2 * Pi
3.14159 6.28319

%: -i.6     NB. square roots of minus 0 to minus 5
0 0j1 0j1.41421 0j1.73205 0j2 0j2.23607
```

The function "?" generates random numbers.

For example, to generate 20 random numbers in the range 0-99:

```   ? 20#100
13 75 45 53 21 4 67 67 93 38 51 83 3 5 52 67 0 38 6 41

? 20#100
68 58 93 84 52 9 65 41 70 91 76 26 4 73 32 63 75 99 36 24
```

For example, read in the stats functions:

```   load 'stats'
```

Example:

```   dstat ? 20#100
sample size:         20
minimum:              6
maximum:             98
median:            55.5
mean:             54.25
std devn:       30.1765
skewness:     _0.140884
kurtosis:       1.66846
```

normalrand generates random numbers in a normal distribution with mean 0 and standard deviation 1:

```   normalrand 6
2.4598 0.120974 _0.269407 _3.27581 _1.73646 _0.60928

dstat normalrand 10000
sample size:       10000
minimum:        _4.79869
maximum:         4.05703
median:        0.0095872
mean:         0.00343015
std devn:       0.997596
skewness:     _0.0475715
kurtosis:        3.03493
```

Read in the trig and plot functions:

```   load 'trig plot'
```

Try generating sines. Here the arguments are in radians:

```   sin i.6
0 0.841471 0.909297 0.14112 _0.756802 _0.958924

sin i.3 4
0  0.841471  0.909297  0.14112
_0.756802 _0.958924 _0.279415 0.656987
0.989358  0.412118 _0.544021 _0.99999
```

You can plot numbers directly. The next section plots the sin of i.3 4. The lines are jagged because each point is joined by a straight line and there are very few points.

```   plot sin i.3 4
```

The next two sections plot sin 0.2 * i.30 30 , first as a series of lines, then as a surface. This time there are enough data points to show smoother lines.

```   plot sin 0.2 * i.30 30
```
```   'surface' plot sin 0.2 * i.30 30
```

End of lab