home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / e / e032 / 3.ddi / FILES / CALCULUS.PAK / DSOLVE.M < prev    next >
Encoding:
Text File  |  1992-07-29  |  70.6 KB  |  2,262 lines

  1.  
  2. (* :Name: Calculus`DSolve` *)
  3.  
  4. (* :Title: Differential Equations of the Hypergeometric Type and
  5.            Selected Nonlinear firsti/second order equations *)
  6.  
  7. (* :Author: Victor S. Adamchik and Alexei V. Bocharov respectively*)
  8.  
  9. (* :Context: System` *)
  10.  
  11. (* :Package Version: 2.1 *)
  12.  
  13. (* :Copyright: Copyright 1992  Wolfram Research, Inc.
  14.  
  15.     Permission is hereby granted to modify and/or make copies of
  16.     this file for any purpose other than direct profit, or as part
  17.     of a commercial product, provided this copyright notice is left
  18.     intact.  Sale, other than for the cost of media, is prohibited.
  19.  
  20.     Permission is hereby granted to reproduce part or all of
  21.     this file, provided that the source is acknowledged.
  22. *)
  23.  
  24. (* :History:
  25.     Version 2.1 by Victor S. Adamchik,  1991 and
  26.                 by Alexei V. Bocharov,  1992.
  27.                     Last touch by A.V.Bocharov: March 1992.
  28. *)
  29.  
  30. (* :Keywords:  *)
  31.  
  32. (* :Source:
  33.     V. Adamchik, The Dissertation Thesis, 1987,
  34.     A. Bocharov, Unpublished
  35. *)
  36.  
  37. (* :Mathematica Version: 2.1 *)
  38.  
  39. (*****************************************************************************)
  40. BeginPackage["Calculus`DSolve`"]
  41.  
  42. Unprotect[ DSolve ]
  43.  
  44. DSolve::dsing =
  45. "Unable to fit initial condition `1` at singular point `2`."
  46.  
  47. DSolve::dvsep =
  48. "Unable to separate variables due to complexity of intermediate expressions."
  49.  
  50. Begin["`Private`"]
  51.  
  52. (*****************************************************************************)
  53.  
  54. DSolve[prob_,uu_,t_] :=
  55.       Module[ {DS,memo,DSFail},
  56.  
  57.  
  58. DS[y_'[x_]==F_,y_[x_]] :=
  59.  
  60.      Module[
  61.  
  62.         { wF , var, key , ShiftFactor, SepVarQ , CautionSolve,
  63.               IMultiplier, ScaleFactor, DeepThought, Reducible1,
  64.           Reducible2, GeneralizedBernoulli, GeneralizedScale,
  65.           DeeperThought}
  66.  
  67.         , (* here goes the second argument of the Module *)
  68.  
  69.         ShiftFactor :=
  70.           Module [ {w} , w = Simplify[ D[wF,x] / D[wF,var] ] ;
  71.                If [ FreeQ[w,var] && FreeQ[w,x] ,
  72.                w, Infinity ] ] (*module*) ;
  73.  
  74.             ScaleFactor :=
  75.            Module [ {w},
  76.             w = Simplify[Expand[
  77.             ( wF - var D[wF,var]) / (wF + x D[wF,x])]];
  78.             If [ FreeQ[w,var] && FreeQ[w,x] ,
  79.                  w,
  80.                  Infinity ]
  81.                       ] ; (*eludom*)
  82.  
  83.             DilatationFactor :=
  84.            Module [ {w, Den },
  85.             Den = Simplify[Expand[x D[wF,var] -1 ]] ;
  86.             If [ Den === 0 ,
  87.                  Infinity ,
  88.                   w = Simplify[Expand[
  89.                    ( wF^2 + var D[wF,x])/ Den ]];
  90.                               If [ FreeQ[w,var] && FreeQ[w,x] ,
  91.                    w,
  92.                    Infinity
  93.                                  ]
  94.                             ]
  95.                        ] ; (*eludom*)
  96.  
  97.             ScaleFactors :=
  98.                Module[ { a , b , c , Den, Fx, Fy, Fxx, Fxy, Fyy },
  99.               Fx=D[wF,x];
  100.               Fy=D[wF,var];
  101.               Fxx=D[Fx,x];
  102.               Fxy=D[Fx,var];
  103.               Fyy=D[Fy,var];
  104.               Den = Simplify[ -(Fx*Fxy*Fy) + Fx^2*Fyy +
  105.               Fxy^2*wF - Fxx*Fyy*wF] ;
  106.                           If[ Den === 0,
  107.                  Infinity,
  108.                     (* Comments: zero Den has a very complicated  *)
  109.                 (*           meaning.                         *)
  110.                 (* If we note, however that Den = Fx D1 + F D2*)
  111.                 (* where                                      *)
  112.                 (*      D1 = Fx Fyy - Fy Fxy ,                *)
  113.                 (*      D2 = Fxx Fyy - Fxy^2 ,                *)
  114.                                 (* Then we can go into the special case       *)
  115.                 (*      D1 === 0 , D2 === 0                   *)
  116.                 (* the contents of which is that either       *)
  117.                 (* F === G[ y + a x + b]                      *)
  118.                 (* and thus a shiftable equation              *)                                (* OR                                         *)                                (*    F is linear in y[x]                     *)
  119.                 (*                                            *)                                (* ****************************************** *)
  120.  
  121.                              a = Simplify[
  122.                  (Fx*Fy^2 - Fxy*Fy*wF + Fx*Fyy*wF + Fx*Fxy*Fy*x -
  123.                  Fx^2*Fyy*x - Fxy^2*wF*x + Fxx*Fyy*wF*x)/Den];
  124.                  If[ FreeQ[a,x] && FreeQ[a,var],
  125.                  b = Simplify[
  126.                                  (-3*Fx*Fxy*Fy + Fxx*Fy^2 + 2*Fx^2*Fyy +
  127.                  Fxy^2*wF - Fxx*Fyy*wF)/Den];
  128.                  If[ FreeQ[b,x] && FreeQ[b,var],
  129.                     c = Simplify[(-(Fx^2*Fy) - Fx*Fxy*wF +
  130.                     Fxx*Fy*wF + 3*Fx*Fxy*Fy*var -
  131.                     Fxx*Fy^2*var - 2*Fx^2*Fyy*var -
  132.                     Fxy^2*wF*var + Fxx*Fyy*wF*var )/Den];
  133.                     If[ FreeQ[c,x] && FreeQ[c,var] ,
  134.                     {a,b,c},
  135.                     (*else*)
  136.                     Infinity
  137.                                       ] (*fi*)
  138.                       ,
  139.                                     (*else*)
  140.                     Infinity
  141.                                    ] (*fi*)
  142.                    ,
  143.                                  (*else*)
  144.                  Infinity
  145.                                ] (*fi*)
  146.                         ] (*fi*)
  147.                  ] (*eludom*)
  148.  
  149.                  ;
  150.  
  151.             Reducible1[ wF_,var_,z_ ] :=
  152.                Module[ { Fy , Fyy, Den ,f1 },
  153.                Fy = D[ wF , var ];
  154.                Fyy = D [ Fy , var ] ;
  155.                Den = Simplify[Expand[Fyy^2 - Fy D[Fyy, var ] ] ] ;
  156.                If [ Den === 0 ,
  157.                 Infinity,
  158.                 (*else*)
  159.                 f1 = Simplify[Expand[(-D[Fy,z] Fyy +
  160.                          Fy D[Fyy,z] ) / Den ] ] ;
  161.                             If [ FreeQ[ f1 , var ] ,
  162.                  If [
  163.                    Simplify[ Expand[
  164.                       D[f1,z] Fy - f1 Fy^2 +
  165.                       wF f1 Fyy - f1^2 Fyy -
  166.                       Fy D[wF,z] + (wF - f1) D[Fy,z]
  167.                       ]] === 0 ,
  168.                       f1,
  169.                       Infinity
  170.                                      ]
  171.                                   ,
  172.                   (*else*)
  173.                   Infinity
  174.                                 ] (*fi*)
  175.                             ] (*fi*)
  176.                       ] (*eludom*)  ;
  177.  
  178.             Reducible2[ wF_,var_,z_ ] :=
  179.            Module[ { u, g , Den , Fy, Fyy , Fyyy, Fx, Fxy , Fxyy },
  180.                Fy = D[wF, var ] ;
  181.                Fx = D[wF, z] ;
  182.                Fxy = D[Fx , var ] ;
  183.                Fyy = D[Fy , var ] ;
  184.                Fxyy = D[Fxy , var ] ;
  185.                Fyyy = D[Fyy , var ] ;
  186.  
  187.                Den = Simplify[ Expand[ 2*wF*Fyy - 2*var*Fy*Fyy +
  188.                       var^2*Fyy^2 + var*wF*Fyyy -
  189.                       var^2*Fy*Fyyy ] ] ;
  190.                        If [ Den === 0 ,
  191.                 Infinity ,   (*This is however a palliative*)
  192.                      (* Den===0 has a meaning !    *)
  193.                             g= Together[ Simplify [ Expand[
  194.                   (Fyy*Fx - var*Fyy*Fxy -
  195.                    wF*Fxyy + var*Fy*Fxyy)/Den ] ] ] ;
  196.                             If [ FreeQ[ g , var] ,
  197.                 (*then*)
  198.                 g = Simplify[ PowerExpand[Exp[
  199.                        Integrate[ g , z ] ] ] ] ;
  200.                                 If [ SepVarQ [ (wF/.var:>(g u )) -
  201.                     D[g,z] u , u,z  ] ,
  202.                                      (*then*)
  203.                      g,
  204.                      Infinity
  205.                                    ],
  206.                                 (*else*)
  207.                 Infinity
  208.                                ]
  209.                            ]
  210.                       ] (*eludom*) ;
  211.  
  212.             GeneralizedBernoulli[wF_,var_,z_] :=
  213.                Module[ { Fx , Fy , Fxy ,Den, k  } ,
  214.                Fx= D[wF,z] ;
  215.                Fy= D[wF,var] ;
  216.                Fxy= D[Fx,var] ;
  217.                Den = Fxy wF - Fx Fy ;
  218.                                          (* Recall that Den = 0 means  *)
  219.                      (* equation with separating   *)
  220.                      (* variables : so if separation
  221.                         of variables shadows Bernoulli
  222.                         then the Den must be nonzero
  223.                         here.                      *)
  224.                                          (* SEP VAR MUST SHADOW        *)
  225.                      (* GEN BERNOULLI              *)
  226.                        k=Simplify[(wF D[Fxy,var] - Fx D[Fy,var])/Den];
  227.  
  228.                If [ FreeQ[k,z] &&
  229.                 FreeQ[Simplify[Expand[Fy-k wF]],var] ,
  230.                 (*then*)
  231.                 k,
  232.                 Infinity
  233.                           ] (*if*)
  234.                      ] (*eludom*)
  235.  
  236.                      ;
  237.  
  238.         RiccattiSolution[ a_ , b_ , z_ , s_ , var_ ] :=
  239.                          (* s = s[x] is a special solution
  240.                         of the Riccatti equation   *)
  241.                                          (* y'[x] == a y[x]^2 + b y[x] + c
  242.                       *)
  243.            Module[ { S } ,
  244.           (* z'[x] == - ( 2 a s + b ) z[x] - a *)
  245.                S = Integrate[ 2 a s + b , z ];
  246.                {{var-> s + 1/
  247.                Simplify[Expand[
  248.               (C[1] - Integrate[a Exp[S],z]) Exp[-S] ]]
  249.               }}
  250.                      ] (*eludom*)
  251.              ;
  252.  
  253.  
  254. RiccattiSolve[ wF_ , var_ , z_ ] :=
  255.    Module[ { a , b , c , A , RR , L } ,
  256.        L = CoefficientList[ Collect[wF,var] ,var];
  257.        a=Last[L];
  258.        b=L[[2]];
  259.        c=First[L];
  260.        If [ FreeQ[ Simplify[b/a] , z ] &&
  261.         FreeQ[ Simplify[c/a] , z],
  262.         (*then*)
  263.         RiccattiSolution[ a, b, z ,
  264.          Simplify[
  265.             PowerExpand[
  266.                Simplify[( -b + Sqrt[ b^2 - 4 a c ] ) / ( 2 a ) ]]] ,
  267.                   var ] , (* then clause *)
  268.                 (*else*)
  269.         RR = Sqrt[ (b z - 1)^2 - 4 a z^2 c ] / ( 2 a z^2 ) ;
  270.         A = Simplify[
  271.                PowerExpand[
  272.               Simplify[(1- b z ) / ( 2 a z^2) + RR ]]];
  273.                 If [ FreeQ[A,z],
  274.              RiccattiSolution[ a, b, z, A z , var ]
  275.                      ,
  276.              (*else*)
  277.              A = Simplify[
  278.                 PowerExpand[
  279.                    Simplify[(1- b z ) / ( 2 a z^2) - RR ]]];
  280.                      If [ FreeQ[A,z],
  281.               RiccattiSolution[ a, b, z, A z , var ]
  282.                           ,
  283.               (*else*)
  284.               RR = Sqrt[ (b z + 1)^2 - 4 a z^2 c ] / ( 2 a ) ;
  285.               A = Simplify[
  286.                  PowerExpand[
  287.                     Simplify[(-1- b z ) / ( 2 a ) + RR ]]];
  288.                           If [ FreeQ[A,z],
  289.                    RiccattiSolution[ a, b, z, A / z , var ]
  290.                    ,
  291.                    (*else*)
  292.                    A = Simplify[
  293.                       PowerExpand[
  294.                      Simplify[(-1- b z ) / ( 2 a ) - RR ]]];                               If [ FreeQ[A,z] ,
  295.                                     RiccattiSolution[ a, b, z, A / z , var ]
  296.                     ,
  297.                     (*else*)
  298.                     Infinity
  299.                                   ] (*fi*)
  300.                              ] (*fi*)
  301.                         ] (*fi*)
  302.                    ] (*fi*)
  303.               ] (*fi*)
  304.          ] (*eludom*)
  305.      ;
  306.  
  307.         SepVarQ[wF_,var_,z_] :=
  308.            Module [ {w} , w = Factor[wF,Trig->True];
  309.             Simplify[D[D[w,z],var] w - D[w,z] D[w,var]] === 0 ]
  310.                        (*eludom*) ;
  311.  
  312.             CautionSolve [ eq_, term_ ] :=
  313.            Module [ {ws} ,
  314.             Off[Solve::ifun];
  315.             ws = Solve[ eq/.
  316.             { c_. Log[a_]+d_. Log[b_] :> Log [ a^c b^d ] }, term ] ;
  317.             On[Solve::ifun];
  318.             If [ MatchQ[ws,{}],
  319.             DSFail,
  320.             ws ] ]
  321.             (*eludom*) ;
  322.  
  323.         IMultiplier [ m_ , wF_ , var_ , z_ ] :=
  324.                                          (* Integrate using Integrating *)
  325.                      (*  Multiplier  m_             *)
  326.                Module [ {  w } ,
  327.              w= Integrate [ m , var ] ;
  328.              Simplify[(*Expand*) ( w - Integrate [
  329.             D[w, z ] + wF m , z ] )] == C[1]
  330.                    ] (*eludom*) ;
  331.  
  332.         GeneralizedScale[B_] :=
  333.            Module[ { Fx , Fy , Fxy ,  Den, a, b , wB } ,
  334.                Fx= D[wF,x] ;
  335.                Fy= D[wF,var] ;
  336.                Fxy= D[Fx,var] ;
  337.                Den = Fxy wF - Fx Fy ;
  338.                      (* Recall that Den = 0 means   *)
  339.                      (* equation with separating    *)
  340.                      (* variables : so if separation*)
  341.                      (* of variables shadows GenScal*)
  342.                      (* the Den must be nonzero here*)
  343.  
  344.                        b= 1/D[B,var];    (* this is the symmetry coeff. *)
  345.                        a = Simplify[Expand[
  346.                   ( wF^2 D[D[b,var],var] - wF Fy D[b,var] +
  347.                   ( Fy^2 - wF D[Fy,var] ) b ) / Den ]] ;
  348.                      (* this is, potentially, the other *)
  349.                        If[ FreeQ[a,var] ,
  350.               (*then*)
  351.               If[Simplify[Expand[
  352.                 b Fy + a Fx - D[b,var] wF + D[a,x] wF
  353.                 ]] === 0 ,
  354.                              (*then*)
  355.                  a,
  356.                  (*else*)
  357.                  Infinity
  358.                  ] (*fi*)
  359.                  ,
  360.                           (*else*)
  361.               Infinity
  362.                          ] (*fi*)
  363.                       ] (*eludom*)
  364.               ;
  365.  
  366.         DeeperThought :=
  367.                      (* Still more sophisticated    *)
  368.                      (* tricks implemented here     *)
  369.             Module[{a},
  370.              a=GeneralizedScale[-1/var];
  371.              If[a=!=Infinity,
  372.                 CautionSolve[
  373.                    IMultiplier[var^2-a wF,wF,var,x],var]/.
  374.                    var:>y[x] ,
  375.                             (*else*)
  376.                 a=GeneralizedScale[-1/var^2];
  377.                 If[a=!=Infinity,
  378.                    CautionSolve[
  379.                   IMultiplier[var^3/2-a wF,wF,var,x],var]/.
  380.                   var:>y[x] ,
  381.                                (*else*)
  382.                    a=GeneralizedScale[-1/var^3];
  383.                    If[a=!=Infinity,
  384.                   CautionSolve[
  385.                      IMultiplier[var^4/3-a wF,wF,var,x],var]/.
  386.                      var:>y[x] ,
  387.                                   (*else*)
  388.                   a=GeneralizedScale[var^2/2];
  389.                   If[a=!=Infinity,
  390.                      CautionSolve[
  391.                     IMultiplier[1/var-a wF,wF,var,x],
  392.                     var]/.
  393.                     var:>y[x] ,
  394.                                      (*else*)
  395.                      a=GeneralizedScale[var^3/3];
  396.                      If[a=!=Infinity,
  397.                     CautionSolve[
  398.                        IMultiplier[1/var^2-a wF,wF,var,x],
  399.                        var]/.
  400.                        var:>y[x] ,
  401.                                         (*else*)
  402.                         Infinity
  403.                                        ] (*fi*)
  404.                                     ] (*fi*)
  405.                                  ] (*fi*)
  406.                               ] (*fi*)
  407.                            ] (*fi*)
  408.                       (* ] fi*)
  409.                      (* ] fi*)
  410.              ] (*eludom*)
  411.                      ;
  412.  
  413.             DeepThought[wF_ , var_ , z_ ]      :=
  414.                      (* More sophisticated tricks   *)
  415.                      (* implemented here            *)
  416.             Module[ { u , g , uF , w }
  417.                   ,
  418.           key=Reducible1[wF,var,z];
  419.           If[key=!=Infinity,
  420.             (*then*)
  421.                     uF = Simplify[Expand[
  422.                 ( wF/.var:>(Integrate[key,z] + u )) - key
  423.                 ]] ;
  424.                     w=Simplify[ D[uF,z]/uF ] ;
  425.                     g = Exp[Integrate[ w , z ]] ;
  426.             If [ FreeQ [ g , var ] ,
  427.                         CautionSolve[
  428.                (Simplify[Integrate[ Simplify[Expand[g/uF]],
  429.                     u]/.u:>(var - Integrate[key,z])])
  430.                            == (Integrate[g, z] + C[1]) , var ] (*cauti*)
  431.                         ,
  432.             DSFail
  433.                        ] (*fi*)
  434.                     ,
  435.             (*else*)
  436.             key=Reducible2[wF,var,z];
  437.             If[key=!=Infinity,
  438.               (*then*)
  439.                       uF = Simplify[Expand[
  440.                   ((wF/.var:>(key u)) - D[key,z] u)/key ]];
  441.                       w=Simplify[ D[uF,z]/uF ] ;
  442.               g = Exp[Integrate[ w , z ]] ;
  443.               If [ FreeQ [ g , u ],
  444.               (*then*)
  445.               CautionSolve[
  446.                  (Simplify[Integrate[ Simplify[Expand[g/uF]],
  447.                        u]/.u:>(var - Integrate[key,z])])
  448.                              == (Integrate[g, z] + C[1]) , var ] (*cauti*)
  449.                           ,
  450.               DSFail
  451.                          ] (*fi*)
  452.                        ,
  453.                (*else*)
  454.                key=GeneralizedBernoulli[wF,var,z];
  455.                If[key=!=Infinity,
  456.              (*then*)
  457.              g = Simplify[D[wF,var] - key wF];
  458.                          CautionSolve[
  459.                 IMultiplier [
  460.                    1/Simplify[ PowerExpand [ Exp[
  461.                   Integrate[key,var]] *
  462.                   Exp[Integrate[g,z] ]]],wF,var,z],
  463.                   var ] (*cautionsolve*)
  464.                          ,
  465.              (*else*)
  466.                          If[Simplify[D[D[D[wF,var],var],var]]===0,
  467.                 (*then*)
  468.                 RiccattiSolve[wF,var,z]
  469.                 ,
  470.                 (*else*)
  471.                 Infinity
  472.                            ]
  473.              ] (*fi*)(*generalized bernoulli clause *)
  474.                      ] (*fi*)(*Reducible2 clause*)
  475.                  ] (*fi*)(*Reducible1 clause*)
  476.                ] (*eludom*) (*DeepThought*)
  477.                ;
  478.  
  479. (************************** front-end block *****************************)
  480.  
  481.         wF=F/.y[x]:>var ;            (* Good y[x] is a dead y[x]    *)
  482.  
  483.             If[ FreeQ[wF,var] , {{ y[x] -> Integrate[ F , x ] + C[1] }} ,
  484.         (*else1*)
  485.           If[ SepVarQ[wF,var,x] ,    (* Separating variables clause *)
  486.                   (*then*)
  487.           If [ PolynomialQ[wF,var],
  488.                (*then*)
  489.                key = Last[CoefficientList[Collect[wF,var],var]];
  490.                CautionSolve[ Integrate[key/F , y[x] ]
  491.               == Integrate[key,x ] + C[1],y[x]],
  492.                        (*else3*)
  493.                       If [ PolynomialQ[wF,x],
  494.                (*then*)
  495.                            key = Last[CoefficientList[Collect[F,x],x]];
  496.                            CautionSolve[ Integrate[ 1/key, y[x] ]
  497.                   == Integrate[ F/key , x ] + C[1],y[x]],
  498.                            (*else4*)
  499.                key = Exp[Integrate[Simplify[ D[wF,x]/wF ],
  500.                     x ]] ;
  501.                            If[ FreeQ[key,var] ,
  502.                                CautionSolve[ Integrate[Simplify[key/
  503.                    F], y[x] ] == Integrate[key, x] + C[1] ,
  504.                    y[x] ] , (*then CautionSolve*)
  505.                    (*else5*)
  506.                    Message[DSolve::dvsep];
  507.                    DSFail
  508.                              ] (*fi5*)
  509.                          ] (*fi4*)
  510.                      ] (*fi3*)
  511.                   , (* sep var clause ended here *)
  512.           (*else2*)
  513.                   key = ShiftFactor ;
  514.           If[ key =!= Infinity ,
  515.               (*then*)
  516.                       CautionSolve[
  517.              IMultiplier[1/(wF+key),wF,var,x],var] (*caut*)
  518.              /.var:>y[x]
  519.                       ,
  520.               (*else3*)
  521.               key =  ScaleFactor ;
  522.               If[ key =!= Infinity ,  (* scalable equation clause  *)
  523.               (*then*)            (*      may become obsolete  *)
  524.               CautionSolve[
  525.                  IMultiplier[1/(var - key x wF),wF,var,x],var]
  526.                  /.var:>y[x]
  527.                           ,
  528.               (*else4*)
  529.                           key = DilatationFactor;
  530.                           (* dilatation test passed    *)
  531.               If[ key =!= Infinity,
  532.                   (*then*)
  533.                               CautionSolve[
  534.                  IMultiplier[1/(var wF + key x),wF,var,x],
  535.                  var ]
  536.                  /.var:>y[x]
  537.                               ,
  538.                   (*else5*)
  539.                   key = ScaleFactors;
  540.                   If[ key =!= Infinity,
  541.                   (*then*)
  542.                   CautionSolve[
  543.                   IMultiplier[1/(key[[2]] var + key[[3]] -
  544.                      ( x + key[[1]]) wF),wF,var,x],
  545.                      var ]
  546.                      /.var:>y[x]
  547.                                   ,
  548.                   (*else6*)
  549.                       key = DeepThought[ wF , var , x ] ;
  550.                       If[ (key =!= Infinity) (*&&*) ,
  551.                       (*then*)
  552.                       key/.var:>y[x],
  553.                       (*else*)
  554.                       key = DeeperThought;
  555.                       If[ key =!= Infinity,
  556.                           (*then*)
  557.                           key,
  558.                           (*else*)
  559.                                           key = DeepThought[1/wF, x , var ] ;
  560.                           If[ key =!= Infinity,
  561.                           (*then*)
  562.                               (key/.var:>y)/.x:>x[y],
  563.                               (*else*)
  564.                               DSFail
  565.                           (* the ultimate failure *)
  566.                                             ] (*fi9*)
  567.                                         ] (*fi8*)
  568.                                     ] (*fi7*)
  569.                                 ] (*fi6*)
  570.                             ] (*fi5*)
  571.                         ] (*fi4*)
  572.                     ] (*fi3*)
  573.                 ] (*fi2*) (*this was the if starting with Sep Var test    *)
  574.            ]  (*fi1*)(*this was the if starting with the "no var" condition*)
  575.  
  576.       ] (* Module ends here *)
  577. ;
  578. (****************** further front end considerations ***********************)
  579.  
  580. DS[ G_. y_'[x_] + H_. == 0/;(FreeQ[G,y'[x]] && FreeQ[H,y'[x]]) , y_[x_] ] :=
  581.     Module[ {var, w, wG, wH, K, g },
  582.         CautionSolve [ eq_, term_ ] :=
  583.            Module [ {ws} , ws = Solve[ eq/.
  584.           { c_. Log[a_]+d_. Log[b_] :> Log [ a^c b^d ] }, term ] ;
  585.           If [ MatchQ[ws,{}],SolveFail[ eq, term ] , ws ] ]
  586.           (*eludom*) ;
  587.         wG = G/.y[x]:>var;
  588.         wH = H/.y[x]:>var;
  589.         K  = Simplify[Expand[D[wG,x]-D[wH,var]]];
  590.         If[ K  === 0,
  591.         (*then*)
  592.         w = Integrate[wG,var];
  593.         CautionSolve[ w + Integrate[wH-D[w,x],x]==C[1],var]/.var:>y[x]
  594.         ,
  595.         (*else*)
  596.         g = Simplify[ K/wG];
  597.         If[ FreeQ[g,var],
  598.             DS[Collect[Expand[Exp[-Integrate[g,x]](G y'[x] + H)],y'[x]]
  599.                ==0,y[x]]
  600.             ,
  601.             (*else*)
  602.             g = Simplify[ K/wH];
  603.             If[ FreeQ[g,x],
  604.             DS[Collect[Expand[Exp[Integrate[g,var]/.var:>y[x]] *
  605.                (G y'[x] + H)],y'[x]]==0,y[x]]
  606.                         ,
  607.             (*else*)
  608.                 DS[y'[x]==-H/G,y[x]]
  609.                       ] (*fi*)
  610.                    ] (*fi*)
  611.                ]
  612.            ] (*eludom*)
  613.  
  614. ;
  615. ClairotQ[ F_,y_[x_] ] :=
  616.     Module [ { var , var1 , wF },
  617.          wF=F/.{y[x]:>var,y'[x]:>var1};
  618.          If[ FreeQ[wF,x] && FreeQ[wF,var],
  619.          (*then*)
  620.          False,
  621.          (*else*)
  622.                  Simplify[Expand[
  623.                  D[wF,x] + var1 D[wF,var]
  624.              ]] === 0
  625.                ] (*fi*)
  626.            ] (*eludom*)
  627. ;
  628. DS[ F_==0 /; ClairotQ[F,y[x]] , y_[x_]] :=
  629.     Module[
  630.        {u, wF, wS},
  631.        wF = F/.{y[x]:>u+x y'[x] } ;
  632.        wF = Simplify[Expand[wF]];
  633.        (* Must depend only on y'[x] and u now *)
  634.        wF = wF/.{y'[x]:>C[1]} ;
  635.        wS = Solve[wF==0,u];
  636.        If [ MatchQ[wS,{__}],
  637.         wS/.{(u->r_):>(y[x]->C[1] x + r)},
  638.         (*else*)
  639.         DSFail
  640.               ] (*fi*)
  641.           ] (*eludom*)
  642. ;
  643.  
  644. (* Update as of 2/15/92: provide for equations not resolved *)
  645. (* w.r.t. the first derivative                              *)
  646.  
  647. DS[lhs_==rhs_ ,y_[x_]] :=
  648.  
  649.     Module[ {w,DSList} ,
  650.         DSList[{}] := {};
  651.         DSList[{{y'[x]->term_},L___}] :=
  652.            Union[DSolve[y'[x]==term,y[x],x],DSList[{L}]];
  653.             DSList[other_] := DSFail;
  654.         w=Simplify[Expand[lhs-rhs]];
  655.         If[ FreeQ[w,y'[x]],
  656.         Solve[w==0,y[x]],
  657.         If[ D[D[w,y'[x]],y'[x]] === 0 ,
  658.             DS[Collect[w,y'[x]] == 0 , y[x] ],
  659.             (*else*)
  660.             DSList[Solve[w==0,y'[x]]]
  661.                   ]
  662.               ]
  663.           ]
  664. ;
  665. DS[pro_ , y_[x_], x_ ] := DS[pro,y[x]]
  666. ;
  667. DS[{lhs_==rhs_,c_. y_[a_]==b_},y_[x_]]
  668.         /; (FreeQ[{a,b,c},x] && FreeQ[{a,b,c},y] ) :=
  669.             Module[ { Instantiate , memo },
  670.         Instantiate[{y[x]->term_},aa_,bb_] :=
  671.            Module[ { s },
  672.                s = Solve[bb ==(term/.x:>aa),C[1]];
  673.                If[ MatchQ[s,{}],
  674.                (*then*)
  675.                Message[DSolve::dsing, y[a]==b, a];
  676.                 {},
  677.                            (*else*)
  678.                If[ MatchQ[ s , {L__} ],
  679.                    (*then*)
  680.                    {y[x]->term}/.s
  681.                    ,
  682.                    (*else*)
  683.                    {{y[x]->term},s}
  684.                              ] (*fi*)
  685.                          ] (*fi*)
  686.                      ] (*eludom*)
  687.              ;
  688.              Off[Solve::eqf];
  689.  
  690.          Instantiate[Solve[other_,y[x]],aa_,bb_] :=
  691.         Module[ { s }.
  692.             s = Solve[(other/.{y[x]:>bb,x:>aa}),C[1]];
  693.             If[ MatchQ[ s , {L__} ],
  694.                 Solve[other,y[x]]/.s
  695.                 ,
  696.                 {Solve[other,y[x]],s}
  697.                           ]
  698.                      ]
  699.              ;
  700.              On[Solve::eqf];
  701.  
  702.          Instantiate[{},aa_,bb_] := {};
  703.  
  704.          Instantiate[{first_,rest___},aa_,bb_] :=
  705.          Union[Instantiate[first,aa,bb],Instantiate[{rest},aa,bb]];
  706.  
  707.          Instantiate[other_,aa_,bb_]:= other ;
  708.  
  709.               memo = DS[lhs==rhs,y[x]];
  710.          Instantiate[memo,a,b/c]
  711.           ] (*eludom*)
  712.       ;
  713.  
  714. DS[{c_. y_[a_]==b_,lhs_==rhs_},y_[x_]]
  715.         /; (FreeQ[{a,b,c},x] && FreeQ[{a,b,c},y] ) :=
  716.         DS[{lhs==rhs,c y[a] == b},y[x]]
  717. ;
  718. DS[ pro_ , y_ , x_ ] /; (MatchQ[pro,lhs_==rhs_] ||
  719.             MatchQ[pro,{lhs_==rhs_,c_. y[a_] == b_ }] ||
  720.             MatchQ[pro,{c_. y[a_] == b_ , lhs_==rhs_}] ) :=
  721.  
  722.     Module[ {Purify, memo},
  723.         Purify[{u_[z_]->term_}] :=
  724.            {u->(Function[term]/.z:>#)};
  725.             Purify[S_] :=  (S/.y[x]:>y)/.x:># ;
  726.             memo=DS[pro,y[x]];
  727.         If[ MatchQ[memo, {___}],
  728.         Map[Purify,memo],
  729.         (*else*)
  730.         Purify[memo]
  731.               ] (*fi*)
  732.       ] (*eludom*)
  733.           ;
  734.  
  735. DS[otherwise___]/;True:=DSFail;
  736.  
  737. (*********************** The main interface part ******************)
  738.    memo=DS[prob,uu,t];
  739.    memo /; FreeQ[memo, DSFail]
  740. ]/; Applicable1[prob, uu, t]
  741.  
  742. Applicable1[GG_==HH_,uu_,t_] := Applicable11[GG,HH,uu,t]
  743.  
  744. Applicable1[{GG_==HH_,cc_. yy_[aa_]==bb_},uu_,t_] := Applicable11[GG,HH,uu,t]
  745.  
  746. Applicable1[{cc_. yy_[aa_]==bb_,GG_==HH_},uu_,t_] := Applicable11[GG,HH,uu,t]
  747.  
  748. Applicable1[other_]:= False
  749.  
  750. Applicable11[GG_, HH_, uu_, t_]:= Module[ {h},
  751.           If [ MatchQ[uu , y_[t] ] ,
  752.            (*then*)
  753.            h=Head[uu],
  754.            (*else*)
  755.            h=uu
  756.                  ] (*fi*)
  757.          ;
  758.           If [ Union[Cases[GG==HH,Derivative[_][h][t],-1 ]] =!= {h'[t]},
  759.                    (* then it is not a first- order equation *)
  760.             False,
  761.            (*else*)
  762.            ( D[D[GG-HH,h[t]],h[t]] =!= 0 ||
  763.                D[D[GG-HH,h'[t]],h'[t]] =!= 0 ||
  764.                D[D[GG-HH,h[t]],h'[t]] =!= 0 )
  765.                  ] (*fi*)
  766.            ]
  767.  
  768. Unprotect[D]
  769.  
  770. D[integrate[f_,{var_,lower_,upper_}],x_]:= integrate[D[f,x],{var,lower,upper}] +
  771. (f/.{var:>upper}) D[upper,x]
  772.  
  773. D[integrate[f_,u_],x_] :=
  774.        Module[ {var},
  775.        If [ FreeQ[u,x],
  776.         Integrate[ D[f,x], u] ,
  777.         (* else *)
  778.             Integrate[ D[f/.{u:>var},x],{var,0,u}] +
  779.         f D[u,x]
  780.           ] (*fi*)
  781.       ] (*eludom*)
  782.  
  783. D[Integrate[f_,{var_,lower_,upper_}],x_]:= Integrate[D[f,x],{var,lower,upper}] +
  784.                       (f/.{var:>upper}) D[upper,x]
  785.  
  786. Protect[D]
  787.  
  788.  
  789. DSolve[prob_,uu_,t_] :=
  790.    Module[ {DS2, memo, DSNormalize, DSFail },
  791.  
  792. DS2[y_''[x_]==F_,y_[x_]] :=
  793.  
  794.      Module [ { CautionSub , CautionIntegrate , CrazySolve ,
  795.                 IMultiplier , ScaleSpecial , ScaleSpecial1 ,
  796.                 SepVarSpecial , ShiftFactor ,
  797.                 Scale2Special , ClairotwoQ , EasyTotal , FirstObstacleQ ,
  798.                 SecondObstacleQ , SepVar , key , wF, var , var1 , u , z ,
  799.                 memo , LSFQ , LSFIQ , LS2F , LSV , wwF ,
  800.                 RemoveFirstObstacle , SeparateVariables } ,
  801.  
  802. (*************** Here goes the 2nd argument of this Module ***************)
  803.  
  804.    CautionSub[expr_,lhs_,rhs_]:=
  805.     Module[ { w , CS },
  806.         CS[f_,u_,l_,r_] :=
  807.           If [ u =!= (u/.l:>r),
  808.                (*then*)
  809.                Integrate[ (f/.u:>var) , {var,0,u} ],
  810.                (*else*)
  811.                Integrate[ f , u ]
  812.                      ] ;
  813.  
  814.         w = expr/.Integrate[f_,u_]:> CS[f,u,lhs,rhs];
  815.             (w/.{lhs:>rhs})
  816.           ]  (*eludom*) ;
  817.  
  818.  
  819.    CautionIntegrate[ expr_ , yy_ ] :=
  820.      Module [ {v, memo },
  821.           memo = Integrate[expr,yy];
  822.           If[ FreeQ[ memo , Integrate[f_,u_] ],
  823.            (*then*)
  824.            memo,
  825.            (*else*)
  826.            Integrate[(expr/.{yy:>v}),{v,0,yy}]
  827.                 ] (*fi*)
  828.             ] (*eludom*) ;
  829.  
  830.  
  831.    CrazySolve [ eq_, term_ ] :=
  832.     Module [ {ws} ,
  833.               If[ FreeQ[eq,Integrate[f_,u_]] && FreeQ[eq,integrate[f_,u_]],
  834.           Off[Solve::dinv];
  835.           Off[General::stop];
  836.           ws = Solve[ eq
  837.           , term ] ;
  838.           On[Solve::dinv];
  839.           On[General::stop];
  840.           If [ MatchQ[ws,{}],
  841.           SolveFail[ eq, term ] , ws ]
  842.               ,
  843.               {eq}
  844.           ] ] ;
  845.  
  846.    IMultiplier [ m_ , FF_ , yy_[xx_] ] :=
  847.        Module [ { w } ,
  848.         w= CautionIntegrate [ m,yy[xx]] ;
  849.         CrazySolve [
  850.              (( w - Integrate [ Simplify[
  851.              D[ w/.yy[xx]:>var , xx ] +
  852.              (FF m )/.yy[xx]:>var ] , xx ] )
  853.              /.var:>yy[xx]) == C[1] , yy[xx]
  854.              ]
  855.               ]  ;
  856.  
  857.     ScaleSpecial :=
  858.      Module[ { Fx, Fy, Fxx, Fxy, Fyy, Fxyy, Fyyy, a, Den  } ,
  859.          Fx = D[wF,x];
  860.          Fy = D[wF,var1];
  861.          Fxx = D[Fx,x];
  862.          Fxy = D[Fx,var1];
  863.          Fyy = D[Fy,var1];
  864.          Fxyy = D[Fyy,x];
  865.          Fyyy = D[Fyy,var1];
  866.          Den=Simplify[
  867.     (2*Fxyy*wF - 2*Fxyy*Fy*var1 + Fx*Fyyy*var1 + Fxyy*Fyy*var1^2 - Fxy*Fyyy*var1^2)
  868.                  ];
  869.              If[ Den===0,
  870.          Infinity,
  871.          a= Simplify[
  872.     (-2*Fyy*wF + 2*Fy*Fyy*var1 - Fyyy*wF*var1 - Fyy^2*var1^2 + Fy*Fyyy*var1^2)
  873.          /Den
  874.         ];
  875.         If [ FreeQ[a,var1],
  876.              If [ Simplify[Expand[
  877.                  (D[a,x]-1) var1 Fy - a Fx + (1-2 D[a,x] ) wF-
  878.                  D[D[a,x],x] var1 ]]===0,
  879.                           a,
  880.               Infinity
  881.                         ] 
  882.             ,
  883.                      Infinity
  884.                    ] 
  885.                ] 
  886.                ]  ;
  887.  
  888.  
  889.    ScaleSpecial1 :=
  890.    Module[ { Fy, Fy1, Fyy1, Fy1y1, Fyy1y1, Fy1y1y1, Den, b },
  891.        Fy=D[wF,var];
  892.        Fy1=D[wF,var1];
  893.        Fyy1=D[Fy,var1];
  894.        Fy1y1=D[Fy1,var1];
  895.        Fyy1y1=D[Fyy1,var1];
  896.        Fy1y1y1=D[Fy1y1,var1];
  897.            Den = Simplify[
  898.   (2*Fyy1*wF - 2*Fyy1*Fy1*var1 - 2*Fyy1y1*wF*var1 + 2*Fyy1y1*Fy1*var1^2 +
  899.    Fyy1*Fy1y1*var1^2 - 2*Fy*Fy1y1y1*var1^2 - Fyy1y1*Fy1y1*var1^3 +
  900.    Fyy1*Fy1y1y1*var1^3)
  901.              ] ;
  902.            If [ Den === 0,
  903.         Infinity,        
  904.                 b = Simplify[
  905.  (-2*Fy1*wF + 2*Fy1^2*var1 + 2*Fy1y1*wF*var1 - 3*Fy1*Fy1y1*var1^2 +
  906.      2*Fy1y1y1*wF*var1^2 + Fy1y1^2*var1^3 - Fy1*Fy1y1y1*var1^3)/
  907.                  Den ];
  908.                 If [ FreeQ[b,var1],
  909.              If [ Simplify[Expand[
  910.                  (D[b,var]-1) var1 Fy1 + b Fy + (2 - D[b,var]) *
  911.                  wF - D[D[b,var],var] var1^2 ]] === 0,
  912.               b,
  913.               Infinity
  914.                         ]
  915.             ,
  916.                      Infinity
  917.                   ] 
  918.              ] 
  919.         ]  ;
  920.  
  921.   SepVarSpecial :=
  922.  
  923.      Module [ { w, Den } ,
  924.               Den=Simplify[Expand[var1^2 D[D[wF,var1],var1] -
  925.                   2 var1 D[wF,var1] + 2 wF ] ] ;
  926.               If [ Den === 0,
  927.            Infinity,      
  928.                    w = Simplify[Expand[
  929.                2 D[wF,var] - var1 D[D[wF,var],var1] ]/Den] ;
  930.                    If [ FreeQ[w,var1],
  931.             If[ Simplify[Expand[
  932.                    D[wF,var] + w var1 D[wF,var1] -
  933.                    w wF - ( D[w,var] + w^2 ) var1^2 ]] === 0,
  934.                             Exp[Integrate[w,var]],
  935.                  Infinity
  936.                           ] 
  937.               ,
  938.             Infinity
  939.                       ] 
  940.                  ] 
  941.             ]  ;
  942.  
  943.  
  944.   ShiftFactor  :=
  945.      Module [ { w } ,
  946.           w = Simplify[ D[wF,x] / D[wF,var] ] ;
  947.           If [ FreeQ[w,var] && FreeQ[w,x] && FreeQ[w,var1] ,
  948.             w,
  949.             Infinity ]
  950.             ] ;
  951.  
  952.  
  953.  
  954.    Scale2Factor :=
  955.     Module [ { b },
  956.  
  957.         b = Simplify[Expand[
  958.         ( 2 wF + x D[wF,x] - var1 D[wF,var1])/
  959.         ( wF - var D[wF,var] - var1 D[wF,var1] )
  960.         ]];
  961.  
  962.            If [ FreeQ[b,x] && FreeQ[b,var] && FreeQ[b,var1],
  963.         b,
  964.         Infinity
  965.               ] 
  966.            ]  ;
  967.  
  968.   ClairotwoQ :=
  969.     Module[ { },
  970.             Simplify[Expand[D[wF,x]+var1 D[wF,var]]] === 0
  971.           ]  ;
  972.  
  973.  
  974.    EasyTotal :=
  975.     Module[ {l, Fx, Fy, Fyy1, Fxy1, eq, Den},
  976.         Fx=D[wF,x];
  977.         Fy=D[wF,var];
  978.         Den=Simplify[Expand[(Fx+var1 Fy)]];
  979.           Fy1=D[wF,var1];
  980.           Fxy1=D[Fx,var1];
  981.           Fyy1=D[Fy,var1];
  982.         l= Simplify[Expand[(Fy - Fyy1 var1 - Fxy1)/Den]];
  983.         If [ FreeQ[l,x] && FreeQ[l,var] ,
  984.          If [ Simplify[Expand[
  985.               wF (D[l,var1] + l^2) + 2 Fy1 l + D[Fy1,var1]
  986.               ]] === 0,
  987.               l = Exp[Integrate[l,var1]];
  988.               l/.var1:>y'[x],
  989.               Infinity
  990.                     ] 
  991.             ,
  992.          Infinity
  993.                ] 
  994.           ]  ;
  995.  
  996.    FirstObstacleQ :=
  997.  
  998.     Module[ {},
  999.         Simplify[D[D[wF,x],var]]===0
  1000.           ]  ;
  1001.  
  1002.    RemoveFirstObstacle :=
  1003.     Module[ {l, Fx, Fy, Fyy1, Fxy1, eq },
  1004.             Fx=D[wF,x];
  1005.         Fy=D[wF,var];
  1006.         Fy1=D[wF,var1];
  1007.         Fxy1=D[Fx,var1];
  1008.         Fyy1=D[Fy,var1];
  1009.             l= Simplify[Expand[(Fy - Fyy1 var1 - Fxy1)/(Fx+var1 Fy)]];
  1010.         If [ FreeQ[l,x] && FreeQ[l,var] ,
  1011.          If [ Simplify[Expand[
  1012.               wF (D[l,var1] + l^2) + 2 Fy1 l + D[Fy1,var1]
  1013.               ]] === 0,
  1014.               l = Exp[Integrate[l,var1]];
  1015.                       eq= Integrate[(l/.var1:>y'[x]) (y''[x] - F ) , x]
  1016.                            == C[2];
  1017.               DSolve[eq,y[x],x]
  1018.               ,
  1019.                       DSFail
  1020.                     ] ,
  1021.             DSFail
  1022.                ] 
  1023.            ] 
  1024.            ;
  1025.  
  1026.    SecondObstacleQ :=
  1027.  
  1028.     Module[ {},
  1029.         Simplify[var1 D[D[wF,x],var1]-D[wF,x]]===0
  1030.           ]  ;
  1031.  
  1032.  
  1033.    SepVar :=
  1034.  
  1035.     Module[ { g1, Den, Num },
  1036.         Den=Simplify[Expand[D[wF,x] - var1*D[D[wF,x],var1] ]];
  1037.         Num = Simplify[Expand[D[D[wF,x],var]]];
  1038.         If [ Den === 0 ,
  1039.          If [ Num =!= 0 ,
  1040.               Infinity,
  1041.               Pending
  1042.                     ] 
  1043.                  ,
  1044.                  g1 = Simplify[Num/Den] ;
  1045.              If [ FreeQ[g1,x] && FreeQ[g1,var1],
  1046.                       If[ Simplify[Expand[
  1047.                   D[wF,var]/g1 + var1 D[wF,var1] - wF -
  1048.                   (D[g1,var]/g1+g1) var1^2 ]] === 0,
  1049.                   Exp[Integrate[g1,var]]/.var:>y[x],
  1050.                   Infinity
  1051.                         ] 
  1052.                 ,
  1053.                 Infinity
  1054.                      ] 
  1055.                 ] 
  1056.  
  1057.           ]  ;
  1058.  
  1059.    SeparateVariables :=
  1060.        Module[ { },
  1061.            wF = PowerExpand[Simplify[Expand[(F/.y'[x]:>
  1062.                                                     (var1[x] key ))/key -
  1063.                                     D[key,y[x]] var1[x]^2
  1064.                                                     ]]];
  1065.            If [ FreeQ[wF,y[x]],
  1066.             memo = DSolve[var1'[x]==wF,var1[x],x];
  1067.             If [ MatchQ[Head[memo],DSolve],
  1068.                  DSFail,
  1069.                              LSV[memo]
  1070.                           ] 
  1071.             ,
  1072.             DSFail
  1073.                       ] 
  1074.                 ] 
  1075.                 ;
  1076.  
  1077.           LSFQ[{}]:={};
  1078.       LSFQ[{var1[x]->term_}]:= {y[x]->Integrate[term,x]+C[2]}; 
  1079.       LSFQ[{first_,L___}] :=
  1080.                  Prepend[LSFQ[{L}],LSFQ[first]];
  1081.           LSFQ[other_]:={other/.{var1[x]:>y'[x]}} ;
  1082.  
  1083.           Off[Union::heads];
  1084.           LSFIQ[{}]:={};
  1085.       LSFIQ[{var1[z]->term_}]:=
  1086.                  CrazySolve[(Integrate[1/term,z]/.z:>y[x])==x+C[2],
  1087.                        y[x]] ;
  1088.           LSFIQ[{first_,L__}]:= Union[LSFIQ[first],LSFIQ[{L}]];
  1089.           LSFIQ[{only_}] := LSFIQ[only] ;
  1090.           LSFIQ[other_]:={other/.{var1[z]:>y'[x], z:>y[x]}} ;
  1091.  
  1092.           LS2F[{u[z]->term_}] :=
  1093.          IMultiplier[
  1094.             1/Simplify[ key y[x] -
  1095.             x^key CautionSub[term,z,y[x]/x^key]],
  1096.             x^(key-1) CautionSub[term,z,y[x]/x^key], y[x] ];
  1097.           LS2F[{}] := {};
  1098.           LS2F[{first_,rest__}] :=
  1099.          Union[LS2F[first],LS2F[{rest}]];
  1100.           LS2F[{only_}] := 
  1101.       LS2F[only]
  1102.       ;          
  1103.           LS2F[other_] := 
  1104.       {other/.{u[z]:>(y'[x] x^(1-key)),z:>(y[x]/x^key)}} 
  1105.       ;
  1106.  
  1107.           LSV[ {var1[x]->term_} ] :=
  1108.                  CrazySolve[ Integrate[1/key,y[x]] ==
  1109.                     Integrate[term,x] + C[2] , y[x] ];
  1110.           LSV[{}] := {};
  1111.           LSV[{first_,rest__}] :=
  1112.                 Union[LSV[first],LSV[{rest}]];
  1113.           LSV[{only_}]:=LSV[only] ;
  1114.  
  1115.           LSV[other_] := {other/.
  1116.                   {  var1[x] :> (y'[x]/key) }} ;
  1117.           On[Union::heads];
  1118. (************** the main body of the module starts here ****************)
  1119.           Off[Union::heads];
  1120. wF = (F/.y[x]:>var)/.y'[x]:>var1 ;  
  1121. If[ FreeQ[wF,var] ,
  1122.     key= ScaleSpecial;
  1123.     If [ key=!=Infinity,
  1124.      memo= IMultiplier[1/((1-D[key,x]) var1[x] - key (wF/.{var1:>var1[x]})),
  1125.                (wF/.{var1:>var1[x]}),
  1126.                var1[x]]
  1127.          ,
  1128.          memo=DSolve[var1'[x]==(wF/.var1:>var1[x]),var1[x],x];
  1129.         ]  ;
  1130.      If[ MatchQ[Head[memo],DSolve]
  1131.          ,
  1132.          DSFail,
  1133.          LSFQ[memo]
  1134.         ] 
  1135.       ,
  1136.       (*1else*)
  1137.       If[ FreeQ[wF,x],
  1138.       wwF=((F/.y[x]:>z)/.y'[x]:>var1[z])/var1[z];
  1139.       key= ScaleSpecial1;
  1140.       If [ key=!=Infinity,
  1141.                key=key/.var:>z;
  1142.                memo= IMultiplier[1/((D[key,z]-1) var1[z] -key wwF), wwF,
  1143.                          var1[z]]
  1144.                ,
  1145.            key=SepVarSpecial;
  1146.            If [ key=!=Infinity,
  1147.             key=key/.var:>z;
  1148.                     memo= IMultiplier[1/(D[key,z] var1[z] - key wwF), wwF,
  1149.                               var1[z]],
  1150.             memo=DSolve[var1'[z]==wwF,var1[z],z]
  1151.                   ] 
  1152.              ] 
  1153.          ;
  1154.           If[ MatchQ[Head[memo],DSolve]
  1155.           ,
  1156.           DSFail,
  1157.               LSFIQ[memo]
  1158.              ]
  1159.           ,
  1160.           (*2else*) 
  1161.           key =  Simplify[Expand[ F x y[x]^2 ]] ;
  1162.           If [ FreeQ[ key , x ] ,
  1163.            {Solve[Log[-1 + 2 C[1] y[x] / x + 2 Sqrt[C[1]]/x
  1164.             Sqrt[C[1] y[x]^2 - x y[x] ] ] /(2 C[1]^(3/2)) +
  1165.                         Sqrt[C[1] y[x]^2 - x y[x] ] / (C[1] x) ==
  1166.             (2 key)/ x + C[2] , y[x] ],
  1167.                      Solve[Log[-1 + 2 C[1] y[x] / x + 2 Sqrt[C[1]]/x
  1168.             Sqrt[C[1] y[x]^2 - x y[x] ] ] /(2 C[1]^(3/2)) +
  1169.             Sqrt[C[1] y[x]^2 - x y[x] ] / (C[1] x) ==
  1170.             - (2 key)/ x + C[2] , y[x] ] }
  1171.                ,
  1172.                (*3else*) 
  1173.                key:=TotalDerivative[F,y[x]] ;
  1174.                If[ key=!=Infinity,
  1175.                 memo = DSolve[y'[x] == key + C[2], y[x],x];
  1176.                If [ MatchQ[Head[memo],DSolve],
  1177.                 DSFail,
  1178.                 memo
  1179.                       ]
  1180.                       ,
  1181.                    (*4else*)
  1182.                    If[Simplify[F - D[F,y[x]] y[x] - D[F,y'[x]] y'[x] ] === 0
  1183.                       ,
  1184.                   key = ( F/y[x] )/.{y'[x]:>u[x] y[x]};
  1185.                   key = PowerExpand[Simplify[Expand[key]]];
  1186.                   If [ FreeQ[key,y[x]],
  1187.                    memo:=DSolve[u'[x]==key - u[x]^2, u[x],x];
  1188.                    If [ MatchQ[ Head[memo] , DSolve ],
  1189.                         DSFail,
  1190.                                 memo/.
  1191.                         {{u[x]->term_}:>
  1192.                                  {y[x]->C[2] Exp[Integrate[term,x]]},
  1193.                          u:>y'}
  1194.                               ] 
  1195.                   ,
  1196.                   DSFail
  1197.                          ] 
  1198.                       ,
  1199.                       (*5else*)
  1200.                       key = ShiftFactor;
  1201.                       If[ key =!=Infinity,
  1202.                       wwF = Simplify[F/.{y[x]:>(z-w x),y'[x]:>var1[z]}] ;
  1203.                       If [ FreeQ[wwF,x],
  1204.                                memo=DSolve[ (var1[z]+key) var1'[z] == (wwF) ,
  1205.                                     var1[z],z];
  1206.                        If[ MatchQ[Head[memo],DSolve],
  1207.                            DSFail,
  1208.                                    memo=memo/.C[1]:>C[2];
  1209.                            memo/.{{var1[z]->term_} :>
  1210.                            IMultiplier[1/(key+(term/.z:>
  1211.                                                (y[x]+key x))),
  1212.                                (term/.z:>(y[x]+key x)),
  1213.                                                y[x] ] ,
  1214.                                                z :> (y[x]+key x) , var1:>y'}
  1215.                                  ] 
  1216.                        ,
  1217.                        DSFail
  1218.                              ] 
  1219.                           ,
  1220.                           (*6else*)
  1221.                           key = Scale2Factor;
  1222.                           If[ key=!=Infinity,
  1223.                           wwF = PowerExpand[Simplify[Expand[
  1224.                       ((x^(2-key)F+(1-key) u[z])/(u[z]-key z))/.
  1225.                       {y[x]:>(z x^key),y'[x]:>(u[z] x^(key-1) )}
  1226.                                           ]]];
  1227.                               If [ FreeQ[wwF,x] ,
  1228.                            memo = DSolve[ u'[z] == wwF , u[z],z ];
  1229.                            If [ MatchQ [ Head[memo], DSolve ]
  1230.                             ,
  1231.                             DSFail,
  1232.                                         memo=memo/.C[1]:>C[2];
  1233.                             LS2F[memo]
  1234.                                       ] 
  1235.                            ,
  1236.                            DSFail
  1237.                                  ] 
  1238.                               ,
  1239.                               (*7else*)
  1240.                               If[ ClairotwoQ,
  1241.                               wwF=F/.{y'[x]:>z,y[x]:>(u[z]- z u'[z]),
  1242.                                           x:>(-u'[z])};
  1243.                               If [ FreeQ[wwF,x],
  1244.                                memo := DS2[u''[z]==-1/wwF,u[z]]
  1245.                                      ;
  1246.                                memo/.
  1247.                                {{u[z]->term_}:>
  1248.                                         (ParametricForm[x->-D[term,z],
  1249.                         y-> term - z D[term,z]]
  1250.                         /.z:>DSolve`z),
  1251.                                         Solve[aeq_,u[z]]:>
  1252.                                         (ParametricForm[x ->
  1253.                                         -((u'[z]/.Solve[D[aeq,z],u'[z]])[[1]]),
  1254.                                         Solve[(aeq/.{u[z]:>(y - x z)}),y]]/.
  1255.                                         z:>Dsolve`z)
  1256.                                        } 
  1257.                                ,
  1258.                                DSFail
  1259.                                      ] 
  1260.                                   ,
  1261.                                   (*8else*)
  1262.                                   key =EasyTotal;
  1263.                                   If[ key =!=Infinity,
  1264.                                   DSolve[Integrate[key y''[x]-key F,x]==
  1265.                                          C[2],y[x],x]
  1266.                                       ,
  1267.                                       (*9else*)
  1268.                                       If [ FirstObstacleQ,
  1269.                                            RemoveFirstObstacle,
  1270.                                            (*10else*)
  1271.                                            If[ SecondObstacleQ,
  1272.                                                DSFail,
  1273.                                                (*11else*)
  1274.                                                key = SepVar;
  1275.                                                If[ key =!=Infinity,
  1276.                                                    SeparateVariables,
  1277.                                                    DSFail
  1278.                                                  ] (*fi11*)
  1279.                                              ] (*fi10*)
  1280.                                          ] (*fi9*)
  1281.                                     ] (*fi8*)
  1282.                                 ] (*fi7*)
  1283.                             ] (*fi6*)
  1284.                         ] (*fi5*)
  1285.                      ] (*fi4*)
  1286.                  ] (*fi3*)
  1287.              ] (*fi2*)
  1288.         ] (*fi1*)
  1289.   ] (*fi*)
  1290. ] (*eludom*)  (* The main , hardworking DS2 module ends here *)
  1291. ;
  1292. TotalDerivative[F_,y_[x_]]:=
  1293.     Module[ { IntSuccess , memo, Casualties },
  1294.         IntSuccess[{}] := True;
  1295.         IntSuccess[{Integrate[f_,tt_], L___}] :=
  1296.            FreeQ[f,y'[x]] && FreeQ[f,y[x]] && IntSuccess[{L}] ;
  1297.             memo= Integrate[F,x];
  1298.         Casualties = If[ MatchQ[memo,Integrate[g_,z_]],
  1299.                  {memo},
  1300.                  (*else*)
  1301.                  Cases[memo,Integrate[g_,z_],Infinity]
  1302.                            ] (*fi*)
  1303.                            ;
  1304.         If [ IntSuccess[Casualties],
  1305.          memo,
  1306.          (*else*)
  1307.          Infinity
  1308.                ] (*fi*)
  1309.           ] (*eludom*)
  1310. ;
  1311.  
  1312. DS2[(G_==F_) ,y_[x_]] :=
  1313.      Module[ {memo },
  1314.          memo = TotalDerivative[G-F,y[x]];
  1315.          memo = DSolve[ memo == C[2], y[x],x];
  1316.          If [ MatchQ[Head[memo],DSolve],
  1317.           DSFail,
  1318.           (*else*)
  1319.           memo
  1320.                 ]
  1321.            ] (*eludom*)  /; TotalDerivative[G-F,y[x]]=!=Infinity
  1322. ;
  1323. DS2[lhs_==rhs_ ,y_[x_]] :=
  1324.     Module[ {w,DSList} ,
  1325.         DSList[{}]:= {};
  1326.         DSList[{{y''[x]->first_},rest___}] :=
  1327.            Union[DSolve[y''[x]==first,y[x],x],DSList[{rest}]];
  1328.             DSList[other_]:=DSFail;
  1329.         w=Simplify[Expand[lhs-rhs]];
  1330.         If[ FreeQ[w,y''[x]],
  1331.         DSolve[w==0,y[x],x],
  1332.         (*else*)
  1333.         DSList[Solve[w==0,y''[x]]]
  1334.               ] (*fi*)
  1335.           ] (*eludom*)
  1336. ;
  1337.  
  1338. DS2[pro_ , y_[x_], x_ ] := DS2[pro,y[x]]
  1339. ;
  1340.  
  1341. DS2[{lhs_==rhs_,c0_. y_[a_] == b0_ , c1_. y_'[a_] == b1_} , y_[x_] ]
  1342.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1343.     Module[ { Instantiate , memo },
  1344.         Instantiate[{y[x]->term_},aa_,bb0_,bb1_] :=
  1345.            Module[ { s },
  1346.                s = Solve[{bb0==(term/.x:>aa),
  1347.                   bb1==(D[term,x]/.x:>aa)},
  1348.                   {C[1],C[2]}
  1349.                                 ];
  1350.                        If[ MatchQ[s,{}],
  1351.                (*then*)
  1352.                Message[DSolve::dsing,
  1353.                                {c0 y[a] == b0, c1 y'[a] == b1}, a];
  1354.                {},
  1355.                (*else*)
  1356.                If[ MatchQ[ s , {L__} ],
  1357.                    (*then*)
  1358.                    {y[x]->term}/.s
  1359.                    ,
  1360.                    (*else*)
  1361.                    {{y[x]->term},s}
  1362.                              ] (*fi*)
  1363.                          ] (*fi*)
  1364.                      ] (*eludom*)
  1365.              ;
  1366.  
  1367.          Instantiate[{},aa_,bb0_,bb1_] := {};
  1368.  
  1369.          Instantiate[{first_,rest___},aa_,bb0_,bb1_] :=
  1370.          Union[Instantiate[first,aa,bb0,bb1],
  1371.             Instantiate[{rest},aa,bb0,bb1]];
  1372.  
  1373.              Instantiate[other_,aa_,bb0_,bb1_]:= {other, c0 y[a] == b0,
  1374.                            c1 y'[a] == b1 };
  1375.  
  1376.          memo = DS2[lhs==rhs,y[x]];
  1377.          Instantiate[memo,a,b0/c0,b1/c1]
  1378.           ] (*eludom*)
  1379.       ;
  1380.  
  1381. DS2[{lhs_==rhs_,c1_. y_'[a_] == b1_,c0_. y_[a_] == b0_}, y_[x_] ]
  1382.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1383.     DS2[{lhs==rhs,c0 y[a] == b0,c1 y'[a] == b1},y[x]] ;
  1384.  
  1385. DS2[{c0_. y_[a_] == b0_,lhs_==rhs_,c1_. y_'[a_] == b1_}, y_[x_] ]
  1386.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1387.     DS2[{lhs==rhs,c0 y[a] == b0,c1 y'[a] == b1},y[x]] ;
  1388.  
  1389. DS2[{c0_. y_[a_] == b0_,c1_. y_'[a_] == b1_,lhs_==rhs_}, y_[x_] ]
  1390.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1391.     DS2[{lhs==rhs,c0 y[a] == b0,c1 y'[a] == b1},y[x]] ;
  1392.  
  1393. DS2[{c1_. y_'[a_] == b1_,lhs_==rhs_,c0_. y_[a_] == b0_}, y_[x_] ]
  1394.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1395.     DS2[{lhs==rhs,c0 y[a] == b0,c1 y'[a] == b1},y[x]] ;
  1396.  
  1397. DS2[{c1_. y_'[a_] == b1_,c0_. y_[a_] == b0_,lhs_==rhs_}, y_[x_] ]
  1398.     /; (FreeQ[{a,b0,b1,c0,c1},x] && FreeQ[{a,b0,b1,c0,c1},y] ) :=
  1399.     DS2[{lhs==rhs,c0 y[a] == b0,c1 y'[a] == b1},y[x]] ;
  1400.  
  1401.  
  1402. DS2[pro_ , y_ , x_ ] 
  1403.      /; (MatchQ[pro,lhs_==rhs_] ||
  1404.      MatchQ[pro,{lhs_==rhs_, c0_. y[a_] == b0_ , c1_. y'[a_] == b1_ }] ||
  1405.      MatchQ[pro,{lhs_==rhs_, c1_. y'[a_] == b1_ , c0_. y[a_] == b0_ }] ||
  1406.       MatchQ[pro,{c0_. y[a_] == b0_ ,lhs_==rhs_, c1_. y'[a_] == b1_ }] ||
  1407.       MatchQ[pro,{c0_. y[a_] == b0_ ,c1_. y'[a_] == b1_,lhs_==rhs_ }] ||
  1408.       MatchQ[pro,{c1_. y'[a_] == b1_,lhs_==rhs_, c0_. y[a_] == b0_ }] ||
  1409.       MatchQ[pro,{c1_. y'[a_] == b1_,c0_. y[a_] == b0_ ,lhs_==rhs_ }] )
  1410. :=
  1411.     Module[ {Purify, memo},
  1412.         Purify[{u_[z_]->term_}] :=
  1413.            {u->(Function[term]/.z:>#)};
  1414.             Purify[S_] := (S/.y[x]:>y)/.x:># ;
  1415.         memo=DSNormalize[DS2[pro,y[x]]];
  1416.             If[ MatchQ[memo, {___}],
  1417.         Map[Purify,memo],
  1418.         (*else*)
  1419.         Purify[memo]
  1420.               ] (*fi*)
  1421.           ] (*eludom*)
  1422. ;
  1423. DS2[otherwise___]/;True := DSFail
  1424. ;
  1425. DSNormalize[{}] := {}
  1426. ;
  1427. DSNormalize[{y_[x_]->term_,rest___}] :=
  1428.      Prepend[ DSNormalize[{rest}],{y[x]->term}]
  1429. ;
  1430. DSNormalize[{y_->func_,rest___}] :=
  1431.      Prepend[ DSNormalize[{rest}],{y->func}]
  1432. ;
  1433. DSNormalize[Union[{},S_]]:= S 
  1434. ;
  1435. DSNormalize[other_] := other
  1436. ;
  1437. (********************* The main interface part ***********************)
  1438.    memo= DSNormalize[DS2[prob,uu,t]];
  1439.    memo /; FreeQ[memo, DSFail]
  1440. ] /;Applicable2[prob, uu, t]
  1441.  
  1442. Applicable2[GG_==HH_,uu_,t_] := Applicable22[GG,HH,uu,t]
  1443.  
  1444. Applicable2[{GG_==HH_,c0_. y_[a_] == b0_ , c1_. y_'[a_] == b1_ },uu_,t_] :=
  1445.        Applicable22[GG,HH,uu,t]
  1446.  
  1447. Applicable2[{GG_==HH_,c1_. y_'[a_] == b1_ ,c0_. y_[a_] == b0_ },uu_,t_] :=
  1448.        Applicable22[GG,HH,uu,t]
  1449.  
  1450. Applicable2[{c0_. y_[a_] == b0_ ,GG_==HH_,c1_. y_'[a_] == b1_ },uu_,t_] :=
  1451.        Applicable22[GG,HH,uu,t]
  1452.  
  1453. Applicable2[{c0_. y_[a_] == b0_ ,c1_. y_'[a_] == b1_ ,GG_==HH_},uu_,t_] :=
  1454.        Applicable22[GG,HH,uu,t]
  1455.  
  1456. Applicable2[{c1_. y_'[a_] == b1_,GG_==HH_,c0_. y_[a_] == b0_},uu_,t_] :=
  1457.        Applicable22[GG,HH,uu,t]
  1458.  
  1459. Applicable2[{c1_. y_'[a_] == b1_,c0_. y_[a_] == b0_,GG_==HH_},uu_,t_] :=
  1460.        Applicable22[GG,HH,uu,t]
  1461.  
  1462. Applicable2[other_]:=False
  1463.  
  1464. Applicable22[GG_,HH_,uu_,t_] :=
  1465.    Module[{h,DerList},
  1466.       If[ MatchQ[uu , y_[t] ] ,
  1467.            (*then*)
  1468.            h=Head[uu],
  1469.            (*else*)
  1470.            h=uu
  1471.             ] (*fi*)
  1472.         ;
  1473.           DerList=Union[Cases[GG==HH,Derivative[_][h][t],-1 ]];
  1474.       If[ (DerList=!={h'[t],h''[t]}) && (DerList=!={h''[t]}),
  1475.           (* then it is not a second-order equation *)
  1476.           False,
  1477.           (*else*)
  1478.           DerList=GG-HH;
  1479.           D[D[DerList,h'[t]],h'[t]] =!= 0  ||
  1480.           D[D[DerList,h[t]],h[t]] =!= 0  ||
  1481.           D[D[DerList,h''[t]],h''[t]] =!= 0  ||
  1482.           D[D[DerList,h'[t]],h[t]] =!= 0  ||
  1483.           D[D[DerList,h'[t]],h''[t]] =!= 0  ||
  1484.           D[D[DerList,h[t]],h''[t]] =!= 0
  1485.             ] (*fi*)
  1486.          ] (*eludom*)
  1487.  
  1488.  
  1489.  
  1490.  
  1491.  DSolve[ Equal[a_, b_], f_, x_ ] := 
  1492.    Module[ { answer },
  1493.     VariableC = olderdeg = True;
  1494.     countdsolve = 0; count=-1;
  1495.     answer = DSolveVictor[a==b,If[Head[f]===Symbol,f@@{x},f], x,     
  1496.                 DSolveConstants -> C];
  1497.     If[ FreeQ[answer, FailODE],
  1498.         If[ Head[f]===Symbol,
  1499.         answer = {{f -> Function@@{ answer[[1,1,2]]/.x->#  } }}
  1500.     ]];
  1501.     answer /; FreeQ[answer, FailODE]
  1502.    ]
  1503.    
  1504.  DSolve[ Equal[a_, b_], f_, x_, DSolveConstants -> g_ ] := 
  1505.    Module[ { answer },
  1506.     VariableC = olderdeg = True;
  1507.     countdsolve = 0; count=-1;
  1508.     answer = DSolveVictor[a==b,If[Head[f]===Symbol,f@@{x},f], x,     
  1509.                 DSolveConstants -> g];
  1510.     If[ FreeQ[answer, FailODE],
  1511.         If[ Head[f]===Symbol,
  1512.         answer = {{f -> Function@@{ answer[[1,1,2]]/.x->#  } }}
  1513.     ]];
  1514.     answer /; FreeQ[answer, FailODE]
  1515.    ]
  1516.  
  1517.  DSolve[ {Equal[a_, b_], cond___}, f_, x_ ] := 
  1518.    Module[ { answer, pure, p = If[Head[f]===Symbol,f,Head[f]] },
  1519.     VariableC = olderdeg = True;
  1520.     countdsolve = 0; count=-1;
  1521.     answer = DSolveVictor[a==b,If[Head[f]===Symbol,f@@{x},f], x,     
  1522.                 DSolveConstants -> C];
  1523.     If[ FreeQ[answer, FailODE],
  1524.         pure = 
  1525.         If[ Head[f]===Symbol,
  1526.         {{f -> Function@@{ answer[[1,1,2]]/.x->#} }},
  1527.         {{Head[f] -> Function@@{ answer[[1,1,2]]/.x->#} }} ];
  1528.         p = Max[Cases[Expand[a-b], y_/;!FreeQ[y,Derivative[_][p][x]]]/.
  1529.                  w_. Derivative[n_][p][x] :> n];  
  1530.         pure = Solve[ First[{cond}//.pure], Table[C[i], {i,p}] ];
  1531.         answer = If[ Head[f]===Symbol,
  1532.              {{f -> Function@@{
  1533.                     First[answer[[1,1,2]]/.x->#//.pure]} }},
  1534.              {{f -> First[answer[[1,1,2]]//.pure]}}];                   
  1535.     ];
  1536.     answer /; FreeQ[answer, FailODE]
  1537.    ]
  1538.  
  1539.  DSolve[ {Equal[a_, b_], cond___}, f_, x_, DSolveConstants -> g_  ] := 
  1540.    Module[ { answer, pure, p = If[Head[f]===Symbol,f,Head[f]] },
  1541.     VariableC = olderdeg = True;
  1542.     countdsolve = 0; count=-1;
  1543.     answer = DSolveVictor[a==b,If[Head[f]===Symbol,f@@{x},f], x,     
  1544.                 DSolveConstants -> g];
  1545.     If[ FreeQ[answer, FailODE],
  1546.         pure = 
  1547.         If[ Head[f]===Symbol,
  1548.         {{f -> Function@@{ answer[[1,1,2]]/.x->#} }},
  1549.         {{Head[f] -> Function@@{ answer[[1,1,2]]/.x->#} }} ];
  1550.         p = Max[Cases[Expand[a-b], y_/;!FreeQ[y,Derivative[_][p][x]]]/.
  1551.                  w_. Derivative[n_][p][x] :> n];  
  1552.         pure = Solve[ First[{cond}//.pure], Table[C[i], {i,p}] ];
  1553.         answer = If[ Head[f]===Symbol,
  1554.              {{f -> Function@@{
  1555.                     First[answer[[1,1,2]]/.x->#//.pure]} }},
  1556.              {{f -> First[answer[[1,1,2]]//.pure]}}];                   
  1557.     ];
  1558.     answer /; FreeQ[answer, FailODE]
  1559.    ]
  1560.  
  1561. (*****************************************************************************)
  1562.    
  1563.  DSolveVictor[ Equal[a_, b_], f_[x_], x_, DSolveConstants -> g_ ] := 
  1564.    Module[ { answer = Together[Expand[a - b]], p },
  1565.     Variable = Variable1 = True;
  1566.     countdsolve += 1;
  1567.     answer = 
  1568.     If[ !FreeQ[Denominator[ answer ], f[x]],
  1569.         FailODE,
  1570.         If[ MatchQ[Denominator[answer], Exp[s_/;!FreeQ[s,x]]],
  1571.         answer,
  1572.         Numerator[answer]
  1573.        ]
  1574.     ];
  1575.     answer = 
  1576.     If[ FreeQ[answer,FailODE],
  1577.         p = Cases[answer, w_. Derivative[n_][_][x] ];
  1578.         If[ !FreeQ[ Expand[answer - Plus@@p], Derivative ],
  1579.         FailODE,
  1580.         p = p/.w_. Derivative[n_][f][x] :> n;
  1581.         ODE[ answer, f[x], x, Max[p],
  1582.              If[ FreeQ[answer, f[x]],
  1583.              Min[p], 0], 
  1584.              C ]
  1585.         ],
  1586.         FailODE
  1587.     ]; 
  1588.     If[ count==-1, count = countdsolve-1, count-=1 ]; 
  1589.     If[ And@@(!FreeQ[answer, #]&/@{FailODE, DirectedInfinity[],     
  1590.                     Indeterminate}),
  1591.         If[ countdsolve > 0 || i==0,
  1592.         countdsolve = 0;
  1593.         answer = 
  1594.         Block[ {Integrate,t},
  1595.            Off[DSolve::dnim];
  1596.            t=DSolve[ a==b,f[x],x,DSolveConstants -> g ];
  1597.            On[DSolve::dnim];
  1598.            If[ count==0, dsolve = True];
  1599.            Block[ {DSolve},
  1600.               If[ FreeQ[t,DSolve] ,
  1601.                   {{f[x]->ConstDelDSolve[t[[1,1,2]],x,Max[p],g]}}
  1602.                   ,
  1603.                   FailODE ]
  1604.            ]
  1605.         ],
  1606.         answer = FailODE
  1607.        ]
  1608.        ,
  1609.        answer = If[ Head[answer]===List,
  1610.                {{f[x]->ConstDelDSolve[answer[[1,1,2]],x, Max[p], g]}},
  1611.             answer]
  1612.     ];
  1613.     answer 
  1614.    ]
  1615.   
  1616.  DSolveVictor[ w__ ] := FailODE
  1617.  
  1618.  ODE[ __, max_?(Or[!IntegerQ[#],#<2]&), min_, w_ ] := 
  1619.     FailODE /; True
  1620.  
  1621.  ODE[ expr_, f_[x_], x_, max_, n_?(Or[!IntegerQ[#],#>0]&), w_ ] := 
  1622.    Module[ { newexpr, newf },
  1623.     newexpr = expr/.Derivative[k_][f][x] :> Derivative[k-n][newf][x];
  1624.     newexpr = DSolve[newexpr==0,newf[x],x,DSolveConstants -> w];
  1625.     newexpr = 
  1626.     Block[ {DSolve},
  1627.         If[ FreeQ[newexpr,DSolve],
  1628.             newexpr[[1,1,2]],
  1629.             FailODE ]
  1630.     ];
  1631.     If[ FreeQ[newexpr, FailODE],
  1632.         {{ f[x] ->
  1633.         Nest[ Integrate[#,x]&, newexpr, n ] +
  1634.         Plus@@Table[w[i] x^(i-max+n-1),{i,max-n+1,max}]
  1635.         }},
  1636.         FailODE
  1637.     ]
  1638.    ] 
  1639.  
  1640.  ODE[ expr_, f_[x_], x_, 2, 0, w_ ] := 
  1641.    ODEsecond[Collect[Collect[Collect[ expr,f''[x]],f'[x]],f[x]],f[x],x,w];
  1642.  
  1643.  ODE[ expr_, f_[x_], x_, n_/;n>2, 0, w_ ] := ODEany[expr,f[x],x,n,w]
  1644.  
  1645.  ODE[ __ ] := FailODE
  1646.  
  1647.  ODEsecond[ expr_, f_[x_], x_, w_ ] :=
  1648.    Module[ { r },
  1649.      Off[ Power::infy, Infinity::indet ];
  1650.      r = Kummer[ expr, f[x], x, w];
  1651.      On[ Power::infy, Infinity::indet ];
  1652.      {{f[x] -> ConstDelDSolve[r[[1,1,2]],x,2,w] }} /;
  1653.      FreeQ[r,FailODE]
  1654.    ]
  1655.  
  1656.  ODEsecond[ a_. Derivative[2][f_][x_] + b_. Derivative[1][f_][x_] + c_,
  1657.     f_[x_], x_, w_ ] := 
  1658.    Module[ { newfun,answer },
  1659.     answer = (a f''[x] + b f'[x] + c)/.
  1660.     {
  1661.     Derivative[p_][f][x] :> D[E^(-b/(2a) x) newfun[x] ,{x,p} ],
  1662.     f[x] :> E^(-b/(2 a) x) newfun[x] 
  1663.     };
  1664.     answer = DSolveVictor[ E^(b/(2a) x) answer == 0, newfun[x], x, 
  1665.          DSolveConstants->w ];
  1666.     answer = 
  1667.     If[ FreeQ[answer, FailODE],
  1668.         {{ Rule[f[x], SSimplify[answer[[1,1,2]] E^(-b/(2a) x)]] }},
  1669.         FailODE
  1670.     ];
  1671.     answer /; FreeQ[answer, FailODE]
  1672.    ] /;
  1673.   FreeQ[{a,b},x] 
  1674.  
  1675.  ODEsecond[ a_. x_ Derivative[2][f_][x_]+(b_.+c_. x) f_[x_], f_[x_],x_,w_ ]:= 
  1676.    Module[ { newfun, answer,k=If[Znak[c],-Sqrt[-c/a],-Sqrt[c/a]] },
  1677.     answer = DSolveVictor[ E^(-x k)/x *
  1678.     (a x f''[x] + (b + c x) f[x]/.{
  1679.         f[x]->newfun[x] x E^(x k),
  1680.         f''[x]->D[newfun[x] x E^(x k),{x,2}]
  1681.         }) == 0, 
  1682.     newfun[x], x, DSolveConstants -> w ];
  1683.     If[ FreeQ[answer, FailODE],
  1684.         {{ Rule[f[x], SSimplify[answer[[1,1,2]] E^(x k) x]] }},
  1685.         FailODE
  1686.     ]
  1687.    ] /;
  1688.  FreeQ[{a,c}, x]
  1689.  
  1690.  ODEsecond[ expr_, f_[x_], x_, w_ ] :=  ODEany[ expr,f[x],x,2,w ] 
  1691.  
  1692.  ODEany[ expr_, f_[x_], x_, order_ , w_ ] :=
  1693.    Block[ {Hypergeometric2F1,HypergeometricPFQ},
  1694.    Module[ {xnew,xold,r,answer},
  1695.     collectF = True;
  1696.     r = CheckCoef[ Expand[ CoefODE[
  1697.         CollectListDerivative[ Collect[expr, f[x]], f[x], order],
  1698.         f[x], order ]], x, order]; 
  1699.     If[ !FreeQ[ r, FailODE ], 
  1700.         answer = FailODE,
  1701.         If[ FreeQ[ r, ChangeVariable ],
  1702.         answer = ODEHyperType@@Join[ r, {f[x],x} ]; 
  1703.         If[ FreeQ[answer, FailODE],
  1704.             answer = Sum[w@@{i} answer[[i]],{i,order}];
  1705.             If[ FreeQ[answer,Integrate`sum] && order==2,
  1706.             answer = {{ f[x] -> ConstDelDSolve[answer,x,2,w]}},
  1707.             answer = {{ f[x] -> answer }}
  1708.             ]
  1709.         ],
  1710.         If[ !FreeQ[ r, ChangeVariable[] ],
  1711.             r = ChangeVariables[ expr == 0,f,x,xnew,1/xnew ];
  1712.             answer = DSolveVictor[PowerExpand[r],f[xnew],xnew,
  1713.                 DSolveConstants -> w ]/.
  1714.                 Rule[p_,q_]:>f[x]->q/. xnew->1/x,
  1715.             If[ !FreeQ[ r, ChangeVariable[1] ],
  1716.             r = ChangeVariables[ expr == 0,f,x,xnew,xnew+1 ];
  1717.             answer = DSolveVictor[r,f[xnew],xnew,
  1718.                 DSolveConstants -> w]/.
  1719.                 Rule[p_,q_]:>f[x]->q/.xnew->x-1
  1720.             ,
  1721.             {xold,xnew,val} = 
  1722.               r/.ChangeVariable[ a_,b_,c_ ] :> {a,b,c};
  1723.             r = ChangeVariables[ expr == 0,f,x,xnew,val ]; 
  1724.             answer = 
  1725.             If[ !FreeQ[r, FailODE], FailODE,
  1726.                 DSolveVictor[PowerExpand[r],f[xnew],xnew,
  1727.                 DSolveConstants->w]/.
  1728.                 Rule[p_,q_]:>f[x]->q/.xnew->xold
  1729.             ]
  1730.            ]
  1731.         ]
  1732.         ]
  1733.     ]; 
  1734.     answer
  1735.    ]] /; Depth[expr] < 10 && Length[expr] < 40
  1736.  
  1737.  ODEany[ __ ] := FailODE
  1738.  
  1739.  SSimplify[ expr_ ] := 
  1740.     Block[ {HypergeometricPFQ, HypergeometricU,
  1741.         Hypergeometric2F1, Hypergeometric1F1},
  1742.     Simplify[ expr ] 
  1743.     ] /;
  1744.  And@@(FreeQ[Hold[expr],#]&/@{HypergeometricPFQ, HypergeometricU,
  1745.     Hypergeometric2F1, Hypergeometric1F1})
  1746.  
  1747.  SSimplify[ expr_ ] := Simplify[expr]    
  1748.  
  1749.  Attributes[ SSimplify ] = {HoldFirst}
  1750.     
  1751.  CollectListDerivative[ expr_, f_[x_], r_?Positive ] :=
  1752.     CollectListDerivative[ Collect[expr,f'[x]], f'[x], r-1 ]
  1753.  
  1754.  CollectListDerivative[ g_[expr__], _ , 0 ] := { expr } /; g===Plus 
  1755.  
  1756.  CollectListDerivative[ expr_, _ , 0 ] := { expr }
  1757.  
  1758.  CoefODE[ {a___, w_. Derivative[n_][f_][x_], b___}, f_[x_], n_ ] :=
  1759.     Prepend[ CoefODE[ {a,b}, f[x], n-1 ], w]
  1760.  
  1761.  CoefODE[ expr__,n_?Positive ] := Prepend[ CoefODE[ expr, n-1 ], 0]
  1762.  
  1763.  CoefODE[ {}, __ ] := {}
  1764.  
  1765.  CoefODE[ {a___, w_. f_[x_], b___}, f_[x_], 0 ] :=
  1766.     List[ If[ Length[ {a,b} ] == 0, w, FailODE ] ]
  1767.  
  1768.  CoefODE[ {}, __, -1 ] := { 0 }
  1769.  
  1770.  CoefODE[ {__}, __, -1 ] := { FailODE }
  1771.     
  1772.  FactorSolve[ a_ b_Plus, x_ ]:= If[ !FreeQ[b,x], b, a]
  1773.  
  1774.  FactorSolve[ __ ] :=  FailODE
  1775.  
  1776.  CheckCoef[ expr_, x_, n_ ] := {FailODE} /; !FreeQ[expr,FailODE]
  1777.  
  1778.  CheckCoef[ expr_, x_, n_ ] := {FailODE} /; 
  1779.  Length[ Flatten[ Cases[#,w_ x^k_/;Re[N[k]] < 0]&/@expr ] ] != 0
  1780.  
  1781.  CheckCoef[ expr_, x_, n_ ] := 
  1782.     ( collectF=False; CheckCoef[Collect[expr,x], x, n] ) /;
  1783.  collectF
  1784.  
  1785.  CheckCoef[ {a_. x_^p_, rr__}, x_, n_ ] := 
  1786.     ( olderdeg=False; ChangeVariable[] )/; 
  1787.  olderdeg && FreeQ[a,x] && Re[N[p-n]]>0
  1788.  
  1789.  CheckCoef[ {rr1___, a_. x_^p_. + b_. x_^q_. + c_, rr2___}, x_, n_ ] := 
  1790.    Module[ { root, var=Unique[var], oldvar ,const },
  1791.     Variable = False;
  1792.     Off[Solve::ifun, Solve::tdep];
  1793.     root = Solve[ a x^p + b x^q + c == var, x];
  1794.     On[Solve::ifun, Solve::tdep];
  1795.     If[ WordPresentQ[root], 
  1796.         {FailODE},
  1797.         ChangeVariable[ a x^p + b x^q + c, var, root[[1,1,2]] ]
  1798.     ]
  1799.    ] /; Variable && FreeQ[{a,b,c},x] && Max[Re[{p,q}]]==2
  1800.    
  1801.  CheckCoef[ {rr___, a_. x_^p_. + b_. x_^q_.}, x_, n_ ] := 
  1802.    Module[ { root, varn, r },
  1803.     Variable = False;
  1804.     Off[Solve::ifun, Solve::tdep];
  1805.     root = Solve[ a x^p + b x^q == varn, x];
  1806.     On[Solve::ifun, Solve::tdep];
  1807.     If[ WordPresentQ[root], 
  1808.         If[ (root=Factor[a x^p + b x^q]) =!= a x^p + b x^q,
  1809.         r = FactorSolve[root,x];
  1810.         If[ FreeQ[root,FailODE], 
  1811.             Off[Solve::ifun, Solve::tdep];
  1812.             root = Solve[ r == varn, x];
  1813.             On[Solve::ifun, Solve::tdep];
  1814.             If[ WordPresentQ[root], 
  1815.                 {FailODE},
  1816.                 ChangeVariable[ r, varn, root[[1,1,2]] ]
  1817.             ],
  1818.             {FailODE}
  1819.         ],
  1820.         {FailODE}
  1821.         ],
  1822.         ChangeVariable[ a x^p + b x^q, varn, root[[1,1,2]] ]
  1823.     ]
  1824.    ] /; Variable && FreeQ[{a,b,p,q},x]
  1825.     
  1826.  CheckCoef[ {rr1___, a_. x_^p_. + b_. x_^q_., rr2___}, x_, n_ ] := 
  1827.     If[ Max[p,q] < n - Length[{rr1}],
  1828.         collectF = True;
  1829.         CheckCoef[ Expand[x^(n-Max[p,q]-Length[{rr1}]) *
  1830.             {rr1,a x^p+b x^q,rr2}], x, n ],
  1831.         If[ Min[p,q] =!= n - Length[{rr1}],
  1832.         collectF = True;
  1833.         CheckCoef[ Expand[x^(n-Min[p,q]-Length[{rr1}]) *
  1834.               {rr1,a x^p+b x^q,rr2}], x, n 
  1835.         ],
  1836.         If[ p == n - Length[{rr1}],
  1837.             CheckCoefRest[ 
  1838.             Join[ CollectList[rr1,x], {{a,b}}, 
  1839.                   CollectList[rr2,x] ], 
  1840.                 x, n, Join[ {q-n+Length[{rr1}], n-Length[{rr1}], -b},
  1841.                     If[ Length[{rr1}] == 0, {a}, {} ] ]
  1842.             ],
  1843.             CheckCoefRest[ 
  1844.             Join[ CollectList[rr1,x], {{b,a}}, 
  1845.                   CollectList[rr2,x] ], 
  1846.                 x, n, Join[ {p-n+Length[{rr1}], n-Length[{rr1}], -a},
  1847.                     If[ Length[{rr1}] == 0, {b}, {} ] ]
  1848.             ]
  1849.         ]
  1850.         ]
  1851.     ] /;
  1852.  FreeQ[{a,b},x] && And@@((Im[N[#]]==0 && NumberQ[N[#]])&/@{p,q})
  1853.  
  1854.  CheckCoef[ {rr1___, a_ + b_. x_^q_., rr2___}, x_, n_ ] := 
  1855.    Module[ { answer },
  1856.     answer = 
  1857.     If[ n == Length[{rr1}],
  1858.         CheckCoefRest[ 
  1859.         Join[ CollectList[rr1,x], {{a,b}}, CollectList[rr2,x] ],
  1860.         x, n, {q, 0, -b}
  1861.         ], 
  1862.         CheckCoef[Collect[Expand[x^(n-Length[{rr1}]) {rr1,a+b x^q,rr2}],x],
  1863.         x, n
  1864.         ]
  1865.     ];
  1866.     answer /; FreeQ[answer, FailODE]
  1867.    ] /;
  1868.  FreeQ[{a,b,q}, x]
  1869.  
  1870.  CheckCoef[ {rr___, a_. fun_ + b_.}, x_, n_ ] := 
  1871.    Module[ { varp,root },
  1872.         Variable = False;
  1873.     Off[Solve::ifun,Solve::tdep];
  1874.     root = Solve[ fun == varp, x ]; 
  1875.     On[Solve::ifun,Solve::tdep];
  1876.     root = 
  1877.     If[ WordPresentQ[root],
  1878.         {FailODE},
  1879.         ChangeVariable[ fun, varp, root[[1,1,2]] ]
  1880.     ];
  1881.     root /; FreeQ[root, FailODE]
  1882.    ] /;Variable && n == Length[{rr}] && FreeQ[{a,b},x] && 
  1883.  !FreeQ[fun,x] && !MatchQ[fun,d_. x^_./;FreeQ[d,x]] && 
  1884.  !MatchQ[fun,d_. x^_. + t_/;FreeQ[{t,d},x]]
  1885.  
  1886.  CheckCoef[ {a_. x_^n_, rr___}, x_, n_ ] := 
  1887.     CheckCoefRest[ {{a,0}, rr}, x, n, {Fail,a} ] /;
  1888.  FreeQ[a,x]
  1889.  
  1890.  CheckCoef[ {a_. x_^k_., rr___}, x_, n_ ] := 
  1891.     CheckCoef[ x^(n-k) {a x^k, rr}, x, n ] /;
  1892.  FreeQ[a,x] && NumberQ[N[k]] && Im[N[k]]===0 
  1893.  
  1894.  CheckCoef[ {a_, rr___}, x_, n_ ] := 
  1895.     (Variable1 = False; CheckCoef[ Expand[x^n {a, rr}], x, n ] )/;
  1896.  Variable1 && FreeQ[a, x] && !FreeQ[{rr}, x]
  1897.  
  1898.  CheckCoef[ __ ] := {FailODE}
  1899.  
  1900.  CollectList[ list__, x_ ] := Collect[#,x]&/@{list}
  1901.  
  1902.  CollectList[ x_ ] := {}
  1903.  
  1904.  CheckCoefRest[ {rr1___, {a__}, rr2___}, x_, order_, l_ ] :=
  1905.     CheckCoefRest1[
  1906.        Join[ CoefDevide[ {rr1}, x, order ],{{a}},
  1907.              CoefDevide[ {rr2}, x, order - Length[{rr1}] - 1 ]
  1908.        ],
  1909.     x, order, l
  1910.     ]
  1911.  
  1912.  CoefDevide[ {}, x_, order_ ] := {}
  1913.  
  1914.  CoefDevide[ {w_, v___}, x_, 0 ] := {w}
  1915.  
  1916.  CoefDevide[ {w_, v___}, x_, order_ ] := 
  1917.     Prepend[ CoefDevide[{v},x,order-1], Collect[Expand[w x^(-order)],x] ]
  1918.  
  1919.  CheckCoefRest1[ {a_, rr___}, x_, n_, {c__} ] :=
  1920.     CheckCoefRest1[ {{a,0},rr}, x, n, {c,a} ] /;
  1921.  FreeQ[a,x] && Head[a] =!= List 
  1922.  
  1923.  CheckCoefRest1[ {a_. + b_. x_, rr___}, x_, n_, {1,c__} ] :=
  1924.     CheckCoefRest1[ {{a,b},rr}, x, n, {1,c,a} ] /;
  1925.  FreeQ[{a,b},x] 
  1926.  
  1927.  CheckCoefRest1[ {a_. + b_. x_^lambda_, rr___}, x_, n_, {lambda_,c__} ] :=
  1928.     CheckCoefRest1[ {{a,b},rr}, x, n, {lambda,c,a} ] /;
  1929.  FreeQ[{a,b},x]
  1930.  
  1931.  CheckCoefRest1[ {rr1__, a_. + b_. x_, rr2___}, x_, n_, {1,c__} ] :=
  1932.     CheckCoefRest1[ {rr1,{a,b},rr2}, x, n, {1,c} ] /;
  1933.  FreeQ[{a,b},x] 
  1934.  
  1935.  CheckCoefRest1[ {rr1__, a_. + b_. x_^l_, rr2___}, x_, n_, {l_,c__} ] :=
  1936.     CheckCoefRest1[ {rr1,{a,b},rr2}, x, n, {l,c} ] /;
  1937.  FreeQ[{a,b},x]
  1938.  
  1939.  CheckCoefRest1[ {rr1__, a_, rr2___}, x_, n_, c_ ] :=
  1940.     CheckCoefRest1[ {rr1,{a,0},rr2}, x, n, c ] /;
  1941.  FreeQ[a,x] && Head[a] =!= List
  1942.  
  1943.  CheckCoefRest1[ {rr1__, a_. + b_. x_^q_., rr2___}, x_, n_, {Fail, d_} ] :=
  1944.     CheckCoefRest1[ {rr1,{a,b},rr2}, x, n, {q,n-Length[{rr1}],-b,d} ] /;
  1945.  FreeQ[{a,b,q},x] 
  1946.  
  1947.  CheckCoefRest1[ a__, {Fail, w2_ } ] := {FailODE}
  1948.  
  1949.  CheckCoefRest1[ w__ ] := CheckCoefRest2[ w, {}, {} ]
  1950.  
  1951.  CheckCoefRest2[ { {a_, b_}, w___ }, x_, n_, l_, {up___}, {low___} ] :=
  1952.     CheckCoefRest2[ {w}, x, n, l, {up,a}, {low,b} ]
  1953.  
  1954.  CheckCoefRest2[ {w__}, x_, __ ] := (VariableC = False; ChangeVariable[1]) /;
  1955.  VariableC && Length[{w}]==2 && PolynomialQ[{w}[[1]],x]
  1956.  
  1957.  CheckCoefRest2[ {w__}, __ ] := {FailODE}
  1958.  
  1959.  CheckCoefRest2[ {}, x_, n_, {lambda_, p_, b_, 0}, up_, low_ ] := 
  1960.     ChangeVariable[]
  1961.  
  1962.  CheckCoefRest2[ {}, x_, n_, {lambda_, p_, b_, a_}, up_, low_ ] := 
  1963.     { SolvePar[Reverse[up], n]/lambda, 
  1964.       SolvePar[-Reverse[low], n]/lambda, x^lambda/a b lambda^(p-n), n }
  1965.  
  1966.  WordPresentQ[ word_ ] :=
  1967.      Block[ { Solve },
  1968.            !FreeQ[word,Roots] || !FreeQ[word,Solve] ||     
  1969.                 Length[word]==0
  1970.     ]
  1971.  
  1972.  Attributes[WordPresentQ] = {HoldFirst}
  1973.  
  1974.  SolvePar[ {v_, list__}, n_ ] :=
  1975.    Module[ { x },
  1976.     Flatten[
  1977.       Solve[
  1978.         v + Sum[ {list}[[i]] Product[ x - j + 1, {j,i} ], {i,n} ] == 0, x
  1979.       ] /. Rule[x, w_] :> w
  1980.     ]
  1981.    ]
  1982.  
  1983.  ODEHyperType[ w__/; !FreeQ[{w},FailODE] ] := FailODE 
  1984.  
  1985.  ODEHyperType[ rootX_, __ ] := FailODE /; 
  1986.  Length[rootX] > 2 && LogCaseX[ rootX ]  
  1987.  
  1988.  ODEHyperType[ {x1_, x2_}, rootU_, psi_, 2, f_[x_], x_ ] :=
  1989.    Module[ { xx1=x1, xx2=x2, bb, dd },
  1990.     If[ Re[ N[x1-x2] ] < 0, xx1=x2; xx2=x1 ];
  1991.     If[ LogCase512[ xx1 - rootU ], 
  1992.         { 
  1993.           MeijerReduce[ {}, 1+rootU, {xx1}, {xx2}, 
  1994.                 Exp[Pi I (Length[rootU]+1)] psi, x
  1995.           ],
  1996.           MeijerReduce[ {}, 1+rootU, {xx1,xx2}, {}, 
  1997.                 Exp[Pi I Length[rootU]] psi, x
  1998.           ] 
  1999.         },
  2000.         If[ LogCase512[ xx2 - rootU ], 
  2001.         {
  2002.          If[ LogCase512[rootU-xx1],
  2003.              MeijerReduce[ 1+rootU, {}, {xx1}, {xx2}, -psi, x ],
  2004.              bb = Select[rootU,IntegerQ[xx1-#-1]&&Negative[xx1-#-1]&];
  2005.              dd=Delete[rootU,Union[Flatten[Position[rootU,#]&/@bb,1]]];
  2006.              MeijerReduce[ 1+dd, 1+bb, {xx1}, {xx2}, 
  2007.                         Exp[Pi I (Length[bb]+1)] psi, x ]
  2008.          ], 
  2009.          MeijerReduce[ {}, 1+rootU, {xx1,xx2}, {}, 
  2010.                 Exp[Pi I Length[rootU] ] psi, x
  2011.          ] 
  2012.         },
  2013.         If[ LogCase512[1+rootU-xx2],
  2014.             {
  2015.             MeijerReduce[ 1+rootU, {}, {xx1}, {xx2}, -psi, x ], 
  2016.             MeijerReduce[ 1+rootU, {}, {xx1,xx2}, {}, psi, x] 
  2017.             }
  2018.             ,
  2019.             bb = Select[rootU, IntegerQ[xx2-#-1]&&Negative[xx2-#-1]& ];
  2020.             dd =Delete[rootU,Union[Flatten[Position[rootU,#]&/@bb,1]]];
  2021.             {
  2022.             If[ LogCase512[1+rootU-xx1],
  2023.                 MeijerReduce[ 1+rootU, {}, {xx1}, {xx2},-psi, x ],
  2024.             MeijerReduce[ 1+dd, 1+bb, {xx1}, {xx2},  
  2025.                         Exp[Pi I (Length[bb]+1)] psi, x]
  2026.             ], 
  2027.             MeijerReduce[ 1+dd, 1+bb, {xx1,xx2}, {}, 
  2028.                         Exp[Pi I Length[bb]] psi, x] 
  2029.             }
  2030.         ]
  2031.         ]
  2032.     ]
  2033.    ] /;
  2034.  LogCaseX[ {x1,x2} ] 
  2035.  
  2036.  ODEHyperType[ rootX_, rootU_, psi_, order_, f_[x_], x_ ] :=
  2037.     Module[ { bb, dd },
  2038.     Table[ 
  2039.          If[ LogCase512a[ rootX[[i]] - rootU ], 
  2040.          bb = Select[rootU,IntegerQ[rootX[[i]]-#]&&
  2041.              (Negative[rootX[[i]]-#] || SameQ[rootX[[i]]-#,0])& ];
  2042.          dd =Delete[rootU,Union[Flatten[Position[rootU,#]&/@bb,1]]];
  2043.          MeijerReduce[ 1+dd, 1+bb, {rootX[[i]]}, Drop[rootX,{i}], 
  2044.                 Exp[Pi I (Length[bb]-1)] psi, x
  2045.          ],
  2046.          MeijerReduce[ 1+rootU, {}, {rootX[[i]]}, Drop[rootX,{i}], 
  2047.                 Exp[Pi I] psi, x
  2048.          ]
  2049.          ],
  2050.     {i, order}
  2051.     ] 
  2052.     ]
  2053.  
  2054.  MeijerReduce[ n_, p_, m_, q_, arg_, x_ ] :=
  2055.    Module[ { r1, r2 },
  2056.     r1 = ReducePar[ m,p ];
  2057.     r2 = ReducePar[ q,n ];
  2058.     MeijerPrelimCaseGlog[ r2[[2]], r1[[2]], r1[[1]], r2[[1]], arg ] /. 
  2059.         { a_^k_ :> a^Expand[ k ] } //.
  2060.         { (a_ x^b_.)^k_ :> a^k x^Expand[b k] }/.BesselComplexArg 
  2061.   ] 
  2062.  
  2063.  LogCaseX[ {x_, root__} ] := 
  2064.     If[ Or@@( IntegerQ[ # ]&/@( x-{root} )),
  2065.         True,
  2066.         LogCase[ {root} ]
  2067.     ] 
  2068.  
  2069.  LogCaseX[ x_ ] := False
  2070.  
  2071.  LogCase512a[ {u1___, z_Integer, u2___} ] := True/;Negative[z]||z===0
  2072.  
  2073.  LogCase512a[ __ ] := False
  2074.  
  2075.  LogCase512[ {u1___, z_Integer?Positive, u2___} ] := False
  2076.  
  2077.  LogCase512[ __ ] := True
  2078.  
  2079.  ConstDelDSolve[ expr_?(!FreeQ[#,FailODE]&), __ ] := FailODE
  2080.  
  2081.  ConstDelDSolve[ expr_, x_ , 2, C_ ] := 
  2082.    Module[ { r = Expand[expr], r1, r2 }, 
  2083.     r1 = Cases[ Cases[ r,s_/; !FreeQ[s,C[1]] ]/C[1], s_/;FreeQ[s,C[_]] ];
  2084.     r2 = Cases[ Cases[ r,s_/; !FreeQ[s,C[2]] ]/C[2], s_/;FreeQ[s,C[_]] ];
  2085.     If[ Complement[r2,r1]==={},
  2086.         r1 = Complement[r1,r2],
  2087.         If[ Complement[r1,r2]==={},
  2088.         r2 = Complement[r2,r1] ]];
  2089.     C[1] SSimplify[Plus@@r1] + C[2] SSimplify[Plus@@r2] +
  2090.     Plus@@Cases[ r,s_/; FreeQ[s,C[1]] && FreeQ[s,C[2]]] 
  2091.    ] /; FreeQ[expr, Literal[Integrate[__]]]
  2092.  
  2093. ConstDelDSolve[ expr_, __ ] := expr
  2094.  
  2095.  MeijerPrelimCaseGlog[ n_, p_, m_, q_, arg_ ] :=
  2096.   Module[ { answer },
  2097.     answer = Integrate`MeijerPrelimCase[ n,p,m,q,arg ];
  2098.     If[ FreeQ[answer,Integrate`FailInt], 
  2099.         answer,
  2100.         answer = Integrate`MeijerPrelimCase[ 1-m,1-q,1-n,1-p,1/arg ];
  2101.         If[ FreeQ[answer,Integrate`FailInt], 
  2102.             answer,
  2103.             GfunToHyper[ n,p,m,q,arg ] 
  2104.         ]
  2105.     ]
  2106.   ]
  2107.  
  2108.  GfunToHyper[ n_, p_, {v1___, b_, v2___}, {v3___, a_, v4___}, arg_] :=
  2109.     (-1)^(a-b) GfunToHyper[ n,p,{a,v1,v2},{b,v3,v4},arg ] /;
  2110.  IntegerQ[Expand[a-b-1]] && NonNegative[Expand[a-b-1]]
  2111.  
  2112.  GfunToHyper[ n_, p_, m_, q_, arg_ ] := 
  2113.     If[ Length[n] + Length[p] == Length[m] + Length[q] && Length[n]!=0,
  2114.         GfunToHyper[ 1-m, 1-q, 1-n, 1-p, 1/arg],
  2115.     0
  2116.     ]/; Length[ m ] == 0
  2117.     
  2118.  GfunToHyper[ n_, p_, m_/;!LogarithmCase[ m ], q_, arg_ ] := 
  2119.     Sum[ FinalGfunToHyper[ m[[i]],n,p,Drop[ m,{i,i} ],q,arg],
  2120.             { i,Length[m] } 
  2121.     ]   
  2122.  
  2123.  GfunToHyper[ n_, p_, m_, q_, arg_ ] := 
  2124.    Module[ { answer },
  2125.     answer = Integrate`MeijerLogCase[n,p,m,q,arg ];
  2126.     If[ FreeQ[answer,Integrate`FailInt],
  2127.         answer/.{Integrate`sum :> Sum, Integrate`dummy :> Integrate`var},
  2128.         FailODE
  2129.     ]
  2130.    ]
  2131.  
  2132.  FinalGfunToHyper[ w_, n_, p_, m_, q_, arg_ ] :=
  2133.     arg^Together[w] *
  2134.     Module[ { ww },
  2135.        SimplifyGamma[Limit[ SimplifyGamma[
  2136.             MultGamma[1 + ww - n] * MultGamma[m - ww] /
  2137.             (MultGamma[p - ww]  MultGamma[1 + ww - q]) ], 
  2138.         ww -> w]]
  2139.     ]*
  2140.     HypergeometricPFQ[ Expand[1 + w - Join[n,p]],
  2141.                  Expand[1 + w - Join[m,q]],
  2142.                  arg (-1)^(Length[p]-Length[m]+1) ]
  2143.  
  2144.  LogarithmCase[ {___, v_ ,___, u_, ___} ] := True /; IntegerQ[Expand[u-v]]
  2145.  
  2146.  LogarithmCase[ {___} ] := False
  2147.  
  2148.  ReducePar[ {w1___, u_, w2___},{w3___, v_, w4___} ] := 
  2149.     ReducePar[ {w1, w2},{w3, w4} ] /; 
  2150.  u===v
  2151.  
  2152.  ReducePar[ w1_, w2_ ] := { w1, w2 }
  2153.  
  2154.  MultGamma[a_] := Times@@( Gamma[#]&/@Expand[a] ) 
  2155.  MultPochham[a_List,l_] := Times@@( Pochhammer[#,l]&/@Expand[a] ) 
  2156.  
  2157.  BesselComplexArg =
  2158.  {
  2159.    BesselJ[ v_, w_. Complex[0, a_] ] :> Exp[Pi v I/2] BesselI[v,a w],
  2160.    BesselI[ v_, w_. Complex[0, a_] ] :> Exp[-Pi v I/2] BesselJ[v,-a w]
  2161.  }
  2162.  
  2163.  ChangeVariables[ equation_, y_, x_, xnew_, expr_?(Less[Depth[#],9]&) ] :=
  2164.    equation/.
  2165.    {
  2166.    c_. Derivative[a_][y][x] :> 
  2167.    (c/.x->expr) DerivativeD[a, y, x, xnew, expr ],
  2168.    c_. y[x] :> (c/.x->expr) y[xnew]
  2169.    }/.
  2170.    Equal[ a_, b_] :> Equal[ Expand[a], Expand[b] ]/.InversTrig  
  2171.  
  2172.  ChangeVariables[ __ ] := FailODE
  2173.  
  2174.  DerivativeD[1, y_, x_, xnew_, expr_ ] := 
  2175.  Derivative[1][y][xnew]/D[expr,xnew]
  2176.  
  2177.  DerivativeD[ n_, y_, x_, xnew_, expr_ ] := 
  2178.  D[ DerivativeD[n-1,y,x,xnew,expr], xnew]/D[expr,xnew]
  2179.  
  2180.  InversTrig = {
  2181.   Cos[ArcSin[w_]]^m_. :> (1-w^2)^(m/2),
  2182.   Cos[ArcTan[w_]]^m_. :> 1/(1+w^2)^(m/2),
  2183.   Sec[ArcCos[w_]]^m_. :> w^(-m),
  2184.   Sec[ArcSin[w_]]^m_. :> (1-w^2)^(-m/2),
  2185.   Sec[ArcTan[w_]]^m_. :> (1+w^2)^(m/2),
  2186.   Sin[ArcCos[w_]]^m_. :> (1-w^2)^(m/2),
  2187.   Sin[ArcTan[w_]]^m_. :> w^m/(1+w^2)^(m/2),
  2188.   Csc[ArcSin[w_]]^m_. :> w^(-m),
  2189.   Csc[ArcCos[w_]]^m_. :> (1-w^2)^(-m/2),
  2190.   Csc[ArcTan[w_]]^m_. :> (1+w^2)^(m/2)/w^m,
  2191.   Tan[ArcSin[w_]]^m_. :> w^m/(1-w^2)^(m/2),
  2192.   Tan[ArcCos[w_]]^m_. :> (1-w^2)^(m/2)/w^m,
  2193.   Cot[ArcCos[w_]]^m_. :> w^m/(1-w^2)^(m/2),
  2194.   Cot[ArcSin[w_]]^m_. :> (1-w^2)^(m/2)/w^m,
  2195.   ArcTan[Tan[w_]]^m_. :> w^m
  2196.         }
  2197.  
  2198.  Kummer[ r1_ f_[x_] + r2_ Derivative[1][f_][x_] + 
  2199.      r3_ Derivative[2][f_][x_], f_[x_], x_, w_ ] :=
  2200.    Module[ { a1, b1, a2, b2, a3, b3 },
  2201.      {a1,b1} = r1/.a_. + b_. x :> {a,b}/;FreeQ[{a,b},x];
  2202.      {a2,b2} = r2/.a_. + b_. x :> {a,b}/;FreeQ[{a,b},x];
  2203.      {a3,b3} = r3/.a_. + b_. x :> {a,b}/;FreeQ[{a,b},x];
  2204.    {{f[x]->E^(-(b2*x)/(2*b3) - (((b2^2 - 4*b1*b3)/b3^2)^(1/2)*(a3/b3 + x))/2 + 
  2205.       ((a3*b2 - a2*b3)*Log[a3 + b3*x])/(2*b3^2))*
  2206.    (a3/b3 + x)^((1 + ((a3*b2 - a2*b3 + b3^2)^2/b3^4)^(1/2))/2)*
  2207.    (w[1]*HypergeometricU[(-(a3*b2^2) + 2*a3*b1*b3 + a2*b2*b3 - 2*a1*b3^2)/
  2208.          (2*b3^3*((b2^2 - 4*b1*b3)/b3^2)^(1/2)) + 
  2209.         (1 + ((a3*b2 - a2*b3 + b3^2)^2/b3^4)^(1/2))/2, 
  2210.        1 + ((a3*b2 - a2*b3 + b3^2)^2/b3^4)^(1/2), 
  2211.        ((b2^2 - 4*b1*b3)/b3^2)^(1/2)*(a3/b3 + x)] + 
  2212.      w[2]*Hypergeometric1F1[(-(a3*b2^2) + 2*a3*b1*b3 + a2*b2*b3 - 
  2213.            2*a1*b3^2)/(2*b3^3*((b2^2 - 4*b1*b3)/b3^2)^(1/2)) + 
  2214.         (1 + ((a3*b2 - a2*b3 + b3^2)^2/b3^4)^(1/2))/2, 
  2215.        1 + ((a3*b2 - a2*b3 + b3^2)^2/b3^4)^(1/2), 
  2216.        ((b2^2 - 4*b1*b3)/b3^2)^(1/2)*(a3/b3 + x)]) }}/;b2^2 - 4*b1*b3=!=0
  2217.    ]/;
  2218.  MatchQ[r1, a_. + b_. x/;FreeQ[{a,b},x] ] &&
  2219.  MatchQ[r2, a_. + b_. x/;FreeQ[{a,b},x] ] &&
  2220.  MatchQ[r3, a_. + b_. x/;FreeQ[{a,b},x] ] 
  2221.  
  2222.  Kummer[ r1_ f_[x_] + a2_. Derivative[1][f_][x_] + 
  2223.      r3_ Derivative[2][f_][x_], f_[x_], x_, w_ ] :=
  2224.    Module[ { a1, b1, a3, b3 },
  2225.      {a1,b1} = r1/.a_. + b_. x :> {a,b}/;FreeQ[{a,b},x];
  2226.      {a3,b3} = r3/.a_. + b_. x :> {a,b}/;FreeQ[{a,b},x];
  2227.   {{f[x] -> E^(-(((-4*b1)/b3)^(1/2)*(a3/b3 + x))/2 - 
  2228.          (a2*Log[a3 + b3*x])/(2*b3))*
  2229.       (a3/b3 + x)^((1 + ((-(a2*b3) + b3^2)^2/b3^4)^(1/2))/2)*
  2230.       (w[1]*HypergeometricU[(2*a3*b1*b3 - 2*a1*b3^2)/
  2231.             (2*((-4*b1)/b3)^(1/2)*b3^3) + 
  2232.            (1 + ((-(a2*b3) + b3^2)^2/b3^4)^(1/2))/2, 
  2233.           1 + ((-(a2*b3) + b3^2)^2/b3^4)^(1/2), 
  2234.           ((-4*b1)/b3)^(1/2)*(a3/b3 + x)] + 
  2235.         w[2]*Hypergeometric1F1[(2*a3*b1*b3 - 2*a1*b3^2)/
  2236.             (2*((-4*b1)/b3)^(1/2)*b3^3) + 
  2237.            (1 + ((-(a2*b3) + b3^2)^2/b3^4)^(1/2))/2, 
  2238.           1 + ((-(a2*b3) + b3^2)^2/b3^4)^(1/2), 
  2239.           ((-4*b1)/b3)^(1/2)*(a3/b3 + x)])}}
  2240.    ]/;
  2241.  MatchQ[r1, a_. + b_. x/;FreeQ[{a,b},x] ] &&
  2242.  MatchQ[r3, a_. + b_. x/;FreeQ[{a,b},x] ] && FreeQ[ a2, x]
  2243.  
  2244.  Kummer[ ___ ] := FailODE
  2245.  
  2246.  Unprotect[ HypergeometricU ]
  2247.  
  2248.  HypergeometricU[ a_, b_, x_ ] := 
  2249.    Simplify[Pi^(-1/2) Exp[x/2] x^(-a+1/2) BesselK[ a-1/2, x/2 ]] /;
  2250.  Expand[2 a - b] === 0
  2251.  
  2252.  Protect[ HypergeometricU ]
  2253.  
  2254. (*========================================================================*)
  2255.  
  2256.  SetAttributes[DSolve, { ReadProtected, Protected } ];
  2257.  
  2258.  End[ ] (* "Calculus`DSolve`Private` *)
  2259.  
  2260.  EndPackage[ ] (* "Calculus`DSolve`" *)
  2261.  
  2262.