コード例 #1
0
        /// <summary>
        /// 交换矩阵的r1与r2行
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <param name="r1">r1行</param>
        /// <param name="r2">r2行</param>
        /// <returns>交换后的矩阵</returns>
        public static FMatrix <T> ExchangeR1R2(FMatrix <T> mat, int r1, int r2)
        {
            FMatrix <T> result = null;

            if (mat.matrix != null && r1 >= 0 && r1 < mat.row && r2 >= 0 && r2 < mat.row)                //可用的矩阵
            {
                result = new FMatrix <T>(mat);
                result.ExchangeR1R2(r1, r2);
            }
            return(result);
        }
コード例 #2
0
        /// <summary>
        /// 代数余子式(不推荐使用,因为矩阵的代数余子式与行列式的代数余子式不同)
        /// </summary>
        /// <param name="_matrix">矩阵</param>
        /// <param name="i">行</param>
        /// <param name="j">列</param>
        /// <returns>代数余子式</returns>
        public static FMatrix <T> Aij(FMatrix <T> _matrix, int i, int j)
        {
            FMatrix <T> result = Mij(_matrix, i, j);

            if (result != null)
            {
                if ((i + j) % 2 == 1)
                {
                    result = MultiplyNumber(result, (T)(object)(-1));
                }
            }
            return(result);
        }
コード例 #3
0
 /// <summary>
 /// 拷贝构造函数
 /// </summary>
 /// <param name="fmatrix"></param>
 public FMatrix(FMatrix <T> fmatrix)
 {
     row    = fmatrix.row;
     column = fmatrix.column;
     if (fmatrix.matrix != null)
     {
         matrix = new List <List <T> >();
         for (int i = 0; i < fmatrix.row; i++)
         {
             matrix.Add(new List <T>());
             for (int j = 0; j < fmatrix.column; j++)
             {
                 matrix[i].Add(fmatrix.matrix[i][j]);
             }
         }
     }
 }
コード例 #4
0
        /// <summary>
        /// 矩阵的转置
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <returns>转置矩阵</returns>
        public static FMatrix <T> Transpose(FMatrix <T> mat)
        {
            FMatrix <T> result = null;

            if (mat.matrix != null)
            {
                result = new FMatrix <T>(mat.column, mat.row, default(T));
                for (int i = 0; i < mat.row; i++)
                {
                    for (int j = 0; j < mat.column; j++)
                    {
                        result[j][i] = mat[i][j];
                    }
                }
            }
            return(result);
        }
コード例 #5
0
        /// <summary>
        /// 余子式
        /// </summary>
        /// <param name="_matrix">原矩阵</param>
        /// <param name="i">行</param>
        /// <param name="j">列</param>
        /// <returns>返回余子式</returns>
        public static FMatrix <T> Mij(FMatrix <T> _matrix, int i, int j)
        {
            FMatrix <T> result = null;
            int         nowi = 0, nowj = 0;

            if (_matrix.matrix != null && _matrix.row > 1 && _matrix.column > 1)                    //矩阵可用
            {
                if (i >= 0 && i < _matrix.row && j >= 0 && i < _matrix.column)                      //去掉的行和列
                {
                    result = new FMatrix <T>(_matrix.row - 1, _matrix.column - 1, default(T));
                    for (int a = 0; a < _matrix.row; a++)
                    {
                        if (a < i)
                        {
                            nowi = a;
                        }
                        else if (a > i)
                        {
                            nowi = a - 1;
                        }
                        else
                        {
                            continue;
                        }
                        for (int b = 0; b < _matrix.column; b++)
                        {
                            if (b < j)
                            {
                                nowj = b;
                            }
                            else if (b > j)
                            {
                                nowj = b - 1;
                            }
                            else
                            {
                                continue;
                            }

                            result.matrix[nowi][nowj] = _matrix.matrix[a][b];
                        }
                    }
                }
            }
            return(result);
        }
コード例 #6
0
        /// <summary>
        /// 矩阵乘法 AB,左矩阵的列数等于右矩阵的行数
        /// </summary>
        /// <param name="mata">矩阵A</param>
        /// <param name="matb">矩阵B</param>
        /// <returns>结果矩阵</returns>
        public static FMatrix <T> Multiply(FMatrix <T> mata, FMatrix <T> matb)
        {
            if (mata.matrix != null && matb.matrix != null && mata.column == matb.row)             //行列相同的矩阵
            {
                FMatrix <T> result = new FMatrix <T>(mata.row, matb.column, default(T));

                for (int i = 0; i < mata.row; i++)
                {
                    for (int j = 0; j < matb.column; j++)
                    {
                        result.matrix[i][j] = (T)(object)0;
                        for (int k = 0; k < mata.column; k++)
                        {
                            switch (typeof(T).Name)
                            {
                            case "Int32":
                            {
                                result.matrix[i][j] = (T)(object)(Convert.ToInt32(result.matrix[i][j]) + Convert.ToInt32(mata.matrix[i][k]) * Convert.ToInt32(matb.matrix[k][j]));
                                break;
                            }

                            case "Double":
                            {
                                result.matrix[i][j] = (T)(object)(Convert.ToDouble(result.matrix[i][j]) + Convert.ToDouble(mata.matrix[i][k]) * Convert.ToDouble(matb.matrix[k][j]));
                                break;
                            }

                            case "Single":
                            {
                                result.matrix[i][j] = (T)(object)(Convert.ToSingle(result.matrix[i][j]) + Convert.ToSingle(mata.matrix[i][k]) * Convert.ToSingle(matb.matrix[k][j]));
                                break;
                            }

                            default:
                            {
                                break;
                            }
                            }
                        }
                    }
                }
                return(result);
            }
            return(null);
        }
コード例 #7
0
        /// <summary>
        /// 矩阵乘一个数
        /// </summary>
        /// <param name="mat">原矩阵</param>
        /// <param name="number">数</param>
        /// <returns>结果矩阵</returns>
        public static FMatrix <T> MultiplyNumber(FMatrix <T> mat, T number)
        {
            if (mat.matrix != null)
            {
                FMatrix <T> matrix = new FMatrix <T>(mat.row, mat.column, default(T));
                Type        type   = number.GetType();
                for (int i = 0; i < mat.row; i++)
                {
                    for (int j = 0; j < mat.column; j++)
                    {
                        switch (type.Name)
                        {
                        case "Int32":
                        {
                            matrix.matrix[i][j] = (T)(object)(Convert.ToInt32(number) * Convert.ToInt32(mat.matrix[i][j]));
                            break;
                        }

                        case "Double":
                        {
                            matrix.matrix[i][j] = (T)(object)(Convert.ToDouble(number) * Convert.ToDouble(mat.matrix[i][j]));
                            break;
                        }

                        case "Single":
                        {
                            matrix.matrix[i][j] = (T)(object)(Convert.ToSingle(number) * Convert.ToSingle(mat.matrix[i][j]));
                            break;
                        }

                        default:
                        {
                            break;
                        }
                        }
                    }
                }
                return(matrix);
            }
            return(null);
        }
コード例 #8
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);
        }
コード例 #9
0
        /// <summary>
        /// 矩阵相加,重载运算符 +
        /// </summary>
        /// <param name="mata">矩阵A</param>
        /// <param name="matb">矩阵B</param>
        /// <returns>相加结果</returns>
        public static FMatrix <T> operator +(FMatrix <T> mata, FMatrix <T> matb)
        {
            FMatrix <T> mat = FMatrix <T> .Add(mata, matb);

            return(mat);
        }