【刘庆源码共享】稀疏线性系统求解算法MGMRES(m) 之 矩阵类(C++)

/* 
 * Copyright (c) 2009 湖南师范大学数计院 一心飞翔项目组
 * All Right Reserved
 *
 * 文件名:matrix.h
 * 摘  要:定义矩阵类,包括矩阵的相关信息和方法
 *
 * 作  者:刘 庆
 * 完成日期:2009年2月28日
 *
*/


#ifndef _MATRIX_         // 防止matrix.h被重复引用
#define _MATRIX_

#include <string>         // 引用标准库的头文件
#include <iostream>
#include "define.h"      // 引用非标准库的头文件
#include "Number.h"

using namespace std;

/* 元素类,包含值及索引 */
class Point
{
public:
 Double value;         /* 数值 */
 long index;             /* 索引值 */
};

/* 结点类,包含矩阵所有元素的信息 */
class Node
{                    /* MCRF存储法 */
protected:
 long length;               /* point的长度 */
public:
 Point* point;             /* 结点信息 */

 Node();                     /* 默认构造函数 */
 Node(const Node& node);        /* 拷贝构造函数 */
 void Delete();                           /* 释放point的空间 */
 ~Node();                                  /* 析构函数 */
 Point& operator[](const long index) const; /* 重载[],判断参数索引值是否越界 */
 Node& operator=(const Node& node);       /* 重载 等于号,创建一个与参数一样Node对象 */
 void GetSpace(const long len);                   /* 替point申请空间 */
 long Length() const;                                   /* 得到point的长度 */
}; 

/* 矩阵类 包含矩阵的总行数、总列数、总的非零元素个数,以及矩阵所有元素的信息 */
class Matrix
{
protected:
 long total_elem;           /* 总非零元素个数 */
 long total_ln;                /* 总行数 */
 long total_col;              /* 总列数 */
public:
 Node data;                   /* 组合 结点类,以表示矩阵元素信息 */

public:
 Matrix();                       /* 默认构造函数 */
 Matrix(const Matrix &matrix);     /* 拷贝构造函数 */
 ~Matrix();                                  /* 析构函数 */
 void InitZero();                          /* 置0函数,将三个值置为0 */
 void SetTotal_elem(long totalElem);  /* 替total_elem赋值 */
 long GetTotal_elem();                        /* 返回total_elem */
 void SetTotal_ln(long totalLn);          /* 替total_ln赋值 */
 long GetTotal_ln();                            /* 返回total_ln */
 void SetTotal_col(long totalCol);       /* 替total_col赋值 */
 long GetTotal_col();                          /* 返回total_col */

 void Init(Double value);                    /* 初始化矩阵信息 index赋值为-1,value赋值为参数值 */
 void Display(char *str = 0) const;     /* 显示矩阵信息,参数为提示信息 */
 void DisplayAsMatrix(char *str = 0, int zero = 1) const;       /* 以矩阵的形式输出矩阵, zero == 1 输出0元素,否则不输出 */
 void QuickSortLn(long *ln,const long low,const long high); /* 用快速排序法将行标排序 */  
 long QSPartitionLn(long *ln,long low,long high);                 /* 实现行标的分区 */
 void QuickSortCol(const long low, const long high);            /* 用快速排序法将列标排序 */
 long QSPartitionCol(long low,long high);                             /* 实现列标的分区 */
 long QuickLocate(const long ln, const long col)const;          /* 用折半查找,搜索[ln,col]位置的元素,不为零则返回该值的位置,否则返回-1; */
 
 void LnTransformMultiply(const long base,const Double& gene); /* 矩阵行初等变换 第base行乘以gene */
 void LnTransfromAdd(const long base,const long ln);                  /* 矩阵行初等变换 第base行加上第ln行 */ 
 Matrix& Transpose();                                               /* 矩阵转置 */ 
 void ToBeIdentityMatrix();                                       /* 单位化一个矩阵 */
 void ToBeZeroMatrix();                                            /* 将当前矩阵化为0矩阵 */
 void ContraryMatrix();                                             /* 求矩阵的逆 返回当前矩阵的逆矩阵 */ 
 long GetMatrixRank() const;                                    /* 求矩阵的秩 */
 long GetExtendMatrixRank(const Matrix& b) const; /* 求增广矩阵的秩 增光矩阵为 A|b */
 int IsEMRequalMR(const Matrix& b) const;               /* 判断增广矩阵的秩和原矩阵的秩是否一致 */
 Matrix GetVector(const long ln) const;                     /* 在矩阵中提取ln行作为一个矩阵 */ 
 Matrix GetColVector(const long col) const;              /* 在矩阵中提取col列作为一个矩阵 */
 Double GetModulus() const;                                    /* 求矩阵的范数(摸) */ 
 Double GetDotMetrix(const Matrix& matrix)const;   /* 返回两矩阵的点积 */
 int AddLine(const Matrix &vector);                           /* 向矩阵中添加1行 */
 int AddCol(const Matrix& verctor);                           /* 向矩阵中添加1列 */
 Matrix& operator *(const Matrix &genMatrix);         /* 两矩阵相乘 返回当前矩阵 */ 
 Matrix& operator +(const Matrix &genMatrix);         /* 两矩阵相加 返回当前矩阵 */ 
 Matrix& operator *(const Double& gen);                 /* 矩阵的数乘 返回当前矩阵 */
 Matrix& operator /(const Double& gen);                  /* 矩阵除以一个数 返回当前矩阵 */
 Matrix& operator =(const Matrix &matrix);               /* 赋值运算,创建一个与参数一样的矩阵 返回当前矩阵 */
 Matrix& operator -(const Matrix &genMatrix);          /* 两矩阵相减 返回当前矩阵 */
 friend ostream& operator<<(ostream& os, const Matrix& matrix);               /* 输出矩阵信息 */
 Matrix& PosMultiply(const long ln, const long col, const Double& gen);        /* 第ln,col位置的元素乘以gen */
 Matrix& PosSetValue(const long ln, const long col, const Double& value);   /* 替ln,col位置赋值 */
 Matrix& DeleteElement(const long ln, const long col);                                  /* 删除ln,col位置的元素 */
 Matrix& AddElement(const long ln, const long col, const Double& value);    /* 增加ln和col位置的元素 */
 void OutFile(char* fileName)const;                             /* 将矩阵的信息输出到fileName文件中 */
 static Matrix GetE(const long total_ln, const long j);  /* 得到一个total_ln行,第j行为1其他为0的矩阵 */
 static Matrix GetE(const long ln);                                /* 得到一个全为1的矩阵 */
};

#endif //_MATRIX_


/*
 C++ 注意事项:  用new产生堆对象
 1、绝不要返回一个局部栈对象的引用
 2、不要轻易返回一个局部堆对象的引用,除非调用函数晓得什么时候该销毁该局部堆对象
 3、参数传递可以以引用的形式传递,可以提高程序的运行速度,节省了构造对象的过程。
*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值