用c++编写的用于矩阵计算的类_第1页
用c++编写的用于矩阵计算的类_第2页
用c++编写的用于矩阵计算的类_第3页
用c++编写的用于矩阵计算的类_第4页
用c++编写的用于矩阵计算的类_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

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

文档简介

头文件和代码文件都在这篇文档中头文件和代码文件都在这篇文档中 h 文件 include 基本输入输出 include 用于数据转换 从字符串中提取数字 include 数学函数库 include 用于生成随机 include 的矩阵 using namespace std 以下 定义了矩阵中的数据分布样式 日期 2008 8 4 作者 杨赋庚 define Zero 0 define One 1 define Number 2 define Unit 3 define Rand 4 下面 定义了几种矩阵变换方式 define All 5 define Horizontal 6 define Vertical 7 define Nomal 8 define Pretty 9 用一维数组表示二维矩阵 程序中表示矩阵的方法 第一行 第二行 第三行 第四行 class Matrix 受保护对象 protected int rows 总行数 int cols 总列数 float m pData 矩阵数据块 int size 矩阵数据块的大小 float Init int rows 1 int cols 1 int style Zero int num 0 初始化 0 化 public 矩阵的初始化函数 Matrix Matrix Matrix int rows 1 int cols 1 int style Zero int num 0 Matrix char Matrix int rows int cols float a Matrix int size float a 公有接口函数 Matrix operator float Matrix operator float Matrix operator Matrix Matrix operator Matrix Matrix operator Matrix Matrix operator Matrix Matrix operator char bool operator Matrix bool operator Matrix void Clear 设置 获取矩阵中元素 float GetElement int row 0 int col 0 bool SetElement float value 0 int row 0 int col 0 float GetData return m pData void StrToMatrix char string MatrixToStr int GetRows return rows int GetCols return cols int GetSize return size Matrix friend ostream 算法 函数名 Find 函数说明 style1 指定查找方式 Vertical 为垂直查找 Horizontal 为水平查找 pos1 和 pos2 指定了查找区间 Index 指定了查找操作所处的行 列 k 为比较参数 style2 指定了比较方式 0 则返回相等的目标 否则返回不等的目标 int Find float k int Index int pos1 int pos2 int style1 Vertical int style2 0 函数名 Move 函数说明 将矩阵的某行 列 乘以常数 加到另外一行 列 上 void Move float k int Index1 0 int Index2 1 int Direction Vertical 函数名 Times 函数说明 将矩阵的某一行 列 乘以一个常数 void Times float k int Index int Direction Horizontal 函数名 Devided 函数说明 将矩阵的某一行 列 除以一个常数 void Devided float k int Index int Direction Horizontal 函数名 Change 函数说明 交换矩阵中的两个行 列 void Change int Index1 0 int Index2 0 int Direction Horizontal 函数名 Adverse 函数说明 利用代数余子式法 计算矩阵的逆 Matrix Adverse 函数名 Simplify 函数说明 利用消元法 将矩阵最简化 bool Simplify int style Nomal Matrix Star 计算矩阵的 float Abs 计算矩阵的行列式 Matrix Left int row 0 int col 0 计算矩阵中某元素的余子式 float NumLeft int row 0 int col 0 计算矩阵中某元素的代数余子式 int Rank Matrix Transpose 将矩阵转置 cpp 文件 include Matrix h float Matrix Init int rows int cols int style int num int row int col if rows 0 cols 0 rows 1 cols 1 size 1 m pData new float 1 m pData 0 return m pData rows rows cols cols size rows cols m pData new float size if style Zero for row 0 row rows row for col 0 col cols col m pData row cols col 0 else if style One for row 0 row rows row for col 0 col cols col m pData row cols col 1 else if style One for row 0 row rows row for col 0 col cols col m pData row cols col num else if style Unit if rows cols for row 0 row rows row for col 0 col cols col if row col m pData row cols col 1 else m pData row cols col 0 else if style Rand srand unsigned int time NULL for row 0 row rows row for col 0 col cols col m pData row cols col rand 123 else return m pData Matrix Matrix int rows int cols int style int num Init rows cols style num Matrix Matrix Matrix rows M GetRows cols M GetCols size M GetSize float m pData target M GetData m pData new float size for i 0 i size i m pData i m pData target i Matrix Matrix char str m pData 0 this str Matrix Matrix int rows int cols float a rows rows cols cols size rows cols m pData new float size int i for i 0 i size i m pData i a i Matrix Matrix int size float a size size rows sqrt size cols rows m pData new float size int i for i 0 i size i m pData i a i Matrix Matrix operator Matrix rows M GetRows cols M GetCols size M GetSize float m pData target M GetData if m pData delete m pData m pData new float size for i 0 i size i m pData i m pData target i return this Matrix Matrix operator char str int row int col rows 1 cols 1 char p str while p 0 if p cols else if p rows cols p cols cols rows row 0 col 0 size rows cols if m pData delete m pData m pData new float size p str char temp 128 int k 1 while p 0 if p temp k 0 m pData row cols col atof temp k 1 col else if p temp k 0 m pData row cols col atof temp k 1 col 0 row else temp k p p temp k 0 m pData row cols col atof temp return this bool Matrix operator Matrix int cols M GetCols if rows rows int i for i 0 i size i if m pData i m pData temp i return false return true else return false bool Matrix operator Matrix int cols M GetCols if rows rows int i for i 0 i size i if m pData i m pData temp i return false return true else return true float Matrix GetElement int row int col return m pData row cols col bool Matrix SetElement float value int row int col m pData row cols col value return true void Matrix StrToMatrix char str this str string Matrix MatrixToStr string str char temp 32 int row int col for row 0 row rows row gcvt m pData row cols 30 temp str temp for col 1 col cols col str gcvt m pData row cols col 30 temp str temp if row rows str return str void Matrix Clear cols 0 rows 0 delete m pData Matrix Matrix delete m pData ostream int col int rows M GetRows int cols M GetCols float m pData M GetData for row 0 row rows row for col 0 col cols col out m pData row cols col t out endl istream cin temp M temp return in Matrix Matrix operator Matrix int rows 2 M GetRows int cols 2 M GetCols float m pData 2 M GetData if rows rows 2 temp1 Clear m pData 1 temp1 Init rows cols int i for i 0 i size i m pData 1 i m pData i m pData 2 i else temp1 this return temp1 Matrix Matrix operator Matrix int rows 2 M GetRows int cols 2 M GetCols float m pData 2 M GetData if rows rows 2 temp1 Clear m pData 1 temp1 Init rows cols int i for i 0 i size i m pData 1 i m pData i m pData 2 i else temp1 this return temp1 Matrix Matrix operator Matrix int col Matrix temp1 int rows 2 M GetRows int cols 2 M GetCols float m pData 2 M GetData if cols rows 2 float m pData 1 temp1 Clear m pData 1 temp1 Init rows cols 2 for row 0 row rows row for col 0 col cols 2 col float sum 0 int i for i 0 i cols i sum m pData row cols i m pData 2 i rows col m pData 1 row cols col sum else temp1 this return temp1 Matrix Matrix operator float k Matrix temp temp Clear float m pData 1 temp Init rows cols int i for i 0 i size i m pData 1 i m pData i k return temp Matrix Matrix operator float k Matrix temp temp Clear float m pData 1 temp Init rows cols int i if k 0 for i 0 i size i m pData 1 i m pData i k else temp this return temp Matrix Matrix Transpose int row int col Matrix temp temp Clear float m pData 1 temp Init cols rows for col 0 col cols col for row 0 row rows row m pData 1 col rows row m pData row cols col return temp Matrix Matrix Left int row int col int row int col Matrix temp temp Clear float m pData 1 temp Init rows 1 cols 1 int i 1 for row 0 row rows row if row row for col 0 col cols col if col col m pData 1 i m pData row cols col return temp float Matrix Abs 基本算法 float abs 0 if rows cols if rows 1 return m pData 0 else int i int state 1 for i 0 i cols i state state abs m pData i state Left 0 i Abs return abs else return 1 上三角算法 Matrix temp this temp Simplify float m pData temp temp GetData int i float sum 1 for i 0 i rows i sum m pData temp i cols i return sum float Matrix NumLeft int row int col return Left row col Abs pow 1 row col Matrix Matrix Adverse Matrix temp float abs Abs if rows cols abs 0 return this temp Clear float m pData temp Init rows cols int i int j for i 0 i rows i for j 0 j cols j m pData i cols j NumLeft j i abs return temp Matrix Matrix Star Matrix temp if rows cols return this temp Clear float m pData temp Init rows cols int i int j for i 0 i rows i for j 0 j cols j m pData i cols j NumLeft j i return temp void Matrix Change int Index1 int Index2 int Direction int row int col float temp if Direction Horizontal for row 0 row rows row temp m pData row cols Index2 m pData row cols Index2 m pData row cols Index1 m pData row cols Index1 temp else if Direction Vertical for col 0 col cols col temp m pData Index2 cols col m pData Index2 cols col m pData Index1 cols col m pData Index1 cols col temp void Matrix Devided float k int Index int Direction int row int col if Direction Horizontal for col 0 col cols col m pData Index cols col k else if Direction Vertical for row 0 row rows row m pData row cols Index k else void Matrix Times float k int Index int Direction int row int col if Direction Horizontal for col 0 col cols col m pData Index cols col k else if Direction Vertical for row 0 row rows row m pData row cols Index k else void Matrix Move float k int Index1 int Index2 int Direction int row int col if Direction Horizontal for row 0 row rows row m pData row cols Index2 m pData row cols Index1 k else if Direction Vertical for col 0 col cols col m pData Index2 cols col m pData Index1 cols col k else bool Matrix Simplify int style int i int j if rows 1 else 以下 确保主斜线上没有 0 for i 0 i rows i if m pData i cols i 0

温馨提示

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

评论

0/150

提交评论