using System;
namespace Zhou.CSharp.Algorithm
{
public delegate double delFunction_x(double x);
public delegate double delFunction_xa(double[] x);
public delegate double delFunction_x_y(double x, double y);
public delegate double delFunction_x_ya(double x, double[] y);
public delegate double delFunction_xa_ya(double[] x, double[] y);
/// <summary>
/// Class NLEquations for solving nonlinear equations
/// Zhou Changfa
/// Adapted to deep confusion
/// </summary>
public static partial class NLEquations
{
/// <summary>
/// The Monte Carlo method for finding a complex root of a real function or a complex function equation
/// When calling, the imaginary value of the modulus ||f(x, y)|| of the function at the left end of the calculation equation must be overwritten Function
/// double Func(double x, double y)
/// </summary>
/// <param name="Func">Function to calculate the left end of the equation</param>
/// <param name="x"> Pass in the real part of the initial value (guessed solution) and return the real part of the obtained root</param>
/// <param name="y">Pass in the imaginary part of the initial value (guessed solution) and return the obtained root The imaginary part of the root</param>
/// <param name="xStart">The initial value of the endpoint of the uniform distribution</param>
/// <param name="nControlB">Control parameters</param>
/// <param name="eps">control precision</param>
public static void GetRootMonteCarlo(delFunction_x_y Func, ref double x, ref double y,double xStart, int nControlB, double eps)
{ int k;
double xx, yy, a, r, z, x1, y1, z1;
// Solving condition and initial value
a = xStart;
k = 1;
r = 1.0;
xx = x;
yy = y;
z = Func(xx, yy);
// Precision control solution
while (a >= eps)
{ x1 = -a + 2.0 * a * rnd(ref r); x1 = xx + x1; y1 = -a + 2.0 * a * rnd(ref r); y1 = yy + y1; z1 = Func(x1, y1); k = k + 1; if (z1 >= z) { if (k > nControlB) { k = 1; a = a / 2.0; } } else { k = 1;
xx = x1;
yy = y1;
z = z1;
if (z < eps)
{
x = xx;
y = yy;
return;
}
}
}
x = xx;
y = yy;
}
}
}