Пример #1
0
 internal static int[][] multiply(Matrix A, Matrix B)  //AB矩阵相乘
 {
     if (A == null || B == null || A.tuple == null || B.tuple == null)
     {
         throw new NullReferenceException("请先创建矩阵!");
     }
     if (A.maxcol != B.maxrow)  //A列数需要等于B行数
     {
         throw new ArgumentException("这两个矩阵不可以相乘!");
     }
     int[][] ArrayA = MatrixMethod.toArray(A);
     int[][] ArrayB = MatrixMethod.toArray(B);
     int[][] result = new int[A.maxrow][];            //根据矩阵定义可知相乘行数等于A行数,列数等于B列数
     for (int i = 0; i < A.maxrow; i++)
     {
         int[] column = new int[B.maxcol];
         for (int j = 0; j < B.maxcol; j++)
         {
             column[j] = 0;                                           //所有元素初始化为0
         }
         result[i] = column;
     }
     for (int i = 0; i < A.maxrow; i++)
     {
         for (int j = 0; j < B.maxcol; j++)
         {
             for (int k = 0; k < B.maxrow; k++)
             {
                 result[i][j] += ArrayA[i][k] * ArrayB[k][j]; //对应元素A行乘B列相加
             }
         }
     }
     return(result);
 }
Пример #2
0
 internal void printmatrix()      //打印矩阵
 {
     if (tuple == null)
     {
         Console.WriteLine("请先创建矩阵");
         return;
     }
     Console.WriteLine("打印矩阵");
     int[][] matrix = MatrixMethod.toArray(this); //转换为二维数组
     DataStruct5.printarray(matrix);              //打印二维数组
 }