⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 h12.inl

📁 C环境下矩阵运算类,包含矩阵的基本操作,另有其范数,均值等特殊函数
💻 INL
📖 第 1 页 / 共 5 页
字号:
////常用的矩阵运算类CMatrix的C++实现matrix.cpp(1)
////常用的矩阵运算类CMatrix的C++实现
 
//////////////////////////////////////////////////////////////////////
// Matrix.cpp
//
// 操作矩阵的类 CMatrix 的实现文件
//
//////////////////////////////////////////////////////////////////////
#include "Matrix.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
// 基本构造函数
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix()
{
 m_nNumColumns = 1;
 m_nNumRows = 1;
 m_pData = NULL;
 bool bSuccess = Init(m_nNumRows, m_nNumColumns);
 assert(bSuccess);
}
//////////////////////////////////////////////////////////////////////
// 指定行列构造函数
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2. int nCols - 指定的矩阵列数
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix(int nRows, int nCols)
{
 m_nNumRows = nRows;
 m_nNumColumns = nCols;
 m_pData = NULL;
 bool bSuccess = Init(m_nNumRows, m_nNumColumns);
 assert(bSuccess);
}
//////////////////////////////////////////////////////////////////////
// 指定值构造函数
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2. int nCols - 指定的矩阵列数
// 3. double value[] - 一维数组,长度为nRows*nCols,存储矩阵各元素的值
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix(int nRows, int nCols, double value[])
{
 m_nNumRows = nRows;
 m_nNumColumns = nCols;
 m_pData = NULL;
 bool bSuccess = Init(m_nNumRows, m_nNumColumns);
 assert(bSuccess);
 SetData(value);
}
//////////////////////////////////////////////////////////////////////
// 方阵构造函数
//
// 参数:
// 1. int nSize - 方阵行列数
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix(int nSize)
{
 m_nNumRows = nSize;
 m_nNumColumns = nSize;
 m_pData = NULL;
 bool bSuccess = Init(nSize, nSize);
 assert (bSuccess);
}
//////////////////////////////////////////////////////////////////////
// 方阵构造函数
//
// 参数:
// 1. int nSize - 方阵行列数
// 2. double value[] - 一维数组,长度为nRows*nRows,存储方阵各元素的值
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix(int nSize, double value[])
{
 m_nNumRows = nSize;
 m_nNumColumns = nSize;
 m_pData = NULL;
 bool bSuccess = Init(nSize, nSize);
 assert (bSuccess);
 SetData(value);
}
//////////////////////////////////////////////////////////////////////
// 拷贝构造函数
//
// 参数:
// 1. const CMatrix& other - 源矩阵
//////////////////////////////////////////////////////////////////////
CMatrix::CMatrix(const CMatrix& other)
{
 m_nNumColumns = other.GetNumColumns();
 m_nNumRows = other.GetNumRows();
 m_pData = NULL;
 bool bSuccess = Init(m_nNumRows, m_nNumColumns);
 assert(bSuccess);
 // copy the pointer
 memcpy(m_pData, other.m_pData, sizeof(double)*m_nNumColumns*m_nNumRows);
}
//////////////////////////////////////////////////////////////////////
// 析构函数
//////////////////////////////////////////////////////////////////////
CMatrix::~CMatrix()
{
 if (m_pData)
 {
  m_pData = NULL;
  delete[] m_pData;
 }
}
//////////////////////////////////////////////////////////////////////
// 初始化函数
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2. int nCols - 指定的矩阵列数
//
// 返回值:bool 型,初始化是否成功
//////////////////////////////////////////////////////////////////////
bool CMatrix::Init(int nRows, int nCols)
{
 if (m_pData)
 {
  delete[] m_pData;
  m_pData = NULL;
 }
 m_nNumRows = nRows;
 m_nNumColumns = nCols;
 int nSize = nCols*nRows;
 if (nSize < 0)
  return false;
 // 分配内存
 m_pData = new double[nSize];
 
 if (m_pData == NULL)
  return false;     // 内存分配失败
// if (IsBadReadPtr(m_pData, sizeof(double) * nSize))
//  return false;
 // 将各元素值置0
 memset(m_pData, 0, sizeof(double) * nSize);
 return true;
}
///////////////.///////////////////////////////////////////////////////
// 将方阵初始化为单位矩阵
//
// 参数:
// 1. int nSize - 方阵行列数
//
// 返回值:bool 型,初始化是否成功
//////////////////////////////////////////////////////////////////////
bool CMatrix::MakeUnitMatrix(int nSize)
{
 if (! Init(nSize, nSize))
  return false;
 for (int i=0; i<nSize; ++i)
  for (int j=0; j<nSize; ++j)
   if (i == j)
    SetElement(i, j, 1);
 return true;
}
//////////////////////////////////////////////////////////////////////
// 将字符串转化为矩阵的值
//
// 参数:
// 1. CString s - 数字和分隔符构成的字符串
// 2. const CString& sDelim - 数字之间的分隔符,默认为空格
// 3. bool bLineBreak - 行与行之间是否有回车换行符,默认为真(有换行符)
//         当该参数为false时,所有元素值都在一行中输入,字符串的第一个
//         数值应为矩阵的行数,第二个数值应为矩阵的列数
//
// 返回值:bool 型,转换是否成功
//////////////////////////////////////////////////////////////////////
//bool CMatrix::FromString(CString s, const CString& sDelim /*= " "*/, bool bLineBreak /*= true*/)
/*{
 if (s.IsEmpty())
  return false;
 // 分行处理
 if (bLineBreak)
 {
  CTokenizer tk(s, "\r\n");
  CStringList ListRow;
  CString sRow;
  while (tk.Next(sRow))
  {
   sRow.TrimLeft();
   sRow.TrimRight();
   if (sRow.IsEmpty())
    break;
   ListRow.AddTail(sRow);
  }
  // 行数
  m_nNumRows = ListRow.GetCount();
  sRow = ListRow.GetHead();
  CTokenizer tkRow(sRow, sDelim);
  CString sElement;
  // 列数
  m_nNumColumns = 0;
  while (tkRow.Next(sElement))
  {
   m_nNumColumns++;
  }
  // 初始化矩阵
  if (! Init(m_nNumRows, m_nNumColumns))
   return false;
  // 设置值
  POSITION pos = ListRow.GetHeadPosition();
  for (int i=0; i<m_nNumRows; i++)
  {
   sRow = ListRow.GetNext(pos);
   int j = 0;
   CTokenizer tkRow(sRow, sDelim);
   while (tkRow.Next(sElement))
   {
    sElement.TrimLeft();
    sElement.TrimRight();
    double v = atof(sElement);
    SetElement(i, j++, v);
   }
  }
  return true;
 }
 
 // 不分行(单行)处理
 CTokenizer tk(s, sDelim);
 CString sElement;
 
 // 行数
 tk.Next(sElement);
 sElement.TrimLeft();
 sElement.TrimRight();
 m_nNumRows = atoi(sElement);
 // 列数
 tk.Next(sElement);
 sElement.TrimLeft();
 sElement.TrimRight();
 m_nNumColumns = atoi(sElement);
 // 初始化矩阵
 if (! Init(m_nNumRows, m_nNumColumns))
  return false;
 // 设置值
 int i = 0, j = 0;
 while (tk.Next(sElement))
 {
  sElement.TrimLeft();
  sElement.TrimRight();
  double v = atof(sElement);
  SetElement(i, j++, v);
  if (j == m_nNumColumns)
  {
   j = 0;
   i++;
   if (i == m_nNumRows)
    break;
  }
 }
 return true;
}
*/
//////////////////////////////////////////////////////////////////////
// 将矩阵各元素的值转化为字符串
//
// 参数:
// 1. const CString& sDelim - 数字之间的分隔符,默认为空格
// 2 bool bLineBreak - 行与行之间是否有回车换行符,默认为真(有换行符)
//
// 返回值:CString 型,转换得到的字符串
//////////////////////////////////////////////////////////////////////
//CString CMatrix::ToString(const CString& sDelim /*= " "*/, bool bLineBreak /*= true*/) const
/*{
 CString s="";
 for (int i=0; i<m_nNumRows; ++i)
 {
  for (int j=0; j<m_nNumColumns; ++j)
  {
   CString ss;
   ss.Format("%f", GetElement(i, j));
   s += ss;
   if (bLineBreak)
   {
    if (j != m_nNumColumns-1)
     s += sDelim;
   }
   else
   {
    if (i != m_nNumRows-1 || j != m_nNumColumns-1)
     s += sDelim;
   }
  }
  if (bLineBreak)
   if (i != m_nNumRows-1)
    s += "\r\n";
 }
 return s;
}*/
//////////////////////////////////////////////////////////////////////
// 将矩阵指定行中各元素的值转化为字符串
//
// 参数:
// 1. int nRow - 指定的矩阵行,nRow = 0表示第一行
// 2. const CString& sDelim - 数字之间的分隔符,默认为空格
//
// 返回值:CString 型,转换得到的字符串
//////////////////////////////////////////////////////////////////////
//CString CMatrix::RowToString(int nRow, const CString& sDelim /*= " "*/) const
/*{
 CString s = "";
 if (nRow >= m_nNumRows)
  return s;
 for (int j=0; j<m_nNumColumns; ++j)
 {
  CString ss;
  ss.Format("%f", GetElement(nRow, j));
  s += ss;
  if (j != m_nNumColumns-1)
   s += sDelim;
 }
 return s;
}*/
//////////////////////////////////////////////////////////////////////
// 将矩阵指定列中各元素的值转化为字符串
//
// 参数:
// 1. int nCol - 指定的矩阵行,nCol = 0表示第一列
// 2. const CString& sDelim - 数字之间的分隔符,默认为空格
//
// 返回值:CString 型,转换得到的字符串
//////////////////////////////////////////////////////////////////////
//CString CMatrix::ColToString(int nCol, const CString& sDelim /*= " "*/) const
/*{
 CString s = "";
 if (nCol >= m_nNumColumns)
  return s;
 for (int i=0; i<m_nNumRows; ++i)
 {
  CString ss;
  ss.Format("%f", GetElement(i, nCol));
  s += ss;
  if (i != m_nNumRows-1)
   s += sDelim;
 }
 return s;
}*/
//////////////////////////////////////////////////////////////////////
// 设置矩阵各元素的值
//
// 参数:
// 1. double value[] - 一维数组,长度为m_nNumColumns*m_nNumRows,存储
//                     矩阵各元素的值
//
// 返回值:无
//////////////////////////////////////////////////////////////////////
void CMatrix::SetData(double value[])
{
 // empty the memory
 memset(m_pData, 0, sizeof(double) * m_nNumColumns*m_nNumRows);
 // copy data
 memcpy(m_pData, value, sizeof(double)*m_nNumColumns*m_nNumRows);
}
//////////////////////////////////////////////////////////////////////
// 设置指定元素的值
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2. int nCols - 指定的矩阵列数
// 3. double value - 指定元素的值
//
// 返回值:bool 型,说明设置是否成功
//////////////////////////////////////////////////////////////////////
bool CMatrix::SetElement(int nRow, int nCol, double value)
{
 if (nCol < 0 || nCol >= m_nNumColumns || nRow < 0 || nRow >= m_nNumRows)
  return false;      // array bounds error
 if (m_pData == NULL)
  return false;       // bad pointer error
 
 m_pData[nCol + nRow * m_nNumColumns] = value;
 return true;
}
//////////////////////////////////////////////////////////////////////
// 设置指定元素的值
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2. int nCols - 指定的矩阵列数
//
// 返回值:double 型,指定元素的值
//////////////////////////////////////////////////////////////////////
double CMatrix::GetElement(int nRow, int nCol) const
{
 assert(nCol >= 0 && nCol < m_nNumColumns && nRow >= 0 && nRow < m_nNumRows); // array bounds error
 assert(m_pData);       // bad pointer error
 return m_pData[nCol + nRow * m_nNumColumns] ;
}
//////////////////////////////////////////////////////////////////////
// 获取矩阵的列数
//
// 参数:无
//
// 返回值:int 型,矩阵的列数
//////////////////////////////////////////////////////////////////////
int CMatrix::GetNumColumns() const
{
 return m_nNumColumns;
}
//////////////////////////////////////////////////////////////////////
// 获取矩阵的行数
//
// 参数:无
//
// 返回值:int 型,矩阵的行数
//////////////////////////////////////////////////////////////////////
int CMatrix::GetNumRows() const
{
 return m_nNumRows;
}
//////////////////////////////////////////////////////////////////////
// 获取矩阵的数据
//
// 参数:无
//
// 返回值:double型指针,指向矩阵各元素的数据缓冲区
//////////////////////////////////////////////////////////////////////
double* CMatrix::GetData() const
{
 return m_pData;
}
//////////////////////////////////////////////////////////////////////
// 获取指定行的向量
//
// 参数:
// 1. int nRows - 指定的矩阵行数
// 2.  double* pVector - 指向向量中各元素的缓冲区
//
// 返回值:int 型,向量中元素的个数,即矩阵的列数
//////////////////////////////////////////////////////////////////////
int CMatrix::GetRowVector(int nRow, double* pVector) const
{
 if (pVector == NULL)
  delete pVector;
 pVector = new double[m_nNumColumns];
 assert(pVector != NULL);
 for (int j=0; j<m_nNumColumns; ++j)
  pVector[j] = GetElement(nRow, j);
 return m_nNumColumns;
}
//////////////////////////////////////////////////////////////////////
// 获取指定列的向量
//
// 参数:
// 1. int nCols - 指定的矩阵列数
// 2.  double* pVector - 指向向量中各元素的缓冲区
//
// 返回值:int 型,向量中元素的个数,即矩阵的行数
//////////////////////////////////////////////////////////////////////
int CMatrix::GetColVector(int nCol, double* pVector) const
{
 if (pVector == NULL)
  delete pVector;
 pVector = new double[m_nNumRows];
 assert(pVector != NULL);
 for (int i=0; i<m_nNumRows; ++i)
  pVector[i] = GetElement(i, nCol);
 return m_nNumRows;
}
//////////////////////////////////////////////////////////////////////
// 重载运算符=,给矩阵赋值
//
// 参数:
// 1. const CMatrix& other - 用于给矩阵赋值的源矩阵
//
// 返回值:CMatrix型的引用,所引用的矩阵与other相等
//////////////////////////////////////////////////////////////////////
CMatrix& CMatrix::operator=(const CMatrix& other)
{
 if (&other != this)
 {
  bool bSuccess = Init(other.GetNumRows(), other.GetNumColumns());
  assert(bSuccess);
  // copy the pointer
  memcpy(m_pData, other.m_pData, sizeof(double)*m_nNumColumns*m_nNumRows);
 }
 // finally return a reference to ourselves
 return *this ;
}
//////////////////////////////////////////////////////////////////////
// 重载运算符==,判断矩阵是否相等
//
// 参数:
// 1. const CMatrix& other - 用于比较的矩阵
//
// 返回值:bool 型,两个矩阵相等则为true,否则为false
//////////////////////////////////////////////////////////////////////
bool CMatrix::operator==(const CMatrix& other) const
{
 // 首先检查行列数是否相等
 if (m_nNumColumns != other.GetNumColumns() || m_nNumRows != other.GetNumRows())
  return false;
 for (int i=0; i<m_nNumRows; ++i)
 {
  for (int j=0; j<m_nNumColumns; ++j)
  {
   if (GetElement(i, j) != other.GetElement(i, j))
    return false;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -