国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法

2020-01-24 01:33:07
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了C#實現(xiàn)矩陣加法、取負、數(shù)乘、乘法的方法。分享給大家供大家參考。具體如下:

1.幾個基本函數(shù)

1)判斷一個二維數(shù)組是否為矩陣:如果每行的列數(shù)都相等則是矩陣,沒有元素的二維數(shù)組是矩陣

/// <summary>/// 判斷一個二維數(shù)組是否為矩陣/// </summary>/// <param name="matrix">二維數(shù)組</param>/// <returns>true:是矩陣 false:不是矩陣</returns>private static bool isMatrix(double[][] matrix){ //空矩陣是矩陣 if (matrix.Length < 1) return true; //不同行列數(shù)如果不相等,則不是矩陣 int count = matrix[0].Length; for (int i = 1; i < matrix.Length; i++) {  if (matrix[i].Length != count)  {   return false;  } } //各行列數(shù)相等,則是矩陣 return true;}

2)計算一個矩陣的行數(shù)和列數(shù):就是計算兩個維度的Length屬性

/// <summary>/// 計算一個矩陣的行數(shù)和列數(shù)/// </summary>/// <param name="matrix">矩陣</param>/// <returns>數(shù)組:行數(shù)、列數(shù)</returns>private static int[] MatrixCR(double[][] matrix){ //接收到的參數(shù)不是矩陣則報異常 if (!isMatrix(matrix)) {  throw new Exception("接收到的參數(shù)不是矩陣"); } //空矩陣行數(shù)列數(shù)都為0 if (!isMatrix(matrix) || matrix.Length == 0) {  return new int[2] { 0, 0 }; } return new int[2] { matrix.Length, matrix[0].Length };}

3)向控制臺打印矩陣:注意,如果前后都是兩個char類型的量,則運算符+會把前后兩個字符轉化為整數(shù)相加,而不會將前后字符視為字符串連接

/// <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("傳入的參數(shù)并不是一個矩陣"); } //參數(shù)為空矩陣則返回空矩陣 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]; } //矩陣取負:各元素取相反數(shù) 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.矩陣數(shù)乘

/// <summary>/// 矩陣數(shù)乘/// </summary>/// <param name="matrix">矩陣</param>/// <param name="num">常數(shù)</param>/// <returns>積</returns>private static double[][] MatrixMult(double[][] matrix, double num){ //合法性檢查 if (!isMatrix(matrix)) {  throw new Exception("傳入的參數(shù)并不是一個矩陣"); } //參數(shù)為空矩陣則返回空矩陣 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]; } //矩陣數(shù)乘:用常數(shù)依次乘以矩陣各元素 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 的列數(shù)與 matrix2 的行數(shù)不想等"); } //矩陣中沒有元素的情況 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.函數(shù)調用示例

1)Main函數(shù)代碼

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(); //矩陣數(shù)乘 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#程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 治县。| 绍兴市| 大竹县| 沙湾县| 湘潭县| 鄱阳县| 蒙城县| 普兰店市| 沈丘县| 绥江县| 自贡市| 金寨县| 宜黄县| 星座| 望奎县| 合江县| 南康市| 诸暨市| SHOW| 霍城县| 皋兰县| 德江县| 福泉市| 舞钢市| 曲水县| 广德县| 廉江市| 峨边| 北宁市| 惠安县| 宁乡县| 宜城市| 深圳市| 柞水县| 宜君县| 恩平市| 连州市| 论坛| 云阳县| 密山市| 汝城县|