📄 matrix.cpp
字号:
{
break;
}
}
}
// Using for debugging
//CString strHideLayerValveValue = TEXT("TempHideLayerValveValue.txt");
//cMatrixHideLayerValveValue.SaveDataToFile (strHideLayerValveValue);
// 根据网络参数设置隐含层到输出层的权值矩阵的行列数
cMatrixHideToOutputWeightValue.SetMatrixRowAndCol (nOutputLayerNumber,nHideLayerNumber);
nIndex = 0;
nVerifyRowNum = 0;
// 读取文件中的隐含层到输出层的权值矩阵到相应的矩阵对象中
while(dataFile.ReadString (strData))
{
strData.TrimLeft ();
strData.TrimRight ();
if((strData.Find (cFirstCharacter)) == 0)
{
continue;
}
else
{
// 装入文本文件的数据到相对应的矩阵中
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);
cMatrixHideToOutputWeightValue.m_pTMatrix [nVerifyRowNum][nVerifyColNum - 1] = RealNumber;
strData = strData.Right (strData.GetLength () - nIndex -1);
// 去掉多余的空格
strData.TrimLeft ();
}
else
{
double RealNumber = atof(strData);
cMatrixHideToOutputWeightValue.m_pTMatrix [nVerifyRowNum][nVerifyColNum - 1] = RealNumber;
}
}while(nIndex != -1);
if(nVerifyColNum != cMatrixHideToOutputWeightValue.m_nCol)
{
CString strRowNumber;
strRowNumber.Format("%d",nVerifyRowNum + 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;
}
++nVerifyRowNum;
if( nVerifyRowNum == cMatrixHideToOutputWeightValue.m_nRow )
{
break;
}
}
}
// Using for debugging
//CString strHideToOutputWeightValue = TEXT("TempHideToOutputWeightValue.txt");
//cMatrixHideToOutputWeightValue.SaveDataToFile (strHideToOutputWeightValue);
// 根据网络参数设置输出层的阀值矩阵的行列数
cMatrixOutputLayerValveValue.SetMatrixRowAndCol (nOutputLayerNumber,(unsigned int)1);
nIndex = 0;
nVerifyRowNum = 0;
// 读取文件中的输出层的阀值矩阵到相应的矩阵对象中
while(dataFile.ReadString (strData))
{
strData.TrimLeft ();
strData.TrimRight ();
if((strData.Find (cFirstCharacter)) == 0)
{
continue;
}
else
{
// 装入文本文件的数据到相对应的矩阵中
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);
cMatrixOutputLayerValveValue.m_pTMatrix [nVerifyRowNum][nVerifyColNum - 1] = RealNumber;
strData = strData.Right (strData.GetLength () - nIndex -1);
// 去掉多余的空格
strData.TrimLeft ();
}
else
{
double RealNumber = atof(strData);
cMatrixOutputLayerValveValue.m_pTMatrix [nVerifyRowNum][nVerifyColNum - 1] = RealNumber;
}
}while(nIndex != -1);
if(nVerifyColNum != cMatrixOutputLayerValveValue.m_nCol)
{
CString strRowNumber;
strRowNumber.Format("%d",nVerifyRowNum + 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;
}
++nVerifyRowNum;
if( nVerifyRowNum == cMatrixOutputLayerValveValue.m_nRow )
{
break;
}
}
}
// Using for debugging
//CString strOutputLayerValveValue = TEXT("TempOutputLayerValveValue.txt");
//cMatrixOutputLayerValveValue.SaveDataToFile (strOutputLayerValveValue);
dataFile.Close ();
return TRUE;
}
bool CMatrix::SaveDataToFile (CString& strFileName)
{
CStdioFile dataFile;
LPCTSTR lpszFileName = "";
// CString convert to LPCTSTR
strFileName.TrimLeft ();
strFileName.TrimRight ();
lpszFileName = (LPCTSTR)strFileName;
if(!dataFile.Open (lpszFileName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText))
{
::AfxMessageBox(TEXT("不能创建文件!"),MB_OK | MB_ICONERROR);
dataFile.Close ();
return false;
}
dataFile.SeekToEnd ();
// 将对象(矩阵)中的数据写进文件
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
CString strRealNumber;
strRealNumber.Format ("%.16f ", m_pTMatrix [i][j]);
// Using for debugging
//double nReadNumber = m_pTMatrix [i][j];
char *pBuffer = new char[strRealNumber.GetLength()];
memcpy(pBuffer,strRealNumber,strRealNumber.GetLength());
dataFile.Write (pBuffer,strRealNumber.GetLength ());
}
if( i != m_nRow - 1)
{
//char ReturnNewline[] = "\r\n";
char ReturnNewline[] = "\n";
dataFile.Write (ReturnNewline, (sizeof(ReturnNewline) - 1)/sizeof(char));
}
}
dataFile.Close ();
return true;
}
/////////////////////////////////////////////////////////////////////////////
// 对矩阵中的元素进行一次操作:
// 使矩阵变为单位阵
/////////////////////////////////////////////////////////////////////////////
void CMatrix::Eye()
{
// Verify whether the rows is equal to the columns or not
if(m_nRow != m_nCol)
{
::AfxMessageBox (TEXT("此矩阵的行列数不相等!不能转变为单位阵!"),MB_OK | MB_ICONERROR);
return;
}
for(unsigned int i=0; i < m_nRow; i++)
{
for(unsigned int j=0; j < m_nCol; j++)
{
if(i == j)
{
m_pTMatrix [i][j] = 1;
}
else
{
m_pTMatrix [i][j] = 0;
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被拷贝的数据源
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置,从0开始
// Purpose:
// This function will copy all the data of the cMatrix
// Notes:
// 此对象必须是列向量!!!
/////////////////////////////////////////////////////////////////////////////
void CMatrix::GetMatrixData(CMatrix& cMatrix, unsigned int nIndex)
{
if(m_nCol != 1)
{
::AfxMessageBox (TEXT("拷贝的矩阵不是列向量!"),MB_OK | MB_ICONERROR);
return;
}
if((m_nRow - nIndex) < (cMatrix.m_nRow * cMatrix.m_nCol))
{
::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++)
{
m_pTMatrix [nIndex + i * cMatrix.m_nCol + j][0] = cMatrix.m_pTMatrix [i][j];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 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!!!
/////////////////////////////////////////////////////////////////////////////
void CMatrix::SetMatrixData(CMatrix& cMatrix, unsigned int nIndex)
{
// Verify whether the colunm number is 1
if(m_nCol != 1)
{
::AfxMessageBox (TEXT("本矩阵对象不是列向量,不满足条件!"),MB_OK | MB_ICONERROR);
return;
}
// Verify whether the number of the object element is enough to be copyed
if((m_nRow - nIndex) < (cMatrix.m_nRow * cMatrix.m_nCol))
{
::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 [nIndex + i * cMatrix.m_nCol + j][0];
// Using for debugging
//unsigned int nIndexNumber = nIndex + i * cMatrix.m_nRow + j;
//double nData = cMatrix.m_pTMatrix [i][j];
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 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!!!
/////////////////////////////////////////////////////////////////////////////
void CMatrix::SetMatrixRowData(CMatrix& cMatrix, unsigned int nIndex, unsigned int nRow)
{
// Verify whether the column number is 1
if(m_nCol != 1)
{
::AfxMessageBox (TEXT("本矩阵对象不是列向量,不满足条件!"),MB_OK | MB_ICONERROR);
return;
}
// Verify whether the number of the object element is enough to be copyed
if((m_nRow - nIndex) < cMatrix.m_nCol )
{
::AfxMessageBox (TEXT("对象的元素数量不足!"),MB_OK | MB_ICONERROR);
return;
}
for(unsigned int i=0; i < cMatrix.m_nCol; i++)
{
cMatrix.m_pTMatrix [nRow][i] = m_pTMatrix [nIndex + i][(unsigned int)0];
}
}
/////////////////////////////////////////////////////////////////////////////
// Parameter:
// CMatrix& cMatrix: 被拷贝的数据源
// unsigned int nIndex: 被拷贝的数据在对象中的开始索引位置
// unsigned int nRow: 被拷贝的数据在被拷贝对象中的行索引(从0开始)
// Purpose:
// This function will copy all the data of the cMatrix
// Notes:
// 此对象必须是列向量!!!
/////////////////////////////////////////////////////////////////////////////
void CMatrix::GetMatrixRowData(CMatrix& cMatrix, unsigned int nIndex, unsigned int nRow)
{
if(m_nCol != 1)
{
::AfxMessageBox (TEXT("拷贝的矩阵不是列向量!"),MB_OK | MB_ICONERROR);
return;
}
if((m_nRow - nIndex) < cMatrix.m_nCol)
{
::AfxMessageBox (TEXT("拷贝矩阵的空间容量不足!"),MB_OK | MB_ICONERROR);
return;
}
for(unsigned int i=0; i < cMatrix.m_nCol; i++)
{
m_pTMatrix [nIndex + i][(unsigned int)0] = cMatrix.m_pTMatrix [nRow][i];
}
}
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()
// 矩阵初始化函数,矩阵的行列数目被初始化为零,矩阵中的元素全部初始化为零
/////////////////////////////////////////////////////////////////////////////
void CMatrix::Initialize()
{
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -