# Integrate ignoring assumptions

I want to evaluate an integral with all variables real and positive. Specifically,

Integrate[
Log[\[Sqrt](r^2 + s^2 - 2 r s Cos[\[Theta]])], \[Theta],
Assumptions -> {r >= 0, s >= 0, \[Theta] \[Element] Reals}]


I expect the answer to be real, however Mathematica returns me an answer that is complex: it starts as

1/2 (\[Theta] Log[r^2 + s^2 - 2 r s Cos[\[Theta]]] -
1/(2 (r - s))
I (2 \[Pi] r \[Theta] - 2 \[Pi] s \[Theta] +
E^-ArcTanh[(r - s)/(r + s)] r Sqrt[(r s)/(r + s)^2] \[Theta]^2 - ...


i.e. there is an explicit imaginary part.

(Q0: When writing my questions here, how do I format Mathematica input and output so that it looks more human-readable, like it is in my Mathematica notebook?!)

Q1: Is there a way of telling Mathematica that I expect a real answer in this problem, apart from specifying real inputs using Assumptions?

Q2: When calculating the definite integral between 0 and 2pi I get a sensible answer:

ConditionalExpression[- \[Pi] Log[2/(r^2 + s^2 + Abs[r^2 - s^2])],
r != 0 || s != 0]


However try as I might, I can't retrieve this answer by substituting 2pi and 0 into the above answer for the indefinite integral and subtracting the two. I've tried substituting into the full antiderivative, and taking the real part before substituting, but I just get a long complicated expression in either case. Can anyone help me understand why the definite integral looks so different to (and simpler than) substituting by hand into the indefinite integral and subtracting?

• Did you already see this?
– Alan
Commented Jul 3, 2022 at 22:07
• Thanks, I didn't see it, however I've told Mathematica about all the variables in the problem being real, unlike in the question that you kindly linked to. Commented Jul 3, 2022 at 22:13
• (NB I've asked a related question over on Math StackExchange about how to find the integral analytically, if anyone can help me there!) Commented Jul 3, 2022 at 22:26
• Here is the Math SE link. I think posting in two places simultaneously is a bit disrespectful, see also this meta post. Why not post in one place, and then give people time to answer. Commented Jul 4, 2022 at 3:59
• Indefinite integration mostly ignores assumptions; an antiderivative is an antiderivative. (Granted, some forms might be better than others for various purposes. But this usually is unrelated to how assumptions might be used.) Commented Jul 4, 2022 at 15:45

Question Q2. Here is the indefinite integral:

ii[\[Theta]_]=Integrate[Log[Sqrt[r^2+s^2-2*r*s*Cos[\[Theta]]]],\[Theta],
Assumptions->{r>0,s>0,\[Theta]\[Element]Reals}];


I have not studied the resulting expression in detail. You are right that it has nonzero imaginary part, but that imaginary part does not depend on $$\theta$$, so that does not contradict this being an antiderivative. It also does not mean that assumptions have been ignored. (But note that $$1/(r-s)$$ factors appear, so the result may not make immediate sense without some condition such as $$r \neq s$$, but the case $$r=s$$ can be done separately if necessary.)

In any case, Q2 asks if we can recover the definite integral from this, and this can be done here by explicitly distinguishing the $$r>s$$ and the $$s>r$$ cases:

ii[2*Pi]-ii[0]//FullSimplify[#,Assumptions->{r>s>0,\[Theta]\[Element]Reals}]&
(* gives 2 \[Pi] Log[r] *)

ii[2*Pi]-ii[0]//FullSimplify[#,Assumptions->{s>r>0,\[Theta]\[Element]Reals}]&
(* gives 2 \[Pi] Log[s] *)


in agreement with your definite integral result.

Question Q1. If ii[\[Theta]] is an antiderivative then so is the real part Re[ii[\[Theta]]] as long as \[Theta] is real, so that is one answer for Q1. The expression may still be complicated though. You can try to apply things such as ComplexExpand[#,TargetFunctions->{Re,Im}]& and so on, but it may not come out as simple as you are hoping, still containing PolyLog[2,...] for instance.

Remark. I understand from your question on Math SE that you are surprised that the definite integral is so simple, but the indefinite integral is relatively complicated. In fact here PolyLog[2,...] appears as a subexpression.

That is a standard phenomenon. Two simpler examples are

Integrate[Log[Sin[x]],{x,0,Pi/2}]
Integrate[Log[Sin[x]],x]


and

Integrate[Log[1-x]/x,{x,0,1}]
Integrate[Log[1-x]/x,x]


In both cases, the definite integral is simple, the indefinite integral involves PolyLog[2,...] and so on. If you find this interesing, you could study these examples first.