$include "main.mpl"

kernelopts(assertlevel=2);

with(gfun);
with(NumGfun);

try

# basic test, order 1
rec := {u(n+1) = (n+1)*u(n), u(0) = 1};
res := nth_term(rec,u(n),5); ASSERT(res=120);
res := nth_term(rec,u(n),5,ndseries); ASSERT(res=34);
res :=  rectoproc_binsplit(rec,u(n))(5); ASSERT(res=120);

# trivial
rec := {u(n+1) = u(n), u(0) = 1};
res := nth_term(rec,u(n),100); ASSERT(res=1);
res := nth_term(rec,u(n),100,ndseries); 
p := rectoproc_binsplit(rec,u(n));
for i from 0 to 100 by 17 do ASSERT(p(i)=1) end do;

rec := {u(n+3) = u(n) + u(n+1) + u(n+2), u(0) = 1, u(1) = 1, u(2) = 1};
nth_term(rec,u(n),100);
nth_term(rec,u(n),100,ndseries);
p := rectoproc_binsplit(rec,u(n));

g := proc(s, prec)
  local k, rec, a;
  k := ceil(evalf((prec)*ln(10) + ln((prec)*ln(10)))):
  rec := {u(n+1) = k/(n+s+1)*u(n), u(0) = 1/s}:
  a := fnth_term(rec,u(n),6*k,prec+1,ndseries):
  evalf[prec+1](k^s*exp(-k)*a):
end proc:
p := 300:
r1 := g(5/3,p);
r2 := evalf[p](GAMMA(5/3));
ASSERT(abs(r1-r2) <= 10^(-p));

end;
