?? matrix.cpp
字號:
{
break;
}
}
}
// Using for debugging
//CString strHideLayerValveValue = TEXT("TempHideLayerValveValue.txt");
//cMatrixHideLayerValveValue.SaveDataToFile (strHideLayerValveValue);
// 根據(jù)網(wǎng)絡(luò)參數(shù)設(shè)置隱含層到輸出層的權(quán)值矩陣的行列數(shù)
cMatrixHideToOutputWeightValue.SetMatrixRowAndCol (nOutputLayerNumber,nHideLayerNumber);
nIndex = 0;
nVerifyRowNum = 0;
// 讀取文件中的隱含層到輸出層的權(quán)值矩陣到相應(yīng)的矩陣對象中
while(dataFile.ReadString (strData))
{
strData.TrimLeft ();
strData.TrimRight ();
if((strData.Find (cFirstCharacter)) == 0)
{
continue;
}
else
{
// 裝入文本文件的數(shù)據(jù)到相對應(yīng)的矩陣中
strData.TrimLeft ();
strData.TrimRight ();
// 驗(yàn)證每行的列數(shù)是否與第一行的列數(shù)相等
unsigned int nVerifyColNum = 0;
do
{
nIndex = strData.Find (SPACE_CHARACTER);
++nVerifyColNum;
if(nIndex != -1)
{
// 提取字符串的子字符串,即提取一個(gè)double型實(shí)數(shù)數(shù)據(jù)
CString strDoubleNumber = strData.Left (nIndex);
// 將字符串轉(zhuǎn)換為double型實(shí)數(shù)
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("列,與第一行中的列數(shù)")) + 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);
// 根據(jù)網(wǎng)絡(luò)參數(shù)設(shè)置輸出層的閥值矩陣的行列數(shù)
cMatrixOutputLayerValveValue.SetMatrixRowAndCol (nOutputLayerNumber,(unsigned int)1);
nIndex = 0;
nVerifyRowNum = 0;
// 讀取文件中的輸出層的閥值矩陣到相應(yīng)的矩陣對象中
while(dataFile.ReadString (strData))
{
strData.TrimLeft ();
strData.TrimRight ();
if((strData.Find (cFirstCharacter)) == 0)
{
continue;
}
else
{
// 裝入文本文件的數(shù)據(jù)到相對應(yīng)的矩陣中
strData.TrimLeft ();
strData.TrimRight ();
// 驗(yàn)證每行的列數(shù)是否與第一行的列數(shù)相等
unsigned int nVerifyColNum = 0;
do
{
nIndex = strData.Find (SPACE_CHARACTER);
++nVerifyColNum;
if(nIndex != -1)
{
// 提取字符串的子字符串,即提取一個(gè)double型實(shí)數(shù)數(shù)據(jù)
CString strDoubleNumber = strData.Left (nIndex);
// 將字符串轉(zhuǎn)換為double型實(shí)數(shù)
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("列,與第一行中的列數(shù)")) + 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("不能創(chuàng)建文件!"),MB_OK | MB_ICONERROR);
dataFile.Close ();
return false;
}
dataFile.SeekToEnd ();
// 將對象(矩陣)中的數(shù)據(jù)寫進(jìn)文件
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;
}
/////////////////////////////////////////////////////////////////////////////
// 對矩陣中的元素進(jìn)行一次操作:
// 使矩陣變?yōu)閱挝魂?/////////////////////////////////////////////////////////////////////////////
void CMatrix::Eye()
{
// Verify whether the rows is equal to the columns or not
if(m_nRow != m_nCol)
{
::AfxMessageBox (TEXT("此矩陣的行列數(shù)不相等!不能轉(zhuǎn)變?yōu)閱挝魂?"),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: 被拷貝的數(shù)據(jù)源
// unsigned int nIndex: 被拷貝的數(shù)據(jù)在對象中的開始索引位置,從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: 被拷貝的數(shù)據(jù)在對象中的開始索引位置
// 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("對象中的元素?cái)?shù)量不足!"),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: 被拷貝的數(shù)據(jù)在對象中的開始索引位置
// unsigned int nRow: 被填充的數(shù)據(jù)在被填充對象中的行索引
// 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("對象的元素?cái)?shù)量不足!"),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: 被拷貝的數(shù)據(jù)源
// unsigned int nIndex: 被拷貝的數(shù)據(jù)在對象中的開始索引位置
// unsigned int nRow: 被拷貝的數(shù)據(jù)在被拷貝對象中的行索引(從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;
}
}
}
/////////////////////////////////////////////////////////////////////////
// 設(shè)置矩陣的行列數(shù)
void CMatrix::SetMatrixRowAndCol(unsigned int nRow,unsigned int nCol)
{
m_nRow = nRow;
m_nCol = nCol;
// 分配內(nèi)存
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()
// 矩陣初始化函數(shù),矩陣的行列數(shù)目被初始化為零,矩陣中的元素全部初始化為零
/////////////////////////////////////////////////////////////////////////////
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;
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -