Matrix(C#实现).docx_第1页
Matrix(C#实现).docx_第2页
Matrix(C#实现).docx_第3页
Matrix(C#实现).docx_第4页
Matrix(C#实现).docx_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

更多资料using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;using System.Diagnostics;namespace WindowsFormsApplication1 public class Matrix public Matrix(int row) m_data = new doublerow, row; public Matrix(int row, int col) m_data = new doublerow, col; /复制构造函数 public Matrix(Matrix m) /有什么用 int row = m.Row; int col = m.Col; m_data = new doublerow, col; for (int i = 0; i row; i+) for (int j = 0; j col; j+) m_datai, j = mi, j; /*/ /* /分配方阵的大小 /对于已含有内存的矩阵,将清空数据 public void SetSize(int row) m_data = new doublerow,row; /分配矩阵的大小 /对于已含有内存的矩阵,将清空数据 public void SetSize(int row,int col) m_data = new doublerow,col; */ /unit matrix:设为单位阵 public void SetUnit() for (int i = 0; i m_data.GetLength(0); i+) for (int j = 0; j m_data.GetLength(1); j+) m_datai, j = (i = j) ? 1 : 0); /设置元素值 public void SetValue(double d) for (int i = 0; i m_data.GetLength(0); i+) for (int j = 0; j m_data.GetLength(1); j+) m_datai, j = d; / Value extraction:返中行数 public int Row get return m_data.GetLength(0); /返回列数 public int Col get return m_data.GetLength(1); /重载索引 /存取数据成员 public double thisint row, int col get return m_datarow, col; set m_datarow, col = value; /primary change /初等变换对调两行:rirj public Matrix Exchange(int i, int j) double temp; for (int k = 0; k Col; k+) temp = m_datai, k; m_datai, k = m_dataj, k; m_dataj, k = temp; return this; /初等变换第index 行乘以mul public Matrix Multiple(int index, double mul) for (int j = 0; j Col; j+) m_dataindex, j *= mul; return this; /初等变换 第src行乘以mul加到第index行 public Matrix MultipleAdd(int index, int src, double mul) for (int j = 0; j Col; j+) m_dataindex, j += m_datasrc, j * mul; return this; /transpose 转置 public Matrix Transpose() Matrix ret = new Matrix(Col, Row); for (int i = 0; i Row; i+) for (int j = 0; j Col; j+) retj, i = m_datai, j; return ret; /binary addition 矩阵加 public static Matrix operator +(Matrix lhs, Matrix rhs) if (lhs.Row != rhs.Row) /异常 System.Exception e = new Exception(相加的两个矩阵的行数不等); throw e; if (lhs.Col != rhs.Col) /异常 System.Exception e = new Exception(相加的两个矩阵的列数不等); throw e; int row = lhs.Row; int col = lhs.Col; Matrix ret = new Matrix(row, col); for (int i = 0; i row; i+) for (int j = 0; j col; j+) double d = lhsi, j + rhsi, j; reti, j = d; return ret; /binary subtraction 矩阵减 public static Matrix operator -(Matrix lhs, Matrix rhs) if (lhs.Row != rhs.Row) /异常 System.Exception e = new Exception(相减的两个矩阵的行数不等); throw e; if (lhs.Col != rhs.Col) /异常 System.Exception e = new Exception(相减的两个矩阵的列数不等); throw e; int row = lhs.Row; int col = lhs.Col; Matrix ret = new Matrix(row, col); for (int i = 0; i row; i+) for (int j = 0; j col; j+) double d = lhsi, j - rhsi, j; reti, j = d; return ret; /binary multiple 矩阵乘 public static Matrix operator *(Matrix lhs, Matrix rhs) if (lhs.Col != rhs.Row) /异常 System.Exception e = new Exception(相乘的两个矩阵的行列数不匹配); throw e; Matrix ret = new Matrix(lhs.Row, rhs.Col); double temp; for (int i = 0; i lhs.Row; i+) for (int j = 0; j rhs.Col; j+) temp = 0; for (int k = 0; k lhs.Col; k+) temp += lhsi, k * rhsk, j; reti, j = temp; return ret; /binary division 矩阵除 public static Matrix operator /(Matrix lhs, Matrix rhs) return lhs * rhs.Inverse(); /unary addition单目加 public static Matrix operator +(Matrix m) Matrix ret = new Matrix(m); return ret; /unary subtraction 单目减 public static Matrix operator -(Matrix m) Matrix ret = new Matrix(m); for (int i = 0; i ret.Row; i+) for (int j = 0; j ret.Col; j+) reti, j = -reti, j; return ret; /number multiple 数乘 public static Matrix operator *(double d, Matrix m) Matrix ret = new Matrix(m); for (int i = 0; i ret.Row; i+) for (int j = 0; j ret.Col; j+) reti, j *= d; return ret; /number division 数除 public static Matrix operator /(double d, Matrix m) return d * m.Inverse(); /功能:返回列主元素的行号 /参数:row为开始查找的行号 /说明:在行号row,Col)范围内查找第row列中绝对值最大的元素,返回所在行号 int Pivot(int row) int index = row; for (int i = row + 1; i m_dataindex, row) index = i; return index; /inversion 逆阵:使用矩阵的初等变换,列主元素消去法 public Matrix Inverse() if (Row != Col) /异常,非方阵 System.Exception e = new Exception(求逆的矩阵不是方阵); throw e; / StreamWriter sw = new StreamWriter(.annexclose_matrix.txt); Matrix tmp = new Matrix(this); Matrix ret = new Matrix(Row); /单位阵 ret.SetUnit(); int maxIndex; double dMul; for (int i = 0; i Row; i+) maxIndex = tmp.Pivot(i); if (tmp.m_datamaxIndex, i = 0) System.Exception e = new Exception(求逆的矩阵的行列式的值等于0,); throw e; if (maxIndex != i) /下三角阵中此列的最大值不在当前行,交换 tmp.Exchange(i, maxIndex); ret.Exchange(i, maxIndex); ret.Multiple(i, 1 / tmpi, i); tmp.Multiple(i, 1 / tmpi, i); for (int j = i + 1; j 0; i-) for (int j = i - 1; j = 0; j-) dMul = -tmpj, i / tmpi, i; tmp.MultipleAdd(j, i, dMul); ret.MultipleAdd(j, i, dMul); /end for / sw.WriteLine(tmp=rn + tmp); / sw.WriteLine(ret=rn + ret); / sw.WriteLine(*=rn + this * ret); / sw.Close(); return ret; /end Inverse #region /*/ /* /inversion 逆阵:使用矩阵的初等变换,列主元素消去法 public Matrix Inverse() if(Row != Col) /异常,非方阵 System.Exception e = new Exception(求逆的矩阵不是方阵); throw e; / StreamWriter sw = new StreamWriter(.annexmatrix_mul.txt); / / Matrix tmp = new Matrix(this); Matrix ret =new Matrix(Row); /单位阵 ret.SetUnit(); int maxIndex; double dMul; for(int i=0;iRow;i+) maxIndex = tmp.Pivot(i); if(tmp.m_datamaxIndex,i=0) System.Exception e = new Exception(求逆的矩阵的行列式的值等于0,); throw e; if(maxIndex != i) /下三角阵中此列的最大值不在当前行,交换 tmp.Exchange(i,maxIndex); ret.Exchange(i,maxIndex); ret.Multiple(i,1/tmpi,i); / /sw.WriteLine(nul t+tmpi,i+t+reti,i); / tmp.Multiple(i,1/tmpi,i); /sw.WriteLine(mmm t+tmpi,i+t+reti,i); sw.WriteLine(111111111 tmp=rn+tmp); for(int j=i+1;j0;i-) for(int j=i-1;j=0;j-) dMul = -tmpj,i; tmp.MultipleAdd(j,i,dMul); ret.MultipleAdd(j,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论