📄 matrix.cpp
字号:
// Matrix.cpp: 矩阵类的实现文件.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Matrix.h"
#include "CustomExcep.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
//构造函数
TMatrix::TMatrix()
{
m_nRows=0;
m_nCols=0;
pMatrix=NULL;
}
//分配[nRows][nCols]维矩阵,
//其元素值为位于(MinValue,MaxValue)区间的随机数.
TMatrix::TMatrix(int nRows,int nCols,double MinValue,double MaxValue)
{
double Slope,Offset;
double TempMax,TempMin;
m_nRows=nRows; //矩阵行数
m_nCols=nCols; //矩阵列数
if(m_nRows>0 && m_nCols>0)
{
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
srand((unsigned)time(NULL)); //利用时间设置随机数种子
TempMax=-pow(2,sizeof(int)*8-1);
TempMin=pow(2,sizeof(int)*8-1)-1;
for(int i=0; i<nRows; i++)
{
for(int j=0; j<nCols; j++)
{
pMatrix[i*m_nCols+j]=rand();
if(pMatrix[i*m_nCols+j]>TempMax) TempMax=pMatrix[i*m_nCols+j];
if(pMatrix[i*m_nCols+j]<TempMin) TempMin=pMatrix[i*m_nCols+j];
}
}
Slope=(MaxValue-MinValue)/(TempMax-TempMin); //斜率
Offset=MaxValue-TempMax*(MaxValue-MinValue)/(TempMax-TempMin); //截距
for(int i=0; i<nRows; i++)
for(int j=0; j<nCols; j++) pMatrix[i*m_nCols+j]=pMatrix[i*m_nCols+j]*Slope+Offset;
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
//分配[nRows][nCols]维矩阵,其元素值为Data。
TMatrix::TMatrix(int nRows,int nCols,double Data)
{
m_nRows=nRows; //矩阵行数
m_nCols=nCols; //矩阵列数
if(m_nRows>0 && m_nCols>0)
{
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
for(int i=0; i<nRows; i++)
for(int j=0; j<nCols; j++) pMatrix[i*m_nCols+j]=Data;
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
//利用指针p指向的数组来创建矩阵
TMatrix::TMatrix(double *p, int nRows, int nCols)
{
m_nRows=nRows; //矩阵行数
m_nCols=nCols; //矩阵列数
if(m_nRows>0 && m_nCols>0)
{
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
for(int i=0; i<nRows; i++)
for(int j=0; j<nCols; j++) pMatrix[i*m_nCols+j]=*(p+i*m_nCols+j);
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
//拷贝构造函数
TMatrix::TMatrix(TMatrix& OneMatrix)
{
m_nRows=OneMatrix.m_nRows;
m_nCols=OneMatrix.m_nCols;
//分配存贮空间
if(m_nRows>0 && m_nCols>0)
{
pMatrix=new double[m_nRows*m_nCols];
if(pMatrix!=0)
{
for(int i=0; i<m_nRows; i++)
for(int j=0; j<m_nCols; j++) pMatrix[i*m_nCols+j]=OneMatrix.pMatrix[i*m_nCols+j];
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
//析构函数
TMatrix::~TMatrix()
{
delete[] pMatrix;
pMatrix=NULL;
m_nRows=0;
m_nCols=0;
}
/*函 数;IntiAtRandom()。
参 数:int nRows,int nCols,double MinValue,double MaxValue。
功 能:若矩阵为空,则将其初始化为nRows*nCols的矩阵,
否则nRows、nCols均不起作用。
矩阵元素值均为位于(MinValue,MaxValue)区间的随机数。
返回值:无。 */
void TMatrix::InitAtRandom(int nRows,int nCols,double MinValue,double MaxValue)
{
double Slope,Offset;
double TempMax,TempMin;
if(nRows>0 && nCols>0)
{
delete[] pMatrix;
m_nRows=nRows; //矩阵行数
m_nCols=nCols; //矩阵列数
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
srand((unsigned)time(NULL)); //利用时间设置随机数种子
TempMax=-pow(2,sizeof(int)*8-1);
TempMin=pow(2,sizeof(int)*8-1)-1;
for(int i=0; i<nRows; i++)
{
for(int j=0; j<nCols; j++)
{
pMatrix[i*m_nCols+j]=rand();
if(pMatrix[i*m_nCols+j]>TempMax) TempMax=pMatrix[i*m_nCols+j];
if(pMatrix[i*m_nCols+j]<TempMin) TempMin=pMatrix[i*m_nCols+j];
}
}
Slope=(MaxValue-MinValue)/(TempMax-TempMin); //斜率
Offset=MaxValue-TempMax*(MaxValue-MinValue)/(TempMax-TempMin); //截距
for(int i=0; i<nRows; i++)
for(int j=0; j<nCols; j++) pMatrix[i*m_nCols+j]=pMatrix[i*m_nCols+j]*Slope+Offset;
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败!");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
/*函 数;IntiByData()。
参 数:int nRows,int nCols,double Data=0.0。
功 能:将矩阵初始化为nRows*nCols、数据均为Data的矩阵,
返回值:无。 */
void TMatrix::InitByData(int nRows,int nCols,double Data)
{
if(nRows>0 && nCols>0)
{
delete[] pMatrix;
m_nRows=nRows; //矩阵行数
m_nCols=nCols; //矩阵列数
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
for(int i=0; i<nRows; i++)
for(int j=0; j<nCols; j++) pMatrix[i*m_nCols+j]=Data;
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败!");
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
/*函 数;IntiByData()。
参 数:double Data=0.0。
功 能:将矩阵初始化为数据均为Data的矩阵,
返回值:无。 */
void TMatrix::InitByData(double Data)
{
if(pMatrix!=NULL)
{
for(int i=0; i<m_nRows; i++)
for(int j=0; j<m_nCols; j++) pMatrix[i*m_nCols+j]=Data;
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
/*函 数;IntiByArray()。
参 数:double *p,int nRows,int nCols。
功 能:将矩阵初始化为nRows*nCols的矩阵并用p指向的数组为其赋值;
返回值:无。 */
void TMatrix::InitByArray(double *p, int nRows, int nCols)
{
if(nRows>0 && nCols>0)
{
if(pMatrix!=0 && m_nRows==nRows && m_nCols==nCols)
{
for(int i=0;i<nRows;i++)
for(int j=0;j<nCols;j++) pMatrix[i*m_nCols+j]=*(p+i*m_nCols+j);
}
else
{
delete[] pMatrix;
m_nRows=nRows;
m_nCols=nCols;
pMatrix=new double[nRows*nCols];
if(pMatrix!=0)
{
for(int i=0;i<nRows;i++)
for(int j=0;j<nCols;j++) pMatrix[i*m_nCols+j]=*(p+i*m_nCols+j);
}
else
{
throw TCustomExcep(1,"矩阵对象分配失败!");
}
}
}
else
{
throw TCustomExcep(2,"矩阵初始化错误:其行数、列数必须大于0!");
}
}
/*函 数;IntiByMatrix()。
参 数:TMatrix& OneMatrix。
功 能:无论矩阵是否为空,将其初始化为OneMatrix;
返回值:无。 */
void TMatrix::InitByMatrix(TMatrix& OneMatrix)
{
if(pMatrix!=0 && m_nRows==OneMatrix.m_nRows && m_nCols==OneMatrix.m_nCols)
{
for(int i=0; i<m_nRows; i++)
for(int j=0; j<m_nCols; j++) pMatrix[i*m_nCols+j]=OneMatrix.pMatrix[i*m_nCols+j];
}
else
{
//分配存贮空间
delete[] pMatrix;
m_nRows=OneMatrix.m_nRows;
m_nCols=OneMatrix.m_nCols;
pMatrix=new double[m_nRows*m_nCols];
if(pMatrix!=0)
{
for(int i=0; i<m_nRows; i++)
for(int j=0; j<m_nCols; j++) pMatrix[i*m_nCols+j]=OneMatrix.pMatrix[i*m_nCols+j];
}
else
throw TCustomExcep(1,"矩阵对象分配失败!");
}
}
//获取矩阵的行数
int TMatrix::GetRows() const
{
return m_nRows;
}
//获取矩阵的列数
int TMatrix::GetCols() const
{
return m_nCols;
}
//获取矩阵数据区的指针
double *TMatrix::GetAddr() const
{
return pMatrix;
}
//获得矩阵中(i,j)元素
double TMatrix::GetValue(int i, int j) const
{
if(pMatrix!=NULL)
{
if(i>=m_nRows || j>=m_nCols || i<0 || j<0)
throw TCustomExcep(4,"对矩阵的操作超出了矩阵的范围!");
else
return pMatrix[i*m_nCols+j];
}
else
{
throw TCustomExcep(5,"试图对空矩阵进行操作!");
}
}
//给矩阵中(i,j)元素赋值
void TMatrix::SetValue(int i, int j,double value)
{
if(pMatrix!=NULL)
{
if(i>=m_nRows || j>=m_nCols || i<0 || j<0)
throw TCustomExcep(4,"对矩阵的操作超出了矩阵的范围!");
else
pMatrix[i*m_nCols+j]=value;
}
else
{
throw TCustomExcep(5,"试图对空矩阵进行操作!");
}
}
//获取矩阵中行向量
TMatrix TMatrix::GetRowVector(int i) const
{
TMatrix Temp;
if(pMatrix!=NULL)
{
if(i<m_nRows && i>=0) Temp.InitByArray(pMatrix+i*m_nCols,1,m_nCols);
else throw TCustomExcep(4,"对矩阵的操作超出了矩阵的范围!");
}
else
{
throw TCustomExcep(5,"试图对空矩阵进行操作!");
}
return Temp;
}
//获取矩阵中列向量
TMatrix TMatrix::GetColVector(int i) const
{
TMatrix Temp;
if(pMatrix!=NULL)
{
if(i<m_nCols && i>=0)
{
Temp.InitByData(m_nRows,1);
for(int j=0;j<m_nRows;j++) Temp.pMatrix[j]=pMatrix[j*m_nCols+i];
}
else
{
throw TCustomExcep(4,"对矩阵的操作超出了矩阵的范围!");
}
}
else
{
throw TCustomExcep(5,"试图对空矩阵进行操作!");
}
return Temp;
}
//释放矩阵占用的资源
void TMatrix::Release()
{
delete[] pMatrix;
pMatrix=NULL;
m_nRows=0;
m_nCols=0;
}
//重载赋值运算符
void TMatrix::operator=(const TMatrix OneMatrix)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -