int main() { int num[MAXN], n, m; printf("input n, m: "); scanf("%d %d", &n, &m); m_sum(n, m, num, 0, n); return 0; }
void m_sum(int n, int m, int num[], int l, int t) { int i; if (l == m) { if (n != 0) return; for (i = 0; i < l; i++) printf("%d ", num[i]); printf("\n"); return; } for (i = 0; i <= min(t, n); i++) { num[l] = i; m_sum(n - i, m, num, l + 1, i); } }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////// THE MAIN ////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// void main() { float *a;//Will contain pointer pointing to the resultant matrix int choice; //////////////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////START OF MENU/////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////// do { choice=menu();//variable recieves option number selected by the user switch(choice) { case 0:choice=0; printf("\n\n\nTHANK YOU\n\n"); break; case 1:compatibility=1; get_input(compatibility);//will input all data into global struct A and B a=m_sum(X,Y);//ADDITION of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 2:compatibility=1; get_input(compatibility);//will input all data into global struct A and B a=m_diff(X,Y);//Subraction of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 3:compatibility=2; get_input(compatibility);//will input all data into global struct A and B a=m_prod(X,Y);//Product of struct matrix X,struct matrix Y show_output(a);//will print the result in matrix A[][] break; case 4:/* compatibility=2; get_input(compatibility); a=m_div(X,Y); show_output(a); */ break; case 5:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 6:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 7:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 8:/* compatibility=refer header comments; get_input(compatibility); a=m_funtion(input parameters); show_output(a); */ break; case 9: compatibility=3; get_input(compatibility); a=m_reshape(X,Y); show_output(a); break; case 10: compatibility=0; get_input(compatibility); ans=m_det(X.arr,X.row_size*X.colm_size); printf("The result of the determinant is %f",ans); break; } }while(choice!=0); }
/* Add a weak learner h to the strong classifier H */ void addWeak( int posCount, int negCount, int blockCount, int selectTable[], float ***POS, float ***NEG, Matrix *posWeight, Matrix *negWeight, Ada *strong, int *hUsed ) { /* For all possible features (Bid, Fid) */ int Iid, Bid, Fid; Matrix posCorrect, negCorrect, ONES; /* classification correctness matrices */ Matrix bestPosCorrect, bestNegCorrect; float bestError = 1.f; int bestBid, bestFid; short bestParity; float bestDecision; float alpha; float normalize; #if SHOWAVGERROR float avgError = 1.f; int round = 0; #endif ones( &posCorrect, 1, posCount, 1 ); ones( &negCorrect, 1, posCount, 1 ); ones( &ONES, 1, posCount, 1 ); #if 0 /* Show the data weight */ full_dump( posWeight, "posWeight", ALL, FLOAT ); full_dump( negWeight, "negWeight", ALL, FLOAT ); #endif printf( "Add a weak classifier\n" ); for ( Bid = 0; Bid < blockCount; Bid++ ) { for ( Fid = 0; Fid < FEATURE_COUNT; Fid++ ) { float POS_mean = 0.f, NEG_mean = 0.f; float decision; float error = 0.f; short parity = +1; /* default: ... NEG_mean ... | ... POS_mean ... */ /* [0] Initilize the matrices: Restore to all 1's */ full_assign( &ONES, &posCorrect, ALL, ALL ); full_assign( &ONES, &negCorrect, ALL, ALL ); /* [1] Compute the POS & NEG mean feature value */ for ( Iid = 0; Iid < posCount; Iid++ ) { POS_mean += POS[ Iid ][ Bid ][ Fid ]; NEG_mean += NEG[ selectTable[ Iid ] ][ Bid ][ Fid ]; } POS_mean /= posCount; NEG_mean /= posCount; /* default decision threshold: avg of POS_mean and NEG_mean */ decision = ( POS_mean + NEG_mean ) / 2.f; if ( POS_mean < NEG_mean ) { parity = -1; /* toggle: ... POS_mean ... | ... NEG_mean ... */ } #if 0 printf( "Bid: %d, Fid: %d, POS_mean: %f, NEG_mean: %f\n", Bid, Fid, POS_mean, NEG_mean ); #endif /* [2] Classification and compute the WEIGHTED error rate */ for ( Iid = 0; Iid < posCount; Iid++ ) { /* positive & wrong */ if ( parity * POS[ Iid ][ Bid ][ Fid ] < parity * decision ) { error += posWeight->data[ 0 ][ 0 ][ Iid ]; posCorrect.data[ 0 ][ 0 ][ Iid ] = -1.f; } /* negative & wrong */ if ( parity * NEG[ selectTable[ Iid ] ][ Bid ][ Fid ] > parity * decision ) { error += negWeight->data[ 0 ][ 0 ][ Iid ]; negCorrect.data[ 0 ][ 0 ][ Iid ] = -1.f; } } #if SHOWAVGERROR printf( "Weighted error rate: %f\n", error ); #endif /* record the best h */ if ( error < bestError ) { copy( &posCorrect, &bestPosCorrect ); copy( &negCorrect, &bestNegCorrect ); bestError = error; bestBid = Bid; bestFid = Fid; bestParity = parity; bestDecision = decision; } #if SHOWAVGERROR round++; avgError += error; #endif } /* end of loop Fid */ } /* end of loop Bid */ #if SHOWAVGERROR avgError /= round; printf( "Avg. weighted error rate: %f\n", avgError ); #endif printf( "\nBest weighted error rate: %f, Bid: %d, Fid: %d\n", bestError, bestBid, bestFid ); /* make sure that best error rate < 0.5 (random guessing) */ if ( bestError >= 0.5 ) { error( "addWeak(): Best error rate is above 0.5!" ); } #if 0 full_dump( &bestPosCorrect, "bestPosCorrect", ALL, INT ); full_dump( &bestNegCorrect, "bestNegCorrect", ALL, INT ); #endif /* [3] Record the best parameters, then determine the alpha weight for this weak classifier. */ strong[ *hUsed ].Bid = bestBid; strong[ *hUsed ].Fid = bestFid; strong[ *hUsed ].parity = bestParity; strong[ *hUsed ].decision = bestDecision; alpha = logf( ( 1.f - bestError ) / bestError ) / 2.f; strong[ *hUsed ].alpha = alpha; printf( "alpha%d: %f\n", *hUsed, alpha ); /* [4] Update the data weight */ s_mul( &bestPosCorrect, -alpha ); s_mul( &bestNegCorrect, -alpha ); s_expRaise( &bestPosCorrect ); s_expRaise( &bestNegCorrect ); e_mul( &bestPosCorrect, posWeight, posWeight ); e_mul( &bestNegCorrect, negWeight, negWeight ); normalize = m_sum( posWeight ) + m_sum( negWeight ); s_mul( posWeight, 1.f / normalize ); s_mul( negWeight, 1.f / normalize ); (*hUsed)++; /* free memory space */ freeMatrix( &posCorrect ); freeMatrix( &negCorrect ); freeMatrix( &bestPosCorrect ); freeMatrix( &bestNegCorrect ); }