!!!!!!!!! HISTORIQUE EXOS 4 ou 5
!!!!!!!!! manipuler des ingalits simples

!if $cmd=reply and $status=waiting
 
!if $user=Valider and $error=$empty

!!! signes par dfaut inchangs ; modifis par action 1 ou 2
!!! types par dfaut 0 ; mis  1 par action 1/2 pour parenthser.
s1=$signglb
s2=$signlub
t1=0
t2=0

!!!! si le sign de var est marqu inconnu dans l'ingalit choisie
!!!! le mettre  jour si l'exp a un signe dj marqu ailleurs
!!!! only  if  $action > 2

!if $action >2
  !if $x isvarof $glb 
  var = $glb
  s0 = $s1
  cas = 1
  !else 
  var = $lub
  s0 = $s2
  cas = 2
 !endif
 !if $s0=0
  j=1
  M = !rowcnt  $lesexp
  !while $j <= $M 
     line = !row $j of $lesexp
     oldvar = !item 1 of $line
     s = !item 2 of $line
     diff = !exec pari $var - ($oldvar)
!!!! si l'exp a dj t cre avec un signe  connu
    !if $diff=0  and  $s!=0 
          s0 =   $s
          j = $M+1
          s$(cas)=$s0
      !else
         !advance j
      !endif 
    !endwhile 
  !endif
!endif


!!!! DETECTION d'ERREUR
!!!!!  rappel : signes gaux  -1 (<0) ou 1 (>=0) ou 0 (signe ?)

!if $action=3 
!!! interdire deux inverser consecutifs
   last=!item -1 of $histo
   !if $last=3
        error = $error dbleinv
   !else
      !if  $[$s1*$s2]!=1
           error = $error inverse
      !endif
      !if  ($lub = 0 or $glb=0)
          error = $error inverse0
     !endif
   !endif
!endif

!if  $action=4 and  $lub!=0 and $[$s1*$s2]!=1
error = $error carre
!endif

!if  $action=5  and ($s2=-1 or $s1=-1)
error = $error sqrt1
!endif

!if  $action=5  and $s1=0
error = $error sqrt2
!endif

!!!! exo5 : erreur si 2 inegalites n'encadrant pas exp(x)

!if  $action=6  and $mode=exo5
!if  ($x isvarof $lub and $lub!=$glb2) or ($x isvarof $glb and $glb!=$lub2)
   error = $error maj
 !endif
!endif

!if $action=7 and $mode=exo4 and ($lub=0 or $glb=0)
error = $error log0
!endif

!if  $action=7 and $mode=exo4 and ($s2=-1 or $s1=-1)
error = $error log1
!endif

!if  $action=7  and $mode=exo4 and $s1=0
error = $error log2
!endif

!if $action=7 and $mode=exo5 
  !if  $nombre=$empty
     error = $error nombre
   !else
!!!!!! polish (add stars)
     !for ch in 0,1,2,3,4,5,6,7,8,9
         nombre = !replace internal $(ch)$x by $(ch)*$x in $nombre
     !next ch
     tvar= !varlist nofn $nombre
     !if $x != $tvar
         error = $error variable 
    !endif
  !endif
!endif

!if $action=7 and $mode=exo5 

!endif

!endif
!!! end if user=Valider


!!!!! CREATION d'une nouvelle ingalit
!!!!!  mise  jour des nouveaux signes s1 et s2 des nouvelles bornes

!if $user=Valider and $error=$empty

!!!!! noeval bloque les valuations abusives (sinon risque de DL(8))
!!!!! noeval = 1 ds que l'exp contient ln, exp, sqrt, quotient

!if ln isin $nombre or exp isin $nombre or sqrt isin $nombre
noeval=1
!endif

