LABTITLE=: 'Mathematical Roots of J'
LABAUTHOR=: 'Roger K. W. Hui Kenneth E. Iverson'
LABCOMMENTS=: 'Lab based on a paper presented at APL97.'
LABWRAP=: 0

NB. =========================================================
Lab Section INTRODUCTION
This lab shows how mathematical ideas and notation were
adopted (or adapted) in J, and why such adaptations were
made.
)

NB. =========================================================
Lab Section ASSIGNMENT AND COMPARISON
           ASSIGNMENT                 COMPARISON

ENGLISH	     x is 3          x less than 3     x equals 3

MATH         Let x=3         x<3               x=3

C            x=3             x<3               x==3

APL          x{<-}3          x<3               x=3

J            x=:3            x<3               x=3

_______________________


ENGLISH      cube function

MATH         cube(x) = x3

APL          {del} z{<-}cube X
             [1]Z{<-}X*3 {del}

J            cube =: ^&3
)

NB. =========================================================
Lab Section CONSTANTS
          FUNCTION EXPRESSION           CONSTANT

ENGLISH   3 plus 4 tenths
MATH      3 + 4/10                      3.4
APL       3 + 4{divide}10               3.4
J         3 + 4%10                      3.4
-------------------------------------------------------------
ENGLISH   3 plus 4 imaginary
MATH      3 + 4i
APL       3 + 4{times}{negative}1*0.5   3j4
J         3 + 4*_1^0.5                  3j4
-------------------------------------------------------------
ENGLISH   Eulers Number
          1
MATH      e                             e
APL       *1
J         ^1                            1x1
-------------------------------------------------------------
ENGLISH   Pi
MATH      Pi{times}1                    Pi
APL       {circle}1
J         o.1                           1p1
)

NB. =========================================================
Lab Section CONSTANT FUNCTIONS
       Function  1st derivative   2nd der     3rd der

         3          2
MATH	x         3x               6x          6

J       ^&3       3:*^&2           6:*^&1      6:
)
y=: 4
! y
6:y

6"_ y
4 5 6"_ y
'abcd'"_ y

NB. =========================================================
Lab Section OPERATORS
ENGLISH An operator applies to a function, or
        functions, to produce a new function,
        much as an adverb applies to a verb to
        produce a new verb. We will illustrate
        by compositions.

MATH	f{jot}g

   J    f&g   f&.g   f@g
        And   Dual   Atop
)
^. 10 2      NB. Natural logs
10 -&^. 2    NB. Diff of logs
10 -&.^. 2   NB. Dual of - wrt log
^ 10 -&^. 2  NB. Inv log of diff
10 ^. 2      NB. Base 10 log of 2
10 -@^. 2    NB. - of base10 log

x=. 3 1 4 1 5 9
|. x         NB. Reverse
+/\ & |. x   NB. Partial sums of reverse
+/\ &. |. x  NB. Dual of partial sums wrt reverse
|. +/\ |. x

NB. =========================================================
Lab Section AMBIVALENCE
In math, the expression a-b denotes subtraction, and the
expression -b denotes negation.  We therefore say that the
function denoted by - is ambivalent, its use as subtraction
or negation being determined by context. A few of the
ambivalent primitives in J are:

          MONADIC           DYADIC

 +        Conjugate         Plus
 -        Negation          Subtraction
 *        Signum            Times
 %        Reciprocal        Divide
 ^        Exponential       Power

In J, all functions are ambivalent, including those derived
from other functions by the application of operators.  For
example:
)
a=: 0 1 2 3 4
+/ a       NB. Plus over a (sum)
a +/ a     NB. Plus table

NB. =========================================================
Lab Section FORK

ENGLISH	Sum of functions f and g           +
                                         /   \
                                        f     g
                                        |     |
                                        x     x

  MATH  f + g

   J	f + g
)
h=: ! + *:
h 4
d=: 2 3 4
h d
q=: ! , *:
q 4
q d
mean=: +/ % #
center=: ] - mean
(] ; mean ; center ; ] - +/ % #) d

NB. =========================================================
Lab Section ARRAYS, CELLS, and ITEMS
)
[a=: i.2 3 4
<"2 a           NB. Rank 2 cell, 2-cell, Items of 3-cell
<"1 a           NB. 1-cells, Items of 2-cells
<"0 a           NB. 0-cells, Atoms

NB. =========================================================
Lab Section
)
[a=: i.2 3 4
<"_1 a          NB. _1 cells of a, Items of a
v=: 0 1 2 3 4 5
<"_1 v          NB. _1 cells of v, Items of v

NB. =========================================================
Lab Section FUNCTION RANK
)
d=: 2 3 4
a=: d $ 'ABCDEFGHIJKLMNOPQRSTUVWX'
(] ; |. ; |."3 ; |."2 ; |."1 ; |."0) a
i. d
i."0 d

