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

pari_precision=20
pari_serieslength=5
wims_multiexec=pari
wims_backslash_insmath=yes
ins_align=middle
f=<i>f</i>
!default formula=A^2+3*A+2
!default exp_method=numerical

!if $cmd == resume and $preset != A
  !read deflist  
  mat_pari=!translate $\
$ to ; in $matrix
  mat_pari=!rawmath $mat_pari
  preset=!rawmath $preset
  tmp=!exec pari A=[$mat_pari]; $deflist_pari\
    print($preset)
  tmp=!line 2 of $tmp
  matrix=!translate ;\]\[ to $\n  $ in $tmp
!endif

matrix=!rawmatrix $matrix
!if $cmd != resume and $matrix != $empty and $show != $empty
  compute=yes
  l=!linecnt $matrix
  !if inconsistent_columns isin $wims_warn_rawmatrix
    use_space=yes
  !endif
  loadv=objs=$l
  !for i=1 to $l
   ll=!line $i of $matrix
   loadv=$loadv&v$i=$ll
  !next i
  mat_pari=!translate $\
$ to ; in $matrix
  mat_pari=!rawmath $mat_pari
  mat_mup=!replace ; by \],\[ in $mat_pari
  ddd=!exec pari print(matsize([$mat_pari]))\
    printtex([$mat_pari])\
    print(matrank([$mat_pari]))\
    \v
  size=!line 1 of $ddd
  matr_tex=!line 2 of $ddd
  rank=!line 3 of $ddd
  gp_version=!line 4 of $ddd
  gp_version=!word 4 of $gp_version
  n=!itemnum $size
  !ifval $n=2
    rows=!item 1 of $size
    cols=!item 2 of $size
    wims_module_log=$show: $rows x $cols
    !ifval $rows=$cols
      ddd=!exec pari print(trace(A=[$mat_pari]))\
        print(matdet(A))\
	if(A==mattranspose(A),print(qfsign(A)),print("fail"))\
	printtex(poly=charpoly(A,X))\
	print(polroots(poly))
	
      trace=!line 1 of $ddd
      det=!line 2 of $ddd
      signat=!line 3 of $ddd
      polych_tex=!line 4 of $ddd
      eigenval=!line 5 of $ddd
      eigenval=!translate \[\]~ to {} $ in $eigenval
      
      !if square isin $show
        tex_src2=!exec pari printtex([$mat_pari]^2)
      !endif
      !if cube isin $show
        tex_src3=!exec pari printtex([$mat_pari]^3)
      !endif
      !if inverse isin $show and $det != 0
        tex_srci=!exec pari printtex([$mat_pari]^-1)
      !endif
#      !if exponent isin $show
#        !if $exp_method=numerical
#          tex_srce=!exec mupad print(Unquoted,generate::TeX(exp(Dom::Matrix(Dom::Numerical)([[$mat_mup]]))));
#	!else
#          tex_srce=!exec mupad print(Unquoted,generate::TeX(exp(Dom::Matrix(Dom::ExpressionField(normal))([[$mat_mup]]))));
#	!endif
#      !endif
      !if eigen isin $show
#        eigen=!exec mupad print(Unquoted,generate::TeX(linalg::eigenVectors(Dom::Matrix(Dom::ExpressionField(normal))([[$mat_mup]]))));
	eigen=!exec maxima eigenvectors(matrix([$mat_mup]));
	evalue=!item 1 of $eigen
	evector=!item 2 to -1 of $eigen
	evector=!translate internal [] to () in $evector
	evalue=!declosing $evalue
	emult=!item 2 of $evalue
	evalue=!item 1 of $evalue
	evalue=!declosing $evalue
	emult=!declosing $emult
	ecnt=!itemcnt $evalue
	ecnt2=!itemcnt $evector
	!if $ecnt2>$ecnt
	 !if $ecnt=1
	  evec1=$evector
	 !else
	  ecnt3=!sum x for x in $emult
	  !if $ecnt2=$ecnt3
	   j=1
	   !for i=1 to $ecnt
	    m=!item $i of $emult
	    evec$i=!item $j to $j+$m-1 of $evector
	    j=$[$j+$m]
	   !next i
	  !endif
	 !endif
	!else
	 !for i=1 to $ecnt
	  evec$i=!item $i of $evector
	 !next i
	!endif
      !endif
#      !if jordan isin $show
#        jordan=!exec mupad print(Unquoted,generate::TeX(linalg::jordanForm(Dom::Matrix(Dom::ExpressionField(normal))([[$mat_mup]]))));
#      !endif
    !endif
    
    !if register isin $show
      M$register=$matrix
    !endif

    !if every isin $show
      !read deflist
      !for i=1 to 9
        tex_srcM$i=!exec pari printtex([$(M$(i)_pari)])
      !next i
    !endif
    
    !if formula isin $show
      !read deflist
      formula=!rawmath $formula
      subst_tex=!exec pari A=[$mat_pari]; $deflist_pari \
       printtex($formula)
      subst_tex=!line 2 of $subst_tex
    !endif
        
  !else #bad matrix
    bad_mat=yes
  !endif
!endif

