Testing improper integrals with Mathematica

Intro

Studying space curves, I often need
to estimate convergence/divergence of infinite integrals, without necessarily obtaining an exact value.
On the one hand, output of Mathematica meets mathematical standards;
e.g. both these examples considered divergence:
In[]:=
Integrate[1/Log[x+2],{x,0,∞}]
Integrate
:Integral of
1
Log[2+x]
does not converge on {0,∞}.
Out[]=
∞
∫
0
1
Log[2+x]
x
In[]:=
Integrate[Cos@x,{x,0,∞}]
Integrate
:Integral of Cos[x] does not converge on {0,∞}.
Out[]=
∞
∫
0
Cos[x]x
But often this is not enough! The behavior of curves depends on finite and infinite limits of integrals. ​
In these examples we can get more satisfying results:
In[]:=
Limit[Integrate[1/Log[x+2],x],x->∞]
Out[]=
∞
In[]:=
Limit[Integrate[Cos@x,x],x->∞]
Out[]=
Indeterminate
I decided to write a template for a module that analyzes in different ways integrals with infinite limits,
and tries to find the most useful result.

Important notes

 Integrand assumed to be smooth and continuous, at least for x >
x
min
​
​ Functions run in time constrained mode with user defined
t
max

Possible built-in's and it's outputs

General possible otputs are Timeout and finite numerical value.
More specific cases:
◼
  • Integrate[f@x,{x,xMin,∞}]:​
     Message Integrate::idiv
     Expression itself (Head === Integrate)
  • ◼
  • Limit[Integrate[f@x,x],x∞] and ​
    Limit[AsymptoticIntegrate[f@x,x,x∞], x∞]
    ​ For these functions, if result is a finite number it is not value of integral,
    but rather test that it's convergence.​
     Infinity
    ​ Signed Infinity
    ​ Indeterminate
    ​ Interval
    ​ Expression (Head === Limit)
  • ◼
  • NIntegrate[f@x,{x,xMin,∞}]​
    Always finite num with possible messages:
    ​ ::slwcon
    ​ ::ncvb
    etc.
    ​
  • Module

    Input:​
    ​ f - integrand as pure function
    ​ tMax - maximal time to wait response
    ​ xMin - lower bound of integral
    ​ rMax - MaxRecursion value for NIntegrate​
    ​
    In[]:=
    ClearAll[infinityIntegration];​​​​infinityIntegration[f_,tMax_,xMin_,rMax_]:=​​Module[{res,​​​​extractMsgTag=First@Select[StringSplit@#,StringContainsQ["::"]]&,​​​​(*Messages*)​​msgTmOut=StringTemplate["`1` method time out"],​​msgUnDef=StringTemplate["Result of `1` method is undefined"],​​msgResDef=StringTemplate["Result of `1` method is `2`"],​​msgResDefErr=StringTemplate["Result of `1` method is `2` with error `3`"],​​msgResDefErrWrn=StringTemplate["Result of method `1` is `2` with error `3` with warnings `4`"]​​},​​​​(*TryIntegrate*)​​res=Quiet@TimeConstrained[Integrate[f@x,{x,xMin,∞}],tMax,"TimeOut"];​​Which[​​Head@res===Integrate,Print@msgUnDef["Integrate"],​​NumericQ@res,Print@msgResDef["Integrate",EngineeringForm@res],​​res==="TimeOut",Print@msgTmOut["Integrate"]​​];​​​​(*TryLimit@Integrate*)​​res=Quiet@TimeConstrained[Limit[Integrate[f@x,x],x->∞],tMax,"TimeOut"];​​​​Which[​​Head@res===Limit,Print[msgUnDef["LimitIntegrate"]],​​​​NumericQ@res,Print@msgResDef["LimitIntegrate","integral convergence"],​​Abs@res===Infinity,Print@msgResDef["LimitIntegrate","integral divergence"],​​res===Indeterminate||Head@res===Interval,Print@msgResDef["LimitIntegrate","integral has no value"],​​​​res==="TimeOut",Print[msgTmOut["LimitIntegrate"]]​​];​​​​(*TryAsymptoticIntegrate*)​​res=Quiet@TimeConstrained[Limit[AsymptoticIntegrate[f@x,x,x->∞],x->∞],tMax,"TimeOut"];​​​​Which[​​Head@res===Limit,Print[msgUnDef["AsymptoticIntegrate"]],​​​​NumericQ@res,Print@msgResDef["AsymptoticIntegrate","integral convergence"],​​Abs@res===Infinity,Print@msgResDef["AsymptoticIntegrate","integral divergence"],​​res===Indeterminate||Head@res===Interval,Print@msgResDef["AsymptoticIntegrate","integral has no value"],​​​​res==="TimeOut",Print[msgTmOut["AsymptoticIntegrate"]]​​];​​​​(*TryNIntegrate*)​​Block[{tags,errs,$Messages={}},​​​​res=EvaluationData@NIntegrate[f@x,{x,xMin,∞},​​AccuracyGoal->6,PrecisionGoal->6,​​MaxRecursion->rMax,​​IntegrationMonitor:>((errs=Through[#1@"Error"])&)];​​​​tags=extractMsgTag/@res["MessagesText"];​​If[tags==={},​​Print[msgResDefErr["NIntegrate",​​EngineeringForm@res["Result"],​​EngineeringForm@Total@errs]],​​Print[msgResDefErrWrn["NIntegrate",​​EngineeringForm@res["Result"],​​EngineeringForm@Total@errs,​​tags]]​​]​​];​​];

    Examples

    In[]:=
    infinityIntegration[
    -2
    #
    &,5,1,20]
    Result of Integrate method is 1
    Result of LimitIntegrate method is integral convergence
    Result of AsymptoticIntegrate method is integral convergence
    Result of NIntegrate method is 1. with error 159.595*10^-18
    In[]:=
    infinityIntegration
    Log@ArcTan@#
    2
    #
    +Log[1+
    2
    #
    ]
    &,10,1,30
    Result of Integrate method is undefined
    Result of LimitIntegrate method is integral convergence
    Result of AsymptoticIntegrate method is integral convergence
    Result of NIntegrate method is 102.576*10^-3 with error 193.209*10^-9
    In[]:=
    infinityIntegration(
    3
    #
    +1)^-1Log[#^2+2]&,5,1,20
    Integrate method time out
    LimitIntegrate method time out
    Result of AsymptoticIntegrate method is integral convergence
    Result of NIntegrate method is 239.818*10^-3 with error 273.278*10^-9
    In[]:=
    ​​infinityIntegration[(Sin[1-Exp[-#]]-Sin@1)&,10,0,30]
    Result of Integrate method is -712.965*10^-3
    Result of LimitIntegrate method is integral convergence
    Result of AsymptoticIntegrate method is integral convergence
    Result of NIntegrate method is -712.965*10^-3 with error 164.857*10^-9
    ◼
  • SE question
  • In[]:=
    infinityIntegration[Cos[#]Log[1+#^2]&,20,1,30]
    Result of Integrate method is undefined
    ◼
  • SE question
  • ◼
  • SE Question
  • ◼
  • SE Question​
    ​