C#, code sea picking shells (41) - the C# source code of the "generalized inverse method" for solving the "linear least squares problem"

 

using System;

namespace Zhou.CSharp.Algorithm
{     /// <summary>     /// Class LEquations for solving linear equations     /// Original Zhou Changfa     /// Adapted to deep confusion     /// </summary>     public static partial class LEquations     {






        /// <summary>
        /// Generalized inverse method for solving linear least squares problem
        /// </summary>
        /// <param name="mtxLECoef">specified coefficient matrix</param>
        /// <param name="mtxLEConst">specified constant matrix</param>
        /// <param name="mtxResult">Matrix object, returns the equation system solution matrix</param>
        /// <param name="mtxAP">Matrix object , return the generalized inverse matrix of the coefficient matrix</param>
        /// <param name="mtxU">Matrix object, return U matrix</param>
        /// <param name="mtxV">Matrix object, return V matrix </param>
        /// <param name="eps">Control precision</param>
        /// <return>Bool type, whether the solution of the equation system is successful</return>
        public static bool GetRootsetGinv(MatrixmtxLECoef, Matrix mtxLEConst, Matrix mtxResult, Matrix mtxAP, Matrix mtxU, Matrix mtxV, double eps)
        {
            // Number of equations and unknowns
            int m = mtxLECoef.GetNumRows();
            int n = mtxLECoef.GetNumColumns();

            // Initialize the solution vector
            mtxResult.Init(n, 1);

            double[] pDataConst = mtxLEConst.GetData();
            double[] x = mtxResult.GetData();

            // temporary matrix
            Matrix mtxA = new Matrix(mtxLECoef);

            // Find the generalized inverse matrix
            if (!Matrix.InvertUV(mtxA, mtxAP, mtxU, mtxV, eps))
            {                 return false;             }

            double[] pAPData = mtxAP.GetData();

            // 求解
            for (int i = 0; i <= n - 1; i++)
            {
                x[i] = 0.0;
                for (int j = 0; j <= m - 1; j++)
                {
                    x[i] = x[i] + pAPData[i * m + j] * pDataConst[j];
                }
            }
            return true;
        }
}

}

Guess you like

Origin blog.csdn.net/beijinghorn/article/details/131133542