### variable initialisation for EXO 5
### file randrep
### generer  quatre reprsentations : formule, arbre, phrase
### tester lesquelles sont gales.

wims_instex=yes
wims_backslash_insmath=yes

!if $mode=5 or $mode=tool

fns=carre,cube,sqrt,inv,cos,sin,ln,exp
pfns=carr,cube,racine carre,inverse,cosinus,sinus,logarithme,exponentielle
du = du , du , de la , de l' , du , du , du , de l' 
!if $cls=S
    n=4
!else
    !if $cls=P
        n=6
    !else
        n=8
    !endif
!endif
ind=!shuffle $n

!for i in $ind
fct$(i)=!item $i of $fns
pfct$(i)=!item $i of $pfns
du$(i)=!item $i of $du
!next i
lesfct=$fct1,$fct2,$fct3,$fct4
pfct= $pfct1,$pfct2,$pfct3,$pfct4
du= $du1,$du2,$du3,$du4
bins=sum,diff,prod
pbins=somme,diffrence,produit
!if  inv notitemof $lesfct
bins=$bins,div
pbins=$pbins,division
!endif
listactions =$bins,$lesfct,leaf
promptactions=$pbins,$pfct,feuille
!!!! operations list 
lesops= !shuffle $bins,$lesfct,$bins
!!! total number of unary/binary operation in tree


nbop=$[2+$niv]



width=400
w0=$[0.5*$width]
h0=10

leaves=!shuffle $x,$y,$z,$t
leaves=$leaves,$leaves
!if $mode=5
N=4
!else
N=1
!endif

!!!! CREATE N NODEMATRICES ; ITERATE 1 TO N
!!!! difference wrt tree.init : no random choice of leaves

!for i=1 to $N
w=$w0
tree=$(lesops[1]) 
xposmat=$w 
fathers=!row 1 of $tree
nbfathers=1
names=$leaves
!!!nbvar=0
level=1
cursor=1
nbnodes=1
!while $level<=$nbop or $nbfathers>0
  newnodes=
  newpositions=
!!! divide current  w by 2  whenever needed 
!!! no change needed if there is no binary node at current level
   binary=!listintersect $fathers and sum,diff,prod,div
   bincnt=!itemcnt $binary
   !if $bincnt>0
       w=$[0.5*$w] 
   !endif
  !for k=1 to $nbfathers
       curfather=$(fathers[$k])
       xpos=$(xposmat[$level;$k])
       de = !randint 1,3
      !if $curfather isitemof  $lesfct
          !if ($de=1 and $level>=$niv) or $cursor>=$nbop
             new=!item 1 of $names
             names=!item 2 to -1 of $names
             newpos=$xpos
          !else
            !advance cursor
             new=$(lesops[$cursor]) 
             newpos=$xpos
           !endif
           newnodes=$newnodes,$new
           newpositions=$newpositions,$newpos
       !else
!!! interdire les nombres en argument droit ?
           !if $curfather isitemof sum,diff,prod,div
              !if $de=1   or $cursor>=$nbop
                new=!item 1 of $names
                names=!item 2 to -1 of $names
                newpos=$[$xpos-$w]
              !else
                !advance cursor
                 new=$(lesops[$cursor]) 
                 newpos=$[$xpos-$w]
              !endif
               newnodes=$newnodes,$new
               newpositions=$newpositions,$newpos
              !if  $cursor>=$nbop
                new=!item 1 of $names
                names=!item 2 to -1 of $names
                    newpos=$[$xpos+$w]
               !else
                   !advance cursor
                   new=$(lesops[$cursor]) 
                   newpos=$[$xpos+$w]
               !endif
               newnodes=$newnodes,$new
               newpositions=$newpositions,$newpos
            !endif
      !endif
   !next k
  fathers=!item 2 to -1 of $newnodes
  newpositions=!item 2 to -1 of $newpositions
  nbfathers=!itemcnt $fathers
  nbnodes=$[$nbnodes+$nbfathers]
  !if $nbfathers!=0
     tree=$tree;$fathers
     xposmat=$xposmat;$newpositions
  !endif
!advance level
!endwhile
tree$(i)=$tree
depth$(i) = $[$level-1]
xposmat$(i)=$xposmat
!next i

!!!! SYNTACTIC EQUALITY TEST
!!!! ids=0 lorsque toutes les affirmations sont fausses
!!!! sinon contient les numros des exp egales  exp1
ids=0
!for i = 2 to $N
!if $(tree$(i)) = $tree1
    ids=$ids,$i
!endif
!next i 
!if $ids!=0
ids=!item 2 to -1 of $ids
!endif


!!!! GENERATE N FORMULAS : THEEXP (inline)
!!!! + PPEXP (latex pretty print) + NLEXP (exp in natural language)
!!!! DO job only for trees different from tree1

!for i = 1 to $N
!!!! case where theexp$i = $theexp1 ; job done
!if $i>1 and $i isin $ids
theexp$(i)=$theexp1
ppexp$(i)=$ppexp1
nlexp$(i)=$nlexp1
!goto NEXT
!endif
tree=$(tree$(i))
!!!times= \times{}
times=
exps=!row -1 of $tree
exps=!replace , by ; in $exps
texs=$exps
nlexps=$exps
!!!! ajoute article indfini 'de'  toutes les feuilles
nlexps=!replace internal ; by ; de in $nlexps
nlexps= de $nlexps
level=$(depth$(i))
!if $level=1
  theexp=$tree
   ppexp=!texmath $theexp
   nlexp=theexp
!else
 !while $level>1
   newexps=$empty
   newtexs=$empty
   newnls=$empty
   level= $[$level-1]
   fathers=!row $level of $tree
   cursor=1
   !for node in $fathers
      !if $node isitemof sum,diff,prod,div
           !if $node isitemof sum,diff
              pr=3
              !if $node=sum
                  texop=+
                  nlop=  de la somme
               !else
                  texop=-
                  nlop=  de la diffrence
               !endif
            !else
               pr=2
               !if $node=prod
                  texop=$times
                  nlop =  du produit
               !else
                  texop=\frac
                  nlop = du quotient
               !endif
            !endif
            exp1 = $(exps[$cursor;1])
            tex1 = $(texs[$cursor;1])
            nl1=  $(nlexps[$cursor;])
            pr1=$(exps[$cursor;2])
            !advance cursor
            exp2 = $(exps[$cursor;1])
            tex2 = $(texs[$cursor;1])
            nl2= $(nlexps[$cursor;])
            pr2=$(exps[$cursor;2])
            !advance cursor  
            !if $pr1=$empty
                 pr1=0
            !endif
            !if $pr2=$empty
                 pr2=0
            !endif
            !if $pr1>=$pr
              exp1=($exp1)
              !if $node!=div
              tex1=\left($tex1\right)
              !endif
            !endif
             ch2=!char 1 of $exp2
            !if $pr2>=$pr or ($node=diff and $ch2=-)
              exp2=($exp2)
              !if $node!=div
                tex2=\left($tex2\right)
              !endif
            !endif 
            newexps = $newexps;$exp1$node$exp2,$pr  
            !if $node=div
             newtexs = $newtexs;\frac{$tex1}{$tex2},$pr
             newnls=$newnls;$nlop $nl1 par$nl2
            !else
             newtexs = $newtexs;$tex1$texop$tex2,$pr 
             newnls=$newnls;$nlop $nl1 et $nl2
            !endif          
     !else
         !if $node isitemof $lesfct
             exp1 = $(exps[$cursor;1])
             pr1=$(exps[$cursor;2])
             tex1 = $(texs[$cursor;1])
             nl1 = $(nlexps[$cursor;])
             !if $pr1=$empty
                 pr1=0
             !endif
             !advance cursor
             !if $node=carre
                  newnls=$newnls; du carr $nl1
                  !if $pr1>0
                  newexps= $newexps;($exp1)^2,1
                  newtexs= $newtexs;\left($tex1\right)^{2},1
                  !else
                   newexps= $newexps;$(exp1)^2,1
                  newtexs= $newtexs;$(tex1)^{2},1
                  !endif
             !else
                 !if $node=cube
                  newnls=$newnls; du cube $nl1
                     !if $pr1>0
                         newexps= $newexps;($exp1)^3,1
                         newtexs= $newtexs;$\left($tex1\right)^{3},1
                     !else
                         newexps= $newexps;$(exp1)^3,1
                         newtexs= $newtexs;$(tex1)^{3},1

                     !endif
                   !else
                       !if $node=inv
                            !if $pr1>1
                                    exp1=($exp1)
                            !endif
                            newexps= $newexps;1/$exp1,1
                            newtexs= $newtexs;\frac{1}{$tex1},1
                        !else
                           newexps= $newexps;$node($exp1),0  
                           !if $node=sqrt
                         newtexs= $newtexs;\sqrt\left{$tex1\right},0
                           !else
                         newtexs= $newtexs;\$node\left($tex1\right),0
                           !endif
                        !endif
