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

!default ffirst=1
!bound max between integer 1 and 1000 default 10
compute_max=8192

!read pari_src

!if $cmd=help or robot isin $session
 !exit
!endif

# how to detect the need to compute?
input=!trim $input
!if $input != $empty and $noshow!=yes and $cmd!=resume
 error=
 compute=yes
 !if $noshow=$empty or $noshow notwordof last next
  eq==
  !if $eq isin $input
   error=has_equal
   !goto end
  !endif
  input=!rawmath $input
  !if . isin $input
   error=is_real
   !goto end
  !endif
  !if i isvarof $input or I isvarof $input
   error=complex
   !goto end
  !endif
  varlist=!varlist nofn $input
  !if $varlist!=$empty
   error=function
   !goto end
  !endif
  fact_out=!exec pari $pari_src1
  !distribute lines $fact_out into number,factor,delta
  number=!nospace $number
  factor=!trim $factor
  delta=!trim $delta
  !if $number=$empty
   error=bad_number
   !goto end
  !endif
  !if $number=0
   error=is_zero
   !goto end
  !endif
  !if $number=1 or $number=-1
   error=is_one
   !goto end
  !endif
  varlist=!varlist $number
  !if I isin $varlist
   error=complex
   !goto end
  !endif
  !if $varlist!=$empty
   error=function
   !goto end
  !endif
  !if . isin $number or E isin $number or e isin $number
   error=is_real
   !goto end
  !endif
  !if $factor=$empty
   error=factor_fail
   !goto end
  !endif
  !if $delta==$empty or $delta<0
   error=factor_fail
   !goto end
  !endif
  ffirst=1
  wims_module_log=decomp $input
 !else
  !if $noshow=last
   ffirst=$[max(1,$ffirst-$max)]
  !else
   !if $ffirst+$max<=$delta
    ffirst=$[$ffirst+$max]
   !endif
  !endif
  wims_module_log=$noshow: $ffirst
 !endif
 !if $max>=$delta
  ffirst=1
 !endif
 first=$[max(1,min($ffirst,$delta))]
 last=$[min($first+$max-1,$delta)]
 !if $delta>0
  squares=!exec pari f=[$factor]; $pari_src2
  squares=!nospace $squares
  !if $squares=$empty
   error=square_fail
   !goto end
  !endif
  squares=!translate ; to \n in $squares
 !endif
!endif

:end
total=$delta
noshow=
!read htmlfactors

