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>
/// QR method for finding all roots of algebraic equation with real coefficients
/// </summary>
/// <param name="n">degree of polynomial equation
/// <param name="dblCoef" >One-dimensional array with a length of n+1, storing n+1 coefficients of polynomial equations of degree n in descending order</param> /// <
param name="xr">One-dimensional array with a length of n, Return the real part of n roots</param>
/// <param name="xi">One-dimensional array with length n, return the imaginary part of n roots</param>
/// <param name="nMaxIt ">Number of iterations</param>
/// <param name="eps">Precision control parameters</param>
/// <return>Bool type, whether the solution is successful</return>
public static bool GetRootQr(int n, double[] dblCoef, double[] xr, double[] xi, int nMaxIt,double eps)
{ // initialize matrix Matrix mtxQ = new Matrix(); mtxQ.Init(n, n);
double[] q = mtxQ.GetData();
//Construct the Hershenberg matrix
for (int j = 0; j <= n - 1; j++)
{ q[j] = -dblCoef[n - j - 1] / dblCoef[n]; } for (int j = n; j <= n * n - 1; j++) { q[j] = 0.0; } for (int i = 0; i <= n - 2; i++) { q[(i + 1) * n + i] = 1.0; } // Find the eigenvalues and eigenvectors of the Hershenberg matrix, which is the solution of the equation if (Matrix.ComputeEvHBerg(mtxQ, out xr, out xi, nMaxIt, eps)) { return true; } return false; }
}
}