栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#实现矩阵加法、取负、数乘、乘法的方法

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#实现矩阵加法、取负、数乘、乘法的方法

本文实例讲述了C#实现矩阵加法、取负、数乘、乘法的方法。分享给大家供大家参考。具体如下:

1.几个基本函数

1)判断一个二维数组是否为矩阵:如果每行的列数都相等则是矩阵,没有元素的二维数组是矩阵

/// 
/// 判断一个二维数组是否为矩阵
/// 
/// 二维数组
/// true:是矩阵 false:不是矩阵
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属性

/// 
/// 计算一个矩阵的行数和列数
/// 
/// 矩阵
/// 数组:行数、列数
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类型的量,则运算符+会把前后两个字符转化为整数相加,而不会将前后字符视为字符串连接

/// 
/// 打印矩阵
/// 
/// 待打印矩阵
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.矩阵加法

/// 
/// 矩阵加法
/// 
/// 矩阵1
/// 矩阵2
/// 
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.矩阵取负

/// 
/// 矩阵取负
/// 
/// 矩阵
/// 负矩阵
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.矩阵数乘

/// 
/// 矩阵数乘
/// 
/// 矩阵
/// 常数
/// 
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.矩阵乘法

/// 
/// 矩阵乘法
/// 
/// 矩阵1
/// 矩阵2
/// 
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#程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/124864.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号