int cHamiltonianMatrix::myindex(gsl_vector * site, const int _N_){ // for (int var = 0; var < site->size; ++var) { // cout << gsl_vector_get(site,var) << endl; // } // cout << "total size of site is " << site->size << endl; // cout << "_N_ value is " << _N_ << endl; int p = 0; if (_N_ == 1) { p = gsl_vector_get(site,0); } else { int loop = 1; for (int i = 1; i <= gsl_vector_get(site,loop-1)-1; i++) { p += gsl_sf_choose(L-i,_N_-loop); } loop ++; // do { // for (int i = gsl_vector_get(site,loop-2)+1; i <= gsl_vector_get(site,loop-1)-1; i++) { // I previously made a mistake here too, of having i < gsl_vector_get(site,loop-1)-1 // p += gsl_sf_choose(L-i,_N_-loop); // } // loop ++; // } while (loop<_N_); // The above code use do-while loop, which is different from the while loop in matlab. Off by one error... for(loop=2;loop<_N_;loop++){ for (int i = gsl_vector_get(site,loop-2)+1; i <= gsl_vector_get(site,loop-1)-1; i++) { p += gsl_sf_choose(L-i,_N_-loop); } } p += gsl_vector_get(site,_N_-1)-gsl_vector_get(site,_N_-2); // % that's fine! } // cout << "q is " << p << endl; return p; }
double SecondDerivSmoothedGauss( double x, double ai, double ki, double ci, int n ) { double f1 = gsl_pow_4 (-n) * ai; double parc = 0.0; for (int j=0; j <= 2*n; j++) { double f2 = gsl_sf_choose (2*n, j); f2 *= exp(-ki*(x+j-n-ci)); parc += f2; } return f1 * parc; }
void cHamiltonianMatrix::construct_basis(gsl_matrix *basis, const int spin_flag){ int _N, _dim, loop, sumAA, len, num, sum2, index_loop, index_p, index_j; spin_flag_condition(_N,_dim,spin_flag); gsl_vector * site = gsl_vector_alloc(_N+1); for (int p = 1; p <= _dim; ++p) { index_p = p-1; // MATLAB vector and matrix index starts from 1 while C/C++ index starts from 0. Be careful about the 0 and 1 bug. loop = 1; index_loop = loop-1; if (_N==1) { loop++; index_loop = loop-1; gsl_vector_set(site,index_loop,p); } else { sumAA = 0; do { len = L-gsl_vector_get(site,index_loop); num = _N-loop; for (int jcar = len-1; jcar >= num; jcar=jcar-1) { sum2 = sumAA; sumAA += gsl_sf_choose(jcar,num); if (sumAA >= p) { loop++; index_loop = loop-1; sumAA=sum2; gsl_vector_set(site, index_loop, gsl_vector_get(site,index_loop-1)+len-jcar ); break; } } } while (loop < _N); gsl_vector_set(site, index_loop+1, gsl_vector_get(site,index_loop)+p-sum2); } for (int j = 1; j <= _N; ++j) { index_j = j-1; gsl_matrix_set(basis, index_j, index_p, gsl_vector_get(site,index_j+1)); } } gsl_vector_free (site); // Debugging purpose: output. // if (rank == 0){ // only root CPU prints the result to screen. // for (int i = 0; i < _N; ++i) { // for (int j = 0; j < _dim; ++j) { // cout << gsl_matrix_get(basis,i,j) << '\t'; // } // cout << "EOL" << endl; // } // } }
double gsl_ran_binomial_pdf (const unsigned int k, const double p, const unsigned int n) { if (k > n) { return 0 ; } else { double a = k; double b = n - k; double P; double Cnk = gsl_sf_choose (n, k) ; P = Cnk * pow (p, a) * pow (1 - p, b); return P; } }
void SecondDerivSmoothedGaussPartials( double x, double s, double ai, double ki, double ci, int n, double &Ji0, double &Ji1, double &Ji2 ) { double f2 = gsl_pow_4 (-n); double f1 = f2 * ai; double xci = x - ci; double a1 = 0.0; double k1 = 0.0; double c1 = 0.0; for (int j=0; j <= 2*n; j++) { int jn = j - n; double fbin = gsl_sf_choose( 2*n, j); double fexp = exp(xci + jn); double fa1 = 2.0 * ki *(-1 + 2.0*ki*gsl_pow_2(xci + jn) ); double fk1 = 2.0 * (-1 + 5.0*ki*gsl_pow_2(xci + jn) - 2.0*gsl_pow_2(ki)*gsl_pow_4(xci+jn) ); double fc1 = 4.0 * gsl_pow_2(ki) * (-3 + 2.0 * ki * gsl_pow_2(xci + jn) ) * (jn - xci); a1 += fbin * fexp * fa1; k1 += fbin * fexp * fk1; c1 += fbin * fexp * fc1; } Ji0 = f2 * a1 / s; Ji1 = f1 * k1 / s; Ji2 = f1 * c1 / s; }
STDLL stata_call(int argc, char *argv[]) { ST_uint4 j, i, from, to; ST_uint4 n; ST_long nSubsets; ST_uint4 nOutputVars=9; /* ES_F, varES_F, es_R, varES_R, df, Q, I2, tau2 subsetString */ ST_uint4 nInputVars=2; /* ES, var*/ ST_double z, es, var; ST_retcode rc; //ST_double version = 0.1; ST_double version = 0.2; char buf[80]; gsl_vector * esVector; gsl_vector * varVector; gsl_vector * metaResultsVector; //if (argc != 1) { if (argc != 3) { snprintf(buf, 80, "Plugin (ver: %g) usage: c_meta nStudies\n", version); SF_error(buf); SF_error("Please debug the calling .ado file!!!\n"); return (-1); } n = (ST_uint4) atoi(argv[0]); from = (ST_uint4) atoi(argv[1]); /* added modification */ to = (ST_uint4) atoi(argv[2]); /* added modification */ /* // debugging by printing out arguments for (i=0; i< argc; i++) { snprintf(buf, 80, "\narg%d=%s", i, argv[i]); SF_error(buf); } snprintf(buf, 80, "\nfrom=%d\nto=%d", from, to); SF_error(buf); */ esVector = gsl_vector_alloc(n); varVector = gsl_vector_alloc(n); /* This will hold the meta-analysis results */ metaResultsVector = gsl_vector_alloc(nOutputVars-1); /* last var is string */ /*nSubsets = gsl_pow_int(2,n)-1;*/ nSubsets = 0; for(i=from;i<=to;i++) { nSubsets = nSubsets + gsl_sf_choose(n, i); } if (SF_in2()<nSubsets) { snprintf(buf, 80, "Plugin needs %ld observations in dataset\n", nSubsets); SF_error(buf); SF_error("Please debug the calling .ado file!!!\n"); return (-1); } if (SF_nvars() != nInputVars+nOutputVars) { snprintf(buf, 80, "Plugin expects %d input and %d output variables!\n", nInputVars, nOutputVars); SF_error(buf); SF_error("Please debug the calling .ado file!!!\n"); return(102); } for(j=SF_in1(); j<= n ; j++) { if (SF_ifobs(j)) { if((rc=SF_vdata(1,j,&z))) return(rc); es = z; if((rc=SF_vdata(2,j,&z))) return(rc); var = z; if(SF_is_missing(es) | SF_is_missing(var)) { snprintf(buf, 80,"You have passed missing values (obs # %d) in the C plugin!\n", j); SF_error(buf); SF_error("Please debug the calling .ado file!!!\n"); return(-1); } else { /* do stuff here */ gsl_vector_set(esVector,j-1, es); gsl_vector_set(varVector,j-1, var); } } } if ((rc=AllSubsetsMetaAnalysis(esVector, varVector, metaResultsVector, from, to))) return(rc); gsl_vector_free(esVector); gsl_vector_free(varVector); gsl_vector_free(metaResultsVector); return(0); }
static VALUE rb_gsl_sf_choose(VALUE obj, VALUE n, VALUE m) { return rb_float_new(gsl_sf_choose(FIX2INT(n), FIX2INT(m))); }
PetscErrorCode cHamiltonianMatrix::input(){ MPI_Comm_size(PETSC_COMM_WORLD,&size); MPI_Comm_rank(PETSC_COMM_WORLD,&rank); // cout << rank << '\t' << size << endl; cout.precision(16); for (int ig = 0; ig < size; ++ig) { if (ig ==rank){ char dummyname[100]; double dummyvalue; int intdummyvalue; FILE *input; input = fopen("input.txt","r"); assert(input != NULL); if (ig == 0) { cout << "Starting to read in parameters from file input.txt" << endl; cout << " --- Time Dependent Evolution of Disordered Impurity fermion ---" << endl; cout << "Parameters of System:" << endl; cout << "---------------------------------------------------------- " << endl; } fscanf(input,"%s %d", dummyname, &intdummyvalue); L = intdummyvalue; if (ig == 0) cout << "SystemSize " << dummyname << "=" << L << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); N = intdummyvalue; if (ig == 0) cout << "Majority fermion number " << dummyname << "=" << N << endl; // N = L/2; // --> L has to be even number here!!! TODO: odd number case? // cout << "Number of Particle N=" << N << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); N2 = intdummyvalue; if (ig == 0) cout << dummyname << "=" << N2 << endl; fscanf(input,"%s %lf", dummyname, &dummyvalue); dim = PetscInt ( gsl_sf_choose(L,N)); dim2= PetscInt ( gsl_sf_choose(L,N2)); DIM = dim*dim2; if (ig == 0) cout << "Dimension of Hilbert Space: " << dim*dim2 << endl; W = dummyvalue; if (ig == 0) cout << "Disorder " << dummyname << "=" << W << endl; fscanf(input,"%s %lf", dummyname, &dummyvalue); U = dummyvalue; if (ig == 0) cout << "Interaction" << dummyname << "=" << U << endl; fscanf(input,"%s %lf", dummyname, &dummyvalue); tmax = dummyvalue; if (ig == 0) cout << "Total time " << dummyname << "=" << tmax << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); Nt = intdummyvalue; if (ig == 0) cout << dummyname << "=" << Nt << endl; dt=(log(tmax)+3.0)/Nt; if (ig == 0) cout << "time steps in log time scale = " << dt << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); judgeB = intdummyvalue; if (ig == 0) cout << dummyname << "=" << judgeB << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); judgeI = intdummyvalue; if (ig == 0) cout << dummyname << "=" << judgeI << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); boundary = intdummyvalue; if (ig == 0) cout << dummyname << "=" << boundary << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); position = intdummyvalue; if (ig == 0) cout << dummyname << "=" << position << endl; fscanf(input,"%s %lf", dummyname, &dummyvalue); set_gsl_under_flow_ratio = dummyvalue; if (ig == 0) cout << dummyname << "=" << set_gsl_under_flow_ratio << endl; fscanf(input,"%s %d", dummyname, &intdummyvalue); set_gsl_shift_value = intdummyvalue; if (ig == 0) cout << dummyname << "=" << set_gsl_shift_value << endl; fclose(input); if (ig == 0) { if (judgeB==0) { cout << "Clean background!---This background serves as a heat bath! " << endl; } else { cout << "Disordered background!---'energy' bath due to interaction" << endl; } if (judgeI==0) { cout << "Clean impurity!---This background serves as a heat bath! " << endl; } else { cout << "Disordered impurity!---'energy' bath due to interaction" << endl; } if (boundary==0) { cout << "#Periodic Boundary Conditions" << endl; } else { cout << "#Open Boundary Conditions" << endl; } cout << "---------------------------------------------------------- " << endl; } } } return ierr; }