NB. functions giving complex generalizations of the 3x+1 function and
NB. utilities for visualizing various stopping times

load '~addons/graphics/fvj3/raster6.ijs'
load 'misc files'

mod2a=: *:@(1&o.)@(1r2p1&*)   NB. sin^2
mod2b=: 2r1&|                 NB. Re & Im
mod2c=: |@(2r1&|&.>:)         NB. sawtooth
mod2D=: 1 : 'm&*@(1&o.)@(1p1&*) + *:@(1&o.)@(1r2p1&*)'
mod2e=: -:@(1:-_12&o.@o.)
mod2f=: |@(2r1&|&.>:)&.+.   NB. sawtooth
mod2g=: mod2a&.+.             NB. sin^2 on Re & Im parts

T1=: 1 : '-:@((]+[* 3: ^ ]) u) f.'
T2=: 1 : '-:@((]+[* 3: ^ |@]) u) f.'
T3=: 1 : '-:@(u+]* 3: ^ u@|) f.'
T11x=: 1 : '-:@((]+[* 1: ^ ]) u) f.'
T15x=: 1 : '-:@((]+[* 5: ^ ]) u) f.'

TX=: 1 : '-:@((]+[* m ^ ]) mod2a) f.'
TXe=: 1 : '-:@((]+[* m ^ ]) mod2e) f.'

c1x=: T0=: 0.25 * >:@(2&*) - 2&o.@o.
c3x=: + 0.25 * 1: - 2&o.@o. * >:@+:
c5x=: 0.25 * >:@(6&*) - 2&o.@o. * >:@(4&*)

CX=: 1 : '0.25 * >:@((m+1)&*) - 2&o.@o. * >:@((m-1)&*)'

