Tables are commonly used in mathematics, not only to organize such data as sines and logs, but, more interestingly, to provide insight, as in the addition and multiplication tables used in elementary school.
Computers now make it more convenient to compute results such as sines and logs than to consult tables, but also make it mor practicable to produce tables for purposes of elucidation and exploration. This lab explores the use of tables for insight, using the programming language J.
In [ ]:
v=: (0,1,2,3,4,5)
In [ ]:
v + v NB. Addition of vectors
In [ ]:
v +/ v NB. Addition table (/ is the table operator)
The symmetry of its table suggests that addition is commutative
In [ ]:
(v */ v) ; (v *./ v) ; (v -/ v) NB. Times, lcm, and diff
In [ ]:
binomials=: v !/ v NB. Table of binomial coefficients
In [ ]:
altbin=: %. binomials
In [ ]:
X=: +/ . * NB. The matrix product function
In [ ]:
binomials ; altbin ; binomials X altbin
In [ ]:
a=: 2 3 5 7 NB. (=: may be read as "is" or "are")
In [ ]:
b=: 0 1 2 3 4 NB. Exponents
In [ ]:
a ^/ b NB. Powers of primes
In [ ]:
a ^table b NB. Bordered table of powers
In [ ]:
v (+table ,. -table ,. %table) v NB. Sum, Difference, Quotient
In [ ]:
- |. v
In [ ]:
}.v
In [ ]:
s=: (-|.v) , }.v NB.Symmetric list
In [ ]:
s
In [ ]:
mt=: s */ s
In [ ]:
mt
Other functions provide interesting tables when applied to symmetric arguments. Thus:
In [ ]:
s !table s
Examples from earlier sections may provide further insights if the argument v is replaced by s.
In math, the symbol - is used ambivalently to denote subtraction when used dyadically (with two arguments), and to denote negation when used monadically. In J, all functions are ambivalent. In particular, * denotes multiplication when used dyadically, and signum when used monadically, as in the expression * mt below.
The tables mt and * mt show clearly that a zero row and a zero column divide the multiplication table into four quadrants, each of a common sign, providing a visual representation of the rule of signs used in products. Moreover, a vertical passage from one quadrant to another reverses the sign of the left argument and of the result, and horizontal passage reverses the sign of the right argument, facts that provide some justification for the rule.
In [ ]:
mt=: s */ s
In [ ]:
mt
In [ ]:
* mt
Functions are commonly completed by extending them to arguments not comprehended in their original definitions. For example, expressing the simple notions of squares and cubes
```2 3 in the forms x and x suggests the possible extensions to exponents that are negative, non-integral, zero, and complex; completions that provide both insight and power.```
The similar completion of the factorial, in the gamma function, is a further familiar example. Completions are normally subjected to careful analysis to ensure that they fit smoothly into the original definitions. Consider, for example, the summation function, denoted in math by a capital Greek Sigma, and in J by +/, used monadically. Thus
In [ ]:
+/2 3 5 7 NB. Sum over primes
In [ ]:
2+3+5+7
In [ ]:
i. 5 NB. First five non-negative integers
In [ ]:
+/ i. 5
In [ ]:
+/ i. 0
The last example above concerns the case of an empty vector, a case not comprehended in the simple notion of inserting the function + between successive items in a list. The result in J is a zero, a completion that might appear to be "obvious".
But what is the rationale for the completions of the following functions?
In [ ]:
*/ i.0 NB. Times on empty vector
In [ ]:
<./ 3 2 5 7 NB. Minimum over
In [ ]:
<./ i.0 NB. Minimum over empty vector (_ is infinity)
The rationale may be illustrated as follows:
In [ ]:
a=: 2 3 5 7 11
In [ ]:
k=: 3
In [ ]:
k{.a NB. Take k items of a
In [ ]:
k}.a NB. Drop k items of a
In [ ]:
(*/a) ; (*/k{.a) ; (*/k}.a) ; (*/k{.a) * (*/k}.a)
In [ ]:
(*/a) = (*/k{.a) * (*/k}.a) NB. A tautology
In [ ]:
k=:0
In [ ]:
k (*/@] ; */@{. ; */@}. ; */@{. * */@}.) a
Similarly for the minimum function, whose neutral is infinity (denoted here by _):
In [ ]:
(<./a) ; (<./k{.a) ; (<./k}.a) ; (<./k{.a)<.(<./k}.a)
These results suggest (correctly) that the result of f/i.0 is the "identity element" or "neutral" of the function f, the neutral of f being that argument n for which n f x yields x for any argument x The case of the minimum function <. is particularly interesting, since infinity might well be DEFINED as its neutral.
Function tables can suggest the manner of completion of a function. For example, what value would you suggest for the blank entry (at 0 f 0) in order to complete the following function?
```+--+-------------------------+```
```| | _3 _2 _1 0 1 2 3|```
```+--+-------------------------+```
```|_3|_1r27 1r9 _1r3 1 _3 9 _27|```
```|_2| _1r8 1r4 _1r2 1 _2 4 _8|```
```|_1| _1 1 _1 1 _1 1 _1|```
```| 0| _ _ _ 0 0 0|```
```| 1| 1 1 1 1 1 1 1|```
```| 2| 1r8 1r4 1r2 1 2 4 8|```
```| 3| 1r27 1r9 1r3 1 3 9 27|```
```+--+-------------------------+```
Obvious possibilities include _
and 0 and 1 and _.
"indeterminate". Since this is a portion of the power table (^), and since some math texts insist that zero to the power zero is undefined, the indeterminate might suggest itself.
However, further analysis will suggest the value 1, used in J, for the following reasons:
Expressions for polynomials of the form $c_0 x^0 +c_1 x^2 +c_2 x^2$ and $\sum c_k x^k$ would otherwise fail to evaluate properly for a zero argument x
If one argument is made to approach its limit as a function of the other (as in x^p x), and if p is any polynomial other than identically zero, then LHopitals Rule can be used to show that the indeterminate 0^0 must be 1.
Again, suggest a completion for the following function table:
```+--+----------------------------+```
```| | _3 _2 _1 0 1 2 3|```
```+--+----------------------------+```
```|_3| 1 3r2 3 __ _3 _3r2 _1|```
```|_2| 2r3 1 2 __ _2 _1 _2r3|```
```|_1| 1r3 1r2 1 __ _1 _1r2 _1r3|```
```| 0| 0 0 0 0 0 0|```
```| 1|_1r3 _1r2 _1 _ 1 1r2 1r3|```
```| 2|_2r3 _1 _2 _ 2 1 2r3|```
```| 3| _1 _3r2 _3 _ 3 3r2 1|```
```+--+----------------------------+```
If this is recognized as part of the divide (%) table, the indeterminate might again be suggested. However, the pattern of the table clearly suggests 0, as consistent with the rest of the row and "midway between" the negative and positive infinities in the column. Moreover, the analysis by E.E. McDonnell in "Zero Divided by Zero", APL76, published by ACM, makes a strong case for the zero used in J.
In [ ]:
3 % 4
In [ ]:
4 %~ 3
In [ ]:
into=: %~
In [ ]:
3 4 5 into 6 12 10
In [ ]:
from=: -~
In [ ]:
3 4 5 from 6 12 10
A table of the function f~ is clearly the transpose of the table of f. For example:
In [ ]:
i=: i. 5
In [ ]:
i (-/ ; -~/ ; */ ; *~/ ; */ = *~/) i
In [ ]:
k=: 1 2 3 4
In [ ]:
st=: k -/ k NB. Subtraction table
In [ ]:
cst=: k -~/ k NB. Commuted subtraction table
In [ ]:
dv=: k %/ k NB. Divide table
In [ ]:
cdv=: k %~/ k NB.Commuted divide table
In [ ]:
st ; cst ; st + cst
In [ ]:
dv ; cdv ; dv * cdv
The relations <, <: (less or equal), >, >:, =, and ~: (notequal) and their commutes also provide interesting function tables. For example:
In [ ]:
s <:table s
In [ ]:
i=: 0 1 2 3 4
In [ ]:
goe=: i >:/ i NB. Greater or equal table
In [ ]:
goe ; %. goe NB. Greater or equal and its inverse
The inverse matrix %. goe is a difference matrix, in a sense to be established. The matrix product function produces sums (+/) over the element-by-element products (*) of rows of the left argument with columns of the right. The function may be defined using the inner product or dot operator as follows:
In [ ]:
X=: +/ . * NB. The space before the dot is essential
In [ ]:
goe X (%. goe) NB. Product of inverses is an identity matrix
In [ ]:
pr=: 2 3 5 7 11 NB. Primes
In [ ]:
partials=: goe X pr NB. Partial sums or subtotals
In [ ]:
partials
In [ ]:
(%. goe) X partials NB. Differences of partial sums
In [ ]:
outof=: !
In [ ]:
3 outof 5
The number of distinct ways of choosing 3 things from 5
In [ ]:
5 outof 3
The number of distinct ways of choosing 5 things from 3
From this it appears that the zeros in the binomial coefficients table are meaningful; nonetheless, they are normally suppressed (from the commuted table !~/) in the presentation of the triangle of Pascal, presumably in the interest of readability.
The result is that the triangle of Pascal does not represent a matrix, although the matrix represented by the table of binomial coefficients is eminently useful. For example:
In [ ]:
i=: 0 1 2 3 4 5
In [ ]:
bct=: i !/ i
In [ ]:
abct=: %. bct NB. Inverse is table of alternating binomials
In [ ]:
bct ; abct
In [ ]:
+/ bct NB. The sums over the binomial coefficients
In [ ]:
2^i NB. are the powers of two 1 2 4 8 16 32
In [ ]:
+/ abct NB. The sums over the alternating binomials
This last result is sometimes mis-stated (as in the National Bureau of Standards Handbook of Mathematical Functions, page 10) as identically zero.
In [ ]:
c=: 4 0 1 3 2 1
In [ ]:
x=: i.8
In [ ]:
x
In [ ]:
c p. x NB. Polynomial with coefficients c
In [ ]:
d=: bct X c NB. Expanded coefficients
In [ ]:
d
In [ ]:
d p. x
In [ ]:
c p. x+1
In [ ]:
abct X d NB. Inverse restores coefficients
In [ ]:
e=: abct X c
In [ ]:
e
In [ ]:
e p. x
In [ ]:
c p. x-1
The term power is commonly used for the function denoted in mathematics by $x^n$, and in J by x^n, i.e. using the symbol ^ introduced for this purpose by de Morgan. But the term is also used for the operator ^:, with f^:n signifying n applications of the monadic function f.
Note that in math $sin^2$ does not denote two applications of the sine function. The relation between the two notions may be visualized by construing the power function as repeated application of multiplication. Thus:
In [ ]:
2^3 8
In [ ]:
2*2*2 8
In [ ]:
*/2 2 2 8
In [ ]:
*/3#2 NB. Product over three copies of two 8
Although the definition as repeated multiplication does not comprehend the case of a zero exponent, the completion is correctly provided by the neutral of multiplication. Thus:
In [ ]:
*/0#2
In [ ]:
2^0
In [ ]:
s=: 4 -~ i.9
In [ ]:
s
In [ ]:
5^s
In [ ]:
i=: 0 1 2 3 4 5
In [ ]:
bct=: i !/ i
In [ ]:
c=: 4 0 1 3 2 1
In [ ]:
X=: +/ . *
In [ ]:
f=: bct&X
In [ ]:
f f c
In [ ]:
c2=: f f c
In [ ]:
c2 p. x
In [ ]:
c p. x+2
The operator ^: produces a power of its function left argument, or a collection of powers:
In [ ]:
f ^: 2 c
In [ ]:
f^: 0 1 2 3 4 c
In [ ]:
I=: i =/ i
In [ ]:
] powers=: f^: 0 1 2 3 I
In [ ]:
powers %"2 bct
Rank-2 divide applies to each rank-2 item (each matrix)
In [ ]:
bct (*"2) 0 1 2 3 (^"0 2) 0 >. i -~/ i
In [ ]:
c=: 1 3 3 1
In [ ]:
0,c
In [ ]:
c,0
In [ ]:
(0,c)+(c,0)
This process may be embodied in a function as follows:
In [ ]:
next=: 0&,+,&0
In [ ]:
next c
In [ ]:
p=: i.6
In [ ]:
next ^: p 1
Pattern-spotting may be greatly aided by computer calculations, as illustrated by the emergence of a pattern in the matrix-product-powers of the binomial coefficient table bct under element-by-element division by bct itself.
We will now suggest a few general tools. Try to discern patterns in the following lists, and then examine the subsequent calculations on them:
In [ ]:
a=: 2 1 2 2 4 2 4 2 4 6 2 6 4 2 4 6 6 2 6 4
In [ ]:
b=: 2 5 10 17 28 41 58 77 100 129 160 197 238 281 328 381 440 501 568 639
In [ ]:
c=: 2 _1 1 0 2 _2 2 _2 2 2 _4 4 _2 _2 2 2 0 _4 4 _2
In [ ]:
sum=: +/\ NB. Subtotals
In [ ]:
dif=: sum^:_1 NB. Differences are inverse of subtotals
In [ ]:
sum a
In [ ]:
dif b
In [ ]:
sum sum c
In [ ]:
bct=: i !/ i
In [ ]:
I=: i =/ i
In [ ]:
X=: +/ . *
In [ ]:
f=: bct&X
In [ ]:
powers=: f^: 0 1 2 3 I
In [ ]:
box=: <"2 NB.Box rank-2 arrays (matrices)
In [ ]:
box powers
In [ ]:
2 2 $ box powers
For many functions, it is possible to apply an operator to obtain the coefficients of a polynomial that approximates the function. This operator is called a Taylor series operator, and is denoted by t. . We will begin with polynomial functions, for which the approximation is exact. Thus:
In [ ]:
c=: 4 0 1 3 2 1 NB. Coefficients used in earlier Section
In [ ]:
x=: i. 8
In [ ]:
c p. x
In [ ]:
f=: c&p.
In [ ]:
f x
In [ ]:
f t. 0 2 NB. Coefs 0 and 2 of Taylor series
In [ ]:
tcf=: f t. i=: 0 1 2 3 4 5 6 7 8 NB. First 9 Taylor coefs
In [ ]:
tcf
In [ ]:
tcf p. x
In [ ]:
g=: f@>: NB. Function f atop increment
In [ ]:
g x
In [ ]:
tcg=: g t. x
In [ ]:
tcg
In [ ]:
tcg p. x
In [ ]:
c p. x+1
In [ ]:
exp=: ^
In [ ]:
sin=: 1&o.
In [ ]:
cos=: 2&o.
In [ ]:
exp t. k=: i.7
In [ ]:
(exp t. , sin t. ,: cos t.) k
The non-zero coefficients may be recognized as reciprocal factorials, and a display of the reciprocals shows the pattern more clearly:
In [ ]:
% exp t. k
In [ ]:
%(exp t. , sin t. ,: cos t.) k
Finally, the operator t: gives weighted Taylor coefficients (that is, each multiplied by the appropriate factorial), and its results show the pattern even more clearly. The less familiar hyperbolic sine and hyperbolic cosine may also be added to this list. Their series do not alternate in sign:
In [ ]:
(exp t: , sin t: ,: cos t:) k
In [ ]:
sinh=: 5&o.
In [ ]:
cosh=: 6&o.
In [ ]:
(exp t: , sinh t: , sin t: , cosh t: ,: cos t:) k
In [ ]:
(exp t: ,: sinh t: + cosh t:) k
The last result illustrates the fact that the exponential is the sum of the hyperbolics. Such patterns can be explored further by applying these functions to complex arguments, using the function j. that multiplies its argument by the square root of minus one. For example:
In [ ]:
j. a=: 0.1*i.5
In [ ]:
cosh j. a
In [ ]:
cos a
In [ ]:
cosh@j. a
In [ ]:
r1=: 1&p. % 1 1&p.
In [ ]:
r1 k=: i. 8
In [ ]:
r1 t. k
In [ ]:
r2=: 1 4 6 4 1&p. % 1 2 1&p.
In [ ]:
r2 k
In [ ]:
r2 t. k
In [ ]:
r3=: 0 1&p. % 1 _1 _1&p. NB. Expressed in math as z/1-z-z2
In [ ]:
r3 t. k NB. Fibonacci numbers
The expression:
$$ 3+\frac{1}{7} $$is commonly used as an approximation to pi. An even better approximation is given by:
$$ 3+\frac{1}{7+\frac{1}{15+\frac{1}{1}}} $$Such an expression is called a continued fraction, and can be completely characterized by the list of integers, in this case, 3 7 15 1. In J it might be evaluated as:
In [ ]:
3 + 1 % 7 + 1 % 15 + 1 % 1
Using the reciprocal (monadic %) it may be written more simply:
In [ ]:
3 + % 7 + % 15 + % 1
In [ ]:
cf=: (+%)/
In [ ]:
cf 3 7 15 1
In [ ]:
cf\ 3 7 15 1
In [ ]:
cf\ 1 1 1 1 1 1 1 1 1
In [ ]:
cf\ 1 2 2 2 2 2 2
In [ ]:
cf 3 , 3 6 , 3 6 , 3 6
In [ ]:
cf 5 , 2 1 1 2 10
In [ ]:
%: 2 11 29
In [ ]:
gm=: 1.61765 NB. Approximation to the Golden Mean
In [ ]:
gm * gm -1
In [ ]:
]x =: o. 1
In [ ]:
]x=: 3 , % 0.14159
In [ ]:
]x=: 3 7 , % 0.06265
In [ ]:
]x=: 3 7 15 , % 0.9617
In [ ]:
]x=: 3 7 15 1 , % 0.03983
An expansion function may therefore be defined and used as follows:
In [ ]:
cfex=: }: , <.@{: , 1: % {: - <.@{:
In [ ]:
cfex pi=: o.1
In [ ]:
cfex cfex pi
In [ ]:
cfex ^: 0 1 2 3 4 5 6 pi
In [ ]:
] e=: 3 3 $ 6 5 4 7 0 3 8 1 2 6 5 4 7 0 3 8 1 2
In [ ]:
stick=: ] , >:@i.@{:@$ + >./@,
In [ ]:
roll=: |:@|.
In [ ]:
snow=: roll@stick ^:2
In [ ]:
stick e
In [ ]:
roll stick e
In [ ]:
(] ; snow ; snow^:2 ; snow^:4) e
In [ ]:
]spiral =: snow^:2 e
It should be possible to use the pattern shown in the table as a guide in defining a more direct solution to the generation of a spiral.
In his paper on Volutes in Vector, Vol 13 # 2, E.E. McDonnell analyzes solutions by six different authors, including one in Concrete Mathematics by Graham, et al. One interesting approach is to ravel the table to produce a permutation vector which can then be used to permute the list i. 25, and finally reshape the result into a table:
In [ ]:
perm=: ,spiral
In [ ]:
perm
In [ ]:
5 5 $ perm { i. 25
Of course, the problem of how to generate this permutation remains, and forms the bulk of the discussion of McDonnell. He remarks that there are sixteen related spirals: zero to three rolls produce tables that "end" at four different corners; reversal (|.) reverses the sense to produce a clockwise spiral, and subtraction, i.e. from 24 in the case of the 5-by-5 table, produces an involute. Examples of each case, and all cases, are produced as follows:
In [ ]:
box=: <"2 NB. Box rank-2 arrays (matrices)
In [ ]:
box spiral , (|.spiral) , (roll spiral) ,: (24-spiral)
In [ ]:
invol=: */@$ - 1: + ] NB. Involute from evolute
In [ ]:
all=: invol^:0 1"2@(|.^:0 1"2)@(roll^:0 1 2 3)
In [ ]:
$ all spiral
In [ ]:
$ box all spiral
In [ ]:
$ , box all spiral
In [ ]:
4 4 $ , box all spiral
In [ ]:
]i=: 2 %: _1
In [ ]:
3 + 4 * i
In [ ]:
j. 4
In [ ]:
3 j. 4
In [ ]:
%: _1 NB. Primitive third root of _1
In [ ]:
r=: %: & _1
In [ ]:
,. r a=: 2 3 4 5 6 NB. Some primitive roots of _1
Since the nth power of r n is _1, the 2*
nth power is 1, and powers therefore repeat in cycles of 2*n This is illustrated by the left panel of the following table.
The middle panel shows the real and imaginary parts that result from the function +., and the right panel shows them in a more readable form, formatted to 7 spaces per column, with 3 digits following the decimal point:
In [ ]:
t=: (r 3) ^ i. 8
In [ ]:
(,. ; +. ; 7j3&":@+.) t
In [ ]:
poly=: 7j3&": @ +. @ (r ^ +:@i.)"0
In [ ]:
<@poly 2 3 4 6
In [ ]:
r=: %:&_1
In [ ]:
all=: r ^ i.@+:
In [ ]:
a3=: all 3
In [ ]:
a3
In [ ]:
t=: a3 */ a3 NB. Times table
In [ ]:
d=: a3 %/ a3 NB. Divide table
In [ ]:
t
In [ ]:
(1=t) ; (_1=t) ; (1=d) ; (_1=d)
In [ ]: