# Symbolic integration in real domain only ( assumptions and ComplexExpand don't work)

Integrate[m^2/((x - m^2)^2 + y^2), m]

mathematica gives me a complex-valued reuslt, but maple 17 gives me what I want.

I tried using assumptions, but it doesn't work.

In MMA, is there a general way to do integrations in real domains, just like maple.

Can this wrap bulit-in command proposed by Todd Gayley (see: https://stackoverflow.com/questions/4198961/what-is-in-your-mathematica-tool-bag ) do the trick?

Message[args___] := Block[{$inMsg = True, result}, "some code here"; result = Message[args]; "some code here"; result] /; ! TrueQ[$inMsg]

Perhaps the reason for the complex-valude result is the invovled power calculation during the integration, so maybe what I really need is a general way to do symbolic power calculation in real domains?

ComplexExpand doesn't work as in the post integration on real domain only!.

thanks :)

• @Artes Integrate[m^2/((1 - m^2)^2 + 1), {m, 0, 1}], Maple 17 gives 1/8 Sqrt[-1 + Sqrt[ 2]] (2 (2 + Sqrt[2]) ArcTan[Sqrt[2 (1 + Sqrt[2])]] + Sqrt[2] Log[5 + 4 Sqrt[2] - 2 Sqrt[2 (7 + 5 Sqrt[2])]]) Commented May 13, 2013 at 11:31
• This issue is more general. Take a look at this answer: mathematica.stackexchange.com/questions/23080/…. One might get rid of unwanted imaginary part working with appropriate assumptions, see what happens for special values x and y, e.g. Integrate[m^2/((1 - m^2)^2 + 1), {m, 0, 1}] Commented May 13, 2013 at 11:34
• @chyanog You can map all ComplexExpand i.e. ComplexExpand //@ Integrate[m^2/((1 - m^2)^2 + 1), {m, 0, 1}], then you'll see how one could proceed to remove apparently immaginary result. Commented May 13, 2013 at 11:40
• @Artes and chyanog. These are not what I want. Please, normally, given a integration problem, you don't do some tests using specific values as what Artes does. If I have time to do these tests, why don't I using maple's results directly? Please no tests, just want a general way to do symbolic indefinite integration. Anyway, thanks. Commented May 14, 2013 at 1:26
• @pengfei_guo Maple results and Mathematica ones can be equivalent under some assumptions, it doesn't matter that they are apparently different. If you analyze carefully the link I gave above you'll probably better understand the problem. However demonstrating that the both results are equivalent may depend on case by case basis. Commented May 14, 2013 at 1:36

It is not quite clear, what do you you want to get out of the answer.

Would you like to compare Maple and Mma and understand, which one is better ?

Or would you like to understand the alternative forms of taking this integral?

Or the reason, why the results of Marple and Mma are different?

Or transform the Mma result in terms of xand y?

Or, finally, transform the Mma result in terms of xand y, such that it does ot contain imaginary unit?

If your question concerns any comparison Maple with Mma, the point is that they differently understand the expression complexity. So Mma returns the result (demonstrated by Bob above) that seems it more concise, and it, inded, looks shorter than that of Maple (you show above).

The second question is what do you intend to do with this result. If you, say, only need to plot it in Mma (or to do something comparable), the Mma result is perfect, do, plot it.

However, you might want to further transform it, and may, say, differently treat different terms. In such a case you may need to have it in terms of xand y, such that it does not contain imaginary unit.

I choose this possibility, since the task seems me personally challenging. So my response below is for this special case. If you do not need it, disregard it.

So, let me first make the integral a bit differently with respect to Bobs approach, by representing it in a more canonical form. Let us introduce z=m^2and integrate over z:

expr1 = Integrate[z/((z + x)^2 + y^2)*1/(2 Sqrt[z]), z,
Assumptions -> {z > 0, x ∈ Reals, y ∈ Reals}] /.
Sqrt[z] -> m

(*  1/2 (((I x + y) ArcTan[m/Sqrt[x - I y]])/(
Sqrt[x - I y] y) + ((-I x + y) ArcTan[m/Sqrt[x + I y]])/(
Sqrt[x + I y] y))    *)

Now let us make a replacement of variables:

expr2 = expr1 /. {1/Sqrt[x + I y] ->
1/r*Exp[-I f/2], -I x + y -> -I r^2 Exp[I f],
1/Sqrt[x - I y] -> 1/r*Exp[I f/2], I x + y -> I r^2 Exp[-I f]}

