NB. Mostly J NB. Keith Smillie NB. Department of Computing Science NB. University of Alberta NB. Edmonton, Alberta T6G 2H1 NB. January 2000 NB. Utilities by=: ' '&;@,.@[,.] NB. By (format) each=: &.> NB. Boxed each Each=: &> NB. Open each ei=: i. @ >: NB. Extended integers io=: [:<:[:+/[: @ i. NB. Positive integers sort=: /:~ NB. Sort table=: 1 : 0 NB. Table adverb [by]over[x./] ) NB. Utilities for Pictures Figure=: 3 : 0 'auto' Figure y. : file=. 'c:\j401\temp\',x.,'.bmp' ((255,:0 0 0);y.) writebmp8 file viewbmp file ) writebmp8=: 3 : 0 : 'spal sbmp'=.$@>"0 x. xsbmp=.sbmp+(i.2)*4|-sbmp h=.524289 0,(*/xsbmp),0 0,2#spal=.0{spal h=.(54+(4*spal)+*/xsbmp),0,(54+4*spal),40,(|.sbmp),h head=. 'BM',,a.{~,|."1 (4#256)#:h pal=. ,(0,"1~|."1 >{.x.){a. bmp=. ,|.(xsbmp{.>{:x.){a. (head,pal,bmp) 1!:2 am=: +/ % # NB. *** Part Three. Coupons *** NB. Coupon calculations cc=: * +/ @: % @: >: @ i. cctable=.(".@(6&":)@cc@+)table rows=: 5 * i. 10 cols=: >:i. 5 NB. More coupon calculations ccsim=: 3 : 0 n=. y. Sample=. i. 0 while. n > # ~. Sample do. Sample=. Sample, ?n end. >:Sample ) ccsize=: (#@ccsim)"0 @ # fr=: +/"1 @ (=/) cfr=: i.@(<:@$@[) fr io cfrtab=: midpts@[,.cfr tab1=. 10+5*i.14 tab2=. 0 9 16 19 30 7 7 6 3 1 1 0 0 1 tab=: tab1,.tab2 NB. *** Part Four. Numbers *** NB. "Numbers Without End" np=: p:^:_1 ratio=: %np NB. Really big numbers GoldenSphere=: 3 : 0 N=. y. Gold=. 437.09 NB. Can$/Troy ounce G=. 0.698*12*Gold NB.Can$/cu. in. G=. G*(5280*12)^3 NB.Can$/cu. mi. M3=. G %~ 1.05^6500 NB. Cubic miles of gold R=. (3*M3%12.5664)^%3 NB. Radius in miles R=. R%5.878e12 NB. Radius in light years ) mpy=: +/@$~ power=: */@$~ f=: ^/@$~ g=: f/@$~ NB. *** Part Five. A Miscellany of Examples *** NB. An orderly arrangement of data T1=: 62.3 58.5 44.6 50.3 63.4 50.4 45 46.7 64.5 46.1 62.6 50.3 T1=: 3 4 $ T1 T2=: 75.4 65.6 54 52.7 70.3 67.3 57.6 58.5 68.8 65.3 45.6 51 T2=: 3 4 $ T2 T=: T1,:T2 NB. Patterns (Drafts and cloth diagram only) both=: +./ . *. get=: >@{ weave=: (2&get both |: @ (1&get)) both 0&get diagram=: ((weave);2&get),: (0&get);1&get cdis=: = @ (<"1 @ |:) H0a=: 4 3 2 1 =/ 4 1 2 3 4 3 2 1 4 NB. Upper I0a=: |:> 1 0 0 1;1 1 0 0;0 1 1 0;0 0 1 1 NB. left R0a=: |: 1 2 3 4 =/ 1 2 3 2 1 4 3 2 1 4 NB. corner W00a=:1 1 0 0 1 0 0 1 1;1 0 0 1 1 1 0 0 1 W00a=: >W00a,0 0 1 1 0 1 1 0 0;1 0 0 1 1 1 0 0 1 NB. Pictures WRAP=: wrap : (wrap@]^:[) NB. From "Phrases" wrap=: RL@(,.(next+i.@#))^:4 RL=: |.@|: next=: >:@(>./)@, Prime=: 1: = #@q: PrimeSpiral=: Prime @ WRAP NB. CoffeeTable=: |: }. }."1 (8 PrimeSpiral |. >:i. 28) NB. CoffeeTable=: 1.0":16 43 $ 2 (335) } ,CT perext=: {:,],{. NB. From "Fractals ..." auto=: {~ 3&(#.\)@perext v74=: 0 1 0 0 1 0 1 0 NB. v74&auto^:(i.64) ?64$2 NB. Taking chances NB. load 'plot' CoinTossing=: 3 : 0 NB. Try CoinTossing 300 N=. y. NB. and plot Heads=. +/\ ? N $ 2 NB. Tosses vs Prop Tosses=: >: i. N NB. and Prop=: Heads % Tosses NB. Tosses vs Dev empty Dev=: | Tosses - 2*Heads ) NB. Die faces NB. Faces=: 1 2 3 4 5 6 NB. Total number of combinations NB. *: #Faces NB. Combinations of faces NB. {Faces;Faces NB. Sums for two rolls NB. + table Faces NB. Grouped sums NB. :?2 1000$6 NB. Expected frequencies (rounded) NB. ". 5.0": 1000*Prob NB.*** Part Six. Another Look at J *** NB. Notation pos=: >: @ i. D=: 4 1 4 1 1 2 4 4 2 2 DD=: >D;3 3 2 1 3 2 3 3 2 2;3 3 3 3 2 4 2 3 2 1 nubfrtab=: ~. ,. +/"1 @ = NB. About verbs dev=: - am ccexp=: * +/ @: % @ pos f1=: 3 : 0 % y. ) f2=: 3 : 0 : x. % y. ) f3=: 3 : 0 % y. : x. % y. ) ccexp1=: 3 : 0 n=. y. i=. 1 sum=. 0 while. i <: n do. sum=. sum + %i i=. >: i end. n * sum ) ccsize1=: 3 : 0 : Reps=. x. n=. y. SampleSize=. i. 0 while. Reps > # SampleSize do. SampleSize=. SampleSize, ccsim n end. ) ss1=: +/ @: *: @ dev ss2=: [: +/ [: *: dev ss3=: [: +/ [: *: [ - +/ % # NB. Going in circles ELSE=: ` IF=: @. RIGHT=: ] factorial=: (RIGHT*factorial@(RIGHT-1:)) ELSE 1: IF (RIGHT=0:) Hanoi=: ((Hanoi@(RIGHT-1:)) , RIGHT , (Hanoi@(RIGHT-1:))) ELSE 1: IF (RIGHT=1:) fact=: 1:`(]*fact@<:) @. (]>0:) Hanoi1=: 1:`((Hanoi1@(]-1:)) , ] , (Hanoi1@(]-1:))) @.(]>1:) rsum=: ({:@RIGHT + rsum@}:@RIGHT) ELSE 0: IF (0: = #@RIGHT) rsum1=. 0:`({:+rsum1@}:) @. (#>0:) NB. Watching television Times=: 9 0 8; 9 2 19; 9 5 0; 9 13 27; 9 16 32; 9 26 41 Times=: Times, 9 29 25; 9 30 0 tosecs=: 24 60 60&#. TVcomm=: 3 : 0 0 TVcomm y. : Switch=. x. Times=. y. t=: diff tosecs Each Times 'tp tc'=. Switch|.(2|i.#t): @ i. cc=: * +/ @: % @: pos Coupons1=: 0 : 0 pc coupons1; xywh 5 8 75 52;cc input groupbox;cn " Input "; xywh 10 20 30 10;cc coupons edit; xywh 45 20 30 10;cc cname static;cn "Coupons"; xywh 10 40 30 10;cc price edit; xywh 45 40 30 10;cc pname static;cn "Unit price"; xywh 5 70 75 50;cc output groupbox;cn " Output "; xywh 10 82 30 10;cc exp edit; xywh 45 82 25 10;cc expname static;cn "Av. no."; xywh 10 102 30 10;cc total edit; xywh 45 102 30 10;cc totalname static;cn "Cost"; xywh 90 11 34 14;cc ok button;cn "OK"; xywh 90 31 34 14;cc reset button;cn "Reset"; xywh 90 51 34 14;cc cancel button;cn "Cancel"; xywh 90 80 34 34;cc jicon button bs_ownerdraw;cn "system\examples\data\jr.ico"; pas 10 10;pcenter; rem form end; ) Reset1=: 3 : 0 wd 'set coupons *', ' 0 ' wd 'set price *', ' 0 ' wd 'set exp *', ' ' wd 'set total *', ' ' ) coupons1_run=: 3 : 0 wd Coupons1 Reset1 '' wd 'setfont coupons "MS Sans Serif" 12 bold;' wd 'setfont price "MS Sans Serif" 12 bold;' wd 'setfont exp "MS Sans Serif" 12 bold;' wd 'setfont total "MS Sans Serif" 12 bold;' wd 'pshow;' ) coupons1_ok_button=: 3 : 0 ('n p')=: ". coupons,' ', price wd 'set exp *',7.1&":cc n wd 'set total *',8.2&":p * cc n ) coupons1_reset_button=: 3 : 0 Reset1 '' ) coupons1_cancel_button=: 3 : 0 wd 'pclose;' ) NB. ***** First coupon collector's example - End ***** NB. ***** Easter calculation - Beginning ***** div=: [: <. % rem=: |~ Easter=: 3 : 0 year=. y. a=. year rem 19 b=. year div 100 c=. year rem 100 d=. b div 4 e=. b rem 4 g=. (13 + 8*b) div 25 h=. ((19*a) + b + 15 - d + g) rem 30 m=. (a + 11*h) div 319 i=. c div 4 k=. c rem 4 l=. (32 + (2*e+i) + m - h + k) rem 7 n=. (h + l + 90 - m) div 25 p=. (h + l + n + 19 - m) rem 32 n,p ) EASTER=: 0 : 0 pc easter; xywh 10 10 40 10;cc year edit; xywh 55 10 30 10;cc yname static;cn "Year"; xywh 10 30 40 10;cc date edit; xywh 55 30 30 10;cc cdate static;cn "Date"; xywh 80 10 34 12;cc ok button;cn "OK"; xywh 80 30 34 12;cc cancel button;cn "Cancel"; pas 6 6;pcenter; rem form end; ) easter_run=: 3 : 0 wd EASTER wd 'set year *', ' ' wd 'set date *', ' ' wd 'setfont year "MS Sans Serif" 11 bold;' wd 'setfont date "MS Sans Serif" 11 bold;' wd 'pshow;' ) easter_ok_button=: 3 : 0 y=. ". year if. (y <: 1582) +. (0 ~: y rem 1) do. r=. 'Invalid date' else. 'm d'=. Easter y r=.(>(m-3){'March ';'April '),":d end. wd 'set date *',r ) easter_close=: 3 : 0 wd'pclose' ) easter_cancel_button=: 3 : 0 easter_close'' ) NB. ***** Easter calculation - Beginning ***** NB. ***** Second coupon collector's example - Beginning ***** each=: &.> am=: +/ % # pos=: >: @ i. fr=: +/"1 @ (=/) frtab=: [,.fr nubfr=: +/"1 @ = nubtab=: ~. ,. nubfr cc=: * +/ @: % @: pos sort=: /:~ ccsim=: 3 : 0 n=. y. r=. i. 0 while. n > # ~. r do. r=. r, ?n end. >:r ) ccsize=: (#@ccsim)"0 @ # WDtable=: 3 : 0 NB. Display table : ;(<"1 (x. ": y.)), each LF ) Coupons2=: 0 : 0 pc coupons2; xywh 5 8 75 72;cc input groupbox;cn " Input "; xywh 10 20 30 10;cc coupons edit; xywh 45 20 30 10;cc cname static;cn "Coupons"; xywh 10 40 30 10;cc price edit; xywh 45 40 30 10;cc pname static;cn "Unit price"; xywh 10 60 30 10;cc sim edit; xywh 45 60 30 10;cc simname static;cn "Trials"; xywh 5 90 75 52;cc output1 groupbox;cn " Expectation "; xywh 10 102 30 10;cc expnum edit; xywh 45 102 25 10;cc expnumname static;cn "Exp. no."; xywh 10 122 30 10;cc expcost edit; xywh 45 122 30 10;cc expcostname static;cn "Exp. cost"; xywh 10 164 30 10;cc simave edit; xywh 45 164 30 10;cc simnumname static;cn "Sim. ave."; xywh 5 152 75 92;cc output2 groupbox;cn "Simulation "; xywh 10 184 30 10;cc simcost edit; xywh 45 184 30 10;cc simcostname static;cn "Sim. cost"; xywh 10 204 30 10;cc min edit; xywh 45 204 30 10;cc minname static;cn "Minimum"; xywh 10 224 30 10;cc max edit; xywh 45 224 30 10;cc maxname static;cn "Maximum"; xywh 102 10 50 75;cc frtable editm ws_border ws_vscroll; xywh 90 90 75 28;cc frame groupbox;cn "Frequencies"; xywh 100 98 30 14;cc rb0 radiobutton;cn "Range"; xywh 133 98 30 14;cc rb1 radiobutton group;cn "Nub"; xywh 110 125 34 14;cc ok button;cn "OK"; xywh 110 145 34 14;cc reset button;cn "Reset"; xywh 110 165 34 14;cc cancel button;cn "Cancel"; xywh 110 194 34 34;cc jicon button bs_ownerdraw;cn "system\examples\data\jr.ico"; pas 10 10;pcenter; rem form end; ) Reset2=: 3 : 0 wd 'set coupons *', ' 0 ' wd 'set price *', ' 0 ' wd 'set sim *',' 0 ' wd 'set expnum *', '' wd 'set expcost *', '' wd 'set simave *', '' wd 'set simcost *', '' wd 'set min *', '' wd 'set max *', '' wd 'set frtable *','' ) coupons2_run=: 3 : 0 wd Coupons2 Reset2 '' wd 'setfont coupons "MS Sans Serif" 12 bold;' wd 'setfont price "MS Sans Serif" 12 bold;' wd 'setfont sim "MS Sans Serif" 12 bold;' wd 'setfont expnum "MS Sans Serif" 12 bold;' wd 'setfont expcost "MS Sans Serif" 12 bold;' wd 'setfont simave "MS Sans Serif" 12 bold;' wd 'setfont simcost "MS Sans Serif" 12 bold;' wd 'setfont min "MS Sans Serif" 12 bold;' wd 'setfont max "MS Sans Serif" 12 bold;' wd 'setfont frtable "MS Sans Serif" 12 bold;' wd 'pshow;' ) coupons2_ok_button=: 3 : 0 ('n p r')=: ". coupons,' ',price,' ',sim wd 'set expnum *',7.1&":cc n wd 'set expcost *',8.2&":p * cc n if. r > 0 do. s=. r ccsize n wd 'set simave *',6.1&":am s wd 'set simcost *', 8.2&":p * am s wd 'set min *',6.0&":<./s wd 'set max *',6.0&":>./s if. rb1 = '1' do. CCtable=: nubtab sort s else. range=: 0, pos >./s CCtable=: range frtab s end. wd 'set frtable *',6.0 WDtable CCtable end. ) coupons2_reset_button=: 3 : 0 Reset2 '' ) coupons2_cancel_button=: 3 : 0 wd 'pclose;' ) NB. ***** Second coupon collector's example - End ***** NB. ***** TV commercials example example - Beginning ***** each=: &.> Each=: &> tosecs=: 24 60 60&#. secs=: 100&| mins=: 100&| @ <. @ %&100 hrs=: 10000&| @ <. @ %&10000 unpack=: hrs,mins,secs tosecs=: 24 60 60&#. diff=: 2: (-~/\) ] TVcomm=: 3 : 0 0 TVcomm y. : Switch=. x. Times=. y. t=: diff tosecs Each Times 'tp tc'=. Switch|.(2|i.#t)