NB. =========================================================
Lab Section FUNCTION TABLES
ENGLISH  Table of binomial coefficients;
         column c of row r gives the number of ways of
         choosing c things from r.

MATH     Pascals Triangle

        1
        1 1
        1 2 1
        1 3 3 1
        1 4 6 4 1
        1 5 10 10 5 1

J
)
i=: 0 1 2 3 4 5
bc=: i !~/i
bc ; (%. bc); (+/"1 bc)

rou=: ^@(0j2p1"_ * i. % ])    NB. roots of unity
r=: rou 7
r

*/~ r         NB. multiplication table

r i. */~ r

7&|@+/~ i.7   NB. addition modulo 7

NB. =========================================================
Lab Section FUNCTION POWERS
ENGLISH	Applying a function n times is called the
        nth power of the function

MATH     x  = x       x  = f x        x  = f x
         0            1      0        n      n-1

J       f^:n
)
x=: 0
cos=: 2&o.

cos 0
cos cos 0

cos ^: 0 1 2 3 4 x

y=: cos ^: _ x

y
y = cos y

]z=: cos ^: _1 x=: _0.5 0 0.5 0.75 1

cos z

NB. =========================================================
Lab Section
)
f=: -:@(+ 1000&%)    NB. halve of x plus 1000 divided by x

f 1

f f 1

f^:(i.2 5) 1

] y=: f^:(_) 1

y*y

1000-y*y

NB. =========================================================
Lab Section
)
SG=: 1 : '~.@(, ,/@(x./~))^:_'   NB. subgroup

f=: 7&|@*      NB. multiplication modulo 7

f SG 2         NB. subgroup generated by 4

f/~ f SG 2     NB. group table of subgroup

f SG 3         NB. subgroup generated by 3

f SG 2 3       NB. subgroup generated by 2 and 3

p=: (1|.i.5),: (<0 1) C. i.5

p              NB. two permutations

$ {"1 SG p     NB. subgroup generated by the two permutations

NB. =========================================================
Lab Section
f^:g y is equivalent to f^:(g y) y.  In particular, if g is a
proposition, f^:g y applies f to y or not according to
whether g y is true.
)
f=: |.@(|/\)

g=: *@{.

f^:g y=: 32 44

f^:g^:(i.5) y

f^:g^:_ y

+./y

f=: {: ,: {. - {: * <.@%&{./

g=: *@{.@{:

f^:g^:_ y,.=i.2

_4 3 +/ .* y   NB. GCD as a linear combination of original arg

<"2 f^:g^:(i.6) y,.=i.2

NB. =========================================================
Lab Section
ENGLISH   A function that "undoes" the effect of a function

           -1 MATH      f

J         f^:_1
)
I=: ^:_1       NB. Inverse operator

cos I 0

^I 1x1 1x2     NB. Natural log

^ ^I 1x1 1x2   NB. Exponential of log

cube=: ^&3

cube I 27 64 100

NB. =========================================================
Lab Section FUNCTION DUALS
ENGLISH	If a function f is applied to the result of a
        function g, and the inverse of g is then
        applied, the entire process is a function
        called the dual of f with respect to g

         -1 MATH    g   f g

   J    f &. g
)
'a b'=: 0 0 1 1 ; 0 1 0 1

a *. b     NB. a and b

-.a        NB. Not a

a *.&.-. b NB. Dual of and wrt not

a +. b     NB. Is equivalent to or

3 +&.^. 4

3 * 4

log=: 10&^.

log 3 4 10 100

3 +&.log 4

NB. =========================================================
Lab Section FUNCTION FAMILIES
Many benefits resulted from uniting the treatment of the
individual functions square, cube, square root, etc. in a
single family under the notation x superscript n.  Similar
benefits result from using complex numbers to unite the
treatment of the trigonometric and hyperbolic functions under
the exponential.

We will illustrate the matter by using the fit operator (!.)
to unite the treatment of the falling and rising factorial
functions under the power function:
)
'x e s' =: 6 4 1

(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s)   NB. Rising

s=: _1

(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s)   NB. Falling

s=: 0

(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s)   NB. Power

x ^ e

x ^!.1 e     NB. Rising  factorial

x ^!._1 e    NB. Falling factorial

x ^!.0 e     NB. Power

NB. =========================================================
Lab Section
Function tables for ^!.e can be used to make clear the
relationship between Stirling numbers of the first kind and
Stirling numbers of the second kind.
)
FT=: 1 : '^!.x./~@i.'   NB. falling/rising factorial table

_1 FT 6                 NB. falling factorial table

0 FT 6                  NB. power table

]S2=:(0 FT %. _1 FT) 6  NB. Matrix quotient of above tables

]S1=:(_1 FT %. 0 FT) 6  NB. Matrix inverse of S2

NB. |: S2  are the Stirling numbers of the second kind
NB. |: |S1 are the Stirling numbers of the first  kind

NB. =========================================================
Lab Section POLYNOMIALS
A polynomial is so named because it can be expressed as a sum
of monomials of the form:

     k
  c x

It may also be expressed as a weighted sum of factorial
polynomials, or as a (multiple of a) product of factors of
the form x-r. We will illustrate some transformations between
the coefficients that weight the monomials, and the roots
that represent the corresponding product form:
)
'c x y'=: _3.75 11.5 _9 2 ; 5 ; 0 1 2 3 4

c p. x

+/ c * x ^ 0 1 2 3

c p. y

]r=: p. c

r p. x

p. r

NB. =========================================================
Lab Section TAYLOR COEFFICIENTS
The Taylor coefficient operator t. produces the coefficients
of a polynomial function to which it is applied. More
generally, it produces the coefficients of a polynomial
approximation to other functions, such as the exponential,
the trigonometric functions, and rational functions,i.e. ratios
of two polynomials. Thus:
)
f=: 3 1 4 2 & p.

'x i'=: 0 1 2 3 4 ; 0 1 2 3 4 5

f x

f t. i

^ t. i

(^ t. i) p. x

^ x

% ^ t. i

^ t: i      NB. weighted Taylor coefficients for exponential

1&o. t: i   NB. sine
5&o. t: i   NB. hyperbolic sine

NB. =========================================================
Lab Section
)
p=: 4 _3 1 2&p.

q=: _1 5 1&p.

(p+q) t. i.10

(p-q) t. i.10

(q-p) t. i.10

(p*q) t. i.10

(p%q) t. i.10

(q%p) t. i.10

p@q t. i.10

q@p t. i.10

p@>: t. i.10

p@<: t. i.10

(0 1&p. % 1 _1 _1&p.) t. i.15

(% -. - *:) t. i.15

NB. =========================================================
Lab Section PERMUTATIONS
Permutations may be represented in a variety of ways, the
simplest being by a vector of indices. Since the permutation
vectors of any given order can be arranged in a table in
lexical order, a specific permutation can also be referred to
compactly by its index in the table:
)
]rp=: 8 ?. 8      NB. Random permutation

rp { 'ABCDEFGH'

(i. ! 3) A. i. 3  NB. All !3 of order 3

A. 1 0 2          NB. Index of perm 1 0 2

]p=: 20 ?. 20

A. p              NB. Extended precision

]cy=: C. rp       NB. Cycle representation

C. cy             NB. C. is self-inverse

NB. =========================================================
Lab Section
)
SG=: 1 : '~.@(, ,/@(x./~))^:_'

] p=. ?.~ 22        NB. a random permutation of order 22

g=.{"1 SG ,: p      NB. subgroup generated by p

#g

C. p                NB. cycles of p

*./ #&> C. p        NB. LCM of cycle lengths

NB. =========================================================
Lab Section COMPLETION
In mathematics it is common to complete a function by
extending it beyond its original domain in ways that preserve
its main properties, often leading to significant
generalization, as in the extension of the square and cube to
the form:

     n
    x

for all values of n.

In APL such completion occurred in many cases, as in
reduction over an empty, to extend to the case k=0 the
identity (f/x)=(f/k{take}x) f (f/k{drop}x), and in the
definition of zero to the power zero as 1, which is often
declared to be undefined in elementary math texts.
)
s=: _3 _2 _1 0 1 2 3

s % table s

^table~ -:s

NB. =========================================================
Lab Section
Completions in J fall in four classes:

* Specific functions, such as the power and divide, and
operators, such as the application of scan to monadic rather
than to dyadic functions as in APL.

* The variant or fit operator, used in the extension of the
power function to the rising factorial function.

* Completion of user-defined functions by specified identity
elements, inverses, derivatives, and integrals.

* Extensions of conformability rules.
)
0 1 (+./  ; *./ ; >./ ; <./) 0 1

(+./  ; *./ ; >./ ; <./) ''

+/\a=. 0 1 2 3 4

<\a

NB. =========================================================
Lab Section PRIMES AND FACTORING
)
p: 0        NB. Prime from its index

p: 0 1 2 3 4 5 6

]n=: ?. 100000

pi=: p:^:_1 NB. # of primes < or =

]m=: pi n

p: m - 1 0  NB. Results bracket n

q: 700

pi 7

p: i. 4

]exp=: 4 q: 700

(p: i.4)^exp

*/(p: i.4)^exp

NB. =========================================================
Lab Section
)
m=. 63

]e=. _ q: m

p: i.#e

]d=. (#: i.@(*/)) 1+e

d */ .(^~) p:i.#e     NB. all factors

n=. 182

_ q: m,n

(m*n), +/&.(_&q:) m,n

(m*.n), >./&.(_&q:) m,n

(m+.n), <./&.(_&q:) m,n

totient=: * -.@%@~.&.q:

(totient m), +/1=m+.i.m
