# Complex line integral

Can someone recommend an online article or introductory tutorial that will show me how to do real and complex line integrals using Mathematica?

-
Closely related: Symbolic integration in the complex plane – Jens Feb 17 '13 at 3:08

Here's a quick function that does a real line integral:

SyntaxInformation[
lineIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}},
"ArgumentsPattern" -> {_, _, _}};

lineIntegrate[r_?VectorQ, f_, {t_, tMin_, tMax_}] :=
Module[{param, localR}, localR = r /. t -> param;
Integrate[(f[localR, #] Sqrt[#.#]) &@D[localR, param], {param, tMin,
tMax}]]

lineIntegrate[{Cos[t], Sin[t]}, 1 &, {t, 0, 2 Pi}]

(* ==> 2 Pi *)


The second argument is a function to be evaluated at points along the curve. This function in turn can take two arguments: the position $\vec{r}$ and the derivative of the position with respect to the parameter, $d\vec{r}/dt$.

One could modify this definition so the function takes only the curve parameter as the argument, but I settled on this so that I can calculate things like work integrals easily. For example, take a closed path in 3D, and show that the work along it under a conservative force $\vec{F} = (0, 0, z)$ vanishes:

ParametricPlot3D[{Cos[t], Sin[t], Sin[2 t]}, {t, 0,
2 Pi}, PlotStyle -> Tube[.01]]


dwork[r_, tangent_] := {0, 0, r[[3]]}.tangent

lineIntegrate[{Cos[t], Sin[t], Sin[2 t]},
dwork[#1, #2/Sqrt[#2.#2]] &, {t, 0, 2 Pi}]

(* ==> 0 *)


In this example, I defined the differential work dwork as a function that takes the unit tangent as its second argument. To get the unit tangent from the derivative of the curve, I need to pass it the combination #2/Sqrt[#2.#2] where #2 stands for the derivative (which is the second argument provided inside lineIntegrate.

A perhaps more readable definition achieving the same result would be:

dwork2[r_, derivative_] := {0, 0, r[[3]]}.Normalize[derivative];

lineIntegrate[{Cos[t], Sin[t], Sin[2 t]}, dwork2, {t, 0, 2 Pi}]

(* ==> 0 *)


Edit to address questions in comment

The command SyntaxInformation can be omitted without affecting the execution of the function lineIntegrate. Its purpose is to give the user feedback while entering the arguments of the function, to help with the correct order of the arguments. In particular, "LocalVariables" are highlighted in their own aquamarine color. That's how it's done in Plot, Table etc. For more information on SyntaxInformation, see this Q&A.

Edit 2 to add complex contour integral in analogous way

For completeness, here is the analogous definition of a complex contour integral. As in the real line integral, the path is specified in terms of a curve parameter t that is treated as a local variable, but now it's a complex function. The integrand in this application is just a function of the complex position:

SyntaxInformation[
contourIntegrate] = {"LocalVariables" -> {"Plot", {3, 3}},
"ArgumentsPattern" -> {_, _, _}};

contourIntegrate[z_, f_, {t_, tMin_, tMax_}] :=
Module[{param, localZ}, localZ = z /. t -> param;
Integrate[f[localZ] D[localZ, param], {param, tMin, tMax}]]

f[z_] := 1/z

contourIntegrate[Cos[t] + I Sin[t], f, {t, 0, 2 Pi}]

(* ==> 2 I Pi *)


The test shows that for an integral of $1/z$ around a circle containing the origin, the residue theorem holds.

-
There's an error in that example: when I evaluate it I'm getting error Function::sloth : Slot number 2 in work[#1,#2/Sqrt[#2.#2]]& cannot be filled from.... – murray Feb 17 '13 at 18:09
@murray Thanks, I edited the definition of the function. I originally had f only as a single argument function and forgot to copy the new definition when I edited the example. – Jens Feb 17 '13 at 18:18
To keep things parallel with Integrate and NIntegrate, it would be a good idea also to have an NlineIntegrate variant. (The change to get that would be the obvious one.) – murray Feb 17 '13 at 18:33
@murray Certainly. Looking at it now, I should also switch the order of the first two arguments for more consistency. But I think I'll wait with further edits until the OP comes back with feedback... it was supposed to be for illustrative purposes. – Jens Feb 17 '13 at 18:53
I'm afraid that the code above is far too advanced for me to understand, so I'm going to have to ask some questions and take some time to learn more Mathematica before I can understand this answer. Let me start with these questions: (1) What is the purpose of SyntaxInformation? (2) What does "LocalVariables" -> {"Plot", {3, 3}} accomplish? (3) I was able to try the ArgumentsPattern and watch what happens if you enter too few or too many arguments to the function, but may I ask what would "Arguments"->{_, __, ___} mean? – David Feb 18 '13 at 1:02