   NB. The Role of Composition in Computer Programming
   NB. APL95 Conference Proceedings, San Antonio, Texas.  June 1995
   NB. Copyright ACM
   
   NB. Donald B. McIntyre
   NB. Luachmhor, Church Road
   NB. Perth PH2 7LD
   NB. Scotland - UK
   NB. Telephone:  (44) 1738-860-726
   NB. E-mail: donald.mcintyre@almac.co.uk
   
   NB. Set View to boxed display.
   NB. Using Run, press shift key to display in the active session
   
   NB. 2  WHAT IS FUNCTION COMPOSITION?
      - 0 1 2 3                   NB. (verb noun) is noun. Monad
0 _1 _2 _3
      -: 0 1 2 3                  NB. Halve
0 0.5 1 1.5
      (-:@-)  0 1 2 3             NB. Compose a new verb (function)
0 _0.5 _1 _1.5
      -:@-  0 1 2 3               NB. Parentheses not necessary
0 _0.5 _1 _1.5
      h=.+:@+                     NB. Name a composite function
      g=. >:                      NB. Increment
      f=. *:                      NB. Square
      h=. g@f                     NB. Mathematics Dictionary (1992)
      e=. f@g                     NB. h and e are not the same
      h 2
5
      e 2
9
      NB. @ (atop) " (rank) and D. (derivative) are conjunctions
      v=. h"0                     NB. (v c n) is verb
      y=. h x=. i.5
      v D.1 x                     NB. First derivative (v c n)
0 2 4 6 8
   
      d=. h"0 D.                  NB. ((v c n) c) is adverb
      (1 d)                       NB. (n a) is v.  First derivative
Ŀ
ĿD.1
h"0   
ٳ   

      (2 d)                       NB. Second derivative
Ŀ
ĿD.2
h"0   
ٳ   

x,y,(1 d y),: 2 d y         NB. 1st and 2nd derivatives
0 1  2  3  4
1 2  5 10 17
2 4 10 20 34
2 2  2  2  2
      y=. i.3 4  [  n=. 1 1 ; 2 2; 0 3
      amend=. n}                  NB. (n - a) is verb
      97 98 99 amend y            NB. (n v n) is noun.  Dyad
0  1  2 99
4 97  6  7
8  9 98 11
      %&180 (30 60 90)            NB. (v c n) Divide with (by) 180
