void uhfsolve::diagonalizeF(){ //diagonalize the Fock matrix Fprime = V.t()*F*V; Fprimeu = V.t()*Fu*V; Fprimed = V.t()*Fd*V; eig_sym(epsilon, Cprime, Fprime); eig_sym(epsilonu, Cprimeu, Fprimeu); eig_sym(epsilond, Cprimed, Fprimed); //C = V*Cprime.submat(0, 0, nStates - 1, nElectrons/2 -1); C = V*Cprime; //alternating between these two to implement coupled cluster calculations (need virtual orbitals) Cu = V*Cprimeu; //alternating between these two to implement coupled cluster calculations (need virtual orbitals) Cd = V*Cprimed; //alternating between these two to implement coupled cluster calculations (need virtual orbitals) }
inline mat CovMeans_mat_kth::mehrtash_suggestion(mat cov_i) { //Following Mehrtash suggestions as per email dated June26th 2014 mat new_covi; double THRESH = 0.000001; new_covi = 0.5*(cov_i + cov_i.t()); vec D; mat V; eig_sym(D, V, new_covi); uvec q1 = find(D < THRESH); if (q1.n_elem>0) { for (uword pos = 0; pos < q1.n_elem; ++pos) { D( q1(pos) ) = THRESH; } new_covi = V*diagmat(D)*V.t(); } return new_covi; //end suggestions }
//------------------------------------------------------------------------------ void OrbitalEquation::computeOneParticleReducedDensity() { // All possible spatial orbitals for(int i=0; i< nOrbitals; i++){ for(int j=i; j<nOrbitals; j++){ invRho(i,j) = reducedOneParticleOperator(2*i,2*j); invRho(i,j) += reducedOneParticleOperator(2*i+1,2*j+1); invRho(j,i) = conj(invRho(i,j)); } } #ifdef DEBUG //#if 1 cout << "OrbitalEquation::computeOneParticleReducedDensity()" << endl; cout << "Trace(rho) = " << real(trace(invRho)) << " nParticles = " << nParticles << endl; #endif #if 0 //--------------------------- // Regularization //--------------------------- const double e = 1e-10; cx_mat rho_U; vec rho_lambda; eig_sym(rho_lambda, rho_U, invRho); rho_lambda = rho_lambda + e*exp(-rho_lambda*(1.0/e)); cx_mat X = rho_U*diagmat(1.0/rho_lambda)*rho_U.t(); invRho += e*X; //--------------------------- #endif }
// }}} itpp::Mat<std::complex<double> > RandomCUE(int const dim) { //{{{ itpp::Mat<std::complex<double> > H; H = RandomGUEDeltaOne(dim); itpp::Mat<std::complex<double> > U(dim,dim); itpp::Vec<double> eigenvalues(dim); eig_sym(H, eigenvalues, U); // FlatSpectrumGUE(U, eigenvalues); // return exp( 2*itpp::pi*std::complex<double>(0.,1.)* itpp:randu())*U; return exp(2.*itpp::pi*std::complex<double>(0.,1.) *itpp::randu() )*U; }
inline Col<typename T1::pod_type> eig_sym(const Base<typename T1::elem_type,T1>& X) { arma_extra_debug_sigprint(); Col<typename T1::pod_type> out; eig_sym(out, X); return out; }
inline bool op_logmat_cx::helper(Mat<eT>& A, const uword m) { arma_extra_debug_sigprint(); if(A.is_finite() == false) { return false; } const vec indices = regspace<vec>(1,m-1); mat tmp(m,m,fill::zeros); tmp.diag(-1) = indices / sqrt(square(2.0*indices) - 1.0); tmp.diag(+1) = indices / sqrt(square(2.0*indices) - 1.0); vec eigval; mat eigvec; const bool eig_ok = eig_sym(eigval, eigvec, tmp); if(eig_ok == false) { arma_extra_debug_print("logmat(): eig_sym() failed"); return false; } const vec nodes = (eigval + 1.0) / 2.0; const vec weights = square(eigvec.row(0).t()); const uword N = A.n_rows; Mat<eT> B(N,N,fill::zeros); Mat<eT> X; for(uword i=0; i < m; ++i) { // B += weights(i) * solve( (nodes(i)*A + eye< Mat<eT> >(N,N)), A ); //const bool solve_ok = solve( X, (nodes(i)*A + eye< Mat<eT> >(N,N)), A, solve_opts::fast ); const bool solve_ok = solve( X, trimatu(nodes(i)*A + eye< Mat<eT> >(N,N)), A ); if(solve_ok == false) { arma_extra_debug_print("logmat(): solve() failed"); return false; } B += weights(i) * X; } A = B; return true; }
double Solver::CorrolationEnergy(){ // Set up matrices for all blocks #pragma omp parallel { int id = omp_get_thread_num(); int threads = omp_get_num_threads(); for (int i=id; i<Npphh; i+=threads) blockspphh[i] ->SetUpMatrices_Energy(t); //SetUpMatrices_Energy(T); } // Do the matrix-matrix multiplications for all blocks double E = 0.0; for (int i=0; i<Npphh; i++){ mat Energy = blockspphh[i]->V * blockspphh[i]->T; vec eigval = eig_sym(Energy); E += sum(eigval); } return E / 4.0; }
inline void CovMeans_mat_kth::gmm_one_video( std::string load_feat_video_i, std::string load_labels_video_i, int sc, int pe, int act, const int Ng ) { //cout << all_people (pe) << "_" << actions(act) << " "; mat mat_features_video_i; mat_features_video_i.load( load_feat_video_i, hdf5_binary ); int n_vec = mat_features_video_i.n_cols; //cout << " " << n_vec; bool is_finite = mat_features_video_i.is_finite(); if (!is_finite ) { cout << "is_finite?? " << is_finite << endl; cout << mat_features_video_i.n_rows << " " << mat_features_video_i.n_cols << endl; getchar(); } gmm_diag gmm_model; gmm_diag bg_model; bool status_em = false; int rep_em=0; int km_iter = 10; int em_iter = 5; double var_floor = 1e-10; bool print_mode = false; while (!status_em) { bool status_kmeans = false; //int rep_km = 0; while (!status_kmeans) { arma_rng::set_seed_random(); status_kmeans = gmm_model.learn(mat_features_video_i, Ng, eucl_dist, random_subset, km_iter, 0, var_floor, print_mode); //Only Kmeans bg_model = gmm_model; //rep_km++; } status_em = gmm_model.learn(mat_features_video_i, Ng, eucl_dist, keep_existing, 0, em_iter, var_floor, print_mode); rep_em++; if (rep_em==9) { status_em = true; gmm_model = bg_model; } } //cout <<"EM was repeated " << rep_em << endl << endl; mat dcov = gmm_model.dcovs; mat means = gmm_model.means; mat cov_i, logM_cov_i, V; vec mean_i, D; std::stringstream save_folder; save_folder << "./CovMeans/sc" << sc << "/scale" << scale_factor << "-shift"<< shift ; for (int i=1; i<=Ng; ++i) { //cout << i << " out " << Ng << endl; cov_i = diagmat( dcov.col(i-1) ); cov_i = mehrtash_suggestion(cov_i); mean_i = means.col(i -1); eig_sym(D, V, cov_i); mat logM_cov_i = V*diagmat( log(D) )*V.t(); std::stringstream save_Covs; save_Covs << save_folder.str() << "/Cov_"<< i << "_out_" << Ng << "_" << all_people (pe) << "_" << actions(act) << ".h5"; std::stringstream save_logMCovs; save_logMCovs << save_folder.str() << "/logM_Cov_" << i << "_out_" << Ng << "_" << all_people (pe) << "_" << actions(act) << ".h5"; std::stringstream save_Means; save_Means << save_folder.str() << "/Means_" << i << "_out_" << Ng << "_" << all_people (pe) << "_" << actions(act) << ".h5"; //cout << save_Covs.str() << endl; cov_i.save( save_Covs.str(), hdf5_binary ); logM_cov_i.save( save_logMCovs.str(), hdf5_binary ); mean_i.save( save_Means.str(), hdf5_binary ); } }
inline void CovMeans_mat_kth::one_video_one_cov( std::string load_feat_video_i, std::string load_labels_video_i, int sc, int pe, int act ) { // #pragma omp critical // { // cout << load_feat_video_i << endl; // getchar(); // } mat mat_features_video_i; mat_features_video_i.load( load_feat_video_i, hdf5_binary ); int n_vec = mat_features_video_i.n_cols; std::stringstream save_folder; //Shifting both save_folder << "./CovMeans/sc" << sc << "/scale" << scale_factor << "-shift"<< shift ; { // If you want to use. You have to add the flag_shift in this method. // if (flag_shift) //Horizontal Shift // { // // save_folder << "./kth-one-CovsMeans-mat/sc" << sc << "/scale" << scale_factor << "-horshift"<< shift ; // // } // // if (!flag_shift)//Vertical Shift // { // save_folder << "./kth-one-CovsMeans-mat/sc" << sc << "/scale" << scale_factor << "-vershift"<< shift ; // } // } running_stat_vec<rowvec> stat_seg(true); for (int l=0; l<n_vec; ++l) { vec sample = mat_features_video_i.col(l); stat_seg (sample); } std::stringstream save_Covs; save_Covs << save_folder.str() << "/Cov_" << all_people (pe) << "_" << actions(act) << ".h5"; std::stringstream save_logMCovs; save_logMCovs << save_folder.str() << "/logM_Cov_" << all_people (pe) << "_" << actions(act) << ".h5"; std::stringstream save_Means; save_Means << save_folder.str() << "/Means_" << all_people (pe) << "_" << actions(act) << ".h5"; double THRESH = 0.000001; mat cov_i = stat_seg.cov(); vec mean_i = stat_seg.mean(); //Following Mehrtash suggestions as per email dated June26th 2014 cov_i = 0.5*(cov_i + cov_i.t()); vec D; mat V; eig_sym(D, V, cov_i); uvec q1 = find(D < THRESH); if (q1.n_elem>0) { for (uword pos = 0; pos < q1.n_elem; ++pos) { D( q1(pos) ) = THRESH; } cov_i = V*diagmat(D)*V.t(); } //end suggestions eig_sym(D, V, cov_i); mat logM_cov_i = V*diagmat( log(D) )*V.t(); #pragma omp critical { cout << "saving " << all_people (pe) << endl; cov_i.save( save_Covs.str(), hdf5_binary ); logM_cov_i.save( save_logMCovs.str(), hdf5_binary ); mean_i.save( save_Means.str(), hdf5_binary ); } }
void uhfsolve::setupUnitMatrices(){ //Bring overlap matrix to unit form, set up V eig_sym(s_diag,U,Bs.S); //following Thijssen, p38-39 V = U*diagmat(1.0/sqrt(s_diag)); }
int main(){ int n = 200; double N = (double) n; double rho_max = 10.; double rho_min = 0.; double h = (rho_max - rho_min)/N; double omega_r = 1.; double TIME1, TIME2; vec rho(n), V(n-2), eigval; mat A = zeros<mat>(n-2,n-2); mat eigvec; clock_t start, finish, start2, finish2; // Declare start and finish time for(int i=1; i<n-1; i++){ rho[i] = rho_min + (i+1)*h; V[i] = omega_r*omega_r*rho[i]*rho[i] + 1./rho[i]; } rho[0] = 0.; rho[n-1] = rho_max; //Initializes the matrix A: A.diag(-1) += -1./(h*h); A.diag(+1) += -1./(h*h); A.diag() = (2./(h*h)) + V; // Eigenvector matrix: mat R = eye<mat>(n-2,n-2); // Built in eigenvalue/eigenvector procedure in armadillo: start2 = clock(); eig_sym(eigval, eigvec, A); finish2 = clock(); // Jacobi method: start = clock(); Jacobi(&A, &R, (n-2)); finish = clock(); TIME1 = (finish - start)/(double)CLOCKS_PER_SEC; TIME2 = (finish2 - start2)/(double)CLOCKS_PER_SEC; vec l; l = A.diag(); // Wavefunction for the lowest energy level: double MIN = max(l); int index = 0; for(int i=0; i<n-2; i++){ if(l(i) <= MIN){ MIN = l(i); index = i; } } vec u_1 = R.col(index); vec wave(n); for(int i=1; i<n-1; i++){ wave(i) = u_1(i-1); } // Dirichlet boundary conditions wave(0) = 0; wave(n-1) = 0; //Writing wave and rho to file: string filename; string Omega = static_cast<ostringstream*>( \ &(ostringstream() << omega_r*100) )->str(); filename = "wavefunc2_omega"+Omega+".dat"; ofile.open(filename.c_str(), ios::out); for(int i=0; i<n; i++){ ofile << rho[i] << ' ' << wave[i] << endl; } ofile.close(); vec lam = sort(l); cout << "omega_r = " << omega_r << endl; cout << "three lowest eigenvalues:" << endl; for(int i=0;i<3;i++){ cout << "lambda = " << lam(i) << endl; } cout << "computation time for jacobi.cpp with n = " << n << ": " << TIME1 << " s" << endl; cout << "computation time for built in procedure with n = " << n << ": " << TIME2 << " s" << endl; return 0; }
//------------------------------------------------------------------------------ void MfLowRankApproximation::initialize() { double dx; double constValue; double endValue; double constEnd; double epsilon; dx = grid.DX; try { nOrbitals = cfg->lookup("spatialDiscretization.nSpatialOrbitals"); constEnd = cfg->lookup("meanFieldIntegrator.lowRankApproximation.constEnd"); constValue = cfg->lookup("meanFieldIntegrator.lowRankApproximation.constValue"); endValue = cfg->lookup("meanFieldIntegrator.lowRankApproximation.endValue"); epsilon = cfg->lookup("meanFieldIntegrator.lowRankApproximation.epsilon"); } catch (const SettingNotFoundException &nfex) { cerr << "MfLowRankApproximation::Error reading entry from config object." << endl; exit(EXIT_FAILURE); } int nConst = constEnd/dx; int constCenter = nGrid/2; Vxy = zeros(nGrid, nGrid); for(uint p=0; p<potential.size(); p++) { for(int i=0; i<nGrid; i++) { for(int j=0; j<nGrid; j++) { Vxy(i, j) += potential[p]->evaluate(i, j); } } } // Using a simple discretization equal to the discretization of // the system. mat h = hExactSpatial(); // mat h = hPiecewiseLinear(); mat Q = eye(nGrid,nGrid); // Using a consant weight in the center of the potential // and a linear decrease from the center. vec g = gLinear(nGrid, constCenter, nConst, constValue, endValue); mat C = cMatrix(g, h, dx); vec lambda; mat eigvec; eig_sym(lambda, eigvec, inv(C.t())*Vxy*inv(C)); mat Ut = C.t()*eigvec; mat QU = inv(Q)*Ut; // Sorting the eigenvalues by absoulte value and finding the number // of eigenvalues with abs(eigenval(i)) > epsilon uvec indices = sort_index(abs(lambda), 1); M = -1; for(uint m=0; m <lambda.n_rows; m++) { cout << abs(lambda(indices(m))) << endl; if(abs(lambda(indices(m))) < epsilon) { M = m; break; } } // cout << min(abs(lambda)) << endl; // cout << "hei" << endl; // cout << lambda << endl; // M = 63; if(M < 0) { cerr << "MfLowRankApproximation:: no eigenvalues < epsilon found." << " Try setting epsilon to a higher number" << endl; exit(EXIT_FAILURE); } eigenval = zeros(M); for(int m=0; m <M; m++) { eigenval(m) = lambda(indices(m)); } // Calculating the U matrix U = zeros(nGrid, M); for(int m=0; m < M; m++) { for(uint j=0; j<h.n_rows; j++) { U(j,m) = 0; for(uint i=0; i<h.n_cols; i++) { U(j,m) += h(j,i)*QU(i,indices(m)); } } } cout << "MfLowRankApproximation:: Trunction of eigenvalues at M = " << M << endl; #if 1 // For testing the low rank approximation's accuracy mat appV = zeros(nGrid, nGrid); for(int i=0; i<nGrid; i++) { for(int j=0; j<nGrid; j++) { appV(i,j) = 0; for(int m=0; m<M; m++) { appV(i,j) += eigenval(m)*U(i,m)*U(j,m); } } } mat diffV = abs(Vxy - appV); cout << "max_err = " << max(max(abs(Vxy - appV))) << endl; diffV.save("../DATA/diffV.mat"); appV.save("../DATA/Vapp.mat"); Vxy.save("../DATA/Vex.mat"); cout << nGrid << endl; // exit(EXIT_SUCCESS); #endif cout << "test" << endl; Vm = zeros<cx_vec>(M); Vqr = zeros<cx_vec>(nGrid); }