LABTITLE=: 'Pythagorean Triples'

NB. =========================================================
Lab Section PYTHAGOREAN TRIPLES

This is a companion to the Beauregard and Suryanarayan treatment of Pythagorean Triples in The College Mathematics Journal:

  Beauregard, R.A., and E.R. Suryanarayan, Pythagorean
  Triples: the Hyperbolic View
  The College Mathematics Journal, Vol 27, No. 3, May 1996

A vector such as 3 4 5, whose last element is the hypotenuse of the right-triangle with a pair specified by the first two, is called a pythagorean triple (PT) if the hypotenuse is an integer. We may define and use a hypotenuse function c as follows:
)
c=: %: @: (+/) @: *:
c 3 4

c 5 12

pt=: ],c  NB. Pythagorean triple
pt 3 4

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

We may assign more mnemonic names to the primitive functions used, and re-define c and pt in terms of them, as follows:
)
   on=: @:
    c=: sqrt on sum on sqr
   pt=: (right,c) "1
   sqrt=: %:
    sqr=: *:
    sum=: +/
   l=:  left=: [
   r=: right=: ]
a=: first=: {.
b=:  last=: {:
v=: 3 4
c v

pt v

NB. =========================================================
Lab Section THE SEMIGROUP OF TRIPLES

In the section of this title, B&S, i.e. Beauregard and Suryanarayan, define a function of two arguments that they denote by *. Applied to the pairs of two PTs it can be shown to produce the pair of a new PT. We will denote the function by p, and define it as specified in B&S:
)
p=:(a on l*a on r) , (b on l*c on r)+(b on r*c on l)

3 4 p  3 4

pt 3 4 p 3 4

3 4 p 5 12

pt 3 4 p 5 12

3 4 p 3 4 p 3 4

pt 3 4 p 3 4 p 3 4


NB. =========================================================
Lab Section MATRIX REPRESENTATION

B&S also define a mapping from a PT to a matrix, that we will treat as a function M to be applied to a pair. Thus:
)
M=: _3&(]\)@(c,b,0:,b,c,0:,0:,0:,a)

F=: _1 1&{@,

m=: M 3 4

m

F m

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

The matrix products of such matrices produce matrix representations of PTs. Thus:
)
   X=: +/ . *  NB. Matrix product
   m X m

   F m X m

   pt F m X m

NB. =========================================================
Lab Section PROPOSITION 1

B&E define the quasi-inverse of a pair and a number of its properties, which we will illustrate as follows:
)
qi=: 1 _1&*

A=: 4 3
B=: 3 4

qi A p B

(qi A) p (qi B)

qi qi A

A
A p qi A

a A

(*: a A), 0

E=: 1 0  NB. The identity of p
E p A
A p E

D=: 5 0

D p D

(*:@a D) * E

NB. =========================================================
Lab Section PRIMITIVE PAIRS AND NORMALIZATION

An integer scalar multiple of a pair is itself a pair, and to provide a single representative of the entire class a primitive pair is defined as one whose elements are "in lowest terms", and contain no common factors. We also define a normalization function N that produces the primitive representative of any pair:
)
D=: 3 * A

D

pt D

+./D  NB. The GCD of the elements of D

D % +./D

N=: ] % +./

N D

NB. =========================================================
Lab Section PROPOSITIONS 2,3

Under these propositions, B&S refer to the final section that presents a method of obtaining two integers m and n from any primitive pair, a method that we will incorporate in a function mn. We will also define an inverse function ab. Thus:
)
mn=: N@(a , b + c)

ab=: N on (*/ , half on diff on flip on sqr)
  half=: -:
  diff=: -/
  flip=: |.

v=: 5 12
w=: flip v
w

mn v

ab mn v

mn w

ab mn w

x=: v p w
x

pt x

mn x

ab mn x

NB. Note that the two case of Proposition 2 (for even and
NB. odd) are combined in mn by the use of normalization.

NB. =========================================================
Lab Section PRIMITIVE PAIRS AND NORMALIZATION

An integer scalar multiple of a pair is itself a pair, and to provide a single representative of the entire class a primitive pair is defined as one whose elements are "in lowest terms", and contain no common factors. We also define a normalization function N that produces the primitive representative of any pair:
)
D=: 3 * A
D

pt D

+./D  NB. The GCD of the elements of D

D % +./D

N=: ] % +./

N D

NB. =========================================================
Lab Section LIBRARY
)
on=: @:

c=: sqrt on sum on sqr
pt=: (right,c) "1
sqrt=: %:
 sqr=: *:
 sum=: +/
l=:  left=: [
r=: right=: ]
a=: first=: {.
b=:  last=: {:

p=: (a on l*a on r) , (b on l*c on r)+(b on r*c on l)

M=: _3&(]\)@(c,b,0:,b,c,0:,0:,0:,a)
F=: _1 1&{@,

X=: +/ . *  NB. Matrix product

qi=: 1 _1&*

E=: 1 0  NB. The identity of p

N=: ] % +./

mn=: N@(a , b + c)
ab=: N on (*/ , half on diff on flip on sqr)
  half=: -:
  diff=: -/
  flip=: |.

NB. =========================================================
Lab Section GENERATING PAIRS

B&S point out that all pairs can be produced by applying p to certain basis pairs. We will explore this by making a table of basis pairs and applying p between each of the pairs, using the "outer product" p"1/ . Thus:
)
] T=: 3 4,:4 3      NB. Table of basis elements

T p"1/ T            NB. Table of p applied between each pair

,/ T p"1/ T         NB. Table "ravelled" to a list of pairs

N"1 ,/ T p"1/ T     NB. Normalized table

~. N"1 ,/ T p"1/ T  NB. Nub (~.) suppresses duplicate items

pt"1~. N"1 ,/ T p"1/ T

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

We now embody these computations in a function, and finally sort the result for easy reading:
)
g=: ~. on (,/ on (N"1 on (p"1/~))) NB. Generating function

g T                    NB. Compare with earlier result

sort=: /:~             NB. Define sort function

sort g T               NB. Sort result

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

Since each row of the result is a product (by p) of two of the basis pairs, they are not themselves included. To ensure that they are included, we add the identity element E to the table:
)
] T1=: 1 0,T

/:~ g T1

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

To obtain complete results, the quasi-inverses of the bases must be added to the table. Thus:
)
]T2=: T1, qi"1 T  NB. Append quasi-inverses

/:~ g T2

/:~ g^:2 T2

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

Approximate results are due to machine arithmetic:
)
/:~ g^:3 T2

NB. =========================================================
Lab Section EXTENDED PRECISION

Larger integers may be handled to infinite precision by using the function x: as illustrated below:
)
x: T2

/:~ pt"1 g^:3 x: T2

NB. =========================================================
Lab Section DISTINCT TRIANGLES

We may obtain a more abbreviated list of distinct triangles by taking the magnitude, replacing an entry such as 5 _12 by 5 12, and by sorting each row, replacing an entry such as 12 5 by 5 12, and then suppressing duplicate rows. We will also sort the result. Thus:
)
tri=: pt sort ~. sort"1 | g^:3 x: T2
tri

NB. =========================================================
Lab Section PRIME FACTORS

We will conclude by showing the prime factors of the table of triangles, first removing the identity element, because an attempt to factor a zero would give a domain error:
)

<@q: }. tri