!!!!! LN exp - look up  for correspondance fct -> pfct                       
                        !for  j = 1 to 4
                               fn = $(fct$(j))
                              !if $node=$fn
                           newnls=$newnls;$(du$(j)) $(pfct$(j)) $nl1
                              !endif
                        !next j
                   !endif
              !endif
          !else
                   newexps= $newexps;$node,0 
                   newtexs= $newtexs;$node,0
                   newnls=$newnls;de $node
          !endif 
    !endif  
  !next node
   exps= !row 2 to -1 of $newexps
   texs= !row 2 to -1 of $newtexs
   nlexps=!row 2 to -1 of $newnls
 !endwhile
 theexp=!item 1 of $exps
 ppexp=!item 1 of $texs
 nlexp=!item 1 of $nlexps
!endif
!!! polish expressions
theexp = !replace internal sum by + in $theexp
theexp = !replace internal diff by - in $theexp
theexp = !replace  internal prod by * in $theexp
theexp = !replace internal  div by / in $theexp
nlexp= !replace  internal pardu by par&nbsp;le in $nlexp
nlexp= !replace  internal parde  by par  in $nlexp
theexp$(i)=$theexp
nlexp$(i)=!word 2 to -1 of $nlexp
ppexp$(i)=$ppexp
:NEXT
!next i 


!!! NO SEMANTIC EQUALITY TEST
!!! TAYLOR DEV NOT WORKING WITH 2 VARS 


!!!! DRAW THEGRAPH of matrices  tree_4
h0=50
d=10

!if $mode=5
i=4
!else
i=1
!endif

thegraph= linewidth 1
xposmat=$(xposmat$(i))
tree=$(tree$(i))
level=0
!while $level < $(depth$(i))
  ypos = $[0.5*$h0+$level*$h0]
  y1=$[$ypos+$h0-0.5*$d]
  !advance level
  fathers=!row $level of $tree
   nbfathers = !itemcnt $fathers
  cursor=1
  !for k = 1 to $nbfathers
    curfath=$(fathers[$k])
    xpos = $(xposmat[$level;$k])
    thegraph=$thegraph \
          text black , $xpos , $ypos , medium , $curfath
    !if $curfath isitemof sum,diff,prod,div
        thegraph=$thegraph \
          line $xpos,$[$ypos+$d],$(xposmat[$[$level+1];$cursor]), $y1,blue
        thegraph=$thegraph \
          line $xpos,$[$ypos+$d],$(xposmat[$[$level+1];$[$cursor+1]]), $y1,blue
         cursor=$[$cursor+2]
    !else
         !if $curfath isitemof $lesfct
              thegraph=$thegraph \
                line $xpos,$[$ypos+$d],$xpos,$[$ypos+$h0-0.5*$d],blue
              !advance cursor
         !endif
    !endif
  !next k
!endwhile
graph$(i)=$thegraph



height=$[$h0*$(depth$(i))]



!!!! FOR FINAL EXPLANATION
!if $mode=5
ppexp=\($ppexp1),\($ppexp2),\($ppexp3),\($ppexp4)
!endif


!endif
!!! fin CREATION mode 5 / mode tool


wims_module_log=$cmd mode $mode