Ejemplo n.º 1
0
gboolean
RawBound01Thread ( gpointer gdata ) {
	GuiData *gd = GetGuiData ();

	if ( gd->gauss == NULL ) return TRUE;
	if ( gd->raw == NULL ) {
		gint width = cairo_image_surface_get_width ( gd->gauss );
		gint height = cairo_image_surface_get_height ( gd->gauss );
		gsl_matrix *pic = gsl_matrix_alloc ( height, width );
		unsigned char *data = cairo_image_surface_get_data ( gd->gauss );
		for ( gint row=0; row<height; row++ ) {
			for ( gint col=0; col<width; col++ ) {
				gsl_matrix_set ( pic, row, col, data[row*width+col] ); 
			}
		}
		gsl_matrix *h = gsl_matrix_alloc ( 3, 3 );
		gsl_matrix_set_all ( h, 0 );
		gsl_matrix_set ( h, 0, 0, 1 );
		gsl_matrix_set ( h, 0, 1, 2 );
		gsl_matrix_set ( h, 0, 2, 1 );
		gsl_matrix_set ( h, 2, 0, -1 );
		gsl_matrix_set ( h, 2, 1, -2 );
		gsl_matrix_set ( h, 2, 2, -1 );
		gsl_matrix *gx = Convolution ( pic, h );
		gsl_matrix_set_all ( h, 0 );
		gsl_matrix_set ( h, 0, 0, 1 );
		gsl_matrix_set ( h, 1, 0, 2 );
		gsl_matrix_set ( h, 2, 0, 1 );
		gsl_matrix_set ( h, 0, 2, -1 );
		gsl_matrix_set ( h, 1, 2, -2 );
		gsl_matrix_set ( h, 2, 2, -1 );
		gsl_matrix *gy = Convolution ( pic, h );

		gsl_matrix *raw = gsl_matrix_alloc ( height, width );
		ignore -= 10;
		for ( gint row=0; row<height; row++ ) {
			for ( gint col=0; col<width; col++ ) {
				if ( row < ignore || col < ignore 
						|| row > height - ignore 
						|| col > width - ignore ) {
					gsl_matrix_set ( raw, row, col, 0 );
				} else {
					gsl_matrix_set ( raw, row, col, 
							pow ( gsl_matrix_get ( gx, row, col ), 2 )
							+ pow ( gsl_matrix_get ( gy, row, col ), 2 ) );
				}
			}
		}
		ignore += 10;

		gfloat val = 0;
		gint start1 = 0;

		/*
		// 从右到左
		for ( gint row=height/2; row<height; row++ ) {
			if ( gsl_matrix_get ( raw, row, width-ignore ) > val ) {
				val = gsl_matrix_get ( raw, row, width-ignore );
				start1 = row;
			}
		}
		gint start2 = start1 - 1;
		val = gsl_matrix_get ( raw, start1-1, width-ignore-1 );
		if ( val < gsl_matrix_get ( raw, start1, width-ignore-1 ) ) {
			val = gsl_matrix_get ( raw, start1, width-ignore-1 );
			start2++;
		}
		if ( val < gsl_matrix_get ( raw, start1+1, width-ignore-1 ) ) {
			val = gsl_matrix_get ( raw, start1+1, width-ignore-1 );
			start2++;
		}

		gsl_vector *v = gsl_vector_alloc (3);
		gsl_vector_set ( v, 0, start1 );
		gsl_vector_set ( v, 1, width-ignore );
		gsl_vector_set ( v, 2, gsl_matrix_get ( raw, start1, width-ignore ) );
		gd->rawlist = g_slist_append ( gd->rawlist, v );
		gsl_vector *v1 = gsl_vector_alloc (3);
		gsl_vector_set ( v1, 0, start2 );
		gsl_vector_set ( v1, 1, width-ignore-1 );
		gsl_vector_set ( v1, 2, gsl_matrix_get ( raw, start2, width-ignore-1 ) );
		gd->rawlist = g_slist_append ( gd->rawlist, v1 );

		*/

		// 从左到右
		for ( gint row=height/2; row<height; row++ ) {
			if ( gsl_matrix_get ( raw, row, ignore ) > val ) {
				val = gsl_matrix_get ( raw, row, ignore );
				start1 = row;
			}
		}
		gint start2 = start1 - 1;
		val = gsl_matrix_get ( raw, start1-1, ignore+1 );
		if ( val < gsl_matrix_get ( raw, start1, ignore+1 ) ) {
			val = gsl_matrix_get ( raw, start1, ignore+1 );
			start2++;
		}
		if ( val < gsl_matrix_get ( raw, start1+1, ignore+1 ) ) {
			val = gsl_matrix_get ( raw, start1+1, ignore+1 );
			start2++;
		}

		gsl_vector *v = gsl_vector_alloc (3);
		gsl_vector_set ( v, 0, start1 );
		gsl_vector_set ( v, 1, ignore );
		gsl_vector_set ( v, 2, gsl_matrix_get ( raw, start1, ignore ) );
		gd->rawlist = g_slist_append ( gd->rawlist, v );
		gsl_vector *v1 = gsl_vector_alloc (3);
		gsl_vector_set ( v1, 0, start2 );
		gsl_vector_set ( v1, 1, ignore+1 );
		gsl_vector_set ( v1, 2, gsl_matrix_get ( raw, start2, ignore+1 ) );
		gd->rawlist = g_slist_append ( gd->rawlist, v1 );

		for ( gint w=0; w<weightlen; w++ ) {
			weight = weights[w];
			while ( FindSinglePoint( pic, raw, width, height, &v, &v1, gd ) );
			gint length = g_slist_length ( gd->rawlist );
			if ( gsl_vector_get ( v1, 1 ) > width - 2*ignore ) break;
			for ( int i=length-1; i>1; i-- ) {
				gsl_vector *vv = g_slist_nth_data ( gd->rawlist, i );
				gd->rawlist = g_slist_remove ( gd->rawlist, vv );
				gsl_vector_free ( vv );
			}
			v = g_slist_nth_data ( gd->rawlist, 0 );
			v1 = g_slist_nth_data ( gd->rawlist, 1 );
		}
		/*
		printf ( "value:%f\n", gsl_matrix_get ( raw, 1660, 51 ) );
		gsl_matrix_view vview = gsl_matrix_submatrix ( raw, 1660, 45, 10, 10 );
		DispMatrix ( (gsl_matrix*)&vview );
		*/

		gd->raw = cairo_image_surface_create ( 
					CAIRO_FORMAT_A8, width, height );
		cairo_surface_flush ( gd->raw );
		data = cairo_image_surface_get_data ( gd->raw );
		
		guint len = g_slist_length ( gd->rawlist );
		for ( int i=0; i<len; i++ ) {
			gsl_vector *v = g_slist_nth_data ( gd->rawlist, i );
			data[(int)(gsl_vector_get(v,0)*width+gsl_vector_get(v,1))] = 255;
		}
		cairo_surface_mark_dirty ( gd->raw );

		gsl_matrix_free ( pic );
		gsl_matrix_free ( gx );
		gsl_matrix_free ( gy );
		gsl_matrix_free ( h );
		gsl_matrix_free ( raw );
	} 

	gd->sCur = "粗边界后图片";
	gd->sRaw = "已粗边界";
	gd->cur = gd->raw;
	UpdateStatusbar ();
	return TRUE;
}
Ejemplo n.º 2
0
int main (void) {
	int i, j;
	int signum = 0;
	double determinant_m = 0.0;
	gsl_permutation  *m_LU_permutation = gsl_permutation_alloc(8);
	gsl_matrix *m_test = gsl_matrix_alloc(8, 8);
	gsl_matrix *m_original = gsl_matrix_alloc(8,8);
	gsl_matrix *m_inverted = gsl_matrix_alloc(8,8);
	
	/*for (i = 0; i < 8; i++){
		for(j = 0; j < 8; j++){
			gsl_matrix_set(m_test, i, j, 1);
		}
	}*/
	
	gsl_matrix_set(m_test, 0, 7, 1);
	gsl_matrix_set(m_test, 7,0, 1);
	gsl_matrix_set(m_test, 3,2, 5);
	gsl_matrix_set(m_test, 5,2, 5);


	for( i = 0; i < 8; i++){
		gsl_matrix_set(m_test, i, i, 3);
	}


	// set the oritinal one too
	gsl_matrix_memcpy(m_original, m_test); 	
	// now print the matrix out
	//print_matrix(m_test, 8, 8);
	
	printf("\n");
	print_matrix(m_original, 8,8);

	// simple matrix operations
	//gsl_matrix_add(m_test, m_test);
	//gsl_matrix_add_constant(m_test, 1.05);

	//print_matrix(m_test, 8,8);

	// creates the lu decomp of the matrix
	// need to allocate the permutation
	gsl_linalg_LU_decomp(m_test, m_LU_permutation, &signum);

	// calculate the determinant of m_test from the lu
	determinant_m = gsl_linalg_LU_det(m_test, signum);
	printf("det_m = %g\n", determinant_m);
	// invert the matrix
	gsl_linalg_LU_invert(m_test, m_LU_permutation, m_inverted);

	// multiply the inverse and the matrix together  (can't use the LU one bc it's f****d up)
	// m_test should be I
	gsl_blas_dgemm( CblasNoTrans,CblasNoTrans , 1.0, m_original, m_inverted, 0.0, m_test);

	print_matrix(m_test, 8,8);


	gsl_permutation_free(m_LU_permutation);
	gsl_matrix_free(m_inverted);
	gsl_matrix_free(m_test);
	return(0);
}
Ejemplo n.º 3
0
void ccl_MP_pinv(const double *A_in, int row, int col,double *invA) {
    gsl_matrix *V, *Sigma_pinv, *U, *A_pinv,*A;
    gsl_matrix *_tmp_mat = NULL;
    gsl_vector *_tmp_vec;
    gsl_vector *u;
    double x, cutoff;
    size_t i, j;
    int n = row;
    int m = col;
    bool was_swapped = false;
    double rcond = 1E-15;
    if (m > n) {
        /* libgsl SVD can only handle the case m <= n - transpose matrix */
        was_swapped = true;
        A = gsl_matrix_alloc(col,row);
        _tmp_mat = gsl_matrix_alloc(n, m);
        memcpy(_tmp_mat->data,A_in,m*n*sizeof(double));
        gsl_matrix_transpose_memcpy(A,_tmp_mat);
        i = m;
        m = n;
        n = i;
    }
    else{
        A = gsl_matrix_alloc(row,col);
        memcpy(A->data,A_in,row*col*sizeof(double));
    }

    /* do SVD */
    V = gsl_matrix_alloc(m, m);
    u = gsl_vector_alloc(m);
    _tmp_vec = gsl_vector_alloc(m);
    gsl_linalg_SV_decomp(A, V, u, _tmp_vec);
    gsl_vector_free(_tmp_vec);

    /* compute Σ⁻¹ */
    Sigma_pinv = gsl_matrix_alloc(m, n);
    gsl_matrix_set_zero(Sigma_pinv);
    cutoff = rcond * gsl_vector_max(u);

    for (i = 0; i < m; ++i) {
        if (gsl_vector_get(u, i) > cutoff) {
            x = 1. / gsl_vector_get(u, i);
        }
        else {
            x = 0.;
        }
        gsl_matrix_set(Sigma_pinv, i, i, x);
    }

    /* libgsl SVD yields "thin" SVD - pad to full matrix by adding zeros */
    U = gsl_matrix_alloc(n, n);
    gsl_matrix_set_zero(U);

    for (i = 0; i < n; ++i) {
        for (j = 0; j < m; ++j) {
            gsl_matrix_set(U, i, j, gsl_matrix_get(A, i, j));
        }
    }

    if (_tmp_mat != NULL) {
        gsl_matrix_free(_tmp_mat);
    }

    /* two dot products to obtain pseudoinverse */
    _tmp_mat = gsl_matrix_alloc(m, n);
    gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1., V, Sigma_pinv, 0., _tmp_mat);

    if (was_swapped) {
        A_pinv = gsl_matrix_alloc(n, m);
        gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1., U, _tmp_mat, 0., A_pinv);
    }
    else {
        A_pinv = gsl_matrix_alloc(m, n);
        gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1., _tmp_mat, U, 0., A_pinv);
    }
    memcpy(invA,A_pinv->data,row*col*sizeof(double));
    gsl_matrix_free(A);
    gsl_matrix_free(A_pinv);
    gsl_matrix_free(_tmp_mat);
    gsl_matrix_free(U);
    gsl_matrix_free(Sigma_pinv);
    gsl_vector_free(u);
    gsl_matrix_free(V);
}
Ejemplo n.º 4
0
void ccl_learn_lambda(const double * Un,const double *X,void (*J_func)(const double*,const int,double*),const int dim_b,const int dim_r,const int dim_n,const int dim_x,const int dim_u,LEARN_A_MODEL optimal){
    LEARN_A_MODEL model;
    double * centres,*var_tmp,*vec, *BX, *RnVn,*Rn,*Vn;
    double variance;
    int i,lambda_id;
    gsl_matrix *Un_,*X_;

    //     J_fun       = &Jacobian;
    model.dim_b = dim_b;
    model.dim_r = dim_r;
    model.dim_x = dim_x;
    model.dim_n = dim_n;
    model.dim_t = dim_r-1;
    model.dim_u = dim_u;
    // calculate model.var
    var_tmp = malloc(dim_u*sizeof(double));
    ccl_mat_var(Un,dim_u,dim_n,0,var_tmp);
    model.var = ccl_vec_sum(var_tmp,dim_u);
    free(var_tmp);

    Un_     = gsl_matrix_alloc(dim_u,dim_n);
    memcpy(Un_->data,Un,dim_u*dim_n*sizeof(double));
    X_ = gsl_matrix_alloc(dim_x,dim_n);
    memcpy(X_->data,X,dim_x*dim_n*sizeof(double));


    optimal.nmse = 1000000;

    gsl_vector* X_col = gsl_vector_alloc(dim_x);
    gsl_vector* Un_col = gsl_vector_alloc(dim_u);
    gsl_matrix* Vn_container = gsl_matrix_alloc(dim_r,dim_n);
    gsl_matrix_set_zero(Vn_container);
    double*     norm   = malloc(dim_n*sizeof(double));
    double*     J_x    = malloc(dim_r*dim_u*sizeof(double));
    gsl_vector* Jx_Un  = gsl_vector_alloc(dim_r);
    int*     id_keep   = malloc(dim_n*sizeof(double));
    for (i=0;i<dim_n;i++){
        gsl_matrix_get_col(X_col,X_,i);
        gsl_matrix_get_col(Un_col,Un_,i);
        J_func(X_col->data,dim_x,J_x);
        ccl_dot_product(J_x,dim_r,dim_u,Un_col->data,dim_u,1,Jx_Un->data);
        gsl_matrix_set_col(Vn_container,i,Jx_Un);
        norm[i] = gsl_blas_dnrm2(Jx_Un);
    }
    // here dim_n maybe change.
    int new_dim_n = ccl_find_index_double(norm,dim_n,2,1E-3,id_keep);
    model.dim_n = new_dim_n;

    Vn = malloc(dim_r*model.dim_n*sizeof(double));
    gsl_matrix Vn_ = gsl_matrix_view_array(Vn,dim_r,model.dim_n).matrix;
    gsl_matrix*X_new = gsl_matrix_alloc(dim_x,model.dim_n);
    gsl_matrix*Un_new = gsl_matrix_alloc(dim_u,model.dim_n);
    for (i=0;i<new_dim_n;i++){
        gsl_vector* Vn_tmp = gsl_vector_alloc(dim_r);
        gsl_matrix_get_col(Vn_tmp,Vn_container,id_keep[i]);
        gsl_matrix_set_col(&Vn_,i,Vn_tmp);
        gsl_vector_free(Vn_tmp);
        Vn_tmp = gsl_vector_alloc(dim_x);
        gsl_matrix_get_col(Vn_tmp,X_,id_keep[i]);
        gsl_matrix_set_col(X_new,i,Vn_tmp);
        gsl_vector_free(Vn_tmp);
        Vn_tmp = gsl_vector_alloc(dim_u);
        gsl_matrix_get_col(Vn_tmp,Un_,id_keep[i]);
        gsl_matrix_set_col(Un_new,i,Vn_tmp);
        gsl_vector_free(Vn_tmp);
    }
    gsl_vector_free(Jx_Un);
    gsl_vector_free(X_col);
    gsl_vector_free(Un_col);
    gsl_matrix_free(Vn_container);
    gsl_matrix_free(Un_);
    free(J_x);
    free(norm);
    free(id_keep);

    //  prepare for BX
    centres = malloc(dim_x*dim_b*sizeof(double));
    generate_kmeans_centres(X_new->data,dim_x,model.dim_n,dim_b,centres);
    var_tmp = malloc(dim_b*dim_b*sizeof(double));
    vec     = malloc(dim_b*sizeof(double));
    ccl_mat_distance(centres,dim_x,dim_b,centres,dim_x,dim_b,var_tmp);

    for (i=0;i<dim_b*dim_b;i++){
        var_tmp[i] = sqrt(var_tmp[i]);
    }
    ccl_mat_mean(var_tmp,dim_b,dim_b,1,vec);
    variance = pow(gsl_stats_mean(vec,1,dim_b),2);
    BX = malloc(dim_b*model.dim_n*sizeof(double));
    ccl_gaussian_rbf(X_new->data,dim_x,model.dim_n,centres,dim_x,dim_b,variance,BX);
    gsl_matrix_free(X_new);
    gsl_matrix_free(Un_new);
    free(var_tmp);
    free(vec);

    Rn = malloc(dim_r*dim_r*sizeof(double));
    gsl_matrix Rn_ = gsl_matrix_view_array(Rn,dim_r,dim_r).matrix;
    gsl_matrix_set_identity(&Rn_);

    RnVn = malloc(dim_r*model.dim_n*sizeof(double));
    memcpy(RnVn,Vn,dim_r*model.dim_n*sizeof(double));
    model.dim_u = model.dim_r;
    ccl_learn_alpha_model_alloc(&model);
    memcpy(model.c,centres,model.dim_x*model.dim_b*sizeof(double));
    model.s2 = variance;
    clock_t t = clock();
    for(lambda_id=0;lambda_id<dim_r;lambda_id++){
        model.dim_k = lambda_id+1;
        //        model.w[lambda_id] = malloc((dim_u-model.dim_k)*dim_b*sizeof(double*));
        if(dim_r-model.dim_k==0){
            model.dim_k = lambda_id;
            break;
        }
        else{
            search_learn_alpha(BX,RnVn,&model);
            double* theta = malloc(model.dim_n*model.dim_t*sizeof(double));
            double *W_BX = malloc((dim_r-model.dim_k)*model.dim_n*sizeof(double));
            double *W_BX_T = malloc(model.dim_n*(dim_r-model.dim_k)*sizeof(double));
            ccl_dot_product(model.w[lambda_id],dim_r-model.dim_k,dim_b,BX,dim_b,model.dim_n,W_BX);
            ccl_mat_transpose(W_BX,dim_r-model.dim_k,model.dim_n,W_BX_T);
            if (model.dim_k ==1){
                memcpy(theta,W_BX_T,model.dim_n*(dim_r-model.dim_k)*sizeof(double));
            }
            else{
                gsl_matrix* ones = gsl_matrix_alloc(model.dim_n,model.dim_k-1);
                gsl_matrix_set_all(ones,1);
                gsl_matrix_scale(ones,M_PI/2);
                mat_hotz_app(ones->data,model.dim_n,model.dim_k-1,W_BX_T,model.dim_n,dim_r-model.dim_k,theta);

                gsl_matrix_free(ones);
            }
            for(i=0;i<model.dim_n;i++){
                gsl_matrix theta_mat = gsl_matrix_view_array(theta,model.dim_n,model.dim_t).matrix;
                gsl_vector *vec      = gsl_vector_alloc(model.dim_t);
                gsl_matrix_get_row(vec,&theta_mat,i);
                ccl_get_rotation_matrix(vec->data,Rn,&model,lambda_id,Rn);
                gsl_vector_free(vec);
                vec                  = gsl_vector_alloc(dim_r);
                gsl_matrix_get_col(vec,&Vn_,i);
                ccl_dot_product(Rn,dim_r,dim_r,vec->data,dim_r,1,vec->data);
                gsl_matrix RnVn_     = gsl_matrix_view_array(RnVn,dim_r,model.dim_n).matrix;
                gsl_matrix_set_col(&RnVn_,i,vec);
                gsl_vector_free(vec);
            }
            if(model.nmse > optimal.nmse && model.nmse > 1E-5){
                model.dim_k = lambda_id;
                printf("\n I am out...\n");//optimal;
                break;
            }
            else{
                printf("\n copy model -> optimal\n");//optimal;
            }
            free(W_BX);
            free(W_BX_T);
            free(theta);
        }
    }
    t = clock()-t;
    printf("\n learning lambda used: %f second \n",((float)t)/CLOCKS_PER_SEC);
    double* A = malloc(model.dim_k*model.dim_r*sizeof(double));
    gsl_matrix* Iu = gsl_matrix_alloc(model.dim_r,model.dim_r);
    gsl_matrix_set_identity(Iu);
    gsl_vector* x = gsl_vector_alloc(model.dim_x);
    gsl_matrix_get_col(x,X_,5);
//    predict_proj_lambda(x->data, model,Jacobian,centres,variance,Iu->data,A);
//    print_mat_d(A,model.dim_k,dim_r);
    ccl_write_learn_lambda_model("/home/yuchen/Desktop/ccl-1.1.0/data/learn_lambda_model_l.txt", &model);
    free(A);
    gsl_matrix_free(Iu);
    gsl_vector_free(x);
    free(Vn);
    free(Rn);
    free(BX);
    free(RnVn);
    free(centres);
    gsl_matrix_free(X_);
    ccl_learn_alpha_model_free(&model);
}
double main() {
	/* savgol coefficients */
	int i,j,points=3, order=1;
        gsl_matrix *h;
	
	h = gsl_matrix_alloc(points, points);
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",3*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
        gsl_matrix_free(h);

	points=5;
	h = gsl_matrix_alloc(points, points);
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",5*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
	order=3;
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",35*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
        gsl_matrix_free(h);

	points=7;
        h = gsl_matrix_alloc(points, points);
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",21*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
	order=4;
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",231*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
        gsl_matrix_free(h);

	order=2;
	points=9;
        h = gsl_matrix_alloc(points, points);
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",231*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
	order=4;
	nsl_smooth_savgol_coeff(points, order, h);
	for(j=0;j<points;j++)
		printf(" %g",429*gsl_matrix_get(h,(points-1)/2,j));
	printf("\n");
        gsl_matrix_free(h);

	/* Savitzky Golay with different modes */
	printf("\n");
	int m=5;
	order=2;

	double data[9]={2,2,5,2,1,0,1,4,9};
	printf("mode:interp\n");
	nsl_smooth_savgol(data,9,m,order,nsl_smooth_pad_interp);
	for(i=0;i<9;i++)
		printf(" %7.4f",data[i]);
	printf("\n");

	double data2[9]={2,2,5,2,1,0,1,4,9};
	printf("mode:mirror\n");
	nsl_smooth_savgol(data2,9,m,order,nsl_smooth_pad_mirror);
	for(i=0;i<9;i++)
		printf(" %7.4f",data2[i]);
	printf("\n");

	double data3[9]={2,2,5,2,1,0,1,4,9};
	printf("mode:nearest\n");
	nsl_smooth_savgol(data3,9,m,order,nsl_smooth_pad_nearest);
	for(i=0;i<9;i++)
		printf(" %7.4f",data3[i]);
	printf("\n");

	double data4[9]={2,2,5,2,1,0,1,4,9};
	printf("mode:constant\n");
	nsl_smooth_savgol(data4,9,m,order,nsl_smooth_pad_constant);
	for(i=0;i<9;i++)
		printf(" %7.4f",data4[i]);
	printf("\n");

	double data5[9]={2,2,5,2,1,0,1,4,9};
	printf("mode:wrap\n");
	nsl_smooth_savgol(data5,9,m,order,nsl_smooth_pad_periodic);
	for(i=0;i<9;i++)
		printf(" %7.4f",data5[i]);
	printf("\n");
}
Ejemplo n.º 6
0
gsl_vector* least_square_nl_fit(struct data dat, struct parameters par, gsl_multifit_function_fdf f)
{
	size_t n, p;
	int status, i, iter = 0; 
	double *x_init;
	const gsl_rng_type * type;
	const gsl_multifit_fdfsolver_type *T;
	gsl_multifit_fdfsolver *s;
	
	n = dat.n;
	p = par.n;
		
	gsl_matrix *covar = gsl_matrix_alloc(p,p);

	x_init = (double *) calloc(par.n, sizeof(double));

		for(i=0; i<par.n; i++) 
			x_init[i] = par.guess_p[i];
		

		gsl_vector_view x = gsl_vector_view_array (x_init, p);
		gsl_rng * r;
		gsl_rng_env_setup();
     
    	   	type = gsl_rng_default;
    	   	r = gsl_rng_alloc (type);

  	        T = gsl_multifit_fdfsolver_lmsder;
    		s = gsl_multifit_fdfsolver_alloc(T, n, p);
    		gsl_multifit_fdfsolver_set(s, &f, &x.vector);

#ifdef PRINT_INFO
	       print_state (iter, s);
#endif

       do
         {
           iter++;
           status = gsl_multifit_fdfsolver_iterate (s);

#ifdef PRINT_INFO
	       print_state (iter, s);
#endif

           if (status)
             break;
     
           status = gsl_multifit_test_delta (s->dx, s->x,
                                             1e-6, 1e-6);
         }
       while (status == GSL_CONTINUE && iter < 500);
     
#ifdef PRINT_INFO
	       print_state (iter, s);
#endif

       gsl_multifit_covar (s->J, 0.0, covar);
     
       { 
       gsl_matrix_free (covar);
       gsl_rng_free (r);
     }

	return s->x;
       gsl_multifit_fdfsolver_free (s);
}
Ejemplo n.º 7
0
 Matrix::Matrix(const gsl_matrix * gsl) {
   myData = gsl_matrix_alloc(gsl->size1, gsl->size2);
   gsl_matrix_memcpy(myData, gsl);
 }
