16
$\begingroup$

If I request mathematica evaluate an integral for me, I'll often get a more general ConditionalExpression than I want. Example :

Clear[ii, u, z, l, jj]
ii = Integrate[ 1 / Sqrt[z^2 + u^2], {z, -l, l}]
ConditionalExpression[ -Log[-l + Sqrt[l^2 + u^2]] + Log[l + Sqrt[l^2 + u^2]], 
                                       Re[u/l] != 0 || Im[u/l] >= 1 || Im[u/l] <= -1]

I can reduce this after the fact with something like:

jj = FullSimplify[ii, u > 0 && l > 0 && Element[ u | l, Reals] ]
Log[(u^2 + 2 l (l + Sqrt[l^2 + u^2]))/u^2]  

but I'd imagine it should often simplify the calculations if I could provide the assumptions up front, especially the obvious ones like restricting various variables to the domain of reals.

Is there a way to do this?

$\endgroup$

2 Answers 2

15
$\begingroup$

Integrate can take the option Assumptions.

Integrate[1/Sqrt[z^2 + u^2], {z, -l, l}, 
  Assumptions -> u > 0 && l > 0 && Element[u | l, Reals]]

 ==> 2 Log[(l + Sqrt[l^2 + u^2])/u]

Alternatively use Assuming.

Assuming[u > 0 && l > 0 && Element[u | l, Reals], 
 Integrate[1/Sqrt[z^2 + u^2], {z, -l, l}]]

==> 2 Log[(l + Sqrt[l^2 + u^2])/u]
$\endgroup$
15
$\begingroup$

For Integrate as well as for Simplify, Refine FunctionExpand, Limit etc. there is an option Assumptions:

Integrate[ 1/Sqrt[ z^2 + u^2], {z, -l, l}, Assumptions -> (u | l) ∈ Reals]
ConditionalExpression[ 2 ArcSinh[ l/Abs[ u]], u != 0 && l >= 0]

or one can use

Assuming[ (u | l) ∈ Reals, Integrate[ 1/Sqrt[ z^2 + u^2], {z, -l, l}]]

the latter is more handy for CompoundExpression's, e.g.

Assuming[ (u | l) ∈ Reals,
          int = Integrate[1/Sqrt[z^2 + u^2], {z, -l, l}]; Simplify[int, int[[2]]] ]
2 ArcSinh[ l/Abs[ u]]

Another way of making assumptions is to use $Assumptions globally in a Mathematica session or to close it in Block, e.g.

Block[{ $Assumptions = (u | l) ∈ Reals}, 
                                         Integrate[ 1/Sqrt[ z^2 + u^2], {z, -l, l}]]

Edit

The integral in the question provides a good example for a throughout discussion of assumptions methods like Assumptions, Assuming or $Assumptions.

The OP seems to need the integral in the real domain and if not specified explicitely in general Mathematica evaluates integrals by default in complex numbers.

  • u > 0 and l > 0 implies Element[u | l, Reals], thus we need not to add this assumption :

     Integrate[ 1/Sqrt[ z^2 + u^2], {z, -l, l}, Assumptions -> u > 0 && l > 0]
    
    2 Log[( l + Sqrt[ l^2 + u^2])/u]
    
  • Element[u | l, Reals] is a more general assumption, and when we use it in Assuming or adding such Assumptions in Integrate we obtain a slightly more general expression. To see it we write :

    ComplexExpand[ 2 ArcSinh[ l/Abs[ u]]]
    
    2 I Arg[Sqrt[ 1 + l^2/Abs[ u]^2] + l/Abs[ u]] + Log[(Sqrt[1 + l^2/Abs[u]^2] + l/Abs[u])^2]
    

enter image description here

then we can impose a stronger assumption, to get what we get using Assumptions in Integrate :

    Refine[ %, l ∈ Reals && u > 0]
  2 Log[ Sqrt[ 1 + l^2/u^2] + l/u ]

We could also get this with FunctionExpand using u > 0 and then passing it to TrigToExp:

FunctionExpand[ 2 ArcSinh[ l /Abs[ u]], u > 0] // TrigToExp
$\endgroup$
6
  • $\begingroup$ Good point about the inequalities implicitly assuming Reals +1. $\endgroup$
    – Andy Ross
    Commented Feb 28, 2012 at 4:01
  • $\begingroup$ @AndyRoss Thanks ! Such issues are ubiquitous, making a good CAS is a really hard task. $\endgroup$
    – Artes
    Commented Feb 28, 2012 at 4:13
  • $\begingroup$ Thanks for the mention of $Assumptions. That looks very handy. $\endgroup$ Commented Feb 28, 2012 at 16:59
  • $\begingroup$ @Artes Considering your answer, I don't understand why Assuming[x>0,TrueQ[x>0]] does reply False. Could you please explain? $\endgroup$
    – max
    Commented Mar 26, 2012 at 22:35
  • $\begingroup$ @max When you evaluate only TrueQ[x > 0] outside Assuming you get False since TrueQ may have only logical values and while you haven't assigned any value to x it has to return False. Assuming[cond, eqn] is supposed to work with equations or mathematical functions which return numerical values. Nevertheless when you set e.g. x=5 then Assuming[x > 0, TrueQ[x > 0]] will return True unlike e.g. Assuming[x < 0, TrueQ[x < 0]] just because TrueQ is a logical operator not an equation and x has an assigned value. $\endgroup$
    – Artes
    Commented Mar 26, 2012 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.