(*   1/2 (-((I E^((I f)/2) r ArcTan[(E^(-((I f)/2)) m)/r])/y) + (
I E^(-((I f)/2)) r ArcTan[(E^((I f)/2) m)/r])/y)   *)

and ComplexExpand the result

expr3 = ComplexExpand[expr2]

I do not show the result, since it is rather long, you may obtain it, and just looking at it you will find expressions like Arg[1 - (I E^(-((I f)/2)) m)/r]. They can be replaced by ArcTans:

expr4 = expr3 /. {Arg[
1 - (I E^(-((I f)/2)) m)/
r] -> -ArcTan[(( m Cos[f/2])/r)/(1 - (m Sin[f/2])/r)],
Arg[1 + (I E^(-((I f)/2)) m)/r] ->
ArcTan[((m Cos[f/2])/r)/(1 + (m Sin[f/2])/r)],
Arg[1 - (I E^((I f)/2) m)/
r] -> -ArcTan[(( m Cos[f/2])/r)/(1 + (m Sin[f/2])/r)],
Arg[1 + (I E^((I f)/2) m)/r] ->
ArcTan[((m Cos[f/2])/r)/(1 - (m Sin[f/2])/r)]}

(*
(r Cos[f/2] Log[(m^2 Cos[f/2]^2)/r^2 + (1 - (m Sin[f/2])/r)^2])/(
4 y) - (r Cos[f/
2] Log[(m^2 Cos[f/2]^2)/r^2 + (1 + (m Sin[f/2])/r)^2])/(4 y) + (
r ArcTan[(m Cos[f/2])/(r (1 - (m Sin[f/2])/r))] Sin[f/2])/(2 y) + (
r ArcTan[(m Cos[f/2])/(r (1 + (m Sin[f/2])/r))] Sin[f/2])/(2 y)
*)

The resulting expression is Real. It is only left to replace the amplitude and phase in it:

expr5 = expr4 /. {r -> (x^2 + y^2)^(1/4), f -> ArcTan[y/x]}

(*     ((x^2 + y^2)^(1/4)
Cos[1/2 ArcTan[y/x]] Log[(m^2 Cos[1/2 ArcTan[y/x]]^2)/Sqrt[
x^2 + y^2] + (1 - (m Sin[1/2 ArcTan[y/x]])/(x^2 + y^2)^(
1/4))^2])/(
4 y) - ((x^2 + y^2)^(1/4)
Cos[1/2 ArcTan[y/x]] Log[(m^2 Cos[1/2 ArcTan[y/x]]^2)/Sqrt[
x^2 + y^2] + (1 + (m Sin[1/2 ArcTan[y/x]])/(x^2 + y^2)^(
1/4))^2])/(
4 y) + ((x^2 + y^2)^(1/4)
ArcTan[(m Cos[1/2 ArcTan[y/x]])/((x^2 + y^2)^(
1/4) (1 - (m Sin[1/2 ArcTan[y/x]])/(x^2 + y^2)^(1/4)))] Sin[
1/2 ArcTan[y/x]])/(
2 y) + ((x^2 + y^2)^(1/4)
ArcTan[(m Cos[1/2 ArcTan[y/x]])/((x^2 + y^2)^(
1/4) (1 + (m Sin[1/2 ArcTan[y/x]])/(x^2 + y^2)^(1/4)))] Sin[
1/2 ArcTan[y/x]])/(2 y)
*)

That's it. It may by done still a bit simpler:

FullSimplify[expr5, {m ∈ Reals, x ∈ Reals,
y ∈ Reals}]

(*   (1/(4 y))(x^2 + y^2)^(
1/4) (Cos[
1/2 ArcTan[y/x]] (Log[
m^2 + Sqrt[x^2 + y^2] -
2 m (x^2 + y^2)^(1/4) Sin[1/2 ArcTan[y/x]]] -
Log[m^2 + Sqrt[x^2 + y^2] +
2 m (x^2 + y^2)^(1/4) Sin[1/2 ArcTan[y/x]]]) +
2 (ArcTan[(
m Cos[1/2 ArcTan[y/x]])/((x^2 + y^2)^(1/4) -
m Sin[1/2 ArcTan[y/x]])] +
ArcTan[(m Cos[1/2 ArcTan[y/x]])/((x^2 + y^2)^(1/4) +
m Sin[1/2 ArcTan[y/x]])]) Sin[1/2 ArcTan[y/x]])
*)

