Exactly that is the solution JDC gives. I guess this is the formula should be used always in computational calcs to trust the output.

Exactly that is the solution JDC gives. I guess this is the formula should be used always in computational calcs to trust the output.

- Cuchulainn
**Posts:**59998**Joined:****Location:**Amsterdam-
**Contact:**

Well, there may be even other solutionsExactly that is the solution JDC gives. I guess this is the formula should be used always in computational calcs to trust the output.

1. iterative (Newton Raphson, bisection)

2. Multi-precision arithmetic in Boost C++

3. Yet another clever closed solution

Sometimes we get lucky, but 99.99% of problems don' have a _computable_ closed solution.

- Cuchulainn
**Posts:**59998**Joined:****Location:**Amsterdam-
**Contact:**

Maybe you can modify the algorithm to find roots by avoiding (catastrophic) cancellation, etc. After all, the formula was invented in an era when round-off error was in the future

https://en.wikipedia.org/wiki/Completing_the_square

e.g. root x1 of OK then find nasty x2 by

ax^2 + bx + c = (x - x1)(x - x2)

https://en.wikipedia.org/wiki/Completing_the_square

e.g. root x1 of OK then find nasty x2 by

ax^2 + bx + c = (x - x1)(x - x2)

- Cuchulainn
**Posts:**59998**Joined:****Location:**Amsterdam-
**Contact:**

True.It is much more "complex" to use it with a computer code. Suppose you have a C++ program which output some "cost" function and you want compute the derivative of this cost function to do some optimisation problem with respect to the cost function.Computing derivatives without the pain of 1) differentiation, 2) catastrophic round-off errors. Sample code.

The scalar (almost a 1-liner). I leave the vector case as an exercise.

Code: Select all`// TestComplexStep.cpp // // Complex-step method to compute approximate derivatives. // Example is scalar-valued function of a scalar argument. // // https://pdfs.semanticscholar.org/3de7/e8ae217a4214507b9abdac66503f057aaae9.pdf // // http://mdolab.engin.umich.edu/sites/default/files/Martins2003CSD.pdf // // (C) Datasim Education BV 2018 // #include <functional> #include <complex> #include <iostream> #include <iomanip> #include <cmath> // Notation and function spaces using value_type = double; template <typename T> using FunctionType = std::function < T(const T& c)>; using CFunctionType = FunctionType<std::complex<value_type>>; // Test case from Squire&Trapp 1998 template <typename T> T func(const T& t) { T n1 = std::exp(t); T d1 = std::sin(t); T d2 = std::cos(t); return n1 / (d1*d1*d1 + d2*d2*d2); } template <typename T> T func2(const T& t) { // Derivative of e^t, sanity check return std::exp(std::pow(t,1)); // return std::exp(std::pow(t, 5)); } value_type Derivative(const CFunctionType& f, value_type x, value_type h) { // df/dx at x using tbe Complex step method std::complex<value_type> z(x, h); // x + ih, i = sqrt(-1) return std::imag(f(z)) / h; } int main() { // Squire Trapp double x = 1.5; double h = 0.1; do { std::cout << std::setprecision(12) << Derivative(func<std::complex<value_type>>, x, h) << '\n'; h *= 0.1; } while (h > 1.0e-300); // Exponential function (101 sanity check) x = 5.0; h = 1.0e-10; std::cout << "Exponential 1: " << std::setprecision(12) << Derivative(func2<std::complex<value_type>>, x, h) << '\n'; return 0; }`

You have no choice but complexify your code, e.g define a new class (complex_type) and all the functions that are required for the complex-step method. Replace all "double" with "complex_type" and some cosmetics dealing with IO.

One could write templated code and then instantiate it for compex rtype. This is a potential Pandora's box. for example,I can compute exact BS greeks but this necessitates using Faddeeva's function for N(z) with z complex.

For general functions of several complex variables I compute the gradient and Jacobian without too much effort. The only issue is the fact that I permute the value of the complex arguments in a loop. Maybe there is a more clever way?..

At some stage I would like to test it with (S)GD,

- Cuchulainn
**Posts:**59998**Joined:****Location:**Amsterdam-
**Contact:**

I think I can live with that.

It is much more "complex" to use it with a computer code. Suppose you have a C++ program which output some "cost" function and you want compute the derivative of this cost function to do some optimisation problem with respect to the cost function.

You have no choice but complexify your code, e.g define a new class (complex_type) and all the functions that are required for the complex-step method. Replace all "double" with "complex_type" and some cosmetics dealing with IO.

Question: It's just during this complexification process the usual inner product in real Euclidean space must become the Hermitian inner product, yes?

- FaridMoussaoui
**Posts:**439**Joined:****Location:**Genève, Genf, Ginevra, Geneva

per definition of the inner product for complex numbers.

GZIP: On