Пример #1
0
        /// <summary>
        /// 二维矩阵乘法运算,矩阵相乘
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="martix2"></param>
        /// <returns></returns>
        public static Martix operator *(Martix martix1, Martix martix2)
        {
            double[,] array = new double[martix1.Rows, martix2.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix1.Columns == martix2.Rows)
                {
                    for (int row1 = 0; row1 < martix1.Rows; row1++)
                    {
                        int row2 = 0;
                        for (int column2 = 0; column2 < martix2.Columns; column2++)
                        {
                            double Sum = 0;
                            for (int column1 = 0; column1 < martix1.Columns; column1++)
                            {
                                Sum += martix1[row1, column1] * martix2[column1, row2];
                            }
                            result[row1, column2] = Sum;
                            row2++;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("矩阵乘法运算两个矩阵行列不匹配!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵乘法运算错误!");
            }
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 递归计算方阵的行列式,代数余子式法
        /// </summary>
        /// <param name="martix"></param>
        /// <returns></returns>
        public double Determinant(Martix martix)
        {
            double sum = 0;

            try
            {
                int sign = 1;
                if (martix.Rows == 1)//递归出口
                {
                    return(martix[0, 0]);
                }
                for (int i = 0; i < martix.Rows; i++)
                {
                    double[,] _tempmatrix = new double[martix.Rows - 1, martix.Columns - 1];
                    Martix tempmatrix = new Martix(_tempmatrix);
                    for (int j = 0; j < martix.Rows - 1; j++)
                    {
                        for (int k = 0; k < martix.Columns - 1; k++)
                        {
                            tempmatrix[j, k] = martix[j + 1, k >= i ? k + 1 : k];
                        }
                    }
                    sum += sign * martix[0, i] * Determinant(tempmatrix);//递归,调用函数自身
                    sign = sign * (-1);
                }
            }
            catch
            {
                MessageBox.Show("矩阵求行列式错误!");
            }
            return(sum);
        }
Пример #3
0
        /// <summary>
        /// 二维矩阵减法运算
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="martix2"></param>
        /// <returns></returns>
        public static Martix operator -(Martix martix1, Martix martix2)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix1.Rows == martix2.Rows && martix1.Columns == martix2.Columns)
                {
                    for (int i = 0; i < martix1.Rows; i++)
                    {
                        for (int j = 0; j < martix1.Columns; j++)
                        {
                            result[i, j] = martix1[i, j] - martix2[i, j];
                        }
                    }
                }
            }
            catch
            {
                if (martix1.Rows != martix2.Rows)
                {
                    MessageBox.Show("减法运算行数不匹配!");
                }
                if (martix1.Columns != martix2.Columns)
                {
                    MessageBox.Show("减法运算列数不匹配!");
                }
            }
            return(result);
        }
Пример #4
0
            /// <summary>
            /// 输入界面上存储两个矩阵的控件内容
            /// </summary>
            /// <param name="LeftMartix"></param>
            /// <param name="RightMartix"></param>
            //public input(string LeftMartix, string RightMartix)
            //{
            //    double[,] Left;
            //    double[,] Right;
            //    string[] LeftLine;
            //    string[] RightLine;
            //    string[] Leftelement;
            //    string[] Rightelement;
            //    int leftrow, rightrow;
            //    int leftcolumm, rightcolumn;
            //    LeftLine = LeftMartix.Split('\n');
            //    RightLine = RightMartix.Split('\n');
            //    if (LeftMartix != "")
            //    {
            //        leftrow = LeftMartix.Split('\n').Length;
            //        leftcolumm = LeftLine[0].Split(',').Length;
            //    }
            //    else
            //    {
            //        leftrow = 2;
            //        leftcolumm = 2;
            //    }
            //    if (RightMartix != "")
            //    {
            //        rightrow = RightMartix.Split('\n').Length;
            //        rightcolumn = RightLine[0].Split(',').Length;
            //    }
            //    else
            //    {
            //        rightrow = 2;
            //        rightcolumn = 2;
            //    }
            //    Left = new double[leftrow, leftcolumm];

            //    Right = new double[rightrow, rightcolumn];
            //    if (LeftMartix != "")
            //    {
            //        for (int i = 0; i < LeftLine.Length; i++)
            //        {
            //            Leftelement = LeftLine[i].Split(',');
            //            for (int j = 0; j < Leftelement.Length; j++)
            //                Left[i, j] = double.Parse(Leftelement[j]);
            //        }
            //    }
            //    if (RightMartix != "")
            //    {
            //        for (int i = 0; i < RightLine.Length; i++)
            //        {
            //            Rightelement = RightLine[i].Split(',');
            //            for (int j = 0; j < Rightelement.Length; j++)
            //                Right[i, j] = double.Parse(Rightelement[j]);
            //        }
            //    }
            //    Leftmartix = new Martix(Left);
            //    Rightmartix = new Martix(Right);
            //}
            public input(string InputMartix)
            {
                double[,] martix;
                string[] Line;
                string[] element;
                int      row    = 0;
                int      column = 0;

                NULL = false;
                while (InputMartix.Contains("\n\n"))//去除多余的回车
                {
                    InputMartix = InputMartix.Replace("\n\n", "\n");
                }
                Line = InputMartix.Split('\n');
                if (InputMartix != " ")
                {
                    for (int i = 0; i < Line.Length; i++)
                    {
                        if (Line[i] != "")
                        {
                            row     = InputMartix.Split('\n').Length;
                            Line[i] = Line[i].Replace(",", ",");
                            column  = Line[i].Split(',').Length;
                            break;
                        }
                    }
                }
                else
                {
                    row    = 2;
                    column = 2;
                }
                martix = new double[row, column];

                if (InputMartix != " ")
                {
                    for (int i = 0; i < Line.Length; i++)
                    {
                        Line[i] = Line[i].Replace(",", ",");
                        if (Line[i] == "")
                        {
                            MessageBox.Show("输入含空字符!");
                            NULL = true;
                        }
                        else
                        {
                            element = Line[i].Split(',');
                            for (int j = 0; j < element.Length; j++)
                            {
                                martix[i, j] = double.Parse(element[j]);
                            }
                        }
                    }
                }
                Martix = new Martix(martix);
            }