!if $action=1 
  t1=1
  t2=1
 !if $x isvarof $(lub)
      newglb = $[$glb+$nombre]
     !if $noeval = 1
        newlub = $lub+$nombre
      !else
        newlub = !exec pari $lub + $nombre
      !endif
     !if  $newglb >=0 
       s1 = 1
       s2 = 1
     !else 
      s1 = -1
      !if ($nombre <0 and $s2 = 1) or ($nombre >0 and $s2 = -1) 
        s2 = 0
      !endif
     !endif
  !else 
!!!!! x isvarof glb , newglb
      newlub = $[$lub+$nombre]
     !if $noeval = 1
       newglb = $glb+$nombre
      !else
        newglb = !exec pari $glb + $nombre
      !endif
     !if  $newlub < 0 
      s1 = -1
      s2 = -1
     !else 
      s2 = 1
      !if ($nombre <0 and $s1 = 1) or ($nombre >0 and $s1 = -1) 
        s1 = 0
      !endif
      !if $newlub=0
        s1=-1
      !endif
     !endif
  !endif
!endif  

!!!! rgler les pb parenthses dans les multiplications
!!!  valuer quand il s'agit de valeurs numriques

!if $action=2 
  typ1=$typglb
  typ2=$typlub
 !if $nombre > 0 
  bounds=$glb,$lub
 !else
  bounds=$lub,$glb
  s= $[(-1)*$s2]
  s2 = $[(-1)*$s1]
  s1 = $s
  t1=1
  t2=1
 !endif
 !for i=1 to 2
    var=!item $i of $bounds
    !if $x notvarof $var
       new$(i) = $[$nombre*$var]
    !else
       !if $(typ$(i))=0
         new$(i) = $nombre*$var
         !else 
         new$(i) = $nombre*($var)
        !endif
     !endif
  !next i
  newglb = $new1
  newlub = $new2
!endif 

!!! mj d'un signe initialement inconnu aprs action 1 ou 2 

!if $action=1 or $action=2
   !if $x isvarof $newlub and $s2 = 0 and $s1 = 1
             s2 = 1
    !endif
   !if  $x isvarof $newglb  and $s1 = 0 and $s2 = -1
          s1 = -1
   !endif
!endif  

!!!! inverser quand les bornes sont non nulles et de meme signe
!!!! pas de changemnt de signe

!if $action=3 
    newglb =  1/($lub)
    newlub =  1/($glb)
 !endif  

!!!! elever au carr quand les bornes sont positives ou nulles

!if $action=4  and $s1=1
    !if $glb=0 
        newglb = 0
      !else
        newglb = ($glb)^2
    !endif
    newlub = ($lub)^2
    s1 = 1
    s2 = 1
    t1=1
    t2=1
!endif  

!!!!! elever au carr quand les bornes sont ngatives ou nulles

!if $action=4 and ($s2=-1 or $lub=0)
     newlub = ($glb)^2
    !if $lub=0
      newglb= 0
      !else
      newglb = ($lub)^2
     !endif
    s1 = 1
    s2 = 1
!endif  

!!!! passer  la racine quand les bornes sont >=0  (s1=1 or glb=0)
!!!! traitement special pour sqrt(0) et sqrt(1)

!if $action=5 and ($s1=1 or $glb=0)
    !if $glb!=0 and $glb!=1
       newglb = sqrt($glb)
     !else
       newglb = $glb
     !endif
    !if $lub!=0 and $lub!=1
       newlub = sqrt($lub)
     !else
       newlub = $lub
     !endif
    s1 = 1
    s2 = 1
 !endif  

!!!! monter  l'exponentielle 
!if $action=6 and $mode=exo4
    newglb = exp($glb)
    newlub = exp($lub)
    s1 = 1
    s2 = 1
 !endif  

!!!! EXO5 : dduire majorant de carre ou val.abs
!!!! a partir de deux inegalits m <= X et X <= M
!!!! on deduit 0 <= X^2 <= max(m^2, M^2)
!!!! il faut detecter les bornes contenant la variable
!!!! les exp contenant les variables sont les mmes

