C#完成矩陣加法、取負、數乘、乘法的辦法。本站提示廣大學習愛好者:(C#完成矩陣加法、取負、數乘、乘法的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成矩陣加法、取負、數乘、乘法的辦法正文
本文實例講述了C#完成矩陣加法、取負、數乘、乘法的辦法。分享給年夜家供年夜家參考。詳細以下:
1.幾個根本函數
1)斷定一個二維數組能否為矩陣:假如每行的列數都相等則是矩陣,沒有元素的二維數組是矩陣
/// <summary> /// 斷定一個二維數組能否為矩陣 /// </summary> /// <param name="matrix">二維數組</param> /// <returns>true:是矩陣 false:不是矩陣</returns> private static bool isMatrix(double[][] matrix) { //空矩陣是矩陣 if (matrix.Length < 1) return true; //分歧行列數假如不相等,則不是矩陣 int count = matrix[0].Length; for (int i = 1; i < matrix.Length; i++) { if (matrix[i].Length != count) { return false; } } //各行列數相等,則是矩陣 return true; }
2)盤算一個矩陣的行數和列數:就是盤算兩個維度的Length屬性
/// <summary> /// 盤算一個矩陣的行數和列數 /// </summary> /// <param name="matrix">矩陣</param> /// <returns>數組:行數、列數</returns> private static int[] MatrixCR(double[][] matrix) { //吸收到的參數不是矩陣則報異常 if (!isMatrix(matrix)) { throw new Exception("吸收到的參數不是矩陣"); } //空矩陣行數列數都為0 if (!isMatrix(matrix) || matrix.Length == 0) { return new int[2] { 0, 0 }; } return new int[2] { matrix.Length, matrix[0].Length }; }
3)向掌握台打印矩陣:留意,假如前後都是兩個char類型的量,則運算符+會把前後兩個字符轉化為整數相加,而不會將前後字符視為字符串聯接
/// <summary> /// 打印矩陣 /// </summary> /// <param name="matrix">待打印矩陣</param> private static void PrintMatrix(double[][] matrix) { for (int i = 0; i < matrix.Length; i++) { for (int j = 0; j < matrix[i].Length; j++) { Console.Write(matrix[i][j] + "\t"); //留意不克不及寫為:Console.Write(matrix[i][j] + '\t'); } Console.WriteLine(); } }
2.矩陣加法
/// <summary> /// 矩陣加法 /// </summary> /// <param name="matrix1">矩陣1</param> /// <param name="matrix2">矩陣2</param> /// <returns>和</returns> private static double[][] MatrixAdd(double[][] matrix1, double[][] matrix2) { //矩陣1和矩陣2須為同型矩陣 if (MatrixCR(matrix1)[0] != MatrixCR(matrix2)[0] || MatrixCR(matrix1)[1] != MatrixCR(matrix2)[1]) { throw new Exception("分歧型矩陣沒法停止加法運算"); } //生成一個與matrix1同型的空矩陣 double[][] result = new double[matrix1.Length][]; for (int i = 0; i < result.Length; i++) { result[i] = new double[matrix1[i].Length]; } //矩陣加法:把矩陣2各元素值加到矩陣1上,前往矩陣1 for (int i = 0; i < result.Length; i++) { for (int j = 0; j < result[i].Length; j++) { result[i][j] = matrix1[i][j] + matrix2[i][j]; } } return result; }
3.矩陣取負
/// <summary> /// 矩陣取負 /// </summary> /// <param name="matrix">矩陣</param> /// <returns>負矩陣</returns> private static double[][] NegtMatrix(double[][] matrix) { //正當性檢討 if (!isMatrix(matrix)) { throw new Exception("傳入的參數其實不是一個矩陣"); } //參數為空矩陣則前往空矩陣 if (matrix.Length == 0) { return new double[][] { }; } //生成一個與matrix同型的空矩陣 double[][] result = new double[matrix.Length][]; for (int i = 0; i < result.Length; i++) { result[i] = new double[matrix[i].Length]; } //矩陣取負:各元素取相反數 for (int i = 0; i < result.Length; i++) { for (int j = 0; j < result[0].Length; j++) { result[i][j] = -matrix[i][j]; } } return result; }
4.矩陣數乘
/// <summary> /// 矩陣數乘 /// </summary> /// <param name="matrix">矩陣</param> /// <param name="num">常數</param> /// <returns>積</returns> private static double[][] MatrixMult(double[][] matrix, double num) { //正當性檢討 if (!isMatrix(matrix)) { throw new Exception("傳入的參數其實不是一個矩陣"); } //參數為空矩陣則前往空矩陣 if (matrix.Length == 0) { return new double[][] { }; } //生成一個與matrix同型的空矩陣 double[][] result = new double[matrix.Length][]; for (int i = 0; i < result.Length; i++) { result[i] = new double[matrix[i].Length]; } //矩陣數乘:用常數順次乘以矩陣各元素 for (int i = 0; i < result.Length; i++) { for (int j = 0; j < result[0].Length; j++) { result[i][j] = matrix[i][j] * num; } } return result; }
5.矩陣乘法
/// <summary> /// 矩陣乘法 /// </summary> /// <param name="matrix1">矩陣1</param> /// <param name="matrix2">矩陣2</param> /// <returns>積</returns> private static double[][] MatrixMult(double[][] matrix1, double[][] matrix2) { //正當性檢討 if (MatrixCR(matrix1)[1] != MatrixCR(matrix2)[0]) { throw new Exception("matrix1 的列數與 matrix2 的行數不想等"); } //矩陣中沒有元素的情形 if (matrix1.Length == 0 || matrix2.Length == 0) { return new double[][] { }; } //matrix1是m*n矩陣,matrix2是n*p矩陣,則result是m*p矩陣 int m = matrix1.Length, n = matrix2.Length, p = matrix2[0].Length; double[][] result = new double[m][]; for (int i = 0; i < result.Length; i++) { result[i] = new double[p]; } //矩陣乘法:c[i,j]=Sigma(k=1→n,a[i,k]*b[k,j]) for (int i = 0; i < m; i++) { for (int j = 0; j < p; j++) { //對乘加軌則 for (int k = 0; k < n; k++) { result[i][j] += (matrix1[i][k] * matrix2[k][j]); } } } return result; }
6.函數挪用示例
1)Main函數代碼
static void Main(string[] args) { //示例矩陣 double[][] matrix1 = new double[][] { new double[] { 1, 2, 3 }, new double[] { 4, 5, 6 }, new double[] { 7, 8, 9 } }; double[][] matrix2 = new double[][] { new double[] { 2, 3, 4 }, new double[] { 5, 6, 7 }, new double[] { 8, 9, 10 } }; //矩陣加法 PrintMatrix(MatrixAdd(matrix1, matrix2)); Console.WriteLine(); //矩陣取負 PrintMatrix(NegtMatrix(matrix1)); Console.WriteLine(); //矩陣數乘 PrintMatrix(MatrixMult(matrix1, 3)); Console.WriteLine(); //矩陣乘法 PrintMatrix(MatrixMult( new double[][] { new double[]{ 4, -1, 2 }, new double[]{ 1, 1, 0 }, new double[]{ 0, 3, 1 }}, new double[][] { new double[]{ 1, 2 }, new double[]{ 0, 1 }, new double[]{ 3, 0 }})); Console.WriteLine(); Console.ReadLine(); }
2)示例運轉成果
願望本文所述對年夜家的C#法式設計有所贊助。