Example #1
0
        public static double[] LinSolve(double[,] A, double[] b, out bool isValid)
        {
            var rref = A.AppendColumn(b).ReducedRowEchelonForm();

            var rows = rref.GetLength(0);
            var cols = rref.GetLength(1);

            isValid = true;
            const double Tolerance = 1e-12;

            for (int row = 0; row < rows; row++)
            {
                for (int col = 0; col < cols - 1; col++)
                {
                    var value = rref[row, col];
                    if (row == col)
                    {
                        if ((value - 1).Abs() > Tolerance)
                        {
                            isValid = false;
                            break;
                        }
                    }
                    else
                    {
                        if (value.Abs() > Tolerance)
                        {
                            isValid = false;
                            break;
                        }
                    }
                }
            }
            return(rref.GetColumn(rref.GetLength(1) - 1));
        }