/* Test cases for ode1_riccati.mac functions */

/* Test cases with n=2*a   ode1_riccati_special(a,b,c,n,y,x)*/
eq:x*'diff(y,x)-a*y+b*y^2=c*x^n;
x*'diff(y,x)-a*y+b*y^2=c*x^n;

/* Case a.i.1 n=2*a, b>0, c>0 */
(print("Special Riccati equation: Case a.i.1: n=2*a, b>0, c>0"),done);
done;
ans:ode1_riccati_special(a:1,b:1,c:1,n:2,y,x);
y=x*tanh(x+%c);
ode_check(eq,ans);
0;
ans:ode1_riccati(eqn:x*'diff(y,x)-y+y^2=x^2,y,x);
y=x*tanh(x+%c);
ode_check(eqn,ans);
0;
ans:ode1_riccati_special(a:2,b:2,c:3,n:4,y,x);
y=sqrt(3)*x^2*tanh(sqrt(6)*x^2/2+%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:3,b:2,c:3,n:6,y,x);
y=sqrt(3)*x^3*tanh(sqrt(6)*x^3/3+%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:-1,b:2,c:3,n:-2,y,x);
y=-sqrt(3)*tanh(sqrt(6)/x-%c)/(sqrt(2)*x);
radcan(ode_check(eq,ans));
0;

/* Case a.i.2 n=2*a, b<0, c<0 */
(print("Special Riccati equation: Case a.i.2: n=2*a, b<0, c<0"),done);
done;
ans:ode1_riccati_special(a:1,b:-1,c:-1,n:2,y,x);
y=-x*tanh(x-%c);
ode_check(eq,ans);
0;
ans:ode1_riccati(eqn:x*'diff(y,x)-y-y^2=-x^2,y,x);
y=-x*tanh(x-%c);
ode_check(eqn,ans);
0;
ans:ode1_riccati_special(a:2,b:-2,c:-3,n:4,y,x);
y=-sqrt(3)*x^2*tanh(sqrt(6)*x^2/2-%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:3,b:-2,c:-3,n:6,y,x);
y=-sqrt(3)*x^3*tanh(sqrt(6)*x^3/3-%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:-1,b:-2,c:-3,n:-2,y,x);
y=sqrt(3)*tanh(sqrt(6)/x+%c)/(sqrt(2)*x);
radcan(ode_check(eq,ans));
0;

/* Case a.i.3 n=2*a, b>0, c<0 */
(print("Special Riccati equation: Case a.i.3: n=2*a, b>0, c<0"),done);
done;
ans:ode1_riccati_special(a:1,b:1,c:-1,n:2,y,x);
y=-x*tan(x-%c);
trigsimp(ode_check(eq,ans));
0;
ans:ode1_riccati(eqn:x*'diff(y,x)-y+y^2=-x^2,y,x);
y=-x*tan(x-%c);
trigsimp(ode_check(eqn,ans));
0;
ans:ode1_riccati_special(a:2,b:2,c:-3,n:4,y,x);
y=-sqrt(3)*x^2*tan(sqrt(6)*x^2/2-%c)/sqrt(2);
radcan(trigsimp(ode_check(eq,ans)));
0;
ans:ode1_riccati_special(a:3,b:2,c:-3,n:6,y,x);
y=-sqrt(3)*x^3*tan(sqrt(6)*x^3/3-%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:-1,b:2,c:-3,n:-2,y,x);
y=sqrt(3)*tan(sqrt(6)/x+%c)/(sqrt(2)*x);
radcan(ode_check(eq,ans));
0;

/* Case a.i.4 n=2*a, b<0, c>0 */
(print("Special Riccati equation: Case a.i.4: n=2*a, b<0, c>0"),done);
done;
ans:ode1_riccati_special(a:1,b:-1,c:1,n:2,y,x);
y=x*tan(x+%c);
ode_check(eq,ans);
0;
ans:ode1_riccati(eqn:x*'diff(y,x)-y-y^2=x^2,y,x);
y=x*tan(x+%c);
ode_check(eqn,ans);
0;
ans:ode1_riccati_special(a:2,b:-2,c:3,n:4,y,x);
y=sqrt(3)*x^2*tan(sqrt(6)*x^2/2+%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:3,b:-2,c:3,n:6,y,x);
y=sqrt(3)*x^3*tan(sqrt(6)*x^3/3+%c)/sqrt(2);
radcan(ode_check(eq,ans));
0;
ans:ode1_riccati_special(a:-1,b:-2,c:3,n:-2,y,x);
y=-sqrt(3)*tan(sqrt(6)/x-%c)/(sqrt(2)*x);
radcan(ode_check(eq,ans));
0;
kill(a,b,c,n);
done;

/* Check that the sign of b and c is handled correctly */
assume(b>0,c>0);
[b>0,c>0];
ode1_riccati_special(2,b,c,4,y,x);
y=sqrt(c)*x^2*tanh(sqrt(b)*sqrt(c)*x^2/2+%c)/sqrt(b);
ans:ode1_riccati(eqn:x*'diff(y,x)-2*y+b*y^2=c*x^4,y,x);
y=sqrt(c)*x^2*tanh(sqrt(b)*sqrt(c)*x^2/2+%c)/sqrt(b);
ode_check(eqn,ans);
0;
forget(b>0,c>0);
[b>0,c>0];

assume(b<0,c<0);
[b<0,c<0];
ans:ode1_riccati_special(2,b,c,4,y,x);
y=-sqrt(-c)*x^2*tanh(sqrt(-b)*sqrt(-c)*x^2/2-%c)/sqrt(-b);
ans:ode1_riccati(eqn:x*'diff(y,x)-2*y+b*y^2=c*x^4,y,x);
y=-sqrt(-c)*x^2*tanh(sqrt(-b)*sqrt(-c)*x^2/2-%c)/sqrt(-b);
ode_check(eqn,ans);
0;
forget(b<0,c<0);
[b<0,c<0];

assume(b>0,c<0);
[b>0,c<0];
ode1_riccati_special(2,b,c,4,y,x);
y=-sqrt(-c)*x^2*tan(sqrt(b)*sqrt(-c)*x^2/2-%c)/sqrt(b);
ans:ode1_riccati(eqn:x*'diff(y,x)-2*y+b*y^2=c*x^4,y,x);
y=-sqrt(-c)*x^2*tan(sqrt(b)*sqrt(-c)*x^2/2-%c)/sqrt(b);
ode_check(eqn,ans);
0;
forget(b>0,c<0);
[b>0,c<0];

assume(b<0,c>0);
[b<0,c>0];
ode1_riccati_special(2,b,c,4,y,x);
y=sqrt(c)*x^2*tan(sqrt(-b)*sqrt(c)*x^2/2+%c)/sqrt(-b);
ans:ode1_riccati(eqn:x*'diff(y,x)-2*y+b*y^2=c*x^4,y,x);
y=sqrt(c)*x^2*tan(sqrt(-b)*sqrt(c)*x^2/2+%c)/sqrt(-b);
ode_check(eqn,ans);
0;
forget(b<0,c>0);
[b<0,c>0];

/* Tests for ode1_riccati_special - Case ii: (n-2*a)/2n=k , positive integer */
/* k=2 */
ans:ode1_riccati_special(a:1,b:1,c:1,n:-2/3,y,x);
y=1/((1/3-1/(tanh(3/x^(1/3)-%c)*x^(1/3)))*x^(2/3))+1;
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:2,b:1,c:1,n:-4/3,y,x);
y=1/((2/3-1/(tanh(3/(2*x^(2/3))-%c)*x^(2/3)))*x^(4/3))+2;
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:-3/2,b:1,c:1,n:1,y,x);
y=x/(sqrt(x)/tanh(2*sqrt(x)+%c)-1/2)-3/2;
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:3/2,b:1,c:1,n:-1,y,x);
y=1/((1/2-1/(tanh(2/sqrt(x)-%c)*sqrt(x)))*x)+3/2;
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:-3/2,b:4,c:9,n:1,y,x);
y=x/(2*sqrt(x)/(3*tanh(12*sqrt(x)+%c))-1/18)-3/8;
ode_check(eq,ans);
0;

/* Tests for ode1_riccati_special - Case iii: (n+2*a)/2n=k , positive integer */
/* k=2 */
ans:ode1_riccati_special(a:1,b:1,c:1,n:2/3,y,x);
y=x^(2/3)/(x^(1/3)/tanh(3*x^(1/3)+%c)-1/3);
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:2,b:1,c:1,n:4/3,y,x);
y=x^(4/3)/(x^(2/3)/tanh(3*x^(2/3)/2+%c)-2/3);
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:3/2,b:1,c:1,n:1,y,x);
y = x/(sqrt(x)/tanh(2*sqrt(x)+%c)-1/2);
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:3,b:1,c:1,n:2,y,x);
y = x^2/(x/tanh(x+%c)-1);
ode_check(eq,ans);
0;
ans:ode1_riccati_special(a:3,b:-9,c:4,n:2,y,x);
y=x^2/(3*x/(2*tan(6*x+%c))-1/4);
ode_check(eq,ans);
0;

/* Tests for ode1_riccati_special - Case c: not integrable  a#0 */

/* Bessel function of integer order - easy to check */
ans:ode1_riccati_special(a:1,b:-1,c:1,n:1,y,x);
y=(((bessel_y(2,2*sqrt(x))-bessel_y(0,2*sqrt(x)))*%c
    +bessel_j(2,2*sqrt(x))-bessel_j(0,2*sqrt(x)))*sqrt(x)
    -bessel_y(1,2*sqrt(x))*%c-bessel_j(1,2*sqrt(x)))
 /(2*bessel_y(1,2*sqrt(x))*%c+2*bessel_j(1,2*sqrt(x)));
ode_check(eq,ans);
0;

ans:ode1_riccati_special(a:1,b:-2,c:3,n:1,y,x);
y = (((sqrt(6)*bessel_y(2,2*sqrt(6)*sqrt(x))
         -sqrt(6)*bessel_y(0,2*sqrt(6)*sqrt(x)))
         *%c
         +sqrt(6)*bessel_j(2,2*sqrt(6)*sqrt(x))
         -sqrt(6)*bessel_j(0,2*sqrt(6)*sqrt(x)))
         *sqrt(x)
         -bessel_y(1,2*sqrt(6)*sqrt(x))*%c-bessel_j(1,2*sqrt(6)*sqrt(x)))
         /(4*bessel_y(1,2*sqrt(6)*sqrt(x))*%c
          +4*bessel_j(1,2*sqrt(6)*sqrt(x)));
ode_check(eq,ans);
0;

assume(x>0);
[x>0];
ans:ode1_riccati_special(a:2,b:-2,c:3,n:2,y,x);
y = (((sqrt(2)*sqrt(3)*bessel_y(2,2*sqrt(3)*x/sqrt(2))
      -sqrt(2)*sqrt(3)*bessel_y(0,2*sqrt(3)*x/sqrt(2)))*%c
      +sqrt(2)*sqrt(3)*bessel_j(2,2*sqrt(3)*x/sqrt(2))
      -sqrt(2)*sqrt(3)*bessel_j(0,2*sqrt(3)*x/sqrt(2)))*x
      -2*bessel_y(1,2*sqrt(3)*x/sqrt(2))*%c
      -2*bessel_j(1,2*sqrt(3)*x/sqrt(2)))
 /(4*bessel_y(1,2*sqrt(3)*x/sqrt(2))*%c+4*bessel_j(1,2*sqrt(3)*x/sqrt(2)));
ode_check(eq,ans);
0;
forget(x>0);
[x>0];

/* Cases that end up in ode1_riccati_original with m=-2 */
ans:ode1_riccati_special(a:1,b:-1,c:1,n:0,y,x);
y = -(((%c-sqrt(3))*sin(sqrt(3)*log(x)/2)+(sqrt(3)*%c+1)*cos(sqrt(3)*log(x)/2))
/(2*%c*sin(sqrt(3)*log(x)/2)+2*cos(sqrt(3)*log(x)/2)));
ode_check(eq,ans);
0;

ans:ode1_riccati_special(a:1,b:1,c:1,n:0,y,x);
y=((sqrt(5)+1)*x^sqrt(5)+(1-sqrt(5))*%c)/(2*x^sqrt(5)+2*%c);
ode_check(eq,ans);
0;

ans:ode1_riccati_special(a:1,b:-1/4,c:1,n:0,y,x);
y=-((2*%c*log(x)+4*%c+2)/(%c*log(x)+1));
ode_check(eq,ans);
0;

/* Cases with a=0 and b*c<0 */
ans:ode1_riccati_special(a:0,b:-1,c:1,n:2,y,x);
y=(bessel_y(1,x)*%c+bessel_j(1,x))*x/(bessel_y(0,x)*%c+bessel_j(0,x));
ode_check(eq,ans);
0;

ans:ode1_riccati_special(a:0,b:-1,c:1,n:1,y,x);
y=(bessel_y(1,2*sqrt(x))*%c+bessel_j(1,2*sqrt(x)))*sqrt(x)/(bessel_y(0,2*sqrt(x))*%c+bessel_j(0,2*sqrt(x)));
ode_check(eq,ans);
0;

/* Cases with a=0 and b*c>0 */
ans:ode1_riccati_special(a:0,b:1,c:1,n:2,y,x);
y=-((bessel_k(1,x)*%c-bessel_i(1,x))*x/(bessel_k(0,x)*%c+bessel_i(0,x)));
ode_check(eq,ans);
0;

ans:ode1_riccati_special(a:0,b:1,c:1,n:1,y,x);
y=-((bessel_k(1,2*sqrt(x))*%c-bessel_i(1,2*sqrt(x)))*sqrt(x)/(bessel_k(0,2*sqrt(x))*%c+bessel_i(0,2*sqrt(x))));
ode_check(eq,ans);
0;

kill(a,b,c,n);
done;

/* some tests for ode1_riccati_original */
eqn:'diff(y,x)+b*y^2=c*x^m;
'diff(y,x)+b*y^2=c*x^m;
/* Case  m*(2*k+1)+4*k=0 and k=0 => m=0 
   also  m*(2*k-1)+4*k=0 and k=0 => m=0 */
ans:ode1_riccati_original(b:4,c:1,m:0,y,x);
y=tanh(2*x+%c)/2;
ode_check(eqn,ans);
0;
ans:ode1_riccati_original(b:-4,c:1,m:0,y,x);
y=tan(2*x+%c)/2;
ode_check(eqn,ans);
0;
ans:ode1_riccati_original(b:4,c:-1,m:0,y,x);
y=-tan(2*x-%c)/2;
ode_check(eqn,ans);
0;
ans:ode1_riccati_original(b:-4,c:-1,m:0,y,x);
y=-tanh(2*x-%c)/2;
ode_check(eqn,ans);
0;

/* Case m*(2*k+1)+4*k=0, k positive integer */
/* m=-4/3  k=1 */
ans:ode1_riccati_original(b:1,c:1,m:-4/3,y,x);
y=3*tanh(3*x^(1/3)+%c)/(3*x^(2/3)-tanh(3*x^(1/3)+%c)*x^(1/3));
ode_check(eqn,ans);
0;
/* m=-8/5  k=2 */
ans:ode1_riccati_original(b:-1,c:1,m:-8/5,y,x);
y=-((25*x^(1/5)-5*tan(5*x^(1/5)-%c))/(25*tan(5*x^(1/5)-%c)*x+15*x^(4/5)-3*tan(5*x^(1/5)-%c)*x^(3/5)));
/* This is correct - checking takes too long for testsuite */
/* m=-12/7  k=3 */
ans:ode1_riccati_original(b:1,c:1,m:-12/7,y,x);
y=(343*tanh(7*x^(1/7)+%c)*x^(2/7)-147*x^(1/7)+21*tanh(7*x^(1/7)+%c))/(343*x^(8/7)-294*tanh(7*x^(1/7)+%c)*x+105*x^(6/7)-15*tanh(7*x^(1/7)+%c)*x^(5/7));
/* This is correct - checking takes too long for testsuite */

/* Case m*(2*k-1)+4*k=0, k positive integer */
/* m=-4, k=1 */
ans:ode1_riccati_original(b:1,c:1,m:-4,y,x);
y=(x*tanh((%c*x-1)/x)+1)/(x^2*tanh((%c*x-1)/x));
ode_check(eqn,ans);
0;
/* m=-8/3 k=2 */
ans:ode1_riccati_original(b:1,c:1,m:-8/3,y,x);
y=(tanh((%c*x^(1/3)-3)/x^(1/3))*x+3*x^(2/3)+3*tanh((%c*x^(1/3)-3)/x^(1/3))*x^(1/3))/(tanh((%c*x^(1/3)-3)/x^(1/3))*x^2+3*x^(5/3));
ode_check(eqn,ratsimp(exponentialize(ans)));
0;
/* m=-12/5 k=3 */
ans:ode1_riccati_original(b:1,c:1,m:-12/5,y,x);
y=(3*tanh((%c*x^(1/5)-5)/x^(1/5))*x^(3/5)+15*x^(2/5)+30*tanh((%c*x^(1/5)-5)/x^(1/5))*x^(1/5)+25)/(3*tanh((%c*x^(1/5)-5)/x^(1/5))*x^(8/5)+15*x^(7/5)+25*tanh((%c*x^(1/5)-5)/x^(1/5))*x^(6/5));
/* ode_check(eqn,ratsimp(exponentialize(ans)));
0;  eventually */
/* m=-16/7 k=4 */
ans:ode1_riccati_original(b:1,c:1,m:-16/7,y,x);
y=(15*tanh((%c*x^(1/7)-7)/x^(1/7))*x^(4/7)+105*x^(3/7)+315*tanh((%c*x^(1/7)-7)/x^(1/7))*x^(2/7)+490*x^(1/7)+343*tanh((%c*x^(1/7)-7)/x^(1/7)))/(15*tanh((%c*x^(1/7)-7)/x^(1/7))*x^(11/7)+105*x^(10/7)+294*tanh((%c*x^(1/7)-7)/x^(1/7))*x^(9/7)+343*x^(8/7));
/* ode_check(eqn,ratsimp(exponentialize(ans)));
0;  eventually */

/* Bessel function solution - integer order with b*c<0 */
ans:ode1_riccati_original(b:3,c:-2,m:-1,y,x);
y = -((((sqrt(6)*bessel_y(2,2*sqrt(6)*sqrt(x))
       -sqrt(6)*bessel_y(0,2*sqrt(6)*sqrt(x)))*%c
       +sqrt(6)*bessel_j(2,2*sqrt(6)*sqrt(x))
       -sqrt(6)*bessel_j(0,2*sqrt(6)*sqrt(x)))*sqrt(x)
    -bessel_y(1,2*sqrt(6)*sqrt(x))*%c-bessel_j(1,2*sqrt(6)*sqrt(x)))
  /((6*bessel_y(1,2*sqrt(6)*sqrt(x))*%c+6*bessel_j(1,2*sqrt(6)*sqrt(x)))*x));
ode_check(eqn,ans);
0;

/* Bessel function solution - integer order with b*c>0 */
assume(x>0);
[x>0];
ans:ode1_riccati_original(b:3,c:2,m:-1,y,x);
y = -((((sqrt(6)*bessel_k(2,2*sqrt(6)*sqrt(x))
        +sqrt(6)*bessel_k(0,2*sqrt(6)*sqrt(x)))*%c
        -sqrt(6)*bessel_i(2,2*sqrt(6)*sqrt(x))
        -sqrt(6)*bessel_i(0,2*sqrt(6)*sqrt(x)))*sqrt(x)
        -bessel_k(1,2*sqrt(6)*sqrt(x))*%c-bessel_i(1,2*sqrt(6)*sqrt(x)))
  /((6*bessel_k(1,2*sqrt(6)*sqrt(x))*%c
    +6*bessel_i(1,2*sqrt(6)*sqrt(x)))*x));
ode_check(eqn,ans);
0;
forget(x>0);
[x>0];

/* Bessel function solution - non-integer order b*c<0 */
ans:ode1_riccati_original(b:1,c:-1,m:2,y,x);
y = -((((bessel_j(3/4,x^2/2)-bessel_j(-5/4,x^2/2))*%c
       +bessel_j(5/4,x^2/2)-bessel_j(-3/4,x^2/2))*x^2
       -bessel_j(-1/4,x^2/2)*%c-bessel_j(1/4,x^2/2))
    /((2*bessel_j(-1/4,x^2/2)*%c+2*bessel_j(1/4,x^2/2))*x));
ode_check(eqn,ans);
0;

/* Bessel function solution - non-integer order b*c>0 */
assume(x>0);
[x>0];
ans:ode1_riccati_original(b:2,c:3,m:-5,y,x);
y=-(((sqrt(6)*bessel_k(2/3,-(2*sqrt(6)/(3*x^(3/2))))*%c-sqrt(6)*bessel_i(-(4/3),-(2*sqrt(6)/(3*x^(3/2)))))*sqrt(x)-bessel_k(1/3,-(2*sqrt(6)/(3*x^(3/2))))*%c*x^2)/((2*bessel_k(1/3,-(2*sqrt(6)/(3*x^(3/2))))*%c+2*bessel_i(-(1/3),-(2*sqrt(6)/(3*x^(3/2)))))*x^3));
ode_check(eqn,ans);
0;
forget(x>0);
[x>0];

/* Original Riccati equation: m=-2 Case i */
ans:ode1_riccati_original(b:2,c:-3,m:-2,y,x);
y=((%c-sqrt(23))*sin(sqrt(23)*log(x)/2)+(sqrt(23)*%c+1)*cos(sqrt(23)*log(x)/2))
  /(4*%c*x*sin(sqrt(23)*log(x)/2)+4*x*cos(sqrt(23)*log(x)/2));
ode_check(eqn,ans);
0;

/* Original Riccati equation: m=-2 Case ii */
ans:ode1_riccati_original(b:2,c:3,m:-2,y,x);
y=(3*x^5-2*%c)/(2*x^6+2*%c*x);
ode_check(eqn,ans);
0;

/* Original Riccati equation: m=-2 Case iii */
ans:ode1_riccati_original(b:1,c:-1/4,m:-2,y,x);
y=(%c*log(x)+2*%c+1)/(2*%c*x*log(x)+2*x);
ode_check(eqn,ans);
0;

kill(b,c,m);
done;

/* Murphy 249 (3-3) */
ans:contrib_ode(eqn:3*x*'diff(y,x)=3*x^(2/3)+(1-3*y)*y,y,x);
[y=tanh(3*x^(1/3)+%c)*x^(1/3)];
ode_check(eqn,ans[1]);
0;

/* murphy 373  (3-2) */
assume(a>0);
[a>0];
ans:ode1_riccati(eqn:x^4*'diff(y,x)+a^2+x^4*y^2=0,y,x);
y=(x*tan((%c*x-a)/x)+a)/(x^2*tan((%c*x-a)/x));
ode_check(eqn,ans);
0;
forget(a>0);
[a>0];

/* Murphy 168 - Case (a.iii) with k = 2 */
ans:ode1_riccati(eqn:x*'diff(y,x)-y+y^2=x^(2/3),y,x);
y=x^(2/3)/(x^(1/3)/tanh(3*x^(1/3)+%c)-1/3);
ode_check(eqn,ans);
0;
