📄 hmm.cpp
字号:
#include "hmm.h"
//#using <mscorlib.dll>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
using namespace std ;
/********************************the default construction and destruction functions****************************/
Hmm::Hmm(void)
{
cout<<"This is the construction function of class Hmm "<<endl ;
cout<<"initialize some variables in Hmm "<<endl ;
//initialize the variables used in Hmm
M = 0 ;
N = 0 ;
iPreT = 0 ;
A = NULL ;
B = NULL ;
alpha = NULL ;
beta = NULL ;
delta = NULL ;
gamma = NULL ;
}
Hmm::~Hmm(void)
{
cout<<"This is the destruction function of class Hmm "<<endl ;
//free all the memory of Hmm
cout<<"Now free all the memory of the matrix A and B "<<endl ;
if( A )
dFreeMatrix( A, 0, N-1, 0, N-1 ) ;
if( B )
dFreeMatrix( B, 0, N-1, 0, M-1 ) ;
cout<<"Now free all the memory of the matrix alpha and beta, delta and gamma"<<endl ;
if( alpha )
dFreeMatrix( alpha, 0, iPreT-1, 0, N-1 ) ;
if( beta )
dFreeMatrix( beta, 0, iPreT-1, 0, N-1 ) ;
if( delta )
dFreeMatrix( delta, 0, iPreT-1, 0, N-1 ) ;
if( gamma )
dFreeMatrix( gamma, 0, iPreT-1, 0, N-1 ) ;
}
/********************************the default construction and destruction functions end************************/
/***********************the adjusted construction and destruction functions****************************/
Hmm::Hmm( string strFileName )
{
cout<<"This is the construction function of class Hmm "<<endl ;
cout<<"initialize some variables in Hmm "<<endl ;
//initialize the variables used in Hmm
alpha = NULL ;
beta = NULL ;
delta = NULL ;
gamma = NULL ;
iPreT = 0 ;
ReadHmm( strFileName.c_str() ) ;
}
Hmm::Hmm( char* sFileName)
{
cout<<"This is the construction function of class Hmm "<<endl ;
cout<<"initialize some variables in Hmm "<<endl ;
//initialize the variables used in Hmm
alpha = NULL ;
beta = NULL ;
delta = NULL ;
gamma = NULL ;
iPreT = 0 ;
ReadHmm( sFileName ) ;
}
Hmm::Hmm( int NHmm, int MHmm, int iSeed )
{
cout<<"This is the construction function of class Hmm "<<endl ;
cout<<"allocate the memory needed for matrix A and B"<<endl ;
//allocate the memory for the matrix A
A = dMatrix( 0, NHmm-1, 0, NHmm-1 ) ;
//allocate the memory for the matrix B
B = dMatrix( 0, NHmm-1, 0, MHmm-1 ) ;
//allocate the memory for the initial probability vector
pi.resize( NHmm ) ;
cout<<"initialize some variables in Hmm "<<endl ;
N = NHmm ;
M = MHmm ;
iPreT = 0 ;
//initialize the variables used in Hmm
alpha = NULL ;
beta = NULL ;
delta = NULL ;
gamma = NULL ;
//initialize the Hmm with random value
InitHmm( iSeed ) ;
}
Hmm::Hmm( int NHmm, int MHmm, double** AHmm, double** BHmm, vector<double>& piHmm )
{
cout<<"This is the construction function of class Hmm "<<endl ;
cout<<"initialize some variables in Hmm "<<endl ;
N = NHmm ;
M = MHmm ;
iPreT = 0 ;
A = AHmm ;
B = BHmm ;
pi = piHmm ;
//initialize the variables used in Hmm
alpha = NULL ;
beta = NULL ;
delta = NULL ;
gamma = NULL ;
}
/***********************the adjusted construction and destruction functions****************************/
/*********************** Globle some service programmes needed by others******************************/
//the function output some messages and exit the programe when there is error allocating memory
//void Hmm::nrerror( string errStr )
void nrerror( string errStr )
{
cerr<<"Numerical Recipes run-time error..."<<endl ;
cerr<<errStr<<endl ;
cerr<<"...now exit the programme"<<endl ;
exit( EXIT_FAILURE ) ;
}
//some allocating memory function to 2-dimonsion array
//int** Hmm::iMatrix( int irLow, int irHigh, int icLow, int icHigh )
int** iMatrix( int irLow, int irHigh, int icLow, int icHigh )
{
int** p ;
p = new int* [irHigh-irLow+1] ;
if( p == NULL )
nrerror("allocate failure in fMatrix") ;
p -= irLow ;
for( int i=irLow ; i<irHigh+1 ; i++ )
{
p[i] = new int[icHigh-icLow+1] ;
if( p[i] == NULL )
nrerror("allocate failure in fMatrix") ;
p[i] -= icLow ;
}
return p ;
}
//float** Hmm::fMatrix( int irLow, int irHigh, int icLow, int icHigh )
float** fMatrix( int irLow, int irHigh, int icLow, int icHigh )
{
float** p ;
p = new float* [irHigh-irLow+1] ;
if( p == NULL )
nrerror("allocate failure in fMatrix") ;
p -= irLow ;
for( int i=irLow ; i<irHigh+1 ; i++ )
{
p[i] = new float[icHigh-icLow+1] ;
if( p[i] == NULL )
nrerror("allocate failure in fMatrix") ;
p[i] -= icLow ;
}
return p ;
}
//double** Hmm::dMatrix( int irLow, int irHigh, int icLow, int icHigh )
double** dMatrix( int irLow, int irHigh, int icLow, int icHigh )
{
double** p ;
p = new double* [irHigh-irLow+1] ;
if( p == NULL )
nrerror("allocate failure in fMatrix") ;
p -= irLow ;
for( int i=irLow ; i<irHigh+1 ; i++ )
{
p[i] = new double[icHigh-icLow+1] ;
if( p[i] == NULL )
nrerror("allocate failure in fMatrix") ;
p[i] -= icLow ;
}
return p ;
}
//void Hmm::iFreeMatrix( int** iMatrix, int irLow, int irHigh, int icLow, int icHigh )
void iFreeMatrix( int** iMatrix, int irLow, int irHigh, int icLow, int icHigh )
{
for( int i=irLow ; i<=irHigh ; i++ )
delete[] (iMatrix[i]+icLow) ;
delete[] (iMatrix+irLow) ;
}
//void Hmm::fFreeMatrix( float** fMatrix, int irLow, int irHigh, int icLow, int icHigh )
void fFreeMatrix( float** fMatrix, int irLow, int irHigh, int icLow, int icHigh )
{
for( int i=irLow ; i<=irHigh ; i++ )
delete[] (fMatrix[i]+icLow) ;
delete[] (fMatrix+irLow) ;
}
//void Hmm::dFreeMatrix( double** dMatrix, int irLow, int irHigh, int icLow, int icHigh )
void dFreeMatrix( double** dMatrix, int irLow, int irHigh, int icLow, int icHigh )
{
for( int i=irLow ; i<=irHigh ; i++ )
delete[] (dMatrix[i]+icLow) ;
delete[] (dMatrix+irLow) ;
}
/************************ Globle some service programmes needed by others end**************************/
/********************************some initialize function**********************************************/
void Hmm::InitHmm( int iSeed )
{
cout<<"Initialize the Hmm with random values"<<endl ;
//set the seed
srand( iSeed ) ;
//for normalize
double sum = 0.0 ;
//set the random value to pi
for( int i=0 ; i<N ; i++ )
{
pi[i] = rand()/RAND_MAX ;
sum += pi[i] ;
}
//normalize
for( i=0 ; i<N ; i++ )
pi[i] /= sum ;
//set the random value to the matrix A
for( i=0 ; i<N ; i++ )
{
sum = 0.0 ;
for( int j=0 ; j<N ; j++ )
{
A[i][j] = rand()/RAND_MAX ;
sum += A[i][j] ;
}
//normalize
for( j=0 ; j<N ; j++ )
A[i][j] /= sum ;
}
//set the random value to the matrix B
for( int j=0 ; j<N ; j++ )
{
sum = 0.0 ;
for( int k=0 ; k<M ; k++ )
{
B[j][k] = rand()/RAND_MAX ;
sum += B[j][k] ;
}
//normalize
for( k=0 ; k<M ; k++ )
B[j][k] /= sum ;
}
}
/********************************some initialize function end******************************************/
/********************************the input-output function*********************************************/
void Hmm::ReadHmm( string strFileName )
{
cout<<"Now read Hmm from file "<<strFileName<<endl ;
ifstream in ;
in.open( strFileName.c_str() ) ;
if( !in.is_open() )
{
cerr<<"Can not open the file of Hmm to read!"<<endl ;
exit( EXIT_FAILURE ) ;
}
//read the matrix A
cout<<"Now read the matrix A "<<endl ;
in>>N ;
//allocate memory first
A = dMatrix( 0 , N, 0, N ) ;
//initialize
for( int i=0 ; i<N ; i++ )
for( int j=0 ; j<N ; j++ )
A[i][j] = 0.0 ;
//read data from file
for( i=0 ; i<N ; i++ )
for( int j=0 ; j<N ; j++ )
in>>A[i][j] ;
//read the matrix B
cout<<"Now read the matrix B "<<endl ;
in>>M ;
//allocate memory first
B = dMatrix( 0, N, 0, M ) ;
//initialize
for( int j=0 ; j<N ; j++ )
for( int k=0 ; k<M ; k++ )
B[j][k] = 0.0 ;
//read data from data
for( j=0 ; j<N ; j++ )
for( int k=0 ; k<M ; k++ )
in>>B[j][k] ;
//read the initial probability vector pi
cout<<"Read vector pi"<<endl ;
double dTemp = 0.0 ;
for( i=0 ; i<N ;i++ )
{
in>>dTemp ;
pi.push_back( dTemp ) ;
}
in.close() ;
}
/*
void Hmm::ReadHmm( char* sFileName )
{
cout<<"Now read Hmm from file "<<sFileName<<endl ;
FILE* pFile ;
pFile = fopen( sFileName, "r" ) ;
if( pFile == NULL )
{
cerr<<"Can not open the file of Hmm to read"<<endl ;
exit( EXIT_FAILURE ) ;
}
//read the matrix A
cout<<"Now read the matrix A "<<endl ;
fscanf( pFile, "%d\n", &N ) ;
//allocate memory first
A = dMatrix( 0 , N, 0, N ) ;
//initialize
for( int i=0 ; i<N ; i++ )
for( int j=0 ; j<N ; j++ )
A[i][j] = 0.0 ;
//read data from file
for( i=0 ; i<N ; i++ )
{
for( int j=0 ; j<N ; j++ )
fscanf( pFile, "%f ", &A[i][j] ) ;
fscanf( pFile, "\n" ) ;
}
//read the matrix B
cout<<"Now read the matrix B "<<endl ;
fscanf( pFile, "%d\n", &M ) ;
//allocate memory first
B = dMatrix( 0, N, 0, M ) ;
//initialize
for( int j=0 ; j<N ; j++ )
for( int k=0 ; k<M ; k++ )
B[j][k] = 0.0 ;
//read data from data
for( j=0 ; j<N ; j++ )
{
for( int k=0 ; k<M ; k++ )
fscanf( pFile, "%f ", &B[j][k] ) ;
fscanf( pFile, "\n" ) ;
}
//read the initial probability vector pi
cout<<"Read vector pi"<<endl ;
double dTemp = 0.0 ;
for( i=0 ; i<N ;i++ )
{
fscanf( pFile, "%f ", &dTemp ) ;
pi.push_back( dTemp ) ;
}
fclose( pFile ) ;
}
*/
void Hmm::ReadHmm( char* sFileName )
{
cout<<"Now read Hmm from file "<<sFileName<<endl ;
ifstream in ;
in.open( sFileName ) ;
if( !in.is_open() )
{
cerr<<"Can not open the file of Hmm to read!"<<endl ;
exit( EXIT_FAILURE ) ;
}
//read the matrix A
cout<<"Now read the matrix A "<<endl ;
in>>N ;
//allocate memory first
A = dMatrix( 0 , N, 0, N ) ;
//initialize
for( int i=0 ; i<N ; i++ )
for( int j=0 ; j<N ; j++ )
A[i][j] = 0.0 ;
//read data from file
for( i=0 ; i<N ; i++ )
for( int j=0 ; j<N ; j++ )
in>>A[i][j] ;
//read the matrix B
cout<<"Now read the matrix B "<<endl ;
in>>M ;
//allocate memory first
B = dMatrix( 0, N, 0, M ) ;
//initialize
for( int j=0 ; j<N ; j++ )
for( int k=0 ; k<M ; k++ )
B[j][k] = 0.0 ;
//read data from data
for( j=0 ; j<N ; j++ )
for( int k=0 ; k<M ; k++ )
in>>B[j][k] ;
//read the initial probability vector pi
cout<<"Read vector pi"<<endl ;
double dTemp = 0.0 ;
for( i=0 ; i<N ;i++ )
{
in>>dTemp ;
pi.push_back( dTemp ) ;
}
in.close() ;
}
void Hmm::WriteHmm( string strFileName )
{
cout<<"Now write Hmm to file "<<strFileName<<endl ;
ofstream out ;
out.open( strFileName.c_str() ) ;
if( !out.is_open() )
{
cerr<<"Can not open the file of Hmm to write"<<endl ;
exit( EXIT_FAILURE ) ;
}
//write the matrix A
cout<<"Now output the matrix A "<<endl ;
out<<N<<endl ;
for( int i=0 ; i<N ; i++ )
{
for( int j=0 ; j<N ; j++ )
out<<A[i][j]<<" " ;
out<<endl ;
}
//write the matrix B
cout<<"Now output the matrix B "<<endl ;
out<<M<<endl ;
for( int j=0 ; j<N ; j++ )
{
for( int k=0 ; k<M ; k++ )
out<<B[j][k]<<" " ;
out<<endl ;
}
//write the initial probability vector pi
cout<<"Now output vector pi"<<endl ;
for( i=0 ; i<N ; i++ )
{
out<<pi[i]<<" " ;
}
out<<endl ;
out.close() ;
}
/*
void Hmm::WriteHmm( char* sFileName )
{
cout<<"Now write Hmm to file "<<sFileName<<endl ;
FILE* pFile ;
pFile = fopen( sFileName, "w" ) ;
if( pFile == NULL )
{
cerr<<"Can not open the file of Hmm to write"<<endl ;
exit( EXIT_FAILURE ) ;
}
//write the matrix A
cout<<"Now output the matrix A "<<endl ;
fprintf( pFile, "%d\n", N ) ;
for( int i=0 ; i<N ; i++ )
{
for( int j=0 ; j<N ; j++ )
fprintf( pFile, "%f ", A[i][j] ) ;
fprintf( pFile, "\n" ) ;
}
cout<<"Now output the matrix B "<<endl ;
fprintf( pFile, "%d\n", M ) ;
for( int j=0 ; j<N ; j++ )
{
for( int k=0 ; k<M ; k++ )
fprintf( pFile, "%f ", B[j][k] ) ;
fprintf( pFile, "\n" ) ;
}
cout<<"Now output the vector pi"<<endl ;
for( i=0 ; i<N ; i++ )
fprintf( pFile, "%f ", pi[i] ) ;
fprintf( pFile, "\n" ) ;
fclose( pFile ) ;
}
*/
void Hmm::WriteHmm( char* sFileName )
{
cout<<"Now write Hmm to file "<<sFileName<<endl ;
ofstream out ;
out.open( sFileName ) ;
if( !out.is_open() )
{
cerr<<"Can not open the file of Hmm to write"<<endl ;
exit( EXIT_FAILURE ) ;
}
//write the matrix A
cout<<"Now output the matrix A "<<endl ;
out<<N<<endl ;
for( int i=0 ; i<N ; i++ )
{
for( int j=0 ; j<N ; j++ )
out<<A[i][j]<<" " ;
out<<endl ;
}
//write the matrix B
cout<<"Now output the matrix B "<<endl ;
out<<M<<endl ;
for( int j=0 ; j<N ; j++ )
{
for( int k=0 ; k<M ; k++ )
out<<B[j][k]<<" " ;
out<<endl ;
}
//write the initial probability vector pi
cout<<"Now output vector pi"<<endl ;
for( i=0 ; i<N ; i++ )
{
out<<pi[i]<<" " ;
}
out<<endl ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -