欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中矩阵运算方法实例分析

程序员文章站 2022-06-09 13:42:08
本文实例讲述了c#中矩阵运算方法。分享给大家供大家参考。具体分析如下: 一、测试环境: 主机:xp 开发环境:vs2008 二、功能: 在c#中实现矩阵运算 三...

本文实例讲述了c#中矩阵运算方法。分享给大家供大家参考。具体分析如下:

一、测试环境:

主机:xp

开发环境:vs2008

二、功能:

在c#中实现矩阵运算

三、源代码:

using system;
using system.collections.generic;
using system.componentmodel;
using system.data;
using system.drawing;
using system.linq;
using system.text;
using system.windows.forms;
//矩阵数据结构 
//二维矩阵 
class _matrix 
{ 
 public int m; 
 public int n; 
 public float[] arr;
 //初始化 
 public _matrix()
 {
  m = 0; 
  n = 0; 
 }
 public _matrix(int mm,int nn)
 {
  m = mm; 
  n = nn; 
 }
 //设置m 
 public void set_mn(int mm,int nn)
 {
  m = mm; 
  n = nn; 
 } 

 //设置m 
 public void set_m(int mm)
 { 
  m = mm; 
 } 
 //设置n 
 public void set_n(int nn)
 { 
  n = nn; 
 }
 //初始化 
 public void init_matrix()
 { 
  arr = new float[m * n]; 
 } 
 //释放 
 public void free_matrix()
 {
  //delete [] arr;
 } 
 //读取i,j坐标的数据 
 //失败返回-31415,成功返回值 
 public float read(int i,int j)
 {
  if (i >= m || j >= n)
  {
   return -31415;
  }
  //return *(arr + i * n + j);
  return arr[i * n + j];
 } 
 //写入i,j坐标的数据 
 //失败返回-1,成功返回1 
 public int write(int i,int j,float val)
 {
  if (i >= m || j >= n)
  {
   return -1;
  }
  arr[i * n + j] = val;
  return 1;
 } 
};
//二维运算类 
class _matrix_calc 
{ 
 //初始化
 public _matrix_calc()
 {
 }
 //c = a + b 
 //成功返回1,失败返回-1 
 public int add(ref _matrix a,ref _matrix b,ref _matrix c)
 { 
  int i = 0; 
  int j = 0; 
  //判断是否可以运算 
  if (a.m != b.m || a.n != b.n || 
   a.m != c.m || a.n != c.n) 
  { 
   return -1; 
  } 
  //运算 
  for (i = 0;i < c.m;i++) 
  { 
   for (j = 0;j < c.n;j++) 
   { 
    c.write(i,j,a.read(i,j) + b.read(i,j)); 
   } 
  } 
  return 1; 
 } 
 //c = a - b 
 //成功返回1,失败返回-1 
 public int subtract(ref _matrix a,ref _matrix b, ref _matrix c)
 { 
  int i = 0; 
  int j = 0; 
  //判断是否可以运算 
  if (a.m != b.m || a.n != b.n || 
   a.m != c.m || a.n != c.n) 
  { 
   return -1; 
  } 
  //运算 
  for (i = 0;i < c.m;i++) 
  { 
   for (j = 0;j < c.n;j++) 
   { 
    c.write(i,j,a.read(i,j) - b.read(i,j)); 
   } 
  } 
  return 1; 
 } 
 //c = a * b 
 //成功返回1,失败返回-1 
 public int multiply(ref _matrix a, ref _matrix b, ref _matrix c)
 { 
  int i = 0; 
  int j = 0; 
  int k = 0; 
  float temp = 0; 
  //判断是否可以运算 
  if (a.m != c.m || b.n != c.n || 
   a.n != b.m) 
  { 
   return -1; 
  } 
  //运算 
  for (i = 0;i < c.m;i++) 
  { 
   for (j = 0;j < c.n;j++) 
   { 
    temp = 0; 
    for (k = 0;k < a.n;k++) 
    { 
     temp += a.read(i,k) * b.read(k,j); 
    } 
    c.write(i,j,temp); 
   } 
  } 
  return 1; 
 } 
 //行列式的值,只能计算2 * 2,3 * 3 
 //失败返回-31415,成功返回值 
 public float det(ref _matrix a)
 { 
  float value = 0; 
  //判断是否可以运算 
  if (a.m != a.n || (a.m != 2 && a.m != 3)) 
  { 
   return -31415; 
  } 
  //运算 
  if (a.m == 2) 
  { 
   value = a.read(0,0) * a.read(1,1) - a.read(0,1) * a.read(1,0); 
  } 
  else 
  { 
   value = a.read(0,0) * a.read(1,1) * a.read(2,2) + 
     a.read(0,1) * a.read(1,2) * a.read(2,0) + 
     a.read(0,2) * a.read(1,0) * a.read(2,1) - 
     a.read(0,0) * a.read(1,2) * a.read(2,1) - 
     a.read(0,1) * a.read(1,0) * a.read(2,2) - 
     a.read(0,2) * a.read(1,1) * a.read(2,0); 
  } 
  return value; 
 }
 //求转置矩阵,b = at 
 //成功返回1,失败返回-1 
 public int transpos(ref _matrix a,ref _matrix b)
 { 
  int i = 0; 
  int j = 0; 
  //判断是否可以运算 
  if (a.m != b.n || a.n != b.m) 
  { 
   return -1; 
  } 
  //运算 
  for (i = 0;i < b.m;i++) 
  { 
   for (j = 0;j < b.n;j++) 
   { 
    b.write(i,j,a.read(j,i)); 
   } 
  } 
  return 1; 
 } 
 //求逆矩阵,b = a^(-1) 
 //成功返回1,失败返回-1 
 public int inverse(ref _matrix a, ref _matrix b)
 { 
  int i = 0; 
  int j = 0; 
  int k = 0; 
  _matrix m = new _matrix(a.m,2 * a.m); 
  float temp = 0; 
  float b = 0; 
  //判断是否可以运算 
  if (a.m != a.n || b.m != b.n || a.m != b.m) 
  { 
   return -1; 
  } 
  /* 
  //如果是2维或者3维求行列式判断是否可逆 
  if (a.m == 2 || a.m == 3) 
  { 
   if (det(a) == 0) 
   { 
    return -1; 
   } 
  } 
  */ 
  //增广矩阵m = a | b初始化 
  m.init_matrix(); 
  for (i = 0;i < m.m;i++) 
  { 
   for (j = 0;j < m.n;j++) 
   { 
    if (j <= a.n - 1) 
    { 
     m.write(i,j,a.read(i,j)); 
    } 
    else 
    { 
     if (i == j - a.n) 
     { 
      m.write(i,j,1); 
     } 
     else 
     { 
      m.write(i,j,0); 
     } 
    } 
   } 
  } 
  //高斯消元 
  //变换下三角 
  for (k = 0;k < m.m - 1;k++) 
  { 
   //如果坐标为k,k的数为0,则行变换 
   if (m.read(k,k) == 0) 
   { 
    for (i = k + 1;i < m.m;i++) 
    { 
     if (m.read(i,k) != 0) 
     { 
      break; 
     } 
    } 
    if (i >= m.m) 
    { 
     return -1; 
    } 
    else 
    { 
     //交换行 
     for (j = 0;j < m.n;j++) 
     { 
      temp = m.read(k,j); 
      m.write(k,j,m.read(k + 1,j)); 
      m.write(k + 1,j,temp); 
     } 
    } 
   } 
   //消元 
   for (i = k + 1;i < m.m;i++) 
   { 
    //获得倍数 
    b = m.read(i,k) / m.read(k,k); 
    //行变换 
    for (j = 0;j < m.n;j++) 
    { 
     temp = m.read(i,j) - b * m.read(k,j); 
     m.write(i,j,temp); 
    } 
   } 
  } 
  //变换上三角 
  for (k = m.m - 1;k > 0;k--) 
  { 
   //如果坐标为k,k的数为0,则行变换 
   if (m.read(k,k) == 0) 
   { 
    for (i = k + 1;i < m.m;i++) 
    { 
     if (m.read(i,k) != 0) 
     { 
      break; 
     } 
    } 
    if (i >= m.m) 
    { 
     return -1; 
    } 
    else 
    { 
     //交换行 
     for (j = 0;j < m.n;j++) 
     { 
      temp = m.read(k,j); 
      m.write(k,j,m.read(k + 1,j)); 
      m.write(k + 1,j,temp); 
     } 
    } 
   } 
   //消元 
   for (i = k - 1;i >= 0;i--) 
   { 
    //获得倍数 
    b = m.read(i,k) / m.read(k,k); 
    //行变换 
    for (j = 0;j < m.n;j++) 
    { 
     temp = m.read(i,j) - b * m.read(k,j); 
     m.write(i,j,temp); 
    } 
   } 
  } 
  //将左边方阵化为单位矩阵 
  for (i = 0;i < m.m;i++) 
  { 
   if (m.read(i,i) != 1) 
   { 
    //获得倍数 
    b = 1 / m.read(i,i); 
    //行变换 
    for (j = 0;j < m.n;j++) 
    { 
     temp = m.read(i,j) * b; 
     m.write(i,j,temp); 
    } 
   } 
  } 
  //求得逆矩阵 
  for (i = 0;i < b.m;i++) 
  { 
   for (j = 0;j < b.m;j++) 
   { 
    b.write(i,j,m.read(i,j + m.m)); 
   } 
  } 
  //释放增广矩阵 
  m.free_matrix(); 
  return 1; 
 } 
}; 
namespace test
{
 public partial class form1 : form
 {
  double zk;
  double xkg, pkg, kk, xk, pk, q, r;
  public form1()
  {
   initializecomponent();
   xk = 0;
   pk = 0;
   q = 0.00001;
   r = 0.0001;

   int i = 0;
   int j = 0;
   int k = 0; 
   _matrix_calc m_c = new _matrix_calc(); 
   //_matrix m1 = new _matrix(3,3); 
   //_matrix m2 = new _matrix(3,3);
   //_matrix m3 = new _matrix(3,3);
   _matrix m1 = new _matrix(2, 2);
   _matrix m2 = new _matrix(2, 2);
   _matrix m3 = new _matrix(2, 2); 
   //初始化内存 
   m1.init_matrix(); 
   m2.init_matrix(); 
   m3.init_matrix(); 
   //初始化数据 
   k = 1; 
   for (i = 0;i < m1.m;i++) 
   { 
    for (j = 0;j < m1.n;j++) 
    { 
     m1.write(i,j,k++); 
    } 
   } 
   for (i = 0;i < m2.m;i++) 
   { 
    for (j = 0;j < m2.n;j++) 
    { 
     m2.write(i,j,k++); 
    } 
   }
   m_c.multiply(ref m1,ref m2, ref m3);
   //output.text = convert.tostring(m3.read(1,1));
   output.text = convert.tostring(m_c.det(ref m1));
  }
  /*
  private void button1_click(object sender, eventargs e)
  {
   zk = convert.todouble(input.text);
   //时间方程
   xkg = xk;
   pkg = pk + q;
   //状态方程
   kk = pkg / (pkg + r);
   xk = xkg + kk * (zk - xkg);
   pk = (1 - kk) * pkg;
   //输出
   output.text = convert.tostring(xk);
  }
  private void textbox1_textchanged(object sender, eventargs e)
  {
  }
   * */
 }
}

希望本文所述对大家的c#程序设计有所帮助。