예제 #1
0
        /// <summary>
        /// 把第r1行的k倍加到r2上
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <param name="row1">第row1行</param>
        /// <param name="kTimes">k倍</param>
        /// <param name="row2">第row2行</param>
        /// <returns>增加后的矩阵</returns>
        public static FMatrix <T> AddKTimesOfRow1ToRow2(FMatrix <T> mat, int row1, T kTimes, int row2)
        {
            FMatrix <T> result = null;

            result = new FMatrix <T>(mat);
            result.AddKTimesOfRow1ToRow2(row1, kTimes, row2);
            return(result);
        }
예제 #2
0
        /// <summary>
        /// 行最简形
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <returns>行最简形式的矩阵</returns>
        public static FMatrix <double> RowSimplestFormOf(FMatrix <T> mat)
        {
            FMatrix <double> result = null;
            bool             flag1;
            int searchi;

            if (mat.matrix != null && mat.column > 0 && mat.row > 0)                  //合理矩阵,方阵,
            {
                result = new FMatrix <double>(mat.row, mat.column, 0.0);

                for (int i = 0; i < mat.row; i++)
                {
                    for (int j = 0; j < mat.column; j++)
                    {
                        result[i][j] = Convert.ToDouble(mat[i][j]);
                    }
                }

                for (int a = 0; a < result.row; a++)                                 //当前行
                {
                    flag1 = true;
                    for (searchi = a; searchi < result.row; searchi++)
                    {
                        if (!IsZero_DBL(result[searchi][a]))
                        {
                            flag1 = false;                       //找到某一行当前位置不是0
                            break;
                        }
                        else
                        {
                            result[searchi][a] = 0.0;
                        }
                    }
                    if (flag1)
                    {
                        continue;                        //某一列全为零,换下一列
                    }
                    if (searchi != a)
                    {
                        result.ExchangeR1R2(searchi, a);            //交换两行,保证不为零
                    }

                    for (int i = 0; i < result.row; i++)
                    {
                        if (i == a)
                        {
                            continue;                                                           //绕过当前行
                        }
                        result.AddKTimesOfRow1ToRow2(a, -1.0 * result[i][a] / result[a][a], i); //清零当前列
                    }

                    result.RowXMultiplyK(a, 1.0 / result[a][a]);                                  //当前行
                }
            }
            return(result);
        }
예제 #3
0
        /// <summary>
        /// 矩阵的逆矩阵(返回矩阵内部类型一定是double)
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <returns>double的逆矩阵</returns>
        public static FMatrix <double> Inverse(FMatrix <T> mat)
        {
            FMatrix <double> result = null;
            bool             flag1;              //“第一位”为空(每一行都必须非0,这样才能保证)
            int searchi;

            if (mat.matrix != null && mat.row == mat.column && mat.row > 0)                  //合理矩阵,方阵,
            {
                result = new FMatrix <double>(mat.row, mat.row, 0.0);
                FMatrix <double> AE = new FMatrix <double>(mat.row, mat.row + mat.row, 0);        //mat放置一个单位矩阵
                for (int i = 0; i < mat.row; i++)
                {
                    for (int j = 0; j < mat.column; j++)
                    {
                        AE[i][j] = Convert.ToDouble(mat[i][j]);
                        if (i == j)
                        {
                            AE[i][j + mat.row] = 1;                      //对角线
                        }
                        else
                        {
                            AE[i][j + mat.row] = 0;
                        }
                    }
                }

                for (int a = 0; a < AE.row; a++)                                 //当前行
                {
                    flag1 = true;

                    for (searchi = a; searchi < AE.row; searchi++)
                    {
                        if (!IsZero_DBL(AE[searchi][a]))
                        {
                            flag1 = false;                       //找到某一行当前位置不是0
                            break;
                        }
                    }
                    if (flag1)
                    {
                        return(null);                       //某一列全为零,不存在逆矩阵,返回空
                    }
                    if (searchi != a)
                    {
                        AE.ExchangeR1R2(searchi, a);            //交换两行,保证不为零
                    }

                    for (int i = 0; i < AE.row; i++)
                    {
                        if (i == a)
                        {
                            continue;                                               //绕过当前行
                        }
                        AE.AddKTimesOfRow1ToRow2(a, -1.0 * AE[i][a] / AE[a][a], i); //清零当前列
                    }

                    AE.RowXMultiplyK(a, 1.0 / AE[a][a]);                                  //当前行
                }

                for (int i = 0; i < AE.row; i++)
                {
                    for (int j = 0; j < AE.row; j++)
                    {
                        result[i][j] = AE[i][j + AE.row];
                    }
                }

                return(result);
            }
            return(null);
        }