Ejemplo n.º 8
0
int main(int argc, char *argv[]) {
	int i,j;
	char eigen_name[100] = "eigen.dat";
	char pca_name[100] = "pca_eigen.dat";
	int atom[2];
	int mode = 6;
	int nm = 10;
	char filename[100] = "undefined";
	int verbose =1 ;
	int lig = 0;
	for (i = 1;i < argc;i++) {
		if (strcmp("-i",argv[i]) == 0) {strcpy(filename,argv[i+1]);}
		if (strcmp("-lig",argv[i]) == 0) {++lig;}
 		if (strcmp("-ieig1",argv[i]) == 0) {strcpy(eigen_name,argv[i+1]);}
		if (strcmp("-ieig2",argv[i]) == 0)  {strcpy(pca_name,argv[i+1]);}
		if (strcmp("-m",argv[i]) == 0)  {sscanf(argv[i+1],"%d",&mode);}
		if (strcmp("-nm",argv[i]) == 0)  {sscanf(argv[i+1],"%d",&nm);}
	}
	printf("-ieig1 %s -ieig2 %s -i %s\n",eigen_name,pca_name,filename);
	if (strcmp("undefined",filename) == 0) {
		printf("You need to give the WT form pdb\n");
		return(0);
	}
	
	int all = count_atom(filename);
 	int nconn = count_connect(filename);
 	
 	if (verbose == 1) {printf("Connect:%d\n",nconn);}
 	
	if (verbose == 1) {printf("Assigning Structure\n\tAll Atom\n");}
	
	// Array qui comprend tous les connects
	
	int **connect_h=(int **)malloc(nconn*sizeof(int *)); 
    for(i=0;i<nconn;i++) { connect_h[i]=(int *)malloc(7*sizeof(int));}
    
    assign_connect(filename,connect_h);
	
	// Assign tous les atoms
	
	struct pdb_atom strc_all[all];
	int atom_strc = build_all_strc(filename,strc_all); // Retourne le nombre de Node
	
	if (verbose == 1) {printf("	Node:%d\n	Atom:%d\n",atom_strc,all);}

	check_lig(strc_all,connect_h,nconn,all);

	// Assign les Nodes
	
	if (verbose == 1) {printf("	CA Structure\n");}
	
	//atom = count_atom_CA_n(strc_all,all,super_node,lig);
	if (verbose == 1) {printf("	Node:%d\n",atom_strc);}
	struct pdb_atom strc_node[atom_strc];
	atom_strc = build_cord_CA(strc_all, strc_node,all,lig,connect_h,nconn);
	if (verbose == 1) {printf("	Assign Node:%d\n",atom_strc);}
	
	if (verbose == 1) {printf("Open eigen 1:%s\n",eigen_name);}
	
	atom[0] = count_eigen(eigen_name);
	
	gsl_vector *eval = gsl_vector_alloc(atom[0]);
	gsl_matrix *evec = gsl_matrix_alloc (atom[0],atom[0]);
	
	load_eigen(eval,evec,eigen_name,atom[0]);
	if (verbose == 1) {printf("Open eigen 2:%s\n",pca_name);}
	atom[1] = count_eigen(pca_name);
	
	gsl_vector *evalpca = gsl_vector_alloc(atom[1]);
	gsl_matrix *evecpca = gsl_matrix_alloc (atom[1],atom[1]);
	
	load_eigen(evalpca,evecpca,pca_name,atom[1]);
	
	printf("Atom:   %d::%d\n",atom[0],atom[1]);
	if (atom[0] != atom[1]) {printf("I exited, car pas même nombre de Ca entre les eigenvecoctors.... la première structure de build_pca, est la strc de référence pour comparer\n");return(1);}
	float corr;
	int k;
	printf("J:NMA I:PCA\n");
	//float overlap;
	for(i=mode-1;i<nm+mode-1;++i) {
		for(j=mode-1;j<nm+mode-1;++j) {
			corr = over_eigen(evec,evecpca,atom[0],i,j, strc_node);
			
			
				printf("I:%3d J:%3d %8.5f Val %8.5f :: %8.5f\n",i,j,corr*corr,gsl_vector_get(eval,i),gsl_vector_get(evalpca,j));
				
			
		}
	}
	return(1);
}
Ejemplo n.º 9
0
static VALUE rb_gsl_cheb_eval_n_err(VALUE obj, VALUE nn, VALUE xx)
{
  gsl_cheb_series *p = NULL;
  double result, err;
  VALUE x, ary, aerr;
  size_t n, order, i, j;
  gsl_vector *v, *vnew, *verr;
  gsl_matrix *m, *mnew, *merr;
#ifdef HAVE_NARRAY_H
  struct NARRAY *na;
  double *ptr1, *ptr2, *ptr3;
#endif
  CHECK_FIXNUM(nn); 
  order = FIX2INT(nn);
  Data_Get_Struct(obj, gsl_cheb_series, p);
  if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
  switch (TYPE(xx)) {
  case T_FIXNUM:
  case T_BIGNUM:
  case T_FLOAT:
    gsl_cheb_eval_n_err(p, order, NUM2DBL(xx), &result, &err);
    return rb_ary_new3(2, rb_float_new(result), rb_float_new(err));
    break;
  case T_ARRAY:
    n = RARRAY(xx)->len;
    ary = rb_ary_new2(n);
    aerr = rb_ary_new2(n);
    for (i = 0; i < n; i++) {
      x = rb_ary_entry(xx, i);
      Need_Float(xx);
      gsl_cheb_eval_n_err(p, order, NUM2DBL(x), &result, &err);
      rb_ary_store(ary, i, rb_float_new(result));
      rb_ary_store(aerr, i, rb_float_new(err));
    }
    return rb_ary_new3(2, ary, aerr);
    break;
  default:
#ifdef HAVE_NARRAY_H
    if (NA_IsNArray(xx)) {
      GetNArray(xx, na);
      ptr1 = (double*) na->ptr;
      n = na->total;
      ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
      aerr = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
      ptr2 = NA_PTR_TYPE(ary,double*);
      ptr3 = NA_PTR_TYPE(aerr,double*);
      for (i = 0; i < n; i++) {
	gsl_cheb_eval_n_err(p, order, ptr1[i], &result, &err);
	ptr2[i] = result;
	ptr3[i] = err;
      }
      return rb_ary_new3(2, ary, aerr);
    }
#endif
    if (VECTOR_P(xx)) {
      Data_Get_Struct(xx, gsl_vector, v);
      vnew = gsl_vector_alloc(v->size);
      verr = gsl_vector_alloc(v->size);
      for (i = 0; i < v->size; i++) {
	gsl_cheb_eval_n_err(p, order, gsl_vector_get(v, i), &result, &err);
	gsl_vector_set(vnew, i, result);
	gsl_vector_set(verr, i, err);
      }
      return rb_ary_new3(2, 
			 Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew),
			 Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, verr));
    } else if (MATRIX_P(xx)) {
      Data_Get_Struct(xx, gsl_matrix, m);
      mnew = gsl_matrix_alloc(m->size1, m->size2);
      merr = gsl_matrix_alloc(m->size1, m->size2);
      for (i = 0; i < m->size1; i++) {
	for (j = 0; j < m->size2; j++) {
	  gsl_cheb_eval_n_err(p, order, gsl_matrix_get(m, i, j), &result, &err);
	  gsl_matrix_set(mnew, i, j, result);
	  gsl_matrix_set(merr, i, j, err);
	}
      }
      return rb_ary_new3(2,
			 Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew),
			 Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, merr));
    } else {
      rb_raise(rb_eTypeError, "wrong argument type");
    }
    break;
  }
  return Qnil;   /* never reach here */
}
Ejemplo n.º 10
0
/* Function loading the noise data from a directory */
int LLVSimFD_Noise_Init(const char dir[]) {
  if(!__LLVSimFD_Noise_setup) {
    printf("Error: LLVSimFD noise was already set up!");
    exit(1);
  }

  /* Loading noise data in gsl_vectors */
  int ret = SUCCESS;
  gsl_matrix* noise_LHO = gsl_matrix_alloc(noisedata_pts, 2);
  gsl_matrix* noise_LLO = gsl_matrix_alloc(noisedata_pts, 2);
  gsl_matrix* noise_VIRGO = gsl_matrix_alloc(noisedata_pts, 2);
  char* file_LIGO = malloc(strlen(dir)+64);
  char* file_VIRGO = malloc(strlen(dir)+64);
  //sprintf(file_LIGO, "%s", "LIGO-P1200087-v18-aLIGO_DESIGN.txt");
  //sprintf(file_VIRGO, "%s", "LIGO-P1200087-v18-AdV_DESIGN.txt");
  sprintf(file_LIGO, "%s", "aLIGO_sensitivity.dat");
  sprintf(file_VIRGO, "%s", "aVirgo_sensitivity.dat");
  ret |= Read_Text_Matrix(dir, file_LIGO, noise_LHO);
  ret |= Read_Text_Matrix(dir, file_LIGO, noise_LLO);
  ret |= Read_Text_Matrix(dir, file_VIRGO, noise_VIRGO);

  if(ret==FAILURE) {
    printf("Error: problem reading LLV noise data.");
    exit(1);
  }

  /* Linear interpolation of the data, after setting the gsl_spline structures */
  else if(ret==SUCCESS) {
    /* Extracting te vectors for the frequencies and data */
    gsl_vector* noise_LHO_freq = gsl_vector_alloc(noisedata_pts);
    gsl_vector* noise_LLO_freq = gsl_vector_alloc(noisedata_pts);
    gsl_vector* noise_VIRGO_freq = gsl_vector_alloc(noisedata_pts);
    gsl_vector* noise_LHO_data = gsl_vector_alloc(noisedata_pts);
    gsl_vector* noise_LLO_data = gsl_vector_alloc(noisedata_pts);
    gsl_vector* noise_VIRGO_data = gsl_vector_alloc(noisedata_pts);
    gsl_matrix_get_col(noise_LHO_freq, noise_LHO, 0);
    gsl_matrix_get_col(noise_LLO_freq, noise_LLO, 0);
    gsl_matrix_get_col(noise_VIRGO_freq, noise_VIRGO, 0);
    gsl_matrix_get_col(noise_LHO_data, noise_LHO, 1);
    gsl_matrix_get_col(noise_LLO_data, noise_LLO, 1);
    gsl_matrix_get_col(noise_VIRGO_data, noise_VIRGO, 1);
    /* Setting the global variables that indicate the range in frequency of these splines */
    __LLVSimFD_LHONoise_fLow = gsl_vector_get(noise_LHO_freq, 0);
    __LLVSimFD_LHONoise_fHigh = gsl_vector_get(noise_LHO_freq, noise_LHO_freq->size - 1);
    __LLVSimFD_LLONoise_fLow = gsl_vector_get(noise_LLO_freq, 0);
    __LLVSimFD_LLONoise_fHigh = gsl_vector_get(noise_LLO_freq, noise_LLO_freq->size - 1);
    __LLVSimFD_VIRGONoise_fLow = gsl_vector_get(noise_VIRGO_freq, 0);
    __LLVSimFD_VIRGONoise_fHigh = gsl_vector_get(noise_VIRGO_freq, noise_VIRGO_freq->size - 1);
    /* Initializing the splines and accelerators */
    *__LLVSimFD_LHONoiseSpline = gsl_spline_alloc(gsl_interp_linear, noisedata_pts);
    *__LLVSimFD_LLONoiseSpline = gsl_spline_alloc(gsl_interp_linear, noisedata_pts);
    *__LLVSimFD_VIRGONoiseSpline = gsl_spline_alloc(gsl_interp_linear, noisedata_pts);
    *__LLVSimFD_LHONoiseAccel = gsl_interp_accel_alloc();
    *__LLVSimFD_LLONoiseAccel = gsl_interp_accel_alloc();
    *__LLVSimFD_VIRGONoiseAccel = gsl_interp_accel_alloc();
    gsl_spline_init(*__LLVSimFD_LHONoiseSpline, gsl_vector_const_ptr(noise_LHO_freq, 0), gsl_vector_const_ptr(noise_LHO_data, 0), noisedata_pts);
    gsl_spline_init(*__LLVSimFD_LLONoiseSpline, gsl_vector_const_ptr(noise_LLO_freq, 0), gsl_vector_const_ptr(noise_LLO_data, 0), noisedata_pts);
    gsl_spline_init(*__LLVSimFD_VIRGONoiseSpline, gsl_vector_const_ptr(noise_VIRGO_freq, 0), gsl_vector_const_ptr(noise_VIRGO_data, 0), noisedata_pts);
    /* Setting the global tag to success and clean up */
    gsl_matrix_free(noise_LHO);
    gsl_matrix_free(noise_LLO);
    gsl_matrix_free(noise_VIRGO);
    gsl_vector_free(noise_LHO_freq);
    gsl_vector_free(noise_LLO_freq);
    gsl_vector_free(noise_VIRGO_freq);
    gsl_vector_free(noise_LHO_data);
    gsl_vector_free(noise_LLO_data);
    gsl_vector_free(noise_VIRGO_data);
    __LLVSimFD_Noise_setup = SUCCESS;
  }
  
  /* Cleaning and output */
  free(file_LIGO);
  free(file_VIRGO);
  return(ret);
}
Ejemplo n.º 11
0
SEXP multifit_cor(SEXP par, SEXP Thalf, SEXP x, SEXP y, SEXP err, SEXP tr, 
		  SEXP prec, SEXP N, SEXP max_iter, SEXP no_masses)
{
  int npar, nx, ny, i, iter_max;
  double p1, p2, m;
  double *yp, *errp, *parp, *precp, *statep;
  double chi_square, red_chi_square;
  int dof;
  int * xp, *Thalfp, *trp, *Np, *mip, *nmp;
  SEXP state;
  const gsl_multifit_fdfsolver_type *solver_type =
    gsl_multifit_fdfsolver_lmsder;
  /*  Allocate the solver. */
  gsl_multifit_fdfsolver *solver;
  /*  Initialize the data structure. */
  struct data data_struct;
  gsl_multifit_function_fdf function_fdf;
  gsl_matrix *covar;
  double * para_initial, c=1.;
  gsl_vector_view para_initial_;
  int status, iter=0, no_points=0;

  PROTECT(par = AS_NUMERIC(par));
  PROTECT(Thalf = AS_INTEGER(Thalf));
  PROTECT(x = AS_INTEGER(x));
  PROTECT(y = AS_NUMERIC(y));
  PROTECT(err = AS_NUMERIC(err));
  PROTECT(tr = AS_INTEGER(tr));
  PROTECT(prec = AS_NUMERIC(prec));
  PROTECT(N = AS_INTEGER(N));
  PROTECT(max_iter = AS_INTEGER(max_iter));
  PROTECT(no_masses = AS_INTEGER(no_masses));

  xp = INTEGER_POINTER(x);
  Thalfp = INTEGER_POINTER(Thalf);
  trp = INTEGER_POINTER(tr);
  Np = INTEGER_POINTER(N);
  yp = NUMERIC_POINTER(y);
  errp = NUMERIC_POINTER(err);
  parp = NUMERIC_POINTER(par);
  precp = NUMERIC_POINTER(prec);
  mip = INTEGER_POINTER(max_iter);
  nmp = INTEGER_POINTER(no_masses);
  iter_max = mip[0];

  npar = LENGTH(par);
  nx = LENGTH(x);
  ny = LENGTH(y);

  assert(npar == nmp[0]*(Np[0]+1));
  PROTECT(state = NEW_NUMERIC(5+npar));
  statep = NUMERIC_POINTER(state);

/*   PROTECT(gradient = allocMatrix(REALSXP, npar, npar)); */
  
  if(Np[0] == 2) no_points = 3*trp[0];
  if(Np[0] == 4) no_points = 10*trp[0];
  if(Np[0] == 6) no_points = 21*trp[0];

  solver = gsl_multifit_fdfsolver_alloc(solver_type, ny, npar);

  data_struct.x = (double*) malloc(nx*sizeof(double));
  data_struct.y = (double*) malloc(ny*sizeof(double));
  data_struct.err = (double*) malloc(ny*sizeof(double));
  para_initial = (double*) malloc(npar*sizeof(double));
  for(i = 0; i < nx; i++) {
    data_struct.x[i] = (double)xp[i];
  }

  for(i = 0; i < ny; i++) {
    data_struct.y[i] = yp[i];
    data_struct.err[i] = errp[i];
  }

  data_struct.Thalf = Thalfp[0];
  data_struct.tr = trp[0];
  data_struct.N = Np[0];
  data_struct.no_masses = nmp[0];

  // The ansatz.
  function_fdf.f = &exp_f;
  function_fdf.df = &exp_df;
  function_fdf.fdf = &exp_fdf;
  function_fdf.n = ny;
  function_fdf.p = npar;
  function_fdf.params = &data_struct;

  for(i = 0; i < npar; i++) {
    para_initial[i] = parp[i];
  }

  para_initial_ = gsl_vector_view_array(para_initial, npar);

  gsl_multifit_fdfsolver_set(solver, &function_fdf, &para_initial_.vector);

  // Perform the fit.
  // Print the initial state.
#ifdef _DEBUG
  Print_State_Mass_Fit_Helper_1(iter, solver);
#endif

  do {
    iter++;
    
    /*  Do a solver iteration. */
    status = gsl_multifit_fdfsolver_iterate(solver);
#ifdef _DEBUG
    fprintf(stderr, "status = %s.\n", gsl_strerror(status));
    Print_State_Mass_Fit_Helper_1(iter, solver);
#endif
    
    if(status) {
      break;
    }

    status = gsl_multifit_test_delta(solver->dx, solver->x,
				     precp[0], precp[1]);

  }
  while(status == GSL_CONTINUE && iter < iter_max);
#ifdef _DEBUG
  fprintf(stderr, "\n");
#endif
  
  // *****
  
  
  // Compute the covariance matrix.

  covar = gsl_matrix_alloc(npar, npar);
  gsl_multifit_covar(solver->J, 0.0, covar);

  // Output.

  chi_square = pow(gsl_blas_dnrm2(solver->f), 2.0);
#ifdef _DEBUG
  fprintf(stderr, "chi_square = %13.6lf.\n", chi_square);
#endif
  dof = no_points - npar;
#ifdef _DEBUG
  fprintf(stderr, "dof = %d\n", dof);
#endif
  red_chi_square = chi_square / (double)dof;
#ifdef _DEBUG
  fprintf(stderr, "red_chi_square = %13.6lf.\n", red_chi_square);
  fprintf(stderr, "\n");
#endif
  p1 = gsl_vector_get(solver->x, 0);
  p2 = gsl_vector_get(solver->x, 1);
  m = gsl_vector_get(solver->x, npar-1);

  if(red_chi_square > 1.0)
    c = sqrt(red_chi_square);
#ifdef _DEBUG
  fprintf(stderr, "p1 = %+9.6lf +/- %9.6lf.\n",
      p1, c * sqrt(gsl_matrix_get(covar, 0, 0)));
  fprintf(stderr, "p2 = %+9.6lf +/- %9.6lf.\n",
      p2, c * sqrt(gsl_matrix_get(covar, 1, 1)));
  fprintf(stderr, "m = %+9.6lf +/- %9.6lf.\n",
      m, c * sqrt(gsl_matrix_get(covar, npar-1, npar-1)));
  fprintf(stderr, "\n");
  fprintf(stderr, "status = %s.\n", gsl_strerror(status));
  fprintf(stderr, "\n");
#endif

  for(i = 0; i < npar; i++) {
    statep[5+i] =  gsl_vector_get(solver->x, i);
  }

  statep[0] = chi_square;
  statep[1] = gsl_blas_dnrm2(solver->f);
  statep[2] = (double)iter;
  statep[3] = (double)dof;
  statep[4] = (double)status;

  
  gsl_multifit_fdfsolver_free(solver);
#ifdef _DEBUG
  gsl_matrix_free(covar);
#endif

  free(data_struct.x);
  free(data_struct.y);
  free(data_struct.err);
  free(para_initial);

  UNPROTECT(11);
  return(state);
}
Ejemplo n.º 12
0
Archivo: work.c Proyecto: hironics/GSL
gsl_multifit_linear_workspace *
gsl_multifit_linear_alloc (const size_t nmax, const size_t pmax)
{
  gsl_multifit_linear_workspace *w;

  w = calloc (1, sizeof (gsl_multifit_linear_workspace));

  if (w == 0)
    {
      GSL_ERROR_VAL ("failed to allocate space for multifit_linear struct",
                     GSL_ENOMEM, 0);
    }

  w->nmax = nmax;                     /* max number of observations */
  w->pmax = pmax;                     /* max number of parameters */
  w->n = 0;
  w->p = 0;

  w->A = gsl_matrix_alloc (nmax, pmax);

  if (w->A == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for A", GSL_ENOMEM, 0);
    }

  w->Q = gsl_matrix_alloc (pmax, pmax);

  if (w->Q == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for Q", GSL_ENOMEM, 0);
    }

  w->QSI = gsl_matrix_alloc (pmax, pmax);

  if (w->QSI == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for QSI", GSL_ENOMEM, 0);
    }

  w->S = gsl_vector_alloc (pmax);

  if (w->S == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for S", GSL_ENOMEM, 0);
    }

  w->t = gsl_vector_alloc (nmax);

  if (w->t == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for t", GSL_ENOMEM, 0);
    }

  w->xt = gsl_vector_calloc (pmax);

  if (w->xt == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for xt", GSL_ENOMEM, 0);
    }

  w->D = gsl_vector_calloc (pmax);

  if (w->D == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for D", GSL_ENOMEM, 0);
    }

  w->LTQR = gsl_matrix_alloc(pmax, pmax);
  if (w->LTQR == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for LTQR", GSL_ENOMEM, 0);
    }

  w->LTtau = gsl_vector_alloc(pmax);

  if (w->LTtau == 0)
    {
      gsl_multifit_linear_free(w);
      GSL_ERROR_VAL ("failed to allocate space for LTtau", GSL_ENOMEM, 0);
    }

  return w;
}
Ejemplo n.º 13
0
  /** Executes the algorithm
  *
  *  @throw runtime_error Thrown if algorithm cannot execute
  */
  void GenericFit::exec()
  {
    // Try to retrieve optional properties
    const int maxInterations = getProperty("MaxIterations");

    Progress prog(this,0.0,1.0,maxInterations?maxInterations:1);

    std::string funIni = getProperty("Function");
    m_function.reset( API::FunctionFactory::Instance().createInitialized(funIni) );

    if (m_function.get() == NULL)
    {
      throw std::runtime_error("Function was not set.");
    }

    prog.report("Setting workspace");
    API::Workspace_sptr ws = getProperty("InputWorkspace");
    std::string input = getProperty("Input");
    m_function->setWorkspace(ws,input);

    prog.report("Setting minimizer");
    // force initial parameters to satisfy constraints of function
    m_function->setParametersToSatisfyConstraints();

    // check if derivative defined in derived class
    bool isDerivDefined = true;
    try
    {
      m_function->functionDeriv(NULL);
    }
    catch (Exception::NotImplementedError&)
    {
      isDerivDefined = false;
    }

    // What minimizer to use
    std::string methodUsed = getProperty("Minimizer");
    if ( !isDerivDefined && methodUsed.compare("Simplex") != 0 )
    {
      methodUsed = "Simplex";
      g_log.information() << "No derivatives available for this fitting function"
                          << " therefore Simplex method used for fitting\n";
    }

    // set-up minimizer

    std::string costFunction = getProperty("CostFunction");
    IFuncMinimizer* minimizer = FuncMinimizerFactory::Instance().createUnwrapped(methodUsed);
    minimizer->initialize(m_function.get(), costFunction);

    // create and populate data containers. Warn user if nData < nParam 
    // since as a rule of thumb this is required as a minimum to obtained 'accurate'
    // fitting parameter values.

    const size_t nParam = m_function->nActive();
    const size_t nData = m_function->dataSize();
    if (nParam == 0)
    {
      g_log.error("There are no active parameters.");
      setProperty("OutputChi2overDoF", minimizer->costFunctionVal());
      throw std::runtime_error("There are no active parameters.");
    }
    if (nData == 0)
    {
      g_log.error("The data set is empty.");
      throw std::runtime_error("The data set is empty.");
    }
    if (nData < nParam)
    {
      g_log.error("Number of data points less than number of parameters to be fitted.");
      throw std::runtime_error("Number of data points less than number of parameters to be fitted.");
    }

    // finally do the fitting

    int iter = 0;
    int status = 0;
    double finalCostFuncVal = 0.0;
    double dof = static_cast<double>(nData - nParam);  // dof stands for degrees of freedom

    // Standard least-squares used if derivative function defined otherwise simplex
    if ( methodUsed.compare("Simplex") != 0 )
    {
      status = GSL_CONTINUE;
      while (status == GSL_CONTINUE && iter < maxInterations)
      {
        iter++;

        status = minimizer->iterate();

        if (status != GSL_SUCCESS && minimizer->hasConverged() != GSL_SUCCESS)
        { 
          // From experience it is found that gsl_multifit_fdfsolver_iterate occasionally get
          // stock - even after having achieved a sensible fit. This seem in particular to be a
          // problem on Linux. For now only fall back to Simplex if iter = 1 or 2, i.e.   
          // gsl_multifit_fdfsolver_iterate has failed on the first or second hurdle
          if (iter < 3)
          {
            g_log.warning() << "GenericFit algorithm using " << methodUsed << " failed "
              << "reporting the following: " << gsl_strerror(status) << "\n"
              << "Try using Simplex method instead\n";
            methodUsed = "Simplex";
            delete minimizer;
            minimizer = FuncMinimizerFactory::Instance().createUnwrapped(methodUsed);
            minimizer->initialize(m_function.get(), costFunction);
            iter = 0;
          }
          break;
        }
        
        status = minimizer->hasConverged();
        prog.report("Iteration "+boost::lexical_cast<std::string>(iter));
      }

      finalCostFuncVal = minimizer->costFunctionVal() / dof;
    }


    if ( methodUsed.compare("Simplex") == 0 )
    {
      status = GSL_CONTINUE;
      while (status == GSL_CONTINUE && iter < maxInterations)
      {
        iter++;
        status = minimizer->iterate();

        if (status)  // break if error
        {
          // if failed at first iteration try reducing the initial step size
          if (iter == 1)
          { 
            g_log.information() << "Simplex step size reduced to 0.1\n";
            delete minimizer;
            SimplexMinimizer* sm = new SimplexMinimizer;
            sm->initialize(m_function.get(), costFunction);
            sm->resetSize(0.1, m_function.get(), costFunction);
            minimizer = sm;
            status = GSL_CONTINUE;
            continue;
          }
          break;
        }

        status = minimizer->hasConverged();
        prog.report("Iteration "+boost::lexical_cast<std::string>(iter));
      }

      finalCostFuncVal = minimizer->costFunctionVal() / dof;
    }

    // Output summary to log file

    std::string reportOfFit = gsl_strerror(status);

    g_log.information() << "Method used = " << methodUsed << "\n" <<
      "Iteration = " << iter << "\n";
    Mantid::API::ICostFunction* costfun 
     = Mantid::API::CostFunctionFactory::Instance().createUnwrapped(costFunction);
    if ( reportOfFit == "success" )
      g_log.notice() << reportOfFit << "  " << costfun->shortName() << 
         " (" << costfun->name() << ") = " << finalCostFuncVal << "\n";
    else
      g_log.warning() << reportOfFit << "  " << costfun->shortName() << 
         " (" << costfun->name() << ") = " << finalCostFuncVal << "\n";
    for (size_t i = 0; i < m_function->nParams(); i++)
    {
      g_log.debug() << m_function->parameterName(i) << " = " << m_function->getParameter(i) << "  \n";
    }


    // also output summary to properties

    setProperty("OutputStatus", reportOfFit);
    setProperty("OutputChi2overDoF", finalCostFuncVal);
    setProperty("Minimizer", methodUsed);
    setPropertyValue("Function",*m_function);
    

    // if Output property is specified output additional workspaces

    std::vector<double> standardDeviations;
    std::string output = getProperty("Output");
    gsl_matrix *covar(NULL);

    // only if derivative is defined for fitting function create covariance matrix output workspace
    if ( isDerivDefined )    
    {
      // calculate covariance matrix
      covar = gsl_matrix_alloc (nParam, nParam);
      minimizer->calCovarianceMatrix( 0.0, covar);

      // take standard deviations to be the square root of the diagonal elements of
      // the covariance matrix
      int iPNotFixed = 0;
      for(size_t i=0; i < m_function->nParams(); i++)
      {
        standardDeviations.push_back(1.0);
        if (m_function->isActive(i))
        {
          standardDeviations[i] = sqrt(gsl_matrix_get(covar,iPNotFixed,iPNotFixed));
          if (m_function->activeParameter(iPNotFixed) != m_function->getParameter(m_function->indexOfActive(iPNotFixed)))
          {// it means the active param is not the same as declared but transformed
            standardDeviations[i] *= fabs(transformationDerivative(iPNotFixed));
          }
          iPNotFixed++;
        }
      }
    }

    if (!output.empty())
    {
      // only if derivative is defined for fitting function create covariance matrix output workspace
      if ( isDerivDefined )    
      {
        // Create covariance matrix output workspace
        declareProperty(
          new WorkspaceProperty<API::ITableWorkspace>("OutputNormalisedCovarianceMatrix","",Direction::Output),
          "The name of the TableWorkspace in which to store the final covariance matrix" );
        setPropertyValue("OutputNormalisedCovarianceMatrix",output+"_NormalisedCovarianceMatrix");

        Mantid::API::ITableWorkspace_sptr m_covariance = Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");
        m_covariance->addColumn("str","Name");
        std::vector<std::string> paramThatAreFitted; // used for populating 1st "name" column
        for(size_t i=0; i < m_function->nParams(); i++)
        {
          if (m_function->isActive(i)) 
          {
            m_covariance->addColumn("double",m_function->parameterName(i));
            paramThatAreFitted.push_back(m_function->parameterName(i));
          }
        }

        for(size_t i=0; i<nParam; i++)
        {
          Mantid::API::TableRow row = m_covariance->appendRow();
          row << paramThatAreFitted[i];
          for(size_t j=0; j<nParam; j++)
          {
            if (j == i)
              row << 100.0;
            else
            {
              row << 100.0*gsl_matrix_get(covar,i,j)/sqrt(gsl_matrix_get(covar,i,i)*gsl_matrix_get(covar,j,j));
            }
          }
        }

        setProperty("OutputNormalisedCovarianceMatrix",m_covariance);
      }

      // create output parameter table workspace to store final fit parameters 
      // including error estimates if derivative of fitting function defined

      declareProperty(
        new WorkspaceProperty<API::ITableWorkspace>("OutputParameters","",Direction::Output),
        "The name of the TableWorkspace in which to store the final fit parameters" );

      setPropertyValue("OutputParameters",output+"_Parameters");

      Mantid::API::ITableWorkspace_sptr m_result = Mantid::API::WorkspaceFactory::Instance().createTable("TableWorkspace");
      m_result->addColumn("str","Name");
      m_result->addColumn("double","Value");
      if ( isDerivDefined ) 
        m_result->addColumn("double","Error");

      for(size_t i=0;i<m_function->nParams();i++)
      {
        Mantid::API::TableRow row = m_result->appendRow();
        row << m_function->parameterName(i) << m_function->getParameter(i);
        if ( isDerivDefined && m_function->isActive(i)) 
        {
          row << standardDeviations[i];
        }
      }
      // Add chi-squared value at the end of parameter table
      Mantid::API::TableRow row = m_result->appendRow();
      row << "Cost function value" << finalCostFuncVal;      
      setProperty("OutputParameters",m_result);


      if ( isDerivDefined ) 
        gsl_matrix_free(covar);
    }

    // Add Parameters, Errors and ParameterNames properties to output so they can be queried on the algorithm.
    declareProperty(new ArrayProperty<double> ("Parameters",new NullValidator<std::vector<double> >,Direction::Output));
    declareProperty(new ArrayProperty<double> ("Errors",new NullValidator<std::vector<double> >,Direction::Output));
    declareProperty(new ArrayProperty<std::string> ("ParameterNames",new NullValidator<std::vector<std::string> >,Direction::Output));
    std::vector<double> params,errors;
    std::vector<std::string> parNames;

    for(size_t i=0;i<m_function->nParams();i++)
    {
      parNames.push_back(m_function->parameterName(i));
      params.push_back(m_function->getParameter(i));
      if (!standardDeviations.empty())
      {
        errors.push_back(standardDeviations[i]);
      }
      else
      {
        errors.push_back(0.);
      }
    }
    setProperty("Parameters",params);
    setProperty("Errors",errors);
    setProperty("ParameterNames",parNames);
    
    // minimizer may have dynamically allocated memory hence make sure this memory is freed up
    delete minimizer;

    return;
  }
Ejemplo n.º 14
0
//spline locations held fixed in Mpc^-1; CMB basically fixed P(k) in these units
void dopksmoothbspline_(double *kvals, double *lnpklinear, double *lnpksmooth, int *npts)  {

	double kmaxsuppress = 0.01*0.7;
	size_t n, ncoeffs, nbreak;
	gsl_bspline_workspace *bw;
	gsl_vector *B;
	gsl_vector *c, *w, *x, *y;
	gsl_matrix *X, *cov;
	gsl_multifit_linear_workspace *mw;
	double deltak,lastk;
	int i,j,countkeep;

	nbreak = 9;
	gsl_vector *mybreaks = gsl_vector_alloc(nbreak);
	gsl_vector_set(mybreaks,0,(0.001*0.7));
	gsl_vector_set(mybreaks,1,(0.025*0.7));
	gsl_vector_set(mybreaks,2,(0.075*0.7));
	gsl_vector_set(mybreaks,3,(0.125*0.7));
	gsl_vector_set(mybreaks,4,(0.175*0.7));
	gsl_vector_set(mybreaks,5,(0.225*0.7));
	gsl_vector_set(mybreaks,6,(0.275*0.7));
	gsl_vector_set(mybreaks,7,(0.325*0.7));
	gsl_vector_set(mybreaks,8,(0.375*0.7));

	countkeep = 0;
	for(i=0;i<(*npts);i++)  {
		if((kvals[i]) >= gsl_vector_get(mybreaks,0) && (kvals[i]) <= gsl_vector_get(mybreaks,nbreak-1)) {
			countkeep += 1;
			}
		}
	n = countkeep;
	ncoeffs = nbreak + 2;

	/* allocate a cubic bspline workspace (k = 4) */
	bw = gsl_bspline_alloc(4, nbreak);
	B = gsl_vector_alloc(ncoeffs);     
	x = gsl_vector_alloc(n);
	y = gsl_vector_alloc(n);
	X = gsl_matrix_alloc(n, ncoeffs);
	c = gsl_vector_alloc(ncoeffs);
	w = gsl_vector_alloc(n);
	cov = gsl_matrix_alloc(ncoeffs, ncoeffs);
	mw = gsl_multifit_linear_alloc(n, ncoeffs);
	i=0;
	for(j=0;j<(*npts);j++)  {
		if((kvals[j]) >= gsl_vector_get(mybreaks,0) && (kvals[j]) <= gsl_vector_get(mybreaks,nbreak-1)) {
			gsl_vector_set(x,i,(kvals[j]));
			gsl_vector_set(y,i,exp(lnpklinear[j])*pow(kvals[j],1.5));
			if(j>0)  {
				deltak = kvals[j] - kvals[j-1];
				}
			else {
				deltak = kvals[0];
				if(kvals[1] - kvals[0] < deltak)  {
					deltak = kvals[1]-kvals[0];
					}
				}
			gsl_vector_set(w,i,deltak);
			i+=1;
			}
		}
	gsl_bspline_knots(mybreaks,bw);
	for(i=0;i<n;i++)  {
		double xi = gsl_vector_get(x,i);
		gsl_bspline_eval(xi,B,bw);
		for(j=0;j<ncoeffs;j++)  {
			double Bj = gsl_vector_get(B,j);
			gsl_matrix_set(X,i,j,Bj);
			}
		}
	//do fit
	double yi,yierr,chisq;
	gsl_multifit_wlinear(X,w,y,c,cov,&chisq,mw);
	i = 0;
	for(j=0;j<(*npts);j++)  {
		if((kvals[j]) >= gsl_vector_get(mybreaks,0) && (kvals[j]) <= gsl_vector_get(mybreaks,nbreak-1)) {
			gsl_bspline_eval(gsl_vector_get(x,i),B,bw);
			gsl_multifit_linear_est(B,c,cov,&yi,&yierr);
			lnpksmooth[j] = log(yi*pow(kvals[j],-1.5));
			i += 1;
			}
		else {
			lnpksmooth[j] = lnpklinear[j];
			}
		//spline is wacky at small k -- suppress difference at k < 0.01
		if(kvals[j] < kmaxsuppress)  {
			lnpksmooth[j] = lnpklinear[j];
			}
		}
	assert(i==n);
	gsl_bspline_free(bw);
	gsl_vector_free(B);
	gsl_vector_free(x);
	gsl_vector_free(y);
	gsl_vector_free(mybreaks);
	gsl_matrix_free(X);
	gsl_vector_free(c);
	gsl_vector_free(w);
	gsl_matrix_free(cov);
	gsl_multifit_linear_free(mw);
	}
Ejemplo n.º 15
0
int main() {
  int i,j,k;
  double s;

#if 1



  std::vector< std::vector< std::vector<double> > > patients(count);
  
  for(i = 0; i<count; ++i) {
    std::cout << files[i] << std::endl;
    patients[i] = Patient(folder_name + files[i]).data;
  }

  gsl_matrix* A_avg = gsl_matrix_alloc(3, 64);
  for(i = 0; i<3; ++i) {
    for(j = 0; j<64; ++j) { 
      s = 0;
      for(k = 0; k < count; ++k) {
        s+=patients[k][i][j];
      }
      gsl_matrix_set(A_avg, i, j, s/count);            
    }
  }
  // gsl_matrix_print(A_avg, 3, 64);


  gsl_matrix* S = gsl_matrix_alloc(3, 64);
  for(i = 0; i<3; ++i) {
    for(j = 0; j<64; ++j) { 
      s = 0;
      for(k = 0; k < count; ++k) {
        s += pow((patients[k][i][j] - gsl_matrix_get(A_avg, i, j)), 2);            
      }
      gsl_matrix_set(S, i, j, sqrt(s/count));            	
    }
  }
  // gsl_matrix_print(S, 3, 64);
  gsl_matrix *T[count];    
  for(k = 0; k < count; ++k) {
    T[k] =  gsl_matrix_alloc(3, 64);
    for(i = 0; i<3; ++i) {
      for(j = 0; j<64; ++j) { 
        s = (patients[k][i][j] - gsl_matrix_get(A_avg, i, j)) / gsl_matrix_get(S, i, j);            
        gsl_matrix_set(T[k], i, j, s);            	
      }
    }
  }

  size_t cube_sizes[3] = {3, 64, count};
  Tensor<3> cube(cube_sizes);
  size_t matrix_sizes[3] = {cube.size(), 1};
  Tensor<2> matrix(matrix_sizes);
  int M, N;
  std::cout << cube.size() << std::endl;
  for (size_t offset = 0; offset < cube.size(); ++offset) {
    i = offset/(3*64);
    j = (offset%(3*64))/64;
    k = (offset%(3*64))%64;
    cube.data[offset] = gsl_matrix_get(T[i], j, k); // patients[i][j][k];
  }

#else

  size_t cube_sizes[3] = {3, 3, 3};
  Tensor<3> cube(cube_sizes);
  size_t matrix_sizes[3] = {cube.size(), 1};
  Tensor<2> matrix(matrix_sizes);
  int M, N;

  double tdata[27] = {
    0.9073,  0.7158, -0.3698,
    0.8924, -0.4898,  2.4288,
    2.1488,  0.3054,  2.3753,

    1.7842,  1.6970,  0.0151,
    1.7753, -1.5077,  4.0337,
    4.2495,  0.3207,  4.7146,

    2.1236, -0.0740,  1.4429,
   -0.6631,  1.9103, -1.7495,
    1.8260,  2.1335, -0.2716
  };

  for (size_t offset = 0; offset < cube.size(); ++offset) {
    cube.data[offset] = tdata[offset];
  }

#endif

  /* FLATTEN X */
  // std::cout << "X flatten:" << std::endl;
  cube.flatten(matrix, 0);
  // print(matrix);

  M = (int) matrix.sizes[1];
  N = (int) matrix.sizes[0];

  gsl_matrix* Ax = TensorToGSLMatrix(matrix);
	gsl_matrix* Ux;
  gsl_matrix* Vx;
  gsl_vector* Sx;
  gsl_vector* workx;

  //gsl_matrix_print(Ax, Ax->size1, Ax->size2);
  workx = gsl_vector_alloc(N);
  Sx = gsl_vector_alloc(N);
  Vx = gsl_matrix_alloc(N, N);

  gsl_linalg_SV_decomp(Ax, Vx, Sx, workx);
  Ux = Ax;
  //std::cout << "A = U S V^T" << std::endl;
  //std::cout << "U" << std::endl;
  //gsl_matrix_print(Ux, M, N);
  std::cout << "S1" << std::endl;
  gsl_vector_print(Sx, N);
  //std::cout << "V" << std::endl;
  //gsl_matrix_print(Vx, Vx->size1, Vx->size2);
  //std::cout << "work" << std::endl;
  //gsl_vector_print(workx, N);

  if (M < N) {
    Vx = gsl_matrix_alloc(Ux->size1, Ux->size1);
    for(i = 0; i < Ux->size1; ++i) {
      for(j = 0; j < Ux->size1; ++j) {
        if (j < Ux->size2 )
          gsl_matrix_set(Vx, j, i, gsl_matrix_get(Ux,i,j));
        else
          gsl_matrix_set(Vx, j, i, 0.0);
      }
    }
  }




  /* FLATTEN Y */
  //std::cout << "Y flatten:" << std::endl;
  cube.flatten(matrix, 1);
  //print(matrix);
  M = (int) matrix.sizes[1];
  N = (int) matrix.sizes[0];

  gsl_matrix* Ay = TensorToGSLMatrix(matrix);
	gsl_matrix* Uy;
  gsl_matrix* Vy;
  gsl_vector* Sy;
  gsl_vector* worky;

  //gsl_matrix_print(Ay, Ay->size1, Ay->size2);
  worky = gsl_vector_alloc(Ay->size2);
  Sy = gsl_vector_alloc(Ay->size2);
  Vy = gsl_matrix_alloc(Ay->size2, Ay->size2);

  gsl_linalg_SV_decomp(Ay, Vy, Sy, worky);
  Uy = Ay;
  //std::cout << "A = U S V^T" << std::endl;
  //std::cout << "U" << std::endl;
  //gsl_matrix_print(Uy, Uy->size1, Uy->size2);
  std::cout << "S2" << std::endl;
  gsl_vector_print(Sy, Ay->size2);
  //std::cout << "V" << std::endl;
  //gsl_matrix_print(Vy, Vy->size1, Vy->size2);
  //std::cout << "work" << std::endl;
  //gsl_vector_print(worky, Ay->size2);

  if (M < N) {
    Vy = gsl_matrix_alloc(Uy->size1, Uy->size1);
    for(i = 0; i < Uy->size1; ++i) {
      for(j = 0; j < Uy->size1; ++j) {
        if (j < Uy->size2 )
          gsl_matrix_set(Vy, j, i, gsl_matrix_get(Uy,i,j));
        else
          gsl_matrix_set(Vy, j, i, 0.0);
      }
    }
  }

  /* FLATTEN Z */
  //std::cout << "Z flatten:" << std::endl;
  cube.flatten(matrix, 2);
  //print(matrix);

  M = (int) matrix.sizes[1];
  N = (int) matrix.sizes[0];

  gsl_matrix* Az = TensorToGSLMatrix(matrix);
	gsl_matrix* Uz;
  gsl_matrix* Vz;
  gsl_vector* Sz;
  gsl_vector* workz;

  //gsl_matrix_print(Az, Az->size1, Az->size2);
  workz = gsl_vector_alloc(Az->size2);
  Sz = gsl_vector_alloc(Az->size2);
  Vz = gsl_matrix_alloc(Az->size2, Az->size2);
  gsl_linalg_SV_decomp(Az, Vz, Sz, workz);
  Uz = Az;
  //std::cout << "A = U S V^T" << std::endl;
  //std::cout << "U" << std::endl;
  //gsl_matrix_print(Uz, Uz->size2, Uz->size2);
  std::cout << "S3" << std::endl;
  gsl_vector_print(Sz, Az->size2);
  //std::cout << "V" << std::endl;
  //gsl_matrix_print(Vz, Vz->size1, Vz->size2);
  //std::cout << "work" << std::endl;
  //gsl_vector_print(workz, Az->size2);

  if (M < N) {
    Vz = gsl_matrix_alloc(Uz->size1, Uz->size1);
    for(i = 0; i < Uz->size1; ++i) {
      for(j = 0; j < Uz->size1; ++j) {
        if (j < Uy->size2 )
          gsl_matrix_set(Vz, j, i, gsl_matrix_get(Uz,i,j));
        else
          gsl_matrix_set(Vz, j, i, 0.0);
      }
    }
  }

/*
  gsl_matrix *Vx_t, *Uy_t, *Vz_t;
  Vx_t = gsl_matrix_alloc(Vx->size2, Vx->size1);
  gsl_matrix_transpose_memcpy (Vx_t, Vx);

  std::cout << "M = " << Uy->size1 << " N = " << Uy->size2 << std::endl;
  Uy_t = gsl_matrix_alloc(Uy->size1, Uy->size1);
  for(i = 0; i < Uy->size1; ++i) {
    for(j = 0; j < Uy->size1; ++j) {
      if (j < Uy->size2 )
        gsl_matrix_set(Uy_t, j, i, gsl_matrix_get(Uy,i,j));
      else
        gsl_matrix_set(Uy_t, j, i, 0.0);
    }
  }
  //gsl_matrix_transpose_memcpy (Uy_t, Uy);
  gsl_matrix_print(Uy_t,64,64);

  Vz_t = gsl_matrix_alloc(Vz->size2, Vz->size1);
  gsl_matrix_transpose_memcpy (Vz_t, Vz);

  std::cout << "M = " << Uy_t->size1 << " N = " << Uy_t->size2 << std::endl;
 */

  Tensor<3> G(cube_sizes);
  /* compute G */ {
    gsl_matrix *Vs[3] = {Vx, Vy, Vz};
    double component, summand;

    for(IndexIterator<3> IJK(cube_sizes); IJK; ++IJK) {
      component = 0.0;
      for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) {
        summand = cube[*ijk];
        for (size_t dim = 0; dim < 3; ++dim) {
          summand *= gsl_matrix_get(Vs[dim], (*IJK)[dim], (*ijk)[dim]);
        }
        component += summand;
      }
      G[*IJK] = component;
    }
  }

  /* HOSVD(cube) = G * Vs[0] * Vs[1] * Vs[2] */

  /* validate result */ {
    Tensor<3> T(cube_sizes);
    
    gsl_matrix *Vs[3] = {Vx, Vy, Vz};
    double component, summand;

    for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) {
      component = 0.0;
      for(IndexIterator<3> IJK(cube_sizes); IJK; ++IJK) {
        summand = G[*IJK];
        for (size_t dim = 0; dim < 3; ++dim) {
          summand *= gsl_matrix_get(Vs[dim], (*IJK)[dim], (*ijk)[dim]);
        }
        component += summand;
      }
      T[*ijk] = component;
    }

    /* T is restored cube tensor */
    bool valid_HOSVD = true;

    for(IndexIterator<3> ijk(cube_sizes); ijk; ++ijk) {
      if (!nearly_equal(cube[*ijk], T[*ijk], 1e-3)) {
        // wrong HOSVD
        valid_HOSVD = false;

        printf("Error in HOSVD: [");
        for (size_t dim = 0; dim < 3; ++dim) {
          printf("%d%c ", (*ijk)[dim], dim == 2 ? ']' : ',');
        }
        printf("%.15lf != %.15lf\n", cube[*ijk], T[*ijk]);
      }
    }
    if (valid_HOSVD) {
      printf("OK: HOSVD work!\n");
    } else {
      printf("HOSVD computation FAILED.\n");
    }
  }
 

  gsl_matrix *Vx_t, *tmp[count], *Z[count], *Z_avg;
  Vx_t = gsl_matrix_alloc(Vx->size2, Vx->size1);
  gsl_matrix_transpose_memcpy (Vx_t, Vx);
  


    gsl_blas_dgemm (CblasNoTrans, CblasNoTrans, 1.0, tmp[i], Vy, 0.0, Z[i]);
  }
