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

wims_print_precision=5
wims_compare_precision=1000

!if $cmd!=new and $cmd!=renew
 !for j=1 to $y
  l=!line $j of $matrix
  !for i=1 to $x
   m$i$j=!item $i of $l
  !next i
 !next j
!endif

!if $cmd=reply and $status=waiting
  cmatrix=$matrix
  reply_cnt=!eval $reply_cnt+1
  !for i=1 to $f
   c$i=!item 1 of $(c$i)
   !if $(c$i)=$empty
    error=has_empty
    !exit
   !endif
   c=$[$(c$i)]
   !if NaN isin $c
    error=bad_num
    !exit
   !endif
   cmatrix=!mathsubst c$i=$(c$i) in $cmatrix
  !next i
  crel=
  !if $rel=1
   r=0
   !for j=1 to $y
    rel$j=!item 1 of $(rel$j)
    !if $(rel$j)=$empty
     error=has_empty
     !exit
    !endif
    c=$[$(rel$j)]
    !if NaN isin $c
     error=bad_num
     !exit
    !endif
    r=$[$r+abs($c)]
    crel=!append item $(rel$j) to $crel
   !next j
   !if $r<0.1
    error=trivial_relation
    !exit
   !endif
  !endif
  cmatrix=!translate $\
$ to ; in $cmatrix
  pari_src=M=[$cmatrix];print(matrank(M))
  !if $rel=1
   pari_src=$pari_src\
    a=0;r=[$crel];for(j=0,$y-1,a=a+abs(r*vecextract(M,2^j)));print(a)
  !endif
  pariout=!exec pari $pari_src
  rank=!line 1 of $pariout
  relabs=!line 2 of $pariout
  !if $rank!=$empty and $rank<$x and $rank<$y and ($rel=0 or $relabs<0.0001)
   status=
   module_score=$[30/($reply_cnt+2)]
   !if $module_score<2
    module_score=2
   !endif
   wims_module_log=good reply, score $module_score
  !endif
!endif

!if $status!=waiting
 !for i=1 to $f
  m$(pos$i)=<font color=blue><b>$(c$i)</b></font>
 !next i
!endif