0.1666667 0.3333333 0.5
      NB. =============================================
      NB. 3  COMPOSITIONS OF 2 OR 3 VERBS
      f=. %  [. g=. +  [. h=. *   NB. Name verbs
      x=. 2 [ y=. 5               NB. Name nouns
   
      NB. 3.1  COMPOSITION OF 2 VERBS USED AS DYAD
      x g@h y                     NB. Conjunction:  Atop
10
      g (x h y)
10
      x g&h y                     NB. Conjunction:  With
2
      (h x) g (h y)
2
      x (g h) y                   NB. Hook
3
      x g (h y)
3
   
      NB. 3.2  COMPOSITION OF 2 VERBS USED AS MONAD
      g@h y
1
      g (h y)
1
      g&h y
1
      g (h y)
1
      (g h) y
6
      y g (h y)
6
   
      NB. 3.3  COMPOSITION OF 3 VERBS:  FORKS
      x (f g h) y               NB. Dyad
10.4
      (x f y) g (x h y)
10.4
      (f g h) y                 NB. Monad
1.2
      (f y) g (h y)
1.2
   
      NB. Some variants
      (] g h) y
6
      (g h) y
6
      x ([ g h@]) y
3
      x (g h) y
3
      x (] g h) y
15
      y g (x h y)
15
      x ([ g h) y
12
      x g ( x h y)
12
      x (f@[ g h@]) y
1.5
      (f x) g (h y)
1.5
   
      NB. Conjunctions have long left-scope and short right-scope
      f@g@h            NB. Conjunctions are "fom left to right"
Ŀ
Ŀ@h
f@g  
ٳ  

      f@(g@h)          NB. Parentheses control order of execution
Ŀ
f@Ŀ
  g@h
  ٳ

      tree=. 5!:4@<    NB. (n c n) is v  and  (v c v) is v
      tree 'tree'      NB. (v n) is n
              5
       !:  4
 @  <       
      e=. f@g@h
      tree 'e'
             f
       @  g
 @  h      
   
      NB. VIEW can be SET to linear, box, or tree
      NB. !: is the "foreign" conjunction
      NB. (n c n) is v  and (v c v) is v.
      linear=. 5!:5@<               NB. Linear display
      erase=. 4!:55@;:              NB. Erase objects
      setrl=. 9!:1                  NB. Set random link
      nc=. 4!:0@<                   NB. Name class: 0-3 is n a c v
      nc 'f'
3
      nc=. 4!:0@;:                  NB. ;: is word formation
      b=. b.                        NB. b. is an adverb
      r=. "                         NB. " is a conjunction
      nc 'x b r f'
0 1 2 3
   
      NB. =============================================
      NB. 4  TRAINS
      NB. g and h must be defined BEFORE e and f !!
      g=. +/      [.  h=. +/"1  NB. these 2 lines were reversed!
      e=. [: g h  [.  f=. g@h   NB. Rank of f is rank of h
   
      y=. i.3 4
      g h y
66
      h g y
66
      e y
66
      f y
6 22 38
   
      g@h b. 0         NB. Use adverb b. to show ranks
1 1 1
      p=. g@:h         NB. @: (AT)
      p b. 0           NB. With @: the ranks of p are unbounded
_ _ _
      p y
66
   
      abs=. | : [:        NB. Absolute value. Monad only
      res=. [: : |        NB. Residue. Dyad only
   
      NB. =============================================
      NB. 5  COMPOSITION BY FORKS AND CONJUNCTIONS
      mean=. +/ % #
      mean
Ŀ
Ŀ%#
+/  
ٳ  

      tree 'mean'
   /  +
 %      
   #      
      g h i j k     NB. Train with an odd number of verbs
Ŀ
ghĿ
  ijk
  ٳ

      f g h i j k   NB. Train with an even number of verbs
Ŀ
fĿ
 ghĿ
   ijk
   ٳ
 ٳ

      rss=. %:@(+/@*:)     NB. Root sum of squares
      %:@+.@*:             NB. Conjunctions: Left to right
Ŀ
Ŀ@*:
%:@+.   
ٳ   

      NB. [: caps a fork: the verb to its right is a monad
      rss=. [: %: +/@*:         NB. A fork
      rss
Ŀ
[:%:Ŀ
    Ŀ@*:
    +/   
    ٳ   
    ٳ

      rss 3 4
5
      rss=. [: %: [: +/ *:      NB. A train of 5 verbs
      rss=. +/&.*:              NB. Conjunction &. Under
      rss 3 4
5

      NB. 5.1  A TRUTH TABLE AS A FORK
      2 2 2 #: i. 2^3                 NB. #: as a dyad
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
      tt=. #&2 #: i.@(2&^)
      tt=. #&2 #: [: i. 2&^           NB. train of 5 verbs
      tt=. (] # 2:) #: [: i. 2: ^ ]   NB. train of 7 verbs
      tt
Ŀ
Ŀ#:Ŀ
]#2:  [:i.Ŀ
ٳ      2:^]
              ٳ
          ٳ

      s=. '(y. # 2) #: i. 2^y.'       NB. Character string
      e=. 3 : s                       NB. Explicit definition
      t=. 13 : s                      NB. Tacit definition
   
      linear 't'
(] # 2:) #: ([: i. 2: ^ ])
      tt=. #:@i.@(2&^)               NB. #: as a monad
      tt=. [: #: [: i. 2: ^ ]        NB. Train of 7 verbs
      tt 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
   
      NB. =============================================

      NB. 6  HOOKS
      NB. 6.1  BORDERING A TABLE
      h=. , mean       NB. Hook
      h y=. i.3 4      NB. Append column means
0 1  2  3
4 5  6  7
8 9 10 11
4 5  6  7
      h"1 y            NB. Append mean to rank-1 cells
0 1  2  3 1.5
4 5  6  7 5.5
8 9 10 11 9.5
   
      cols=. ,: +/&.>           NB. A hook with Under  &.
      rows=. ; ,.@:(+/"1)       NB. A hook with At     @:
      rows
Ŀ
;Ŀ
 ,.@:Ŀ
     Ŀ"1
     +/  
     ٳ  
     ٳ
 ٳ

      $ ,.@(+/"1)  y            NB. Using @
3 1 1
      $ ,.@:(+/"1) y            NB. Using @:
3 1
   
      rows=. ; [: ,. +/"1       NB. Train of 4 verbs

      rows                      NB. Hook with a fork tine
Ŀ
;Ŀ
 [:,.Ŀ
     Ŀ"1
     +/  
     ٳ  
     ٳ
 ٳ

      totals=. cols@rows
      totals y
Ŀ
0 1  2  3   6
4 5  6  7  22
8 9 10 11  38
Ĵ
12 15 18 2166


      NB. SELECT NON-NEGATIVE ITEMS:  Hook or fork?
      setrl 7^5

      ]y=. 10-~?10#20
_8 5 _1 0 _6 _10 3 3 8 _3
      h=. >:&0
      h=. >: 0:               NB. Alternative form
      (h y) # y
5 0 3 3 8
      y #~ h y                NB. Commute adverb
5 0 3 3 8
      select=. #~ h           NB. Hook
      select  y
5 0 3 3 8
      select=. (h # ])        NB. Fork
      select y
5 0 3 3 8
   
      NB. 6.2  A MARKOV CHAIN EXAMPLE
   y=.0 11 36 21 52;28  0  4  4  0;34  2  0 45 13;29  1 45  0  3;28 23  9 8  0
      ]m =. >y
 0 11 36 21 52
28  0  4  4  0
34  2  0 45 13
29  1 45  0  3
28 23  9  8  0
      diag=. (<0 1)&|:               NB. Diagonal
      ixd=. [: <"1 $ #: diag@i.@$    
      ix=. ixd@]                     NB. Indices of diagonal
      ix m
Ŀ
0 01 12 23 34 4

      amend=. [`ix`]}           NB. (Gerund - Adverb) is Noun
      n=. 1000 amend m
      g=. *:@(+/"1) % +/@,      NB. Fork
      5 ": (amend~ g) n         NB. Hook.  Format a table
  232   11   36   21   52
   28  199    4    4    0
   34    2  222   45   13
   29    1   45  215    3
   28   23    9    8  211
   
      NB. =============================================
      NB. 7.1 SURFACE AREAS AND VOLUMES OF SPHERES
      surface=. 4&*@o.@*:            NB. Conjunctions
      surface=. 4: * [: o. *:        NB. Train of 5 verbs
      surface 2 3 4 5
50.2655 113.097 201.062 314.159
      vol=. 4r3&*@o.@(^&3)           NB. 4r3 is a rational number
      vol=. 4r3"_ * [: o. ^&3        NB. Train of 5 verbs
      4r3"_  9             NB. 4r3"_ is a verb returning 4 over 3
1.333333333333333"_ 9
      vol 2 3 4 5
33.5103 113.097 268.083 523.599
   
      NB. 7.2 HERON'S FORMULA: AREA OF AN OBLIQUE TRIANGLE
      s=. -:@(+/)                    NB. s is half the sum
      s=. [: -: +/                   NB. Avoids parentheses
      h=. -~ s                  NB. Hook, commuting the arguments
      h=. s - ]                      NB. Fork
      g=. s , s - ]                  NB. Train of 5 verbs
      area=. %:@(*/@(s , s - ]))
      area=. [: %: [: */ s , s - ]   NB. Train of 9 verbs
      area 15 17 20
124.274
   
      NB. 7.3  ANGLES OF A TRIANGLE WHOSE SIDES ARE GIVEN
      rfd=. %&180@o.                 NB. Radians from degrees
      rfd=. o. % 180"_               NB. 180"_ is a verb
      dfr=. rfd^:_1                  NB. Degrees from radians
      arctan=. dfr@(_3&o.)           NB. Arctangent
      arctan=. [: dfr _3"_ o. ]      NB. Train of 5 verbs
   
      r=. %:@(numerator % denominator)
      erase 'n'                      NB. Avoid old definition
1
      r=. [: %: n % s                NB. Train of 5 verbs
      n=. */@(s - ])
      n=. [: */ s - ]                NB. Train of 5 verbs
      tanA2=. r % s - {.             NB. Train of 5 verbs
      A=. +:@arctan@tanA2            NB. Conjunctions
      A=. [: +: [: arctan tanA2      NB. Train of 5 verbs
      NB. Apply A to a cyclic rotation of y 
      A 0 1 2 |."0 1 y=. 15 17 20
46.9722 55.9442 77.0836
      f=. [: A 0 1 2&|."0 1
      NB. f y      would give a length error
   
      e=. 3 : 'A 0 1 2 |."0 1 y.'        NB. Explicit definition
      e y
46.9722 55.9442 77.0836
      g=. 13 : 'A 0 1 2 |."0 1 y.'       NB. Tacit definition
      linear 'g'
[: A 0 1 2"_ |."0 1 ]
      angles=. [: A 0 1 2"_ |."0 1 ]
      angles                       NB. Display show train of 5 verbs
Ŀ
[:AĿ
   ĿĿ]
   0 1 2"_|."0 1 
   ٳٳ 
   ٳ

      angles y
46.9722 55.9442 77.0836

   
      NB. 7.4  FIND SUBSTRINGS COMMON TO TWO STRINGS

      x=. 0 1 2  [ y=. 4 5 1 2
      equ=. =/ |.         NB. Equals table. Reversed for (/.) later
      x equ y
0 0 0 0
0 1 0 0
1 0 0 0
      ]b=. x [/.@equ y    NB. Illustrates Oblique adverb (/.) 
0 0 0
0 0 0
0 1 1
0 0 0
0 0 0
0 0 0
      b #&.>"1 x          NB. Rank-1 cells of b compress x
Ŀ
  
Ĵ
  
Ĵ
12
Ĵ
  
Ĵ
  
Ĵ
  

    
      ]m=. >:i.3 4
1  2  3  4
5  6  7  8
9 10 11 12
      ]/. m               NB. Illustrates Oblique adverb (/.)
 1  0  0
 2  5  0
 3  6  9
 4  7 10
 8 11  0
12  0  0
   
      x=. 1 2 3 [ y=. 2 3 5 4
      x equ y
0 0 0 0
0 0 0 1
0 0 1 0
      b=. x ]/.@equ y
      b #&.>"1 x
¿
  
Ŵ
  
Ŵ
  
Ŵ
  
Ŵ
12
Ŵ
  

      NB. Incorrectly reports finding substring 1 2
   
      equ=. (=/ |.) ,"1 #@[ # 0:  NB. Pad with zeros
      x equ y
0 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 1 0 0 0 0
   
      x=. 1 2 3 0 5 6
      y=. 1 2 8 0 5 6 3 0
      x (=/ |.) y              NB. Two substrings on one diagonal
0 0 0 0 0 0 0 1
0 0 0 0 0 0 1 0
0 1 0 0 0 0 0 0
1 0 0 0 1 0 0 0
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
      h=. [/.@equ #&.>"1 [     NB. Fork
      ]&.> i.2 3
Ŀ
012
Ĵ
345

      g=. ,@((<$0)&,.)         NB. Append a column of empty cells
      f=. <;._1                NB. Cut using empty cells as frets
      e=. -.&(<$0)             NB. Not empty
      x e@f@g@h y
Ŀ
ĿĿĿ
3012056
ٳٳٳ

      ss=. ;&.>@e@f@g@h        NB. Raze
      x ss y                   NB. Substrings in common
Ŀ
3 01 20 5 6

      y ss x
Ŀ
1 20 5 63 0

   
      NB. 7.5  FIND THE PIVOT OF A MATRIX
      utm=. [: , </~@i.@#            NB. Upper triangle mask
      ut=. utm # ,                   NB. Upper triangle
      ixp=. $ #: (i. >./)@|@ut { utm # ,@i.@$    NB. Indices of pivot
      pivot=. <@ixp { ]              NB. Signed value of the pivot
   
      perm=. 0 0&{ ; ] ; |. ; 1 1&{     NB. Permutations for amend
      ixa=. perm@ixp@{.@]               NB. Indices for amend
      imat=. {:@]                       NB. Amend the last item
      amend=. [`ixa`imat }              NB. (Gerund - Adverb) is verb
   
      setrl 7^5                         NB. Create a test matrix

      smatrix=. |: +/ .* ]              NB. See Section 7.6
      t=. smatrix 100%~ 50-~ ?6 6$100
      y=. _0.5 (0 0; 2 4; 4 2)} t
      6.2 ": y
 _0.50  0.12 _0.11 _0.09  0.11  0.18
  0.12  0.53  0.08 _0.11  0.08 _0.23
 _0.11  0.08  0.45 _0.15 _0.50  0.23
 _0.09 _0.11 _0.15  0.11 _0.12 _0.05
  0.11  0.08 _0.50 _0.12  0.59  0.40
  0.18 _0.23  0.23 _0.05  0.40  0.64
      pivot y
_0.5
      ixp y
2 4
      NB. Amend corresponding identity matrix
      96 97 98 99 amend y ,: =i.#y       
1 0  0 0  0 0
0 1  0 0  0 0
0 0 96 0 97 0
0 0  0 1  0 0
0 0 98 0 99 0
0 0  0 0  0 1
   
      utm y                 NB. Upper triangle mask
0 1 1 1 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
      6.2 ": ut y           NB. Upper triangle
  0.12 _0.11 _0.09  0.11  0.18  0.08 _0.11  0.08 _0.23 _0.15 _0.50  0.23 _0.12 _0.05  0.40
      ixa y,:=i.#y          NB. Indices for amend
Ŀ
2 22 44 24 4

      imat y ,: =i.#y       NB. Identity matrix (with laminate)
1 0 0 0 0 0
0 1 0 0 0 0
0 0 1 0 0 0
0 0 0 1 0 0
0 0 0 0 1 0
0 0 0 0 0 1
   
      NB. 7.6  DETERMINANTS AND THE AREA OF A TRIANGLE
   
      ip=. +/ . *           NB. Inner product (with conjunction)
      det=. -/ . *          NB. Determinant
      smatrix=. |: ip ]     NB. Symmetric matrix
      smatrix i.3 4
 80  92 104 116
 92 107 122 137
104 122 140 158
116 137 158 179
   
      a=.0.5 1.25 [ b=.4.4 2.45 [ c=.2.6 0.2   NB. 3 points
      (,"1 1:) t=. > a;b;c         NB. Hook
0.5 1.25 1
4.4 2.45 1
2.6  0.2 1
      area=. -:@det@(,"1 1:)
      area > a;b;c          NB. Signed areas
_3.3075
      area > a;c;b
3.3075
   
      NB. =============================================
      NB. 8  COMPOSITION OF ADVERBS
      a=. "_               NB. Adverb
      g=. [: i. 6:         NB. Constant function. Monad
      h=. (i.2 3) a        NB. Constant function. Dyad
      f=. g : h            NB. Constant function. Ambi-valent
      f 7
0 1 2 3 4 5
      8 f 10
0 1 2
3 4 5
      x=. 0 1 2 3
      (1*x^2) + (_1*x^1) + (_2*x^0)     NB. Polynomial
_2 _2 0 4
      ff=. #.&1 _1 _2 "0         NB. Tacit definition
      ff 2                       NB. 2 is a root
0
      D=. D.1                    NB. (c n) is adverb
      x - (ff x) % ff D x        NB. Newton-Raphson step
_2 3 2 2.2
      e=. 3 : 'y. - (ff y.) % (ff D y.)'    NB. Explicit
      t=. ] - ff % ff D                     NB. Tacit
      t t t x                        NB. 3 applications of t
_1.01176 2.01176 2 2.00005
      t^:3 x                         NB. t "to the power" 3
_1.01176 2.01176 2 2.00005
      t ^:_ x                        NB. Limit
_1 2 2 2
   
      NB. Now define an Adverb.  x. stands for the verb
      Newton=. 1 : '] - x. % x. D'   NB. Explicit
      t=. 11 : '] - x. % x. D'       NB. Tacit
      linear 't'
(([.+) ([. % ([. D))) (]`-`) \
      limit=. ^:_          NB. (c n) is adverb
      N=. Newton limit     NB. (a a) is adverb
      ff N _2 + i.6        NB. Try 6 different starting points
_1 _1 _1 2 2 2
      ff _1 2              NB. _1 and 2 are roots
0 0
   
      NB. Norman Thomson's functions f and g
      f=. [: <: *:@{. * {:
      g=. 2&o.@{. +  1&o.@{:
      h=. f , g              NB. A  fork
   
      y=. 5 0                NB. A starting point on the x-y plane
      y - (h y) (%.|:) h  D y            NB. (a) Immediate execution
4.66247 0.04
      e=. 3 : 'y. - (h y.)(%.|:)h D y.'  NB. (b) Explicit definition
      t=. ] - h (%.|:) h D               NB. (c) Tacit definition
      a=. 1 : '] - x. (%.|:) x. D'       NB. (d) Adverb: x. for verb
   
      y=.>5 0;1 1;1 4;0.5 4.5;0.5 4.8;1 5    NB. 6 starting points
      NB. Find the 4 points of intersection on Thomson's graph
      NB. h a limit    is   (v a a)   which is verb
      7.2": y,"1 h a limit ("1) y
   5.00   0.00   4.67   0.05
   1.00   1.00   1.86   0.29
   1.00   4.00   0.49   4.23
   0.50   4.50   0.49   4.23
   0.50   4.80   0.44   5.15
   1.00   5.00   0.44   5.15
   
      NB. =============================================
      NB. 9  COMPOSITION OF CONJUNCTIONS

      f=. 1&o.                         NB. Sine
      g=. ]                            NB. Returns its argument
      s=. 10 20                        NB. Dimension of the plot
      x=. -:i.8                        NB. Argument
      u0=. >./ - ]                     NB. Verb
      u=. u0@                          NB. Adverb
      (u0@f x) -: f u x                NB. These match
1
      v0=. ] - <./                     NB. Verb
      v=. v0@                          NB. Adverb
      z0=. (f u x) ,: (g v x)          NB. Scale smallest value to 0
      a=. z0 % >./"1 z0                NB. Immediate execution
      scale=. ] % >./"1                NB. Fork
      a -: scale (f u x) ,: (g v x)    NB. These match
1
   
      NB. Conjunction. Explicit definition
      N2=. 2 : 'scale@(x. u ,: y. v)@]'
      a -: f N2 g x                          NB. These match
1
      N=. scale@(([. u) ,: (]. v))@]         NB. Tacit definition
      N
Ŀ
Ŀ@]
scale@Ŀ  
      Ŀ,:Ŀ  
      [.u  ].v  
      ٳ  ٳ  
      ٳ  
ٳ  

      NB. Automatic translation from explicit to tacit definition
      N12=. 12 : 'scale@(x. u ,: y. v)@]'
   
      NB. Produce a graph
      round=. <.@+&0.5
      r=. [ #. [: |:@round <:@[ * ]
      ]b=. s r a
140 63 25 8 31 74 136 199
      h=. {&'.*'
      ]z=. h (i. s) e. b
........*...........
.....*.....*........
....................
...*..........*.....
....................
....................
................*...
*...................
....................
...................*
   
      NB. Conjunction.  noun arguments given by verbs [ and ]
      NB.   verb arguments are given by conjunctions [. and ].
      VS=. 12 : '[: h i.@[ e. [ r x. N y.'
      z-: s f VS g x       NB. These match.  f VERSUS g
1
   
      NB. A fuller plot is given by
      NB. 25 90 f VS g 4%~ i.60
   
      NB. =============================================
      NB. 10.1 HIDDEN LINE PROBLEM

      a=.0.5 1.25 [ b=.4.4 2.45 [ c=.2.6 0.2
    p=. > 3.5 5.1; 7 2.2; 1.3 3.6;3.3 2.9; 0.2 _0.5; 5.7 3.2;6 _1.1; 6 2.942
   
      NB. Combine a and b with each point in turn, and find the areas
      NB. of all triangles so formed
      A=. [: area"2 ,"2 1
      base=. [: +/&.*: -/
      NB. Double the area and divide by the length of the base
      d=. +:@] % base@[                NB. Distance to the line
   
      NB. Append the sign of the area and the distance
      NB. Classify by sorting output by the signed size of the
      NB. distance

      class=. \:@A { ] ,"1 *@A ,. [ d A
      NB. First note the conjunctions, and then count
      NB. the number of forks
      class
Ŀ
Ŀ{Ŀ
\:@A ]ĿĿ
ٳ  ,"1Ŀ,.Ŀ
          ٳ*@A  [dA
                 ٳ  ٳ
                 ٳ
         ٳ

      z=.(a,:b) class c,p,-:a+b
      5.2 9.3 3 9.4": z                NB. Format the table
 3.50    5.100  1   2.7975
 1.30    3.600  1   2.0108
 3.30    2.900  1   0.7536
 5.70    3.200  1   0.3345
 2.45    1.850  0   0.0000
 6.00    2.942 _1  _0.0003
 7.00    2.200 _1  _1.0036
 0.20   _0.500 _1  _1.5844
 2.60    0.200 _1  _1.6211
 6.00   _1.100 _1  _3.8636
   
      NB. 10.2  COMPLEX NUMBERS: EASY AND USEFUL
      2&* 1 2             NB. Double
2 4
      _1&* 1 2            NB. Reverse
_1 _2
      g=. ] * [: %: 2:
      g 1 2
1.41421 2.82843
      g g 1 2             NB. Applied twice, g doubles
2 4
      h=. ] * [: %: _1:
      h h 1 2             NB. Applied twice, h reverses
_1 _2
   
      p=. 1j2             NB. 1 on x-axis, 2 on y-axis
      2&* p               NB. Double
2j4
      _1&* p              NB. Reverse
_1j_2
      g p
1.41421j2.82843
      g g p               NB. Applied twice, g doubles
2j4
      h p                 NB. Rotate 90 counterclockwise
_2j1
      dfr _2 o. 1 2 ip _2 1    NB. 90 degrees between them
90
      rss 1 2,: _2 1      NB. Same length
2.23607 2.23607
      h h p               NB. Applied twice, h reverses
_1j_2
   
      1ad60               NB. Unit vector at angle of 60 degrees
0.5j0.8660254
      2ar1                NB. Unit vector at angle of 1 radian
1.0806j1.68294
      +. 1ad60            NB. Separate the two parts
0.5 0.8660254
      r. rfd 60           NB. Coordinates of unit vectors
0.5j0.8660254
      2 r. rfd 60         NB. Polar to Cartesian coordinates
1j1.73205
      +/&.*:@+. 3j4       NB. Pythagoras
5
   
      NB. 10.3  AREA OF A POLYGON
      a=. 3%:_1           NB. Reverse 1j0 to _1j0 in three steps
      +. a
0.5 0.8660254
      +. a^0
1 0
      +. a^3              NB. Three steps takes us half way round
_1 1.22461e_16
      ,. +. a^i.6         NB. Six steps completes the hexagon
   1           0
 0.5   0.8660254
_0.5   0.8660254
  _1 1.22461e_16
_0.5  _0.8660254
 0.5  _0.8660254
   
      poly=. (-: %: _1:) ^ i.     NB. The general case
      polygon=. ,.@+.@poly
      hex=. polygon 6         NB. Hexagon
      polygon 4               NB. Square
           1           0
 6.12303e_17           1
          _1 1.22461e_16
_1.83691e_16          _1
      polygon 5               NB. Pentagon
        1          0
 0.309017  0.9510565
_0.809017  0.5877853
_0.809017 _0.5877853
 0.309017 _0.9510565
      p=. 4 _3                NB. Arbitrary point
      hex , {. hex            NB. Cycle must be complete
   1           0
 0.5   0.8660254
_0.5   0.8660254
  _1 1.22461e_16
_0.5  _0.8660254
 0.5  _0.8660254
   1           0
      y=. 2 <\ (, {.) hex     NB. The 6 sides of the hexagon
      each=. &.>              NB. (Conjunction - Verb) is Adverb
      p&,each y               NB. Complete 6 triangles

      ...

      NB. Sum signed areas of the triangles for the polygon area
      +/ > area each p&,each 2 <\ (, {.) hex
2.59808
      +/ > area&.>   p&,&.>  2 <\ (, {.) hex
2.59808
   
      NB. Alternative using the rank conjunction
      +/area"2 (2) p&,"1 2\ (, {.) hex
2.59808
   
      s=. '+/ > area each p&,each 2 <\ (, {.) y.'
      e=. 3 : s            NB. Explicit definition
      e hex
2.59808
      T=. 13 : s           NB. Tacit definition
      linear 'T'
[: +/ [: > [: area&.> [: 4 _3&,&.> 2: <\ (, {.)
   
      NB. Build the triangles by composing functions
      h=. 2: <\ ] , {.
      p&,&.> h hex

      ...

      NB. To change p, it must be freed from the verb to which
      NB. it is bonded
      g=. [ ,&.> h@]      NB. p can be an argument to g
      p=. <4 _3.          NB. p must be boxed for agreement
      p g hex             NB. Boxed corners of the triangles

      ...

      NB. Open the boxes. Find the areas. Then sum the areas
      f=.  area &.>@g
      poly=. [: +/ >@f
      p poly hex
2.59808
   
      NB. Try a series of arbitrary points
      p=. <"1 i. 6 3 2
      $ h hex             NB. Number of 2 by 2 boxes
6
      $ p g hex           NB. Number of triangles
6 3
      p g hex             NB. This is the general case
Ŀ
  0         1     2         3     4         5   
  1         0     1         0     1         0   
0.5 0.8660254   0.5 0.8660254   0.5 0.8660254   
Ĵ
   6         7     8         9    10        11  
 0.5 0.8660254   0.5 0.8660254   0.5 0.8660254  
_0.5 0.8660254  _0.5 0.8660254  _0.5 0.8660254  
Ĵ
                      ...
Ĵ
 30         31   32         33   34         35  
0.5 _0.8660254  0.5 _0.8660254  0.5 _0.8660254  
  1          0    1          0    1          0  

      NB. We need the special case.
      p=. 6 3$ 4 _3; 0 0; 1 2
      p g hex
Ŀ
  4        _3     0         0     1         2   
  1         0     1         0     1         0   
0.5 0.8660254   0.5 0.8660254   0.5 0.8660254   
Ĵ
                       ...

Ĵ
   4         _3    0          0    1          2 
_0.5 _0.8660254 _0.5 _0.8660254 _0.5 _0.8660254 
 0.5 _0.8660254  0.5 _0.8660254  0.5 _0.8660254 
Ĵ
  4         _3    0          0    1          2  
0.5 _0.8660254  0.5 _0.8660254  0.5 _0.8660254  
  1          0    1          0    1          0  

      NB. Numbers in the middle column are the same because
      NB. all corners are equidistant from the origin
      area each p g hex
Ŀ
_0.54903810.4330127_0.5      
Ĵ
1.93301   0.4330127_0.5669873
Ĵ
2.91506   0.43301270.3660254 
Ĵ
1.41506   0.43301271.36603   
Ĵ
_1.06699  0.43301271.43301   
Ĵ
_2.04904  0.43301270.5       

      +/ > area each p g hex    NB. All points give same area
2.59808 2.59808 2.59808
   
      NB. Now compose a function
      poly=. [: +/ >@(area each@g)
      (>area) each@g            NB. Without parentheses!
Ŀ
Ŀ@g
Ŀ&.>  
>area     
ٳ     
ٳ  

      poly=. [: +/ [: > area&.>@g
      p poly hex
2.59808 2.59808 2.59808
      NB. Show that the area is unaffected by rotation
      rot=. 2 1&o. ,: _1 1"_ * 1 2&o.
      rotate=. rot@rfd    NB. Rotation matrix
   
      NB. Rotate and make an arbitrary translation
      setrl=. 9!:1
      setrl 7^5

      y=. 100 %~ ?10000
      2 3 +"1 hex +/ .* rotate y
2.97378  3.2275
2.28987 3.95707
1.31609 3.72957
1.02622  2.7725
1.71013 2.04293
2.68391 2.27043
      p poly hex      NB. All rotations give the same area
2.59808 2.59808 2.59808
   
      NB. 10.4 LINE OF INTERSECTION OF TWO PLANES
      NB.    x - 2y + 4z = 6
      NB.   2x +  y - 3z = 8
      m=. 1 _2 4 6,: 2 1 _3 8    NB. Define two planes
      n=. |: m                   NB. Transpose
   
      k=. 1 2, 2 0,: 0 1
      h=. det@(k&{"1 2)@}:
      h n                NB. Direction numbers
2 11 5
      (h n) % rss h n    NB. Normalize
0.1632993 0.8981462 0.4082483
      norm=. % rss       NB. Hook
      dc=. norm@h        NB. Direction cosines
   
      NB. Coordinates of a point on the line
      ct=. 0: ,~ {: %. |:@(2&{.)@}:
      ct n
4.4 _0.8 0
   
      (9!:1) 7^5              NB. Set random link

      ]t=. 0.1* 50-~?5#100    NB. Choose 5 random parameters
_3.7 2.5 _0.5 0.3 _2.9
   
      NB. Coordinates of 5 random points on the line of intersection
      ]z=. (ct n) +"1 (dc n) *"1 0 t
3.79579   _4.12314   _1.51052
4.80825    1.44537    1.02062
4.31835   _1.24907 _0.2041241
4.44899 _0.5305561  0.1224745
3.92643   _3.40462   _1.18392
      NB. Parametric equation of the line
      pequ=. ct@[ +"1 dc@[ *"1 0 ]
      z-: n pequ t                   NB. These match
1
      NB. Verify that these points satisfy the equations
      NB. of both planes
      (n pequ t) +/ .* }:n
6 8
6 8
6 8
6 8
6 8
   
      normal=. norm@}:"1     NB. Find the normals to the two planes
      normal m
0.2182179 _0.4364358  0.8728716
0.5345225  0.2672612 _0.8017837
   
      NB. On each plane choose 2 arbitrary points.
      NB. The line joining any 2 points on the plane is perpendicular
      NB. to the normal
      (8 1 0-2 0 1) +/ .* normal {.m
1.11022e_16
      (1 6 0- 5.5 0 1) +/ .* normal{:m
1.11022e_16
   
      NB. The perpendicular distances from the origin to each plane
      dist=. ({: % rss@}:)"1
      dist m
1.30931 2.13809
   
      NB. The points where the normals from the origin meet the
      NB. planes
      (dist * normal) {.m
0.2857143 _0.5714286 1.14286
      (dist * normal) {: m
1.14286 0.5714286 _1.71429
   
      NB. Proof that these points lie on their respective planes
      1 _2 4 +/ .* (dist * normal) {.m
6
      2 1 _3 +/ .* (dist * normal) {:m
8
   
      NB. Angle between the planes is the angle between their normals
      'ab'=. (normal {.m);normal {:m       NB. Define both a and b
      dfr _2 o. a +/ .* b
134.415
      dfr _2 o. a +/ .* -b
45.5847
   
      NB. The two normals define a plane perpendicular to the line of
      NB. intersection
      u=. 1&|.@[ * _1&|.@]
      v=. _1&|.@[ * 1&|.@]
      NB. Vector cross-product is line of intersection
      vcp=. u - v
      c=. a vcp b
      c +/ .* a,.b    NB. c is normal to a-b
0 0
   
      NB. Direction cosines of the line of intersection in two ways
      (dc n) -: norm c         NB. These match
1
   
      NB. 10.5  PROJECTION OF A LINE ONTO A GIVEN PLANE
      NB. vector p makes angles of 110 80 22 to the Cartesian axes
      ]p=. 2 o. rfd 110 80 22
_0.3420201 0.1736482 0.9271839
      NB. Project p onto the plane x - 2y + 4z = 6,
      NB. whose normal is 'a'
      NB. q is normal to the plane p-a
      q=. norm p vcp a
      NB. r is the normal to plane a-q. 
      NB. It is the required projection of p
      r=. norm a vcp q
      ]z=. norm a vcp norm p vcp a
_0.6419622 0.6094676 0.4652244
      NB. The required projection as a train of 5 verbs
      proj=. [: norm ] vcp norm@vcp
      z -: p proj a      NB. These match
1
   
      NB. 10.6 VOLUME OF A PARALLELEPIPED
      (;:'a b c'),: ;:'alpha beta gamma'
Ŀ
a    b   c    
Ĵ
alphabetagamma

      <"0 ch=. 6.11 10.673 5.95,: 97.583 107.167 77.55
Ŀ
6.11  10.673 5.95 
Ĵ
97.583107.16777.55

      sin=. 1&o.           NB. sine
      cos=. 2&o.           NB. cosine
      cosd=. cos@rfd       NB. cosine of angle in degrees
   
      erase 'h p q'        NB. Avoid earlier definitions
1 1 1
   
      volume=. */@{. * %:@h@cosd@{:
      NB. h is as yet undefined. 
      NB. It is assumed it will bedefined as a verb
      h=. p - q
      p=. >:@+:@(*/)       NB. 1+ twice the product
      q=. +/@*:            NB. Sum of squares
      volume ch
361.035
   
      NB. h is equivalent to the determinant given by g
      g=. (0 3 2, 3 0 1,: 2 1 0)"_ { 1&,
      g                    NB. A fork
Ŀ
Ŀ{Ŀ
0 3 2"_ 1&,
3 0 1   ٳ
2 1 0          
ٳ        

      h=. det@g
      volume=. */@{. * %:@h@cosd@{:
      volume ch
361.035
   
      NB. Axial lengths or (depending on context) interacial angles
      a=. 0&{ [.  b=. 1&{ [.  c=. 2&{
      axisa=. sin@b , 0: , cos@b
   
      NB. Alternative forms
      CosRho=. (cos@c - */@cos@(a,b)) % sin@b
      CosRho=. (cos@c - [: */@cos a,b) % sin@b
      CosRho=. sin@b %~ cos@c - [: /@cos a,b
      CosRho=. sin@b %~ cos@c - cos@a * cos@b
      NB. "cos(rho) is sin b divided into
      NB.                  {cos c - (cosa times cos b)}"
   
      CosSigma=. sin@b %~ (>:@+:@(*/@cos) - +/@*:@cos)
      CosSigma=. sin@b %~ [: %: >:@+:@(*/@cos) - +/@*:@cos
      NB. "cos (sigma) is sin b divided into the square root of
      NB. {1+ twice the product of the cosines) - the sum of
      NB. the squares of the cosines}"
   
      axisb=. CosRho,CosSigma,cos@a
      dm=. ,&0 0 1 @(axisa,:axisb)@rfd@b
      dm=. 0 0 1"_ ,~ (axisa,:axisb)@rfd@b
      dmat=. ({. *"0 1 dm)"2     NB. The direct lattice
      ]d=. dmat ch               NB. The "dmatrix"
5.83779      0 _1.80341
1.97316 10.394 _1.40843
      0      0     5.95
   
      det d           NB. Volume of the cell
361.035
      NB. Bordering the dmatrix gives a signed volume
      det (,"1 1:) d,0
361.035
      det (,"1 1:) 0,d
_361.035
      e=. 1 _2 3 +"1 d,0    NB. After an arbitrary translation
      NB. the matrix must be bordered in order to get the volume
      det (,"1 1:) e
361.035
   
      NB. The transpose of the inverse is the inverse of the transpose
      cl=. * 1e_15&<@|                 NB. Clean very small values
      (cl |:@%. d)  -:  cl %.@|: d     NB. These match
1
      ch=. chalcanthite=. 6.11 10.673 5.95,: 97.583 107.167 77.55
      or=. orthoclase=. 8.562 12.996 7.193,: 90 116.01 90
      an=. anorthite=. 8.177 12.877 14.169,: 93.17 115.85 81.22
      ax=. axinite=. 7.15 12.57 13.05,: 91.383 75.5 93.383
      ky=. kyanite=. 7.12 7.85 5.57,: 89.983 101.117 106
      $minerals=. ch,or,an,ax,:ky
5 2 3
      $x=. dmat minerals
5 3 3
      dmat b. 0       NB. dmat is monadic and has rank 2
2 2 2
      %. b. 0         NB. monadic matrix-inverse has rank 2
2 _ 2
      |: b. 0         NB. monadic transpose has unbounded rank
_ 1 _
      NB. The reciprocal lattice is defined by the transpose of the
      NB. inverse of dmat
      rmat=. |:@%.
      |:@%. b. 0
2 _ 2
      %.@|: b. 0
_ 1 _
      $ rmat x
5 3 3
      NB. Atempted execution of the following give length errors
      NB. %. |: x
      NB. %.@|: x



1