Ejemplo n.º 16
0
static VALUE rb_gsl_cheb_eval(VALUE obj, VALUE xx)
{
  gsl_cheb_series *p = NULL;
  VALUE x, ary;
  size_t i, j, n;
  gsl_vector *v = NULL, *vnew = NULL;
  gsl_matrix *m = NULL, *mnew = NULL;
#ifdef HAVE_NARRAY_H
  struct NARRAY *na;
  double *ptr1, *ptr2;
#endif
  Data_Get_Struct(obj, gsl_cheb_series, p);
  if (CLASS_OF(xx) == rb_cRange) xx = rb_gsl_range2ary(xx);
  switch (TYPE(xx)) {
  case T_FIXNUM:
  case T_BIGNUM:
  case T_FLOAT:
    return rb_float_new(gsl_cheb_eval(p, NUM2DBL(xx)));
    break;
  case T_ARRAY:
    n = RARRAY(xx)->len;
    ary = rb_ary_new2(n);
    for (i = 0; i < n; i++) {
      x = rb_ary_entry(xx, i);
      Need_Float(xx);
      rb_ary_store(ary, i, rb_float_new(gsl_cheb_eval(p, NUM2DBL(x))));
    }
    return ary;
    break;
  default:
#ifdef HAVE_NARRAY_H
    if (NA_IsNArray(xx)) {
      GetNArray(xx, na);
      ptr1 = (double*) na->ptr;
      n = na->total;
      ary = na_make_object(NA_DFLOAT, na->rank, na->shape, CLASS_OF(xx));
      ptr2 = NA_PTR_TYPE(ary,double*);
      for (i = 0; i < n; i++) ptr2[i] = gsl_cheb_eval(p, ptr1[i]);
      return ary;
    }
#endif
    if (VECTOR_P(xx)) {
      Data_Get_Struct(xx, gsl_vector, v);
      vnew = gsl_vector_alloc(v->size);
      for (i = 0; i < v->size; i++) {
	gsl_vector_set(vnew, i, gsl_cheb_eval(p, gsl_vector_get(v, i)));
      }
      return Data_Wrap_Struct(cgsl_vector, 0, gsl_vector_free, vnew);
    } else if (MATRIX_P(xx)) {
      Data_Get_Struct(xx, gsl_matrix, m);
      mnew = gsl_matrix_alloc(m->size1, m->size2);
      for (i = 0; i < m->size1; i++) {
	for (j = 0; j < m->size2; j++) {
	  gsl_matrix_set(mnew, i, j, gsl_cheb_eval(p, gsl_matrix_get(m, i, j)));
	}
      }
      return Data_Wrap_Struct(cgsl_matrix, 0, gsl_matrix_free, mnew);
    } else {
      rb_raise(rb_eTypeError, "wrong argument type");
    }
    break;
  }
  return Qnil;   /* never reach here */
}
Ejemplo n.º 17
0
match_result algorithm_ca::match(graph& g, graph& h,gsl_matrix* gm_P_i,gsl_matrix* _gm_ldh,double dalpha_ldh)
{
	double dhung_max=get_param_d("hungarian_max");
	bool bblast_match_end=(get_param_i("blast_match_proj")==1);	
	bool bblast_match=(get_param_i("blast_match")==1);
	double dfw_xeps=get_param_d("algo_fw_xeps");
	double bgreedy=(get_param_i("hungarian_greedy")==1);
	double dfvalue;
	if (bverbose)
		*gout<<"C-adaptation strategy"<<std::endl;	
	//some duplicate variables
	gsl_matrix* gm_Ag_d=g.get_descmatrix(cdesc_matrix);
	gsl_matrix* gm_Ah_d=h.get_descmatrix(cdesc_matrix);
	gsl_matrix * gm_C_a=gsl_matrix_alloc(N,N);
	gsl_matrix * gm_C_up=gsl_matrix_alloc(N,N);
	gsl_matrix * gm_P=gsl_matrix_alloc(N,N);
	gsl_matrix * gm_temp=gsl_matrix_alloc(N,N);
	//initialization
	gsl_matrix_memcpy(gm_C_a,gm_P_i);
	//cycle over linear combination of linear and non linear terms
	double astep=1e-30;bool bcont=true;
	if (dalpha_ldh>0)
		gsl_matrix_scale(gm_ldh,dalpha_ldh);

	while (bcont){
	bcont=(astep<1);
	//optimal permutation
	gsl_matrix_scale(gm_C_a,-dhung_max);
	gsl_matrix_transpose(gm_C_a);
	gsl_matrix_hungarian(gm_C_a,gm_P,NULL,NULL,false,(bblast_match?gm_ldh:NULL),bgreedy);
	gsl_matrix_transpose(gm_C_a);
	gsl_matrix_scale(gm_C_a,-1.0/dhung_max);
	//C-matrix update
	gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1,gm_Ag_d,gm_P,0,gm_temp);
	gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1,gm_temp,gm_Ah_d,0,gm_C_up);
	gsl_matrix_scale(gm_C_up,1-dalpha_ldh);
	if (dalpha_ldh>0) gsl_matrix_add(gm_C_up,gm_ldh);
	gsl_matrix_scale(gm_C_a,1-astep);
	gsl_matrix_scale(gm_C_up,astep);
	gsl_matrix_add(gm_C_a,gm_C_up);
	dfvalue=f_qcv(gm_Ag_d,gm_Ah_d,gm_P,gm_temp,true);
	if (bverbose) *gout<<"astep="<<astep<<", f="<<dfvalue<<std::endl;
	astep+=0.01;//0.01*astep;
	astep=(astep<1)?astep:1;
	};
	gsl_matrix_free(gm_Ag_d);
	gsl_matrix_free(gm_Ah_d);
	gsl_matrix_free(gm_temp);
	gsl_matrix_free(gm_C_up);
	gsl_matrix_free(gm_C_a);
	if (dalpha_ldh>0)
		gsl_matrix_scale(gm_ldh,1.0/dalpha_ldh);
	
	match_result mres;
	mres.gm_P=gm_P;
	
	//initial score
	mres.vd_trace.push_back(graph_dist(g,h,cscore_matrix));
	//final score
	mres.vd_trace.push_back(graph_dist(g,h,gm_P,cscore_matrix));
	//other output parameters
	mres.dres=mres.vd_trace.at(1);
	mres.inum_iteration=2;
	//transpose matrix save
	mres.gm_P=gm_P;
	mres.gm_P_exact=NULL;
	return mres;
}
Ejemplo n.º 18
0
/** Solves A*x = B using SVD
* @param A :: [input] The matrix A
* @param B :: [input] The vector B
* @return :: The solution x
*/
std::vector<double> MaxEnt::solveSVD(const DblMatrix &A, const DblMatrix &B) {

  size_t dim = A.size().first;

  gsl_matrix *a = gsl_matrix_alloc(dim, dim);
  gsl_matrix *v = gsl_matrix_alloc(dim, dim);
  gsl_vector *s = gsl_vector_alloc(dim);
  gsl_vector *w = gsl_vector_alloc(dim);
  gsl_vector *x = gsl_vector_alloc(dim);
  gsl_vector *b = gsl_vector_alloc(dim);

  // Need to copy from DblMatrix to gsl matrix

  for (size_t k = 0; k < dim; k++)
    for (size_t l = 0; l < dim; l++)
      gsl_matrix_set(a, k, l, A[k][l]);
  for (size_t k = 0; k < dim; k++)
    gsl_vector_set(b, k, B[k][0]);

  // Singular value decomposition
  gsl_linalg_SV_decomp(a, v, s, w);

  // A could be singular or ill-conditioned. We can use SVD to obtain a least
  // squares
  // solution by setting the small (compared to the maximum) singular values to
  // zero

  // Find largest sing value
  double max = gsl_vector_get(s, 0);
  for (size_t i = 0; i < dim; i++) {
    if (max < gsl_vector_get(s, i))
      max = gsl_vector_get(s, i);
  }

  // Apply a threshold to small singular values
  const double THRESHOLD = 1E-6;
  double threshold = THRESHOLD * max;

  for (size_t i = 0; i < dim; i++)
    if (gsl_vector_get(s, i) > threshold)
      gsl_vector_set(s, i, gsl_vector_get(s, i));
    else
      gsl_vector_set(s, i, 0);

  // Solve A*x = B
  gsl_linalg_SV_solve(a, v, s, b, x);

  // From gsl_vector to vector
  std::vector<double> delta(dim);
  for (size_t k = 0; k < dim; k++)
    delta[k] = gsl_vector_get(x, k);

  gsl_matrix_free(a);
  gsl_matrix_free(v);
  gsl_vector_free(s);
  gsl_vector_free(w);
  gsl_vector_free(x);
  gsl_vector_free(b);

  return delta;
}
Ejemplo n.º 19
0
  /** \brief Constructor defining the dimensions, time step and allocating workspace. */
  numericalSchemeSDDE(const size_t dim_, const size_t nDelays_, const size_t dimWork_,
		      const double dt_)
    : dim(dim_), nDelays(nDelays_), dimWork(dimWork_), dt(dt_)
  { work = gsl_matrix_alloc(dimWork, dim); }
