From the paper, the condition number is given in terms of the mean of the absolute value around the contour, which you can just as easily compute with QuadGK or any integration method (especially since you probably only need the condition number to low accuracy).
On the other hand, if you adaptively choose the contour, you probably end up with a contour that is not close to a pole(?), in which case the trapezoidal rule should be fine.
I forget the details but if I recall correctly most functions are “exponential like”: actually only a small part of the contour contributes to the final result. So its possible you are right that QuadGK still uses less function evaluations.
Let \gamma:[0,1] \to \mathbb{C} be a smooth parametrization of the curve \Gamma. Let w_i = \gamma(\frac{i}{N}) with i = 0... N-1. From the trapezoidal approximation and cauchy formula it follows that
What its almost magic about this representation is that the errors for z close the the boundary magically cancel out, and this expression ends up being an extremely good approximation.
But the question still remains, what are you after that requieres so many derivatives? Is it a complicated function that the taylor expansion is easier to evaluate? Has the derivative itself has any meaning? Either way, taking 4 derivatives is already super ill-condition problem numerically, so even if your function is analytic I would be shocked if you are able to get 1 decimal correctly if you are taking 1000 derivatives.
I don’t see how it helps with the example I gave, assuming you don’t know the location of the pole. If you know the location z of the pole(s), I agree that there are lots of useful tricks (both for the trapezoidal rule and for other numerical integration schemes).
Why is taking this summation any better than dividing than 2\pi i? The denominator here is effectively 2\pi i. Is it just that \frac 1N cancels?
Perhaps this is a restatement of Steven’s concern, the choice of \Gamma needs to ensure that f is holomorphic (equivalently, analytic) in the interior of \Gamma. Do you have a general scheme to choose \Gamma?
It is only 2\pi i in the limit N \to \infty. For finite N there are errors, and these errors can grow large when the (known) pole z is close to the contour \Gamma; from what @Veenty said, these finite-N errors somehow mostly cancel between the numerator and denominator.
(It would be nice to have a reference; this technique isn’t mentioned in Trefethen’s paper on the trapezoidal rule for contour integrals and similar cases, for example.)
I could imagine that such a pithy reply might be okay-ish if people in the field knew what he was talking about, but I strongly suspect that if you did a survey of theoretical physicists asking them if they knew of a single use for the 1000th order derivative of a function, the number who answered yes would be around the same number as those who believed the world was flat.
I think there are still a couple of errors in this. You wrote dz instead of dw and the dw isn’t included in the sums. But I tried what I think you intended to evaluate \cos(z) at the point where @stevengj placed the pole in his example using the unit circle as the contour. And I have to agree with you that this does seem quite magical. The plot I got was
The quick-and-dirty script I used was
using Plots
function trap(f, z, n)
sum = 0.0im
for j in 0:(n - 1)
w = exp(im * 2 * pi * j / n)
sum += f(w) * w / (w - z)
end
return sum / n
end
function trap_ratio(f, z, n)
sum_num = 0.0im
sum_den = 0.0im
for j in 0:(n - 1)
w = exp(im * 2 * pi * j / n)
sum_num += f(w) * w / (w - z)
sum_den += w / (w - z)
end
return sum_num / sum_den
end
z = 0.9790 + 0.198im
f(z) = cos(z)
ns = [2:2:50; (k -> round(10^(k/4))).(8:20)]
results = (n -> abs(trap(f,z,n)-f(z))).(ns)
results_ratio = (n -> abs(trap_ratio(f,z,n)-f(z))).(ns)
plot(
ns,
results,
yaxis = :log10,
xaxis = :log10,
yticks = (k -> 10.0^(-2 * k)).(-1:16),
marker = :circle,
markersize = 2,
label = "Trapezoidal",
xlabel = "points",
ylabel = "error",
)
plot!(ns, results_ratio, marker=:diamond, markersize=2, label="Trapezoidal Ratio")
I’d like to second the request for a reference if you have it. I’d really like to understand how this works.
Interesting. So it has a straight-forward (if perhaps obscure) explanation based on the fact that it’s really barycentric Lagrange interpolation. I skimmed over that page before responding previously, but didn’t notice what the Matlab code was actually doing. (Berrut and Trefethen’s paper on barycentric interpolation https://people.maths.ox.ac.uk/trefethen/barycentric.pdf is also a good read.)
Unfortunately it looks like if you want f^{(k)}(z) for k\geq 1 and have the factor 1/(w-z)^{k+1} in the integral, you will still have the delay in exponential convergence when z is close to the contour. (Unless there is some further trick…)
In the original paper (that I also linked) have general formulas for higher order derivatives. However, it seems that you loose one digit of precision for very derivative that you take (which is quite standard with derivatives).
I was hoping for the further trick. I guess I should look at that paper. Unfortunately I don’t have institutional online access. I’ll have to put a little more work into getting a copy, but it sounds worthwhile.