(* :Name: Calculus`Limit` *)

(* :Author: Victor S. Adamchik, Summer 1991 *)

(* :Mathematica Version: 2.0 *)

(* :Context: Calculus`Limit` *)

(* :Keywords:
	Limit
*)

(* :Copyright: Copyright 1990-1999, Wolfram Research, Inc.
*)

(* :Summary:
This package provides an enhancement to the built-in Limit.
It allows one to find the limits of expressions containing
elements of a wide class of elementary and special functions.
*)

(* :Requirements: None. *)

(* :Limitations:
	This package contains a development version of code for the
	evaluation of Limits of elementary and special functions.
	It will eventually replace the present Limit code.

	If this package is loaded the internal Limit is overwritten.

	This package cannot evaluate limits of hypergeometric
        and elliptic functions. 
*)

(*========================================================================*)

 Unprotect[Limit]

 BeginPackage["Calculus`Limit`"]

 Begin["`Private`"] 

 messages = Messages[Limit]
 
 If[ Length[ messages] > 1, Limit::usage = Last[messages][[2]]]
 If[ Length[ messages] > 1,Limit::ldir  = messages[[1,2]] ]
 If[ Length[ messages] > 1,Limit::lim   = messages[[2,2]] ]

(* utility function to temporarily turn off messages, variant
   of code by Robby Villegas -- note that this functionality
   should be found shortly in a startup package *)
Attributes[DeactivateMessages] = HoldAll

DeactivateMessages[{msgs___MessageName}, body_] :=
  Module[{wasOn = Select[Hold[msgs], Head[#] =!= $Off &], result},
    CheckAbort[
      Scan[Off, wasOn];
      result = body;
      Scan[On, wasOn];
      result
      ,
      Scan[On, wasOn];
      Abort[]
    ]
  ]

(*========================================================================*)
 
 context = ToString[$Context]
 direction = False

 Limit[ a_/;!FreeQ[Hold[a],Integrate], x_->s_, d___ ] := 
   Module[ {answer},
     analytic = cardFlag = False;
     asymptotic = True; sub={};
     answer = LimitN[Release[Hold[a]/.Integrate->integrate],x->s,d];
     (answer/.sub) /; FreeQ[answer,FailLimit]
   ]   

 Limit[ a_/;!FreeQ[Hold[a],If], x_->s_, d___ ] := 
   Module[ {answer},
     analytic = cardFlag = False;
     asymptotic = True; sub={};
     answer = LimitN[Release[Hold[a]/.If->if],x->s,d];
     (answer/.sub) /; FreeQ[answer,FailLimit]
   ]  

 Limit[ c_. HoldPattern[Nest[w__]], x_->s_, d___ ] := 
  Module[ {answer},
     analytic = cardFlag = False;
     asymptotic = True; sub={};
     answer = If[ FreeQ[{w},x], 
		  Nest[w] * Limit[c,x->s,d],
		  LimitN[c nest[w], x->s, d] ];
     (answer/.sub) /; FreeQ[answer,FailLimit]
   ] 

 Limit[ a__ ] := 
   Module[ {answer},
     analytic = cardFlag = False;
     asymptotic = True; sub={};
     answer = LimitN[a];
     (answer/.sub) /; FreeQ[answer,LimitN] && FreeQ[answer,FailLimit]
   ] /;
  And@@(FreeQ[Hold[a],#]&/@{Integrate,If,Nest})
 
 LimitN[ a___/;Length[{a}]<2 ] := 
   ( Message[Limit::argrx,Limit,Length[{a}],2]; FailLimit )

 LimitN[ a___/;Length[{a}]>2 && FreeQ[{a},Rule] ] := 
   ( Message[Limit::nonopt,Last[{a}],2,HoldForm[Limit[a]]]; FailLimit )

 LimitN[ expr__/;Length[{expr}]==2,Rule[w_,f_] ] :=
   ( Message[Limit::optx,w,HoldForm[Limit[expr,w->f]]]; FailLimit )/;
 w=!=Direction && w=!=Analytic

 LimitN[ a_List,x__ ] := LimitN[#,x]&/@a
   
 LimitN[ expr__, Direction -> Automatic ] := 
   LimitN[expr]

 LimitN[ expr__, Analytic -> w_ ] := 
   Module[ {answer},
    If[ w,analytic = True,analytic = False,
        Message[ Limit::opttf,Analytic,w];
        Return[FailLimit] ];
    answer = LimitN[expr];
    analytic = False;
    answer
   ]
  
 LimitN[ expr_,x_ -> s_, Direction -> w_ ] := 
   (Message[ Limit::ldir,w];
    Limit[expr,x->s,Direction->w] /; Fail ) /;!NumberQ[N[w]]

 LimitN[expr_,x_->s_/;FreeQ[s,DirectedInfinity],Direction -> w_] := 
   Module[ {var,answer},
     direction = True;
     answer = expr/.{x->(s-If[Sign[w]==0,1,Sign[w]] var)}//.{
		Abs[n_?NumberQ z_] :> Abs[n] Abs[z]};
     answer = LimitN[
     If[ FreeQ[answer, Abs],
	 answer,
	 answer/.{Abs[rt_] :> 
	 If[ FreeQ[rt,var], Abs[rt], 
	     sign[0] If[ ZnakLim[Limit[rt,var->0],var->0], -rt, rt]]}
     ], var->0];
     direction = False;
     answer
   ] /; NumberQ[N[w]] 
  
 LimitN[ expr_, x_->s_, Direction -> w_ ] := 
   Module[ {var,answer},
     direction = True;
     answer = 
     If[ Znak[s w],
	 expr/.x->-var,
         expr/.x->var
     ]//.{ Abs[n_?NumberQ z_] :> Abs[n] Abs[z] };
     answer = LimitN[
     If[ FreeQ[answer, Abs],
         answer,  
         answer/.{Abs[rt_] :> 
	 If[ FreeQ[rt,var], Abs[rt], 
	     sign[0] If[ ZnakLim[Limit[rt,var->s],var->s],-rt, rt]]}
     ], var->s];
     direction = False;
     answer
   ] /; NumberQ[N[w]] 

 LimitN[ c_. if[ f_[body__],a__ ], x_->s_ ] :=
   Module[ {r},
     r = Analys[SimpFunction[#,x->s],x->s]&/@{body};
     If[ !FreeQ[r,FailLimit],r,
       If[ Length[{body}] == Length[Union[r]],
         If[ Length[{a}]==1,
             If[ f@@r, LimitN[c a[[1]],x->s] ],
         If[ Length[{a}]==2,
             If[ f@@r, LimitN[c {a}[[1]],x->s], LimitN[c {a}[[2]],x->s] ],
             If[ f@@r, LimitN[c {a}[[1]],x->s], LimitN[c {a}[[2]],x->s],
                 LimitN[c {a}[[3]],x->s] ]
           ]],
         If[ Length[{a}]==3, LimitN[c {a}[[3]],x->s], FailLimit ]
       ] ] 
   ]

 LimitN[ c_. nest[ f_,expr_,n_ ], x_->s_ ] :=
   LimitN[ c Nest[f,expr,n], x->s ] /; FreeQ[n, x]

 LimitN[ c_. nest[ f_,expr_,x_ ], x_->Infinity ] :=
   Module[ { answer },
     DeactivateMessages[{Solve::ifun,Solve::tdep},
     answer = Solve@@{Solve`var == f[Solve`var],Solve`var};
     answer = 
     If[ Head[answer] === List,
	 If[ Znak[expr],
	     Max[Select[Flatten[answer/.Rule[a_,b_]:> b],N[#]<0&]],
	     Min[Select[Flatten[answer/.Rule[a_,b_]:> b],N[#]>0&]]
         ],
	 FailLimit    
     ]
     ]; (* DeactivateMessages *)
     answer LimitN[c, x->Infinity]
   ]

 LimitN[ expr_/;!FreeQ[expr,nest] ] := FailLimit         

 LimitN[ c_. integrate[expr_,x_], x_->s_ ] :=
   Module[ {inter},
     inter = Integrate[expr,x];
     If[ FreeQ[inter,Integrate],
         LimitN[ c inter,x->s ],
         FailLimit]
   ]

 LimitN[ c_. integrate[expr_,{x_,a_,b_}], x_->s_ ] :=
   Module[ {var},
     LimitN[ c integrate[ expr/.x->var,{var,a,b} ],x->s ]/.
     {var->x,integrate->Integrate} 
   ]

 LimitN[ expr_,x_->s_ ] := (expr/.x->s) /;!direction &&
 Determinate[ expr, x->s]

 LimitN[ expr_,x_->s_/;FreeQ[s,DirectedInfinity] ] := 
   Module[ {var,answer},
     card = 1;
     Map[Simp1[#]&,expr/.x->(var+s),{0,-2}]//.{
        Simp1[ a_ ] :> a /; FreeQ[a,var],
        Simp1[ a_Symbol ] :> a,
        Simp1[ a_Times ]  :> a,
        Simp1[ a_Plus ]   :> a,
        Simp1[ var^n_ ] :> 
           (If[ card < Round[Abs[N[n]]], 
                card = Round[Abs[N[n]]] ]; var^n ) /;
           NumberQ[N[n]] };
     answer = Taylor1[expr/.x->(var+s),var->0];
     answer /; FreeQ[answer,FailLimit]
   ] /;
  And@@(FreeQ[expr,#]&/@listfun) 

 LimitN[ expr_, x_->s_ ] :=
   Module[ { answer = Together[expr],num,den },
     num = LimitN[Numerator[answer],x->s];
     den = LimitN[Denominator[answer],x->s];
     If[ If[ !FreeQ[expr,Sin[_]] || !FreeQ[expr, Cos[_]],
             Simplify[{num,den}, Trig->True],
	     {num,den} ]==={0,0} || (
         !FreeQ[num,DirectedInfinity] && !FreeQ[den,DirectedInfinity]),
	 answer = LimitN[
	       If[ !FreeQ[expr,Sin[_]] || !FreeQ[expr, Cos[_]],
	           Simplify[ D[Numerator[answer],x], Trig->True ]/
	 	   Simplify[ D[Denominator[answer],x], Trig->True ],
		   D[Numerator[answer],x]/D[Denominator[answer],x]
		] ,x->s],
	 answer = FailLimit
     ];
     answer /; FreeQ[answer,Indeterminate] && FreeQ[answer,FailLimit]
  ] /; !direction &&
 LHospital[ Together[expr], x->s ]

 LHospital[ u_, x_->s_ ] := True /; 
 !FreeQ[u,x] && 
 Determinate[Numerator[u],x->s] && Determinate[Denominator[u],x->s] &&
 And@@(FreeQ[u,#]&/@{Abs,Sign,Factorial,Binomial})  
 
 LimitN[ expr_,x_->s_/;s=!=0 && FreeQ[s,DirectedInfinity] ] :=
  Module[ {var,answer},
    card = 1;
    answer = GeneralRule[expr,x];
    Analys[
    SimpAllFunction[Simplify[answer/.x->(var+s)],var->0]/.var->x,x->0]
  ]

 LimitN[ expr_,x_->DirectedInfinity[-1] ] :=
  Module[ {answer,var},
    card = 1;
    answer = GeneralRule[expr,x];
    Analys[ 
    SimpAllFunction[answer/.x->-var, var->Infinity]/.var->x,x->Infinity]
  ]

 LimitN[ expr_,x_->s_ ] :=
  Module[ {answer},
    card = 1;
    answer = GeneralRule[expr,x];
    Analys[ SimpAllFunction[answer, x->s], x->s]
  ]

 LimitN[ expr_,x_/;Head[x]=!=Rule ] :=
   (Message[ Limit::lim,x];
    Limit[expr,x] /; Fail ) 
   
 SimpAllFunction[ expr_,w_ ] := 
   SimpFunction[ Expand[Numerator[expr]]/Denominator[expr],w]
  
 SimpFunction[ expr_/;!FreeQ[expr,If],w_ ] := 
   SimpFunction[expr//.If->if,w]
 
 SimpFunction[ c_. Power[a_,expr_],x_->s_ ] := 
   AsymptoticPower[ E, SimpFunction[expr ,x->s] *
                    AsymptoticLog[SimpFunction[a,x->s],x->s], x->s] *
   SimpFunction[ c,x->s] /; !FreeQ[expr,x]

 SimpFunction[ expr_,x_->s_ ] := 
   Block[ { Simp,AsymptoticPower,answer },
    answer = 
    Map[Simp[#,x->s]&,expr,{0,-2}]//.{
        Simp[ a_,z_->_ ]:>ExpandAll[a]/;And@@(FreeQ[a,#]&/@{z,integrate,if}),
        Simp[ a_Symbol, _ ] :> a,
        Simp[ a_Plus, _ ]   :> a, 
	Simp[ PolyGamma[n_Integer?Positive,e_], w_] :> 
		(card=n; Simp[PolyGamma[n,e], w]),
        Simp[ a_ b_,z_ ] :> Simp[a,z] Simp[b,z],
        Simp[ a_^n_,z_->w_] :> 
        (If[ card < Round[Abs[N[n]]] && asymptotic, 
          If[ Round[Abs[N[n]]]>10, cardFlag=True ];
	  card = Round[Abs[N[n]]] 
	]; 
         If[a===z,z^n,AsymptoticPower[a,n,z->w] ]) /;
        NumberQ[N[n]]
        };
   answer//.{Simp[ Simp[a__],_ ] :> Simp[a],
	     Simp[ AsymptoticPower[a__],_ ] :> AsymptoticPower[a]}
   ] 

 Simp[ a_List,_ ] := a
 Simp[ expr_,w_ ] := expr /; !FreeQ[expr,FailLimit]
 Simp[ a_ expr_,w_ ] := Simp[a,w] Simp[expr,w]
 Simp[ a_^expr_,x_->s_ ] := AsymptoticPower[a,expr,x->s] /; !FreeQ[expr,x] 
 Simp[ x^n_,x_->s_] := 
   (If[ card < Round[Abs[N[n]]] && asymptotic, 
      If[Round[Abs[N[n]]]>10,cardFlag=True];card = Round[Abs[N[n]]] ]; x^n )/;
 NumberQ[N[n]]
 Simp[ LInfinity[n_]^k_.,x_->s_ ] :=
   ( If[ asymptotic && card <= Round[Abs[N[k]]]-1,card = Round[Abs[N[k]]]-1 ];
     LInfinity[n]^k) 
 Simp[ f_[n_]^k_.,x_->s_ ] := f[n]^k/;
 Complement[{f},{EInfinity,EZero,TInfinity}]==={} 
 Simp[ w_Plus,x_ ] := Simp[#,x]&/@w

 Simp[ f_[u__],x_->s_ ] :=
   Module[ {answer},
    DeactivateMessages[{General::spell1},
        answer = ToExpression[StringJoin[context,"Asymptotic",
             ToString[f] ]]@@Join[ Expand[{u}],{x->s}]
    ];
    answer
   ]/; s===DirectedInfinity[1] ||
       Or@@(!FreeQ[f[u],#]&/@{LInfinity,EInfinity,TInfinity,Abs,Sign}) ||
       !Determinate[f[u],x->s] 

 Simp[ u_,w_ ] := Taylor[ExpandAll[u],w]

 AsymptoticComplex[a_, b_, x_] := a + I b

 GeneralPolynomialQ[ a_ + b_,x_ ] :=
   GeneralPolynomialQ[a,x] && GeneralPolynomialQ[b,x]

 GeneralPolynomialQ[ x_^n_.,x_ ] := If[ FreeQ[n,x], True, False]  

 GeneralPolynomialQ[ a_ * b_,x_ ] :=
   GeneralPolynomialQ[a,x] && GeneralPolynomialQ[b,x]

 GeneralPolynomialQ[ a_,x_ ] := True /; 
 And@@(FreeQ[a,#]&/@{x,TInfinity,LInfinity,EInfinity,EZero}) 

 GeneralPolynomialQ[ a__ ] := False

 Taylor1[ expr_,x_->s_ ] := expr /; FreeQ[expr,x]

 Taylor1[ a_^b_ ,x_->s_ ] := FailLimit /; !FreeQ[a,x] && !FreeQ[b,x]

 Taylor1[ f_[___,a_^b_,___],x_->s_ ] := FailLimit /; 
 !FreeQ[a,x] && !FreeQ[b,x]

 Taylor1[ expr_,x_->s_ ] :=
   Module[ {answer,i=0,r,rr},
     DeactivateMessages[{Power::infy,Power::indet,Infinity::indet,General::indet,
         General::dbyz,Series::esss,Series::lss,SeriesData::csa},
     answer = 
     If[ GeneralPolynomialQ[expr,x],
         Analys[expr,x->s],
(*         r = 
          If[ analytic,
              Series[expr,{x,s,card},Analytic -> True],
              Series[expr,{x,s,card},Analytic -> False]
            ];*)
         r = Series[expr,{x,s,card}];
         If[ !FreeQ[r,Series] || !FreeQ[r,ComposeSeries] ||  	  
	     Head[r]=!=SeriesData || Length[Normal[r]//Expand]>15 ||
	     !FreeQ[Normal[r]//N,ComplexInfinity] || 	
	     !FreeQ[Normal[r]//N,Indeterminate] ||
             !FreeQ[rr = Simplify[GeneralRule[Normal[r],x]//ExpandAll, 
	     			  Trig->True],
	     	    DirectedInfinity] ||
             !FreeQ[GeneralRule[Normal[r],x]//.
	      {a_. Log[z_] + b_. Log[u_] + c_. :> Log[Simplify[z^a u^b]] + c},
             DirectedInfinity],
	     FailLimit,
             i+=2; 
	     r = Series[expr,{x,s,card+i}];
             If[ FreeQ[r,Log],
                 Analys[If[i==0,rr, SimplifyLength[GeneralRule[Normal[r],x]]],
		 	x->s],
                 If[ FreeQ[Cases[r,a_/;!FreeQ[a,Log] ]/.
                           a_. Log[b_]^n_. :> b , x],
                     Analys[SimplifyLength[GeneralRule[Normal[r],x]],x->s],
                     FailLimit
                   ]
             ]
	  ]
       ]
     ]; (* DeactivateMessages *)
     answer 
   ]

 SimplifyLength[ expr_, r___ ] := If[ Length[expr] < 8, Simplify[expr,r],expr ]
 
 Taylor[ expr_,x_->s_] := expr /;
 GeneralPolynomialQ[Expand[expr],x]

 Taylor[ expr_,x_->s_] := Normal[Series[ expr,{x,0,card+1} ]] /;
 GeneralPolynomialQ[PowerExpand[Expand[expr]],x]

 Taylor[f_,x_->s_] := NormalSeries[f,{x,s,2}] /; cardFlag

 Taylor[f_,x_->s_] :=
   If[ s=!=Infinity,
       NormalSeries[f,{x,s,If[card==1,2,card]}],
       NormalSeries[f,{x,s,2 card}] 
     ] 

 NormalSeries[ expr_,{x_,Infinity,max_} ] :=
   Module[ {r},
(*    r = If[ analytic,
            Series[expr,{x,Infinity,max},Analytic -> True],
            Series[expr,{x,Infinity,max},Analytic -> False]
          ];*)
    r = Series[expr,{x,Infinity,max}];
    If[ !FreeQ[r,ExpIntegralEi],
        r = r/.ExpIntegralEi[ Log[z_] ] :> LogIntegral[z]
    ];
    If[ Head[r]===Series || (Head[r]=!=SeriesData && !FreeQ[r,SeriesData]), 
	FailLimit,
    If[ Head[r]===SeriesData && Length[r[[3]]] > If[cardFlag,1,2 card] &&
        (r[[5]]-1)/r[[6]] >= 2 If[cardFlag,1,card],
        Normal[r],
        NormalSeries[expr,{x,Infinity,max+
        If[Head[r]===SeriesData,If[r[[4]]==0,1,r[[4]] ], 1]}] 
    ]]
  ]

 NormalSeries[ expr_,{x_,min_,max_} ] :=
   Module[ {r},
(*    r = If[ analytic,
            Series[expr,{x,min,max},Analytic -> True],
            Series[expr,{x,min,max},Analytic -> False]
          ];*)
    r = Series[expr,{x,min,max}];
    If[ !FreeQ[r,ExpIntegralEi],
        r = r/.ExpIntegralEi[ Log[z_] ] :> LogIntegral[z]
    ];
    If[ Head[r]===Series || (Head[r]=!=SeriesData && !FreeQ[r,SeriesData]), 
	FailLimit,
    If[ Head[r]===SeriesData && Length[r[[3]]] >= card &&
        (r[[5]]-1)/r[[6]] >= card,
        Normal[r],
        NormalSeries[expr,{x,min,max+
        If[Head[r]===SeriesData,If[r[[4]]==0,1,r[[4]] ], 1]}] 
     ]]
  ]

(*===================== Asymptotic LOG =====================================*)

 AsymptoticLog[ a_/;NumberQ[N[a]],x_->s_ ] := 
 (Log[Abs[a]] + I Arg[a])//.LogRule 

 AsymptoticLog[ sign[0] , x_ ] := LInfinity[1]

 AsymptoticLog[ a_. x_^n_.,x_->s_ ] := AsymptoticLog[a,x->s] + n LInfinity[1] 

 AsymptoticLog[ a_. f_[n_]^k_.,w_ ] := 
   ( AsymptoticLog[a,w] +
     If[f===LInfinity, k LInfinity[n+1], k n] )/;
 Complement[{f},{EInfinity,EZero,TInfinity, LInfinity}]==={} 

 AsymptoticLog[ 1,w_ ] := 0
 
 AsymptoticLog[ expr_,x_->s_ ] :=
   Module[ {var,main},
     main = MainTerm[expr,x->s];
     If[ !FreeQ[main,FailLimit], var FailLimit,
     If[ main===expr, 
         If[ And@@(FreeQ[main,#]&/@{x,LInfinity,EInfinity}),
             Log[expr]//.LogRule ,
             FailLimit
           ],
         AsymptoticLog[main,x->s] +
         (main = Expand[If[ s===0,
             Taylor[Log[1+var],var->0]/.
              var->((expr-main)/main),
             Taylor[Log[1+1/var],var->Infinity]/.
              var->(main/(expr-main))
            ]/.EInfinity[a_]^k_?Negative :> EZero[a k]];
          If[ !cardFlag,
           var = CasesN[main,a_. x^m_];
           main = (main-Plus@@var) + 
                  Plus@@(var/.a_. x^m_ :> 0/;Abs[N[m]]>2 card+1);
           var = CasesN[main,a_. LInfinity[_]^m_];
           (main-Plus@@var) + 
           Plus@@(var/.a_. LInfinity[_]^m_ :> 0/;Abs[N[m]]>card+1),
           main 
          ]
         )
     ]]
   ] 

(*===================== Asymptotic TRIG ====================================*)

 AsymptoticSin[ expr_,x_->s_ ] := 
   AsymptoticForSin[expr,x->s,Analys[PowerExpand[expr],x->s]]

 AsymptoticForSin[ expr_,x_->s_,w_ ] :=
   Module[ {var}, 
     Taylor[Sin[var],var->w]/.var->expr
   ]/;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticForSin[ expr_,x_->s_,w_ ] :=
   Module[ {rr = PowerExpand[expr],r},
     r = CasesN[rr,a_. x^m_];
     AsymptoticSin1[ rr - Plus@@r + 
         Plus@@(r/.{a_. x^m_ :> 0/;Abs[N[m]] > card+1}),x->s]
   ]

 AsymptoticSin1[ a_,x_->s_ ] := Sin[a] /; NumberQ[N[a]]

 AsymptoticSin1[ a_ + b_,x_->s_ ] :=
   AsymptoticSin[a,x->s] AsymptoticCos[b,x->s] +
   AsymptoticCos[a,x->s] AsymptoticSin[b,x->s]

 AsymptoticSin1[ expr_,x_->s_ ] := AsymptoticTrig[expr,x->s,Sin]

 AsymptoticCos[ expr_,x_->s_ ] := 
   AsymptoticForCos[expr,x->s,Analys[PowerExpand[expr],x->s]]

 AsymptoticForCos[ expr_,x_->s_,w_ ] :=
   Module[ {var}, 
     Taylor[Cos[var],var->w]/.var->expr
   ]/;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticForCos[ expr_,x_->s_,w_ ] :=
   Module[ {rr = PowerExpand[expr],r},
     r = CasesN[rr,a_. x^m_];
     AsymptoticCos1[ rr - Plus@@r + 
         Plus@@(r/.{a_. x^m_ :> 0/;Abs[N[m]] > card+1}),x->s]
   ]

 AsymptoticCos1[ a_,x_->s_ ] := Cos[a] /; NumberQ[N[a]]

 AsymptoticCos1[ expr_,x_->s_ ] := AsymptoticTrig[expr,x->s,Cos]

 AsymptoticCos1[ a_ + b_,x_->s_ ] :=
   AsymptoticCos[a,x->s] AsymptoticCos[b,x->s] -
   AsymptoticSin[a,x->s] AsymptoticSin[b,x->s]

 AsymptoticSec[ expr_,x_->s_ ] := 
   AsymptoticForSec[expr,x->s,Analys[PowerExpand[expr],x->s]]

 AsymptoticForSec[ expr_,x_->s_,w_ ] :=
   Module[ {var}, 
     Taylor[Sec[var],var->w]/.var->expr
   ]/;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticSec[ expr_,x_->s_ ] :=
   Module[ {rr = PowerExpand[expr],r},
     r = CasesN[rr,a_. x^m_];
     AsymptoticSec1[ rr - Plus@@r + 
         Plus@@(r/.{a_. x^m_ :> 0/;Abs[N[m]] > card+1}),x->s]
   ]

 AsymptoticSec1[ a_,x_->s_ ] := Sec[a] /; NumberQ[N[a]]

 AsymptoticSec1[ a_ + b_,x_->s_ ] :=
   Module[ {rr,r},
     rr = Expand[ AsymptoticCos1[a,x->s] AsymptoticCos1[b,x->s] -
          AsymptoticSin1[a,x->s] AsymptoticSin1[b,x->s] ];
     r = CasesN[rr,c_. x^m_];
     rr = Expand[AsymptoticPower[rr - Plus@@r + 
          Plus@@(r/.{c_. x^m_ :> 0/;Abs[N[m]] > card+1}),-1,x->s]];
     r = CasesN[rr,c_. x^m_];
     rr - Plus@@r + 
     Plus@@(r/.{c_. x^m_ :> 0/;Abs[N[m]] > card+1})
   ]

 AsymptoticCsc[ expr_,x_->s_ ] := 
   AsymptoticForCsc[expr,x->s,Analys[PowerExpand[expr],x->s]]

 AsymptoticForCsc[ expr_,x_->s_,w_ ] :=
   Module[ {var}, 
     Taylor[Csc[var],var->w]/.var->expr
   ]/;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticCsc[ expr_,x_->s_ ] :=
   Module[ {rr = PowerExpand[expr],r},
     r = CasesN[rr,a_. x^m_];
     AsymptoticCsc1[ rr - Plus@@r + 
         Plus@@(r/.{a_. x^m_ :> 0/;Abs[N[m]] > card+1}),x->s]
   ]

 AsymptoticCsc1[ a_,x_->s_ ] := Csc[a] /; NumberQ[N[a]]

 AsymptoticCsc1[ a_ + b_,x_->s_ ] :=
   Module[ {rr,r},
     rr = Expand[ AsymptoticSin1[a,x->s] AsymptoticCos1[b,x->s] -
          AsymptoticCos1[a,x->s] AsymptoticSin1[b,x->s] ];
     r = CasesN[rr,c_. x^m_];
     rr = Expand[AsymptoticPower[ rr - Plus@@r + 
         Plus@@(r/.{c_. x^m_ :> 0/;Abs[N[m]] > card+1}),-1,x->s]];
     r = CasesN[rr,c_. x^m_];
     rr - Plus@@r + 
     Plus@@(r/.{c_. x^m_ :> 0/;Abs[N[m]] > card+1})
   ]

 AsymptoticSec1[ expr_,x_->s_ ] := 
    AsymptoticPower[AsymptoticTrig[expr,x->s,Cos],-1,x->s]

 AsymptoticCsc1[ expr_,x_->s_ ] := 
    AsymptoticPower[AsymptoticTrig[expr,x->s,Sin],-1,x->s]

 AsymptoticTan[ expr_,x_->s_ ] :=
   AsymptoticForTan[expr,x->s,Analys[expr,x->s]]

 AsymptoticForTan[ expr_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   TInfinity[Tan[expr]]
   
 AsymptoticForTan[ expr_,x_->s_,Indeterminate ] :=
   TInfinity[ Tan[expr] ] 

 AsymptoticForTan[ expr_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ Tan[var],var->w ]/.var->expr
   ]

 AsymptoticCot[ expr_,x_->s_ ] :=
   AsymptoticForCot[expr,x->s,Analys[expr,x->s]]

 AsymptoticForCot[ expr_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   TInfinity[ Cot[expr] ]
   
 AsymptoticForCot[ expr_,x_->s_,Indeterminate ] :=
   TInfinity[ Cot[expr] ] 

 AsymptoticForCot[ expr_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ Cot[var],var->w ]/.var->expr
   ]

 AsymptoticTrig[ a_. x_^n_.,x_->DirectedInfinity[1],fun_ ] := 
   Which[ Positive[N[n]], TInfinity[a,n,fun],
          Negative[N[n]], Taylor[fun@@{a x^n},x->DirectedInfinity[1]],
	  True,           TInfinity[expr,0,fun] ] /; 
 FreeQ[a,x]

 AsymptoticTrig[ a_. x_^n_.,x_->0,fun_ ] := 
   Which[ Negative[N[n]], TInfinity[a,n,fun],
          Positive[N[n]], Taylor[fun@@{a x^n},x->0],
	  True,           TInfinity[expr,0,fun] ] /; 
 FreeQ[a,x]

 AsymptoticTrig[ expr_,x_->s_,fun_ ] := TInfinity[expr,0,fun]

(*===================== Asymptotic POWER ==================================*)
  
 AsymptoticPower[ u_, 0, _] := 1

 AsymptoticPower[ u_/;NumberQ[N[u]], v_/;NumberQ[N[v]], x_ ] :=  u^v 

 AsymptoticPower[ u_,v_/;!FreeQ[v,FailLimit],x_ ] := u FailLimit
 
 AsymptoticPower[ E,expr1_ + expr2_,x_->s_ ] :=
   AsymptoticPower[ E,expr1,x->s] AsymptoticPower[ E,expr2,x->s] /;
 !FreeQ[expr1,Complex] || !FreeQ[expr2,Complex]
 
 AsymptoticPower[ E,expr_. Complex[0,n_],x_->s_ ] :=
   AsymptoticCos[expr n,x->s] + I AsymptoticSin[expr n,x->s] /;
 FreeQ[expr,Complex]

 AsymptoticPower[ E,expr_/;!FreeQ[expr,Complex],x_->s_ ] :=
   AsymptoticPower[ E,Expand[expr],x->s]
 
 AsymptoticPower[ E,expr_,x_->s_ ] :=
   AsymptoticExp[{ FreeQ[expr,EInfinity], FreeQ[expr,EZero] },
		 If[ FreeQ[expr,x], 
		     Expand[expr], 
		     AsympSimplify[expr,x,card,False]
		 ], x->s]//.
   { EInfinity[ a_. LInfinity[n_] + b_.] :> 
	If[ n==1, x^a, LInfinity[n-1]^a ] EInfinity[b] /; NumberQ[a],
     EZero[a_. LInfinity[n_] + b_.]:>
	If[ n==1, x^a, LInfinity[n-1]^a ] EZero[b] /; NumberQ[a]
   } 
 
 AsymptoticPower[ u_ w_,v_,x_ ] := 
   AsymptoticPower[u,v,x] AsymptoticPower[w,v,x]

 AsymptoticPower[ EZero[a_],v_?Negative,w_ ] := 
   EInfinity[Expand[v a]] 
 
 AsymptoticPower[ EInfinity[a_],v_?Negative,w_ ] := 
   EZero[Expand[v a]] 
 
 AsymptoticPower[ expr_,v_?NumberQ,x_->0 ] :=  
   Normal[Series[ SimplifyPolynom[ CollectSign[  
   		CoefficientList[CollectSign[expr],x]], x, 	
   		0]^v, {x,0,card} ]] /; 
 And@@(FreeQ[expr,#]&/@{LInfinity,EInfinity,EZero,TInfinity}) &&
 PolynomialQ[expr,x] 

 CollectSign[ expr_ ] :=
   Module[ {r},
     r = Union[Level[ expr, {2,Depth[expr]-3} ]];
     If[ !FreeQ[r, Abs] && !FreeQ[r,Sign],
         r = Cases[r, Sign[w_]];
         (Map[ Collect[#,r]&, expr, Depth[expr]-3  ])//.{
	   w_/Sign[w_] :> Abs[w]},
	 expr
     ]
  ]    
 
 AsymptoticPower[ expr_,v_?NumberQ,x_->s_ ] :=  
   Module[ {var,main,exprn,r,k},
    exprn = Together[CompDegree[expr/.w_/Sign[w_] :> Abs[w],x]];
    main = MainTerm[ exprn,x->s ] //.
	{ EInfinity[ a_ + b_ ] :> EInfinity[a] EInfinity[b] };
    If[ FreeQ[main,EInfinity], k=card,k=1 ];
    exprn = Expand[exprn]/.{
        EZero[a_ x^m_.] :>0 /;Abs[a]>card||Abs[m]>card,
        EInfinity[a_ x^m_.] :>0 /;Abs[a]>card||Abs[m]>card };
    r = CasesN[exprn,a_. x^m_.];
    exprn = (exprn-Plus@@r) + 
                Plus@@(r/.a_. x^m_ :> 0/;Abs[N[m]]>2 k+1) ;
    If[ !FreeQ[main,FailLimit], FailLimit,
        AsympSimplify[ main^v *
        If[exprn===main,1,
           If[ s===0,
               Normal[Series[(1+var)^v,{var,0,k+1}]]/.
                 var^n_. :> AsympSimplify1[((exprn-main)/main)^n,x,k,False],
               Normal[Series[(1+var)^v,{var,0,2 k}]]/.
                 var^n_. :> AsympSimplify1[((exprn-main)/main)^n,x,k,False]
             ]/.AsympSimplify1->AsympSimplify  
          ],x,k+1,False]
      ] 
   ] 

 AsymptoticPower[ u_/;u=!=E,v_Plus,x_->s_ ] := 
   Module[ { par },
	sub = Join[sub, {par->v}];
	AsymptoticPower[E,par AsymptoticLog[u,x->s],x->s]
   ]/;!FreeQ[u,x] && And@@(FreeQ[v,#]&/@{x,TInfinity,LInfinity,EInfinity})

 AsymptoticPower[ u_/;u=!=E,v_,x_->s_ ] := 
   AsymptoticPower[E,v AsymptoticLog[u,x->s],x->s]

 AsymptoticPower[ u_,v_,x_->s_ ] := u^v

 SimplifyPolynom[ {0,list__},x_,n_ ] := SimplifyPolynom[{list},x,n+1]

 SimplifyPolynom[ {a_},x_,n_ ] := a x^n

 SimplifyPolynom[ list_,x_,0 ] := 
   (SimplifyM[#]&/@Take[list,If[card==1,2,card] ]).Table[x^k,
   {k,0,If[card==1,1,card-1]}]

 SimplifyPolynom[ list_,x_,n_ ] := 
   (SimplifyM[#]&/@Take[list,If[card==n,1,card-n]]).Table[x^k,
   {k,n,If[card==n,n,card-1]}] /; card>=n

 SimplifyPolynom[ list_,x_,n_ ] := FailLimit /; card < n

 SimplifyM[ expr_/;!FreeQ[expr,FresnelS] || !FreeQ[expr,FresnelC] ] := expr

 SimplifyM[ expr_ ] := Simplify[expr]

 AsympSimplify[ expr_,__] := expr /; cardFlag

 AsympSimplify[ expr_, x_, k_, t_ ] :=
   Module[ {r,answer},
    answer = CompDegree[Expand[expr],x];
    r = CasesN[answer,a_. x^m_];
    answer = (answer-Plus@@r) + 
                Plus@@(r/.a_. x^m_ :> 0/;
		If[!t, Abs[N[m]] > 2 k, Abs[N[m]] > k]);
    If[ !FreeQ[answer,LInfinity],
        r = CasesN[answer,a_. LInfinity[_]^m_];
        answer = (answer-Plus@@r) + 
             Plus@@(r/.a_. LInfinity[_]^m_ :> 0/;Abs[N[m]] > k) 
      ];
    If[ FreeQ[answer,EInfinity], 
	answer,
        r = CasesN[answer,a_. EInfinity[_]^m_];
        (answer-Plus@@r) + 
             Plus@@(r/.a_. EInfinity[_]^m_ :> 0/;Abs[N[m]] > k) 
      ]
   ]
        
 AsymptoticExp[ w_,a_?NumberQ ,x_ ] := E^a (*
   AsymptoticExp[w,expr,x] *)
        
 AsymptoticExp[ {True,True},a_. x_^n_.,x_->DirectedInfinity[1] ] := 
   Module[ {r,var},
     r = { Positive[N[n]],Positive[N[a]] };
     If[ card < Floor[Abs[a]],card = Floor[Abs[a]] ];
      If[ r==={True,True},  EInfinity[a x^n],
       If[ r==={True,False}, EZero[a x^n],
        If[ r[[1]]===False, 
            1 + Sum[ a^k x^(n k)/k!,
                {k,1,If[card==1 || cardFlag,2,card]}],
            EInfinity[a x^n] ]]]
   ] /;
 And@@(FreeQ[a,#]&/@{x,TInfinity,LInfinity})
        
 AsymptoticExp[ {True,True},expr_,x_->DirectedInfinity[1] ] :=  
   Module[ {r,var,rr=Collect[expr,x],rest,c},     
     r = Cases[rr+var,a_. x^n_./;Positive[N[n]]];
     If[ Length[r]==0, 
         r = Cases[rr+var,a_. LInfinity[n_]^k_./;Positive[k]&&FreeQ[a,x]];
         If[ Length[r]==0,
             r = Plus@@Cases[rr+var,a_/;
                 And@@(FreeQ[a,#]&/@{x,var,LInfinity,TInfinity})];
             rest = rr-r;
             rr= Plus@@Cases[rest+var,a_/;!FreeQ[a,TInfinity]];
             E^r E^rr *
             (1+Sum[var^k/k!,{k,1,If[cardFlag,2,card]}]/.var->(rest-rr))
	     ,
             rest = rr - Plus@@r;
             rr = Cases[r,m_ LInfinity[n_]^k_./;ZnakLim[m,x->Infinity]];
	     c = Plus@@Cases[rest+var,a_/;
                 And@@(FreeQ[a,#]&/@{x,var,LInfinity,TInfinity})];
             EZero[Plus@@rr] * E^c * 
             EInfinity[Plus@@r-Plus@@rr] * 
             (1 + Sum[var^k/k!,
                  {k,1,If[card==1 || cardFlag,2,card]}]/.var->(rest-c))
           ],
         rest = rr - Plus@@r;
         rr = Cases[r,a_ x^n_./;ZnakLim[a,x->Infinity]];
         r = Complement[r,rr];
         If[ MaxM[Cases[rr,a_. x^n_. :> n]] > MaxM[Cases[r, a_. x^n_. :> n]], 
             EZero[Plus@@r+Plus@@rr],
             EInfinity[Plus@@r+Plus@@rr] ] *
         If[ rest===0,1,
         (r = Plus@@Cases[rest+var,a_/;!FreeQ[a,TInfinity]];
          rest = rest-r;
          TInfinity[EInfinity[r]] *
          (r = Cases[rest+var,
               a_. LInfinity[n_]^k_./;Positive[k]&&FreeQ[a,x]];
           rest = rest-Plus@@r;
           If[ Length[r]!=0,
               rr=Cases[r, a_. LInfinity[n_]^k_./;
                  ZnakLim[a,x->Infinity]];
               EZero[Plus@@rr]* EInfinity[Plus@@r-Plus@@rr],
               1 ] *
           (r = Plus@@Cases[rest+var,a_/;
                 And@@(FreeQ[a,#]&/@{x,var,LInfinity,TInfinity})];
             rest = rest-r;
             E^r * 
             (1 + Sum[var^k/k!,
                  {k,1,If[card==1 || cardFlag,2,card]}]/.var->rest))))
          ]
       ]
   ] 
        
 AsymptoticExp[ {True,True},a_. x_^n_.,x_->0 ] := 
   Module[ {r,var},
     r = { Positive[N[n]],Positive[N[a]] };
     If[ r==={False,True},  EInfinity[a x^n],
       If[ r==={False,False}, EZero[a x^n],
        If[ r[[1]]===True, 
            1+Sum[ a^k x^(n k)/k!,
              {k,1,If[card==1 || cardFlag,2,card]}],
            var FailLimit]]]
   ] /;
 And@@(FreeQ[a,#]&/@{x,TInfinity,LInfinity})

 AsymptoticExp[ {True,True},expr_,x_->0 ] :=  
   Module[ {r,var,rr=expr,rest},
     r = Cases[rr+var,a_. x^n_/;Negative[N[n]]];
     If[ Length[r]==0, 
         r = Cases[rr+var,a_. LInfinity[n_]^k_./;Positive[k]&&FreeQ[a,x]];
         If[ Length[r]==0,
             r=Cases[rr+var,a_/;FreeQ[a,x]&&FreeQ[a,var]&&FreeQ[a,LInfinity]];
             E^(Plus@@r) *
             (1+Sum[(rr-Plus@@r)^k/k!,
		{k,1,If[card==1 || cardFlag,2,card]}])
	     ,
             rest = rr - Plus@@r;
             rr = Cases[r,m_. LInfinity[n_]^k_./;!ZnakLim[m,x->0]&&OddQ[k] ||
                  ZnakLim[m,x->0]&&EvenQ[k]];
             EZero[Plus@@rr] *
             EInfinity[Plus@@r-Plus@@rr] * 
             (1+Sum[rest^k/k!,{k,1,If[card==1 || cardFlag,2,card]}])
           ],
         rest = rr - Plus@@r;
         rr = Cases[r,a_ x^n_/;ZnakLim[a,x->0]];
         r = Complement[r,rr];
         If[ MinM[Cases[rr,a_. x^n_ :> n]] < MinM[Cases[r, a_. x^n_ :> n]], 
             EZero[Plus@@rr+Plus@@r],
             EInfinity[Plus@@r+Plus@@rr] ] *
         (1+Sum[rest^k/k!,{k,1,If[card==1 || cardFlag,2,card]}])
      ]
   ]  

 AsymptoticExp[ {t_,False},expr_,x_->s_ ] :=  
   Module[ {r,var},
     r = Plus@@Cases[expr+var,a_. EZero[_]];
     If[ FreeQ[r,EInfinity],
         AsymptoticExp[ {t,True},expr-r,x->s] *
           (1 + Sum[var^k/k!,
		{k,1,If[card==1 || cardFlag,2,card]}]/.var->r),
         EInfinity[expr]
       ]
   ] 

 AsymptoticExp[ {False,t_},expr_,x_->s_ ] :=  
   Module[ {r,rr,var},
     r = Cases[(expr/.{EInfinity[a_]^k_ :> EInfinity[k a]}) + var,
               a_. EInfinity[_]];
     If[ r==={}, EInfinity[expr],
         rr = Cases[r,a_/;ZnakLim[a,x->s] ];
         AsymptoticExp[ {True,t},expr-Plus@@r,x->s] *
         EInfinity[Plus@@r-Plus@@rr] *
         EZero[Plus@@rr]
       ]
   ] 

(*===================== Find the Main Term =================================*)

 MainTerm[ 0,_ ] := 0

 MainTerm[ expr_,x_->Infinity ] := 
   Module[ {r = Expand[expr],var,const,max},
	 const = Plus@@Cases[r+var,a_/;FreeQ[a,x]&&FreeQ[a,var]];
	 max = MaxM[(r=CasesN[r,a_. x^n_.])/. a_. x^n_. :> n];
	 If[ NumberQ[max] || max===-Infinity,
	     If[ const=!=0, max=MaxM[0,max] ];
	     If[ max==0, const,
		 x^max(Plus@@(Cases[r,a_. x^n_./;n===max]/x^max))
	     ],
	     If[ FreeQ[max,Max],
		 x^max(Plus@@(Cases[r,a_. x^n_./;n===max]/x^max)),
		 expr 
	     ]
	 ]
   ] /;
 GeneralPolynomialQ[expr,x]

 MainTerm[ expr_,x_->0 ] := 
   Module[ {r = Expand[expr],var,const,min},
	 const = Plus@@Cases[r+var,a_/;FreeQ[a,x]&&FreeQ[a,var]];
	 min = MinM[(r=Cases[r+var,a_. x^n_.])/. a_. x^n_. :> n];
	 If[ NumberQ[min] || min===Infinity,
	     If[ const=!=0, min=MinM[0,min] ];
	     If[ min==0, const,
		 x^min (Plus@@(Cases[r,a_. x^n_./;n===min]/x^min)) 
	     ],
	     If[ FreeQ[min,Min],
		 x^min (Plus@@(Cases[r,a_. x^n_./;n===min]/x^min)),
		 expr 
	     ]
	]
   ] /;
 GeneralPolynomialQ[expr,x]

 MainTerm[ expr1_ expr2_,x_->s_ ] :=
   MainTerm[expr1,x->s] MainTerm[expr2,x->s] 

 MainTerm[ expr_Plus,x_->s_ ] :=
   Module[ {r,rr},
     r = expr/.{EZero[a_] :> 1/EInfinity[-a]};
     If[ !FreeQ[r,EInfinity],
         rr = r/.{a_. EInfinity[w_]^n_ :> 
                If[ n<0 && FreeQ[a,EInfinity],0,
                    a EInfinity[w]^n] };
         If[rr=!=0,r=rr]
       ];
     r = Together[r];
     Expand[
     FindMainTerm[ Numerator[r],x->s] /
     FindMainTerm[ Denominator[r]//Factor,x->s]]
   ] 

 MainTerm[ expr_^n_,x_->s_ ] := MainTerm[expr,x->s]^n

 MainTerm[ EInfinity[expr_],x_->s_ ] := EInfinity[MainTerm[expr,x->s]]

 MainTerm[ expr_,x_->s_ ] := expr
  
 FindMainTerm[ expr_,x_->s_ ] := 
 	(Expand[expr]//.TInfinity[a_,b_,c_] :> limit)  /;
   Module[ {ind},
     If[ !FreeQ[expr,FailLimit] ||
         !FreeQ[expr//.TInfinity[a_,b_,c_] :> ind /; !NumberQ[N[a]] &&
                    And@@(FreeQ[a,#]&/@{TInfinity,LInfinity,EInfinity}),ind],
       True, False]
   ]

 FindMainTerm[ expr_,x_->s_ ] :=
   FindMainTerm[ Together[expr//.{EZero[a_] :> 1/EInfinity[-a]}],x->s ] /; 
 !FreeQ[expr,EZero]

 FindMainTerm[ expr_,x_->s_ ] := expr /;
 And@@(FreeQ[expr,#]&/@{x,LInfinity,EInfinity,TInfinity}) 

 FindMainTerm[ expr1_ expr2_,x_->s_ ] :=
   FindMainTerm[expr1,x->s] FindMainTerm[expr2,x->s]

 FindMainTerm[ expr_Plus,x_->DirectedInfinity[1] ] :=
   Module[ {max,r,var},
     max = MaxM[(r=Cases[expr+var, a_. x^n_.])/.a_. x^n_.:>n];
     If[ !FreeQ[max,Max], FailLimit,
	If[ Length[r]==0,
	    r=Cases[expr+var,a_. LInfinity[_]^_.]; var=expr,
	    r = Cases[Cases[var=Cases[r,a_. x^max],a_/;!FreeQ[a,LInfinity] ],
		a_. LInfinity[_]^_. x^max] 
	];
	If[ Length[r]==0, 
	    Plus@@var,
	    If[ Length[r]<2, 
		Plus@@r,
		FindMainLog[r,x->Infinity] 
	    ] 
	]
     ]
   ] /; 
 FreeQ[expr,EInfinity]

 FindMainTerm[ expr_Plus,x_->0 ] :=
   Module[ {r,var},
     r = Cases[expr+var, a_/;And@@(FreeQ[a,#]&/@{x,var,TInfinity})];
     If[ Length[r]==0,
         r=MinM[Cases[expr+var, a_. x^n_.]/.a_. x^n_.:>n];
         If[ FreeQ[r,Min] && FreeQ[r,DirectedInfinity] &&FreeQ[expr,TInfinity]
	     ,
             x^r FindMainTerm[Expand[expr/x^r]/.
               {
                (w_. x^p_)^n_ :> w^n x^Expand[p n]
               },x->0],
             expr
           ],
         var = Cases[r,a_/;!FreeQ[a,LInfinity]];
         If[ Length[var]==0,
	     If[ FreeQ[expr-r,TInfinity], Plus@@r, expr ],
             If[ Length[var]<2, Plus@@var, FindMainLog[var,x->0] ]
         ]
       ]
   ] /; 
 FreeQ[expr,EInfinity]

 FindMainTerm[ expr_Plus,x_->s_ ] :=
   Module[ {max,var,i=0,buft=1,main,exprn,k,level},
    exprn = ExpandAll[expr]//.EInfinityRule;
    If[ !FreeQ[ exprn, LInfinity[_] ],
        max =
        Plus@@((level[0]=Cases[exprn, a_. x^p_./;FreeQ[{a,p}, EInfinity[_]]])/.
		x^p_.->EInfinity[p LInfinity[1]]//.EInfinityRule);
        exprn = exprn - Plus@@level[0] + max
    ];
    level[0] = main = CasesN[exprn, a_. EInfinity[b_] ];
    If[ Length[main] == 0, expr, 
     If[ Length[main] == 1, 
         If[ FreeQ[SimpEInfinity[main[[1]],x->s],Sign], main[[1]], expr ],
     While[ !FreeQ[main,EInfinity],
            i+=1;
            level[i] = main =
             Complement[Union[(Plus@@Union[
                CasesN[#,a_. EInfinity[_]]/.a_. EInfinity[b_]:>b])&/@main],
                {0}]
          ];
     k = i;
     While[ Length[main]==1 && k>0, 
            k-=1; 
            main = level[k]; 
            buft = EInfinity[ level[k+1][[1]] ] 
          ];
     main = If[ FreeQ[main,EInfinity], main,
              If[Length[main] > 1,
                 Union[Flatten[Cases[#+var,a_/;!FreeQ[a,EInfinity]]&/@main]/. 
                       a_. EInfinity[_] :> a],
                 Union[Cases[main,a_/;!FreeQ[a,EInfinity]]/.
                       a_. EInfinity[_] :> a]
                ]
              ];
     If[ !FreeQ[main,TInfinity],expr,
      max = 
      If[ s===0,
          MinM[(Cases[#+var,a_. x^n_.]&/@main)/. a_. x^n_. :> n,
              If[ Length[k=Complement[Cases[#+var,a_/;
                   FreeQ[a,x]&&FreeQ[a,var]]&/@main,{{}}]] != 0,
                  0,Infinity ]],
          MaxM[(Cases[#+var,a_. x^n_.]&/@main)/. a_. x^n_. :> n,
              If[ Length[k=Complement[Cases[#+var,a_/;
                   FreeQ[a,x]&&FreeQ[a,var]]&/@main,{{}}]] != 0,
                  0,-Infinity ]] 
       ];
      If[ !FreeQ[max,Max] || !FreeQ[max,Min],
          FailLimit,
          main = 
          If[ max=!=0 && FreeQ[max,DirectedInfinity], 
              Cases[main, a_/;!FreeQ[a,x^max]],
              Cases[main, a_/;FreeQ[a,x]]
            ];
          If[ Length[main]==1,
            main = exprn = {main[[1]] buft}; i-=1;
            If[ i==0 && buft == 1, main = EInfinity[#]&/@main];
            While[ i>0,
                   main = If[main==={},exprn,EInfinity[#]&/@((
                      Cases[level[i],w_/;!FreeQ[w,#]]&/@main)[[1]])];
                   i-=1
                 ];
            If[main==={},main=exprn];
            main = 
            If[ Length[main] > 1,
                (Cases[level[i],w_/;!FreeQ[w,#]]&/@main)[[1]],
                Cases[level[i],w_/;!FreeQ[w,main[[1]]] ]
              ];
            If[ Length[main]<2, Plus@@main,
                (CasesN[main,a_. EInfinity[_]]/.
                 a_. EInfinity[b_] :> EInfinity[b])[[1]] *
                MainTerm[
                Plus@@(CasesN[main,a_. EInfinity[_]]/.
                       a_. EInfinity[b_] :> a),x->s]
              ]
	    ,
            If[ !FreeQ[main,LInfinity],
              main = Cases[main,a_/;!FreeQ[a,LInfinity]];
              If[ Length[main]!=1,
               If[ max=!=0 && FreeQ[max,DirectedInfinity],                   
                   main = x^max FindMainLogForExp[main/x^max,x->s],
                   main = FindMainLogForExp[main,x->s]
               ]
              ];
	      If[ !FreeQ[ main, FailLimit ], expr,
                  main = If[ Head[main]===List,{main[[1]] buft},
	                     {main buft} ]; i-=1;
	          If[ i==0, main = EInfinity[#]&/@main,
                      While[ i>0,
                          main = EInfinity[#]&/@((
                            Cases[level[i],w_/;!FreeQ[w,#]]&/@main)[[1]]);
                          i-=1
                      ]
	          ];
	          Plus@@((Cases[level[i],w_/;!FreeQ[w,#]]&/@main)[[1]])
	      ]  
	      ,
              If[ max!=0,
               If[ Length[main] > 1,
                   coef = 
                   MaxM[(Cases[#+var,a_. x^max]&/@main)/.a_. x^_. :> a],
                   coef = 
                   MaxM[Cases[main,a_. x^max]/.a_. x^_. :> a]
                 ];
               If[ FreeQ[coef,Max], 
                   main = Cases[main, a_/;!FreeQ[a,coef x^max] ]
                 ];
               ];
              main *= buft; i-=1;
              If[ i==0 && buft == 1, main = EInfinity[#]&/@main];
              While[ i>0,
                     If[ Length[main] > 1,
                         main = EInfinity[#]&/@((
                         Cases[level[i],w_/;!FreeQ[w,#]]&/@main)[[1]]),
                         main = EInfinity@@
                         Cases[level[i],w_/;!FreeQ[w,main]]
                       ];
                     i-=1
                   ];
              If[ Length[main]==1 && Length[ var=Union[Flatten[
                   Cases[level[i],w_/;!FreeQ[w,#]]&/@main]] ] > 1,
                  main[[1]] MainTerm[Plus@@var/main[[1]],x->s],
                  Plus@@Union[Flatten[
                   Cases[level[i],w_/;!FreeQ[w,#]]&/@main]] 
                ]
            ](* !FreeQ[main,LInfinity]           *)
          ] (* Length[main]==1                   *)
         ] (* !FreeQ[max,Max] || !FreeQ[max,Min] *)
        ] (* !FreeQ[main,TInfinity]              *)
       ] (* Length[main] == 1                    *)
      ] (* Length[main] == 0                     *)
  ]

 FindMainTerm[ expr_,x_->s_ ] := expr

 FindMainLog[ expr_List,x_->s_ ] :=
   Module[ {min,max,r,rr},
     min = Min[(r=expr//.TimesLog)//.{
           a_. LInfinity[n__]^_. :> a ws[{n}]}/.
           {ws[a_] ws[b_] :> ws[{a,b}]}/.c_. ws[a_] :> a ];
     max = Max[(rr=(r=Cases[r,a_. LInfinity[n___,min,m___]^_.])//.{
           a_. LInfinity[n__]^_. :> a ws[Count[{n},min]]}/.
           {ws[a_] ws[b_] :> ws[a+b]})/.c_. ws[a_] :> a ];
     r = Part[r,Flatten[Position[rr/.c_. ws[a_] :> a ,max]]];
     If[ Length[r]==1,
         Plus@@(r/. LInfinity[n__] :> Times@@(LInfinity[#]&/@{n})),
         ((LInfinity@@Table[min,{j,max}])/. 
            LInfinity[m__] :> Times@@(LInfinity[#]&/@{m})) *
         MainTerm[ Plus@@((r/LInfinity@@Table[min,{j,max}])/.
             {LInfinity[n__] LInfinity[k__]^(-1) :>
              If[ Length[{n}] > Length[{k}],
                  LInfinity@@ReducePar[{n},{k}],
                  1/(LInfinity@@ReducePar[{k},{n}])
                ]
             }/.{LInfinity[] :> 1}/. 
             LInfinity[n__] :> Times@@(LInfinity[#]&/@{n})),x->s ]
       ]
   ]

 ReducePar[{w1___,u_,w2___},{w3___,v_,w4___}] :=
      ReducePar[{w1,w2},{w3,w4}]/;u===v

 ReducePar[w_,{}] := w

 ReducePar[{},w_] := w

 FindMainLogForExp[ expr_List,x_->s_ ] :=
   Module[ {min,max,r,rr},
     min = Min[(r=expr//.TimesLog)//.{
           a_. LInfinity[n__]^_. :> a ws[{n}]}/.
           {ws[a_] ws[b_] :> ws[{a,b}]}/.c_. ws[a_] :> a ];
     max = Max[(rr=(r=Cases[r,a_. LInfinity[n___,min,m___]^_.])//.{
           a_. LInfinity[n__]^_. :> a ws[Count[{n},min]]}/.
           {ws[a_] ws[b_] :> ws[a+b]})/.c_. ws[a_] :> a ];
     r = Part[r,Flatten[Position[rr/.c_. ws[a_] :> a ,max]]];
     If[ Length[r]==1,
         Plus@@(r/. LInfinity[n__] :> Times@@(LInfinity[#]&/@{n})),
         (LInfinity@@Table[min,{j,max}])/. 
            LInfinity[m__] :> Times@@(LInfinity[#]&/@{m}) *
         (rr = (r/LInfinity@@Table[min,{j,max}])/.
             {LInfinity[n__] LInfinity[k__]^(-1) :>
              If[ Length[{n}] > Length[{k}],
                  LInfinity@@ReducePar[{n},{k}],
                  1/(LInfinity@@ReducePar[{k},{n}])
                ]
             }/.{LInfinity[] :> 1}/. 
             LInfinity[n__] :> Times@@(LInfinity[#]&/@{n});
          If[ s===Infinity, 
	      If[ FreeQ[r = MaxM[rr],Max],
                  r,FailLimit ],
	      If[ FreeQ[r = MinM[rr],Min],
                  r,FailLimit ]
	  ]  
          )
       ]
   ]

 GeneralRule[ expr_,x_ ] := 
   GeneralRule[expr/.
  {
  Sinh[a_] :> E^a/2 - E^(-a)/2,
  Csch[a_] :> 2E^a/(E^(2a) - 1),
  Cosh[a_] :> E^a/2 + E^(-a)/2,
  Sech[a_] :> 2E^a/(E^(2a) + 1)
  },x] /;
 Or@@(!FreeQ[expr,#]&/@{Sinh,Sech,Csch,Cosh})

 GeneralRule[ expr_,x_ ] := 
  (expr//.LogRule/.
       {Beta[a_,b_]       :> Gamma[a] Gamma[b]/Gamma[a+b]}/.
       {Binomial[a_,b_]   :> Gamma[a+1]/(Gamma[b+1] Gamma[a-b+1])}/.
       {Pochhammer[a_,b_] :> Gamma[a+b]/Gamma[a]}/.
       {Gamma[a_,0,z_]    :> Gamma[a] - Gamma[a,z]}/.
       {HoldPattern[ Derivative[q_,w_,e_][Gamma][a_,0,s_] ] :>
         Derivative[q][Gamma][a] -
         Derivative[q,e][Gamma][a,s]}/.
       {LogIntegral[z_/;!FreeQ[z,x]] :> ExpIntegralEi[Log[z]]})/.
       {Tanh[a_/;FreeQ[a,x]] :> (E^a-E^(-a))/(E^a+E^(-a)),
        Coth[a_/;FreeQ[a,x]] :> (E^a+E^(-a))/(E^a-E^(-a))
       }/.AbsRule
 

 LogRule = 
 {
  Log[a_ b_] :> Log[a]+Log[b],
  Log[a_^n_] :> n Log[a],
  Log[Complex[a_,b_]] :> Log[Abs[a+I b]] + I Arg[a+I b],
  Arg[a_/;FreeQ[a,Complex]] :> If[ N[a] >=0,0,Pi,Arg[a] ],
  Abs[a_/;FreeQ[a,Complex]] :> If[ N[a] >=0,a,-a,Abs[a] ],
  Sign[a_/;FreeQ[a,Complex] && NumberQ[N[a]]]:>
       If@@{N[a] >0, 1,If@@{N[a]<0,-1,If@@{N[a]===0,0,Sign[a]}}},
  Log[ E^a_ ] :> a 
 }

 TimesLog =
 { 
   LInfinity[n_]^k_Interger?Positive :> LInfinity@@Table[n,{i,k}],
   LInfinity[n_]^k_?Positive /; k>1 :> 
     LInfinity@@Table[n,{i,Floor[k]}] * LInfinity[n]^(k-Floor[k]),
   LInfinity[n__] LInfinity[m__] :> LInfinity[n,m]
 }

 OutputRule[ expr_,x_->s_ ] := expr//.
   {
   LInfinity[n_,m__] :> LInfinity[n] LInfinity[m],
   LInfinity[n_] :> Nest[Log,s,n]   
   }//.
   { 
   Log[Infinity]:>Infinity,
   Log[-Infinity]:>ComplexInfinity,
   TInfinity[a__] :> Indeterminate
   }//.EInfinity[n_] :> Exp[n]

 EInfinityRule =
  { 
    EInfinity[a_] EInfinity[b_] :> EInfinity[Expand[a+b]],
    EInfinity[a_]^n_ :> EInfinity[Expand[a n]],
    EZero[a_] :> EInfinity[a]
  } 
 
 EInfinityInv =
  {
    EInfinity[a_ + b_] :> EInfinity[a] EInfinity[b]
  }

 EInfinity[0] = 1
 EInfinity[DirectedInfinity[-1]] = 0
 EInfinity[DirectedInfinity[]] = DirectedInfinity[]
 EInfinity[DirectedInfinity[1]] = DirectedInfinity[1]
 EZero[0] = 1
 TInfinity[1] = 1

 CompDegree[expr_,x_] := expr//.
  {(a_ x)^m_     :> x^m a^m,
   (a_. x^p_)^m_ :> x^Expand[p m] a^m}

 FirstNotZero[ {0,w___} ] := 0

 FirstNotZero[ {a_,w___} ] := a/.{sign[0] :> If[ direction,1,0 ]}

 listfun = {integrate,Factorial,Gamma,Binomial,ExpIntegralE,
  ExpIntegralEi,SinIntegral,CosIntegral,Erf,Erfc,PolyGamma,LogIntegral,
  FresnelS,FresnelC,Zeta,PolyLog,PolyGamma,Sign,Abs,sign,ChebyshevU,
  ChebyshevT}
  
 AnalysList[ expr_List,x_ ] := Analys[#,x]&/@expr

 Analys[ 0,x_->s_ ] := 0
 Analys[ Indeterminate,x_->s_ ] := Indeterminate  
 Analys[ expr_,x_->s_ ] := FailLimit /; BadWords[ToString[expr]]

 Analys[ expr_,x_->0 ] := FirstNotZero[CoefficientList[expr,x]] /;
 PolynomialQ[expr,x] &&
 And@@(FreeQ[expr,#]&/@{LInfinity,EInfinity,EZero,TInfinity,sign}) 

 Analys[ expr_,x_->s_ ] := 
   Module[ {r,rr},
   rr = expr//.{TInfinity[a_] d_ :> TInfinity[a d],
                TInfinity[a_] + TInfinity[d_] :> TInfinity[a+d],
                TInfinity[a_] + d_ :> TInfinity[a + d]}/.
        TInfinity[ TInfinity[a__] ] :> TInfinity[a];
   rr = rr//.w_/Sign[w_] :> Abs[w];
   If[ MatchQ[ rr,TInfinity[a_] ],rr = rr[[1]] ];
   If[ !FreeQ[rr,EInfinity] || !FreeQ[rr,EZero],
       r = PartEZero[ rr,x,card],
       r = Expand[rr]];
   If[ !FreeQ[expr,LInfinity] && Head[r]===Plus,
       rr = CasesN[r,a_. LInfinity[_]^_];
       r = (r-Plus@@rr) + 
           Plus@@(rr/.{a_. LInfinity[_]^m_ :> 0 /; Abs[N[m]] > card+1}) ];
   r = CompDegree[r,x];
   If[ !cardFlag,
       rr = CasesN[r,a_. x^m_.];
       r = (r-Plus@@rr) + Plus@@(rr/.{a_. x^m_ :> 0 /; 
           Abs[N[m]] > If[card==1,2,card]})
     ];
   r = r//.{ (LInfinity[a_]^n_)^p_ :> LInfinity[a]^(n p) };
   Analys1[ MainTerm[r,x->s]//.EInfinityRule, x->s]
   ]

 Analys1[ expr_,x_->s_ ] := FailLimit /; !FreeQ[expr,FailLimit]

 Analys1[ expr_,x_->s_ ] := 
   Module[ {inter,ind,infinity},
     DeactivateMessages[{Power::infy,Power::indet,Infinity::indet,General::indet,
         General::dbyz,Min::nord,Max::nord,List::nord,Interval::nvld},
     If[ direction,
	 Unprotect[Integer];
	 Power[0,n_?Negative] ^:= DirectedInfinity[1]
     ];
      Unprotect[Power,Plus,Log,Sin, Cos,Interval];
      E^Interval[z_] := Interval[Exp@@{z}];
      Log[Interval[z_]] := Interval[Log@@{z}];
      c_. Interval[z_] + d_. Interval[y_] := Indeterminate/;
      z===y && (c+d)===0;
      Sin[ComplexInfinity] := Interval[{-1,1}];
      Cos[ComplexInfinity] := Interval[{-1,1}];
      Interval[{a_,a_}] := a;
   
     inter = 
     If[ !FreeQ[expr//.TInfinity[__]^k_?Negative :> ind,ind],
         Indeterminate,
     If[ !FreeQ[expr//.TInfinity[a_,b__] :> ind /;And@@(FreeQ[a,#]&/@{
         LInfinity,TInfinity,EInfinity}) && !NumberQ[N[a]],ind],
         FailLimit,
	 ind =    
         SimpEInfinity[expr,x->s] //.{
             Sign[Abs[w_]] :> 1,
             EInfinity[a_/;Znak[a]] :> 1/EInfinity[-a],
	     TInfinity[a_,0,c_]     :> c@@{OutputRule[a,x->s]},
             TInfinity[a_,b_,c_]    :> c@@{a s^b}  
                 }//.{
             Interval[{c_,d_}] :> N[Interval[{c,d}]],
             TInfinity[a_]:>a
                 }/.{Min->MinM,Max->MaxM};
      E^Interval[z_] =.;
      Log[Interval[z_]] = .;
      c_. Interval[z_] + d_. Interval[y_] =.;
      Sin[ComplexInfinity] =.;
      Cos[ComplexInfinity] =.;
      Protect[Power,Plus,Log,Sin,Cos];
          ind = Block[ {DirectedInfinity}, ind/.x->s/.
		{
	        HoldPattern[ DirectedInfinity[] c_ ] :> c DirectedInfinity[1] /;
	        And@@(FreeQ[c,#]&/@{EInfinity})&&!NumberQ[N[c]] 
		} ]/.Infinity -> infinity/.infinity -> DirectedInfinity[1];
	  ind/.{Min->MinM,Max->MaxM}/.{
             Interval[{a_/;Znak[a],b_}] :>
                         -Interval[ {-b,-a} ] }//.
             {0^Abs[_] :> 0,
             Interval[{ComplexInfinity,ComplexInfinity}] :>
                                ComplexInfinity,
             DirectedInfinity[a_] LInfinity[_]^_. :>
              If[s===0 && !direction, -1,1] DirectedInfinity[a]}/.{
             Interval[a_] :> Indeterminate/;FreeQ[a,LInfinity]}
       ]];
     If[ !FreeQ[inter,LInfinity] && Head[inter]===Times,
         ind = Cases[inter,a_. LInfinity[_]^_.];
         If[ Length[ind] > 1,
             inter = FindMainLog[ind,x->s]
       ]];
     If[ direction, 
	 Integer/:Power[0,n_?Negative] =.;
	 Protect[Integer]
     ];
     inter = OutputRule[ SimplifyLength[inter],x->s]/.
      {DirectedInfinity[a_/;!FreeQ[a,Complex]] :> ComplexInfinity}/.
        {sign[0]^_?Negative :> If[ direction,1, Indeterminate ]}/.
	{sign[0] :> If[ direction,1,0 ]};
     Interval[{a_,a_}] =.;
     Protect[ Interval ];
     ];  (* DeactivateMessages *)
     If[ !FreeQ[inter,sign],FailLimit,inter ]
   ]

 SimpEInfinity[ a_,x_ ] := a /; FreeQ[a,EInfinity]

 SimpEInfinity[ a_ + b_,x_ ] := 
    SimpEInfinity[a,x] + SimpEInfinity[b,x] /; !FreeQ[{a,b},EInfinity]

 SimpEInfinity[ a_. EInfinity[b_],x_->s_ ] :=
    SignN[a,x,1] EInfinity[ SimpEInfinity1[b,x->s] ]

 SimpEInfinity[ b_,x_->s_ ] := b

 SimpEInfinity1[ a_. EInfinity[b_],x_->s_ ] :=
    SignN[a,x,2] *
    EInfinity[ SimpEInfinity1[b,x->s] ]

 SimpEInfinity1[ b_Plus,x_->s_ ] := 
   Module[ {answer}, 
     answer = 
      MainTerm[MainTerm[
        If[ !FreeQ[b,EInfinity] || !FreeQ[b,EZero],
            PartEZero[ b,x->s,card],
            b],
      x->s]//.EInfinityRule,x->s];
      If[ Head[answer]===Times,
          SimpEInfinity1[answer,x->s],
          answer]
   ]

 SimpEInfinity1[ b_,x_->s_ ] := SignN[b,x,2]

 SignN[ expr_Times,x_,n_ ] := 
 (SignN[#,x,n]&/@expr)//.{
 Sign[ w_/;!FreeQ[w,LInfinity] || !FreeQ[w,TInfinity] ] :> w}//.{
 Sign[a_] Sign[b_] :> Sign[a b]} 

 SignN[ expr_,x_,n_ ] :=    
   If[ NumberQ[N[expr]] && FreeQ[N[expr],Complex],
       Sign[N[expr]],
       If[n==1,expr,Sign[expr] ]
     ] /; 
 And@@(FreeQ[expr,#]&/@{x,LInfinity,TInfinity})

 SignN[ expr_,x_,1] := 1

 SignN[ expr_,x_,2] := expr
 
 PartEZero[ c_ expr_,x_,n_ ] := c PartEZero[expr,x,n] /; 
 FreeQ[c,EInfinity] && FreeQ[c,EZero]

 PartEZero[ expr_,x_,n_ ] :=
   Module[ { r,rr},
     rr=Expand[expr]//.EInfinityRule;
     If[ FreeQ[rr,EInfinity], rr,
         r = CasesN[rr,a_. EInfinity[w_] ];
         (rr - Plus@@r) + Plus@@(PartEZero1[#,x,n]&/@r)
       ]
   ]

 PartEZero1[ b_. EInfinity[expr_/;!FreeQ[expr,EInfinity]],x_,n_ ] :=
   Module[ {r,var,exprm,exprn},
    exprm = PartEZero[expr,x,n];
    r = CasesN[exprm,a_. x^m_];
    If[ Length[r]==0, exprn = exprm,
      exprn = (exprm-Plus@@r) + 
      Plus@@(r/.{a_. x^m_. :> 0 /; Abs[N[m]] > n || Abs[N[a]] > n}) ];
    If[ exprn===0,0,
        r = Cases[exprn+var,a_/;Znak[a]];
        EInfinity[exprn-Plus@@r]/ EInfinity[-Plus@@r]
      ] *
    ( r = CasesN[b,a_. x^m_];
      If[ Length[r]==0,b,
          (b-Plus@@r) + 
          Plus@@(r/.{a_. x^m_. :> 0 /; Abs[N[m]] > n})
        ]
    )
   ] /; !cardFlag

 PartEZero1[ b_. EInfinity[a_. x_^m_.],x_,n_ ] :=  0 /;!cardFlag &&
 Abs[N[m]] > n || Abs[a] > n

 PartEZero1[ b_. EInfinity[a_ x_^m_.],x_,n_ ] :=  
   b/EInfinity[-a x^m] /;Znak[a] 

 PartEZero1[ b_. EInfinity[expr_],x_,n_ ] := 
   Module[ {r,var,exprn},
    r = CasesN[expr,a_. x^m_];
    If[ Length[r]==0, exprn=expr,
      exprn = (expr-Plus@@r) + 
      Plus@@(r/.{a_. x^m_. :> 0 /; Abs[N[m]] > n || Abs[N[a]] > n})];
    If[ exprn===0,0,
        r = Cases[exprn+var,a_/;Znak[a]];
        EInfinity[exprn-Plus@@r]/ EInfinity[-Plus@@r]
      ] *
    ( r = CasesN[b,a_. x^m_];
      If[ Length[r]==0,b,
          (b-Plus@@r) + 
            Plus@@(r/.{a_. x^m_. :> 0 /; Abs[N[m]] > n})
       ]
    )
   ] /; !cardFlag

 PartEZero1[ expr_,x_,n_ ] := expr

 MinM[a___] := MinM1[Union[Flatten[{a}]]] 

 MinM1[{}] := DirectedInfinity[1]

 MinM1[{ComplexInfinity,a__}] := MinM1[{a}]

 MinM1[{DirectedInfinity[w_/;!FreeQ[w,Complex]],a__}] := MinM1[{a}]

 MinM1[{ComplexInfinity}] := ComplexInfinity
 
 MinM1[{DirectedInfinity[w_/;!FreeQ[w,Complex]]}] := ComplexInfinity

 MinM1[a_List] := MinMM[Flatten[a]] 

 MinMM[a_List] := 
   If[ !FreeQ[a,Complex], MinMMM[a],
       Module[ {r},
        r = Min[N[a]];
        If[ FreeQ[r,Min],
            a[[ Position[ N[a],r ][[1,1]] ]] ,
            MinMMM[a]
          ]
       ]
   ]

 MinMMM[ {a___,0,b___} ] := 0

 MinMMM[ a_ ] := 
   Module[ {r},
     If[ Znak[ N[a[[1]]] ], 
         r = N[-a[[1]]] * Min[N[a/(-a[[1]]) ]],
         r = N[a[[1]]] * Min[N[a/a[[1]] ]]
       ];
     If[ FreeQ[r,Min],
         a[[ Position[ N[a]/1.,r ][[1,1]] ]] ,
          Min[a]
       ] 
  ] 

 MaxM[a___] := MaxM1[Union[Flatten[{a}]]] 

 MaxM[{}] := DirectedInfinity[-1]

 MaxM1[{ComplexInfinity,a___}] := ComplexInfinity

 MaxM1[{DirectedInfinity[w_/;!FreeQ[w,Complex]],a___}] := 
 ComplexInfinity

 MaxM1[a_List] := MaxMM[Flatten[a]] 

 MaxMM[a_List] := 
   If[ !FreeQ[a,Complex], MaxMMM[a],
       Module[ {r},
        r = Max[N[a]];
        If[ FreeQ[r,Max],
            a[[ Position[ N[a],r ][[1,1]] ]] ,
            MaxMMM[a]
          ]
       ]
   ]

 MaxMMM[ {a___,0,b___} ] := MaxMMM[{a,b}]

 MaxMMM[ a_ ] :=
   Module[ {r},
     If[ Znak[ N[a[[1]]] ], 
         r = N[-a[[1]]] * Max[N[a/(-a[[1]]) ]],
         r = N[a[[1]]] * Max[N[a/a[[1]] ]]
       ];
     If[ FreeQ[r,Max],
         a[[ Position[ N[a]/1.,r ][[1,1]] ]] ,
            Max[a]
       ] 
  ] 

 CasesN[expr_,w_] := 
  If[ MatchQ[expr,w],{expr}, Cases[expr,w] ] 
 
 BadWords[ expr_ ] := True /;
 Or@@(!SameQ[Length[StringPosition[expr,#]],0]&/@{
 "Asymptotic","Simp","Fail"})

 BadWords[ _ ] := False

 Determinate[ expr_,x_->s_ ] := False /; 
 Or@@(!FreeQ[expr,#]&/@{integrate,if,sign,next})

 Determinate[ expr_,x_->s_ ] := 
   Module[ 
    {answer =
      Module[ {test},
      DeactivateMessages[{Power::infy,Power::indet,Infinity::indet,General::indet,
            General::dbyz},
	Unprotect[Integer,Log,Factorial,Binomial];
	Power[ 0, n_?Negative ] ^:= Indeterminate;
        Power[ 1, c_/;!FreeQ[c,DirectedInfinity] ] ^:= Indeterminate;
       	Log[ c__/;!FreeQ[{c},DirectedInfinity]||!FreeQ[{c},Indeterminate]] = 
		Indeterminate;
	Factorial[ c_/;!FreeQ[c,DirectedInfinity] ] = Indeterminate;      
	Binomial[ c__/;!FreeQ[{c},DirectedInfinity] ] = Indeterminate;      
        Times[ 0, c_/;!FreeQ[c,Indeterminate] || 
		      !FreeQ[c,DirectedInfinity]] ^:= Indeterminate;
        test = {expr/.x->s,N[expr]/.x->s}/.{
	   0^n_ -> Indeterminate, 0.^n_ -> Indeterminate};    
    ]; (* DeactivateMessages *)
	Integer/:Power[ 0, n_?Negative ] =.;
	Integer/:Power[ 1, c_/;!FreeQ[c,DirectedInfinity] ] =.;
	Integer/:Times[ 0,c_/;!FreeQ[c,Indeterminate] || 
			      !FreeQ[c, DirectedInfinity]] =.;
	Log[ c__/;!FreeQ[{c},DirectedInfinity]||!FreeQ[{c},Indeterminate]] =.;
	Factorial[ c_/;!FreeQ[c,DirectedInfinity] ] =.;
	Binomial[ c__/;!FreeQ[{c},DirectedInfinity] ] =.;      
	Protect[Integer,Log,Factorial,Binomial];
        test
     ]},
   True /; And@@(FreeQ[answer,#]&/@{
    DirectedInfinity,ComplexInfinity,Indeterminate,Interval})
  ]

 Determinate[ __ ] := False

 IntegerNonPositive[ expr_Integer /; expr<=0 ] :=  True

 IntegerNonPositive[ _ ] := False

 ZnakLim[ n_?NumberQ a_.,x_->s_] := True /; Negative[n]
 
 ZnakLim[ expr_,x_->s_] := Znak[ N[Analys1[expr,x->s]] ]
 
 Znak[ DirectedInfinity[-1] a_. ] := True

 Znak[ n_?NumberQ a_ ] := True /; Negative[n]
 
 Znak[ n_?NumberQ ] := True /; Negative[n]

 Znak[ _ ] := False

(*======================= INTEGRATE ======================================*)

 ToExpression[StringJoin[
 context,"Asymptotic",context,"integrate"]][ expr_,arg_,x_->s_ ] :=
   Integrate@@{Analys[expr,x->s],arg}

(*======================= SIGN, ABS ======================================*)

 AsymptoticSign[ expr_,x_->s_ ] :=
   AsymptoticForSign[ expr,x->s,Analys[expr,x->s] ]

 AsymptoticForSign[ expr_,x_->s_,w_/;Positive[N[w]] ] := 1

 AsymptoticForSign[ expr_,x_->s_,w_/;Negative[N[w]] ] := -1

 AsymptoticForSign[ expr_,x_->s_,0 ] := 
 If[ ZnakLim[expr,x->s], -sign[0], sign[0] ]

 AsymptoticForSign[ expr_,x_->s_,w_ ] := Sign[w]

 AsymptoticAbs[ expr_/;FreeQ[expr,Complex],x_->s_ ] :=
    expr/AsymptoticForSign[ expr,x->s,Analys[expr,x->s] ]

 AsymptoticAbs[ expr_,x_->s_ ] :=
   Module[ { i,r },
     r = Collect[expr//.Complex[a_,b_] :> a+ i b,i];
     If[ MatchQ[r, a_. i + b_/; FreeQ[{a,b},i]],
	 r = Sqrt[(r/.i->0)^2 + ((r - (r/.i->0))/.i->1)^2];
	 asymptotic = False;
	 SimpFunction[ r,x->s ],
	 FailLimit
     ]
   ]

 AbsRule =
  {
    Abs[ z_/;NumberQ[N[z]] && FreeQ[z,Complex] ] :> 
	If[ Positive[N[z]],z,-z] /; Accuracy[z] === Infinity, 
    Abs[ z_/;NumberQ[N[z]] && !FreeQ[z,Complex] ] :> 
	Block[ { answer,Abs },
		answer = SimpNumbAbs[z];
		answer /; Head[answer] =!=Abs
	] /; Accuracy[z] === Infinity 
  }

 SimpNumbAbs[ a_ b_ ] := SimpNumbAbs[a] SimpNumbAbs[b]

 SimpNumbAbs[ a_^b_/;FreeQ[b,Complex] ] := SimpNumbAbs[a]^b

 SimpNumbAbs[ a_^b_/;!FreeQ[b,Complex] ] := Abs[a^b]

 SimpNumbAbs[ expr_Plus ] :=
   Module[ { i,r },
     r = Collect[expr//.Complex[a_,b_] :> a+ i b,i];
     If[ MatchQ[r, a_. i + b_/; FreeQ[{a,b},i]],
	 Sqrt[(r/.i->0)^2 + ((r - (r/.i->0))/.i->1)^2],
	 Abs[expr]
     ]
   ] /; !FreeQ[expr,Complex]

 SimpNumbAbs[ Complex[a_,b_] ] := Sqrt[a^2 + b^2] 

 SimpNumbAbs[ z_/;FreeQ[z,Complex] ] := If[ Positive[N[z]],z,-z]

 SimpNumbAbs[ expr_ ] := Abs[expr]

(*======================= GAMMA ==========================================*)

 AsymptoticFactorial[a_. x_,x_->DirectedInfinity[1] ] := 
    Sqrt[2 Pi a] Sqrt[x] *
    EInfinity[a x (-1 + LInfinity[1]) + a x Log[a]] /;
 card==1 && NumberQ[N[a]] && Positive[N[a]]

 AsymptoticFactorial[u_,x_->s_] := 
   AsymptoticForGamma[u+1,x->s,Analys[u+1,x->s]]

 AsymptoticGamma[u_,x_->s_] := AsymptoticForGamma[u,x->s,Analys[u,x->s]]

 AsymptoticForGamma[u_,x_->s_,w_] := 
    AsymptoticForGamma[u+1,x->s,w+1] AsymptoticPower[u,-1,x->s] /;
 IntegerNonPositive[w]

 AsymptoticForGamma[u_,x_->s_,w_. DirectedInfinity[-1]] := 
   Pi AsymptoticPower[AsymptoticSin[Pi u,x->s],-1,x->s] *
   AsymptoticPower[AsymptoticForGamma[1-u,x->s,Infinity],-1,x->s]
 
 AsymptoticForGamma[u_,x_->s_,w_. DirectedInfinity[1] ] := 
   AsymptoticPower[E,Expand[-u +(u-1/2) AsymptoticLog[u,x->s]] /.
   {a_. x^n_. :>0 /; Abs[n]>card },x->s] *
   Sqrt[2] Sqrt[Pi] (Expand[Times@@Table[ Sum[
   (-1)^(indsum (indpr+1))  If[BernoulliB[indpr+1]==0,1,
   BernoulliB[indpr+1]^indsum ] *
   AsymptoticPower[u,-indpr indsum,x->s] / 
   (indpr^indsum (indpr+1)^indsum indsum!),
   {indsum,0,If[cardFlag,1,card-1]}],{indpr,1,If[cardFlag,2,card]}]]/.
   a_. x^n_. :>0 /; Abs[n]>card)

 AsymptoticForGamma[u_,x_->s_,Indeterminate] := TInfinity[Gamma[u]]

 AsymptoticForGamma[u_,x_->s_,w_ ] :=
   Module[ {var},
     Normal[Series[Gamma[var],{var,w,card}]]/.var->u
   ]

(*------------------------------------------------------------------------*)

 AsymptoticGamma[u_,0,v_,x_->s_] := 
   AsymptoticForGammaD[u,0,v,x->s,Analys[u,x->s],Analys[v,x->s]]

 AsymptoticForGammaD[u_,0,v_,x_->s_,w_,ww_] := 
   AsymptoticForGamma[u,x->s,w] -
   AsymptoticForGammaDD[u,v,x->s,w,ww]

(*------------------------------------------------------------------------*)

 AsymptoticGamma[ u_,v_,x_->s_ ] := 
   AsymptoticForGammaDD[u,v,x->s,Analys[u,x->s],Analys[v,x->s]]

 AsymptoticForGammaDD[ u_,v_,x_->s_,w_,ww_ ] := 
     Normal[Series[Gamma[u,v],{x,s,card}]] /;
 FreeQ[{ww,w},DirectedInfinity] && FreeQ[{ww,w},Indeterminate]

 AsymptoticForGammaDD[ a_,u_,x_->s_,w_,ww_. DirectedInfinity[1] ] := 
   AsymptoticPower[u,a-1,x->s] AsymptoticPower[E,-u,x->s]*
   Sum[ (-1)^ind Pochhammer[1-a,ind] AsymptoticPower[u,-ind,x->s],
      {ind,0,If[cardFlag,1,card-1]}] /;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticForGammaDD[ a_,u_,x_->s_,w_. DirectedInfinity[1],ww_ ] := 
   AsymptoticForGamma[a,x->s,Infinity] -
   Sum[ (-1)^ind AsymptoticPower[u,a+ind,x->s]/
        ((a+ind) ind!), {ind,0,If[cardFlag,1,card-1]}] /;
 And@@(FreeQ[w,#]&/@{DirectedInfinity,Indeterminate})

 AsymptoticForGammaDD[ __ ] := FailLimit 

(*===================== POLYGAMMA ==========================================*)

 AsymptoticPolyGamma[u_,x_->s_] := 
   AsymptoticForPolyGamma[0,u,x->s,Analys[u,x->s]]

 AsymptoticPolyGamma[k_Integer?NonNegative,u_,x_->s_] := 
   AsymptoticForPolyGamma[k,u,x->s,Analys[u,x->s]]

 AsymptoticPolyGamma[ u__ ] := {u} FailLimit

 AsymptoticForPolyGamma[k_,u_,x_->s_,w_] := 
   ( AsymptoticForPolyGamma[k,u+1,x->s,w+1] - 
     (-1)^k k! AsymptoticPower[u,-k-1,x->s] ) /;
 IntegerNonPositive[w] 

 AsymptoticForPolyGamma[ 0,u_,x_->s_,
                         w_/;!FreeQ[w, DirectedInfinity] ] := 
   AsymptoticLog[u,x->s] - AsymptoticPower[u,-1,x->s]/2 -
   Sum[ BernoulliB[2 ind]/(2 ind) AsymptoticPower[u,-2 ind,x->s],
      {ind,1,If[cardFlag,2,card]}]

 AsymptoticForPolyGamma[ k_,u_,x_->s_,
                         w_/;!FreeQ[w, DirectedInfinity] ] := 
   (-1)^(k-1) *
   ( (k-1)! AsymptoticPower[u,-k,x->s] +
     k!/2  AsymptoticPower[u,-k-1,x->s] +
     Sum[ AsymptoticPower[u,-2ind-k,x->s] BernoulliB[2 ind] *
          (2ind+k-1)!/(2 ind)!,
        {ind,1,If[cardFlag,1,card-1]} ] ) 

 AsymptoticForPolyGamma[k_,u_,x_->s_,Indeterminate] := 
   TInfinity[PolyGamma[k,u]]

 AsymptoticForPolyGamma[k_,u_,x_->s_,w_ ] :=
   Module[ {var},
     Normal[Series[PolyGamma[k,var],{var,w,card}]]/.{ var :> u}
   ]

(*===================== ZETA ===============================================*)

 AsymptoticZeta[ u_,x_->s_ ] := 
   AsymptoticForZeta[u,x->s,Analys[u,x->s]] 

 AsymptoticForZeta[ u_,x_->s_,1 ] := 
   Pi^(-1)*
   AsymptoticPower[2 Pi,u,x->s] * 
   AsymptoticForGamma[2-u,x->s,1] *
   AsymptoticForZeta[1-u,x->s,0] *
   AsymptoticSin[Pi u/2,x->s] AsymptoticPower[1-u,-1,x->s] 

 AsymptoticZeta[u_,v_,x_->s_] := 
   (AsymptoticPower[u-1,-1,x->s] - AsymptoticPolyGamma[0,v,x->s]) /;
 Analys[u,x->s]===1   

 AsymptoticZeta[ u_,v_,x_->s_ ] := 
   AsymptoticForZeta[u,v,x->s,Analys[v,x->s]] /;
 And@@(FreeQ[u,#]&/@{x,LInfinity,EInfinity,TInfinity})

 AsymptoticZeta[ u__ ] := {u} FailLimit

 AsymptoticForZeta[ u_,v_,x_->s_,w_ ] :=
   (Sum[ AsymptoticPower[ind+v,-u,x->s],{ind,0,-w} ] + 
    AsymptoticForZeta[u,v-w+1,x->s,1] ) /;
 IntegerNonPositive[w]

 AsymptoticForZeta[ u__,x_->s_,Indeterminate ] := TInfinity[Zeta[u]]

 AsymptoticForZeta[ u__,x_->s_,w_/;FreeQ[w,DirectedInfinity] ] := 
 Taylor[Zeta[u],x->s] 

 AsymptoticForZeta[ u__ ] := {u} FailLimit

(*===================== ExpIntegralE ========================================*)

 AsymptoticExpIntegralE[ k_,u_,x_->s_ ] := 
   AsymptoticForExpIntegralE[k,u,x->s,Analys[u,x->s]] /;
 And@@(FreeQ[k,#]&/@{x,LInfinity,EInfinity,TInfinity})

 AsymptoticExpIntegralE[ u__ ] := {u} FailLimit

 AsymptoticForExpIntegralE[ k_,u_,x_->s_,
                             w_/;!FreeQ[w,DirectedInfinity] ] :=
   AsymptoticPower[E,-u,x->s] *
   Sum[ (-1)^ind Pochhammer[k,ind] AsymptoticPower[u,-ind-1,x->s],
      {ind,0,If[cardFlag,1,card]}] 

 AsymptoticForExpIntegralE[ k_,u_,x_->s_,Indeterminate ] :=
   TInfinity[ExpIntegralE[k,u]]

 AsymptoticForExpIntegralE[ k_,u_,x_->s_,w_ ] :=
   Module[ {var},
     AsympSimplify[SimpFunction[
     Taylor[ ExpIntegralE[k,var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== ExpIntegralEi =======================================*)

 AsymptoticExpIntegralEi[ u_,x_->s_ ] := 
   AsymptoticForExpIntegralEi[u,x->s,Analys[u,x->s]] 

 AsymptoticForExpIntegralEi[ u_,x_->s_,
                             w_/;!FreeQ[w,DirectedInfinity] ] :=
   AsymptoticPower[E,u,x->s] *
   Sum[ (ind-1)! AsymptoticPower[u,-ind,x->s],{ind,1,If[cardFlag,2,card+1]}] 

 AsymptoticForExpIntegralEi[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ExpIntegralEi[u]]

 AsymptoticForExpIntegralEi[ u_,x_->s_,0 ] :=
   EulerGamma + AsymptoticLog[u,x->s] +
   Sum[ AsymptoticPower[u,ind,x->s]/(ind ind!),{ind,1,If[cardFlag,2,card]} ]

 AsymptoticForExpIntegralEi[ u_,x_->s_,w_ ] :=
   Module[ {var},
     AsympSimplify[SimpFunction[
     Taylor[ ExpIntegralEi[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== Erf && Erfc ========================================*)

 AsymptoticErfc[ u_,x_->s_ ] := 
   AsymptoticForErfc[u,x->s,Analys[u,x->s]]

 AsymptoticForErfc[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   2 - AsymptoticForErfc[-u,x->s,Infinity]

 AsymptoticForErfc[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   AsymptoticPower[E,-u^2,x->s]/Sqrt[Pi] *
   AsymptoticPower[u,-1,x->s] * 
   (1 + Sum[ (-1)^ind (2 ind-1)!! 2^(-ind) *
             AsymptoticPower[u,-2 ind,x->s],{ind,1,If[cardFlag,2,card]}])

 AsymptoticForErfc[ u_,x_->s_,Indeterminate ] := TInfinity[ Erfc[u] ]

 AsymptoticForErfc[ u_,x_->s_,w_ ] := 
   Module[ {var},
     Taylor[ Erfc[var],var->w ]/.var->u
   ]

 AsymptoticErf[ u_,x_->s_ ] := 
   1 - AsymptoticForErfc[u,x->s,Analys[u,x->s]]


(*===================== ArcTan ==============================================*)

 AsymptoticArcTan[ u_,x_->s_ ] := 
   AsymptoticForArcTan[u,x->s,Analys[u,x->s]]

 AsymptoticForArcTan[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   -Pi/2 - 
   Sum[ (-1)^ind AsymptoticPower[u,-2 ind+1,x->s]/(2 ind-1),
      {ind,1,If[cardFlag,2,card]}] 

 AsymptoticForArcTan[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   Pi/2 + 
   Sum[ (-1)^ind AsymptoticPower[u,-2 ind+1,x->s]/(2 ind-1),
      {ind,1,If[cardFlag,2,card]}] 

 AsymptoticForArcTan[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ ArcTan[u] ]

 AsymptoticForArcTan[ u_,x_->s_,w_ ] :=
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcTan[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== ArcCot ==============================================*)

 AsymptoticArcCot[ u_,x_->s_ ] := 
   AsymptoticForArcCot[u,x->s,Analys[u,x->s]]

 AsymptoticForArcCot[u_/; FreeQ[u,Complex],x_->s_,w_. DirectedInfinity[-1] ] :=
   Sum[ (-1)^ind AsymptoticPower[u,-2 ind+1,x->s]/(2 ind-1),
      {ind,1,If[cardFlag,2,card]}] 

 AsymptoticForArcCot[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   Sum[ (-1)^(ind-1) AsymptoticPower[u,-2 ind+1,x->s]/(2 ind-1),
      {ind,1,If[cardFlag,2,card]}] 

 AsymptoticForArcCot[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ ArcCot[u] ]

 AsymptoticForArcCot[ u_,x_->s_,w_ ] :=
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcCot[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*========================================================================
             ArcSec && ArcCsc & ArcSin && ArcCos         
 =========================================================================*)

 AsymptoticArcSec[ u_,x_->s_ ] := 
   AsymptoticArcCos[AsymptoticPower[u,-1,x->s],x->s]

 AsymptoticArcCsc[ u_,x_->s_ ] := 
   AsymptoticArcSin[AsymptoticPower[u,-1,x->s],x->s]

 AsymptoticArcSin[ u_,x_->s_ ] := 
   AsymptoticForArcSin[u,x->s,Analys[u,x->s]]

 AsymptoticArcCos[ u_,x_->s_ ] := 
   AsymptoticForArcCos[u,x->s,Analys[u,x->s]]

 AsymptoticForArcSin[u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] := 
   -I AsymptoticForArcSinh[I u,x->s,w]

 AsymptoticForArcSin[u_,x_->s_,w_ ] := 
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcSin[var],var->w ]/.var->u,x->s],x,card,True]
   ]

 AsymptoticForArcCos[u_,x_->s_,w_. DirectedInfinity[-1] ] := 
   -I AsymptoticForArcCosh[u,x->s,DirectedInfinity[-1]]

 AsymptoticForArcCos[u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] := 
   I AsymptoticForArcCosh[u,x->s,w]

 AsymptoticForArcCos[u_,x_->s_,w_ ] := 
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcCos[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== ArcSinh =============================================*)

 AsymptoticArcSinh[ u_,x_->s_ ] := 
   AsymptoticForArcSinh[u,x->s,Analys[u,x->s]]

 AsymptoticForArcSinh[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   Log[2] + AsympSimplify[ AsymptoticLog[u,x->s] + 
   Sum[ (-1)^ind (2 ind-1)!!/( (2 ind)!! (2 ind) ) *
      AsymptoticPower[u,-2 ind,x->s],
      {ind,1,If[cardFlag,2,card]}],
   x,card,True] 

 AsymptoticForArcSinh[ u_,x_->s_,Indeterminate ] := TInfinity[ ArcSinh[u] ]

 AsymptoticForArcSinh[ u_,x_->s_,w_ ] := 
   Module[ {var},
     SimpFunction[Taylor[ ArcSinh[var],var->w ]/.var->u,x->s]
   ]

(*===================== ArcCosh =============================================*)

 AsymptoticArcCosh[ u_,x_->s_ ] := 
   AsymptoticForArcCosh[u,x->s,Analys[u,x->s]]

 AsymptoticForArcCosh[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   Log[2] + AsymptoticLog[u,x->s] - 
   Sum[ (2 ind-1)!!/( (2 ind)!! (2 ind) ) *
      AsymptoticPower[u,-2 ind,x->s],
      {ind,1,If[cardFlag,2,card]}] 

 AsymptoticForArcCosh[ u_,x_->s_,Indeterminate ] := TInfinity[ ArcCosh[u] ]

 AsymptoticForArcCosh[ u_,x_->s_,w_ ] := 
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcCosh[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== ArcTanh ============================================*)

 AsymptoticArcTanh[ u_,x_->s_ ] :=
   AsymptoticForArcTanh[ u,x->s,Analys[u,x->s] ]

 AsymptoticForArcTanh[ u_,x_->s_,Indeterminate ] := TInfinity[ ArcTanh[u] ]

 AsymptoticForArcTanh[ u_,x_->s_, 1] :=
   1/2 AsymptoticLog[1+u, x->s] - 1/2 AsymptoticLog[1-u, x->s]

 AsymptoticForArcTanh[ u_,x_->s_,w_ ] :=    
   Module[ {var},
     AsympSimplify[
	SimpFunction[Taylor[ ArcTanh[var],var->w ]/.var->u,x->s],x,card,True]
   ]

(*===================== Tanh ===============================================*)

 AsymptoticTanh[ expr_,x_->s_ ] :=
   AsymptoticForTanh[expr,x->s,Analys[expr,x->s]]

 AsymptoticForTanh[ expr_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   1 - 2 Sum[ (-1)^p AsymptoticPower[ E,-2 expr (p+1),x->s ],
	    {p, 0, card-1} ]

 AsymptoticForTanh[ expr_,x_->s_,Indeterminate ] :=
   TInfinity[ Tanh[expr] ] 

 AsymptoticForTanh[ expr_,x_->s_,w_ ] :=
   Module[ {var},
     SimpFunction[ Taylor[ Tanh[var],var->w ]/.var->expr,x->s]
   ]

(*===================== Coth ===============================================*)

 AsymptoticCoth[ expr_,x_->s_ ] :=
   AsymptoticForCoth[expr,x->s,Analys[expr,x->s]]

 AsymptoticForCoth[ expr_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   1 + 2 Sum[ AsymptoticPower[ E,-2 expr (p+1),x->s ],
	    {p, 0, card-1} ]

 AsymptoticForCoth[ expr_,x_->s_,Indeterminate ] :=
   TInfinity[ Coth[expr] ] 

 AsymptoticForCoth[ expr_,x_->s_,w_ ] :=
   Module[ {var},
     SimpFunction[ Taylor[ Coth[var],var->w ]/.var->expr,x->s]
   ]

(*===================== ArcCoth =============================================*)

 AsymptoticArcCoth[ u_,x_->s_ ] :=  
   AsymptoticArcTanh[ AsymptoticPower[u,-1,x->s],x->s ]

(*===================== ArcSech =============================================*)

 AsymptoticArcSech[ u_,x_->s_ ] :=  
   AsymptoticArcCosh[ AsymptoticPower[u,-1,x->s],x->s ]

(*===================== ArcCsch =============================================*)

 AsymptoticArcCsch[ u_,x_->s_ ] :=  
   AsymptoticArcSinh[ AsymptoticPower[u,-1,x->s],x->s ]

(*===================== SinIntegral =========================================*)

 AsymptoticSinIntegral[ u_,x_->s_ ] :=
   AsymptoticForSinIntegral[u,x->s,Analys[u,x->s]]
 
 AsymptoticForSinIntegral[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   - AsymptoticForSinIntegral[ -u,x->s,Infinity ]
 
 AsymptoticForSinIntegral[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   Pi/2 - 
   AsymptoticForCos[u,x->s,w] *
   Sum[ (-1)^(ind+1) (2 ind-2)! AsymptoticPower[u,-2 ind+1,x->s],
      {ind,1,If[cardFlag,2,card]}] -
   If[ card==1,0,
       AsymptoticForSin[u,x->s,w] *
       Sum[ (-1)^(ind+1) (2 ind-1)! AsymptoticPower[u,-2 ind,x->s],
          {ind,1,If[cardFlag,2,card-1]} ]
     ] 
 
 AsymptoticForSinIntegral[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ SinIntegral[u] ]
 
 AsymptoticForSinIntegral[ u_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ SinIntegral[var],var->w ]/.var->u
   ]
 
(*===================== CosIntegral =========================================*)

 AsymptoticCosIntegral[ u_,x_->s_ ] :=
   AsymptoticForCosIntegral[u,x->s,Analys[u,x->s]]
 
 AsymptoticForCosIntegral[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   -Pi I +  AsymptoticForCosIntegral[ -u,x->s,Infinity ]
 
 AsymptoticForCosIntegral[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   AsymptoticForSin[u,x->s,w] *
   Sum[ (-1)^(ind+1) (2 ind-2)! AsymptoticPower[u,-2 ind+1,x->s],
      {ind,1,If[cardFlag,2,card]}] -
   If[ card==1,0,
       AsymptoticForCos[u,x->s,w] *
       Sum[ (-1)^(ind+1) (2 ind-1)! AsymptoticPower[u,-2 ind,x->s],
          {ind,1,If[cardFlag,2,card-1]} ]
     ]
 
 AsymptoticForCosIntegral[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ CosIntegral[u] ]
 
 AsymptoticForCosIntegral[ u_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ CosIntegral[var],var->w ]/.var->u
   ]
 
(*===================== FresnelS  ===========================================*)

 AsymptoticFresnelS[ u_,x_->s_ ] :=
   AsymptoticForFresnelS[u,x->s,Analys[u,x->s]]
 
 AsymptoticForFresnelS[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   - AsymptoticForFresnelS[-u,x->s,Infinity]
 
 AsymptoticForFresnelS[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   1/2 - 
   AsymptoticForCos[Expand[Pi/2 u^2],x->s,Infinity]/Pi *
   ( AsymptoticPower[u,-1,x->s] +
     Sum[ (-1)^ind Product[4 indpr-1,{indpr,1,ind}] Pi^(-2 ind) *
          AsymptoticPower[u,-4 ind -1,x->s],
        {ind,1,If[cardFlag,2,Round[card/2] ]}]) -
   AsymptoticForSin[Expand[Pi/2 u^2],x->s,Infinity]/Pi *
   If[card==1,0,
      Sum[ (-1)^ind Product[4 indpr+1,{indpr,1,ind}] Pi^(-2 ind) *
          AsymptoticPower[u,-4 ind -3,x->s],
         {ind,0,If[cardFlag,1,Floor[card/2] ]}]
     ] 
 
 AsymptoticForFresnelS[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ FresnelS[u] ]
 
 AsymptoticForFresnelS[ u_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ FresnelS[var],var->w ]/.var->u
   ]
 
(*===================== FresnelC ===========================================*)

 AsymptoticFresnelC[ u_,x_->s_ ] :=
   AsymptoticForFresnelC[u,x->s,Analys[u,x->s]]
  
 AsymptoticForFresnelC[ u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   - AsymptoticForFresnelC[-u,x->s,Infinity]

 AsymptoticForFresnelC[ u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   1/2 + 
   AsymptoticForSin[Expand[Pi/2 u^2],x->s,Infinity]/Pi *
   ( AsymptoticPower[u,-1,x->s] +
     Sum[ (-1)^ind Product[4 indpr-1,{indpr,1,ind}] Pi^(-2 ind) *
          AsymptoticPower[u,-4 ind -1,x->s],
        {ind,1,If[cardFlag,2,Round[card/2] ]}]) -
   AsymptoticForCos[Expand[Pi/2 u^2],x->s,Infinity]/Pi *
   If[card==1,0,
      Sum[ (-1)^ind Product[4 indpr+1,{indpr,1,ind}] Pi^(-2 ind) *
          AsymptoticPower[u,-4 ind -3,x->s],
         {ind,0,If[cardFlag,1,Floor[card/2] ]}]
     ] 
 
 AsymptoticForFresnelC[ u_,x_->s_,Indeterminate ] :=
   TInfinity[ FresnelC[u] ]
 
 AsymptoticForFresnelC[ u_,x_->s_,w_ ] :=
   Module[ {var},
     Taylor[ FresnelC[var],var->w ]/.var->u
   ]
 
(*===================== PolyLog  ===========================================*)

 AsymptoticPolyLog[ k_Integer?Positive,u_,x_->s_ ] :=
   AsymptoticForPolyLog[k,u,x->s,Analys[u,x->s]]

 AsymptoticForPolyLog[ k_,u_,x_->s_,w_. DirectedInfinity[-1] ] :=
   (-1)^(k+1) AsymptoticForPolyLog[k,AsymptoticPower[u,-1,x->s],x->s,0] -
   (2 Pi I)^k/k! BernoulliB[k,AsymptoticLog[u,x->s]/(2 Pi I)]

 AsymptoticForPolyLog[ k_,u_,x_->s_,w_/;!FreeQ[w,DirectedInfinity] ] :=
   (-1)^(k+1) AsymptoticForPolyLog[k,AsymptoticPower[u,-1,x->s],x->s,0] -
   (-2 Pi I)^k/k! BernoulliB[k,I AsymptoticLog[u,x->s]/(2 Pi)]

 AsymptoticForPolyLog[ k_,u_,x_->s_,Indeterminate ] :=
   TInfinity[ PolyLog[k,u] ]

 AsymptoticForPolyLog[ k_,u_,x_->s_,w_ ] :=
   Sum[ u^ind/ind^k,{ind,1,If[cardFlag,2,card]}]

(*========================================================================*)

 Attributes[ Limit ] = {HoldFirst,ReadProtected,Protected} 
 End[]  (* Calculus`Limit`Private` *)

 EndPackage[]   (* Calculus`Limit` *)

