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

所属分类: 软件编程 / C#教程 阅读数: 76
收藏 0 赞 0 分享

本文实例讲述了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#程序设计有所帮助。

更多精彩内容其他人还在看

c#开发word批量转pdf源码分享

已经安装有Office环境,借助一些简单的代码即可实现批量Word转PDF,看下面的实例源码吧
收藏 0 赞 0 分享

c# xml API操作的小例子

这篇文章主要介绍了c# xml API操作的小例子,有需要的朋友可以参考一下
收藏 0 赞 0 分享

c#唯一值渲染实例代码

这篇文章主要介绍了c#唯一值渲染实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

淘宝IP地址库采集器c#代码

这篇文章主要介绍了淘宝IP地址库采集器c#代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C#在后台运行操作(BackgroundWorker用法)示例分享

BackgroundWorker类允许在单独的专用线程上运行操作。如果需要能进行响应的用户界面,而且面临与这类操作相关的长时间延迟,则可以使用BackgroundWorker类方便地解决问题,下面看示例
收藏 0 赞 0 分享

c#文本加密程序代码示例

这是一个加密软件,但只限于文本加密,加了窗口控件的滑动效果,详细看下面的代码
收藏 0 赞 0 分享

c#生成站点地图(SiteMapPath)文件示例程序

这篇文章主要介绍了c#生成站点地图(SiteMapPath)文件的示例,大家参考使用
收藏 0 赞 0 分享

C# 键盘Enter键取代Tab键实现代码

这篇文章主要介绍了C# 键盘Enter键取代Tab键实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

C# WinForm导出Excel方法介绍

在.NET应用中,导出Excel是很常见的需求,导出Excel报表大致有以下三种方式:Office PIA,文件流和NPOI开源库,本文只介绍前两种方式
收藏 0 赞 0 分享

C#串口通信程序实例详解

在.NET平台下创建C#串口通信程序,.NET 2.0提供了串口通信的功能,其命名空间是System.IO.Ports,创建C#串口通信程序的具体实现是如何的呢?让我们开始吧
收藏 0 赞 0 分享
查看更多