Пример #5
0
        /// <summary>
        /// 二维矩阵除法运算
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="martix2"></param>
        /// <returns></returns>
        public static Martix operator /(Martix martix1, Martix martix2)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            if (martix2.Rows == martix2.Columns)
            {
                result = martix1 * martix2.Inverse();
            }
            else
            {
                MessageBox.Show("矩阵除法运算右边矩阵必须为方阵!");
            }
            return(result);
        }
Пример #6
0
        /// <summary>
        /// 计算方阵伴随矩阵
        /// </summary>
        /// <param name="martix"></param>
        /// <returns></returns>
        private Martix complement(Martix martix)
        {
            double[,] array = new double[martix.Rows, martix.Columns];
            Martix result = new Martix(array);

            try
            {
                if (martix.Rows == martix.Columns)//方阵
                {
                    for (int i = 0; i < martix.Rows; i++)
                    {
                        for (int j = 0; j < martix.Columns; j++)
                        {
                            //生成aij的余子式矩阵
                            double[,] complement = new double[martix.Rows - 1, martix.Columns - 1]; //n-1阶
                            Martix martix1 = new Martix(complement);                                //aij的余子式矩阵
                            int    row     = 0;
                            for (int k = 0; k < martix.Rows; k++)
                            {
                                int column = 0;
                                if (k == i)//去除第i行
                                {
                                    continue;
                                }
                                for (int l = 0; l < martix.Rows; l++)
                                {
                                    if (l == j)//去除第j列
                                    {
                                        continue;
                                    }
                                    martix1[row, column++] = martix[k, l];
                                }
                                row++;
                            }
                            result[i, j] = Math.Pow(-1, i + j) * Determinant(martix1);
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("非方阵无法求伴随矩阵!");
            }
            return(result);
        }
Пример #7
0
        /// <summary>
        /// 求逆操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Inverse_Click(object sender, EventArgs e)
        {
            ResultMartix2.Text = null;
            input  input  = new input(LeftMartix2.Text);
            Martix Result = input.Martix;

            Result = input.Martix.Inverse();
            for (int i = 0; i < Result.Rows; i++)
            {
                for (int j = 0; j < Result.Columns; j++)
                {
                    ResultMartix2.Text += Result[i, j].ToString();
                    if (j != Result.Columns - 1)
                    {
                        ResultMartix2.Text += ",";
                    }
                }
                ResultMartix2.Text += "\n";
            }
        }
Пример #8
0
        /// <summary>
        /// 二维矩阵转置运算
        /// </summary>
        /// <returns></returns>
        public Martix Transpose()
        {
            double[,] array = new double[Element.GetLength(1), Element.GetLength(0)];
            Martix result = new Martix(array);

            try
            {
                for (int i = 0; i < Element.GetLength(0); i++)
                {
                    for (int j = 0; j < Element.GetLength(1); j++)
                    {
                        result[j, i] = Element[i, j];
                    }
                }
            }
            catch
            {
                MessageBox.Show("矩阵转置错误!");
            }
            return(result);
        }
Пример #9
0
        /// <summary>
        /// 二维矩阵乘法运算,矩阵右乘一个数
        /// </summary>
        /// <param name="num"></param>
        /// <param name="martix1"></param>
        /// <returns></returns>
        public static Martix operator *(int num, Martix martix1)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            try
            {
                for (int i = 0; i < martix1.Rows; i++)
                {
                    for (int j = 0; j < martix1.Columns; j++)
                    {
                        result[i, j] = martix1[i, j] * num;
                    }
                }
            }
            catch
            {
                MessageBox.Show("矩阵右乘一个数错误!");
            }
            return(result);
        }
Пример #10
0
        /// <summary>
        /// 二维矩阵除法运算,矩阵除以一个数
        /// </summary>
        /// <param name="martix1"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public static Martix operator /(Martix martix1, int num)
        {
            double[,] array = new double[martix1.Rows, martix1.Columns];
            Martix result = new Martix(array);

            try
            {
                if (num != 0)
                {
                    result = martix1 * (1.0 / num);
                }
                else
                {
                    MessageBox.Show("除数不能为零!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵除法运算错误!");
            }
            return(result);
        }
Пример #11
0
        /// <summary>
        /// 二维矩阵求逆运算
        /// </summary>
        /// <returns></returns>
        public Martix Inverse()
        {
            double[,] array = new double[Element.GetLength(1), Element.GetLength(0)];
            Martix result = new Martix(array);

            try
            {
                if (Element.GetLength(0) == Element.GetLength(1))//方阵
                {
                    Martix martix = new Martix(Element);
                    if (Determinant(martix) != 0)
                    {
                        if (martix.Rows > 1)
                        {
                            result = complement(martix).Transpose() * (1 / Determinant(martix));
                        }
                        else
                        {
                            result.Element [0, 0] = 1 / martix[0, 0];
                        }
                    }
                    else
                    {
                        MessageBox.Show("矩阵行列式为0!");
                    }
                }
                else
                {
                    MessageBox.Show("非方阵矩阵无法求逆!");
                }
            }
            catch
            {
                MessageBox.Show("矩阵求逆错误!");
            }
            return(result);
        }
Пример #12
0
 /// <summary>
 /// 双目运算
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void Calculate_Click(object sender, EventArgs e)
 {
     if (radioButton_Add.Checked)//加法运算
     {
         ResultMartix.Text = null;
         input Leftinput  = new input(LeftMartix.Text);
         input Rightinput = new input(RightMartix.Text);
         if (!Leftinput.NULL)
         {
             Martix Result = Leftinput.Martix;
             Result = Leftinput.Martix + Rightinput.Martix;
             for (int i = 0; i < Result.Rows; i++)
             {
                 for (int j = 0; j < Result.Columns; j++)
                 {
                     ResultMartix.Text += Result[i, j].ToString();
                     if (j != Result.Columns - 1)
                     {
                         ResultMartix.Text += ",";
                     }
                 }
                 ResultMartix.Text += "\n";
             }
         }
     }
     if (radioButton_Sub.Checked)//减法运算
     {
         ResultMartix.Text = null;
         input Leftinput  = new input(LeftMartix.Text);
         input Rightinput = new input(RightMartix.Text);
         if (!Leftinput.NULL)
         {
             Martix Result = Leftinput.Martix;
             Result = Leftinput.Martix - Rightinput.Martix;
             for (int i = 0; i < Result.Rows; i++)
             {
                 for (int j = 0; j < Result.Columns; j++)
                 {
                     ResultMartix.Text += Result[i, j].ToString();
                     if (j != Result.Columns - 1)
                     {
                         ResultMartix.Text += ",";
                     }
                 }
                 ResultMartix.Text += "\n";
             }
         }
     }
     if (radioButton_Mul.Checked)//乘法运算
     {
         ResultMartix.Text = null;
         input Leftinput  = new input(LeftMartix.Text);
         input Rightinput = new input(RightMartix.Text);
         if (!Leftinput.NULL)
         {
             Martix Result = Leftinput.Martix;
             Result = Leftinput.Martix * Rightinput.Martix;
             for (int i = 0; i < Result.Rows; i++)
             {
                 for (int j = 0; j < Result.Columns; j++)
                 {
                     ResultMartix.Text += Result[i, j].ToString();
                     if (j != Result.Columns - 1)
                     {
                         ResultMartix.Text += ",";
                     }
                 }
                 ResultMartix.Text += "\n";
             }
         }
     }
     if (radioButton_Converse.Checked)//除法运算1,2,3; 2,1,2; 1,3,4
     {
         ResultMartix.Text = null;
         input Leftinput  = new input(LeftMartix.Text);
         input Rightinput = new input(RightMartix.Text);
         if (!Leftinput.NULL)
         {
             Martix Result = Leftinput.Martix;
             Result = Leftinput.Martix / Rightinput.Martix;
             for (int i = 0; i < Result.Rows; i++)
             {
                 for (int j = 0; j < Result.Columns; j++)
                 {
                     ResultMartix.Text += Result[i, j].ToString();
                     if (j != Result.Columns - 1)
                     {
                         ResultMartix.Text += ",";
                     }
                 }
                 ResultMartix.Text += "\n";
             }
         }
     }
 }