Ejemplo n.º 20
0
gsl_matrix* pca(gsl_matrix* feature_matrix, gsl_vector* means, float sig_limit) {

    // subtract means of columns
    for (unsigned int j = 0; j < feature_matrix->size2; j++) {
        gsl_vector_view vv = gsl_matrix_column(feature_matrix,j);
        gsl_vector* v = &vv.vector;
        gsl_vector_set(means, j, getVectorMean(v));
        gsl_vector_add_constant(v, (-1.0) * gsl_vector_get(means,j));
    }

    // initialise matrix
    SEXP m;
    double* matrix;
    PROTECT(m = allocMatrix(REALSXP, feature_matrix->size1, feature_matrix->size2));
    matrix = REAL(m);
    for (unsigned int i = 0; i < feature_matrix->size1; i++) {
        for (unsigned int j = 0; j < feature_matrix->size2; j++) {
            matrix[i+(feature_matrix->size1)*j] = gsl_matrix_get( feature_matrix, i, j);
        }
    }

    // do principal components analysis, using R
    //fprintf(stderr, "PCA\n"); fflush(stdout);
    SEXP pca;
    PROTECT(pca = R_exec("prcomp", m));							//R_exec("print", pca);
    SEXP summary;
    PROTECT(summary = R_exec("summary", pca));						//R_exec("print", summary);


    // get proportion of variance
    SEXP ev;
    PROTECT(ev = get_list_element(pca,(char*)"sdev"));						//R_exec("print",ev);
    unsigned int dim = length(ev);									//printf("dim: %i\n", dim);

    float sum_var = 0.0;
    float c_ev = 0.0;
    for (unsigned int i = 0; i < dim; i++) {
        c_ev = (REAL(ev)[i]) * (REAL(ev)[i]);
        sum_var += c_ev;
    }

    float cum_var = 0.0;
    unsigned int sig_cnt = 0;
    for (unsigned int i = 0; i < dim; i++) {
        c_ev = (REAL(ev)[i]) * (REAL(ev)[i]);
        cum_var += c_ev;							 	//printf("ev%i: %.7g\n", i, REAL(ev)[i]);
        sig_cnt++;
        if ((cum_var/sum_var) > sig_limit) break;
    }

    //fprintf(stderr, "Cumulative variance of %g reached by using %i eigen vector(s).\n" , (cum_var/sum_var), sig_cnt);

    // get loads (eigenvectors)
    SEXP loads;
    PROTECT(loads = get_list_element(pca, (char*)"rotation"));					//R_exec("print", loads);
    gsl_matrix* rot = gsl_matrix_alloc(dim, sig_cnt);
    for (unsigned int i = 0; i < dim; i++) {
        for (unsigned int j = 0; j < sig_cnt; j++) {
            gsl_matrix_set(rot, i, j, REAL(loads)[i+dim*j]);			//printf("%g \n", REAL(loads)[i+dim*j]);
        }
    }

    // de-initialise R
    UNPROTECT(4);
    end_R();
    return(rot);
}
/**
 * Generates the ringdown wave associated with the given real
 * and imaginary parts of the inspiral waveform. The parameters of
 * the ringdown, such as amplitude and phase offsets, are determined
 * by solving the linear equations defined in the DCC document T1100433.
 * In the linear equations Ax=y,
 * A is a 16-by-16 matrix depending on QNM (complex) frequencies,
 * x is a 16-d vector of the 8 unknown complex QNM amplitudes,
 * y is a 16-d vector depending on inspiral-plunge waveforms and their derivatives near merger.
 */
static INT4 XLALSimIMREOBHybridRingdownWave(
  REAL8Vector          *rdwave1,   /**<< OUTPUT, Real part of ringdown waveform */
  REAL8Vector          *rdwave2,   /**<< OUTPUT, Imag part of ringdown waveform */
  const REAL8           dt,        /**<< Sampling interval */
  const REAL8           mass1,     /**<< First component mass (in Solar masses) */
  const REAL8           mass2,     /**<< Second component mass (in Solar masses) */
  REAL8VectorSequence  *inspwave1, /**<< Values and derivs of real part inspiral waveform */
  REAL8VectorSequence  *inspwave2, /**<< Values and derivs of imag part inspiral waveform */
  COMPLEX16Vector      *modefreqs, /**<< Complex freqs of ringdown (scaled by total mass) */
  REAL8Vector          *matchrange /**<< Times which determine the comb of ringdown attachment */
  )
{

  /* XLAL error handling */
  INT4 errcode = XLAL_SUCCESS;

  /* For checking GSL return codes */
  INT4 gslStatus;

  UINT4 i, j, k, nmodes = 8;

  /* Sampling rate from input */
  REAL8 t1, t2, t3, t4, t5, rt;
  gsl_matrix *coef;
  gsl_vector *hderivs;
  gsl_vector *x;
  gsl_permutation *p;
  REAL8Vector *modeamps;
  int s;
  REAL8 tj;
  REAL8 m;

  /* mass in geometric units */
  m  = (mass1 + mass2) * LAL_MTSUN_SI;
  t5 = (matchrange->data[0] - matchrange->data[1]) * m;
  rt = -t5 / 5.;

  t4 = t5 + rt;
  t3 = t4 + rt;
  t2 = t3 + rt;
  t1 = t2 + rt;
  
  if ( inspwave1->length != 3 || inspwave2->length != 3 ||
		modefreqs->length != nmodes )
  {
    XLAL_ERROR( XLAL_EBADLEN );
  }

  /* Solving the linear system for QNMs amplitude coefficients using gsl routine */
  /* Initiate matrices and supporting variables */
  XLAL_CALLGSL( coef = (gsl_matrix *) gsl_matrix_alloc(2 * nmodes, 2 * nmodes) );
  XLAL_CALLGSL( hderivs = (gsl_vector *) gsl_vector_alloc(2 * nmodes) );
  XLAL_CALLGSL( x = (gsl_vector *) gsl_vector_alloc(2 * nmodes) );
  XLAL_CALLGSL( p = (gsl_permutation *) gsl_permutation_alloc(2 * nmodes) );

  /* Check all matrices and variables were allocated */
  if ( !coef || !hderivs || !x || !p )
  {
    if (coef)    gsl_matrix_free(coef);
    if (hderivs) gsl_vector_free(hderivs);
    if (x)       gsl_vector_free(x);
    if (p)       gsl_permutation_free(p);

    XLAL_ERROR( XLAL_ENOMEM );
  }

  /* Define the linear system Ax=y */
  /* Matrix A (2*n by 2*n) has block symmetry. Define half of A here as "coef" */
  /* The half of A defined here corresponds to matrices M1 and -M2 in the DCC document T1100433 */ 
  /* Define y here as "hderivs" */
  for (i = 0; i < nmodes; ++i)
  {
	gsl_matrix_set(coef, 0, i, 1);
	gsl_matrix_set(coef, 1, i, - cimag(modefreqs->data[i]));
	gsl_matrix_set(coef, 2, i, exp(-cimag(modefreqs->data[i])*t1) * cos(creal(modefreqs->data[i])*t1));
	gsl_matrix_set(coef, 3, i, exp(-cimag(modefreqs->data[i])*t2) * cos(creal(modefreqs->data[i])*t2));
	gsl_matrix_set(coef, 4, i, exp(-cimag(modefreqs->data[i])*t3) * cos(creal(modefreqs->data[i])*t3));
	gsl_matrix_set(coef, 5, i, exp(-cimag(modefreqs->data[i])*t4) * cos(creal(modefreqs->data[i])*t4));
	gsl_matrix_set(coef, 6, i, exp(-cimag(modefreqs->data[i])*t5) * cos(creal(modefreqs->data[i])*t5));
	gsl_matrix_set(coef, 7, i, exp(-cimag(modefreqs->data[i])*t5) * 
				      (-cimag(modefreqs->data[i]) * cos(creal(modefreqs->data[i])*t5)
				       -creal(modefreqs->data[i]) * sin(creal(modefreqs->data[i])*t5)));
	gsl_matrix_set(coef, 8, i, 0);
	gsl_matrix_set(coef, 9, i, - creal(modefreqs->data[i]));
	gsl_matrix_set(coef, 10, i, -exp(-cimag(modefreqs->data[i])*t1) * sin(creal(modefreqs->data[i])*t1));
	gsl_matrix_set(coef, 11, i, -exp(-cimag(modefreqs->data[i])*t2) * sin(creal(modefreqs->data[i])*t2));
	gsl_matrix_set(coef, 12, i, -exp(-cimag(modefreqs->data[i])*t3) * sin(creal(modefreqs->data[i])*t3));
	gsl_matrix_set(coef, 13, i, -exp(-cimag(modefreqs->data[i])*t4) * sin(creal(modefreqs->data[i])*t4));
	gsl_matrix_set(coef, 14, i, -exp(-cimag(modefreqs->data[i])*t5) * sin(creal(modefreqs->data[i])*t5));
	gsl_matrix_set(coef, 15, i, exp(-cimag(modefreqs->data[i])*t5) * 
				      ( cimag(modefreqs->data[i]) * sin(creal(modefreqs->data[i])*t5)
				       -creal(modefreqs->data[i]) * cos(creal(modefreqs->data[i])*t5)));
  }
  for (i = 0; i < 2; ++i)
  {
	gsl_vector_set(hderivs, i, inspwave1->data[(i + 1) * inspwave1->vectorLength - 1]);
	gsl_vector_set(hderivs, i + nmodes, inspwave2->data[(i + 1) * inspwave2->vectorLength - 1]);
	gsl_vector_set(hderivs, i + 6, inspwave1->data[i * inspwave1->vectorLength]);
	gsl_vector_set(hderivs, i + 6 + nmodes, inspwave2->data[i * inspwave2->vectorLength]);
  }
  gsl_vector_set(hderivs, 2, inspwave1->data[4]);
  gsl_vector_set(hderivs, 2 + nmodes, inspwave2->data[4]);
  gsl_vector_set(hderivs, 3, inspwave1->data[3]);
  gsl_vector_set(hderivs, 3 + nmodes, inspwave2->data[3]);
  gsl_vector_set(hderivs, 4, inspwave1->data[2]);
  gsl_vector_set(hderivs, 4 + nmodes, inspwave2->data[2]);
  gsl_vector_set(hderivs, 5, inspwave1->data[1]);
  gsl_vector_set(hderivs, 5 + nmodes, inspwave2->data[1]);
  
  /* Complete the definition for the rest half of A */
  for (i = 0; i < nmodes; ++i)
  {
	for (k = 0; k < nmodes; ++k)
	{
	  gsl_matrix_set(coef, i, k + nmodes, - gsl_matrix_get(coef, i + nmodes, k));
	  gsl_matrix_set(coef, i + nmodes, k + nmodes, gsl_matrix_get(coef, i, k));
	}
  }

  #if 0
  /* print ringdown-matching linear system: coefficient matrix and RHS vector */
  printf("\nRingdown matching matrix:\n");
  for (i = 0; i < 16; ++i)
  {
    for (j = 0; j < 16; ++j)
    {
      printf("%.12e ",gsl_matrix_get(coef,i,j));
    }
    printf("\n");
  }
  printf("RHS:  ");
  for (i = 0; i < 16; ++i)
  {
    printf("%.12e   ",gsl_vector_get(hderivs,i));
  }
  printf("\n");
  #endif

  /* Call gsl LU decomposition to solve the linear system */
  XLAL_CALLGSL( gslStatus = gsl_linalg_LU_decomp(coef, p, &s) );
  if ( gslStatus == GSL_SUCCESS )
  {
    XLAL_CALLGSL( gslStatus = gsl_linalg_LU_solve(coef, p, hderivs, x) );
  }
  if ( gslStatus != GSL_SUCCESS )
  {
    gsl_matrix_free(coef);
    gsl_vector_free(hderivs);
    gsl_vector_free(x);
    gsl_permutation_free(p);
    XLAL_ERROR( XLAL_EFUNC );
  }

  /* Putting solution to an XLAL vector */
  modeamps = XLALCreateREAL8Vector(2 * nmodes);

  if ( !modeamps )
  {
    gsl_matrix_free(coef);
    gsl_vector_free(hderivs);
    gsl_vector_free(x);
    gsl_permutation_free(p);
    XLAL_ERROR( XLAL_ENOMEM );
  }

  for (i = 0; i < nmodes; ++i)
  {
	modeamps->data[i] = gsl_vector_get(x, i);
	modeamps->data[i + nmodes] = gsl_vector_get(x, i + nmodes);
  }

  /* Free all gsl linear algebra objects */
  gsl_matrix_free(coef);
  gsl_vector_free(hderivs);
  gsl_vector_free(x);
  gsl_permutation_free(p);

  /* Build ring-down waveforms */

  REAL8 timeOffset = fmod( matchrange->data[1], dt/m) * dt;

  for (j = 0; j < rdwave1->length; ++j)
  {
	tj = j * dt - timeOffset;
	rdwave1->data[j] = 0;
	rdwave2->data[j] = 0;
	for (i = 0; i < nmodes; ++i)
	{
	  rdwave1->data[j] += exp(- tj * cimag(modefreqs->data[i]))
			* ( modeamps->data[i] * cos(tj * creal(modefreqs->data[i]))
			+   modeamps->data[i + nmodes] * sin(tj * creal(modefreqs->data[i])) );
	  rdwave2->data[j] += exp(- tj * cimag(modefreqs->data[i]))
			* (- modeamps->data[i] * sin(tj * creal(modefreqs->data[i]))
			+   modeamps->data[i + nmodes] * cos(tj * creal(modefreqs->data[i])) );
	}
  }

  XLALDestroyREAL8Vector(modeamps);
  return errcode;
}
StationaryCholesky::StationaryCholesky( const StationaryStructure *s,  size_t d ) : 
                                      MuDependentCholesky(s, d), myStStruct(s)  {
  myGammaK = gsl_matrix_alloc(d, d * (getMu() + 1));
}  
Ejemplo n.º 23
0
Archivo: secs2d.c Proyecto: pa345/lib
static void *
secs2d_alloc(const void * params)
{
  const magfit_parameters *mparams = (const magfit_parameters *) params;
  secs2d_state_t *state;
  const size_t flags = mparams->secs_flags;
  const double lat_spacing = mparams->lat_spacing2d;
  const double lat_max = mparams->lat_max;
  const double lat_min = mparams->lat_min;
  const size_t ntheta = (size_t) ((lat_max - lat_min) / lat_spacing + 1.0);
  const double dtheta = lat_spacing * M_PI / 180.0;
  const double theta_min = M_PI / 2.0 - lat_max * M_PI / 180.0;
  const double lon_spacing = mparams->lon_spacing;
  const double lon_max = mparams->lon_max;
  const double lon_min = mparams->lon_min;
  const double phi_min = lon_min * M_PI / 180.0;
  const double diff = wrap180(lon_max - lon_min);
  const size_t nphi = GSL_MAX((size_t) (fabs(diff) / lon_spacing + 1.0), 2);
  const double dphi = diff / (nphi - 1.0) * M_PI / 180.0;
  const size_t npoles = ntheta * nphi;
  size_t i;

  state = calloc(1, sizeof(secs2d_state_t));
  if (!state)
    return 0;

  state->nmax = 30000;
  state->n = 0;
  state->p = 0;
  state->ntheta = ntheta;
  state->nphi = nphi;
  state->R = mparams->R;
  state->flags = flags;
  state->df_offset = 0;
  state->cf_offset = 0;
  state->dtheta = dtheta;

  if (flags & MAGFIT_SECS_FLG_FIT_DF)
    {
      state->df_offset = state->p;
      state->p += npoles;
    }

  if (flags & MAGFIT_SECS_FLG_FIT_CF)
    {
      state->cf_offset = state->p;
      state->p += npoles;
    }

  state->X = gsl_matrix_alloc(state->nmax, state->p);
  state->c = gsl_vector_alloc(state->p);
  state->rhs = gsl_vector_alloc(state->nmax);
  state->wts = gsl_vector_alloc(state->nmax);
  state->cov = gsl_matrix_alloc(state->p, state->p);
  state->multifit_p = gsl_multifit_linear_alloc(state->nmax, state->p);

  state->theta0 = malloc(ntheta * sizeof(double));
  state->phi0 = malloc(nphi * sizeof(double));

  /* fill in theta0 array */
  for (i = 0; i < ntheta; ++i)
    {
      state->theta0[i] = theta_min + i * dtheta;
    }

  /* fill in phi0 array */
  for (i = 0; i < nphi; ++i)
    {
      state->phi0[i] = phi_min + i * dphi;
    }

  fprintf(stderr, "secs2d_alloc: ntheta = %zu\n", state->ntheta);
  fprintf(stderr, "secs2d_alloc: nphi   = %zu\n", state->nphi);
  fprintf(stderr, "secs2d_alloc: ncoeff = %zu\n", state->p);

  return state;
}
Ejemplo n.º 24
0
int
main (int argc, char **argv)
{
  clock_gettime (CLOCK_REALTIME, &t0);
  char message[256];

  parse_args (argc, argv);

  // PREP - read genotype file into memory
  kjg_geno* X;
  if (B)
    {
      kjg_bedIO *bp = kjg_bedIO_bfile_fopen (GENO_FILENAME, "r");
      if (!bp)
        print_usage ("Unable to open bed/bim/fam");

      uint8_t* SNPmask = 0;
      if (SNP_MASK)
        {
          SNPmask = read_mask (bp->m, SNP_MASK);
          if (!SNPmask)
            print_usage ("Unable to read SNP mask");
        }

      uint8_t* indmask = 0;
      if (IND_MASK)
        {
          indmask = read_mask (bp->m, IND_MASK);
          if (!indmask)
            print_usage ("Unable to read individual mask");
        }

      sprintf (message, "Reading geno (%dx%d)", bp->m, bp->n);
      timelog (message);

      X = kjg_bedIO_fread_geno (bp, SNPmask, indmask);
      kjg_bedIO_fclose (bp);

      if (SNPmask)
        free (SNPmask);
      if (indmask)
        free (indmask);

      sprintf (message, "Finished reading geno (%dx%d)", X->m, X->n);
      timelog (message);
    }
  else
    {
      kjg_genoIO *gp = kjg_genoIO_fopen (GENO_FILENAME, "r");
      if (!gp)
        {
          print_usage ("Unable to open geno");
        }

      sprintf (message, "Reading geno (%dx%d)", gp->m, gp->n);
      timelog (message);

      X = kjg_genoIO_fread_geno (gp);
      kjg_genoIO_fclose (gp);
    }

  // calculate the SNP means
  timelog ("Calculating SNP allele frequencies");
  kjg_geno_set_norm (X, 0);

  // run fast PCA
  gsl_vector* eval = gsl_vector_alloc (K);
  gsl_matrix* evec = gsl_matrix_alloc (X->n, K);

  timelog ("fastPCA started");
  kjg_fpca (X, eval, evec, L, I);
  timelog ("fastPCA completed");

  FILE *fh_evec = kjg_util_fopen_suffix (OUTPUT_PREFIX, "evec", "w");
  kjg_gsl_evec_fprintf (fh_evec, eval, evec, "%g");
  fclose (fh_evec);

  timelog ("Done");
  return (0);
}
Ejemplo n.º 25
0
int main(void)
{

	gsl_vector* x = gsl_vector_alloc(N_DATA);
	gsl_vector* y = gsl_vector_calloc(N_DATA);
	gsl_vector* dy = gsl_vector_alloc(N_DATA);
	gsl_vector* c = gsl_vector_alloc(4);
	gsl_vector* dc = gsl_vector_alloc(4);
	gsl_matrix* S = gsl_matrix_alloc(4,4);
	int i, j, k;
	double xi, dx, yi, dyi, fjxi;
	double c_true[] = {5, 10, 2, 3};


	// generate some data with arbitrary "noise" and uncertanties
	dx = (XF - XI)/N_DATA;
	for(i=0; i<N_DATA; i++)
	{
		xi = XI + i*dx;
		gsl_vector_set(x,i,xi);
		yi = NOISE*sin(xi+i);
		for(j=0; j<4; j++)
		{
			yi += c_true[j]*my_func(j,xi);
		}
		gsl_vector_set(y,i,yi);
		gsl_vector_set(dy,i,0.6+0.3*sin(i*i));
	}

	// do the actual fitting
	ls_workspace* w = ls_workspace_alloc(N_DATA,4);
	ls_fit(x,y,dy,my_func,c,S,w);
	ls_workspace_free(w);

	// output datapoints for plotting
	for(i=0; i<N_DATA; i++)
	{
		fprintf(stderr,"%g\t%g\t%g\n",gsl_vector_get(x,i),
								gsl_vector_get(y,i),
								gsl_vector_get(dy,i));
	}

	// output fitting parameters to stdout
	printf("The fitting parameters are:\n");
	printf("i\tc_true\tc_fit\tdc\n");
	for(i=0; i<4; i++)
	{
		gsl_vector_set(dc,i,sqrt(gsl_matrix_get(S,i,i)));
		printf("%d\t%.4g\t%.4g\t%.4g\n",i,c_true[i],gsl_vector_get(c,i),gsl_vector_get(dc,i));
	}
	
	// output the fitted function values to file A_fit.dat
	// with an estimate of the uncertainty on y from the covariance matrix
	dx = (XF - XI)/1000;
	fprintf(stderr,"\n\n");
	for(i=0; i<1000; i++)
	{
		xi = XI + i*dx;
		yi = 0;
		dyi = 0;

		for(j=0; j<4; j++)
		{
			fjxi = my_func(j,xi);
			yi += gsl_vector_get(c,j)*fjxi;
			dyi += gsl_matrix_get(S,j,j)*fjxi*fjxi;
			for(k=0; k<j; k++)
			{
				dyi += 2*gsl_matrix_get(S,j,k)*fjxi*my_func(k,xi);
			}
		}
		dyi = sqrt(dyi);
		fprintf(stderr,"%g\t%g\t%g\t%g\n",xi,yi,yi+dyi,yi-dyi);
	}

	gsl_vector_free(x);
	gsl_vector_free(y);
	gsl_vector_free(dy);
	gsl_vector_free(c);
	gsl_vector_free(dc);
	gsl_matrix_free(S);
}
Ejemplo n.º 26
0
Archivo: SVD.c Proyecto: s-low/physics
int main(void) {
	int n; //dimension of matrix
	int i, j;

	printf("Enter the number of rows and columns n:");
	scanf("%d", &n);

	gsl_matrix * A = gsl_matrix_alloc(n, n);
	gsl_matrix * V = gsl_matrix_alloc(n, n);
	gsl_matrix * Sm = gsl_matrix_alloc(n, n);
	gsl_matrix * X = gsl_matrix_alloc(n, n);
	gsl_matrix * Y = gsl_matrix_alloc(n, n);

	gsl_vector * S = gsl_vector_alloc(n); //only the diagonal elements of S
	gsl_vector * work = gsl_vector_alloc(n); //workspace for GSL decomp

	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++) {
			gsl_matrix_set(A, i, j, (20 * rand()) / (double) RAND_MAX);
		}

	for (i = 0; i < n; i++)
		for (j = 0; j < n; j++)
			printf("A(%d,%d) = %g\n", i, j, gsl_matrix_get(A, i, j));

	printf("\n\n");

	gsl_linalg_SV_decomp(A, V, S, work);

	// A decomposed into: A = U S V
	// A now replaced by U
	// V NOT TRANSPOSED by GSL yet
	// find Pseudo inverse of S, then calculate: A(PI) = V S(PI) U^T

	for (i = 0; i < n; i++) {
		if (gsl_vector_get(S, i) != 0) {
			gsl_matrix_set(Sm, i, i, (1 / gsl_vector_get(S, i)));
		} else {
			gsl_matrix_set(Sm, i, i, (gsl_vector_get(S, i)));
		}
	}

