!set methtit=Direct change of variable
!set methenv=ZZ QQ RR
!set methparmtype=parm function integrate, function
!set methhelp=This method applies the change of variable \
on an indefinite integral.

!if $wims_read_parm iswordof form check
  !goto $wims_read_parm
!endif

!exit
:form
!set N=!linecnt $mtobj1
!if $N>1 and $methsubstep=1
  :first
  !set methsubstep=2
  Apply a change of variable for: <p>
  !read deduc/methparm.phtml 1
!else
  !bound methparm1 between integer 0 and $N default 1
  !if $methparm1=0 or $error!=$empty
    !goto first
  !endif
  <input type="hidden" name="methparm1" value="$methparm1">
  !set methsubstep=1
  !set l=!line $methparm1 of $mtobj1
  !set l=!item 2 to -1 of $l
  !set k=!mathsubst integrate= in $l
  !set k=!declosing $k
  !set v=!item 2 of $k
  !set nvar=!item 1 of $newvariables
  Change of variable:
  <div class="wimscenter">
  \($l = integrate(f($v),$v) = integrate(f(h($nvar))h'($nvar),$nvar)) ,
  </div>
  with
  \($v = h($nvar) = )
  !read deduc/methparm.phtml 2,36
.
!endif
!exit
:check
ker=!item 2 of $methparmobj1
ker=!mathsubst integrate= in $ker
ker=!trim $ker
ker=!declosing $ker
!distribute items $ker into iker,ivar
vartest=!varlist $ivar
!if $ivar != $vartest
  error=Fail
  !exit
!endif

newvar=!item 1 of $newvariables
vv=!varlist nofn $methparm2
vtest=!listcomplement $newvar,e,pi,euler in $vv
!if $vtest!=$empty
  error=\($methparm2) must be a function of \($newvar) only.
  !exit
!endif
diff=!exec maxima diff($methparm2,$newvar);
!if $diff=$empty
  :bad_data
  error=bad_data
  !exit
!endif
inv=!exec maxima solve($ivar=$methparm2,$newvar);
inv=!declosing $inv
n=!itemcnt $inv
inv2=!nospace $inv
eq==
!if $n!=1 or +++**$newvar$eq notin +++**$inv2
  error=Impossible to find the inverse function of $methparm2, which is\
 	   for future substitution:\
	  use the reverse change of variable.
  !exit
!endif
substed=!mathsubst $ivar=($methparm2) in $iker
!read deduc/sub/checkzero diff(($substed)*($diff),$newvar)
difftest=$out
!read deduc/sub/simplify ($substed)*($diff)\
	($substed)*($diff)*$newvar
!distribute lines $out into newker,ktest
!if $newker=$empty or $ktest=$empty or $difftest=$empty
  !goto bad_data
!endif

!if strictintegrate iswordof $m_options and $difftest=yes
  error=This change of variable is not allowed for this environment.\
	  Please change the method.
  !exit
!endif

!if $difftest=yes
  newobject1=$ktest
!else
  newobject1=integrate($newker,$newvar)
!endif
newobject0=$inv
oldobject=0,1
methexp=\($ivar=$methparm2)
variables=$variables,$newvar
!exit
