예제 #1
0
int F(int n,int _a,int _b,int _c) {
	++_a,++_b,++_c;
	int y=_a*_b*_c;
	mat A; 
  clear(&A, y);
	for(int a=0;a<_a;a++) for(int b=0;b<_b;b++) for(int c=0;c<_c;c++) {
		int I=a*_b*_c + b*_c + c;
		do {
			int aa=a+1,bb=b-1,cc=c;
			if(aa==_a || bb<0) break;
			int J=aa*_b*_c + bb*_c + cc;
			A.a[I][J]++;
		} while(0);
		do {
			int aa=a,bb=b+1,cc=c-1;
			if(bb==_b || cc<0) break;
			int J=aa*_b*_c + bb*_c + cc;
			A.a[I][J]++;
		} while(0);
		do {
			int aa=a-1,bb=b,cc=c+1;
			if(cc==_c || aa<0) break;
			int J=aa*_b*_c + bb*_c + cc;
			A.a[I][J]++;
		} while(0);
	}
  mat ee = do_exp(&A,n,y);
	return ans(&ee, y);
}
예제 #2
0
mat do_exp( mat*A, int e,int y) {
	assert(e);
	if(e==1) return *A;
	if(e&1){mat de=do_exp(A, e-1, y); return mul(A, &de, y);}
	mat B = do_exp(A, e>>1, y);
	return mul(&B, &B, y);
}
예제 #3
0
파일: hmm.cpp 프로젝트: sjneph/hmm
void output(const Input& input) {
  if ( input._operation == Ops::TRAIN ) {
    std::cout << nstate_header << " " << input._nstates << std::endl;
    std::cout << nsymbol_header << " " << input._nsymbols << std::endl;

    std::cout << label_header << std::endl << "{" << std::endl;;
    for ( auto& i : input._mapID )
      std::cout << i.second << " " << i.first << std::endl;
    std::cout << "}" << std::endl;

    std::cout << initial_header;
    for ( std::size_t i = 0; i < input._initial.size(); ++i )
      std::cout << " " << input._initial[i];
    std::cout << std::endl;

    std::cout << transitional_header << std::endl << "{" << std::endl;
    for ( std::size_t i = 0; i < input._transition.size(); ++i ) {
      if ( input._transition[i][0] == ci::inf<T>() )
        std::cout << 0;
      else
        std::cout << input._transition[i][0];

      for ( std::size_t j = 1; j < input._transition[i].size(); ++j ) {
        if ( input._transition[i][j] == ci::inf<T>() )
          std::cout << " " << 0;
        else
          std::cout << " " << input._transition[i][j];
      } // for
      std::cout << std::endl;
    } // for
    std::cout << "}" << std::endl;

    std::cout << emission_header << std::endl << "{" << std::endl;
    for ( std::size_t i = 0; i < input._emission.size(); ++i ) {
      if ( input._emission[i][0] == ci::inf<T>() )
        std::cout << 0;
      else
        std::cout << input._emission[i][0];

      for ( std::size_t j = 1; j < input._emission[i].size(); ++j ) {
        if ( input._emission[i][j] == ci::inf<T>() )
          std::cout << " " << 0;
        else
          std::cout << " " << input._emission[i][j];
      } // for
      std::cout << std::endl;
    } // for
    std::cout << "}" << std::endl;
  } else if ( input._operation == Ops::PROB ) {
    std::cout << ci::hmm::evalp(input._observed, input._initial, input._transition, input._emission) << std::endl;
  } else if ( input._operation == Ops::DECODE ) { \
    auto initial_cpy = input._initial;
    do_exp(initial_cpy);
    std::ostream_iterator<U> os(std::cout, " ");
    ci::hmm::viterbi(input._observed, initial_cpy, input._transition, input._emission, os);
    std::cout << std::endl;
  } else { // Ops::TRAIN_AND_DECODE
    std::ostream_iterator<U> os(std::cout, " ");
    ci::hmm::viterbi(input._observed, input._initial, input._transition, input._emission, os);
    std::cout << std::endl;
  }
}