//now Sm is matrix with 1/S elements on diagonal
//V * S * U^T is now the aim

//FIND: A(Ps.Inv) = V Sm(Ps.Inv) U^T
// X = Sm*U
// Y = V*X = V*Sm*U

	gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, Sm, A, 0.0, X); //multiply Sm.A, put in X
	gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, V, X, 0.0, Y); //multiply V.X, put in Y


	//Y now stores the inverse
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			printf("Inv(%d,%d) = %g\n", i, j, gsl_matrix_get(Y, i, j));
		}
	}

}
Ejemplo n.º 27
0
static
int fit_tan_wcs_solve(const double* starxyz,
					const double* fieldxy,
					const double* weights,
					int N,
					const double* crpix,
					const tan_t* tanin,
					tan_t* tanout,
					double* p_scale) {
	int i, j, k;
	double field_cm[2] = {0, 0};
	double cov[4] = {0, 0, 0, 0};
	double R[4] = {0, 0, 0, 0};
	double scale;
	// projected star coordinates
	double* p;
	// relative field coordinates
	double* f;
	double pcm[2] = {0, 0};
	double w = 0;
	double totalw;

    gsl_matrix* A;
    gsl_matrix* U;
    gsl_matrix* V;
    gsl_vector* S;
    gsl_vector* work;
    gsl_matrix_view vcov;
    gsl_matrix_view vR;

	double crxyz[3];

	double star_cm[3] = {0, 0, 0};

	assert(((tanin != NULL) && (crpix != NULL)) || ((tanin == NULL) && (crpix == NULL)));

	if (tanin) {
		// default vals...
		memcpy(tanout, tanin, sizeof(tan_t));
	} else {
		memset(tanout, 0, sizeof(tan_t));
	}

	// -allocate and fill "p" and "f" arrays. ("projected" and "field")
	p = malloc(N * 2 * sizeof(double));
	f = malloc(N * 2 * sizeof(double));

	// -get field center-of-mass
	totalw = 0.0;
	for (i=0; i<N; i++) {
		w = (weights ? weights[i] : 1.0);
		field_cm[0] += w * fieldxy[i*2 + 0];
		field_cm[1] += w * fieldxy[i*2 + 1];
		totalw += w;
	}
	field_cm[0] /= totalw;
	field_cm[1] /= totalw;
	// Subtract it out.
	for (i=0; i<N; i++) {
		f[2*i+0] = fieldxy[2*i+0] - field_cm[0];
		f[2*i+1] = fieldxy[2*i+1] - field_cm[1];
	}

	if (tanin) {
		// Use original WCS to set the center of projection to the new crpix.
		tan_pixelxy2xyzarr(tanin, crpix[0], crpix[1], crxyz);
		for (i=0; i<N; i++) {
			Unused anbool ok;
			// -project the stars around crval
			ok = star_coords(starxyz + i*3, crxyz, TRUE, p + 2*i, p + 2*i + 1);
			assert(ok);
		}
	} else {
		// -get the star center-of-mass (this will become the tangent point CRVAL)
		for (i=0; i<N; i++) {
			w = (weights ? weights[i] : 1.0);
			star_cm[0] += w * starxyz[i*3 + 0];
			star_cm[1] += w * starxyz[i*3 + 1];
			star_cm[2] += w * starxyz[i*3 + 2];
		}
		normalize_3(star_cm);
		// -project the stars around their center of mass
		for (i=0; i<N; i++) {
			Unused anbool ok;
			ok = star_coords(starxyz + i*3, star_cm, TRUE, p + 2*i, p + 2*i + 1);
			assert(ok);
		}
	}

	// -compute the center of mass of the projected stars and subtract it out.
	for (i=0; i<N; i++) {
		w = (weights ? weights[i] : 1.0);
		pcm[0] += w * p[2*i + 0];
		pcm[1] += w * p[2*i + 1];
	}
	pcm[0] /= totalw;
	pcm[1] /= totalw;
	for (i=0; i<N; i++) {
		p[2*i + 0] -= pcm[0];
		p[2*i + 1] -= pcm[1];
	}

	// -compute the covariance between field positions and projected
	//  positions of the corresponding stars.
	for (i=0; i<N; i++)
		for (j=0; j<2; j++)
			for (k=0; k<2; k++)
				cov[j*2 + k] += p[i*2 + k] * f[i*2 + j];

	for (i=0; i<4; i++)
        assert(isfinite(cov[i]));

	// -run SVD
    V = gsl_matrix_alloc(2, 2);
    S = gsl_vector_alloc(2);
    work = gsl_vector_alloc(2);
    vcov = gsl_matrix_view_array(cov, 2, 2);
    vR   = gsl_matrix_view_array(R, 2, 2);
    A = &(vcov.matrix);
    // The Jacobi version doesn't always compute an orthonormal U if S has zeros.
    //gsl_linalg_SV_decomp_jacobi(A, V, S);
    gsl_linalg_SV_decomp(A, V, S, work);
    // the U result is written to A.
    U = A;
    gsl_vector_free(S);
    gsl_vector_free(work);
    // R = V U'
    gsl_blas_dgemm(CblasNoTrans, CblasTrans, 1.0, V, U, 0.0, &(vR.matrix));
    gsl_matrix_free(V);

	for (i=0; i<4; i++)
        assert(isfinite(R[i]));

	// -compute scale: make the variances equal.
	{
		double pvar, fvar;
		pvar = fvar = 0.0;
		for (i=0; i<N; i++) {
			w = (weights ? weights[i] : 1.0);
			for (j=0; j<2; j++) {
				pvar += w * square(p[i*2 + j]);
				fvar += w * square(f[i*2 + j]);
			}
		}
		scale = sqrt(pvar / fvar);
	}

	// -compute WCS parameters.
	scale = rad2deg(scale);

	tanout->cd[0][0] = R[0] * scale; // CD1_1
	tanout->cd[0][1] = R[1] * scale; // CD1_2
	tanout->cd[1][0] = R[2] * scale; // CD2_1
	tanout->cd[1][1] = R[3] * scale; // CD2_2

    assert(isfinite(tanout->cd[0][0]));
    assert(isfinite(tanout->cd[0][1]));
    assert(isfinite(tanout->cd[1][0]));
    assert(isfinite(tanout->cd[1][1]));

	if (tanin) {
		// CRPIX is fixed.
		tanout->crpix[0] = crpix[0];
		tanout->crpix[1] = crpix[1];
		// Set CRVAL temporarily...
		tan_pixelxy2radec(tanin, crpix[0], crpix[1],
						  tanout->crval+0, tanout->crval+1);
		// Shift CRVAL so that the center of the quad is in the right place.
		{
			double ix,iy;
			double dx,dy;
			double dxyz[3];
			tan_pixelxy2iwc(tanout, field_cm[0], field_cm[1], &ix, &iy);
			dx = rad2deg(pcm[0]) - ix;
			dy = rad2deg(pcm[1]) - iy;
			tan_iwc2xyzarr(tanout, dx, dy, dxyz);
			xyzarr2radecdeg(dxyz, tanout->crval + 0, tanout->crval + 1);
		}
	} else {
		tanout->crpix[0] = field_cm[0];
		tanout->crpix[1] = field_cm[1];
		
		xyzarr2radecdegarr(star_cm, tanout->crval);

		// FIXME -- we ignore pcm.  It should get added back in (after
		// multiplication by CD in the appropriate units) to either crval or
		// crpix.  It's a very small correction probably of the same size
		// as the other approximations we're making.
	}

	if (p_scale) *p_scale = scale;
	free(p);
	free(f);
    return 0;
}
Ejemplo n.º 28
0
size_t smf_clean_pca( ThrWorkForce *wf, smfData *data, size_t t_first,
                      size_t t_last, double thresh, size_t ncomp,
                      smfData **components, smfData **amplitudes,
                      int flagbad, int sub, AstKeyMap *keymap,
                      smf_qual_t mask, int *status ){

  double *amp=NULL;       /* matrix of components amplitudes for each bolo */
  size_t abstride;        /* bolo stride in amp array */
  size_t acompstride;     /* component stride in amp array */
  size_t bstride;         /* bolo stride */
  double *comp=NULL;      /* data cube of components */
  size_t ccompstride;     /* component stride in comp array */
  size_t ctstride;        /* time stride in comp array */
  gsl_matrix *cov=NULL;   /* bolo-bolo covariance matrix */
  size_t i;               /* Loop counter */
  int ii;                 /* Loop counter */
  size_t j;               /* Loop counter */
  smfPCAData *job_data=NULL;/* job data */
  size_t k;               /* Loop counter */
  size_t *goodbolo=NULL;  /* Indices of the good bolometers for analysis */
  dim_t nbolo;            /* number of bolos */
  dim_t ndata;            /* number of samples in data */
  size_t ngoodbolo;       /* number good bolos = number principal components */
  dim_t ntslice;          /* number of time slices */
  int nw;                 /* total available worker threads */
  smfPCAData *pdata=NULL; /* Pointer to job data */
  smf_qual_t *qua=NULL;   /* Pointer to quality array */
  gsl_vector *s=NULL;     /* singular values for SVD */
  size_t bstep;           /* Bolo step size for job division */
  size_t step;            /* step size for job division */
  size_t tlen;            /* Length of the time-series used for PCA */
  size_t tstride;         /* time slice stride */
  gsl_vector *work=NULL;  /* workspace for SVD */

  if (*status != SAI__OK) return 0;

  /* How many threads do we get to play with */
  nw = wf ? wf->nworker : 1;

  /* Check for NULL smfData pointer */
  if( !data || !data->pntr[0]) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME
            ": possible programming error, NULL data supplied", status );
    return 0;
  }

  smf_get_dims( data, NULL, NULL, &nbolo, &ntslice, &ndata, &bstride, &tstride,
                status );

  if( data->ndims != 3 ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME
            ": possible programming error, smfData should be 3-dimensional",
            status );
    return 0;
  }

  if( data->dtype != SMF__DOUBLE ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME
            ": possible programming error, smfData should be double precision",
            status );
    return 0;
  }

  if( ntslice <= 2 ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME ": fewer than 2 time slices!", status );
    goto CLEANUP;
  }

  /* If the range of time slices has not been specified, us the total
     range excluding padding and apodizing. */
  qua = smf_select_qualpntr( data, 0, status );
  if( !t_last ) {
     if( qua ) {
        smf_get_goodrange( qua, ntslice, tstride, (SMF__Q_PAD | SMF__Q_APOD),
                           &t_first, &t_last, status );
     } else {
        t_last = ntslice-1;
     }
  }

  if( t_last > (ntslice-1) ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME ": t_last is set past the last time slice!",
            status );
    goto CLEANUP;
  }

  if( (t_last < t_first) || ( (t_last - t_first) < 1 ) ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME ": t_last - t_first must be > 1", status );
    goto CLEANUP;
  }

  tlen = t_last - t_first + 1;

  if( flagbad && (tlen != ntslice ) ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME
            ": flagbad unsupported if t_first/last do not span full data",
            status );
    goto CLEANUP;
  }

  if( qua ) {
    /* If quality supplied, identify good bolometers */
    ngoodbolo = 0;
    for( i=0; i<nbolo; i++ ) {
      if( !(qua[i*bstride]&SMF__Q_BADB) ) {
        ngoodbolo++;
      }
    }

    /* Now remember which were the good bolometers */
    goodbolo = astCalloc( ngoodbolo, sizeof(*goodbolo) );
    ngoodbolo = 0;
    for( i=0; i<nbolo; i++ ) {
      if( !(qua[i*bstride]&SMF__Q_BADB) ) {
        goodbolo[ngoodbolo] = i;
        ngoodbolo++;
      }
    }

  } else {
    /* Otherwise assume all bolometers are good */
    ngoodbolo = nbolo;
    goodbolo = astCalloc( ngoodbolo, sizeof(*goodbolo) );
    for( i=0; i<ngoodbolo; i++ ) {
      goodbolo[i] = i;
    }
  }

  if( ngoodbolo <= 2 ) {
    *status = SAI__ERROR;
    errRep( " ", FUNC_NAME ": fewer than 2 working bolometers!", status );
    goto CLEANUP;
  }

  /* Fill bad values and values flagged via "mask" (except entirely bad
     bolometers) with interpolated data values. */
  mask &= ~SMF__Q_BADB;
  smf_fillgaps( wf, data, mask, status );

  /* Allocate arrays */
  amp = astCalloc( nbolo*ngoodbolo, sizeof(*amp) );
  comp = astCalloc( ngoodbolo*tlen, sizeof(*comp) );
  cov = gsl_matrix_alloc( ngoodbolo, ngoodbolo );
  s = gsl_vector_alloc( ngoodbolo );
  work = gsl_vector_alloc( ngoodbolo );

  /* These strides will make comp time-ordered */
  ccompstride = 1;
  ctstride = ngoodbolo;

  /* These strides will also make amp look time-ordered (sort-of: the time
     axis is now the component number */
  abstride = 1;
  acompstride = nbolo;

  /* Allocate job data for threads */
  job_data = astCalloc( nw, sizeof(*job_data) );

  /* Set up the division of labour for threads: independent blocks of time */

  if( nw > (int) tlen ) {
    step = 1;
  } else {
    step = tlen/nw;
  }

  if( nw > (int) ngoodbolo ) {
    bstep = 1;
  } else {
    bstep = ngoodbolo/nw;
  }

  for( ii=0; (*status==SAI__OK)&&(ii<nw); ii++ ) {
    pdata = job_data + ii;

    /* Blocks of time slices */
    pdata->t1 = ii*step + t_first;
    pdata->t2 = (ii+1)*step + t_first - 1;

    /* Blocks of bolometers. */
    pdata->b1 = ii*bstep;
    pdata->b2 = (ii+1)*bstep - 1;

    /* Ensure that the last thread picks up any left-over tslices */
    if( (ii==(nw-1)) ) {
       pdata->t2 = t_first + tlen - 1;
       pdata->b2 = ngoodbolo - 1;
    }

    /* initialize work data */
    pdata->amp = NULL;
    pdata->abstride = abstride;
    pdata->acompstride = acompstride;
    pdata->bstride = bstride;
    pdata->comp = comp;
    pdata->cov = NULL;
    pdata->covwork = NULL;
    pdata->ccompstride = ccompstride;
    pdata->ctstride = ctstride;
    pdata->data = data;
    pdata->goodbolo = NULL;
    pdata->ijob = -1;
    pdata->nbolo = nbolo;
    pdata->ngoodbolo = ngoodbolo;
    pdata->t_first = t_first;
    pdata->t_last = t_last;
    pdata->tlen = tlen;
    pdata->operation = 0;
    pdata->tstride = tstride;

    /* Each thread will accumulate the projection of its own portion of
       the time-series. We'll add them to the master amp at the end */
    pdata->amp = astCalloc( nbolo*ngoodbolo, sizeof(*(pdata->amp)) );

    /* Each thread will accumulate sums of x, y, and x*y for each bolo when
       calculating the covariance matrix */
    pdata->covwork = astCalloc( ngoodbolo*ngoodbolo,
                                sizeof(*(pdata->covwork)) );

    /* each thread gets its own copy of the goodbolo lookup table */
    pdata->goodbolo = astCalloc( ngoodbolo, sizeof(*(pdata->goodbolo)) );
    if( *status == SAI__OK ) {
      memcpy( pdata->goodbolo, goodbolo,
              ngoodbolo*sizeof(*(pdata->goodbolo)) );
    }

  }

  if( *status == SAI__OK ) {

    /* Remove the mean from each gap-filled bolometer time stream ---------------------*/

    msgOutif( MSG__VERB, "", FUNC_NAME ": removing bolometer means...",
              status );

    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;
      pdata->operation = -1;
      thrAddJob( wf, 0, pdata, smfPCAParallel, 0, NULL, status );
    }

    /* Wait until all of the submitted jobs have completed */
    thrWait( wf, status );



    /* Measure the covariance matrix using parallel code ---------------------*/

    msgOutif( MSG__VERB, "", FUNC_NAME
              ": measuring bolo-bolo covariance matrix...", status );

    /* Set up the jobs to calculate sums for each time block and submit */
    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;
      pdata->operation = 0;
      pdata->ijob = thrAddJob( wf, THR__REPORT_JOB, pdata, smfPCAParallel,
                                 0, NULL, status );
    }

    /* Wait until all of the submitted jobs have completed */
    thrWait( wf, status );

    /* We now have to add together all of the sums from each thread and
       normalize */
    if( *status == SAI__OK ) {
      for( i=0; i<ngoodbolo; i++ ) {
        for( j=i; j<ngoodbolo; j++ ) {
          double c;
          double *covwork=NULL;
          double sum_xy;

          sum_xy = 0;

          for( ii=0; ii<nw; ii++ ) {
            pdata = job_data + ii;
            covwork = pdata->covwork;

            sum_xy += covwork[ i + j*ngoodbolo ];
          }

          c = sum_xy / ((double)tlen-1);

          gsl_matrix_set( cov, i, j, c );
          gsl_matrix_set( cov, j, i, c );
        }
      }
    }
  }

  /* Factor cov = u s v^T, noting that the SVD routine calculates v^T in
     in-place of cov. --------------------------------------------------------*/

  msgOutif( MSG__VERB, "", FUNC_NAME
            ": perfoming singular value decomposition...", status );

  smf_svd( wf, ngoodbolo, cov->data, s->data, NULL, 10*VAL__EPSD,
           1, status );
  if( CHECK ) {
    double check=0;

    for( i=0; i<ngoodbolo; i++ ) {
      for( j=0; j<ngoodbolo; j++ ) {
        check += gsl_matrix_get( cov, j, i );
      }
    }

    printf("--- check inverted: %lf\n", check);
  }

  /* Calculate normalized eigenvectors with parallel code --------------------*/

  msgOutif( MSG__VERB, "", FUNC_NAME
            ": calculating statistically-independent components...", status );

  /* The above calculation tells us what linear combinations of the original
     bolometer time series will give us the statistically independent new
     set of basis vectors (components), which we then normalize by their RMS. */

  /* Set up the jobs to calculate sums for each time block and submit */
  if( *status == SAI__OK ) {
    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;
      pdata->cov = cov;
      pdata->operation = 1;
      pdata->ijob = thrAddJob( wf, THR__REPORT_JOB, pdata, smfPCAParallel,
                                 0, NULL, status );
    }
  }

  /* Wait until all of the submitted jobs have completed */
  thrWait( wf, status );

  /* Then normalize. Some of the components may have zero amplitude and
     so cannot be used (i.e. we are trying to use more components than
     there is evidence for in the data). So we check for zero sigma. In
     fact, we check for silly small sigma, not just zero sigma. Any
     component for which the sigma is less than 1E-10 of the log-mean
     sigma is excluded. */
  {
    double *sigmas = astMalloc( ngoodbolo*sizeof( *sigmas ) );
    double check = 0;
    double s1 = 0.0;
    int s2 = 0;
    int nlow = 0;

    for( i=0; (*status==SAI__OK)&&(i<ngoodbolo); i++ ) {
      double sigma;

      smf_stats1D( comp + i*ccompstride, ctstride, tlen, NULL, 0,
                   0, NULL, &sigma, NULL, NULL, status );

      /* Apparently we need this to get the normalization right */
      sigma *= sqrt((double) tlen);

      if( *status == SAI__OK ) {
        if( sigma > 0.0 ) {
           for( k=0; k<tlen; k++ ) {
             comp[i*ccompstride + k*ctstride] /= sigma;
             sigmas[ i ] = sigma;
             s1 += log10( sigma );
             s2++;
           }
        } else {
           for( k=0; k<tlen; k++ ) {
             comp[i*ccompstride + k*ctstride] = VAL__BADD;
             sigmas[ i ] = VAL__BADD;
           }
           nlow++;
        }
      }
    }

    /* Exclude any components that have a silly small standard deviation
       (less that 1E-10 of the logmean of all components). Any with zero
       standard deviation will already have been excluded. */
    if( s2 > 0 ) {
       double logmean = s1/s2;
       for( i=0; i<ngoodbolo; i++ ) {
          if( sigmas[ i ] != VAL__BADD && sigmas[ i ] < 1E-10*logmean ) {
             for( k=0; k<tlen; k++ ) {
                comp[i*ccompstride + k*ctstride] = VAL__BADD;
                nlow++;
             }
          }
       }
    }

    msgOutiff( MSG__DEBUG, "", FUNC_NAME ": rejecting %d (out of %zu) components"
               " because they are too weak to normalise", status, nlow, ngoodbolo );

    for( i=0; i<ngoodbolo*tlen; i++ ) {
      if( comp[i] != VAL__BADD ) check += comp[i];
    }

    sigmas = astFree( sigmas );

    //printf("--- check component: %lf\n", check);
  }
  /* Now project the data along each of these normalized basis vectors
     to figure out the amplitudes of the components in each bolometer
     time series. ------------------------------------------------------------*/

  msgOutif( MSG__VERB, "", FUNC_NAME
              ": calculating component amplitudes in each bolo...", status );

  /* Set up the jobs  */
  if( *status == SAI__OK ) {
    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;
      pdata->operation = 2;
      pdata->ijob = thrAddJob( wf, THR__REPORT_JOB, pdata, smfPCAParallel,
                                 0, NULL, status );
    }
  }

  /* Wait until all of the submitted jobs have completed */
  thrWait( wf, status );

  /* Add all of the amp arrays together from the threads */
  if( *status == SAI__OK ) {
    size_t index;

    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;

      for( i=0; i<ngoodbolo; i++ ) {        /* Loop over good bolo */
        for( j=0; j<ngoodbolo; j++ ) {      /* Loop over component */
          index = goodbolo[i]*abstride + j*acompstride;
          amp[index] += pdata->amp[index];
        }
      }
    }
  }

  if( CHECK ){
    double check=0;

    for( i=0; i<nbolo*ngoodbolo; i++ ) {
      check += amp[i];
    }
    printf("--- check combined amp: %lf\n", check);
  }

  if( CHECK ){
    double check=0;
    for( i=0; i<ngoodbolo*tlen; i++ ) {
      if( comp[i] != VAL__BADD ) check += comp[i];
    }

    printf("--- check component A: %lf\n", check);
  }

  /* Check to see if the amplitudes are mostly negative or positive. If
     mostly negative, flip the sign of both the component and amplitudes */
  if( *status == SAI__OK ) {
    double total;
    for( j=0; j<ngoodbolo; j++ ) {    /* loop over component */
      total = 0;
      for( i=0; i<ngoodbolo; i++ ) {  /* loop over bolometer */
        total += amp[goodbolo[i]*abstride + j*acompstride];
      }

      /* Are most amplitudes negative for this component? */
      if( total < 0 ) {
        /* Flip sign of the amplitude */
        for( i=0; i<ngoodbolo; i++ ) { /* loop over bolometer */
          amp[goodbolo[i]*abstride + j*acompstride] =
            -amp[goodbolo[i]*abstride + j*acompstride];
        }

        /* Flip sign of the component */
        for( k=0; k<tlen; k++ ) {
           if(  comp[j*ccompstride + k*ctstride] != VAL__BADD ) {
              comp[j*ccompstride + k*ctstride] *= -1;
           }
        }
      }
    }
  }

  /* Finally, copy the master amp array back into the workspace for
     each thread */
  if( *status == SAI__OK ) {
    for( ii=0; ii<nw; ii++ ) {
      pdata = job_data + ii;
      memcpy( pdata->amp, amp, sizeof(*(pdata->amp))*nbolo*ngoodbolo );
    }
  }

  if( CHECK ){
    double check=0;
    for( i=0; i<ngoodbolo*tlen; i++ ) {
      if( comp[i] != VAL__BADD ) check += comp[i];
    }

    printf("--- check component B: %lf\n", check);
  }

  /* Flag outlier bolometers if requested ------------------------------------*/

  if( (*status==SAI__OK) && flagbad ) {
    smfArray *data_array=NULL;
    smfArray *gain_array=NULL;
    smfGroup *gain_group=NULL;
    AstKeyMap *kmap=NULL;         /* Local keymap */
    AstObject *obj=NULL;          /* Used to avoid compiler warnings */
    double *template=NULL;
Ejemplo n.º 29
0
	int maxGens = atoi (argv[1]);
	
	// We do need to set up the generator in order to pass it initialized to the DE routine
	const gsl_rng_type *tipoRNG;
	gsl_rng_env_setup();
	
	tipoRNG = gsl_rng_ranlxs2;
	r = gsl_rng_alloc(tipoRNG);
	
	// Seed the generator 
	long seed = time(NULL);
	gsl_rng_set(r, seed);
	printf("Seed = %ld\n", seed);
	
	// Initialize the global matrix and vectors
	solutions = gsl_matrix_alloc (popSize, dims);	
	costs = (double *) malloc (steps * sizeof (*costs));//gsl_vector_alloc (popSize);

        experiments = gsl_matrix_alloc (steps, 22);

	//expCa = (double *) malloc (steps * sizeof (*expCa)); //gsl_vector_alloc (steps);
	//expV = (double *) malloc (steps * sizeof (*expCa)); //gsl_vector_alloc (steps);

	//Use Differential Evolution Algorithm to renew
	// DE (maxGenerations, populationSize, CR, dimensions, F, RNG, fitnessFunction);
	// 26 dimensions. 22 binary thresholds, 4 ternary thresholds.
	DE (maxGens, popSize, 0.9, dims, 0.5, r, evaluator);

	// At this point, we DO have results. Let's report them
	{
		FILE * fThrs = fopen ("dataPSM/thresholds.dat", "w");
int main()
{

	int i,j;

    gsl_matrix *a;   
    gsl_matrix *b;  

    a = gsl_matrix_alloc(dimension, dimension); /* Data matrix */
    b = gsl_matrix_alloc(dimension, dimension); /* Data matrix */

    srand(42);
	for (i=0;i<dimension;i++)
	{
		for (j=0;j<dimension;j++)
		{
			gsl_matrix_set(a, i, j, (rand()%100)/100.0); 
            gsl_matrix_set(b, i, j, (rand()%100)/100.0);
		}
		//printf("\n");
	}  

    gsl_matrix *res= gsl_matrix_alloc(dimension,dimension);


    if (PRINT_RES)
	{
        printf("a=\n");
        for (i=0;i<dimension;i++)
        {
            for (j=0;j<dimension;j++)
            {
                printf("%g ",gsl_matrix_get(a, i, j)); 
            }
            printf("\n");
        }  
        printf("\n");
	}
    if (PRINT_RES)
	{
        printf("b=\n");
        for (i=0;i<dimension;i++)
        {
            for (j=0;j<dimension;j++)
            {
                printf("%g ",gsl_matrix_get(b, i, j)); 
            }
            printf("\n");
        }  
         printf("\n");
	}


    gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, a, b, 0.0, res); 


    if (PRINT_RES)
	{
        printf("res=\n");
        for (i=0;i<dimension;i++)
        {
            for (j=0;j<dimension;j++)
            {
                printf("%g ",gsl_matrix_get(res, i, j)); 
            }
            printf("\n");
        }  
	}

    return 0;
}