```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) = ```
$x^3$
```APL ∇ z←cube X```
```[1] Z←X*3 ∇```
```J cube =: ^&3```
```FUNCTION EXPRESSION CONSTANT```
```ENGLISH 3 plus 4 tenths```
```MATH 3 + 4/10 3.4```
```APL 3 + 4÷10 3.4```
```J 3 + 4%10 3.4```
```ENGLISH 3 plus 4 imaginary```
```MATH 3 + 4i```
```APL 3 + 4ׯ1*0.5 3J4```
```J 3 + 4*_1^0.5 3j4```
```ENGLISH Eulers Number```
```MATH e```
```APL *1```
```J ^1 1x1```
```ENGLISH Pi```
```MATH ```
$\pi$
```APL ∘1```
```J o.1 1p1```
```)```
In [ ]:
y=: 4
In [ ]:
! y
In [ ]:
6:y
In [ ]:
6"_ y
In [ ]:
4 5 6"_ y
In [ ]:
'abcd'"_ y
In [ ]:
^. 10 2 NB. Natural logs
In [ ]:
10 -&^. 2 NB. Diff of logs
In [ ]:
10 -&.^. 2 NB. Dual of - wrt log
In [ ]:
^ 10 -&^. 2 NB. Inv log of diff
In [ ]:
10 ^. 2 NB. Base 10 log of 2
In [ ]:
10 -@^. 2 NB. - of base10 log
In [ ]:
x=. 3 1 4 1 5 9
In [ ]:
|. x NB. Reverse
In [ ]:
+/\ & |. x NB. Partial sums of reverse
In [ ]:
+/\ &. |. x NB. Dual of partial sums wrt reverse
In [ ]:
|. +/\ |. x
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:
In [ ]:
a=: 0 1 2 3 4
In [ ]:
+/ a NB. Plus over a (sum)
In [ ]:
a +/ a NB. Plus table
In [ ]:
h=: ! + *:
In [ ]:
h 4
In [ ]:
d=: 2 3 4
In [ ]:
h d
In [ ]:
q=: ! , *:
In [ ]:
q 4
In [ ]:
q d
In [ ]:
mean=: +/ % #
In [ ]:
center=: ] - mean
In [ ]:
(] ; mean ; center ; ] - +/ % #) d
In [ ]:
[a=: i.2 3 4
In [ ]:
<"2 a NB. Rank 2 cell, 2-cell, Items of 3-cell
In [ ]:
<"1 a NB. 1-cells, Items of 2-cells
In [ ]:
<"0 a NB. 0-cells, Atoms
In [ ]:
[a=: i.2 3 4
In [ ]:
<"_1 a NB. _1 cells of a, Items of a
In [ ]:
v=: 0 1 2 3 4 5
In [ ]:
<"_1 v NB. _1 cells of v, Items of v
In [ ]:
d=: 2 3 4
In [ ]:
a=: d $ 'ABCDEFGHIJKLMNOPQRSTUVWX'
In [ ]:
(] ; |. ; |."3 ; |."2 ; |."1 ; |."0) a
In [ ]:
i. d
In [ ]:
i."0 d
In [ ]:
i=: 0 1 2 3 4 5
In [ ]:
bc=: i !~/i
In [ ]:
bc ; (%. bc); (+/"1 bc)
In [ ]:
rou=: ^@(0j2p1"_ * i. % ]) NB. roots of unity
In [ ]:
r=: rou 7
In [ ]:
r
In [ ]:
5 5 {.*/~ r NB. multiplication table (first 5 rows, cols)
In [ ]:
r i. */~ r
In [ ]:
7&|@+/~ i.7 NB. addition modulo 7
In [ ]:
x=: 0
In [ ]:
cos=: 2&o.
In [ ]:
cos 0
In [ ]:
cos cos 0
In [ ]:
cos ^: 0 1 2 3 4 x
In [ ]:
y=: cos ^: _ x
In [ ]:
y
In [ ]:
y = cos y
In [ ]:
]z=: cos ^: _1 x=: _0.5 0 0.5 0.75 1
In [ ]:
cos z
In [ ]:
f=: -:@(+ 1000&%) NB. halve of x plus 1000 divided by x
In [ ]:
f 1
In [ ]:
f f 1
In [ ]:
f^:(i.2 5) 1
In [ ]:
] y=: f^:(_) 1
In [ ]:
y*y
In [ ]:
1000-y*y
In [ ]:
SG=: 1 : '~.@(, ,/@(x/~))^:_' NB. subgroup
In [ ]:
f=: 7&|@* NB. multiplication modulo 7
In [ ]:
f SG 2 NB. subgroup generated by 4
In [ ]:
f/~ f SG 2 NB. group table of subgroup
In [ ]:
f SG 3 NB. subgroup generated by 3
In [ ]:
f SG 2 3 NB. subgroup generated by 2 and 3
In [ ]:
p=: (1|.i.5),: (<0 1) C. i.5
In [ ]:
p NB. two permutations
In [ ]:
$ {"1 SG p NB. subgroup generated by the two permutations
In [ ]:
f=: |.@(|/\)
In [ ]:
g=: *@{.
In [ ]:
f^:g y=: 32 44
In [ ]:
f^:g^:(i.5) y
In [ ]:
f^:g^:_ y
In [ ]:
+./y
In [ ]:
f=: {: ,: {. - {: * <.@%&{./
In [ ]:
g=: *@{.@{:
In [ ]:
f^:g^:_ y,.=i.2
In [ ]:
_4 3 +/ .* y NB. GCD as a linear combination of original arg
In [ ]:
<"2 f^:g^:(i.6) y,.=i.2
In [ ]:
I=: ^:_1 NB. Inverse operator
In [ ]:
cos I 0
In [ ]:
^I 1x1 1x2 NB. Natural log
In [ ]:
^ ^I 1x1 1x2 NB. Exponential of log
In [ ]:
cube=: ^&3
In [ ]:
cube I 27 64 100
In [ ]:
'a b'=: 0 0 1 1 ; 0 1 0 1
In [ ]:
a *. b NB. a and b
In [ ]:
-.a NB. Not a
In [ ]:
a *.&.-. b NB. Dual of and wrt not
In [ ]:
a +. b NB. Is equivalent to or
In [ ]:
3 +&.^. 4
In [ ]:
3 * 4
In [ ]:
log=: 10&^.
In [ ]:
log 3 4 10 100
In [ ]:
3 +&.log 4
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:
In [ ]:
'x e s' =: 6 4 1
In [ ]:
(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s) NB. Rising
In [ ]:
s=: _1
In [ ]:
(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s) NB. Falling
In [ ]:
s=: 0
In [ ]:
(x+0*s)*(x+1*s)*(x+2*s)*(x+3*s) NB. Power
In [ ]:
x ^ e
In [ ]:
x ^!.1 e NB. Rising factorial
In [ ]:
x ^!._1 e NB. Falling factorial
In [ ]:
x ^!.0 e NB. Power
In [ ]:
FT=: 1 : '^!.x/~@i.' NB. falling/rising factorial table
In [ ]:
_1 FT 6 NB. falling factorial table
In [ ]:
0 FT 6 NB. power table
In [ ]:
]S2=:(0 FT %. _1 FT) 6 NB. Matrix quotient of above tables
In [ ]:
]S1=:(_1 FT %. 0 FT) 6 NB. Matrix inverse of S2
|: S2 are the Stirling numbers of the second kind |: |S1 are the Stirling numbers of the first kind
A polynomial is so named because it can be expressed as a sum of monomials of the form $cx^k$.
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:
In [ ]:
'c x y'=: _3.75 11.5 _9 2 ; 5 ; 0 1 2 3 4
In [ ]:
c p. x
In [ ]:
+/ c * x ^ 0 1 2 3
In [ ]:
c p. y
In [ ]:
]r=: p. c
In [ ]:
r p. x
In [ ]:
p. r
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:
In [ ]:
f=: 3 1 4 2 & p.
In [ ]:
'x i'=: 0 1 2 3 4 ; 0 1 2 3 4 5
In [ ]:
f x
In [ ]:
f t. i
In [ ]:
^ t. i
In [ ]:
(^ t. i) p. x
In [ ]:
^ x
In [ ]:
% ^ t. i
In [ ]:
^ t: i NB. weighted Taylor coefficients for exponential
In [ ]:
1&o. t: i NB. sine
In [ ]:
5&o. t: i NB. hyperbolic sine
In [ ]:
p=: 4 _3 1 2&p.
In [ ]:
q=: _1 5 1&p.
In [ ]:
(p+q) t. i.10
In [ ]:
(p-q) t. i.10
In [ ]:
(q-p) t. i.10
In [ ]:
(p*q) t. i.10
In [ ]:
(p%q) t. i.10
In [ ]:
(q%p) t. i.10
In [ ]:
p@q t. i.10
In [ ]:
q@p t. i.10
In [ ]:
p@>: t. i.10
In [ ]:
p@<: t. i.10
In [ ]:
(0 1&p. % 1 _1 _1&p.) t. i.15
In [ ]:
(% -. - *:) t. i.15
In [ ]:
]rp=: 8 ?. 8 NB. Random permutation
In [ ]:
rp { 'ABCDEFGH'
In [ ]:
(i. ! 3) A. i. 3 NB. All !3 of order 3
In [ ]:
A. 1 0 2 NB. Index of perm 1 0 2
In [ ]:
]p=: 20 ?. 20
In [ ]:
A. p NB. Extended precision
In [ ]:
]cy=: C. rp NB. Cycle representation
In [ ]:
C. cy NB. C. is self-inverse
In [ ]:
SG=: 1 : '~.@(, ,/@(x/~))^:_'
In [ ]:
] p=. ?.~ 22 NB. a random permutation of order 22
In [ ]:
g=.{"1 SG ,: p NB. subgroup generated by p
In [ ]:
#g
In [ ]:
C. p NB. cycles of p
In [ ]:
*./ #&> C. p NB. LCM of cycle lengths
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 $x^n$ 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↑x) f (f/k↓x)
, and in the
definition of $0^0$ as 1, which is often
declared to be undefined in elementary math texts.
In [ ]:
s=: _3 _2 _1 0 1 2 3
In [ ]:
s % table s
In [ ]:
^table~ -:s
Completions in J fall in four classes:
Specific functions such as 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.
In [ ]:
0 1 (+./ ; *./ ; >./ ; <./) 0 1
In [ ]:
(+./ ; *./ ; >./ ; <./) ''
In [ ]:
+/\a=. 0 1 2 3 4
In [ ]:
<\a
In [ ]:
p: 0 NB. Prime from its index
In [ ]:
p: 0 1 2 3 4 5 6
In [ ]:
]n=: ?. 100000
In [ ]:
pi=: p:^:_1 NB. # of primes < or =
In [ ]:
]m=: pi n
In [ ]:
p: m - 1 0 NB. Results bracket n
In [ ]:
q: 700
In [ ]:
pi 7
In [ ]:
p: i. 4
In [ ]:
]exp=: 4 q: 700
In [ ]:
(p: i.4)^exp
In [ ]:
*/(p: i.4)^exp
In [ ]:
m=. 63
In [ ]:
]e=. _ q: m
In [ ]:
p: i.#e
In [ ]:
]d=. (#: i.@(*/)) 1+e
In [ ]:
d */ .(^~) p:i.#e NB. all factors
In [ ]:
n=. 182
In [ ]:
_ q: m,n
In [ ]:
(m*n), +/&.(_&q:) m,n
In [ ]:
(m*.n), >./&.(_&q:) m,n
In [ ]:
(m+.n), <./&.(_&q:) m,n
In [ ]:
totient=: * -.@%@~.&.q:
In [ ]:
(totient m), +/1=m+.i.m
In [ ]: