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

!default objcnt=2
!default euc1=1
!default euc2=2
pari_precision=12
objcnt=!eval rint($objcnt)
!if $objcnt<2
 objcnt=2
!endif
!if $objcnt>9
 objcnt=9
!endif

xs=
Fs=
total=
xpari=
facsrc=
gcdsrc=
lcmsrc=
!for i=1 to $objcnt
 x$i=!trim $(x$i)
 x$i=!lower $(x$i)
 x$i=!rawmath $(x$i)
 xx=!eval $(x$i)
 !if NaN notin $xx and . isin $(x$i)
  realnum=yes
 !endif
 !if $(x$i)=$empty
  hasempty=yes
 !endif
 xs=!append item $(x$i) to $xs
 Fs=!append item F<sub>$i</sub> to $Fs
 total=$total$(x$i)
 xpari=!append line print(x$i=$(x$i)) to $xpari
 facsrc=!append line print(factor($(x$i))) to $facsrc
 !if $i=1
  gcdsrc=$x1
  lcmsrc=$x1
 !else
  gcdsrc=gcd($gcdsrc,$(x$i))
  lcmsrc=lcm($lcmsrc,$(x$i))
 !endif
!next i

!if $total!=$empty and $noshow!=yes
 compute=yes
 !if $realnum=yes
  error_test=$error_test real
 !endif
 !if ($hasempty=yes and $gcdlcm$bezout!=$empty) or\
  ($eucdiv!=$empty and ($(x$euc1)=$empty or $(x$euc2)=$empty))
  error_test=$error_test hasempty
 !endif
 !if $gcdlcm$eucdiv$bezout$getfactor=$empty
  error_test=$error_test nocommand
 !endif
 !if $error_test!=$empty
  wims_module_log=error $error_test
  !exit
 !endif
 
 wims_module_log=calc $xs
 ddd=!exec pari $xpari\
  print($gcdsrc)\
  print($lcmsrc)
 !for i=1 to $objcnt
  xpari$i=!line $i of $ddd
 !next i
 !if $gcdlcm=yes
  gcd=!line $objcnt+1 of $ddd
  lcm=!line $objcnt+2 of $ddd
 !else
  gcd=2
  lcm=2
 !endif
 
 !if $getfactor=yes
  ddd=!exec pari $facsrc\
   print(factor($gcd))\
   print(factor($lcm))
  !for i=1 to $objcnt
   factor=!line $i of $ddd
   base=$(x$i)
   !read htmlfactors
   xfac$i=$factor_html
  !next i
  factor=!line $objcnt+1 of $ddd
  base=$gcd
  !read htmlfactors
  gcdfac=$factor_html
  factor=!line $objcnt+2 of $ddd
  base=$lcm
  !read htmlfactors
  lcmfac=$factor_html
 !endif
 
 !if $bezout=yes
  bezsrc=bez=bezout($x1,$x2);bez1=bez[1];bez2=bez[2];bezz=bez[3];
  !for i=3 to $objcnt step 1
   bezsrc=$bezsrc bez=bezout(bezz,$(x$i));bezz=bez[3];bez$i=bez[2];
   !for j=1 to $i-1
    bezsrc=$bezsrc bez$j=bez$j*bez[1];
   !next j
  !next i
  bezsrc=$bezsrc print(bezz)
  !for i=1 to $objcnt
   bezsrc=$bezsrc\
    print(bez$i)
  !next i
  ddd=!exec pari $bezsrc
  bezgcd=!line 1 of $ddd
  bezres=
  !for i = 1 to $objcnt
   bez$i=!line $i+1 of $ddd
   !if + isin $(bez$i) or - isin $(bez$i)
    bez$i=($(bez$i))
   !endif
   !if $i=1
    bezres=$bez1*F<sub>1</sub>
   !else
    bezres=$bezres + $(bez$i)*F<sub>$i</sub>
   !endif
  !next i
  bezres=$bezres = $bezgcd
 !endif
 
 euc1=!eval rint($euc1)
 euc2=!eval rint($euc2)
 !if $eucdiv=yes and $euc1>0 and $euc1<=$objcnt and $euc2>0 and $euc2<=$objcnt
  ddd=!exec pari euc1=$(x$euc1);euc2=$(x$euc2);while(euc2,print(euc3=euc1%euc2 ",");print((euc1-euc3)/euc2 ",");euc1=euc2;euc2=euc3)
  euclevel=!itemcnt $ddd
  euclevel=!eval rint(($euclevel-1)/2)
  euc1_1=$(x$euc1)
  euc2_1=$(x$euc2)
  euc3_1=!item 1 of $ddd
  euc4_1=!item 2 of $ddd
  !for i=2 to $euclevel
   euc1_$i=$(euc2_$[$i-1])
   euc2_$i=$(euc3_$[$i-1])
   euc3_$i=!item $i*2-1 of $ddd
   euc4_$i=!item $i*2 of $ddd
  !next i
  !for i=1 to $euclevel
   !for j=2 to 4
    e=$(euc$(j)_$i)
    !if + isin $e or - isin $e
     euc$(j)_$i=($e)
    !endif
   !next j
  !next i
 !endif

!endif

