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

max_step=400

modif=!trim $modif
!if $modif!=$empty
 !bound modif within exchange,add,multiply,auto,undo,redo,restart default add
!endif
!bound origin between integer 1 and $size default 1
!bound dest between integer 1 and $size default 2
coef=!rawmath $coef

!if $cmd=reply and $status!=waiting and \
    $modif iswordof add exchange multiply auto
 error=double_reply
 !goto end
!endif

!if $cmd=reply
 !if $modif=$empty
  error=$error no_modif
 !endif
 !if $step>$max_step and $modif iswordof auto add exchange multiply
  error=too_many_steps
  !goto end
 !endif
 !if $modif iswordof add multiply
  vlist=!varlist $coef
  !if . isin $coef or ($vlist!=$empty and $vlist notwordof i I i,I)
   error=$error real_coef
  !endif
  !if / isin $coef and $field isin ZG
   error=$error illegal_division
  !endif
 !endif
 !if $error!=$empty
  !goto end
 !endif
 !read proc.$modif
 !if $error!=$empty
  !goto end
 !endif
 !if $modif iswordof exchange add multiply and $pari_out!=$empty
  thisstep=$modif,$origin,$dest,$coef_out
  step=$[$step+1]
  steplist=!line 1 to $step-1 of $steplist
  steplist=!append line $thisstep to $steplist
 !endif
 
 !if $ended=yes and $status!=done
  status=done
  click=no
  success_steps=$step
  allow=$[$size*$size]
  !if $field isin ZG
   allow=$[2*$allow]
  !endif
  module_score=$[max(0,10+min(0,($allow-$step)*0.5))]
  wims_module_log=Success, $step steps $module_score/10
 !endif
!endif

:end
!for i=1 to $size
 col=!line $i of $transpose
 cc=
 n=0
 last=
 !for j=1 to $size
  c=!item $j of $col
  !if $[$c]!=0
   n=$[$n+1]
   cc=!append item $j to $cc
   last=$c
  !endif
 !next j
 !if $n>1 or $[$last]!=1
  n$i=$n
  col$i=$cc
 !else
  n$i=0
  col$i=
 !endif
!next i
laststep=!linecnt $steplist
!read names

modif=
origin=1
dest=2
coef=
pmatrix=!translate ; to $\
$ in $matrix
preference=!translate ; to $\
$ in $reference
!if $field=G
 pmatrix=!replace * by $ in $pmatrix
 preference=!replace * by $ in $preference
!endif