Now that is the end. If it looks a bit different to what you see in Maple, it should be for the same reason.

Note that tan^-1in the picture above means ArcTan. If it is what you are looking for, be carefull with the replacements like Arg[1 - (I E^(-((I f)/2)) m)/ r] -> -ArcTan[(( m Cos[f/2])/r)/(1 - (m Sin[f/2])/r)]. In some cases they may introduce an error, check them. I did not. Have fun!

Later Edit: In responce to the notes of Michael E2:

Yes, Michael, you are right, I mixed up the sign in the integral. Then the integral is:

expr1 = Integrate[z/((z - x)^2 + y^2)*1/(2 Sqrt[z]), z,
Assumptions -> {z > 0, x \[Element] Reals, y \[Element] Reals}] /.
Sqrt[z] -> m

The further operations with the sign "-" are the same as with "+" up to (including) expr3. Then one can do the following. These are the replacement rules:

rules = {Arg[
1 - (E^((I f)/2) m)/
r] -> -ArcTan[(( m Sin[f/2])/r)/(1 - (m Cos[f/2])/r)],
Arg[1 + (E^((I f)/2) m)/r] ->
ArcTan[((m Sin[f/2])/r)/(1 + (m Cos[f/2])/r)],
Arg[1 - (E^(-((I f)/2)) m)/r] ->
ArcTan[(( m Sin[f/2])/r)/(1 - (m Cos[f/2])/r)],
Arg[1 + (E^(-((I f)/2)) m)/
r] -> -ArcTan[(( m Sin[f/2])/r)/(1 + (m Cos[f/2])/r)],
Cos[1/2 Arg[x + I y]] -> Cos[1/2*ArcTan[y/x]],
Cos[1/2 Arg[x - I y]] -> Cos[1/2*ArcTan[y/x]],
Sin[1/2 Arg[x - I y]] -> -Sin[1/2 ArcTan[y/x]],
Sin[1/2 Arg[x + I y]] -> Sin[1/2 ArcTan[y/x]]};

In these rules I only accounted for the first quadrant x>0, y>0. In general case one can everywhere in these and later rules write ArcTan[b,a]instead of the ArcTan[a/b]. But then results are not simplified to such a short expression, as we see in the end. Now the substitutions work as follows:

expr4 = expr3 /. rules;
expr5 = expr4 /. {r -> (x^2 + y^2)^(1/4), f -> ArcTan[y/x]};
Simplify[expr5, {x > 0, y > 0, m > 0}]

The result is as follows:

(*
(1/(4 y))(x^2 + y^2)^(
1/4) (2 ArcTan[(
m Sin[1/2 ArcTan[y/x]])/((x^2 + y^2)^(1/4) -
m Cos[1/2 ArcTan[y/x]])] Cos[1/2 ArcTan[y/x]] +
2 ArcTan[(
m Sin[1/2 ArcTan[y/x]])/((x^2 + y^2)^(1/4) +
m Cos[1/2 ArcTan[y/x]])] Cos[1/2 ArcTan[y/x]] +
Log[(m^2 + Sqrt[x^2 + y^2] -
2 m (x^2 + y^2)^(1/4) Cos[1/2 ArcTan[y/x]])/(
m^2 + Sqrt[x^2 + y^2] +
2 m (x^2 + y^2)^(1/4) Cos[1/2 ArcTan[y/x]])] Sin[
1/2 ArcTan[y/x]])
*)

Generally, I think that after the direction is shown, the OP should do details and check for possible errors hiself.

• I think the replacement yields an error when the real of the argument to Arg is negative. (Certainly the ranges imply an error.) I did something similar, albeit not as elegantly as you, but decided it was an unsatisfactory answer to the general question of how to keep an integral in the reals. I approached Arg via ArcCos and Sign[Re[..]]. Then I decided the Mma answer is superior and simpler anyway. With respect to the OP's question, I learned real-only integration as a youngster, but perhaps I'm outgrowing it. :) Commented Aug 2, 2014 at 21:37
• @Michael E2 Yes, that's what I meant with the last sentence in my answer. I simply think that the author may not be interested too much in transformations like that. In fact to avoid the error one should use ArcTan[x,y]instead of ArcTan[y/x]. But then the result looks less "traditional". So, my answer as it is, is only valid for the first qudrant x>0 y>0. Commented Aug 3, 2014 at 9:22
• I didn't have Mma yesterday -- today I noticed that your integrand differs slightly from the OP's. Undoing the substitution, it is equivalent to m^2/((x + m^2)^2 + y^2) whereas the OP has a minus between the x and m^2. Essentially the difference is the same as changing x to -x, which is not an important difference at all. (Also, the ArcTan[y/x] will be valid for x > 0, y <= 0, too.) +1. Commented Aug 3, 2014 at 12:51
• @Michael E2 Have a look to the later edit. Commented Aug 3, 2014 at 13:40

For Element[{m, x, y}, Reals] the function is real; however, numerical noise can result in an imaginary artifact that can be removed with Chop

int[m_, x_, y_] = Assuming[{Element[{m, x, y}, Reals]},
Integrate[m^2/((x - m^2)^2 + y^2), m] //
FullSimplify]

(I*(Sqrt[-x - I*y]*ArcTan[m/Sqrt[-x - I*y]] - Sqrt[-x + I*y]*ArcTan[m/Sqrt[-x + I*y]]))/(2*y)

(int @@@ RandomReal[{-2, 2}, {40, 3}]) // Chop[#, 10^-15] &

{-0.0254214, -0.0710269, 0.017061, -0.159841, 0.00667301, 0.134012, 0.0549759, -0.00119414, -4.68272, -0.797005, -0.0151467, 0.0737341, -0.120778, 0.00454359, -0.870848, -0.0339835, -3.453, 0.465008, -9.04711, -9.02031, 0.000540718, 0.0492567, 0.282303, 0.0357629, -0.155331, -0.482013, 0.0955548, 0.0110472, -0.0248232, -0.126987, 0.268351, 0.0481008, -0.132282, 0.0273012, 0.000381937, -0.21833, -0.00260406, 2.10403, 0.234028, 0.00998243}

$\int \frac{x^2}{\left(x^2-a\right)^2+b^2} \, dx=\int \frac{x^2-\sqrt{a^2+b^2}}{2 \left(a^2-2 a x^2+b^2+x^4\right)} \, dx+\int \frac{\sqrt{a^2+b^2}+x^2}{2 \left(a^2-2 a x^2+b^2+x^4\right)} \, dx =\int \frac{\frac{\partial f(x)}{\partial x}}{2 \left(f(x)^2+2 \left(\sqrt{a^2+b^2}-a\right)\right)} \, dx+\int \frac{\frac{\partial g(x)}{\partial x}}{2 \left(g(x)^2-2 \left(\sqrt{a^2+b^2}+a\right)\right)} \, dx$

where $f(x)=x-\frac{\sqrt{a^2+b^2}}{x},g(x)=x+\frac{\sqrt{a^2+b^2}}{x}$

(Integrate[D[f[x],x]/(2(f[x]^2+2(Sqrt[a^2+b^2]-a))),x]/.f->Function[x,x-Sqrt[a^2+b^2]/x])+
(Integrate[D[g[x],x]/(2(g[x]^2-2(Sqrt[a^2+b^2]+a))),x]/.g->Function[x,x+Sqrt[a^2+b^2]/x])
D[%,x]//FullSimplify

ArcTan[(-(Sqrt[a^2+b^2]/x)+x)/(Sqrt[2] Sqrt[-a+Sqrt[a^2+b^2]])]/(2 Sqrt[2] Sqrt[-a+Sqrt[a^2+b^2]])-ArcTanh[(Sqrt[a^2+b^2]/x+x)/(Sqrt[2] Sqrt[a+Sqrt[a^2+b^2]])]/(2 Sqrt[2] Sqrt[a+Sqrt[a^2+b^2]])

x^2/(b^2 + (a - x^2)^2)

$\frac{\partial \left(\frac{\tan ^{-1}\left(\frac{x-\frac{\sqrt{a^2+b^2}}{x}}{\sqrt{2} \sqrt{\sqrt{a^2+b^2}-a}}\right)}{2 \sqrt{2} \sqrt{\sqrt{a^2+b^2}-a}}-\frac{\tanh ^{-1}\left(\frac{\frac{\sqrt{a^2+b^2}}{x}+x}{\sqrt{2} \sqrt{\sqrt{a^2+b^2}+a}}\right)}{2 \sqrt{2} \sqrt{\sqrt{a^2+b^2}+a}}\right)}{\partial x}=\frac{x^2}{\left(x^2-a\right)^2+b^2}$