char* showBody(const Body* p) { char* buf; char* vel; char* pos; if (!p) return NULL; vel = showVector(Vel(p)); pos = showVector(Pos(p)); if (0 > asprintf(&buf, "body { \n" " mass = %g\n" " position = %s\n" " velocity = %s\n" " ignore = %s\n" " };\n", Mass(p), pos, vel, showBool(ignoreBody(p)))) { mw_fail("asprintf() failed\n"); } free(vel); free(pos); return buf; }
void problem(int n){ switch(n){ case 123:{ //int a[] = {-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4}; int a[] = {1 , 2 , 3 , 0 , 2}; int len = sizeof(a) / sizeof(int); vector<int> in(a , a + len); cout << maxProfit2(in) << endl; } break; case 121:{ //int a[] = {-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4}; int a[] = {1 , 2 , 3 , 0 , 2}; int len = sizeof(a) / sizeof(int); vector<int> in(a , a + len); cout << maxProfit(in) << endl; } break; case 279:{ cout << numSquares(12) << endl; cout << numSquares(13) << endl; } break; case 309:{ //int a[] = {-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4}; int a[] = {1 , 2 , 3 , 0 , 2}; int len = sizeof(a) / sizeof(int); vector<int> in(a , a + len); cout << maxProfit_cooldown(in) << endl; } break; case 343:{ cout << integerBreak(5) << endl; cout << integerBreak(10) << endl; } break; case 338:{ showVector(countBits(5)); showVector(countBits(15)); } break; case 70:{ cout << climbStairs(5) << endl; cout << climbStairs(15) << endl; cout << climbStairs(8) << endl; } break; case 53:{ //int a[] = {-2 , 1 , -3 , 4 , -1 , 2 , 1 , -5 , 4}; int a[] = {-2 , -1 , -3}; int len = sizeof(a) / sizeof(int); vector<int> in(a , a + len); cout << maxSubArray(in) << endl; } break; default: break; } }
void Snap::showVectors() { cout<<"vectors for "<<n<<endl<<"direction :\t"; showVector(direction); cout<<"up :\t\t"; showVector(up); cout<<"cross :\t\t"; showVector(cross); cout<<endl; }
void printNeighbours() { //put here for test (temporarily) std::cout << "compatible categories:\n"; showPrimitiveVector(compatibleCategories); std::cout << "Neighbours:\ngateA:\n"; showVector(gateANeighbours); std::cout << "gateB:\n"; showVector(gateBNeighbours); std::cout << std::endl; }
int main(int argc, char **argv) { real **A, *x, *b; A = allocMatrix(3, 3); x = allocVector(3); b = allocVector(3); A[0][0]= 0; A[0][1]= 2; A[0][2]= 3; A[1][0]= 3; A[1][1]= 0; A[1][2]= 1; A[2][0]= 6; A[2][1]= 2; A[2][2]= 8; b[0] = 5; b[1] = 4; b[2] = 16; showMatrix (3, A); printf ("\n"); solve(3, A, x, b); showVector(3, x); freeMatrix(A); freeVector(x); freeVector(b); return EXIT_SUCCESS; }
int main(int argc, char **argv) { /* start timer */ double clock; clock = timer(); real **A, *x, *b; int n = 1000; A = allocMatrix(n, n); x = allocVector(n); b = allocVector(n); initializeA(A, n); initializeB(b, n); //showMatrix (n, A); //printf ("\n"); solve(n, A, x, b); /* stop timer */ clock = timer() - clock; showVector(n, x); freeMatrix(A); freeVector(x); freeVector(b); printf ("wallclock: %lf seconds\n", clock); return EXIT_SUCCESS; }
char* showCell(const NBodyCell* c) { char* buf; char* posBuf; if (!c) return NULL; posBuf = showVector(Pos(c)); if (0 > asprintf(&buf, "NBodyCell = {\n" " cellnode = {\n" " pos = %s\n" " next = %p\n" " mass = %f\n" " type = %d\n" " }\n" " rcrit2 = %f\n" " more = %p\n" " stuff = {\n" " .quad = {\n" " .xx = %f, .xy = %f, .xz = %f,\n" " .yy = %f, .yz = %f,\n" " .zz = %f\n" " },\n" "\n" " .subp = {\n" " %p, %p, %p, %p,\n" " %p, %p, %p, %p\n" " }\n" " }\n" "}\n", posBuf, (void*) Next(c), Mass(c), Type(c), Rcrit2(c), (void*) More(c), Quad(c).xx, Quad(c).xy, Quad(c).xz, Quad(c).yy, Quad(c).yz, Quad(c).zz, (void*) Subp(c)[0], (void*) Subp(c)[1], (void*) Subp(c)[2], (void*) Subp(c)[3], (void*) Subp(c)[5], (void*) Subp(c)[5], (void*) Subp(c)[6], (void*) Subp(c)[7] )) { mw_fail("asprintf() failed\n"); } free(posBuf); return buf; }
void mexFunction( int nargout, mxArray *out[], int nargin, const mxArray *in[] ) { jhm::cout_redirect(); jhm::MAT mfile("data.mat"); jhm::println( "Opened file with %d variables.", mfile.nfields() ); showLogical(mfile); showNumeric(mfile); showString(mfile); showVector(mfile); showMatrix(mfile); showVolume(mfile); }
void addNewState(int dept, bool verbose, const Node &node, const vector<int> &target, Node &newNode, map<vector<int>, list<Node>::iterator> &score_table, list<Node> &q, bool isNew) { // 履歴を更新 newNode.addHistory(dept); if (verbose) { if (isNew) { cout << "new: "; } else { cout << "update: "; } showVector(newNode.getHistory()); } // 自分が持つ部署集合のスコアを記録 // 一人目の場合は単純にスコアを記録する if (node.getDepth() == 0) { newNode.setScore(target.at(dept)); } // 二人目以降は現在のスコアテーブルの値に今回のスコアを足して、新たな状態を記録する else { newNode.setScore(score_table.at(node.getDepts())->getScore() + target.at(dept)); } if (verbose) { if (isNew) { cout << "new score: " << newNode.getScore() << endl; } else { cout << "updated score: " << newNode.getScore() << endl; } } // 新規ノードをlistに入れる // q.push_back(std::move(newNode)); q.push_back(newNode); auto itr = end(q); --itr; score_table[newNode.getDepts()] = itr; }
int main() { int *vector, size; readSize(&size); vector = (int *)malloc(size*sizeof(int)); if(vector==NULL){ exit(1); } readVector(vector, size); bubble_sort(vector, size); showVector(vector, size); free(vector); return 0; }
list<Node> pdpSearch(const vector<int> &scores, vector<int> &capacity, vector<vector<int> > &choices, vector<int> &choicesID, bool verbose, bool hopelessCut) { // 幅優先探索に使用するqueue list<Node> q; // 枝狩り幅優先探索のためのlistに空のルートノード(深さ0)を入れる q.emplace_back(0, choices.front().size()); // これまでに選択した部署の集合をキーとし、スコアをバリューとしたマップ map<vector<int>, list<Node>::iterator> score_table; double scoreMean = getScoreMean(scores, choices.front().size()); double scoreVariance = getScoreVariance(scores, choices.front().size()); // 探索深さdが人数より小さい間 for (int d = 0; d < (int) choices.size(); d++) { if (verbose) { cout << endl; cout << "depth: " << d << endl; cout << "queue size: " << q.size() << endl; } // d人目の志望度ベクトル vector<int> target = choices.at(d); if (verbose) { cout << choicesID.at(d) << ": "; showVector(target); } // 実際の処理に入る前に劣っているノードを削除する // rmInferiorNodes(q, score_table); /* カットオフ値をここで決めておく。 この値は現在のqの内容から推測する。 */ double cutOff = 0; if (hopelessCut) { cutOff = getCutOffLowerBound(q, scoreMean, scoreVariance, d, choices.size()); if (verbose) { cout << "cutOff: " << cutOff << endl; } } int numRemoved = 0; assert(!q.empty()); cout << "depth, queue size: " << d << ", " << q.size() << endl; // 先頭要素の深さがdである間 while (q.front().getDepth() == d) { assert(!q.empty()); // queueの先頭要素を取り出す Node* node = &q.front(); for (int i = 0; i < (int) choices.front().size(); i++) { // 部署iがすでに定員に達していたら if (node->getNumDept(i) == capacity.at(i)) { if (verbose) { cout << "Dept " << i << " has already reached the limit. Skip!" << endl; } continue; } // 枝刈り閾値よりスコアが小さければスキップ if (hopelessCut && cutOff > (node->getDepth() == 0 ? target.at(i) : score_table.at(node->getDepts())->getScore() + target.at(i))) { numRemoved++; if (verbose) { cout << "The score is lower than the cut off value. Skip!" << endl; } continue; } // 取り出したノードよりも深さを1つ増やした新規ノード情報を作成 Node newNode = *node; newNode.incrementDepth(); // 現在注目している人がi番目の部署を選んだという情報を新規ノードに加える newNode.addDept(i); // もしこれまでに選択された部署の集合が未登録なら if (score_table.find(newNode.getDepts()) == end(score_table)) { addNewState(i, verbose, *node, target, newNode, score_table, q, true); } // 既存のものよりスコアが高ければ else if (score_table.at(newNode.getDepts())->getScore() < score_table.at(node->getDepts())->getScore() + target.at(i)) { q.erase(score_table.at(newNode.getDepts())); addNewState(i, verbose, *node, target, newNode, score_table, q, false); } } q.pop_front(); } if (hopelessCut) { cout << "# of removed node: " << numRemoved << endl; } } cout << "final queue size: " << q.size() << endl; return q; }
int main(int argc, char **argv) { /* **Primeiro Teste ** Alocando dinamicamente e mostrando as distancias. **Liberando memória depois */ /* Point *a; Point *b; a = cria(3.0, 0.0); b = cria(0.0, 0.0);; double distancia; printf("\nPonto A: \n"); showPoint(a); printf("\nPonto B: \n"); showPoint(b); distancia = calculaDistanciaEntrePontos(a, b); printf("\n\n\nDistância entre os pontos é de: %.2f\n\n\n", distancia); free(a); free(b); */ /* **Segundo teste. **Puxando todos os pontos do arquivo para a memória e mostrando todos. */ int numeroDeBytes; int dimensaoDoVetor; Point *vetorDePontos; numeroDeBytes = identificaNumeroDeBytesDeArquivo("points01.bin"); printf("O vetor do arquivo possui %d bytes /n/n/n", numeroDeBytes); dimensaoDoVetor = numeroDeBytes/sizeof(Point); printf("\nLogo a dimensão é de: %d pontos \n", dimensaoDoVetor); //Faço uma alocação dinâmica para a quantidade de vetorres que quero. vetorDePontos = (Point *)calloc(dimensaoDoVetor, sizeof(Point)); if(vetorDePontos == NULL){ exit(1); } if(!loadVector("points01.bin", vetorDePontos, dimensaoDoVetor)){ printf("AAAAAAA que pena. "); exit(1); } showVector(vetorDePontos, dimensaoDoVetor); /* **Terceiro teste ** O ponto mais distante da origem. */ /* int numeroDeBytes; int dimensaoDoVetor; Point *vetorDePontos; Point *maisDistante; numeroDeBytes = identificaNumeroDeBytesDeArquivo("points01.bin"); printf("O vetor do arquivo possui %d bytes /n/n/n", numeroDeBytes); dimensaoDoVetor = numeroDeBytes/sizeof(Point); printf("\nLogo a dimensão é de: %d pontos \n", dimensaoDoVetor); //Faço uma alocação dinâmica para a quantidade de vetorres que quero. vetorDePontos = (Point *)calloc(dimensaoDoVetor, sizeof(Point)); if(vetorDePontos == NULL){ exit(1); } if(!loadVector("points01.bin", vetorDePontos, dimensaoDoVetor)){ printf("AAAAAAA que pena. "); exit(1); } maisDistante = retornaEnderecoDoMaisDistanteDaOrigem(vetorDePontos, dimensaoDoVetor); printf("O mais distante da origem é o seguinte ponto: "); showPoint(maisDistante); */ return 0; }
AUD_Int32s train_keyword_hmm( const AUD_Int8s *pKeywordFile, AUD_Int8s *pHmmName ) { AUD_Error error = AUD_ERROR_NONE; // step 1: read garbage model from file void *hGarbageGmm = NULL; FILE *fpGarbage = fopen( (char*)WOV_UBM_GMMHMMMODEL_FILE, "rb" ); if ( fpGarbage == NULL ) { AUDLOG( "cannot open gmm model file: [%s]\n", WOV_UBM_GMMHMMMODEL_FILE ); return AUD_ERROR_IOFAILED; } error = gmm_import( &hGarbageGmm, fpGarbage ); AUD_ASSERT( error == AUD_ERROR_NONE ); fclose( fpGarbage ); fpGarbage = NULL; // AUDLOG( "garbage GMM as:\n" ); // gmm_show( hGarbageGmm ); // step 2: read template stream & extract MFCC feature vector AUD_Int32s sampleNum = 0; AUD_Int32s bufLen = SAMPLE_RATE * BYTES_PER_SAMPLE * 10; AUD_Int16s *pBuf = (AUD_Int16s*)calloc( bufLen, 1 ); AUD_ASSERT( pBuf ); AUD_Int32s ret; // read stream from file sampleNum = readWavFromFile( (AUD_Int8s*)pKeywordFile, pBuf, bufLen ); AUD_ASSERT( sampleNum > 0 ); AUD_Int32s i = 0, j = 0, k = 0, m = 0; // front end processing // pre-emphasis sig_preemphasis( pBuf, pBuf, sampleNum ); // calc frame number for ( j = 0; j * FRAME_STRIDE + FRAME_LEN <= sampleNum; j++ ) { ; } AUD_Feature feature; feature.featureMatrix.rows = j - MFCC_DELAY; feature.featureMatrix.cols = MFCC_FEATDIM; feature.featureMatrix.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &(feature.featureMatrix) ); AUD_ASSERT( ret == 0 ); feature.featureNorm.len = j - MFCC_DELAY; feature.featureNorm.dataType = AUD_DATATYPE_INT64S; ret = createVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); // init mfcc handle void *hMfccHandle = NULL; error = mfcc16s32s_init( &hMfccHandle, FRAME_LEN, WINDOW_TYPE, MFCC_ORDER, FRAME_STRIDE, SAMPLE_RATE, COMPRESS_TYPE ); AUD_ASSERT( error == AUD_ERROR_NONE ); // calc MFCC feature error = mfcc16s32s_calc( hMfccHandle, pBuf, sampleNum, &feature ); AUD_ASSERT( error == AUD_ERROR_NONE ); free( pBuf ); pBuf = NULL; // step 3: for each feature vector, get the bestN most likelihood component indices from GMM AUD_Vector componentLLR; componentLLR.len = gmm_getmixnum( hGarbageGmm ); componentLLR.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &componentLLR ); AUD_ASSERT( ret == 0 ); AUD_Matrix indexTable; indexTable.rows = feature.featureMatrix.rows ; indexTable.cols = WOV_KEYWORD_GMMMODEL_ORDER; indexTable.dataType = AUD_DATATYPE_INT32S; ret = createMatrix( &indexTable ); AUD_ASSERT( ret == 0 ); AUD_Matrix llrTable; llrTable.rows = feature.featureMatrix.rows; llrTable.cols = WOV_KEYWORD_GMMMODEL_ORDER; llrTable.dataType = AUD_DATATYPE_DOUBLE; ret = createMatrix( &llrTable ); AUD_ASSERT( ret == 0 ); AUD_Double totalLLR; for ( i = 0; i < feature.featureMatrix.rows; i++ ) { totalLLR = gmm_llr( hGarbageGmm, &(feature.featureMatrix), i, &componentLLR ); #if 0 showVector( &componentLLR ); #endif // sort the bestN likelihood AUD_Int32s *pIndex = indexTable.pInt32s + i * indexTable.cols; AUD_Double *pLLR = llrTable.pDouble + i * llrTable.cols; for ( j = 0; j < WOV_KEYWORD_GMMMODEL_ORDER; j++ ) { pIndex[j] = -1; pLLR[j] = 0.; } for ( j = 0; j < componentLLR.len; j++ ) { for ( k = 0; k < WOV_KEYWORD_GMMMODEL_ORDER; k++ ) { if ( pIndex[k] == -1 ) { pIndex[k] = j; pLLR[k] = componentLLR.pDouble[j]; break; } else if ( componentLLR.pDouble[j] > pLLR[k] ) { for ( m = WOV_KEYWORD_GMMMODEL_ORDER - 1; m > k ; m-- ) { pIndex[m] = pIndex[m - 1]; pLLR[m] = pLLR[m - 1]; } pIndex[k] = j; pLLR[k] = componentLLR.pDouble[j]; break; } } } } #if 0 AUDLOG( "index table( %s, %s, %d ):\n", __FILE__, __FUNCTION__, __LINE__ ); showMatrix( &indexTable ); AUDLOG( "llr table( %s, %s, %d ):\n", __FILE__, __FUNCTION__, __LINE__ ); showMatrix( &llrTable ); #endif ret = destroyVector( &componentLLR ); AUD_ASSERT( ret == 0 ); // step 4: cluster GMM AUD_Int32s *pClusterLabel = (AUD_Int32s*)calloc( sizeof(AUD_Int32s) * feature.featureMatrix.rows, 1 ); AUD_ASSERT( pClusterLabel ); error = gmm_cluster( hGarbageGmm, &indexTable, WOV_GMM_CLUSTER_THRESHOLD, pClusterLabel ); AUD_ASSERT( error == AUD_ERROR_NONE ); AUD_Int32s stateNum = pClusterLabel[feature.featureMatrix.rows - 1]; AUD_ASSERT( stateNum >= 5 ); // step 5: select and build state GMM void **phKeywordGmms = (void**)calloc( sizeof(void*) * stateNum, 1 ); AUD_ASSERT( phKeywordGmms ); AUD_Vector indexVector; indexVector.len = WOV_KEYWORD_GMMMODEL_ORDER; indexVector.dataType = AUD_DATATYPE_INT32S; ret = createVector( &indexVector ); AUD_ASSERT( ret == 0 ); AUD_Vector llrVector; llrVector.len = WOV_KEYWORD_GMMMODEL_ORDER; llrVector.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &llrVector ); AUD_ASSERT( ret == 0 ); int start = 0, end = 0; for ( i = 0; i < stateNum; i++ ) { for ( j = 0; j < indexVector.len; j++ ) { indexVector.pInt32s[j] = -1; llrVector.pInt32s[j] = 1.; } for ( j = start; j < feature.featureMatrix.rows; j++ ) { if ( pClusterLabel[j] != i ) { break; } } end = j; for ( k = start * llrTable.cols; k < end * llrTable.cols; k++ ) { for ( m = 0; m < indexVector.len; m++ ) { if ( llrTable.pDouble[k] == llrVector.pDouble[m] && indexTable.pInt32s[k] == indexVector.pInt32s[m] ) { break; } else if ( indexVector.pInt32s[m] == -1 || llrTable.pDouble[k] > llrVector.pDouble[m] ) { for ( int n = indexVector.len - 1; n > m ; n-- ) { indexVector.pInt32s[n] = indexVector.pInt32s[n - 1]; llrVector.pDouble[n] = llrVector.pDouble[n - 1]; } indexVector.pInt32s[m] = indexTable.pInt32s[k]; llrVector.pDouble[m] = llrTable.pDouble[k]; break; } } } // AUDLOG( "Final GMM indices for state[%d]:\n", i ); // showVector( &indexVector ); AUD_Int8s gmmName[256] = { 0, }; sprintf( (char*)gmmName, "state%d", i ); error = gmm_select( &phKeywordGmms[i], hGarbageGmm, &indexVector, 0, gmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); start = end; } ret = destroyMatrix( &indexTable ); AUD_ASSERT( ret == 0 ); ret = destroyMatrix( &llrTable ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &indexVector ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &llrVector ); AUD_ASSERT( ret == 0 ); free( pClusterLabel ); pClusterLabel = NULL; // step 6: generate keyword model by Baum-Welch algorithm AUD_Vector pi; pi.len = stateNum; pi.dataType = AUD_DATATYPE_DOUBLE; ret = createVector( &pi ); AUD_ASSERT( ret == 0 ); pi.pDouble[0] = 1.0f; void *hKeywordHmm = NULL; error = gmmhmm_init( &hKeywordHmm, stateNum, &pi, phKeywordGmms ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = gmmhmm_learn( hKeywordHmm, &feature, 1, 0.001 ); AUD_ASSERT( error == AUD_ERROR_NONE ); // step 8: write model to file error = gmmhmm_export( hKeywordHmm, pHmmName ); AUD_ASSERT( error == AUD_ERROR_NONE ); // gmmhmm_show( hKeywordHmm ); // clean field error = mfcc16s32s_deinit( &hMfccHandle ); AUD_ASSERT( error == AUD_ERROR_NONE ); error = gmm_free( &hGarbageGmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); ret = destroyMatrix( &(feature.featureMatrix) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &(feature.featureNorm) ); AUD_ASSERT( ret == 0 ); ret = destroyVector( &pi ); AUD_ASSERT( ret == 0 ); for ( i = 0; i < stateNum; i++ ) { error = gmm_free( &phKeywordGmms[i] ); AUD_ASSERT( error == AUD_ERROR_NONE ); } free( phKeywordGmms ); phKeywordGmms = NULL; error = gmmhmm_free( &hKeywordHmm ); AUD_ASSERT( error == AUD_ERROR_NONE ); return 0; }