
(*****************************************************************************
*                 For the Trigonometric Integrand
*
******************************************************************************)

 TaylorSeriesTrig[alfa_,{dg_,True},f_,oldf_,x_] :=
   Module[ {s=s,c=c,var,dg2,dg3,cond},      
    dg2 = Exponent[f//.{Sin[z_] :>0,Cos[z_] :>0},x ];
    dg3 = Exponent[f//.{Sin[z_] :>s,Cos[z_] :>c},x];
    dg3 = If[ dg3===-Infinity,0,dg3]; 
    cond =Re[alfa] > -NotZeroTerm[f,dg 3,x] &&
    If[ dg2===-Infinity, 
        dg-dg3 > Re[alfa],
        -Max[dg3- dg,dg2] > Re[alfa] ];
    If[ cond =!= False, 
      Off[Power::infy,Infinity::indet,General::dbyz,On::none];
      If[ cond === True ,
          dg2 = EvalTrig[1,var, InputTrig[f,x]//Expand,x ];
          dg3 = dg2/.var->alfa;
          If[ dg3=!= ComplexInfinity && dg3 =!= Indeterminate, 
              dg2=dg3,
              dg2 = CoefNotZeroTerm[
	      	      PowerExpand2[LimitSum[Expand[dg2],var,alfa],{var}],
		      1,var,alfa] 
            ],
          dg3 = EvalTrig[1,var,InputTrig[f,x]//Expand,x];
          dg2 = dg3/.var->alfa 
       ];
      On[Power::infy,Infinity::indet,General::dbyz,On::none]
      ]; 
    If[ cond === False, FailIntDiv, trigintegral=True; dg2 ]
   ] 

 TaylorSeriesTrig[alfa_,a_,f_,oldf_,x_] := GGfunctions[alfa,1,oldf,x]

 NotZeroTerm[f_,n_,x_] :=
  Module[ {q=0,i=0},
   Off[ Series::serlim,Series::esss ];
   While[ q===0,
      q = Expand[Normal[Series[f,{x,0,n+i}]]];
      i=i+2 ];
   On[ Series::serlim,Series::esss ];
   ExponentFirst[ q,x ] /; FreeQ[q,Series]
 ]

 NotZeroTerm[f_,n_,x_] := False

 EvalTrig[done_,alfa_,f_Plus,x_] := Map[EvalTrig[done,alfa,#,x]&,f]

 EvalTrig[1,alfa_,f_. MeijerG[w__],x_] := EvalTrig[MeijerG[w],alfa,f,x]
 
 EvalTrig[1,alfa_,c_,x_] := 0/;FreeQ[c,x]

 EvalTrig[1,alfa_,c_. x_^n_.,x_] := 0/;FreeQ[c,x]

 EvalTrig[done_,alfa_,x_^n_. f_.,x_] := EvalTrig[done,alfa+n,f,x]
 
 EvalTrig[ MeijerG[ n_,p_,m_,q_,{k_,mult_. x_^dg_.}],alfa_,c_, x_ ] :=
   c If[ NumberQ[dg] && dg < 0,
        EvalTrig2[ 1-m,1-q,1-n,1-p,1/mult,alfa/(-dg),x ]/(-dg),
        EvalTrig2[ n,p,m,q,mult,alfa/dg,x ]/dg ] /;
    FreeQ[mult,x] && FreeQ[dg,x] && FreeQ[c,x]

 EvalTrig[ __ ] := FailInt

 EvalTrig2[ n_,p_,m_,q_,mult_,dg_,x_ ] := 
     MultGamma[ dg + m ] MultGamma[ 1 - dg - n ] /
    (MultGamma[ dg + p ] MultGamma[ 1 - dg - q ] mult^dg)

 ExponentFirst[p_Plus,x_] := Exponent[ First[ p ] ,x]

 ExponentFirst[p_,x_] := Exponent[ p,x ]

 LimitSum[ f_,n_,x_,s_] := x FailInt/;Not[FreeQ[f,FailInt]]

 LimitSum[ f_Plus,x_,s_] := Map[LimitSum[#,x,s]&,f]

 LimitSum[Times[v1___,Literal[HypergeometricPFQ][up_,low_,arg_],v2___],x_,s_] :=
    (HypergeometricPFQ[up,low,arg]//.x->s) LimitSum[v1 v2,x,s]/;
  And@@(Not[CondLim[#/.x->s]]&/@Join[up,low])

 LimitSum[Gamma[u_]^n_.,x_,s_] := 
   LimitSum[Gamma[u+1]^n/u^n,x,s]/;CondLim[u//.x->s]  

 LimitSum[Times[v1___,Gamma[u_]^n_.,v2___],x_,s_] := 
   LimitSum[v1 v2 Gamma[u+1]^n/u^n ,x,s]/;CondLim[u//.x->s]  

 LimitSum[Times[v1___,Gamma[u_,0,a_]^n_.,v2___],x_,s_] := 
   LimitSum[Expand[v1 v2 (Gamma[u+1,0,a]/u + a^u E^(-a)/u)^n],x,s]/;
 CondLim[u//.x->s]  
 
 LimitSum[Times[v1___,Gamma[u_,a_]^n_.,v2___],x_,s_] := 
   LimitSum[Expand[v1 v2 (Gamma[u+1,a] + a^u E^(-a)/Gamma[u+1])^n],x,s]/;
 CondLim[u//.x->s]  

 LimitSum[PolyGamma[k_,u_]^n_.,x_,s_] := 
    LimitSum[Expand[(PolyGamma[k,u+1]-(-1)^k k! u^(-k-1))^n],x,s]/;
  CondLim[u//.x->s] 

 LimitSum[Times[v1___,PolyGamma[k_,u_]^n_.,v2___],x_,s_] := 
    LimitSum[Expand[v1 v2 (PolyGamma[k,u+1]-(-1)^k k! u^(-k-1))],x,s]/;
  CondLim[u//.x->s] 

 LimitSum[Times[v1___,Hypergeometric[up_,low_,arg_],v2___],x_,s_] :=
    (Hypergeometric[up,low,arg]//.x->s) LimitSum[v1 v2,x,s]/;
  And@@(Not[CondLim[#/.x->s]]&/@Join[up,low])

 LimitSum[Times[v1___,Literal[Hypergeometric2F1][a_,b_,c_,arg_],v2___],x_,s_] :=
    (Hypergeometric2F1[a,b,c,arg]//.x->s) LimitSum[v1 v2,x,s]/;
  And@@(Not[CondLim[#/.x->s]]&/@{a,b,c})

 LimitSum[Times[v1___,Literal[Hypergeometric1F1][a_,b_,arg_],v2___],x_,s_] :=
    (Hypergeometric1F1[a,b,arg]//.x->s) LimitSum[v1 v2,x,s]/;
  And@@(Not[CondLim[#/.x->s]]&/@{a,b})

 LimitSum[Times[v1___,Literal[HypergeometricPFQ][up_,low_,arg_],v2___],x_,s_] :=
   If[ FreeQ[low,x],
    LimitSum[Expand[v1 v2 SeriesForHyperFun[up,low,arg,
      Min[Select[up//.x->s,IntegerQ[#] && #<=0&]],x,s ]],x,s],
    low FailInt]
 
 LimitSum[Times[v1___,Hypergeometric[up_,low_,arg_],v2___],x_,s_] :=
   If[ FreeQ[low,x],
    LimitSum[Expand[v1 v2 SeriesForHyperFun[up,low,arg,
      Min[Select[up//.x->s,IntegerQ[#] && #<=0&]],x,s ]],x,s],
   low FailInt]

 LimitSum[Times[v1___,Literal[Hypergeometric2F1][a_,b_,c_,arg_],v2___],x_,s_] :=
   If[ FreeQ[c,x],
    LimitSum[Expand[v1 v2 SeriesForHyperFun[{a,b},{c},arg,
      Min[Select[{a,b}//.x->s,IntegerQ[#] && #<=0&]],x,s ]],x,s],
    c FailInt]

 LimitSum[Times[v1___,Literal[Hypergeometric1F1][a_,b_,arg_],v2___],x_,s_] :=
   If[ FreeQ[b,x],
    LimitSum[Expand[v1 v2 SeriesForHyperFun[{a},{b},arg,
      Min[Select[{a}//.x->s,IntegerQ[#] && #<=0&]],x,s ]],x,s],
    b FailInt]

 LimitSum[f_,x_,s_] := f

 SeriesForHyperFun[up_,low_,arg_,k_Integer,x_,s_] :=
    Sum[MultPochham[up,i] arg^i/((MultPochham[low,i]//.x->s) i!),
    {i,0,k+1}]

 SeriesForHyperFun[up_,low_,arg_,k_,x_,s_] :=
    (HypergeometricPFQ[up,low,arg]//.x->s)

 SeriesForHyperFun[ __ ] := FailInt

 CondLim[u_] := IntegerQ[u] && u <= 0

