本文实例讲述了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#程序设计有所帮助。
新闻热点
疑难解答