VectorXd GLMObjective::coordinate_descent(RegFunction *regfunc, int idx) { VectorXd gr = X[idx].transpose() * -R / n; VectorXd tmp; MatrixXd wXX(p, p); wXX.setZero(); for (int i = 0; i < n; i++) wXX += W[i] * X[idx].row(i).transpose() * X[idx].row(i); wXX /= n; VectorXcd eigenvalues = wXX.eigenvalues(); double step_size = 0; for (int i = 0; i < eigenvalues.size(); i++) { step_size = std::max(step_size, eigenvalues[i].real()); } assert(step_size >= 0); tmp = regfunc->threshold_p((model_param.beta[idx] - gr / step_size), step_size); VectorXd delta_beta = tmp - model_param.beta[idx]; tmp = (wXX*model_param.beta[idx] - gr); VectorXd old_beta = model_param.beta[idx]; model_param.beta[idx] = regfunc->threshold(tmp)/step_size; delta_beta = model_param.beta[idx] - old_beta; if (calc_norm(delta_beta) > 1e-8) { Xb += X[idx] * delta_beta; R -= W.cwiseProduct(X[idx] * delta_beta); } return model_param.beta[idx]; }
void eigs_gen_Cpp(MatrixXd &M, VectorXd &init_resid, int k, int m, double &time_used, double &prec_err) { double start, end; start = get_wall_time(); DenseGenMatProd<double> op(M); GenEigsSolver< double, LARGEST_MAGN, DenseGenMatProd<double> > eigs(&op, k, m); eigs.init(init_resid.data()); int nconv = eigs.compute(); int niter = eigs.num_iterations(); int nops = eigs.num_operations(); // std::cout << "nops = " << nops << std::endl; VectorXcd evals = eigs.eigenvalues(); MatrixXcd evecs = eigs.eigenvectors(); /* std::cout << "computed eigenvalues D = \n" << evals.transpose() << std::endl; std::cout << "first 5 rows of computed eigenvectors U = \n" << evecs.topRows<5>() << std::endl; std::cout << "nconv = " << nconv << std::endl; std::cout << "niter = " << niter << std::endl; std::cout << "nops = " << nops << std::endl; MatrixXcd err = M * evecs - evecs * evals.asDiagonal(); std::cout << "||AU - UD||_inf = " << err.array().abs().maxCoeff() << std::endl; */ end = get_wall_time(); time_used = (end - start) * 1000; MatrixXcd err = M * evecs - evecs * evals.asDiagonal(); prec_err = err.cwiseAbs().maxCoeff(); }
Proector::Proector(HilbertSpace space) { for (int i = 0; i < space.totalDimension(); ++i) { VectorXi toLabel = space.getVector(i); VectorXcd op = space.getBasisVector(toLabel); addOperator(op * op.transpose(), _vecToLabel(toLabel)); } }
Vector<Pointf> DataSource::FFT(Getdatafun getdata, double tSample, bool frequency, int type, bool window) { int numData = int(GetCount()); VectorXd timebuf(numData); int num = 0; for (int i = 0; i < numData; ++i) { double data = Membercall(getdata)(i); if (!IsNull(data)) { timebuf[i] = Membercall(getdata)(i); num++; } } Vector<Pointf> res; if (num < 3) return res; timebuf.resize(num); double windowSum = 0; if (window) { for (int i = 0; i < numData; ++i) { double windowDat = 0.54 - 0.46*cos(2*M_PI*i/numData); windowSum += windowDat; timebuf[i] *= windowDat; // Hamming window } } else windowSum = numData; VectorXcd freqbuf; try { Eigen::FFT<double> fft; fft.SetFlag(fft.HalfSpectrum); fft.fwd(freqbuf, timebuf); } catch(...) { return res; } if (frequency) { for (int i = 0; i < int(freqbuf.size()); ++i) { double xdata = i/(tSample*numData); switch (type) { case T_PHASE: res << Pointf(xdata, std::arg(freqbuf[i])); break; case T_FFT: res << Pointf(xdata, 2*std::abs(freqbuf[i])/windowSum); break; case T_PSD: res << Pointf(xdata, 2*sqr(std::abs(freqbuf[i]))/(windowSum/tSample)); } } } else { for (int i = int(freqbuf.size()) - 1; i > 0; --i) { double xdata = (tSample*numData)/i; switch (type) { case T_PHASE: res << Pointf(xdata, std::arg(freqbuf[i])); break; case T_FFT: res << Pointf(xdata, 2*std::abs(freqbuf[i])/windowSum); break; case T_PSD: res << Pointf(xdata, 2*sqr(std::abs(freqbuf[i]))/(windowSum/tSample)); } } } return res; }
void HIntegrand::CalculateOriginalIntegrand(const VectorXd &x, VectorXcd &y) const { complex_t Hband, Hpow = 1.; y.resize(dim_out()); y.setZero(); getHband(x, Hband, SK, Hf); for (int n = 0; n < N_order; ++n) { Hpow *= Hband; for (int i = 0; i < MSIZE; ++i) y(MSIZE*n+i) = Hpow; } }
double GLMObjective::get_local_change(const VectorXd &old, int idx) { VectorXd delta_beta = old - model_param.beta[idx]; VectorXd delta_Xb = X[idx] * delta_beta; MatrixXd wXX(p, p); for (int i = 0; i < p; i++) for (int j = 0; j < p; j++) wXX(i, j) = 0; for (int i = 0; i < n; i++) wXX += W[i] * X[idx].row(i).transpose() * X[idx].row(i); VectorXcd eigenvalues = wXX.eigenvalues(); double max_eigen = 0; for (int i = 0; i < eigenvalues.size(); i++) { max_eigen = std::max(max_eigen, eigenvalues[i].real()); } return max_eigen * (delta_beta.dot(delta_beta)) / (2 * n); }
void EigsGen::eigenvalueSchur(const MatrixXd &Rm, VectorXcd &result) { int m = Rm.cols(); if(result.size() != m) result.resize(m); for(int i = 0; i < m; i++) { if(i == m - 1 || fabs(Rm(i + 1, i)) < 1e-16) { result[i] = std::complex<double>(Rm(i, i), 0); } else { result[i] = eigenvalue2x2(Rm(i, i), Rm(i, i + 1), Rm(i + 1, i), Rm(i + 1, i + 1)); i++; result[i] = conj(result[i - 1]); } } }
VectorTypeHost eigenToCusp(const VectorXcd& psiEigen) { int length = psiEigen.size(); VectorTypeHost psiCusp(length, 0.0); for (int i = 0; i < length; ++i) psiCusp[i] = eigenToCusp(psiEigen[i]); return psiCusp; }
void GreenIntegrand::CalculateOriginalIntegrand(const VectorXd &x, VectorXcd &y) const { y.resize(dim_out()); complex_t Hband; getHband(x, Hband, SK, H); for (int L = 0; L < MSIZE; ++L) { y(L) = 1. / (w + mu - Hband - SE(L)); } }
void EigsGen::sortDesc(VectorXcd &values) { if(values.imag().isZero()) std::sort(values.data(), values.data() + values.size(), compare_complex_real); else std::sort(values.data(), values.data() + values.size(), compare_complex_mod); }
PyObject* calc_Gavg(PyObject *pyw, const double &delta, const double &mu, PyObject *pySE, PyObject *pyTB, const double &Hf, PyObject *py_bp, PyObject *py_wf, const int nthreads) { try { if (nthreads > 0) omp_set_num_threads(nthreads); MatrixXcd SE; VectorXcd w; VectorXd bp, wf; VectorXd SlaterKosterCoeffs; numpy::from_numpy(pySE, SE); numpy::from_numpy(pyTB, SlaterKosterCoeffs); numpy::from_numpy(py_bp, bp); numpy::from_numpy(py_wf, wf); numpy::from_numpy(pyw, w); assert(w.size() == SE.rows()); double t = SlaterKosterCoeffs(0); VectorXd xl(DIM), xh(DIM); xl << -2*t; xh << 2*t; double BZ1 = 1.; MatrixXcd result(SE.rows(), MSIZE*MSIZE); VectorXcd tmp(MSIZE); GreenIntegrand green_integrand(w, mu, SE, SlaterKosterCoeffs, Hf); result.setZero(); for (int n = 0; n < w.size(); ++n) { green_integrand.set_data(n); tmp = md_int::Integrate(xl, xh, green_integrand, bp, wf); for (int i = 0; i < MSIZE; ++i) result(n, MSIZE*i + i) = tmp(i); } result /= BZ1; return numpy::to_numpy(result); } catch (const char *str) { std::cerr << str << std::endl; return Py_None; } }
void EigsGen::sortDescPair(VectorXcd &values, VectorXi &index) { int len = values.size(); if(len != index.size()) return; std::vector<SortPair> v(len); for(int i = 0; i < len; i++) { v[i].first = values[i]; v[i].second = index[i]; } if(values.imag().isZero()) std::sort(v.begin(), v.end(), compare_pair<COMPREAL>); else std::sort(v.begin(), v.end(), compare_pair<COMPMOD>); for(int i = 0; i < len; i++) { values[i] = v[i].first; index[i] = v[i].second; } }
void getRealValues(std::vector<double> &realValues, VectorXcd &complexValues, double &a, double &b) { double threshold = 10; threshold = pow(10, -20); for (int i = 0; i < complexValues.size(); ++i) { double imagValue = fabs(complexValues(i).imag()); if(imagValue < threshold) { if(a <= complexValues(i).real() && b >= complexValues(i).real()) { realValues.push_back(complexValues(i).real()); } } } std::sort(realValues.begin(), realValues.end()); }
VectorXcd _LC_EXPs<M>::DAtR(const VectorXcd& rs) const { int num_rs(rs.size()); VectorXcd ys = VectorXcd::Zero(num_rs); for(int i_r = 0; i_r < num_rs; i_r++) { dcomplex r(rs[i_r]); dcomplex y(0); for(int j_b = 0; j_b < this->size(); j_b++) { dcomplex c(this->cs[j_b]); int n(this->ns[j_b]); dcomplex z(this->zs[j_b]); y += Exp_DAtR<M>(r, c, n, z); } ys[i_r] = y; } return ys; }
VectorXcd _LC_EXPs<M>::AtR(const VectorXcd& rs) const { int num_rs(rs.size()); VectorXcd ys = VectorXcd::Zero(num_rs); for(int i_r = 0; i_r < num_rs; i_r++) { dcomplex r(rs[i_r]); dcomplex rr(pow(r, M)); dcomplex y(0); for(int j_b = 0; j_b < this->size(); j_b++) { dcomplex c(this->cs[j_b]); int n(this->ns[j_b]); dcomplex z(this->zs[j_b]); y += c * pow(r, n) * exp(-z * rr); } ys[i_r] = y; } return ys; }
int main() { // The eigen approach ArrayXd n = ArrayXd::LinSpaced(N+1,0,N); double multiplier = M_PI/N; Array<double, 1, N+1> nT = n.transpose(); ArrayXd x = - cos(multiplier*n); ArrayXd xsub = x.middleRows(1, N-1); ArrayXd ysub = (x1-x0)/2*xsub + (x1+x0)/2; ArrayXXd T = cos((acos(x).matrix()*nT.matrix()).array()); ArrayXXd Tsub = cos((acos(xsub).matrix()*nT.matrix()).array()); ArrayXd sqinx = 1/sqrt(1-xsub*xsub); MatrixXd inv1x2 = (sqinx).matrix().asDiagonal(); // Can't use the following to test elements of inv1x2 // std::cout << inv1x2(0,0) << "\n"; MatrixXd Usub = inv1x2 * sin(((acos(xsub).matrix())*nT.matrix()).array()).matrix(); MatrixXd dTsub = Usub*(n.matrix().asDiagonal()); MatrixXd d2Tsub = ((sqinx*sqinx).matrix().asDiagonal())*((xsub.matrix().asDiagonal()) * (dTsub.matrix()) - (Tsub.matrix()) * ((n*n).matrix().asDiagonal())); MatrixXd d2T(N+1, N+1); RowVectorXd a = (pow((-1),nT))*(nT*nT+1)*(nT*nT)/3; RowVectorXd b = (nT*nT+1)*(nT*nT)/3; d2T.middleRows(1,N-1) = d2Tsub; d2T.row(0) = a; d2T.row(N) = b; MatrixXd D2 = d2T.matrix() * ((T.matrix()).inverse()); MatrixXd E2 = D2.middleRows(1,N-1).middleCols(1,N-1); MatrixXd Y = ysub.matrix().asDiagonal(); MatrixXd H = - (4 / ((x1-x0)*(x1-x0))) * E2 + k*Y; Eigen::EigenSolver<Eigen::MatrixXd> HE(H); VectorXcd D = HE.eigenvalues(); MatrixXcd V = HE.eigenvectors(); std::cout << HE.info() << std::endl; // Open ofstream ofstream Dfile; Dfile.open("D-output.txt"); ofstream Vfile; Vfile.open("V-output.txt"); ofstream V544file; V544file.open("V544-output.txt"); Dfile.precision(15); Dfile << D.real() << "\n"; Vfile.precision(15); Vfile << V.real() << "\n"; V544file.precision(15); for(int i = 1; i<N-1; i++) { V544file << ysub[i-1]; V544file << " " << V.col(544).row(i-1).real() << "\n"; } Dfile.close(); Vfile.close(); V544file.close(); system("gnuplot -p plot.gp"); system("rsvg-convert -w 2000 -o V544-plot.png V544-plot.svg"); }
void EigsGen::transformEigenvalues(VectorXcd &evals) { // Only transform eigenvalues when using real sigma if(workmode == 3 && fabs(op->getsigmai()) < 1e-16) evals = 1.0 / evals.array() + op->getsigmar(); }
void calc_pseudogap_selfenergy(double kx,double ky, VectorXcd &Epg, double &norm){ MPI_Comm_rank(MPI_COMM_WORLD,&myrank); int namelen; char processor_name[MPI_MAX_PROCESSOR_NAME]; MPI_Get_processor_name(processor_name, &namelen); cout << "hello rank " << myrank << " " << processor_name << endl; VectorXcd one = VectorXcd::Ones(2*Nf-1); VectorXd wd = VectorXd::LinSpaced(2*Nf-1,wLower,wUpper); VectorXcd w = VectorXcd::Zero(2*Nf-1); for(int i=0; i<2*Nf-1; i++) w[i]=wd[i]; int Nk = Nkpoints; double totalN = pow(2*Nk,2)*(2*Nkz); if (Nkz==1) totalN = pow(2*Nk,2); double pgap = PseudoGap(kx,ky); complex<double> i1(0.0,1.0); double qx,qy,qz,px,py,pofq,Energy; for (int q1=-Nk+myrank; q1<Nk; q1+=numprocs){ //BZ momentum loop qx = M_PI*q1/Nk; for(int q2=-Nk; q2<Nk; q2++){ qy = M_PI*q2/Nk; if (Nkz==1){ px = kx - qx; py = ky - qy; Energy = E(px,py); pofq = 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx-Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx+Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx,2)+pow(qy-Q,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx,2)+pow(qy+Q,2)); //pofq = 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx-Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx+Q,2)+pow(qy,2)); Epg += pgap*pofq*one.cwiseQuotient(w-one*E(px,py)+i1*gammae*one)/totalN; norm += pofq/totalN; } else{ for(int q3=-Nkz; q3<Nkz; q3++){ qz = M_PI*q3/Nkz; px = kx - qx; py = ky - qy; pofq = 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx-Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx+Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx,2)+pow(qy-Q,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx,2)+pow(qy+Q,2)); //pofq = 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx-Q,2)+pow(qy,2)) + 1.0/M_PI*gammacdw/(pow(gammacdw,2)+pow(qx+Q,2)+pow(qy,2)); Epg += pgap*pofq*one.cwiseQuotient(w-one*(E(px,py)-2*tz*cos(qz))+i1*gammae*one)/totalN; norm += pofq/totalN; } } } } // end BZ loop }
int main(int argc, char* argv[]){ MPI_Init(&argc, &argv); MPI_Comm_size(MPI_COMM_WORLD,&numprocs); MPI_Comm_rank(MPI_COMM_WORLD,&myrank); if(myrank==0){ cout << "starting program" << endl; cout << "numprocs" << numprocs << endl; } time_t tStart = time(NULL); kx = Q/2; ky = 1.94643; Nkpoints = 300; wLower = E(0,M_PI); wUpper = 0; Nf = 50; VectorXcd Epg = VectorXcd::Zero(2*Nf-1); double norm; double norm_private; VectorXcd one = VectorXcd::Ones(2*Nf-1); VectorXd wd = VectorXd::LinSpaced(2*Nf-1,wLower,wUpper); VectorXcd w = VectorXcd::Zero(2*Nf-1); for(int i=0; i<2*Nf-1; i++) w[i]=wd[i]; while(true){ if (myrank==0){ cout << "Enter new D0_pg \n" << endl; cin >> D0_pg; D0_pg /= tval; } MPI_Bcast(&D0_pg, 1, MPI_DOUBLE, 0, MPI_COMM_WORLD); if (D0_pg == 0){ MPI_Finalize(); return 0; } MPI_Barrier(MPI_COMM_WORLD); if (1==1){ //pseudogap Epg = VectorXcd::Zero(2*Nf-1); norm = 0; norm_private = 0; if(myrank==0) cout << endl << "Calculating pseudogap selfenergies..." << endl; MPI_Barrier(MPI_COMM_WORLD); //calc_phonon_selfenergy(kx, ky, Eph_private, Ephm_private); calc_pseudogap_selfenergy(kx,ky,Epg,norm_private); //set up double[] for reduction complex<double> Epg_private_array[2*Nf-1]; complex<double> Epg_array[2*Nf-1]; for(int i=0;i<2*Nf-1;i++){ Epg_private_array[i] = Epg[i]; } MPI_Reduce(&Epg_private_array, &Epg_array, 2*Nf-1, MPI_DOUBLE_COMPLEX, MPI_SUM, 0, MPI_COMM_WORLD); MPI_Reduce(&norm_private, &norm, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD); if(myrank==0) cout << "the norm is: " << norm << endl; if (myrank==0){ for(int i=0;i<2*Nf-1;i++){ Epg[i] = Epg_array[i]; } Epg /= norm; cout<<"Time taken "<< time(NULL)-tStart<<endl; ofstream myfile; myfile.open("Epg.dat"); for (int i=0; i<2*Nf-1; i++){ myfile << real(Epg[i]) << " " << imag(Epg[i]) << endl; } myfile.close(); VectorXcd G = VectorXcd::Zero(2*Nf-1); //VectorXd A = VectorXd::Zero(2*Nf-1); double A[2*Nf-1]; G = one.cwiseQuotient(w + (1j*gammae + E(kx,ky))*one - Epg); ///A = -imag(G); for (int i=0;i<2*Nf-1;i++){ A[i] = -imag(G[i]); cout << A[i] << endl; } double* biggest; double* first = A; double* last = first + 2*Nf-1; biggest = max_element(first,last); cout << "freq at max " << w[distance(first,biggest)]*tval << endl; //double maxIndex; //G.maxCoeff(&maxIndex); //cout << maxIndex << endl; cout << endl << "Done With Program" << endl; } } }