J Labs

A Taste of J (1)

(1 of 35) 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:


In [ ]:
5 + 10 20 30

(2 of 35) First Steps (ctd)

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.

(3 of 35) First Steps (ctd)

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


In [ ]:
5 + 10 20 30

In [ ]:
5 - 10 20 30

In [ ]:
5 * 10 20 30

In [ ]:
5 % 10 20 30

In [ ]:
5 ^ 10 20 30

In [ ]:
5 ^. 10 20 30

(4 of 35) First Steps (ctd)

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 characters, where the second character is either a period "." or colon ":".

For example, *: squares its argument:


In [ ]:
*: 10 20 30

(5 of 35) First Steps (ctd)

Use =: to assign names.

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

Note that character strings are entered using quotes:


In [ ]:
a=: 5

In [ ]:
b=: 10 20 30

In [ ]:
log=: ^.

In [ ]:
plus=: +

In [ ]:
square=: *:

In [ ]:
sum=: +/

In [ ]:
text=: 'hello world'

(6 of 35) First Steps (ctd)

We can now use the assigned names:


In [ ]:
a plus b

In [ ]:
square b

In [ ]:
sum b

In [ ]:
c=: a plus b

In [ ]:
sum c

In [ ]:
text

(7 of 35) Data Manipulation

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

Define a character string t as follows:


In [ ]:
t=: 'earl of chatham'

In [ ]:
t

(8 of 35) Data Manipulation (ctd)

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


In [ ]:
#t

(9 of 35) Data Manipulation (ctd)

Nub of t, i.e. unique characters:


In [ ]:
~. t

(10 of 35) Data Manipulation (ctd)

Count of nub of t:


In [ ]:
# ~. t

(11 of 35) Data Manipulation (ctd)

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:


In [ ]:
sort t

(12 of 35) Data Manipulation (ctd)

Reverse t:


In [ ]:
|. t

(13 of 35) Data Manipulation (ctd)

Duplicate each letter:


In [ ]:
2 # t

(14 of 35) Data Manipulation (ctd)

Make 3 copies of t:


In [ ]:
3 # ,: t

(15 of 35) Data Manipulation (ctd)

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:


In [ ]:
;: t

(16 of 35) Data Manipulation (ctd)

Sort words in t:


In [ ]:
sort ;: t

(17 of 35) Data Manipulation (ctd)

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


In [ ]:
# each ;: t

(18 of 35) Numbers

The above examples apply equally well to numbers.

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


In [ ]:
i.10           NB. first 10 numbers

In [ ]:
i.4 3          NB. first 12 numbers in a 4 by 3 table

In [ ]:
i.3 4 5        NB. first 60 numbers in a 3 by 4 by 5 table

(19 of 35) Numbers (ctd)

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.


In [ ]:
+/ 10 20 30

In [ ]:
*/ 10 20 30

(20 of 35) Numbers (ctd)

Add up the first 10,000 positive integers:


In [ ]:
+/i.10001  NB. 10001 - since J starts counting at 0

(21 of 35) Numbers (ctd)

Add up i.3 4 5


In [ ]:
+/i.3 4 5

(22 of 35) Numbers (ctd)

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

Can you figure out the other examples below?

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

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


In [ ]:
^/ 10 20 30

In [ ]:
^/ 2 3 4

In [ ]:
^/ 2 3 4x

(23 of 35) Numbers (ctd)

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

```2.41785 * 10^24```

J has other number notations that use letters:


In [ ]:
3b102     NB. base (102 in base 3)

In [ ]:
3r5       NB. ratio (3 % 5)

In [ ]:
3j5       NB. complex number

In [ ]:
3p5       NB. Pi (3 * Pi ^ 5)

In [ ]:
3x5       NB. Exponentional (3 * 2.71828... ^ 5)

(24 of 35) Numbers (ctd)

Examples:


In [ ]:
1p1 2p1     NB. Pi, 2 * Pi

In [ ]:
%: -i.6     NB. square roots of minus 0 to minus 5

(25 of 35) Numbers (ctd)

The function ? generates random numbers.

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


In [ ]:
? 20#100

In [ ]:
? 20#100

(26 of 35) Numbers (ctd)

The function "load" reads in definitions from "script" files.

For example, read in the stats functions:


In [ ]:
load 'stats'

(27 of 35) Numbers (ctd)

Example:


In [ ]:
dstat ? 20#100

(28 of 35) Numbers (ctd)

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


In [ ]:
normalrand 6

In [ ]:
dstat normalrand 10000

(29 of 35) Numbers (ctd)

Read in the trig and plot functions:


In [ ]:
load 'trig plot'

(30 of 35) Numbers (ctd)

Try generating sines, here the arguments are in radians:


In [ ]:
sin i.6

In [ ]:
sin i.3 4

(31 of 35) Numbers (ctd)

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.

(32 of 35) Numbers (ctd)


In [ ]:
plot sin i.3 4

(33 of 35) Numbers (ctd)

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 smooth lines.

(34 of 35) Numbers (ctd)


In [ ]:
plot sin 0.2 * i.30 30

(35 of 35) Numbers (ctd)


In [ ]:
'surface' plot sin 0.2 * i.30 30

End of Lab


In [ ]: