void Test_maxmin_pw(CuTest * tc){
    
    printf("Testing functions: absmax, max and min of pw \n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,maxminpoly,NULL);

    // approximation
    double lb = -1.0, ub = 2.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    opoly_t pl = piecewise_poly_approx1_adapt(opts,fw);
    
    double loc;
    double max = piecewise_poly_max(pl, &loc);
    double min = piecewise_poly_min(pl, &loc);
    double absmax = piecewise_poly_absmax(pl, &loc,NULL);

    CuAssertDblEquals(tc, 1.0, max, 1e-10);
    CuAssertDblEquals(tc, -1.0, min, 1e-10);
    CuAssertDblEquals(tc, 1.0, absmax, 1e-10);

    piecewise_poly_free(pl);
    fwrap_destroy(fw);
    pw_poly_opts_free(opts);
}
void Test_serialize_double(CuTest * tc){

    printf("doubleserialization tests and experiments \n");
    printf("size of double in bytes= %zu\n",sizeof(double));
    printf("DBL_MAX = %G \n",DBL_MAX);
    printf("DBL_MIN = %G \n",DBL_MIN);
    printf("DBL_EPSILON = %G\n",DBL_EPSILON);

    
    double a = 20423.231;
    double value;
    unsigned char buffer[sizeof(double)];
    
    serialize_double(buffer,a);
    deserialize_double(buffer,&value);
    CuAssertDblEquals(tc,value,a,0.0);
    a = DBL_MAX;
    serialize_double(buffer,a);
    deserialize_double(buffer,&value);
    CuAssertDblEquals(tc,value,a,0.0);
    a = 0.0;
    serialize_double(buffer,a);
    deserialize_double(buffer,&value);
    CuAssertDblEquals(tc,value,a,0.0);
}
void Test_lin_elem_exp_orth_basis(CuTest * tc)
{
    printf("Testing function: lin_elem_exp_orth_basis\n");
    double lb = -2.0;
    double ub = 0.2;
    size_t N = 100;
    double * x = linspace(lb,ub,N);
    struct LinElemExpAopts * opts = lin_elem_exp_aopts_alloc(N,x);
    
    /* double * coeff = calloc_double(N); */
    le_t f[100];
    for (size_t ii = 0; ii < N; ii++){
        f[ii] = NULL;// lin_elem_exp_init(N,x,coeff);
    }

    lin_elem_exp_orth_basis(N,f,opts);
    for (size_t ii = 0; ii < N; ii++){
        for (size_t jj = 0; jj < N; jj++){
            double val = lin_elem_exp_inner(f[ii],f[jj]);
            if (ii == jj){
                CuAssertDblEquals(tc,1.0,val,1e-15);
            }
            else{
                CuAssertDblEquals(tc,0.0,val,1e-15);
            }
        }
    }

    for (size_t ii = 0; ii < N; ii++){
        LINELEM_FREE(f[ii]);
    }
    free(x); x = NULL;
    /* free(coeff); coeff = NULL; */
    lin_elem_exp_aopts_free(opts);
}
Beispiel #4
0
void testVectorMalloc(CuTest* tc) {
  vector v = vectorMalloc();
  //CuAssertTrue(tc, NULL != v);
  CuAssertDblEquals(tc, 0.0, v.x, DELTA);
  CuAssertDblEquals(tc, 0.0, v.y, DELTA);
  CuAssertDblEquals(tc, 0.0, v.z, DELTA);
}
Beispiel #5
0
void Testwishart(CuTest* tc) {
  /*set a non-trivial location matrix*/
  gsl_matrix* V = gsl_matrix_alloc(DIM, DIM);
  gsl_matrix_set_identity(V);
  gsl_matrix_scale(V, V0);
  for(size_t i=0; i<DIM; i++) for(size_t j=i+1; j < DIM; j++) {
      gsl_matrix_set(V, i, j, 1.0);
      gsl_matrix_set(V, j, i, 1.0);
  }
  
  p = mcmclib_wishart_lpdf_alloc(V, P);
  x = gsl_vector_alloc(DIM * DIM);
  gsl_matrix_view X_v = gsl_matrix_view_vector(x, DIM, DIM);
  gsl_matrix* X = &(X_v.matrix);
  gsl_matrix_set_all(X, 0.2);
  for(size_t i=0; i<DIM; i++)
    gsl_matrix_set(X, i, i, 1.0);

  CuAssertDblEquals(tc, -7.152627, lpdf(1.0), TOL);
  CuAssertDblEquals(tc, -29.549142, lpdf(0.5), TOL);
  CuAssertDblEquals(tc, 13.380252, lpdf(2.0), TOL);

  mcmclib_wishart_lpdf_free(p);
  gsl_vector_free(x);
  gsl_matrix_free(V);
}
Beispiel #6
0
void test_sp_proj_module(CuTest* tc)
{
  int size = 2;
  Image * a = sp_image_alloc(size,size,1);
  for(int i = 0;i<sp_image_size(a);i++){
    a->image->data[i] = sp_cinit(p_drand48(),p_drand48());
  }
  Image * exp = sp_image_alloc(size,size,1);
  for(int i = 0;i<sp_image_size(a);i++){
    exp->image->data[i] = sp_cinit(p_drand48(),0);
  }

  for(int i = 0;i<sp_image_size(a);i++){
    exp->mask->data[i] = 1;
  }


  Image * proj = sp_proj_module(a,exp,SpOutOfPlace);

  for(int i = 0;i<sp_image_size(a);i++){
    /* Check that the magnitude is the same as the experimental */
    CuAssertDblEquals(tc,sp_cabs(proj->image->data[i]),sp_cabs(exp->image->data[i]),fabs(2*REAL_EPSILON*sp_cabs(exp->image->data[i])));
    /* Check that the phase is the same as the input */
    CuAssertDblEquals(tc,sp_carg(proj->image->data[i]),sp_carg(a->image->data[i]),fabs(2*REAL_EPSILON*sp_carg(a->image->data[i])));
  }
  PRINT_DONE;
}
Beispiel #7
0
void Testiwishart(CuTest* tc) {
  /*set a non-trivial location matrix*/
  gsl_matrix* V = gsl_matrix_alloc(DIM, DIM);
  gsl_matrix_set_identity(V);
  gsl_matrix_add_constant(V, 1.0);
  
  p = mcmclib_iwishart_lpdf_alloc(V, P);
  x = gsl_vector_alloc(DIM * DIM);
  gsl_matrix_view X_v = gsl_matrix_view_vector(x, DIM, DIM);
  gsl_matrix* X = &(X_v.matrix);
  gsl_matrix_set_identity(X);
  gsl_matrix_add_constant(X, 1.0);

  /*check for side-effects*/
  double tmp = lpdf(1.0);
  CuAssertTrue(tc, tmp == lpdf(1.0));

  CuAssertDblEquals(tc, -18.188424, lpdf(1.0), TOL);
  CuAssertDblEquals(tc, 49.59203, lpdf(0.5), TOL);
  CuAssertDblEquals(tc, -88.468878, lpdf(2.0), TOL);

  mcmclib_iwishart_lpdf_free(p);
  gsl_vector_free(x);
  gsl_matrix_free(V);
}
void Test_pw_approx_nonnormal(CuTest * tc){

    printf("Testing function: piecewise_poly_approx on (a,b)\n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,Sin3xTx2,NULL);

    // approximation
    double lb=-3.0, ub=2.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    size_t N = 15;
    double * pts = linspace(lb,ub,N);
    pw_poly_opts_set_pts(opts,N,pts);
    opoly_t pw = piecewise_poly_approx1(opts,fw);

    double lb1 = piecewise_poly_lb(pw->branches[0]);
    double ub1 = piecewise_poly_ub(pw->branches[0]);
    CuAssertDblEquals(tc,pts[0],lb1,1e-14);
    CuAssertDblEquals(tc,pts[1],ub1,1e-14);

    // compute error
    double abs_err;
    double func_norm;
    compute_error_vec(lb,ub,1000,pw,Sin3xTx2,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);

    fwrap_destroy(fw);
    POLY_FREE(pw);
    pw_poly_opts_free(opts);
    free(pts);
}
void test_sorting(CuTest* tc) {
	double flux[] = { 50, 100, 50, 100, 20, 20, 40, 40 };
	double bg[]   = {  0,  10, 10,   0, 10,  0,  5,  0 };

	int trueorder[] = { 4, 5, 7, 6, 2, 0, 1, 3 };

	int i, N;
	starxy_t* s;
	char* infn = "/tmp/test-resort-xylist";
	char* outfn = "/tmp/test-resort-xylist-out";
	xylist_t* xy;

	xylist_t* xy2;
	starxy_t* s2;

	xy = xylist_open_for_writing(infn);
	CuAssertPtrNotNull(tc, xy);
    xylist_set_include_flux(xy, TRUE);
    xylist_set_include_background(xy, TRUE);
	if (xylist_write_primary_header(xy) ||
		xylist_write_header(xy)) {
		CuFail(tc, "write header");
	}

	N = sizeof(flux) / sizeof(double);
	s = starxy_new(N, TRUE, TRUE);

	for (i=0; i<N; i++) {
		starxy_setx(s, i, random()%1000);
		starxy_sety(s, i, random()%1000);
	}
	starxy_set_flux_array(s, flux);
	starxy_set_bg_array(s, bg);
	if (xylist_write_field(xy, s) ||
		xylist_fix_header(xy) ||
		xylist_fix_primary_header(xy) ||
		xylist_close(xy)) {
		CuFail(tc, "close xy");
	}

	CuAssertIntEquals(tc, 0,
					  resort_xylist(infn, outfn, NULL, NULL, TRUE));

	xy2 = xylist_open(outfn);
	s2 = xylist_read_field(xy2, NULL);
	CuAssertPtrNotNull(tc, s2);

	CuAssertPtrNotNull(tc, s2->x);
	CuAssertPtrNotNull(tc, s2->y);
	CuAssertPtrNotNull(tc, s2->flux);
	CuAssertPtrNotNull(tc, s2->background);

	for (i=0; i<N; i++) {
		CuAssertDblEquals(tc, s->x[trueorder[i]], s2->x[i], 1e-6);
		CuAssertDblEquals(tc, s->y[trueorder[i]], s2->y[i], 1e-6);
		CuAssertDblEquals(tc, s->flux[trueorder[i]], s2->flux[i], 1e-6);
		CuAssertDblEquals(tc, s->background[trueorder[i]], s2->background[i], 1e-6);
	}
}
Beispiel #10
0
void testMag(CuTest* tc) {
  vector v;
  assign(&v, 0.0, 3.0, 4.0);
  CuAssertDblEquals(tc, 5.0, mag(v), DELTA);
  assign(&v, 1.0, 1.0, 1.0);
  CuAssertDblEquals(tc, sqrt(3.0), mag(v), DELTA);
  //vectorFree(v);
}
Beispiel #11
0
void testAssign(CuTest* tc) {
  vector v;
  assign(&v, 1.0, 2.0, 3.0);
  CuAssertDblEquals(tc, 1.0, v.x, DELTA);
  CuAssertDblEquals(tc, 2.0, v.y, DELTA);
  CuAssertDblEquals(tc, 3.0, v.z, DELTA);
  //vectorFree(v);
}
Beispiel #12
0
void testEvent_getBranchLength(CuTest* testCase) {
	cactusEventTestSetup();
	CuAssertDblEquals(testCase, INT64_MAX, event_getBranchLength(rootEvent), 1.000);
	CuAssertDblEquals(testCase, 0.5, event_getBranchLength(internalEvent), 0.001);
	CuAssertDblEquals(testCase, 0.2, event_getBranchLength(leafEvent1), 0.001);
	CuAssertDblEquals(testCase, 1.3, event_getBranchLength(leafEvent2), 0.001);
	cactusEventTestTeardown();
}
Beispiel #13
0
void testEvent_getSubTreeBranchLength(CuTest* testCase) {
	cactusEventTestSetup();
	CuAssertDblEquals(testCase, 2.0, event_getSubTreeBranchLength(rootEvent), 0.001);
	CuAssertDblEquals(testCase, 1.5, event_getSubTreeBranchLength(internalEvent), 0.001);
	CuAssertDblEquals(testCase, 0.0, event_getSubTreeBranchLength(leafEvent1), 0.001);
	CuAssertDblEquals(testCase, 0.0, event_getSubTreeBranchLength(leafEvent2), 0.001);
	cactusEventTestTeardown();
}
void Test_unc6(CuTest * tc)
{
    printf("////////////////////////////////////////////\n");
    printf("\t Unconstrained Test: 6\n");


    size_t f1 = 5;
    struct UncTestProblem p = tprobs[f1];
    size_t dim = unc_test_problem_get_dim(&p);

    struct c3Opt * opt = c3opt_alloc(BFGS,dim);
    c3opt_add_objective(opt,unc_test_problem_eval,&p);


    c3opt_set_verbose(opt,0);
    /* c3opt_set_gtol(opt,1e-20); */
    /* c3opt_set_absxtol(opt,1e-20); */
    /* c3opt_set_relftol(opt,1e-14); */
    c3opt_set_maxiter(opt,400);
    c3opt_ls_set_maxiter(opt,1000);

    double * start = unc_test_problem_get_start(&p);
    /* printf("start\n"); */
    /* dprint(2,start); */

    double gerr = c3opt_check_deriv(opt,start,1e-8);
    CuAssertDblEquals(tc,0.0,gerr,1e-5);
    /* printf("gerr = %G\n",gerr); */
    
    double val;
    int res = c3opt_minimize(opt,start,&val);
    CuAssertIntEquals(tc,1,res>=0);

    double * soll = unc_test_problem_get_sol(&p);

    //minimum
    double err = fabs(soll[dim]-val);
    if (fabs(soll[dim]) > 1){
        err /= fabs(soll[dim]);
    }
    CuAssertDblEquals(tc,0.0,err,1e-4);
    
    //minimizer
    for (size_t ii = 0; ii < dim; ii++){
        CuAssertDblEquals(tc,soll[ii],start[ii],1e-2);
    }

    printf("\t\t *True* Minimum:               : %3.6E\n", soll[dim]);
    printf("\t\t Minimum Found:                : %3.6E\n\n",val);

    printf("\t\t Number of Function Evaluations: %zu\n",c3opt_get_nevals(opt));
    printf("\t\t Number of Gradient Evaluations: %zu\n",c3opt_get_ngvals(opt));
    printf("\t\t Number of iterations:           %zu\n",c3opt_get_niters(opt));
    printf("////////////////////////////////////////////\n");

    c3opt_free(opt); opt = NULL;
}
Beispiel #15
0
static void test_param_flt(CuTest * tc)
{
    struct param *par = 0;
    test_cleanup();
    CuAssertDblEquals(tc, 13, get_param_flt(par, "foo", 13), 0.01);
    set_param(&par, "foo", "23.0");
    set_param(&par, "bar", "42.0");
    CuAssertDblEquals(tc, 23.0, get_param_flt(par, "foo", 0.0), 0.01);
    CuAssertDblEquals(tc, 42.0, get_param_flt(par, "bar", 0.0), 0.01);
}
Beispiel #16
0
void testNormalize(CuTest* tc) {
  vector v;
  assign(&v, 1.0, 1.0, 1.0);
  normalize(&v);
  CuAssertDblEquals(tc, 1.0, mag(v), DELTA);
  assign(&v, 123.0, -321.0, 1.23);
  normalize(&v);
  CuAssertDblEquals(tc, 1.0, mag(v), DELTA);
  //vectorFree(v);
}
Beispiel #17
0
void testCopy(CuTest* tc) {
  vector v1;
  vector v2;
  assign(&v1, 23.23, -99.32, 12334.23);
  copy(&v2, v1);
  CuAssertDblEquals(tc, v1.x, v2.x, DELTA);
  CuAssertDblEquals(tc, v1.y, v2.y, DELTA);
  CuAssertDblEquals(tc, v1.z, v2.z, DELTA);
  //vectorFree(v1);
  //vectorFree(v2);
}
Beispiel #18
0
void testAdd(CuTest* tc) {
  vector v1,v2,v3;
  //v1 = vectorMalloc(); v2 = vectorMalloc(); v3 = vectorMalloc();
  assign(&v1, 1.0, 2.0, 3.0);
  assign(&v2, 10.0, 20.0, 30.0);
  add(&v3, v1, v2);
  CuAssertDblEquals(tc, 11.0, v3.x, DELTA);
  CuAssertDblEquals(tc, 22.0, v3.y, DELTA);
  CuAssertDblEquals(tc, 33.0, v3.z, DELTA);
  //vectorFree(v1);  vectorFree(v2);  vectorFree(v3);
}
void Test_lin_elem_exp_inner2(CuTest * tc){

    printf("Testing function: lin_elem_exp_inner (2)\n");
    
    // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    struct Fwrap * fw2 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw2,TwoPowX3,NULL);

    double lb=-2.0,ub=3.0;
    
    size_t N1 = 10;
    double * p1 = linspace(lb,0.5,N1);
    double f[1000];
    fwrap_eval(N1,p1,f,fw1);

    size_t N2 = 20;
    double * p2 = linspace(0.0,ub,N2);
    double g[1000];
    fwrap_eval(N2,p2,g,fw2);
    
    le_t fa = lin_elem_exp_init(N1,p1,f);
    le_t fb = lin_elem_exp_init(N2,p2,g);
        
    double intis = lin_elem_exp_inner(fa,fb);
    double intis2 = lin_elem_exp_inner(fb,fa);

    size_t ntest = 10000000;
    double * xtest = linspace(lb,ub,ntest);
    double integral = 0.0;
    for (size_t ii = 0; ii < ntest; ii++){
        integral += (LINELEM_EVAL(fa,xtest[ii]) *
                     LINELEM_EVAL(fb,xtest[ii]));
    }
    integral /= (double) ntest;
    integral *= (ub - lb);
    double intshould = integral;
    double diff = fabs(intshould-intis)/fabs(intshould);
    CuAssertDblEquals(tc, 0.0, diff, 1e-6);
    CuAssertDblEquals(tc,intis,intis2,1e-15);
    free(xtest);

    LINELEM_FREE(fa);
    LINELEM_FREE(fb);
    free(p1);
    free(p2);
    fwrap_destroy(fw1);
    fwrap_destroy(fw2);
    
}
Beispiel #20
0
void test_nullspace_gsl(CuTest* tc) {
    int i, j;
    gsl_matrix* A;
    gsl_matrix* V;
    gsl_matrix* U;
    gsl_vector* S;
    gsl_matrix_view vcov;
    double cov[4] = {-0.93390448957619598, 1.8004204750064117, 0, 0};
    int M=2, N=2;

    A = gsl_matrix_alloc(M, N);
    S = gsl_vector_alloc(N);
    V = gsl_matrix_alloc(N, N);

    vcov = gsl_matrix_view_array(cov, M, N);
    gsl_matrix_memcpy(A, &(vcov.matrix));

    gsl_linalg_SV_decomp_jacobi(A, V, S);
    // the U result is written to A.
    U = A;

    printf("S = [");
    for (i=0; i<N; i++)
        printf(" %g", gsl_vector_get(S, i));
    printf(" ]\n");

    printf("U = [");
    for (j=0; j<M; j++) {
        if (j)
            printf("    [");
        for (i=0; i<N; i++)
            printf(" %g", gsl_matrix_get(U, j, i));
        printf(" ]\n");
    }

    printf("V = [");
    for (j=0; j<N; j++) {
        if (j)
            printf("    [");
        for (i=0; i<N; i++)
            printf(" %g", gsl_matrix_get(V, j, i));
        printf(" ]\n");
    }

    CuAssertDblEquals(tc, 0, gsl_vector_get(S, 1), 1e-6);
    CuAssertDblEquals(tc, 2.02822373, gsl_vector_get(S, 0), 1e-6);

    gsl_matrix_free(A);
    gsl_matrix_free(V);
    gsl_vector_free(S);

}
Beispiel #21
0
void test_parse_center_gets_1_2i_neg_returns_1_2_neg(CuTest *tc) {
    double actual_c_re;
    double actual_c_im;
    char param[] = "-1-2i";
    double delta = 0.00001;

    double expected_c_re = -1;
    double expected_c_im = -2;
    int result = parse_center( param, &actual_c_re, &actual_c_im );

    CuAssertDblEquals(tc, expected_c_re, actual_c_re, delta);
    CuAssertDblEquals(tc, expected_c_im, actual_c_im, delta);
}
Beispiel #22
0
void test_sp_proj_module_histogram(CuTest* tc)
{
  int size = 1000;
  float base = 10;
  Image * a = sp_image_alloc(size,size,1);
  for(int i = 0;i<sp_image_size(a);i++){
    a->image->data[i] = sp_cinit(sqrt(base)*p_drand48(),sqrt(base)*p_drand48());
  }
  Image * exp = sp_image_alloc(size,size,1);
  for(int i = 0;i<sp_image_size(a);i++){
    /* We have to add base to make sure the values are much above std_dev */
    exp->image->data[i] = sp_cinit(base+p_drand48(),0);
  }

  for(int i = 0;i<sp_image_size(a);i++){
    exp->mask->data[i] = 1;
  }

  Image * std_dev = sp_image_alloc(size,size,1);
  for(int i = 0;i<sp_image_size(a);i++){
    /* We have to keep the standard deviation small */
    std_dev->image->data[i] = sp_cinit(0.1,0);
  }

  Image * norm_sq_int = sp_image_alloc(size,size,1);

  Image * proj = sp_proj_module_histogram(a,exp,std_dev);
  for(int i = 0;i<sp_image_size(a);i++){
    /* Calculate normalized square intensities */
    sp_real(norm_sq_int->image->data[i]) = (sp_cabs2(exp->image->data[i])-sp_cabs2(proj->image->data[i]))/sp_real(std_dev->image->data[i]);
    /* Check that the phase is the same as the input */
    CuAssertDblEquals(tc,sp_carg(proj->image->data[i]),sp_carg(a->image->data[i]),1e-3);
  }
  /* Check that the average normal of the normaized squared magnitudes is 0*/
  double average = 0;
  for(int i = 0;i<sp_image_size(a);i++){
    average += sp_real(norm_sq_int->image->data[i]);
  }
  average /= sp_image_size(a);  

  double variance = 0;
  for(int i = 0;i<sp_image_size(a);i++){
    variance += (average-sp_real(norm_sq_int->image->data[i]))*(average-sp_real(norm_sq_int->image->data[i]));
  }
  variance /= sp_image_size(a);  
  CuAssertDblEquals(tc,average,0,1.0/size);
  CuAssertDblEquals(tc,variance,1,1.0/size);
  PRINT_DONE;
}
static void checkIsValidReference(CuTest *testCase, stList *reference,
        double totalScore) {
    stList *chosenEdges = convertReferenceToAdjacencyEdges(reference);
    //Check that everyone has a partner.
    CuAssertIntEquals(testCase, nodeNumber, stList_length(chosenEdges) * 2);
    stSortedSet *nodes = stSortedSet_construct3((int(*)(const void *, const void *)) stIntTuple_cmpFn,
            (void(*)(void *)) stIntTuple_destruct);
    for (int64_t i = 0; i < nodeNumber; i++) {
        stSortedSet_insert(nodes, stIntTuple_construct1( i));
    }
    checkEdges(chosenEdges, nodes, 1, 0);
    //Check that the score is correct
    double totalScore2 = calculateZScoreOfReference(reference, nodeNumber, zMatrix);
    CuAssertDblEquals(testCase, totalScore2, totalScore, 0.000001);
    //Check that the stubs are properly connected.
    stList *allEdges = stList_copy(chosenEdges, NULL);
    stList_appendAll(allEdges, stubs);
    stList_appendAll(allEdges, chains);
    stList *components = getComponents(allEdges);
    CuAssertIntEquals(testCase, stList_length(stubs), stList_length(reference));
    CuAssertIntEquals(testCase, stList_length(stubs), stList_length(components));
    //Cleanup
    stList_destruct(components);
    stSortedSet_destruct(nodes);
    stList_destruct(allEdges);
    stList_destruct(chosenEdges);
}
void Test_c3axpy(CuTest * tc)
{
    printf("Testing Function: c3axpy\n");
    struct BoundingBox * bds = bounding_box_init_std(5);
    struct FunctionTrain * ft1 = function_train_cross(func1,NULL,bds,NULL,NULL,NULL);
    struct FunctionTrain * ft2 = function_train_cross(func2,NULL,bds,NULL,NULL,NULL);

    c3axpy(3.0,ft1,&ft2,1e-10);

    size_t ii,jj, N = 10000;
    double pt[5];
    double sum = 0.0;
    for (ii = 0; ii < N; ii++) {
        for (jj = 0; jj < 5; jj++) {
            pt[jj] = randu()*2.0-1.0;
        }
        double val = func1(pt,NULL)*3.0 + func2(pt,NULL);
        double vala = function_train_eval(ft2,pt);
        double diff = val-vala;
        sum += pow(diff,2);
    }

    CuAssertDblEquals(tc, 0.0, sum / N, 1e-9);
    bounding_box_free(bds);
    bds = NULL;
    function_train_free(ft1);
    ft1 = NULL;
    function_train_free(ft2);
    ft2 = NULL;
}
Beispiel #25
0
static void
test_default_similarity( CuTest* tc )
{
    apr_pool_t* pool;
    lcn_similarity_t* sim;
    int i;

    LCN_TEST( apr_pool_create( &pool, main_pool ) );

    LCN_TEST( lcn_default_similarity_create( &sim, pool ) );

    for( i = 0; i < 256; i++ )
    {
        CuAssertTrue( tc,
                      test_norm_table[i] - 
                      lcn_similarity_norm_decoder( sim )[i] < 0.00001f );

        CuAssertTrue( tc,
                      lcn_similarity_norm_decoder( sim )[i] -
                      test_norm_table[i] < 0.0001f );
    }
    
    CuAssertDblEquals( tc, 
                       0.341190,
                       lcn_similarity_query_norm( sim, 8.590276 ),
                       0.00001f );
}
void Test_pw_approx_adapt_weird(CuTest * tc){

    printf("Testing function: piecewise_poly_approx1_adapt on (a,b)\n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,Sin3xTx2,NULL);

    // approximation
    double lb=-2.0, ub = -1.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    opoly_t pw = piecewise_poly_approx1_adapt(opts,fw);

    // this is just to make sure no memory errors or segfaults
    size_t nbounds;
    double * bounds = NULL;
    piecewise_poly_boundaries(pw,&nbounds,&bounds,NULL);
    //dprint(nbounds,bounds);
    free(bounds);

    // compute error
    double abs_err;
    double func_norm;
    compute_error_vec(lb,ub,1000,pw,Sin3xTx2,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);

    fwrap_destroy(fw);
    POLY_FREE(pw);
    pw_poly_opts_free(opts);
}
void Test_pw_flatten(CuTest * tc){
   
    printf("Testing functions: piecewise_poly_flatten \n");
    
    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,pw_disc,NULL);

    // approximation
    double lb=-5.0, ub = 1.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    pw_poly_opts_set_minsize(opts,1e-5);
    opoly_t pw = piecewise_poly_approx1_adapt(opts,fw);

    size_t nregions = piecewise_poly_nregions(pw);
    int isflat = piecewise_poly_isflat(pw);
    CuAssertIntEquals(tc,0,isflat);
    piecewise_poly_flatten(pw);
    CuAssertIntEquals(tc,nregions,pw->nbranches);
    isflat = piecewise_poly_isflat(pw);
    CuAssertIntEquals(tc,1,isflat);
    
    // compute error
    double abs_err;
    double func_norm;
    compute_error_vec(lb,ub,1000,pw,pw_disc,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);

    fwrap_destroy(fw);
    POLY_FREE(pw);
    pw_poly_opts_free(opts);
}
void Test_pap1(CuTest * tc){

    printf("Testing function: approx (1/1) \n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,pap1,NULL);

    // approximation
    double lb = -5.0, ub = 5.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    /* pw_poly_opts_set_minsize(opts,1e-1); */
    /* pw_poly_opts_set_tol(opts,1e-5); */
    /* pw_poly_opts_set_maxorder(opts,5); */
    opoly_t pw = piecewise_poly_approx1_adapt(opts,fw);

    // error
    double abs_err;
    double func_norm;
    compute_error_vec(lb,ub,1000,pw,pap1,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);

    fwrap_destroy(fw);
    pw_poly_opts_free(opts);
    POLY_FREE(pw);
}
void Test_pw_inner(CuTest * tc){

    printf("Testing function: piecewise_poly_inner\n");

    // function
    struct Fwrap * fw1 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw1,powX2,NULL);

    struct Fwrap * fw2 = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw2,TwoPowX3,NULL);

    double lb = -2.0, ub = 3.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    opoly_t cpoly = piecewise_poly_approx1_adapt(opts,fw1);
    opoly_t cpoly2 = piecewise_poly_approx1_adapt(opts,fw2);

    double intshould = (pow(ub,6) - pow(lb,6))/3;
    double intis = piecewise_poly_inner(cpoly,cpoly2);
    CuAssertDblEquals(tc, intshould, intis, 1e-10);
    
    POLY_FREE(cpoly);
    POLY_FREE(cpoly2);
    pw_poly_opts_free(opts);
    fwrap_destroy(fw1);
    fwrap_destroy(fw2);
}
void Test_pw_derivative(CuTest * tc){

    printf("Testing function: piecewise_poly_deriv  on (a,b)\n");

    // function
    struct Fwrap * fw = fwrap_create(1,"general-vec");
    fwrap_set_fvec(fw,Sin3xTx2,NULL);

    // approximation
    double lb = -2.0, ub = -1.0;
    struct PwPolyOpts * opts = pw_poly_opts_alloc(LEGENDRE,lb,ub);
    opoly_t cpoly = piecewise_poly_approx1_adapt(opts,fw);
    opoly_t der = piecewise_poly_deriv(cpoly);
    
    // error
    double abs_err;
    double func_norm;
    compute_error(lb,ub,1000,der,funcderiv,NULL,&abs_err,&func_norm);
    double err = abs_err / func_norm;
    CuAssertDblEquals(tc, 0.0, err, 1e-13);
    
    POLY_FREE(cpoly);
    POLY_FREE(der);
    pw_poly_opts_free(opts);
    fwrap_destroy(fw);
}