# Variable calculations should be done in this file.
# It is read and interpreted by wims for all valid calls to the module.

!default fluidity=4
!default speed=3
!default size=2
!default range=5
!default yrange=30
!default t0=-1
!default t1=1

fluiditycnt=7
speedcnt=5
sizecnt=5
ins_anim_frames=!word $fluidity of 15 22 30 40 55 75 100
duration=!word $speed of 10 7 5 3 2
ins_anim_delay=!eval $duration/$ins_anim_frames
size_base=!eval 0.1*$size+0.2

pari_precision=5
wims_rawmath_variables=t
poly=!trim $poly
poly=!lowercase $poly
poly=!translate , to . in $poly

# how to detect the need to compute?
!if $poly!=$empty and $cmd!=resume and $noshow!=yes
 error=
 compute=yes
 !for f in poly,t0,t1,range,yrange
  $f=!rawmath $($f)
 !next f
 !if x notvarof $poly or y isvarof $poly
  error=bad_vars
  !exit
 !endif
 equal==
 !if $equal isin $poly
  error=has_equal
  !exit
 !endif
 !if i isvarof $poly
  error=has_complex
  !exit
 !endif
 tt0=!eval $t0
 tt1=!eval $t1
 !if NaN isin $tt0$tt1 or Inf isin $tt0$tt1
  error=bad_t
  !exit
 !endif
 !if abs($tt1-($tt0))<0.001
  ins_anim_frames=1
 !endif
 !if t notvarof $poly
  ins_anim_frames=1
  polyp=$poly
 !else
  polyp=!mathsubst t=(s*($tt1-($tt0))+($tt0)) in $poly
 !endif
 roots=!exec pari for(tt=1,$ins_anim_frames,s=(tt-1)/$ins_anim_frames;a=polroots($polyp,1)~;b=matsize(a)[2]; for(t2=1,b,d=a[t2]; print(real(d),","imag(d)";"));print(";;"))
 !if $roots=$empty
  error=root_fail
  !exit
 !endif
 roots=!translate ; to $\
$ in $roots
 roots=!replace $ $ by $ in $roots

 !read labels
 !if $ins_anim_frames=1
  bar=0
 !else
  bar=0.08
 !endif
 insplot_set=size $size_base*2,$size_base*1.2+$bar; nozeroaxis; noborder;\
	xtics nomirror; ytics nomirror; bmargin;\
	arrow 1 from -$range,0 to $range,0 linetype 3
 insplot_split=size $size_base,$size_base*1.2; origin 0,$bar; arrow 2 from 0,-$range to 0,$range linetype 3; label 1 "Re" at $range*0.9,$range*0.1; label 2 "Im" at $range*0.05,$range*0.9; label 3 "$label_roots" at $range*0.4,$range*0.9\
	size $size_base,$size_base*1.2; origin $size_base,$bar; arrow 2 from 0,-$yrange to 0,$yrange linetype 3; label 1 "x" at $range*0.9,$yrange*0.1; label 2 "y" at $range*0.05,$yrange*0.9; label 3 "P(x)" at $range*0.6,$yrange*0.9
 insplot_data=!translate , to $ $ in $roots
 insplot_parm_1=[-$range:$range] [-$range:$range] \
	'insplot_data' index anim_step notitle with points pointtype 2
 insplot_parm_2=[-$range:$range] [-$yrange:$yrange] $polyp notitle
 !if $ins_anim_frames>1
  animation=yes
  insplot_split=$insplot_split\
	size $size_base*1.2,$bar+0.032; origin $size_base*0.4,0; noytics; noarrow 1; noarrow 2; nolabel 1; nolabel 2; label 3 "t" at $tt1+($tt1-$tt0)*0.03,0.5; bmargin 2
  insplot_parm_3=[$tt0:$tt1] [0:1] x<s*($tt1-($tt0))+($tt0) || x>(s+1/$ins_anim_frames.0)*($tt1-($tt0))+($tt0) ? 0:1 notitle with impulses 6
 !endif
 wims_module_log=sweep $poly
!endif