!if $action=6 and $mode=exo5 and $fct=carre
    !if $x isvarof $lub   
        newglb = ($lub)^2
       !if ($glb)^2 <= ($lub2)^2
           newlub = ($lub2)^2
       !else 
           newlub = ($glb)^2
       !endif
    !else
       newglb = ($glb)^2
       !if ($glb2)] <= ($lub)^2
           newlub = ($lub)^2
       !else 
           newlub = ($glb2)^2
       !endif
    !endif
    s1 = 1
    s2 = 1
!endif  


!if $action=6 and $mode=exo5 and $fct=abs
    !if $x isvarof $lub 
        newglb = abs($lub) 
       !if abs($glb) <= abs($lub2)
           newlub = abs($lub2)
       !else 
           newlub = abs($glb)
       !endif
    !else
         newglb = abs($glb)
       !if abs($glb2)] <= abs($lub)
           newlub = abs($lub)
       !else 
           newlub = abs($glb2)
       !endif
    !endif
    s1 = 1
    s2 = 1
!endif  

!!!! passer au logarithme quand bornes >0
!!!! petit traitement spcial pour  ln(1) 

!if $action=7   and $mode=exo4  and $s1=1
    !if $glb!=1
       newglb = ln($glb)
     !else
       newglb = 0
    !endif
    !if  $lub!=1
       newlub = ln($lub)
     !else
       newlub = 0
    !endif
    !if $x notvarof $newglb 
      !if  $newglb >=1 
        s1 = 1 
        s2 = 1
      !else 
        s1 = -1
      !endif
    !else
      !if  $newlub <1 
        s1 = -1 
        s2 = -1
      !else 
        s2 = 1
      !endif
    !endif  
!endif


!!!! Ecrire une ingalit 0 \le X^2 ou 0 \le abs(X)
!!!! ce choix s'accompagne d'une saisie dans le champ nombre 

!if $action=7 and $mode=exo5
    newglb = 0
    !if $fct=carre
        newlub = ($nombre)^2
    !else 
        newlub = abs($nombre)
    !endif
    s1 = 1
    s2 = 1
 !endif  


!!!! Mj de la matrice lesexp  (newvar ,signe) 
!!!! simplification de la borne constante, si val entire

!if $x isvarof $newglb
   newvar = $newglb
   snv = $s1
   cas = 1
   val=$[$newlub]
   !if . notin $val
      newlub=$val
   !endif    
!else 
   newvar = $newlub
   snv = $s2
   cas = 2
   val=$[$newglb]
   !if . notin $val
      newglb=$val
   !endif 
!endif

!!! si new exp de signe inconnu (donc affine, obtenue action 1 ou 2)
!! on recherche si l'exp existe dj dans la liste lesexp  

!if $action < 3 and  $snv = 0
j=1
M = !rowcnt  $lesexp
  !while $j <= $M 
     line = !row $j of $lesexp
     var = !item 1 of $line
     s = !item 2 of $line
!!!! attention  : test qui peut donner empty pour exp compliques
     diff = !exec pari $newvar - ($var)
!!!! si l'exp a dj t cre avec un signe  connu
    !if $diff=0  and  $s!=0 
          snv =   $s
          s$cas = $snv
          j = $M+1
      !else
         !advance j
      !endif 
    !endwhile 
!endif


!!!! mettre  jour promptinegalites et lesinegalites

   histo = !append item $action to $histo 
   lesexp = $lesexp ; $newvar , $snv
   newineg =  $newglb , $s1 , $t1 , $newlub , $s2 , $t2
   lesinegalites = $lesinegalites ; $newineg 
   promptinegalites = !append item  \($newglb)  $symb  \($newlub)  to $promptinegalites
    L=!itemcnt $promptinegalites
!!! par dfaut la dernire ingalit de la liste est choisie  
    choix=$L


!!! mode noeval aprs avoir cr  newineg. avec ln, exp, rac,quot
!if $noeval=0 and $action > 2
   noeval = 1
!endif

!endif
!!!! end user=valider and error=empty

!endif
!!!! end final