SIG=:1 : 0     NB. fct arg is "T"
sigs=.0 1&+@}: , u@{:
sigl=.sigs^:((1&{ < 255"_)*. {. <:&| {:)^:_
(((~:>:)@{:*1&{)@sigl@(],0:,]) f. :: 0:)"0
)

TOT=:1 : 0     NB. fct arg is "T"
sigs=.>:@{. , u@{:
sigl=.sigs^:(({. < 255"_)*. 1: ~: {:)^:_
(((~:>:)@{:*{.)@sigl@(0:,]) f. :: 0:)"0
)

BAS=:1 : 0     NB. fct arg is "T"
df=.(|**)&.+.   NB. defuzz
toi=. ((~:>:)@u *. <&1e30@|) { _:,]                  NB. big to infinity
sigs=.>:@{. ,}.,toi@u@{:
sigl=.sigs^:(({. < 255"_)*. {: -.@e. }.@}:)^:_
(( 255&=@{.{{.@:(/:~)@:df@:((]i.{:)}.])@:}.,__"_)@sigl@(0:,]) f. :: _:)"0
)

relBAS=:2 : 0     NB. fct arg is "T"
df=.(|**)&.+.         NB. defuzz
toi=. ((~:>:) *. <&1e10@|) { _:,]                  NB. big to infinity
sigs=.>:@{. ,}.,df@toi@u@{:
sigl=.sigs^:(({. < 255"_)*. {: -.@(e.!.1e_12) (__,_,n)"_)^:_
NB. ((256&*@{. +(__ _,n.)"_ i. (255&=)@{.{{:,__"_)@sigl@(0:,]) f. :: 1:)"0
((256&*@{. +(__ _,n)"_ i.!.1e_12 (255&=)@{.{{:,__"_)@sigl@(0:,]) f. :: 1:)"0
)

LAM1=: 1 : 0    NB. fct arg is "mod2"
lams=: ({.+u@(u T1)@{:),>:@(1&{),(u T1)@{:
laml=: lams^:((1&{ < 255"_)*. >:/@:(3 2&^)@:|@}:)^:_
(((~:>:)@{:*1&{)@laml@(|@u,1:,]) f. :: 0:)"0
)

LAM2=: 1 : 0    NB. fct arg is "mod2"
lams=: ({.+|@u@(u T2)@{:),>:@(1&{),(u T2)@{:
laml=: lams^:((1&{ < 255"_)*. >:/@:(3 2&^)@}:)^:_
(((~:>:)@{:*1&{)@laml@(|@u,1:,]) f. :: 0:)"0
)

LAM3=: 1 : 0    NB. fct arg is "mod2"
lams=: ({.+u@|@(u T3)@{:),>:@(1&{),(u T3)@{:
laml=: lams^:((1&{ < 255"_)*. >:/@:(3 2&^)@}:)^:_
(((~:>:)@{:*1&{)@laml@(|@u,1:,]) f. :: 0:)"0
)

ESC=:1 : 0     NB. fct arg is "T"
sigs=.(>:@{. , u@{:) :: (>:@{. , _:)
sigl=.sigs^:(({. < 255"_)*. ( |@{: < 1e10"_)*.(~:>:)@{:)^:_
({.@sigl@(0:,]) f. )"0
)

$pal3x=:255,0,~254{.<.,/ ( >:-:(i.%-)22) */(Hue (i.%])12)
$pal3y=:0,255,~254{.<.,/ ( >:-:(i.%-)22) */(Hue (i.%])12)

zl_clur=: 4 : 0
w=.-~/9 o.y
h=.-~/11 o.y
(h*(i:%j.)<.0.5+x*h%w) +/ ({.y)+w*(i.%<:)1+x
)

zl_cccr=: 4 : 0
w=.--/y
({.y)+w*((i:%j.) +/ (i:%])) <.-:x
)

mk_strip=:1 : 0
2000 u mk_strip y
:
for_k. Stripe_starts do.
  b=. u x zl_clur k+Stripe_offset
  (pal3x;b) write_image y,(":k),'.bmp'
end.
if. 1<#Stripe_starts do.
  for_k. Stripe_starts do.
    'p b'=. readbmp8 y,(":k),'.bmp'
    try. B=.B,.b catch. B=.b end.
  end.
(pal3x;B) write_image y,'.bmp'
end.
)

mk_bas_strip=:1 : 0   NB. takes T BAS as arg
2000 u mk_bas_strip y
:
for_k. Stripe_starts do.
  xx=. u x zl_clur k+Stripe_offset
  $o=.\:#/.!.1e_11~bas=:(,xx)-. _ __
  $bas=:o{~.!.1e_11 bas
  $pal=.0,255,((10#1),0>._10+254<.#bas)#Hue 5r6*(i.%<:)11
  (pal;255<.(__ _,bas) i.!.1e_11 xx) write_image y,(":k),'.bmp'
  (scriptform 'bas') fwrite y,(":k),'.basin'
end.
)

mk_relbas_strip=:2 : 0   NB. takes T as arg
2000 u mk_bas_strip y
:
for_k. Stripe_starts do.
  xx=. u relBAS n x zl_clur k+Stripe_offset
  b=.256|xx
  xx=.<.xx%256   NB. steps
  ncc=.<.254%#n
  pal=.0,255,<.,/(ncc$1 0.83 0.65) */(#n){.!.160 Hue }:,/0 1r12 +/ 5r6*(i.%<:)6
  (pal;(b*b<2)+(b>:2)*b+(#n)*ncc|xx-1) write_image y,(":k),'.bmp'
end.
)

mk_zoom=:2 : 0
1000 u mk_zoom n y
:
for_k. Zoom_lev do.
  b=. u x zl_cccr n+0 1*1.5*10^-k
  (pal3x;b) write_image y,(":k),'.bmp'
end.
)

mk_bas_zoom=:2 : 0
1000 u mk_bas_zoom n y
:
for_k. Zoom_lev do.
  xx=. u x zl_cccr n+0 1*1.5*10^-k
  $o=.\:#/.!.1e_11~bas=:(,xx)-. _ __
  $bas=:o{~.!.1e_11 bas
  $pal=.0,255,((10#1),0>._10+254<.#bas)#Hue 5r6*(i.%<:)11
  (pal;255<.(__ _,bas) i.!.1e_11 xx) write_image y,(":k),'.bmp'
  (scriptform 'bas') fwrite y,(":k),'.basin'
end.
)

mk_relbas_zoom=:2 : 0  NB. takes T as left arg, zoom spot & bas as right
1000 u mk_bas_zoom n y
:
'n nn'=.n
for_k. Zoom_lev do.
  xx=. u relBAS nn x zl_cccr n+0 1*1.5*10^-k
  b=.256|xx
  xx=.<.xx%256   NB. steps
  ncc=.<.254%#nn
  pal=.0,255,<.,/(ncc$1 0.83 0.65) */(#nn){.!.160 Hue }:,/0 1r12 +/ 5r6*(i.%<:)6
  (pal;(b*b<2)+(b>:2)*b+(#nn)*ncc|xx-1) write_image y,(":k),'.bmp'
end.
)

mk_cx_seq=:4 : 0
for_k. i.#x do.
  b=. (k{x) CX SIG  600 zl_clur _6+0 12j2
  (pal3x;b) write_image y,(fmt k),'.bmp'
end.
)
mk_tx_seq=:4 : 0
for_k. i.#x do.
  b=. (k{x) TX SIG  600 zl_clur _6+0 12j2
  (pal3x;b) write_image y,(fmt k),'.bmp'
end.
)
mk_tx_esc_seq=:4 : 0
for_k. i.#x do.
  b=. (k{x) TX ESC  1200 zl_clur _6+0 12j2
  (pal3x;b) write_image y,(fmt k),'.bmp'
end.
)

mk_towinding_esc_seq=:4 : 0
for_k. i.#x do.
  b=. (k{x) mod2D T1 ESC  1200 zl_clur _6+0 12j2
  (pal3x;b) write_image y,(fmt k),'.bmp'
end.
)
mk_towinding_esc_seq=:4 : 0
for_k. i.#x do.
  b=. (k{x) mod2D T1 ESC  800 zl_clur _6+0 12j2
  (pal3x;b) write_image y,(fmt k),'.bmp'
end.
)

NB.   (j. (% +:@(>./)) -. 0.3^(i.%<:) 81 ) mk_towinding_esc_seq 'd:\v3x+1\moravian\wind_esc_a'

fmt=:3&$: : ([:":[:{&'0123456789'([#10"_)#: ])

Stripe_starts=:_12+10*i.7
Stripe_starts=: _6+12*i.1
Stripe_offset=: 0 10j2.4
Stripe_offset=: 0 12j2

Zoom_lev=:i.7
Zoom_lev=:i.2

rbp_c3x=: 0 1.1925319070463933 1 _1 _1.2777337661717139 _10 _10.034865163615093 _136.00248290381123
rbp_t3x=: 0 0                  1 _1 _1.153874079260693  _10 _10.015715136400743
#rbp_mod2e=: 0 1 1.5257687384548078j0.7212762664152228 _10 3.1020691227222175j0.71384345896992041 _1

test=: 0 : 0
mod2a 1 2 3 4 3.1
mod2b 1 2 3 4 3.1
mod2c 1 2 3 4 3.1
1 mod2D 1 2 3 4 3.1
0.1 mod2D 1 2 3 4 3.1
mod2e 1 2 3 4 3.1
mod2f 1 2 3 4 3.1
mod2g 1 2 3 4 3.1

mod2a T1^:(i.7) 7
mod2e T1^:(i.7) 7.01

mod2a T1 SIG 7 7.1 27
mod2e T1 SIG 7 7.1 27

3 TX 7 8
5 TX 7 8

]z=: 4 zl_clur 0 2j1
mod2a T1 SIG z
mod2e T1 SIG z
spix=:[ #"_1 #
$e=:mod2e T1 SIG 100 zl_clur 6 8j1
view_image pal3x;4 spix e

$e=:mod2e T1 LAM1 100 zl_clur 6 8j1
view_image pal3x;4 spix e

mod2e T1 ESC z
mod2e T1 ESC 10 zl_clur 6 8j1
$e=:mod2e T1 ESC 100 zl_clur 6 8j1
view_image pal3x;4 spix e
)