📄 matrix.cpp
字号:
{
++nRow;
}
// 根据文本文件的数据的行数设置对象(矩阵)的行数
m_nRow = nRow;
SetMatrixRowNumber(m_nRow);
// 重新定位当前文件指针到文件开头
dataFile.SeekToBegin ();
dataFile.ReadString (strData);
strData.TrimLeft ();
strData.TrimRight ();
TCHAR SPACE_CHARACTER = ' ';
// 用来记录文本文件中一行有多少列?
unsigned int nCol = 0;
// 空格符在字符串中的位置索引
int nIndex = 0;
do
{
nIndex = strData.Find (SPACE_CHARACTER);
++nCol;
strData = strData.Right (strData.GetLength () - nIndex -1);
// 去掉多余的空格
strData.TrimLeft ();
}while(nIndex != -1);
// 根据文本文件的数据的列数设置对象(矩阵)的列数
m_nCol = nCol;
SetMatrixColNumber(m_nCol);
// End of Getting the Rows and Cols of the Text File
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// Step 2: 根据文本文件中的数据对矩阵赋值,并检测每行的列数是否和第一行的
// 列数相等,不相等提示出错信息
//
// 重新定位当前文件指针到文件开头
dataFile.SeekToBegin ();
// 对矩阵中的元素装入文本文件的数据
for(unsigned int i=0; i < m_nRow; i++)
{
dataFile.ReadString (strData);
strData.TrimLeft ();
strData.TrimRight ();
// 验证每行的列数是否与第一行的列数相等
unsigned int nVerifyColNum = 0;
do
{
nIndex = strData.Find (SPACE_CHARACTER);
++nVerifyColNum;
if(nIndex != -1)
{
// 提取字符串的子字符串,即提取一个double型实数数据
CString strDoubleNumber = strData.Left (nIndex);
// 将字符串转换为double型实数
double RealNumber = atof(strDoubleNumber);
m_pTMatrix [i][nVerifyColNum - 1] = RealNumber;
//int nTempNumber = strData.GetLength ();
strData = strData.Right (strData.GetLength () - nIndex -1);
// 去掉多余的空格
strData.TrimLeft ();
}
else
{
double RealNumber = atof(strData);
m_pTMatrix [i][nVerifyColNum - 1] = RealNumber;
}
}while(nIndex != -1);
if(nVerifyColNum != m_nCol)
{
CString strRowNumber;
strRowNumber.Format("%d",i + 1);
CString strColNumber;
strColNumber.Format("%d",m_nCol);
CString strVerifyColNumber;
strVerifyColNumber.Format("%d",nVerifyColNum);
CString strMessage = CString(TEXT("文本文件第")) + strRowNumber + CString(TEXT("行一共有")) + strVerifyColNumber + CString(TEXT("列,与第一行中的列数")) + strColNumber + CString(TEXT("不相等!"));
LPCTSTR lpszText = "";
lpszText = (LPCTSTR)strMessage;
::AfxMessageBox (lpszText,MB_OK | MB_ICONERROR);
dataFile.Close ();
return false;
}
}
dataFile.Close ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// 对矩阵中的元素进行一次操作:
// 使矩阵变为单位阵
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::Eye()
//DEL {
//DEL // Verify whether the rows is equal to the columns or not
//DEL if(m_nRow != m_nCol)
//DEL {
//DEL ::AfxMessageBox (TEXT("此矩阵的行列数不相等!不能转变为单位阵!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL for(unsigned int i=0; i < m_nRow; i++)
//DEL {
//DEL for(unsigned int j=0; j < m_nCol; j++)
//DEL {
//DEL if(i == j)
//DEL {
//DEL m_pTMatrix [i][j] = 1;
//DEL }
//DEL else
//DEL {
//DEL m_pTMatrix [i][j] = 0;
//DEL }
//DEL }
//DEL
//DEL }
//DEL
//DEL
//DEL }
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被拷贝的数据源
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置,从0开始
// Purpose:
// This function will copy all the data of the cMatrix
// Notes:
// 此对象必须是列向量!!!
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::GetMatrixData(CMatrix& cMatrix, unsigned int nIndex)
//DEL {
//DEL if(m_nCol != 1)
//DEL {
//DEL ::AfxMessageBox (TEXT("拷贝的矩阵不是列向量!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL if((m_nRow - nIndex) < (cMatrix.m_nRow * cMatrix.m_nCol))
//DEL {
//DEL ::AfxMessageBox (TEXT("拷贝矩阵的空间容量不足!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL for(unsigned int i=0; i < cMatrix.m_nRow; i++)
//DEL {
//DEL for(unsigned int j=0; j < cMatrix.m_nCol; j++)
//DEL {
//DEL m_pTMatrix [nIndex + i * cMatrix.m_nCol + j][0] = cMatrix.m_pTMatrix [i][j];
//DEL }
//DEL
//DEL }
//DEL
//DEL }
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被填充的矩阵
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置
// Purpose:
// This function will copy part of the object data into cMatrix
// Notes:
// The object must be column vector!!!
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::SetMatrixData(CMatrix& cMatrix, unsigned int nIndex)
//DEL {
//DEL // Verify whether the colunm number is 1
//DEL if(m_nCol != 1)
//DEL {
//DEL ::AfxMessageBox (TEXT("本矩阵对象不是列向量,不满足条件!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL // Verify whether the number of the object element is enough to be copyed
//DEL if((m_nRow - nIndex) < (cMatrix.m_nRow * cMatrix.m_nCol))
//DEL {
//DEL ::AfxMessageBox (TEXT("对象中的元素数量不足!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL
//DEL for(unsigned int i=0; i < cMatrix.m_nRow; i++)
//DEL {
//DEL for(unsigned int j=0; j < cMatrix.m_nCol; j++)
//DEL {
//DEL cMatrix.m_pTMatrix [i][j] = m_pTMatrix [nIndex + i * cMatrix.m_nCol + j][0];
//DEL
//DEL // Using for debugging
//DEL //unsigned int nIndexNumber = nIndex + i * cMatrix.m_nRow + j;
//DEL //double nData = cMatrix.m_pTMatrix [i][j];
//DEL
//DEL }
//DEL }
//DEL
//DEL }
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被填充的矩阵
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置
// unsigned int nRow: 被填充的数据在被填充对象中的行索引
// Purpose:
// This function will copy part of the object data to fill the special
// row of the cMatrix
// Notes:
// The object must be column vector!!!
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::SetMatrixRowData(CMatrix& cMatrix, unsigned int nIndex, unsigned int nRow)
//DEL {
//DEL // Verify whether the column number is 1
//DEL if(m_nCol != 1)
//DEL {
//DEL ::AfxMessageBox (TEXT("本矩阵对象不是列向量,不满足条件!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL // Verify whether the number of the object element is enough to be copyed
//DEL if((m_nRow - nIndex) < cMatrix.m_nCol )
//DEL {
//DEL ::AfxMessageBox (TEXT("对象的元素数量不足!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL for(unsigned int i=0; i < cMatrix.m_nCol; i++)
//DEL {
//DEL cMatrix.m_pTMatrix [nRow][i] = m_pTMatrix [nIndex + i][(unsigned int)0];
//DEL }
//DEL
//DEL }
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被拷贝的数据源
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置
// unsigned int nRow: 被拷贝的数据在被拷贝对象中的行索引(从0开始)
// Purpose:
// This function will copy all the data of the cMatrix
// Notes:
// 此对象必须是列向量!!!
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::GetMatrixRowData(CMatrix& cMatrix, unsigned int nIndex, unsigned int nRow)
//DEL {
//DEL if(m_nCol != 1)
//DEL {
//DEL ::AfxMessageBox (TEXT("拷贝的矩阵不是列向量!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL if((m_nRow - nIndex) < cMatrix.m_nCol)
//DEL {
//DEL ::AfxMessageBox (TEXT("拷贝矩阵的空间容量不足!"),MB_OK | MB_ICONERROR);
//DEL return;
//DEL }
//DEL
//DEL for(unsigned int i=0; i < cMatrix.m_nCol; i++)
//DEL {
//DEL m_pTMatrix [nIndex + i][(unsigned int)0] = cMatrix.m_pTMatrix [nRow][i];
//DEL }
//DEL
//DEL }
void CMatrix::SetMatrixRowNumber(unsigned int nRow)
{
m_nRow = nRow;
m_pTMatrix.resize (m_nRow);
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix[i].resize (m_nCol);
m_pTMatrix[i][j] = (double) 0;
}
}
}
void CMatrix::SetMatrixColNumber(unsigned int nCol)
{
m_nCol = nCol;
m_pTMatrix.resize (m_nRow);
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix[i].resize (m_nCol);
m_pTMatrix[i][j] = (double) 0;
}
}
}
/////////////////////////////////////////////////////////////////////////
// 设置矩阵的行列数
void CMatrix::SetMatrixRowAndCol(unsigned int nRow,unsigned int nCol)
{
m_nRow = nRow;
m_nCol = nCol;
// 分配内存
m_pTMatrix.resize (m_nRow);
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix[i].resize (m_nCol);
m_pTMatrix[i][j] = (double) 0;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Initialize()
// 矩阵初始化函数,矩阵的行列数目被初始化为零,矩阵中的元素全部初始化为零
/////////////////////////////////////////////////////////////////////////////
//DEL void CMatrix::Initialize()
//DEL {
//DEL m_nRow = 0;
//DEL m_nCol = 0;
//DEL
//DEL m_pTMatrix.resize (m_nRow);
//DEL for(unsigned int i=0; i < m_nRow; i++)
//DEL {
//DEL for(unsigned int j=0; j < m_nCol; j++)
//DEL {
//DEL m_pTMatrix[i].resize (m_nCol);
//DEL m_pTMatrix[i][j] = (double) 0;
//DEL }
//DEL }
//DEL
//DEL }
/////////////////////////////////////////////////////////////////////////////
// InitializeZero()
// 矩阵初始化函数,矩阵的行列数目被初始化为零,矩阵中的元素全部初始化为零
/////////////////////////////////////////////////////////////////////////////
void CMatrix::InitializeZero()
{
m_nRow = 0;
m_nCol = 0;
m_pTMatrix.resize (m_nRow);
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix[i].resize (m_nCol);
m_pTMatrix[i][j] = (double) 0;
}
}
}
/////////////////////////////////////////////////////////////////////////////
// RandomInitialize()
// 将矩阵中的元素随机初始化函数,元素的值在(-1,1)之间的小数
/////////////////////////////////////////////////////////////////////////////
void CMatrix::RandomInitialize ()
{
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix [i][j] = (double)(rand() - (0.5*RAND_MAX)) / (0.5*RAND_MAX);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 拷贝矩阵的子矩阵元素到另外一个矩阵中
// Parameter:
// [out] cMatrix ----> 矩阵的子矩阵返回的结果
// [in] nStartX ----> 子矩阵在矩阵中的起始坐标,对应行,索引从1开始
// [in] nStartY ----> 子矩阵在矩阵中的起始坐标,对应列,索引从1开始
/////////////////////////////////////////////////////////////////////////////
void CMatrix::CopySubMatrix(CMatrix& cMatrix,unsigned int nStartX,unsigned int nStartY)
{
if((m_nRow < cMatrix.m_nRow + nStartY ) | (m_nCol < cMatrix.m_nCol + nStartX))
{
::AfxMessageBox (TEXT("被拷贝的矩阵维数小于要拷贝的矩阵所需要的维数!"),MB_OK | MB_ICONERROR);
return;
}
for(unsigned int i=0; i < cMatrix.m_nRow; i++)
{
for(unsigned int j=0; j < cMatrix.m_nCol; j++)
{
cMatrix.m_pTMatrix [i][j] = m_pTMatrix [nStartY + i][nStartX + j];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Copy Matrix
// Parameter:
// [in] cMatrix ----> 被拷贝的矩阵
/////////////////////////////////////////////////////////////////////////////
void CMatrix::CopyMatrix(CMatrix& cMatrix)
{
m_nRow = cMatrix.m_nRow ;
m_nCol = cMatrix.m_nCol ;
m_pTMatrix = cMatrix.m_pTMatrix ;
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix [i][j] = cMatrix.m_pTMatrix [i][j];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 对矩阵进列拓展
// 实现功能:
// 对矩阵的列数进行拓展,nTimes是每列拓展的次数
/////////////////////////////////////////////////////////////////////////////
void CMatrix::nncpyi(CMatrix &cMatrix, unsigned int nTimes)
{
m_nRow = cMatrix.m_nRow ;
m_nCol = cMatrix.m_nCol * nTimes;
// 根据空间分配内存
m_pTMatrix.resize (m_nRow);
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
m_pTMatrix[i].resize (m_nCol);
m_pTMatrix[i][j] = (double) 0;
}
}
// 赋值
for(i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < cMatrix.m_nCol; j++)
{
for(unsigned int k=0; k < nTimes; k++)
{
m_pTMatrix [i][j * nTimes + k] = cMatrix.m_pTMatrix [i][j];
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 对矩阵中所有的元素进行一次非线性变换:
// 变换后的值y与变换前的值的关系是:
// y = f(x) = 1 / (1 + exp(-x)) ( 0 < f(x) < 1)
//
/////////////////////////////////////////////////////////////////////////////
CMatrix CMatrix::Sigmoid()
{
CMatrix cMatrix = *this;
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
cMatrix.m_pTMatrix [i][j] = 1 / (1 + exp(-m_pTMatrix [i][j]));
}
}
return cMatrix;
}
/////////////////////////////////////////////////////////////////////////////
// 实现对点乘操作符的重载
/////////////////////////////////////////////////////////////////////////////
CMatrix CMatrix::operator / (CMatrix& cMatrixB)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -