gradientDescent

constexpr Complex gradientDescent(Complex (*f)(Complex), const Complex &z0, const double alpha, const int epochs) noexcept

Performs the gradeint descent algortihm on a complex function.

Parameters

Complex (*f)(Complex)

The function to optimize.

const Complex &z0

The starting point.

const double alpha

The learning rate.

const int epochs

The number of iterations.

Returns

type Complex

A complex number.

This function optimizes the given complex function \(f\) using the following update rule:

\[z_{k+1} = z_k - \alpha f'(z_k)\]

Example

auto fn = [](Complex z) { return sin(z); };
Complex z = gradientDescent(fn, 0.1, 0.1, 1000);
std::cout << z << "\n";
std::cout << cos(z) << "\n"; // f'(z) = cos(z)

Output:

-1.5708 + 0j
3.00294e-10 + 0j

References