/* ----------------------------------------------------------------------------------- * Convert a sparse matrix to dense format (standard GSL matrix). * */ gsl_matrix_complex * gsl_sparse_matrix_complex_convert_to_dense(gsl_sparse_matrix_complex *spmat) { int i; if (spmat == NULL) { return NULL; } spmat->M_dense = gsl_matrix_complex_alloc(spmat->n, spmat->m); gsl_matrix_complex_set_zero(spmat->M_dense); // // triplet form to dense form: // for (i = 0; i < spmat->nz; i++) { gsl_complex z; GSL_SET_COMPLEX(&z, spmat->val_real[i], spmat->val_imag[i]); gsl_matrix_complex_set(spmat->M_dense, spmat->row[i], spmat->col[i], z); } return spmat->M_dense; }
int incmat(gsl_matrix_complex * ILa, double na, double cphia) { gsl_complex zb[8]; zb[0] = gsl_complex_rect(0.5,0); zb[1] = gsl_complex_rect(1/(2*na*cphia),0); zb[2] = gsl_complex_rect(0.5,0); zb[3] = gsl_complex_rect(-1/(2*na*cphia),0); zb[4] = gsl_complex_rect(1/(2*cphia),0); zb[5] = gsl_complex_rect(1/(2*na),0); zb[6] = gsl_complex_rect(-1/(2*cphia),0); zb[7] = gsl_complex_rect(1/(2*na),0); gsl_matrix_complex_set_zero(ILa); gsl_matrix_complex_set(ILa,0,2,zb[0]); gsl_matrix_complex_set(ILa,0,3,zb[1]); gsl_matrix_complex_set(ILa,1,2,zb[2]); gsl_matrix_complex_set(ILa,1,3,zb[3]); gsl_matrix_complex_set(ILa,2,0,zb[4]); gsl_matrix_complex_set(ILa,2,1,zb[5]); gsl_matrix_complex_set(ILa,3,0,zb[6]); gsl_matrix_complex_set(ILa,3,1,zb[7]); return 1; }
void qdpack_matrix_set_zero(qdpack_matrix_t *mat) { if (mat == NULL) return; return gsl_matrix_complex_set_zero(mat->data); }
int lls_complex_reset(lls_complex_workspace *w) { gsl_matrix_complex_set_zero(w->AHA); gsl_vector_complex_set_zero(w->AHb); w->bHb = 0.0; return 0; }
void gtmiso(gsl_matrix_complex * Tiso, gsl_complex eiso, double k0, double eta, double diso) { double eta2 = pow(eta,2); gsl_complex rb = gsl_complex_rect(eta2,0); gsl_complex za = gsl_complex_sub_real(eiso,eta2); gsl_complex qiso = gsl_complex_sqrt(za); gsl_complex zb = gsl_complex_mul_real(qiso,k0); gsl_complex zc = gsl_complex_mul_real(zb,diso); gsl_complex zd = gsl_complex_div(rb,eiso); gsl_complex carg = gsl_complex_cos(zc); gsl_complex sarg = gsl_complex_sin(zc); gsl_complex i = gsl_complex_rect(0,1); gsl_complex one = gsl_complex_rect(1,0); gsl_matrix_complex_set_zero(Tiso); gsl_matrix_complex_set(Tiso,0,0,carg); gsl_matrix_complex_set(Tiso,1,1,carg); gsl_matrix_complex_set(Tiso,2,2,carg); gsl_matrix_complex_set(Tiso,3,3,carg); gsl_complex zd1 = gsl_complex_sub(one,zd); gsl_complex zd2 = eiso; gsl_complex zd3 = one; gsl_complex zd4 = gsl_complex_sub_real(eiso,eta2); gsl_complex zmult1 = gsl_complex_mul(zd1,i); gsl_complex zmult2 = gsl_complex_mul(zd2,i); gsl_complex zmult3 = gsl_complex_mul(zd3,i); gsl_complex zmult4 = gsl_complex_mul(zd4,i); gsl_complex zdiv1 = gsl_complex_div(zmult1,qiso); gsl_complex zdiv2 = gsl_complex_div(zmult2,qiso); gsl_complex zdiv3 = gsl_complex_div(zmult3,qiso); gsl_complex zdiv4 = gsl_complex_div(zmult4,qiso); gsl_complex zmult5 = gsl_complex_mul(zdiv1,sarg); gsl_complex zmult6 = gsl_complex_mul(zdiv2,sarg); gsl_complex zmult7 = gsl_complex_mul(zdiv3,sarg); gsl_complex zmult8 = gsl_complex_mul(zdiv4,sarg); gsl_matrix_complex_set(Tiso,0,1,zmult5); gsl_matrix_complex_set(Tiso,1,0,zmult6); gsl_matrix_complex_set(Tiso,2,3,zmult7); gsl_matrix_complex_set(Tiso,3,2,zmult8); }
std::unique_ptr<gsl_matrix_complex,void (*)(gsl_matrix_complex*)> Const::GetTransformationMatrix(size_t dim) const{ if(dim>SQUIDS_MAX_HILBERT_DIM) throw std::runtime_error("Const::GetTransformationMatrix: dimension must be less than " SQUIDS_MAX_HILBERT_DIM_STR); gsl_matrix_complex* U = gsl_matrix_complex_alloc(dim,dim); gsl_matrix_complex* R = gsl_matrix_complex_alloc(dim,dim); gsl_matrix_complex* dummy = gsl_matrix_complex_alloc(dim,dim); gsl_matrix_complex_set_identity(U); gsl_matrix_complex_set_identity(R); gsl_matrix_complex_set_zero(dummy); const auto unit=gsl_complex_rect(1,0); const auto zero=gsl_complex_rect(0,0); auto to_gsl=[](const std::complex<double>& c)->gsl_complex{ return(gsl_complex_rect(c.real(),c.imag())); }; //construct each subspace rotation and accumulate the product for(size_t j=1; j<dim; j++){ for(size_t i=0; i<j; i++){ //set up the subspace rotation double theta=GetMixingAngle(i,j); double delta=GetPhase(i,j); double c=cos(theta); auto cp=sin(theta)*std::exp(std::complex<double>(0,-delta)); auto cpc=-std::conj(cp); gsl_matrix_complex_set(R,i,i,to_gsl(c)); gsl_matrix_complex_set(R,i,j,to_gsl(cp)); gsl_matrix_complex_set(R,j,i,to_gsl(cpc)); gsl_matrix_complex_set(R,j,j,to_gsl(c)); //multiply this rotation onto the product from the left gsl_blas_zgemm(CblasNoTrans,CblasNoTrans,unit,R,U,zero,dummy); std::swap(U,dummy); //clean up the rotation matrix for next iteration gsl_matrix_complex_set(R,i,i,unit); gsl_matrix_complex_set(R,i,j,zero); gsl_matrix_complex_set(R,j,i,zero); gsl_matrix_complex_set(R,j,j,unit); } } //clean up temporary matrices gsl_matrix_complex_free(R); gsl_matrix_complex_free(dummy); return std::unique_ptr<gsl_matrix_complex,void (*)(gsl_matrix_complex*)>(U,gsl_matrix_complex_free); }
int extmat(gsl_matrix_complex * Lf, double nf, gsl_complex cphif) { gsl_matrix_complex_set_zero(Lf); gsl_complex znf = gsl_complex_rect(nf,0); gsl_complex zk = gsl_complex_mul(znf,cphif); gsl_complex one = gsl_complex_rect(1,0); gsl_matrix_complex_set(Lf,0,2,cphif); gsl_matrix_complex_set(Lf,1,2,znf); gsl_matrix_complex_set(Lf,2,0,one); gsl_matrix_complex_set(Lf,3,0,zk); return 1; }
/* internal function used by qpb_sun_project(). Gets the diagonal matrix D, which contains the phases to be used in the back-projection to SU(3) */ void get_theta_matrix(gsl_matrix_complex *D, gsl_vector *S, qpb_double phi){ qpb_double pi = 4. * atan2(1.0, 1.0); qpb_double theta_step = pi / 3.; qpb_double th_initial[NC-1] = {0, 0}; qpb_double max_f = -1e10; gsl_vector *th_trial = gsl_vector_alloc(NC-1); struct func_params f_params; f_params.phi = phi; for(int i=0; i<NC; i++) f_params.s[i] = gsl_vector_get(S, i); for(qpb_double th1 = pi; th1 > -pi; th1-=theta_step) for(qpb_double th2 = pi; th2 > -pi; th2-=theta_step) { gsl_vector_set(th_trial, 0, th1); gsl_vector_set(th_trial, 1, th2); qpb_double f_trial = -func(th_trial, &f_params); if(f_trial > max_f){ max_f = f_trial; th_initial[0] = gsl_vector_get(th_trial, 0); th_initial[1] = gsl_vector_get(th_trial, 1); } } gsl_vector_set(th_trial, 0, th_initial[0]); gsl_vector_set(th_trial, 1, th_initial[1]); minimize_for_thetas(th_trial, &f_params); gsl_matrix_complex_set_zero(D); qpb_double th_sum = 0; for(int i=0; i<NC-1; i++) { gsl_matrix_complex_set(D, i, i, gsl_complex_polar(1., gsl_vector_get(th_trial, i))); th_sum += gsl_vector_get(th_trial, i); } gsl_matrix_complex_set(D, NC-1, NC-1, gsl_complex_polar (1., -phi-th_sum)); gsl_vector_free(th_trial); return; }
void create_random_complex_posdef_matrix(gsl_matrix_complex *m, gsl_rng *r, gsl_vector_complex *work) { const size_t N = m->size1; size_t i, j; double x, y; gsl_complex z; gsl_complex tau; GSL_SET_IMAG(&z, 0.0); /* make a positive diagonal matrix */ gsl_matrix_complex_set_zero(m); for (i = 0; i < N; ++i) { x = gsl_rng_uniform(r); GSL_SET_REAL(&z, x); gsl_matrix_complex_set(m, i, i, z); } /* now generate random householder reflections and form P D P^H */ for (i = 0; i < N; ++i) { /* form complex vector */ for (j = 0; j < N; ++j) { x = 2.0 * gsl_rng_uniform(r) - 1.0; y = 2.0 * gsl_rng_uniform(r) - 1.0; GSL_SET_COMPLEX(&z, x, y); gsl_vector_complex_set(work, j, z); } tau = gsl_linalg_complex_householder_transform(work); gsl_linalg_complex_householder_hm(tau, work, m); gsl_linalg_complex_householder_mh(gsl_complex_conjugate(tau), work, m); } } /* create_random_complex_posdef_matrix() */
void MCPMPChan::Run() { /// fetch data objects gsl_matrix_complex inmat = min1.GetDataObj(); gsl_matrix_complex cmat = min2.GetDataObj(); // inmat : input signal matrix x(n) (NxM) // i // complex sample at time n from Tx number i // cmat : channel coeffs matrix h(n) (M**2xN) // ij // cmat matrix structure // // +- -+ // | h(0) . . . . h(n) | | // | 11 11 | | // | | | Rx1 // | h(0) . . . . h(n) | | // | 12 12 | | // | | // | h(0) . . . . h(n) | | // | 21 21 | | // | | | Rx2 // | h(0) . . . . h(n) | | // | 22 22 | | // +- -+ // // where h(n) represents the channel impulse response // ij // // at time n, from tx i to rx j // the matrix has MxM rows and N comumns. // The (i,j) channel is locater at row i*M+j // with i,j in the range [0,M-1] and rows counting from 0 // // gsl_matrix_complex_set_zero(outmat); for (int rx=0;rx<M();rx++) { //loop through Rx // // csubmat creates a view on cmat extracting the MxN submatrix for Rx number u // gsl_matrix_complex_const_view csubmat = gsl_matrix_complex_const_submatrix(&cmat,rx*M(),0,M(),N()); // // cut a slice of outmat // gsl_vector_complex_view outvec = gsl_matrix_complex_column(outmat,rx); for (int tx=0;tx<M();tx++) { // loop through Tx // // input signal from tx // gsl_vector_complex_view x = gsl_matrix_complex_column(&inmat,tx); gsl_vector_complex *tmp = gsl_vector_complex_alloc(N()); // // // extract the current tx-rx channel matrix // // for (int i=0; i<N(); i++) { gsl_complex h = gsl_matrix_complex_get(&csubmat.matrix,tx,(N()-i)%N()); for (int j=0; j<N(); j++) { gsl_matrix_complex_set(user_chan,j,(j+i) % N(),h); } } // cout << "Channel (" << tx << "-" << rx << "):" << endl; // gsl_matrix_complex_show(user_chan); // // compute the signal rx = H tx // gsl_blas_zgemv(CblasNoTrans, gsl_complex_rect(1.0,0), user_chan, &x.vector, gsl_complex_rect(0,0), tmp); // // sum for each tx // gsl_vector_complex_add(&outvec.vector,tmp); gsl_vector_complex_free(tmp); } // tx loop for (int i=0; i< N(); i++) { gsl_complex noisesample = gsl_complex_rect( gsl_ran_gaussian(ran,noisestd), gsl_ran_gaussian(ran,noisestd)); gsl_complex ctmp = gsl_complex_add(gsl_vector_complex_get(&outvec.vector,i),noisesample); gsl_vector_complex_set(&outvec.vector,i,ctmp); } } // rx loop // cout << "received signals matrix (" << N() << "x" << M() << ")" << endl; // gsl_matrix_complex_show(outmat); //////// production of data mout1.DeliverDataObj( *outmat ); }
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); }
PetscErrorCode cHamiltonianMatrix::measurement(){ double *ALLdepart = new double[Nt]; double *ALLentropy = new double[Nt]; gsl_matrix* density1 = gsl_matrix_alloc(L,Nt);//background fermion density gsl_matrix* density2 = gsl_matrix_alloc(L,Nt);//background fermion density gsl_vector* corr12 = gsl_vector_alloc(Nt);//correlation betwen fermion up and down. // The density correlation is in fact proportional to the interacting energy. double var_rank; PetscScalar var_tmp, var_tmp2; gsl_complex var_tmp_gsl; Vec vectort; VecScatter ctx; ofstream output; VecScatterCreateToZero(WFt[0],&ctx,&vectort); if(rank==0) cout << size << endl; gsl_matrix_complex* RDM = gsl_matrix_complex_alloc(dim2,dim2); gsl_vector *eval_RDM = gsl_vector_alloc(dim2); gsl_eigen_herm_workspace* w_RDM = gsl_eigen_herm_alloc(dim2); for (int itime = 0; itime < Nt; ++itime) { if (rank==0&&itime%10==0) cout << "this is time " << itime << endl; // % ## departure ## var_rank = 0.0; for (int ivar = rstart; ivar < rend; ++ivar) { ierr = VecGetValues(WFt[itime],1,&ivar,&var_tmp);CHKERRQ(ierr); var_rank += pow(gsl_vector_get(rr,ivar)*PetscAbsComplex(var_tmp),2); } MPI_Reduce(&var_rank, &(ALLdepart[itime]), 1, MPI_DOUBLE, MPI_SUM, 0, PETSC_COMM_WORLD); ALLdepart[itime] = sqrt(ALLdepart[itime]); // % ## entropy ## VecScatterBegin(ctx,WFt[itime],vectort,INSERT_VALUES,SCATTER_FORWARD); VecScatterEnd(ctx,WFt[itime],vectort,INSERT_VALUES,SCATTER_FORWARD); if(rank==0) { int ivar;double eigen_RDM; gsl_matrix_complex_set_zero(RDM); for (int row2 = 0; row2 < dim2; ++row2) { for (int col2 = row2; col2 < dim2; ++col2) { var_tmp_gsl.dat[0] = 0.0; var_tmp_gsl.dat[1] = 0.0; for (int jjj = 0; jjj < dim; ++jjj) { ivar = row2*dim+jjj; ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr); ivar = col2*dim+jjj; ierr = VecGetValues(vectort,1,&ivar,&var_tmp2);CHKERRQ(ierr); var_tmp_gsl.dat[0] += PetscRealPart(var_tmp*PetscConj(var_tmp2)); var_tmp_gsl.dat[1] += PetscImaginaryPart(var_tmp*PetscConj(var_tmp2)); } gsl_matrix_complex_set(RDM,row2,col2,var_tmp_gsl); if (col2 != row2) { gsl_matrix_complex_set(RDM,col2,row2,gsl_matrix_complex_get(RDM,row2,col2)); } } } gsl_eigen_herm(RDM,eval_RDM,w_RDM); ALLentropy[itime] = 0; for (ivar = 0; ivar < dim2; ++ivar) { eigen_RDM = gsl_vector_get(eval_RDM, ivar); // cout << eigen_RDM << endl; ALLentropy[itime] += -eigen_RDM*log(eigen_RDM); } } // % ## density distribution of impurity fermion if(rank==0) { int ivar; for (int row2 = 0; row2 < dim2; ++row2) { for (int jpar = 0; jpar < N2; ++jpar) { double density_tmp=0; for (int jjj = 0; jjj < dim; ++jjj) { ivar = row2*dim+jjj; ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr); density_tmp +=pow(PetscAbsComplex(var_tmp),2); } /*if (itime==0) { if (rank==0) cout << "density_tmp=" << density_tmp << endl; }*/ gsl_matrix_set(density2,gsl_matrix_get(basis2,jpar,row2)-1,itime,gsl_matrix_get(density2,gsl_matrix_get(basis2,jpar,row2)-1,itime)+density_tmp); } } } /*if (rank==0) { cout << "density of impurity:" << endl; for (int jpar =0; jpar < L; ++jpar) { cout << gsl_matrix_get(density2,jpar,itime) << "\t"; } cout << endl; }*/ // % ## density distribution of majority fermions if(rank==0) { int ivar; for (int jjj = 0; jjj < dim; ++jjj) { for (int jpar = 0; jpar < N; ++jpar) { double density_tmp=0; for (int row2 = 0; row2 < dim2; ++row2) { ivar = row2*dim+jjj; ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr); density_tmp +=pow(PetscAbsComplex(var_tmp),2); } gsl_matrix_set(density1,gsl_matrix_get(basis1,jpar,jjj)-1,itime,gsl_matrix_get(density1,gsl_matrix_get(basis1,jpar,jjj)-1,itime)+density_tmp); } } } // correlation between impurity and majority fermion if(rank==0) { int ivar; double corr_tmp=0; for (int jimp=0; jimp<dim2; ++jimp) { for (int jmaj=0; jmaj<dim; ++jmaj) { for (int jpar=0; jpar<N; ++jpar) { if (gsl_matrix_get(basis1,jpar,jmaj)==jimp+1){ ivar = jimp*dim+jmaj; ierr = VecGetValues(vectort,1,&ivar,&var_tmp);CHKERRQ(ierr); corr_tmp+=pow(PetscAbsComplex(var_tmp),2); } } } } gsl_vector_set(corr12,itime,corr_tmp); }// end of correlation } if (rank == 0) { char filename[50]; sprintf(filename,"measurement.data"); output.open(filename); output.is_open(); output.precision(16); for (int itime = 0; itime < Nt; ++itime) { if (itime==0) { // cout << "time t[1] " << '\t' << "departure[2] " << '\t' << "entropy[3]" << '\t' << "density of majority [L]" <<'\t' << "density of impurity [L]" << endl; } output << dt*itime-3 << '\t' << ALLdepart[itime] << '\t' << ALLentropy[itime] << '\t'; for (int jpar = 0; jpar < L; ++jpar) { output << gsl_matrix_get(density1,jpar,itime) << '\t'; } for (int jpar = 0; jpar < L; ++jpar) { output << gsl_matrix_get(density2,jpar,itime) << '\t'; } output << gsl_vector_get(corr12,itime) << '\t'; output << endl; } output.close(); } // CopyFile(source,destination,FALSE); delete[] ALLdepart; VecScatterDestroy(&ctx); VecDestroy(&vectort); gsl_matrix_complex_free(RDM); gsl_vector_free(eval_RDM); gsl_eigen_herm_free(w_RDM); gsl_matrix_free(density1); gsl_matrix_free(density2); gsl_vector_free(corr12); // CopyFile(source,destination,FALSE); return ierr; }