void test_multinomial(void){
    double p[SIZE] = { .1, .2, .3, .4 };
    int n[SIZE] = { 0, 0, 0, 0 };
    int numdraws = 100;
    double prob;

    gsl_ran_multinomial(rng, SIZE, numdraws, p, n); 

    printf("gsl_ran_multinomial\t%d\t", numdraws);
    print_double_array(p, SIZE);
    printf("\t");
    print_int_array(n, SIZE);
    printf("\n");

    prob = gsl_ran_multinomial_pdf(SIZE, p, n); 
    printf("gsl_ran_multinomial_pdf\t");
    print_double_array(p, SIZE);
    printf("\t");
    print_int_array(n, SIZE);
    printf("\t%.12f\n", prob);

    prob = gsl_ran_multinomial_lnpdf(SIZE, p, n); 
    printf("gsl_ran_multinomial_lnpdf\t");
    print_double_array(p, SIZE);
    printf("\t");
    print_int_array(n, SIZE);
    printf("\t%.12f\n", prob);
}
示例#2
0
char test_gradient5()
{
    double dat[] = {3., 90., 98., 99., 4.};
    double x[] = {1., 2., 2.1, 2.4, 4. };
    double grad_min = -50;
    double grad_max = 50;
    double mindx = 0.5;
    double startdat = 0;
    double toldat = 5;
    size_t len = 5;
    signed char expected[] = {1, 0, -99, -99, 1};
    signed char out[] = {1, 1, 1, 1, 1};
    printf("test_gradient5... ");
    gradient(out, dat, x, len, grad_min, grad_max, mindx, startdat, toldat);
    for(int i=0;i<len;i++) {
        if(!(expected[i]==out[i])) {
            message = "Expected doesn't match received";
            printf("\n");
            print_double_array(dat,5);
            print_array(expected,5);
            print_array(out,5);
            return false;
        }
    }
    return true;
}
void runb_p(gsl_odeiv2_driver *d,double *t,double *y,double *b,int starti, int endi,unsigned int S, FILE *out)
{
    double t1 = endi;
    int nbtimsteps = 0;
    for (int i = starti ; i <= endi ; ++i)
    {
	// RUN THE INTEGRATION
	//--------------------  
        const double ti = i * t1 / endi;
        gsl_odeiv2_driver_apply(d, t, ti, y);  
	//display_densities(y,S,i);

	// PUT A THRESHOLD FOR EXTINCTIONS AND SUM THE BIOMASSES OVER TIME
	//----------------------------------------------------------------
	for(int j = 0 ; j < S ; ++j)
	{
	    if(y[j] < EAM_TRESH_SP) y[j] = 0.0;
	    b[j] += y[j];                       
	}
	++nbtimsteps;
	fprintf(out,"\n%d\t",i);
	print_double_array(y,S,out);
    }

    // CALCULATE THE AVERAGE BIOMASSES
    //--------------------------------
    for (int i = 0 ; i < S ; ++i)
    {        
	b[i] = b[i] / nbtimsteps;          
    }
}
示例#4
0
char test_gradient6() 
{
    double x[]   = {0. , 1. , 2. , 3. , 4. , 5. , 6. , 7. , 8. , 9.};
    double dat[] = {2. , 2. , 2. , 2. , 2. , 2. , 2. , 2. , 2. , 2.};
    double grad_min = -10.0;
    double grad_max = 10.0;
    double mindx = 2;
    double startdat = 0;
    double toldat = 5;
    size_t len = 10;
    signed char out[10];
    signed char expected[] = {1, -99, -99, 1, -99, -99, 1, -99, -99, 1};
    memset(out, 1, 10);
    printf("test_gradient6... ");
    gradient(out, dat, x, len, grad_min, grad_max, mindx, startdat, toldat, -99);
    for(int i=0;i<len;i++) {
        if(!(expected[i] == out[i])) {
            message = "Expected doesn't match received";
            printf("\n");
            print_double_array(dat, 10);
            print_array(expected, 5);
            print_array(out, 5);
            return false;
        }
    }
    return true;
}
示例#5
0
char check_expected(double *out, double *expected, size_t len, double atol, double rtol)
{
    size_t i=0;
    for(i=0;i<len;i++) {
        if( fabs(expected[i] - out[i]) > (atol + rtol * fabs(out[i])) ) {
            message = "Expected does not match received.";
            printf("index is %lu\n", i);
            printf("| %.6f - %.6f | >= %.6f\n", out[i], expected[i], (atol + rtol * fabs(out[i])));
            printf("\n");
            print_double_array(expected, len);
            print_double_array(out, len);
            return 0;
        }
    }
    return 1;

}
void test_dirichlet(void){
    double alpha[SIZE] = { .4, .9, .4, .2 };
    double theta[SIZE] = { 0.0, 0.0, 0.0, 0.0 };
    double prob;
    int i;
    
    gsl_ran_dirichlet (rng, SIZE, alpha, theta);
    printf("gsl_ran_dirichlet\t");
    print_double_array(alpha, SIZE);
    printf("\t");
    print_double_array(theta, SIZE);
    printf("\n");

    theta[0] = 0.107873072217;
    theta[1] = 0.518033738502;
    theta[2] = 0.220000000209;
    theta[3] = 0.154093189072;
    
    /* theta and alpha flipped in perl interface */
    prob = gsl_ran_dirichlet_pdf (SIZE, alpha, theta);
    printf("gsl_ran_dirichlet_pdf\t");
    print_double_array(theta, SIZE);
    printf("\t");
    print_double_array(alpha, SIZE);
    printf("\t%.12f\n", prob);

    prob = gsl_ran_dirichlet_lnpdf (SIZE, alpha, theta);
    printf("gsl_ran_dirichlet_lnpdf\t");
    print_double_array(theta, SIZE);
    printf("\t");
    print_double_array(alpha, SIZE);
    printf("\t%.12f\n", prob);
}
void typeArrayKlass::oop_print_on(oop obj, outputStream* st) {
  arrayKlass::oop_print_on(obj, st);
  typeArrayOop ta = typeArrayOop(obj);
  int print_len = MIN2((intx) ta->length(), MaxElementPrintSize);
  switch (element_type()) {
    case T_BOOLEAN: print_boolean_array(ta, print_len, st); break;
    case T_CHAR:    print_char_array(ta, print_len, st);    break;
    case T_FLOAT:   print_float_array(ta, print_len, st);   break;
    case T_DOUBLE:  print_double_array(ta, print_len, st);  break;
    case T_BYTE:    print_byte_array(ta, print_len, st);    break;
    case T_SHORT:   print_short_array(ta, print_len, st);   break;
    case T_INT:     print_int_array(ta, print_len, st);     break;
    case T_LONG:    print_long_array(ta, print_len, st);    break;
    default: ShouldNotReachHere();
  }
  int remaining = ta->length() - print_len;
  if (remaining > 0) {
    tty->print_cr(" - <%d more elements, increase MaxElementPrintSize to print>", remaining);
  }
}
void runi_p(gsl_odeiv2_driver *d,double *t,double *y,int starti, int endi,unsigned int S, FILE *out)
{
    double t1 = endi;
    for (int i = starti ; i <= endi ; ++i)
    {  
	// RUN THE INTEGRATION
	//--------------------            
        const double ti = i * t1 / endi;      
        gsl_odeiv2_driver_apply(d, t, ti, y);   
	//display_densities(y,S,i);

	// PUT A THRESHOLD FOR EXTINCTIONS
	//--------------------------------
	for(int j = 0 ; j < S ; ++j)
	{
	    if(y[j]<EAM_TRESH_SP)y[j] = 0.0;
	}
	fprintf(out,"\n%d\t",i);
	print_double_array(y,S,out);
    }
}
void runb_plus_stab_p(gsl_odeiv2_driver *d,double *t,double *y,double *b,double *minis, double *maxis,int starti, int endi,unsigned int S, int *stab, FILE *out)
{
    memcpy(minis,y,S * sizeof(double));
    memcpy(maxis,y,S * sizeof(double));
    double t1 = endi;
    int nbtimsteps = 0;
    for (int i = starti ; i <= endi ; ++i)
    {
	// RUN THE INTEGRATION
	//--------------------  
        const double ti = i * t1 / endi;
        gsl_odeiv2_driver_apply(d, t, ti, y);  
	//display_densities(y,S,i);

	// PUT A THRESHOLD FOR EXTINCTIONS, SUM THE BIOMASSES AND FIND MINIMUM AND MAXIMUM OVER TIME
	//------------------------------------------------------------------------------------------
	for(int j = 0 ; j < S ; ++j)
	{
	    if(y[j] < EAM_TRESH_SP) y[j] = 0.0;
	    b[j] += y[j];
	    if(minis[j] > y[j]) minis[j] = y[j];
	    if(maxis[j] < y[j]) maxis[j] = y[j];                  
	}
	++nbtimsteps;
	fprintf(out,"\n%d\t",i);
	print_double_array(y,S,out);
    }

    // CALCULATE THE AVERAGE BIOMASSES
    //--------------------------------
    for (int i = 0 ; i < S ; ++i)
    {        
	b[i] = b[i] / nbtimsteps;          
    }
    *stab = (compare_dd(minis,maxis,S)) ? 1 : 2;  

}
示例#10
0
void L1regularizedL2_KDDB() {
	int n;
	int m;
	double lambda = 1;
	double C = 1;
	int NMax = 50;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	printf("Idem nacitavat data!\n");

	loadDataFromFile("/home/s1052689/kddb", &A_test, &R_Idx_test, &C_Idx_test,
			&C_Count_test, &y_test, &n_test, &m_test, &nnz_test, MAXINSTANCES,
			1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test;
	loadDataFromFile("/home/s1052689/kddb", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, MAXINSTANCES, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	printf("Training data loades!\n");

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/home/s1052689/kddb.t", &A_test_final, &R_Idx_test_final,
			&C_Idx_test_final, &C_Count_test_final, &y_test_final,
			&n_test_final, &m_test_final, &nnz_test_final, MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[cscount];
	double validation[cscount];
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < cscount; i++) {
		if (i > 0) {
			free(L);
			free(Li);
		}
		C = CS[i];
		printf("idem pocitat Li\n");
		computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
				C_Count_h, n, nnz, C);
		printf("idem ucit model \n");
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, NMax, value, 0);
		printf("idem ctosvalidation \n");
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 1, m_test);
		printf("idem fiinal test robit \n");
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		//		crossvalidationValue = finalTestValue;

		printf("C:%f, crossvalidation: %f, final: %f \n", C,
				crossvalidationValue, finalTestValue);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
		print_double_array(&w[0], 100);
	}

	for (i = 0; i < cscount; i++) {
		printf("C:%f, crossvalidation: %f; final: %f \n", CS[i], validation[i],
				finalvalidation[i]);
	}

}
示例#11
0
void porovnaniePodlaIteraciiKDDB() {
	int n;
	int m;
	FILE *fp;
	fp = fopen("/tmp/Vyvoj.csv", "w");

	double lambda = 1;
	double C = 1;
	int NMax = 100;
	double* A_h;
	int * C_Idx_h;
	int * R_Idx_h;
	int * C_Count_h;
	int nnz, i, j, k, l;
	double* y;
	double *L;
	double *Li;

	double* A_test;
	int * C_Idx_test;
	int * R_Idx_test;
	int * C_Count_test;
	double* y_test;
	int n_test;
	int m_test;
	int nnz_test;

	loadDataFromFile("/home/s1052689/kddb", &A_test, &R_Idx_test, &C_Idx_test,
			&C_Count_test, &y_test, &n_test, &m_test, &nnz_test, MAXINSTANCES,
			1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test, m_test, nnz_test);

	int trainingset = m_test;
	loadDataFromFile("/home/s1052689/kddb", &A_h, &R_Idx_h, &C_Idx_h,
			&C_Count_h, &y, &n, &m, &nnz, MAXINSTANCES, 1);
	printf("all features:%d , all data:%d, nnz:%d\n", n, m, nnz);

	double* A_test_final;
	int * C_Idx_test_final;
	int * R_Idx_test_final;
	int * C_Count_test_final;
	double* y_test_final;
	int n_test_final;
	int m_test_final;
	int nnz_test_final;

	loadDataFromFile("/home/s1052689/kddb.t", &A_test_final, &R_Idx_test_final,
			&C_Idx_test_final, &C_Count_test_final, &y_test_final,
			&n_test_final, &m_test_final, &nnz_test_final, MAXINSTANCES, 1);
	printf("TEST features:%d , all data:%d, nnz:%d\n", n_test_final,
			m_test_final, nnz_test_final);

	double w[n];
	for (i = 0; i < n; i++)
		w[i] = 0;

	double value = 0;
	double crossvalidationValue;
	double finalTestValue;
	double finalvalidation[NMax];
	double validation[NMax];
	double finalvalidationLog[NMax];
	double validationLog[NMax];
	int nnzofWLog[NMax];
	int nnzofW[NMax];
	computeLipsitzConstantsForL1RegL2(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {
		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 0, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		nnzofW[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofW[i]++;
		}

		clock_t t1, t2;
		t1 = clock();
		value = NRCDM_L1L2SVM(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y,
				w, lambda, C, Li, 1, value, 0);
		t2 = clock();
		float diff = ((float) t2 - (float) t1) / 1000000.0F;
		printf("calculation:%f\n", diff);

		fprintf(
				fp,
				"N,%d,crossvalidation,%f,final,%f,nnz,%d, calculaltionTime:%f\n",
				i, crossvalidationValue, finalTestValue, nnzofW[i], diff);
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i,
				crossvalidationValue, finalTestValue, nnzofW[i]);
		print_double_array(&w[0], 100);
		validation[i] = crossvalidationValue;
		finalvalidation[i] = finalTestValue;
	}

	free(L);
	free(Li);
	computeLipsitzConstantsForL1RegLog(&L, &Li, A_h, R_Idx_h, C_Idx_h,
			C_Count_h, n, nnz, C);
	for (i = 0; i < n; i++)
		w[i] = 0;
	for (i = 0; i < NMax; i++) {

		crossvalidationValue = crossvalidationForL1L2SVM(A_test, R_Idx_test,
				C_Idx_test, C_Count_test, n_test, m_test, y_test, w, 0, m_test);
		finalTestValue = crossvalidationForL1L2SVM(A_test_final,
				R_Idx_test_final, C_Idx_test_final, C_Count_test_final,
				n_test_final, m_test_final, y_test_final, w, 0, m_test_final);
		clock_t t1, t2;
		t1 = clock();
		value = NRCDM_L1Log(A_h, R_Idx_h, C_Idx_h, C_Count_h, n, m, nnz, y, w,
				lambda, C, Li, 1, value, 0);

		t2 = clock();
		float diff = ((float) t2 - (float) t1) / 1000000.0F;
		printf("calculation:%f\n", diff);

		validationLog[i] = crossvalidationValue;
		finalvalidationLog[i] = finalTestValue;
		nnzofWLog[i] = 0;
		for (j = 0; j < n; j++) {
			if (w[j] != 0)
				nnzofWLog[i]++;
		}
		fprintf(fp, "N,%d,crossvalidation,%f,final,%f,nnz,%d,calculationTime:%f\n", i,
				crossvalidationValue, finalTestValue, nnzofWLog[i],diff);
		printf("N,%d,crossvalidation,%f,final,%f,nnz,%d\n", i,
				crossvalidationValue, finalTestValue, nnzofWLog[i]);
	}

	fclose(fp);
	fp = fopen("/tmp/PorovnaniePodlaIteracii.csv", "w");
	printf("N,CVLog,CVL2,FVLog,FVL2,nnzLog,nnzL2\n");
	for (i = 0; i < NMax; i++) {
		printf("%d,%f,%f,%f,%f,%d,%d\n", i, validationLog[i], validation[i],
				finalvalidationLog[i], finalvalidation[i], nnzofWLog[i],
				nnzofW[i]);
		fprintf(fp, "%d,%f,%f,%f,%f,%d,%d\n", i, validationLog[i],
				validation[i], finalvalidationLog[i], finalvalidation[i],
				nnzofWLog[i], nnzofW[i]);
	}
	fclose(fp);
}
示例#12
0
double *integration_eam_p(double *y, void *params, unsigned int S, int *stab, Params *p)
{

    char *buffer = (char*)malloc(100);
    sprintf(buffer, "%s/cycle%d.txt",p->folder,p->cycle_num);
    FILE *outname = fopen(buffer,"w");
    fprintf(outname,"\n%d\t",0);
    print_double_array(y,S,outname);
  

    // DEFINE AND INITIALIZE THE VARIABLES
    //------------------------------------
    int theend = 0;
    int theend2 = 0;
    double *biomass = (double*)malloc(S * sizeof(double));   // array for the temporal mean biomasses calculation
    double *minis = (double*)malloc(S * sizeof(double));     // array for the minimum densities
    double *maxis = (double*)malloc(S * sizeof(double));     // array for the maximum densities
    double *check1 = (double*)malloc(S * sizeof(double));    // array for the minimum densities
    double *check2 = (double*)malloc(S * sizeof(double));    // array for the maximum densities

    for(int i = 0 ; i < S ; ++i)
    {
	biomass[i] = 0.0;
    }
    memcpy(minis,biomass,S * sizeof(double));
    memcpy(maxis,biomass,S * sizeof(double));
    memcpy(check1,biomass,S * sizeof(double));
    memcpy(check2,biomass,S * sizeof(double));


    // PRINT THE INITIAL DENSITIES
    //----------------------------
    //display_densities(y,S,0);    

    // CALCULATION OF INTERACTIONS AND PREFERENCES
    //--------------------------------------------
    interaction((isll*)params,p);

    // DEFINE THE INTEGRATION SYSTEM WITH THE FUNCTION FOO_EAM
    //--------------------------------------------------------
    gsl_odeiv2_system sys = {foo_eam, NULL,S, params};
    //gsl_odeiv2_system sys = {foo_eam, jac_eam,S, params};

        // DEFINE THE SOLVER
    //------------------
    //gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk8pd, 1e-15, 1e-15,1e-20);
    gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkck, 1e-12, 1e-12,0);         // the faster
    //gsl_odeiv2_driver *d = gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rkf45, 1e-15, 1e-15,1e-20);    

    // STARTING TIME
    //--------------
    double t = 0.0;
    int a = EAM_TMAX - EAM_B;
    int b = EAM_B * 3;


    //-------------//
    // INTEGRATION //
    //------------ //

    // MODE WITH CHECKING TIME
    //------------------------
    if(EAM_EQ_MOD)  
    {
        // RUN UNTIL CHECKING TIME
        //------------------------
      runi_p(d,&t,y,1,(EAM_TE_START) ,S,outname);

        int run_cycles = (int)((a - EAM_TE_START) / b);                                                       // number of checking times
	int left_time = EAM_TMAX - (run_cycles * b);                                                          // time left to reach EAM_TMAX if not stopped before
	int index = EAM_TE_START + 1;
	for(int i = 0 ; i < run_cycles ; ++i)
	{
	    theend = index + EAM_B - 1;
	    runb_p(d,&t,y,check1,index, theend,S,outname);
	    theend2 = theend  + EAM_B;
	    runb_p(d,&t,y,check2,(theend + 1), theend2,S,outname);                                      

	    // IF STABILITY IS REACHED, STOP THE INTEGRATION 
	    //----------------------------------------------
	    if(compare_dd(check1,check2,S) == 1)
	    {
		p->time_eq = index + b - 1;
	        runb_plus_stab_p(d,&t,y,biomass,minis,maxis,(theend2 + 1),p->time_eq,S,stab,outname);

                // FREE THE MEMORY
                //----------------
		free(minis);
		free(maxis);
		free(check1);
		free(check2);
		gsl_odeiv2_driver_free(d);
		fclose(outname);
		free(buffer);
		return(biomass);
	    }
	    else
	      {
		runi_p(d,&t,y,(theend2 + 1),(index + b - 1),S,outname);
	      }
	    index += b;
	}

	// IF STABILITY IS NOT REACHED, FINISH THE INTEGRATION AND CALCULATE THE TEMPORAL BIOMASS
	//---------------------------------------------------------------------------------------
        if(left_time != 0)runi_p(d,&t,y,index,(a-1) ,S,outname);
	runb_p(d,&t,y,biomass,a,EAM_TMAX,S,outname);
	*stab = 0;
	p->time_eq =  EAM_TMAX;     
    }
    else  

    // MODE WITHOUT CHECKING TIME
    //---------------------------           
    {
        int index = EAM_TMAX - b;
        runi_p(d,&t,y,1,index,S,outname);
	theend = index + EAM_B;
	runb_p(d,&t,y,check1,(index + 1), theend,S,outname);
	theend2 = theend  + EAM_B;
	runb_p(d,&t,y,check2,(theend + 1), theend2,S,outname); 
	if(compare_dd(check1,check2,S) == 1)
	{
	    runb_plus_stab_p(d,&t,y,biomass,minis,maxis,(theend2 + 1),EAM_TMAX,S,stab,outname);
	}
	else
	{
	    runb_p(d,&t,y,biomass,(theend2 + 1),EAM_TMAX,S,outname);
	    *stab = 0;
	}
	p->time_eq =  EAM_TMAX;
    }
        // FREE THE MEMORY
        //----------------
        free(minis);
        free(maxis);
        free(check1);
        free(check2);
        gsl_odeiv2_driver_free(d);
	fclose(outname);
	free(buffer);
        return (biomass);

}