16

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?

Artes
  • 57,212
  • 12
  • 157
  • 245
Peeter Joot
  • 6,398
  • 4
  • 36
  • 55

2 Answers2

15

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]
Andy Ross
  • 19,320
  • 2
  • 61
  • 93
15

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
Artes
  • 57,212
  • 12
  • 157
  • 245
  • Good point about the inequalities implicitly assuming Reals +1. – Andy Ross Feb 28 '12 at 04:01
  • @AndyRoss Thanks ! Such issues are ubiquitous, making a good CAS is a really hard task. – Artes Feb 28 '12 at 04:13
  • Thanks for the mention of $Assumptions. That looks very handy. – Peeter Joot Feb 28 '12 at 16:59
  • @Artes Considering your answer, I don't understand why Assuming[x>0,TrueQ[x>0]] does reply False. Could you please explain? – max Mar 26 '12 at 22:35
  • @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. – Artes Mar 26 '12 at 23:48
  • @Artes Thank you very much for your answer. As I thought it was a question on its own, I asked it here. Thanks a lot. – max Mar 27 '12 at 09:51