gsl_vector_complex * CRebuildGraph::calculateEgeinval (gsl_matrix *target) { int order = (int)target->size1; gsl_vector_complex *eval = gsl_vector_complex_alloc (order); gsl_matrix_complex *evec = gsl_matrix_complex_alloc (order, order); gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc (order); gsl_eigen_nonsymmv (target, eval, evec, w); gsl_eigen_nonsymmv_free (w); gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_DESC); { int i, j; for (i = 0; i < order; i++) { gsl_complex eval_i = gsl_vector_complex_get (eval, i); gsl_vector_complex_view evec_i = gsl_matrix_complex_column (evec, i); printf ("eigenvalue = %g + %gi\n", GSL_REAL(eval_i), GSL_IMAG(eval_i)); printf ("eigenvector = \n"); for (j = 0; j < order; ++j) { /* gsl_complex z = */ gsl_vector_complex_get(&evec_i.vector, j); // printf("%g + %gi\n", GSL_REAL(z), GSL_IMAG(z)); } } } // gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); return eval; }
static VALUE rb_gsl_linalg_complex_LU_sgndet(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *m = NULL, *mtmp = NULL; gsl_permutation *p = NULL; gsl_complex *z = NULL; VALUE vz; int flagm = 0, signum, itmp; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: CHECK_MATRIX_COMPLEX(argv[0]); Data_Get_Struct(argv[0], gsl_matrix_complex, m); if (CLASS_OF(argv[0]) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 1; break; default: Data_Get_Struct(obj, gsl_matrix_complex, m); if (CLASS_OF(obj) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 0; } if (flagm == 1) { p = gsl_permutation_alloc(m->size1); gsl_linalg_complex_LU_decomp(mtmp, p, &signum); } else { if (itmp != argc-1) rb_raise(rb_eArgError, "signum not given"); signum = NUM2DBL(argv[itmp]); } vz = Data_Make_Struct(cgsl_complex, gsl_complex, 0, free, z); *z = gsl_linalg_complex_LU_sgndet(mtmp, signum); if (flagm == 1) { gsl_matrix_complex_free(mtmp); gsl_permutation_free(p); } return vz; }
double R0(const double theta[numParam], const double r0time, double * eigenvec) { gsl_matrix * Fmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * Vmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * VmatInv = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * ngm = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); createNGM(theta, r0time, Fmat, Vmat); gsl_permutation * p = gsl_permutation_alloc(NG*(DS-1)*RG); int s; gsl_linalg_LU_decomp(Vmat, p, &s); gsl_linalg_LU_invert(Vmat, p, VmatInv); gsl_blas_dgemm(CblasNoTrans, CblasNoTrans, 1.0, Fmat, VmatInv, 0.0, ngm); gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(NG*(DS-1)*RG); gsl_vector_complex * eval = gsl_vector_complex_alloc(NG*(DS-1)*RG); gsl_matrix_complex * evec = gsl_matrix_complex_alloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_set_error_handler_off(); gsl_eigen_nonsymmv(ngm, eval, evec, w); size_t r0_idx = 0; double r0 = 0.0; for(size_t i = 0; i < NG*(DS-1)*RG; i++){ if(GSL_REAL(gsl_vector_complex_get(eval, i)) > r0){ r0_idx = i; r0 = GSL_REAL(gsl_vector_complex_get(eval, i)); } } if(eigenvec != NULL){ for(size_t i = 0; i < NG*(DS-1)*RG; i++) eigenvec[i] = GSL_REAL(gsl_matrix_complex_get(evec, i, r0_idx)); } gsl_matrix_free(Fmat); gsl_matrix_free(Vmat); gsl_matrix_free(VmatInv); gsl_matrix_free(ngm); gsl_permutation_free(p); gsl_eigen_nonsymmv_free(w); gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); return r0; }
bool Matrix_Inverse(gsl_matrix_complex *A, gsl_matrix_complex *B){ int s; gsl_matrix_complex *T; gsl_permutation *p; T=gsl_matrix_complex_alloc(B->size1,B->size2); gsl_matrix_complex_memcpy(T, B); p=gsl_permutation_alloc (T->size1); gsl_linalg_complex_LU_decomp (T, p, &s); gsl_linalg_complex_LU_invert (T, p,A); gsl_permutation_free(p); gsl_matrix_complex_free(T); return true; }
static VALUE rb_gsl_linalg_complex_LU_invert(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *m = NULL, *mtmp = NULL, *inverse = NULL; gsl_permutation *p = NULL; int flagm = 0, signum, itmp; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: CHECK_MATRIX_COMPLEX(argv[0]); Data_Get_Struct(argv[0], gsl_matrix_complex, m); if (CLASS_OF(argv[0]) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 1; break; default: Data_Get_Struct(obj, gsl_matrix_complex, m); if (CLASS_OF(obj) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } itmp = 0; } if (flagm == 1) { p = gsl_permutation_alloc(m->size1); gsl_linalg_complex_LU_decomp(mtmp, p, &signum); } else { Data_Get_Struct(argv[itmp], gsl_permutation, p); } inverse = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_linalg_complex_LU_invert(mtmp, p, inverse); if (flagm == 1) { gsl_matrix_complex_free(mtmp); gsl_permutation_free(p); } return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, inverse); }
static VALUE rb_dirac_anticommute(VALUE obj, VALUE mm1, VALUE mm2) { gsl_matrix_complex *m1, *m2; gsl_matrix_complex *mnew1, *mnew2; CHECK_MATRIX_COMPLEX(mm1); CHECK_MATRIX_COMPLEX(mm2); Data_Get_Struct(mm1, gsl_matrix_complex, m1); Data_Get_Struct(mm2, gsl_matrix_complex, m2); mnew1 = gsl_matrix_complex_alloc(m1->size1, m1->size2); mnew2 = gsl_matrix_complex_alloc(m1->size1, m1->size2); gsl_matrix_complex_mul(mnew1, m1, m2); gsl_matrix_complex_mul(mnew2, m2, m1); gsl_matrix_complex_add(mnew1, mnew2); gsl_matrix_complex_free(mnew2); return Data_Wrap_Struct(cgsl_matrix_complex, 0, gsl_matrix_complex_free, mnew1); }
void clearnoise1D(struct data *d) { int dim3; int j; free(d->noise.M); free(d->noise.M2); free(d->noise.Re); free(d->noise.Im); if (d->noise.matrix) { dim3=d->fh.ntraces; for (j=0;j<dim3;j++) gsl_matrix_complex_free(d->noise.mat[j]); free(d->noise.mat); } /* Set noise matrix flag */ d->noise.matrix=FALSE; /* Set data flag */ d->noise.data=FALSE; }
/*! Returns true if the input matrix is Hermitian. */ bool IsHermitian(const gsl_matrix_complex * const M) { gsl_matrix_complex *N; gsl_complex zm, zn; int i, j; bool isHerm = true; N = gsl_matrix_complex_alloc(M->size1, M->size2); for(i = 0; i < M->size1; ++i) { for(j = 0; j < M->size2; ++j) { zm = gsl_matrix_complex_get(M, i, j); GSL_SET_COMPLEX(&zn, GSL_REAL(zm), ((-1)*GSL_IMAG(zm))); gsl_matrix_complex_set(N, i, j, zn); } } gsl_matrix_complex_transpose(N); for(i = 0; i < M->size1; ++i) { for(j = 0; j < M->size2; ++j) { zm = gsl_matrix_complex_get(M, i, j); zn = gsl_matrix_complex_get(N, i, j); if( GSL_REAL(zm) != GSL_REAL(zn) || GSL_IMAG(zm) != GSL_IMAG(zn) ) { isHerm = false; goto _exit; } } } _exit: gsl_matrix_complex_free(N); return isHerm; }
void test_eigen_nonsymm_matrix(const gsl_matrix * m, size_t count, const char * desc, gsl_eigen_nonsymmv_workspace *w) { const size_t N = m->size1; gsl_matrix * A = gsl_matrix_alloc(N, N); gsl_matrix * Z = gsl_matrix_alloc(N, N); gsl_matrix_complex * evec = gsl_matrix_complex_alloc(N, N); gsl_vector_complex * eval = gsl_vector_complex_alloc(N); /* * calculate eigenvalues and eigenvectors - it is sufficient to * test gsl_eigen_nonsymmv() since that function calls * gsl_eigen_nonsymm() for the eigenvalues */ gsl_matrix_memcpy(A, m); gsl_eigen_nonsymmv(A, eval, evec, w); test_eigen_nonsymm_results (m, eval, evec, count, desc, "unsorted"); /* test sort routines */ gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_ASC); test_eigen_nonsymm_results (m, eval, evec, count, desc, "abs/asc"); gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_DESC); test_eigen_nonsymm_results (m, eval, evec, count, desc, "abs/desc"); /* test Schur vectors */ gsl_matrix_memcpy(A, m); gsl_eigen_nonsymmv_Z(A, eval, evec, Z, w); gsl_linalg_hessenberg_set_zero(A); test_eigen_schur(m, A, Z, Z, count, "nonsymm", desc); /* test if Z is an orthogonal matrix */ if (w->nonsymm_workspace_p->do_balance == 0) test_eigen_orthog(Z, count, "nonsymm", desc); gsl_matrix_free(A); gsl_matrix_free(Z); gsl_matrix_complex_free(evec); gsl_vector_complex_free(eval); }
void test_eigen_gen_free(test_eigen_gen_workspace *w) { gsl_matrix_free(w->A); gsl_matrix_free(w->B); gsl_vector_complex_free(w->alpha); gsl_vector_free(w->beta); gsl_vector_complex_free(w->alphav); gsl_vector_free(w->betav); gsl_vector_complex_free(w->eval); gsl_vector_complex_free(w->evalv); gsl_vector_free(w->x); gsl_vector_free(w->y); gsl_matrix_free(w->Q); gsl_matrix_free(w->Z); gsl_matrix_complex_free(w->evec); gsl_eigen_gen_free(w->gen_p); gsl_eigen_genv_free(w->genv_p); free(w); } /* test_eigen_gen_free() */
int main(void) { double data[] = { -1.0, 1.0, -1.0, 1.0, -8.0, 4.0, -2.0, 1.0, 27.0, 9.0, 3.0, 1.0, 64.0, 16.0, 4.0, 1.0 }; gsl_matrix_view m = gsl_matrix_view_array(data, 4, 4); gsl_vector_complex *eval = gsl_vector_complex_alloc(4); gsl_matrix_complex *evec = gsl_matrix_complex_alloc(4, 4); gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(4); gsl_eigen_nonsymmv(&m.matrix, eval, evec, w); gsl_eigen_nonsymmv_free(w); gsl_eigen_nonsymmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC); { int i, j; for (i = 0; i < 4; i++) { gsl_complex eval_i = gsl_vector_complex_get(eval, i); gsl_vector_complex_view evec_i = gsl_matrix_complex_column(evec, i); printf("eigenvalue = %g + %gi\n", GSL_REAL(eval_i), GSL_IMAG(eval_i)); printf("eigenvector = \n"); for (j = 0; j < 4; ++j) { gsl_complex z = gsl_vector_complex_get(&evec_i.vector, j); printf("%g + %gi\n", GSL_REAL(z), GSL_IMAG(z)); } } } gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); return 0; }
void free_gslws(gslws_t *ws) { gsl_vector_complex_free(ws->evals); gsl_matrix_complex_free(ws->evecs); gsl_matrix_complex_free(ws->evecs_inv); gsl_matrix_free(ws->P); gsl_matrix_complex_free(ws->c_P); gsl_matrix_complex_free(ws->temp1); gsl_matrix_complex_free(ws->temp2); gsl_matrix_free(ws->Q_copy); gsl_matrix_complex_free(ws->evecs_copy); gsl_permutation_free(ws->perm); gsl_eigen_nonsymmv_free(ws->eigenws); gsl_vector_free(ws->tempvec1); gsl_vector_free(ws->tempvec2); }
static VALUE rb_gsl_linalg_complex_LU_lndet(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *m = NULL, *mtmp = NULL; gsl_permutation *p = NULL; double lndet; int flagm = 0, signum; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: CHECK_MATRIX_COMPLEX(argv[0]); Data_Get_Struct(argv[0], gsl_matrix_complex, m); if (CLASS_OF(argv[0]) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } break; default: Data_Get_Struct(obj, gsl_matrix_complex, m); if (CLASS_OF(obj) != cgsl_matrix_complex_LU) { mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); flagm = 1; } else { mtmp = m; } } if (flagm == 1) { p = gsl_permutation_alloc(m->size1); gsl_linalg_complex_LU_decomp(mtmp, p, &signum); } lndet = gsl_linalg_complex_LU_lndet(mtmp); if (flagm == 1) { gsl_matrix_complex_free(mtmp); gsl_permutation_free(p); } return rb_float_new(lndet); }
void test_eigen_herm(void) { size_t N_max = 20; size_t n, i; gsl_rng *r = gsl_rng_alloc(gsl_rng_default); for (n = 1; n <= N_max; ++n) { gsl_matrix_complex * A = gsl_matrix_complex_alloc(n, n); for (i = 0; i < 5; ++i) { create_random_herm_matrix(A, r, -10, 10); test_eigen_herm_matrix(A, i, "herm random"); } gsl_matrix_complex_free(A); } gsl_rng_free(r); { double dat1[] = { 0,0, 0,0, -1,0, 0,0, 0,0, 1,0, 0,0, 1,0, -1,0, 0,0, 0,0, 0,0, 0,0, 1,0, 0,0, 0,0 }; double dat2[] = { 1,0, 0,0, 0,0, 0,0, 0,0, 2,0, 0,0, 0,0, 0,0, 0,0, 3,0, 0,0, 0,0, 0,0, 0,0, 4,0 }; gsl_matrix_complex_view m; m = gsl_matrix_complex_view_array (dat1, 4, 4); test_eigen_herm_matrix(&m.matrix, 0, "herm(4)"); m = gsl_matrix_complex_view_array (dat2, 4, 4); test_eigen_herm_matrix(&m.matrix, 1, "herm(4) diag"); } } /* test_eigen_herm() */
double epidemicGrowthRate(const double theta[numParam], const double r0time, double * eigenvec) { gsl_matrix * Fmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_matrix * Vmat = gsl_matrix_calloc(NG*(DS-1)*RG, NG*(DS-1)*RG); createNGM(theta, r0time, Fmat, Vmat); gsl_matrix_sub(Fmat, Vmat); gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(NG*(DS-1)*RG); gsl_vector_complex * eval = gsl_vector_complex_alloc(NG*(DS-1)*RG); gsl_matrix_complex * evec = gsl_matrix_complex_alloc(NG*(DS-1)*RG, NG*(DS-1)*RG); gsl_set_error_handler_off(); gsl_eigen_nonsymmv(Fmat, eval, evec, w); size_t growth_rate_idx = 0; double growth_rate = -INFINITY; for(size_t i = 0; i < NG*(DS-1)*RG; i++){ if(GSL_REAL(gsl_vector_complex_get(eval, i)) > growth_rate){ growth_rate_idx = i; growth_rate = GSL_REAL(gsl_vector_complex_get(eval, i)); } } if(eigenvec != NULL){ for(size_t i = 0; i < NG*(DS-1)*RG; i++) eigenvec[i] = GSL_REAL(gsl_matrix_complex_get(evec, i, growth_rate_idx)); } gsl_matrix_free(Fmat); gsl_matrix_free(Vmat); gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); gsl_eigen_nonsymmv_free(w); return growth_rate; }
void MarkovChain::setupCDFS(const gsl_matrix * Q) { double cdf, norm; gsl_vector_complex *eval; gsl_matrix_complex *evec; gsl_eigen_nonsymmv_workspace * w; gsl_vector_complex_view S; gsl_matrix_memcpy (m_cdfQ, Q); eval = gsl_vector_complex_alloc (Q->size1); evec = gsl_matrix_complex_alloc (Q->size1, Q->size2); w = gsl_eigen_nonsymmv_alloc(Q->size1); gsl_eigen_nonsymmv (m_cdfQ, eval, evec, w); gsl_eigen_nonsymmv_sort (eval, evec, GSL_EIGEN_SORT_ABS_DESC); /*vector of stationary probabilities corresponding to the eigenvalue 1 */ S = gsl_matrix_complex_column(evec, 0); /*sum of vector elements*/ norm = 0.0; for(size_t i = 0; i < Q->size1; ++i) { norm += GSL_REAL(gsl_vector_complex_get(&S.vector, i)); } /*cdfs*/ cdf = 0.0; for(size_t i = 0; i < Q->size1; ++i) { cdf += GSL_REAL(gsl_vector_complex_get(&S.vector, i)) / norm; gsl_vector_set(m_cdfS, i, cdf); } gsl_eigen_nonsymmv_free (w); gsl_vector_complex_free(eval); gsl_matrix_complex_free(evec); }
static VALUE rb_gsl_linalg_cholesky_solve(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *A = NULL, *Atmp = NULL; gsl_vector_complex *b = NULL, *x = NULL; int flagb = 0, flaga = 0; VALUE vA, vb; switch(TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: if (argc != 2) rb_raise(rb_eArgError, "wrong number of argument (%d for 2)", argc); vA = argv[0]; vb = argv[1]; break; default: if (argc != 1) rb_raise(rb_eArgError, "wrong number of argument (%d for 1)", argc); vA = obj; vb = argv[0]; break; } CHECK_MATRIX_COMPLEX(vA); Data_Get_Struct(vA, gsl_matrix_complex, Atmp); CHECK_VECTOR_COMPLEX(vb); Data_Get_Struct(vb, gsl_vector_complex, b); if (CLASS_OF(vA) == cgsl_matrix_complex_C) { A = Atmp; } else { A = make_matrix_complex_clone(Atmp); flaga = 1; gsl_linalg_complex_cholesky_decomp(A); } x = gsl_vector_complex_alloc(b->size); gsl_linalg_complex_cholesky_solve(A, b, x); if (flaga == 1) gsl_matrix_complex_free(A); if (flagb == 1) gsl_vector_complex_free(b); return Data_Wrap_Struct(cgsl_vector_complex_col, 0, gsl_vector_complex_free, x); }
static int _equalo(CMATRIX *a, void *b) { bool result; CCOMPLEX *c; if (!GB.Is(b, CLASS_Complex)) return -1; c = (CCOMPLEX *)b; if (GSL_IMAG(c->number) == 0.0) return _equalf(a, GSL_REAL(c->number)); if (!COMPLEX(a)) return FALSE; gsl_matrix_complex *m = gsl_matrix_complex_alloc(WIDTH(a), HEIGHT(a)); gsl_matrix_complex_set_identity(m); gsl_matrix_complex_scale(m, c->number); result = gsl_matrix_complex_equal(CMAT(a), m); gsl_matrix_complex_free(m); return result; }
double * pert_initial_condition (complex double * mat, size_t NB) { size_t m, n; double * call = double_array_calloc (2*NB*NB+NB); // where we will store C, omega double * eigval = eigenvalues_herm (mat, NB); gsl_matrix_complex * eigvec = eigenvectors_herm (mat, NB); complex double *callc = (complex double*) call; for (m=0;m<NB;m++) { call[2*NB*NB+m]=eigval[m]; } for (m=0;m<NB;m++) { for (n=0;n<NB;n++) { callc[m+n*NB]=_gsl_matrix_complex_get(eigvec, m, n); } } d_free(eigval); gsl_matrix_complex_free(eigvec); return call; }
void MBlockUser::Run() { // // Allocation Matrices // gsl_matrix_uint signature_frequencies=min2.GetDataObj(); gsl_matrix signature_powers=min3.GetDataObj(); // // input bits // gsl_matrix_uint inputbits = min1.GetDataObj(); // // outer loop: the users // for (int u=0;u<M();u++) { gsl_vector_complex_view tmpout = gsl_matrix_complex_column(outmat,u); // // // FETCH K INPUT SYMBOLS // // for (int j=0;j<K();j++) { symbol_id=0; //////// I take Nb bits from input and map it in new_symbol for (int i=0;i<Nb();i++) { symbol_id = (symbol_id << 1); // symbol_id += in1.GetDataObj(); symbol_id += gsl_matrix_uint_get(&inputbits,u,j*Nb()+i); } new_symbol = gsl_complex_polar(1.0, symbol_arg * double(gsl_vector_uint_get(gray_encoding, symbol_id))); gsl_vector_complex_set(tmp,j,new_symbol); } // // // SELECTION MATRIX UPDATE and POWER // // // gsl_matrix_complex_set_identity(selection_mat); gsl_matrix_complex_set_zero(selection_mat); for (int i=0;i<J(); i++) { unsigned int carrier=gsl_matrix_uint_get(&signature_frequencies,u,i); double power=gsl_matrix_get(&signature_powers,u,i); gsl_complex one=gsl_complex_polar(power,0.0); gsl_matrix_complex_set(selection_mat,carrier,i,one); } // // // PRECODING MATRIX UPDATE // // #ifdef GIANNAKIS_PRECODING double roarg=2.0*double(M_PI/N()); for (int i=0;i<J(); i++) { unsigned int carrier=gsl_matrix_uint_get(&signature_frequencies,u,i); for (int j=0; j<K(); j++) { gsl_complex ro=gsl_complex_polar(sqrt(1.0/double(J())),-j*carrier*roarg); gsl_matrix_complex_set(coding_mat,i,j,ro); } } #else double roarg=2.0*double(M_PI/J()); for (int i=0;i<J(); i++) { for (int j=0; j<K(); j++) { gsl_complex ro=gsl_complex_polar(sqrt(1.0/double(J())),-j*i*roarg); gsl_matrix_complex_set(coding_mat,i,j,ro); } } #endif #ifdef SHOW_MATRIX cout << endl << BlockName << " user: "******"coding matrix (theta) = " << endl; gsl_matrix_complex_show(coding_mat); cout << "T^h*T matrix = " << endl; gsl_matrix_complex_show(THT); cout << "T^h*T trace = " << GSL_REAL(trace) << ", " << GSL_IMAG(trace) << endl; gsl_matrix_complex_free(THT); #endif // // // PRECODING // // gsl_blas_zgemv(CblasNoTrans, gsl_complex_rect(1.0,0), coding_mat, tmp, gsl_complex_rect(0,0), tmp1); // // // CARRIER SELECTION // // gsl_blas_zgemv(CblasNoTrans, gsl_complex_rect(1.0,0), selection_mat, tmp1, gsl_complex_rect(0,0), tmp2); // // // IFFT TRANSFORM // // gsl_blas_zgemv(CblasNoTrans, gsl_complex_rect(1.0,0), transform_mat, tmp2, gsl_complex_rect(0,0), &tmpout.vector); // cout << "\n\n symbols (user " << u << ") = " << endl; // gsl_vector_complex_fprintf(stdout,tmp,"%f"); #ifdef SHOW_MATRIX cout << "\n\n symbols (user " << u << ") = " << endl; gsl_vector_complex_fprintf(stdout,tmp,"%f"); cout << "\n\n precoded = " << endl; gsl_vector_complex_fprintf(stdout,tmp1,"%f"); cout << "\n\n precoded selected = " << endl; gsl_vector_complex_fprintf(stdout,tmp2,"%f"); cout << "\n\n precoded selected transformed = " << endl; gsl_vector_complex_fprintf(stdout,&tmpout.vector,"%f"); #endif } // close user loop mout1.DeliverDataObj(*outmat); }
void test_eigen_nonsymm_results (const gsl_matrix * m, const gsl_vector_complex * eval, const gsl_matrix_complex * evec, size_t count, const char * desc, const char * desc2) { size_t i,j; size_t N = m->size1; gsl_vector_complex * x = gsl_vector_complex_alloc(N); gsl_vector_complex * y = gsl_vector_complex_alloc(N); gsl_matrix_complex * A = gsl_matrix_complex_alloc(N, N); /* we need a complex matrix for the blas routines, so copy m into A */ for (i = 0; i < N; ++i) { for (j = 0; j < N; ++j) { gsl_complex z; GSL_SET_COMPLEX(&z, gsl_matrix_get(m, i, j), 0.0); gsl_matrix_complex_set(A, i, j, z); } } for (i = 0; i < N; i++) { gsl_complex ei = gsl_vector_complex_get (eval, i); gsl_vector_complex_const_view vi = gsl_matrix_complex_const_column(evec, i); double norm = gsl_blas_dznrm2(&vi.vector); /* check that eigenvector is normalized */ gsl_test_rel(norm, 1.0, N * GSL_DBL_EPSILON, "nonsymm(N=%u,cnt=%u), %s, normalized(%d), %s", N, count, desc, i, desc2); gsl_vector_complex_memcpy(x, &vi.vector); /* compute y = m x (should = lambda v) */ gsl_blas_zgemv (CblasNoTrans, GSL_COMPLEX_ONE, A, x, GSL_COMPLEX_ZERO, y); /* compute x = lambda v */ gsl_blas_zscal(ei, x); /* now test if y = x */ for (j = 0; j < N; j++) { gsl_complex xj = gsl_vector_complex_get (x, j); gsl_complex yj = gsl_vector_complex_get (y, j); /* use abs here in case the values are close to 0 */ gsl_test_abs(GSL_REAL(yj), GSL_REAL(xj), 1e8*GSL_DBL_EPSILON, "nonsymm(N=%u,cnt=%u), %s, eigenvalue(%d,%d), real, %s", N, count, desc, i, j, desc2); gsl_test_abs(GSL_IMAG(yj), GSL_IMAG(xj), 1e8*GSL_DBL_EPSILON, "nonsymm(N=%u,cnt=%u), %s, eigenvalue(%d,%d), imag, %s", N, count, desc, i, j, desc2); } } gsl_matrix_complex_free(A); gsl_vector_complex_free(x); gsl_vector_complex_free(y); }
static VALUE rb_gsl_linalg_complex_LU_solve(int argc, VALUE *argv, VALUE obj) { gsl_matrix_complex *m = NULL, *mtmp = NULL; gsl_permutation *p = NULL; gsl_vector_complex *b = NULL, *x = NULL; int flagm = 0, flagx = 0, itmp, signum; switch (TYPE(obj)) { case T_MODULE: case T_CLASS: case T_OBJECT: if (argc < 2 || argc > 4) rb_raise(rb_eArgError, "Usage: solve(m, b), solve(m, b, x), solve(lu, p, b), solve(lu, p, b, x)"); CHECK_MATRIX(argv[0]); Data_Get_Struct(argv[0], gsl_matrix_complex, m); if (CLASS_OF(argv[0]) != cgsl_matrix_complex_LU) { flagm = 1; mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); } else { mtmp = m; } itmp = 1; break; default: if (argc < 1 || argc > 3) rb_raise(rb_eArgError, "Usage: LU_solve(b), LU_solve(p, b), LU_solve(b, x), solve(p, b, x)"); Data_Get_Struct(obj, gsl_matrix_complex, m); if (CLASS_OF(obj) != cgsl_matrix_complex_LU) { flagm = 1; mtmp = gsl_matrix_complex_alloc(m->size1, m->size2); gsl_matrix_complex_memcpy(mtmp, m); } else { mtmp = m; } itmp = 0; } if (flagm == 1) { if (itmp != argc-1) rb_raise(rb_eArgError, "Usage: m.LU_solve(b)"); Data_Get_Struct(argv[itmp], gsl_vector_complex, b); x = gsl_vector_complex_alloc(b->size); p = gsl_permutation_alloc(b->size); gsl_linalg_complex_LU_decomp(mtmp, p, &signum); } else { Data_Get_Struct(argv[itmp], gsl_permutation, p); itmp++; Data_Get_Struct(argv[itmp], gsl_vector_complex, b); itmp++; if (itmp == argc-1) { Data_Get_Struct(argv[itmp], gsl_vector_complex, x); flagx = 1; } else { x = gsl_vector_complex_alloc(m->size1); } } gsl_linalg_complex_LU_solve(mtmp, p, b, x); if (flagm == 1) { gsl_matrix_complex_free(mtmp); gsl_permutation_free(p); } if (flagx == 0) return Data_Wrap_Struct(cgsl_vector_complex, 0, gsl_vector_complex_free, x); else return argv[argc-1]; }
int Umeyama(const gsl_matrix * const A, const gsl_matrix * const B, gsl_matrix * const Assignment, float *score) { int res = 0; gsl_matrix_complex *AE = NULL, *BE = NULL; gsl_matrix *P = NULL, *AS = NULL, *BS = NULL, *AN = NULL, *BN = NULL, *AssignmentT = NULL; #ifdef USE_ASP long *col_mate; long *row_mate; cost **pTempCost; int i, j; #endif struct timeval start, end; long mtime, seconds, useconds; #ifdef VERBOSE PrintGSLMatrix(A, "Umeyama A"); PrintGSLMatrix(B, "Umeyama B"); #endif AE = gsl_matrix_complex_alloc(A->size1, A->size2); if(AE == NULL) goto _exit; AS = gsl_matrix_alloc(A->size1, A->size2); if(AS == NULL) goto _exit; AN = gsl_matrix_alloc(A->size1, A->size2); if(AN == NULL) goto _exit; ComputeHermitianMatrix(A, AE, AS, AN); #ifdef VERBOSE PrintGSLMatrix(AS, "AS"); PrintGSLMatrix(AN, "AN"); #endif if( IsHermitian(AE) == false ) { fprintf(stderr, "FATAL: AE is not Hermitian!\n"); exit(0); } #ifdef VERBOSE fprintf(stderr, "Verified AE is Hermitian.\n"); #endif BE = gsl_matrix_complex_alloc(B->size1, B->size2); if(BE == NULL) goto _exit; BS = gsl_matrix_alloc(B->size1, B->size2); if(BS == NULL) goto _exit; BN = gsl_matrix_alloc(B->size1, B->size2); if(BN == NULL) goto _exit; ComputeHermitianMatrix(B, BE, BS, BN); #ifdef VERBOSE PrintGSLMatrix(BS, "BS"); PrintGSLMatrix(BN, "BN"); #endif if( IsHermitian(BE) == false ) { fprintf(stderr, "FATAL: BE is not Hermitian!\n"); exit(0); } #ifdef VERBOSE fprintf(stderr, "Verified BE is Hermitian.\n"); WriteComplexMatrixToFile(BE); PrintGSLMatrixComplex(AE, "AE"); PrintGSLMatrixComplex(BE, "BE"); #endif P = gsl_matrix_alloc(A->size1, A->size2); if(P == NULL) goto _exit; res = EigenDecomp(AE, BE, P); if( res == -1 ) goto _exit; #ifdef VERBOSE PrintGSLMatrix(P, "P"); PrintGSLMatrix(P, "Computing Hungarian"); #endif // Begin timing Hungarian gettimeofday(&start, NULL); #ifdef USE_ASP col_mate = new long[P->size1]; row_mate = new long[P->size1]; pTempCost = new cost*[P->size1]; for(i = 0; i < P->size1; ++i) pTempCost[i] = new cost[P->size2]; for(i = 0; i < P->size1; ++i) for(j = 0; j < P->size2; ++j) pTempCost[i][j] = gsl_matrix_get(P, i, j); asp(P->size1, pTempCost, col_mate, row_mate); for(i = 0; i < P->size1; ++i) delete[] pTempCost[i]; delete[] pTempCost; // Update assignment matrix for(i = 0; i < P->size1; ++i) gsl_matrix_set(Assignment, i, col_mate[i], 1); delete[] col_mate; delete[] row_mate; #else Hungarian(P, true, Assignment); #endif // End timing Hungarian gettimeofday(&end, NULL); seconds = end.tv_sec - start.tv_sec; useconds = end.tv_usec - start.tv_usec; mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5; fprintf(stderr, "%ld ms\t", mtime); // PrintGSLMatrixUint(Assignment, "Assignment"); // Allocate and initialize AssgtT AssignmentT = gsl_matrix_alloc(Assignment->size1, Assignment->size2); if(AssignmentT == NULL) goto _exit; gsl_matrix_memcpy(AssignmentT, Assignment); gsl_matrix_transpose(AssignmentT); /* PrintGSLMatrix(AS, "AS"); PrintGSLMatrix(AN, "AN"); PrintGSLMatrix(BS, "BS"); PrintGSLMatrix(BN, "BN"); */ *score = CalcScore(AS, AN, BS, BN, Assignment, AssignmentT); _exit: if(P != NULL) gsl_matrix_free(P); if(AS != NULL) gsl_matrix_free(AS); if(AN != NULL) gsl_matrix_free(AN); if(BS != NULL) gsl_matrix_free(BS); if(BN != NULL) gsl_matrix_free(BN); if(AE != NULL) gsl_matrix_complex_free(AE); if(BE != NULL) gsl_matrix_complex_free(BE); if(AssignmentT != NULL) gsl_matrix_free(AssignmentT); return res; }
void test_eigen_genherm(void) { size_t N_max = 20; size_t n, i; gsl_rng *r = gsl_rng_alloc(gsl_rng_default); for (n = 1; n <= N_max; ++n) { gsl_matrix_complex * A = gsl_matrix_complex_alloc(n, n); gsl_matrix_complex * B = gsl_matrix_complex_alloc(n, n); gsl_matrix_complex * ma = gsl_matrix_complex_alloc(n, n); gsl_matrix_complex * mb = gsl_matrix_complex_alloc(n, n); gsl_vector * eval = gsl_vector_alloc(n); gsl_vector * evalv = gsl_vector_alloc(n); gsl_vector * x = gsl_vector_alloc(n); gsl_vector * y = gsl_vector_alloc(n); gsl_vector_complex * work = gsl_vector_complex_alloc(n); gsl_matrix_complex * evec = gsl_matrix_complex_alloc(n, n); gsl_eigen_genherm_workspace * w = gsl_eigen_genherm_alloc(n); gsl_eigen_genhermv_workspace * wv = gsl_eigen_genhermv_alloc(n); for (i = 0; i < 5; ++i) { create_random_herm_matrix(A, r, -10, 10); create_random_complex_posdef_matrix(B, r, work); gsl_matrix_complex_memcpy(ma, A); gsl_matrix_complex_memcpy(mb, B); gsl_eigen_genhermv(ma, mb, evalv, evec, wv); test_eigen_genherm_results(A, B, evalv, evec, i, "random", "unsorted"); gsl_matrix_complex_memcpy(ma, A); gsl_matrix_complex_memcpy(mb, B); gsl_eigen_genherm(ma, mb, eval, w); /* eval and evalv have to be sorted? not sure why */ gsl_vector_memcpy(x, eval); gsl_vector_memcpy(y, evalv); gsl_sort_vector(x); gsl_sort_vector(y); test_eigenvalues_real(y, x, "genherm, random", "unsorted"); gsl_eigen_genhermv_sort(evalv, evec, GSL_EIGEN_SORT_VAL_ASC); test_eigen_genherm_results(A, B, evalv, evec, i, "random", "val/asc"); gsl_eigen_genhermv_sort(evalv, evec, GSL_EIGEN_SORT_VAL_DESC); test_eigen_genherm_results(A, B, evalv, evec, i, "random", "val/desc"); gsl_eigen_genhermv_sort(evalv, evec, GSL_EIGEN_SORT_ABS_ASC); test_eigen_genherm_results(A, B, evalv, evec, i, "random", "abs/asc"); gsl_eigen_genhermv_sort(evalv, evec, GSL_EIGEN_SORT_ABS_DESC); test_eigen_genherm_results(A, B, evalv, evec, i, "random", "abs/desc"); } gsl_matrix_complex_free(A); gsl_matrix_complex_free(B); gsl_matrix_complex_free(ma); gsl_matrix_complex_free(mb); gsl_vector_free(eval); gsl_vector_free(evalv); gsl_vector_free(x); gsl_vector_free(y); gsl_vector_complex_free(work); gsl_matrix_complex_free(evec); gsl_eigen_genherm_free(w); gsl_eigen_genhermv_free(wv); } gsl_rng_free(r); } /* test_eigen_genherm() */
int ComputeUnitaryMatrix_GSL(gsl_matrix_complex * const M, gsl_matrix * const U) { int result; gsl_vector *eval, *order; gsl_matrix_complex *evec; gsl_eigen_hermv_workspace *w; unsigned int i, j; gsl_complex z; float real, imag; // Allocations eval = gsl_vector_alloc(M->size1); order = gsl_vector_alloc(M->size1); evec = gsl_matrix_complex_alloc(M->size1, M->size1); w = gsl_eigen_hermv_alloc(M->size1); #ifdef VERBOSE PrintGSLMatrixComplex(M, "Computing Unitary Matrix for M"); #endif result = gsl_eigen_hermv(M, eval, evec, w); #ifdef VERBOSE fprintf(stderr, "Result from gsl_eigen_hermv = %d\n", result); #endif if(result != GSL_SUCCESS) { fprintf(stderr, "ERROR %d when calling gsl_eigen_hermv()!\n", result); return -1; } if(HasNaN(evec) == true) return -1; // Get ascending order of values in eval // PrintGSLVector(eval, "eval (BEFORE)"); for(i = 0; i < order->size; i++) gsl_vector_set(order, i, i); QuickSort(eval, order, 0, order->size-1); #ifdef VERBOSE PrintGSLVector(eval, "eval (AFTER)"); PrintGSLVector(order, "order (AFTER)"); PrintGSLMatrixComplex(evec, "evec"); #endif for(i = 0; i < U->size1; i++) { for(j = 0; j < U->size2; j++) { z = gsl_matrix_complex_get(evec, i, gsl_vector_get(order,j)); real = GSL_REAL(z); imag = GSL_IMAG(z); gsl_matrix_set(U, i, j, sqrt(real*real + imag*imag)); } } // Free gsl_eigen_hermv_free(w); gsl_matrix_complex_free(evec); gsl_vector_free(order); gsl_vector_free(eval); return GSL_SUCCESS; }
/** * Returns the symmetry axis for the given matrix * * According to ITA, 11.2 the axis of a symmetry operation can be determined by * solving the Eigenvalue problem \f$Wu = u\f$ for rotations or \f$Wu = -u\f$ * for rotoinversions. This is implemented using the general real non-symmetric * eigen-problem solver provided by the GSL. * * @param matrix :: Matrix of a SymmetryOperation * @return Axis of symmetry element. */ V3R SymmetryElementWithAxisGenerator::determineAxis( const Kernel::IntMatrix &matrix) const { gsl_matrix *eigenMatrix = getGSLMatrix(matrix); gsl_matrix *identityMatrix = getGSLIdentityMatrix(matrix.numRows(), matrix.numCols()); gsl_eigen_genv_workspace *eigenWs = gsl_eigen_genv_alloc(matrix.numRows()); gsl_vector_complex *alpha = gsl_vector_complex_alloc(3); gsl_vector *beta = gsl_vector_alloc(3); gsl_matrix_complex *eigenVectors = gsl_matrix_complex_alloc(3, 3); gsl_eigen_genv(eigenMatrix, identityMatrix, alpha, beta, eigenVectors, eigenWs); gsl_eigen_genv_sort(alpha, beta, eigenVectors, GSL_EIGEN_SORT_ABS_DESC); double determinant = matrix.determinant(); Kernel::V3D eigenVector; for (size_t i = 0; i < matrix.numCols(); ++i) { double eigenValue = GSL_REAL(gsl_complex_div_real( gsl_vector_complex_get(alpha, i), gsl_vector_get(beta, i))); if (fabs(eigenValue - determinant) < 1e-9) { for (size_t j = 0; j < matrix.numRows(); ++j) { double element = GSL_REAL(gsl_matrix_complex_get(eigenVectors, j, i)); eigenVector[j] = element; } } } eigenVector *= determinant; double sumOfElements = eigenVector.X() + eigenVector.Y() + eigenVector.Z(); if (sumOfElements < 0) { eigenVector *= -1.0; } gsl_matrix_free(eigenMatrix); gsl_matrix_free(identityMatrix); gsl_eigen_genv_free(eigenWs); gsl_vector_complex_free(alpha); gsl_vector_free(beta); gsl_matrix_complex_free(eigenVectors); double min = 1.0; for (size_t i = 0; i < 3; ++i) { double absoluteValue = fabs(eigenVector[i]); if (absoluteValue != 0.0 && (eigenVector[i] < min && (absoluteValue - fabs(min)) < 1e-9)) { min = eigenVector[i]; } } V3R axis; for (size_t i = 0; i < 3; ++i) { axis[i] = static_cast<int>(boost::math::round(eigenVector[i] / min)); } return axis; }
int main (int argc, char **argv) { if (argc != 7 && argc != 8) { cout << "Usage: " << argv[0] << " <modelfile> <a> <b> <i> <j> <time> [<lambda>]\n"; exit (EXIT_FAILURE); } RateModel rates; ifstream in (argv[1]); ParsedJson pj (in); rates.read (pj.value); const AlphTok a = rates.tokenize (argv[2][0]); const AlphTok b = rates.tokenize (argv[3][0]); const AlphTok i = rates.tokenize (argv[4][0]); const AlphTok j = rates.tokenize (argv[5][0]); const double T = atof (argv[6]); // logger.setVerbose (8); EigenModel eigen (rates); vguard<gsl_matrix*> sub = eigen.getSubProbMatrix(T); vguard<gsl_matrix_complex*> esub = eigen.eigenSubCount(T); const double count = eigen.getSubCount (0, a, b, i, j, sub[0], esub[0]); const double nSteps = 1e5; const double tStep = T / nSteps; double numCount = 0; for (double t = 0; t < T; t += tStep) numCount += eigen.getSubProb(0,t,a,i) * eigen.getSubProb(0,T-t-tStep,j,b); numCount *= gsl_matrix_get (rates.subRate[0], i, j) * tStep / eigen.getSubProb(0,T,a,b); cout << "Eigenvector method: " << count << endl; cout << "Numerical integration: " << numCount << endl; if (argc == 8) { Assert (i != j, "Need i!=j for exact Jukes-Cantor test"); const double lambda = atof (argv[7]); if (a != i && j != b && a != b) { const double jcCount = (lambda/16) * (T + (2/lambda)*(exp(-lambda*T)-1) + T*exp(-lambda*T)) / (1 - exp(-lambda*T)); cout << "Jukes-Cantor (lambda=" << lambda << "): " << jcCount << endl; } double jcNumCount = 0; for (double t = 0; t < T; t += tStep) jcNumCount += jcProb(lambda,t,a,i) * (lambda/4) * tStep * jcProb(lambda,T-t,j,b); jcNumCount /= jcProb(lambda,T,a,b); cout << "Jukes-Cantor numerical (lambda=" << lambda << "): " << jcNumCount << endl; cout << "Rate(i->j): " << gsl_matrix_get (rates.subRate[0], i, j) << endl; cout << "Eigen: P(a->i|T/3): " << eigen.getSubProb(0,T/3,a,i) << endl; cout << "Eigen: P(j->b|2T/3): " << eigen.getSubProb(0,2*T/3,j,b) << endl; cout << "Eigen: P(a->b|T): " << eigen.getSubProb(0,T,a,b) << endl; cout << "JC exact: P(a->i|T/3): " << jcProb(lambda,T/3,a,i) << endl; cout << "JC exact: P(j->b|2T/3): " << jcProb(lambda,2*T/3,j,b) << endl; cout << "JC exact: P(a->b|T): " << jcProb(lambda,T,a,b) << endl; } for (auto& s: sub) gsl_matrix_free (s); for (auto& e: esub) gsl_matrix_complex_free (e); exit (EXIT_SUCCESS); }
void MCPMPChan::Finish() { gsl_rng_free( ran ); gsl_matrix_complex_free(outmat); gsl_matrix_complex_free(user_chan); }
GList * calc_w_phi_coeffs (const ThreeVector * kperp, const ThreeVector * direction) { if(!cptimer) { /* optimization timer */ cptimer = g_timer_new(); g_timer_start(cptimer); } else { g_timer_continue(cptimer); } cp_init(); g_message ("starting calc_w_phi: {%1.2e, %1.2e}", kperp->x[0], kperp->x[1]); if (model->total_bands == 2) /* for checking the two-band PBA model */ return calc_w_phi_constP (kperp); double te, he, kc; size_t qq; gboolean failed = FALSE; size_t NB = model->total_bands; size_t N2 = (model->total_bands)*(model->total_bands); pert_ode * po = pert_ode_init (NB); // generate basis vectors e, f, g const ThreeVector *g = direction; const ThreeVector *e = three_vector_e (g); const ThreeVector *f = three_vector_f (g, e); g_message ("e is %1.2e, %1.2e, %1.2e", e->x[0], e->x[1], e->x[2]); g_message ("f is %1.2e, %1.2e, %1.2e", f->x[0], f->x[1], f->x[2]); g_message ("g is %1.2e, %1.2e, %1.2e", g->x[0], g->x[1], g->x[2]); // get kperp in basis x, y, z ThreeVector *kperp2 = three_vector_new (0,0,0); kperp2->x[0] = kperp->x[0]*e->x[0] + kperp->x[1]*f->x[0] + kperp->x[2]*g->x[0]; kperp2->x[1] = kperp->x[0]*e->x[1] + kperp->x[1]*f->x[1] + kperp->x[2]*g->x[1]; kperp2->x[2] = kperp->x[0]*e->x[2] + kperp->x[1]*f->x[2] + kperp->x[2]*g->x[2]; if (trajectory_intersects_bad (kperp2, direction)) { g_timer_stop(cptimer); return NULL; } complex double * H; // set up ODE solver po->syse.function = cfuncall; odeparams params; po->syse.params = (void *) ¶ms; te = 0.0;//, kc1 = KCMAX/5; he = 5e-7; double * call; complex double *callc; H = model->H(kperp2); double * eigval = eigenvalues_herm (H, model->total_bands); gsl_matrix_complex * eigvec = eigenvectors_herm (H, model->total_bands); m_free(H); size_t m, n; call = double_array_calloc (2*NB*NB+NB); callc = (complex double*) call; for (m=0;m<NB;m++) { for (n=0;n<NB;n++) { callc[m*NB+n]=*(complex double *)(eigvec->data + 2*(m * eigvec->tda + n)); } call[2*NB*NB+m]=eigval[m]; } d_free(eigval); gsl_matrix_complex_free(eigvec); params.kperp = *three_vector_copy (kperp2); po->syse.params = (void *) ¶ms; params.pos = TRUE; params.dir = *g; /* make a copy of the initial condition because we will need it to go negative */ double * cinit = double_array_clone(call,2*N2+NB); // make list of points, add first point GList * pointlist = g_list_append (NULL, matrix_element_create_from_coeffs(kperp, callc, 0.0)); // evolve in positive g direction g_message("going in g direction"); te = 0.0;//, kc1 = KCMAX/5; he = 5e-7; qq=1; double dkc=5e-5; for (kc=dkc*COARSENESS;kc<=KCMAX;kc=kc+dkc*COARSENESS) { // coarse loop for ODE solver for W - positive kc while (te < kc && !failed) { pert_ode_evolve (po, &te, kc, &he, call); ThreeVector *kpar = three_vector_copy(direction); three_vector_scale(kpar,te); ThreeVector *kval = three_vector_add(kperp2,kpar); pointlist = g_list_prepend (pointlist, matrix_element_create_from_coeffs(kval, callc, te)); if (qq>100000) { /* danger with adaptive solver is it might get stuck */ failed=TRUE; /* this detects whether the number of points is getting too high */ g_warning("calc_w_phi_coeffs: ode solver ran away, positive direction"); } qq++; } } pointlist = g_list_reverse (pointlist); // reverse list pert_ode_reset(po); // reset the ode solver, initial condition d_free(call); call = cinit; callc = (complex double*) call; // evolve in negative g direction params.pos = FALSE; qq=0; te=0; he=5e-7; for (kc=dkc*COARSENESS;kc<=KCMAX;kc=kc+dkc*COARSENESS) { while (te < kc && !failed) { pert_ode_evolve (po, &te, kc, &he, call); ThreeVector *kpar = three_vector_copy(direction); three_vector_scale(kpar,-te); ThreeVector *kval = three_vector_add(kperp2,kpar); pointlist = g_list_prepend (pointlist, matrix_element_create_from_coeffs(kval, callc, -te)); if (qq>100000) { failed=TRUE; g_warning("calc_w_phi_coeffs: ode solver ran away, negative direction"); } qq++; } } pert_ode_free(po); d_free(call); g_timer_stop(cptimer); /* optimization timer */ m_free(wc); if(dHx) m_free(dHx); if(dHy) m_free(dHy); if(dHz) m_free(dHz); return pointlist; }
/** ---------------------------------------------------------------------------- * Sort eigenvectors */ int gsl_ext_eigen_sort(gsl_matrix_complex *evec, gsl_vector_complex *eval, int sort_order) { gsl_complex z; gsl_matrix_complex *evec_copy; gsl_vector_complex *eval_copy; int *idx_map, i, j, idx_temp; double p1, p2; if ((evec->size1 != evec->size2) || (evec->size1 != eval->size)) { return -1; } evec_copy = gsl_matrix_complex_alloc(evec->size1, evec->size2); eval_copy = gsl_vector_complex_alloc(eval->size); idx_map = (int *)malloc(sizeof(int) * eval->size); gsl_matrix_complex_memcpy(evec_copy, evec); gsl_vector_complex_memcpy(eval_copy, eval); // calculate new eigenvalue order for (i = 0; i < eval->size; i++) { idx_map[i] = i; } for (i = 0; i < eval->size - 1; i++) { for (j = i+1; j < eval->size; j++) { idx_temp = -1; if (sort_order == GSL_EXT_EIGEN_SORT_ABS) { p1 = gsl_complex_abs(gsl_vector_complex_get(eval, idx_map[i])); p2 = gsl_complex_abs(gsl_vector_complex_get(eval, idx_map[j])); if (p1 > p2) { idx_temp = idx_map[i]; } } if (sort_order == GSL_EXT_EIGEN_SORT_PHASE) { p1 = gsl_complex_arg(gsl_vector_complex_get(eval, idx_map[i])); p2 = gsl_complex_arg(gsl_vector_complex_get(eval, idx_map[j])); if (p1 > M_PI) p1 -= 2*M_PI; if (p1 <= -M_PI) p1 += 2*M_PI; if (p2 > M_PI) p2 -= 2*M_PI; if (p2 <= -M_PI) p2 += 2*M_PI; //if (((p2 < p1) && (p1 - p2 < M_PI)) || ) if (p2 < p1) { idx_temp = idx_map[i]; } } if (idx_temp != -1) { // swap //idx_temp = idx_map[i]; idx_map[i] = idx_map[j]; idx_map[j] = idx_temp; } } } // reshuffle the eigenvectors and eigenvalues for (i = 0; i < eval->size; i++) { for (j = 0; j < eval->size; j++) { z = gsl_matrix_complex_get(evec_copy, idx_map[i], j); gsl_matrix_complex_set(evec, i, j, z); //z = gsl_matrix_complex_get(evec_copy, i, idx_map[j]); //gsl_matrix_complex_set(evec, i, j, z); } z = gsl_vector_complex_get(eval_copy, idx_map[i]); gsl_vector_complex_set(eval, i, z); } gsl_matrix_complex_free(evec_copy); gsl_vector_complex_free(eval_copy); free(idx_map); return 0; }