void LanczosShader::createKernel(float delta, int *size) { const float a = 2.0; // The two outermost samples always fall at points where the lanczos // function returns 0, so we'll skip them. const int sampleCount = qBound(3, qCeil(delta * a) * 2 + 1 - 2, 29); const int center = sampleCount / 2; const int kernelSize = center + 1; const float factor = 1.0 / delta; QVector<float> values(kernelSize); float sum = 0; for (int i = 0; i < kernelSize; i++) { const float val = lanczos(i * factor, a); sum += i > 0 ? val * 2 : val; values[i] = val; } memset(m_kernel, 0, 16 * sizeof(QVector4D)); // Normalize the kernel for (int i = 0; i < kernelSize; i++) { const float val = values[i] / sum; m_kernel[i] = QVector4D(val, val, val, val); } *size = kernelSize; }
TEST_F(isingTest, IsingCorrect) { data[0].J1 = 4.f; data[0].J2 = 0.f; energies = new double* [1]; energies[0] = new double[3]; groundstate = new double* [1]; groundstate[0] = new double[hamil[0].sectorDim]; ConstructSparseMatrix(1, Bond, hamil, data, num_elem, 0); lanczos(1, num_elem, hamil, groundstate, energies, 200, 3, 1e-10); ASSERT_NEAR(energies[0][0], -16.0, 1e-3); ASSERT_NEAR(energies[0][1], -12.0, 1e-3); ASSERT_NEAR(energies[0][2], -8.0, 1e-3); data[0].J1 = 0.f; data[0].J2 = 2.f; ConstructSparseMatrix(1, Bond, hamil, data, num_elem, 0); lanczos(1, num_elem, hamil, groundstate, energies, 200, 3, 1e-10); ASSERT_NEAR(energies[0][0], -16.0, 1e-3); ASSERT_NEAR(energies[0][1], -12.0, 1e-3); ASSERT_NEAR(energies[0][2], -8.0, 1e-3); data[0].J1 = 4.f; ConstructSparseMatrix(1, Bond, hamil, data, num_elem, 0); lanczos(1, num_elem, hamil, groundstate, energies, 200, 3, 1e-10); ASSERT_NEAR(energies[0][0], -20.4044129223, 1e-3); ASSERT_NEAR(energies[0][1], -20.3063407044, 1e-3); ASSERT_NEAR(energies[0][2], -19.6204585904, 1e-3); delete [] groundstate[0]; delete [] energies; delete [] energies[0]; delete [] energies; }
TEST(ResampleEffectTest, DownscaleByTwoGetsCorrectPixelCenters) { const int size = 5; // This isn't a perfect dot, since the Lanczos filter has a slight // sharpening effect; the most important thing is that we have kept // the texel center right (everything is nicely symmetric). // The approximate magnitudes have been checked against ImageMagick. float expected_data[size * size] = { 0.0045, -0.0067, -0.0598, -0.0067, 0.0045, -0.0067, 0.0099, 0.0886, 0.0099, -0.0067, -0.0598, 0.0886, 0.7930, 0.0886, -0.0598, -0.0067, 0.0099, 0.0886, 0.0099, -0.0067, 0.0045, -0.0067, -0.0598, -0.0067, 0.0045, }; float data[size * size * 4], out_data[size * size]; for (int y = 0; y < size * 2; ++y) { for (int x = 0; x < size * 2; ++x) { float weight = lanczos((x - size + 0.5f) * 0.5f, 3.0f); weight *= lanczos((y - size + 0.5f) * 0.5f, 3.0f); data[y * (size * 2) + x] = weight; } } EffectChainTester tester(NULL, size, size, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); ImageFormat format; format.color_space = COLORSPACE_sRGB; format.gamma_curve = GAMMA_LINEAR; FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, size * 2, size * 2); input->set_pixel_data(data); tester.get_chain()->add_input(input); Effect *resample_effect = tester.get_chain()->add_effect(new ResampleEffect()); ASSERT_TRUE(resample_effect->set_int("width", size)); ASSERT_TRUE(resample_effect->set_int("height", size)); tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); expect_equal(expected_data, out_data, size, size); }
TEST(ResampleEffectTest, UpscaleByTwoGetsCorrectPixelCenters) { const int size = 5; float data[size * size] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, }; float expected_data[size * size * 4], out_data[size * size * 4]; for (int y = 0; y < size * 2; ++y) { for (int x = 0; x < size * 2; ++x) { float weight = lanczos((x - size + 0.5f) * 0.5f, 3.0f); weight *= lanczos((y - size + 0.5f) * 0.5f, 3.0f); expected_data[y * (size * 2) + x] = weight; } } EffectChainTester tester(NULL, size * 2, size * 2, FORMAT_GRAYSCALE, COLORSPACE_sRGB, GAMMA_LINEAR); ImageFormat format; format.color_space = COLORSPACE_sRGB; format.gamma_curve = GAMMA_LINEAR; FlatInput *input = new FlatInput(format, FORMAT_GRAYSCALE, GL_FLOAT, size, size); input->set_pixel_data(data); tester.get_chain()->add_input(input); Effect *resample_effect = tester.get_chain()->add_effect(new ResampleEffect()); ASSERT_TRUE(resample_effect->set_int("width", size * 2)); ASSERT_TRUE(resample_effect->set_int("height", size * 2)); tester.run(out_data, GL_RED, COLORSPACE_sRGB, GAMMA_LINEAR); expect_equal(expected_data, out_data, size * 2, size * 2); }
float eval(float x) const { switch (_type) { case Dirac: return 0.0f; case Box: return (x >= -0.5f && x <= 0.5f) ? 1.0f : 0.0f; case Tent: return 1.0f - std::abs(x); case Gaussian: { const float Alpha = 2.0f; return max(std::exp(-Alpha*x*x) - std::exp(-Alpha*4.0f), 0.0f); } case MitchellNetravali: return mitchellNetravali(std::abs(x)); case CatmullRom: return catmullRom(std::abs(x)); case Lanczos: return lanczos(std::abs(x)); default: return 0.0f; } }
TEST_F(heisenbergTest, HeisenbergCorrect) { //Diagonalize the Hamiltonian to see if we get correct answers energies = new double* [1]; energies[0] = new double[3]; groundstate = new double* [1]; groundstate[0] = new double[hamil[0].sectorDim]; lanczos(1, num_elem, hamil, groundstate, energies, 200, 3, 1e-10); ASSERT_NEAR(energies[0][0], -11.2282329996, 1e-3); ASSERT_NEAR(energies[0][1], -10.6499034854, 1e-3); ASSERT_NEAR(energies[0][2], -9.51748118382, 1e-3); delete [] groundstate[0]; delete [] groundstate; delete [] energies[0]; delete [] energies; }
static double lanczos3_kernel (double x) { return lanczos (x, 3); }
static double lanczos2_kernel (double x) { return lanczos (x, 2); }
bool pinpal(char* dataFile, char* initFile, char* outFile, char* paraFile, bool isInitFile, bool isInitSparse, bool isDataSparse, bool isParameter, rParameter::parameterType parameterType, FILE* Display) { rTimeStart(TOTAL_TIME_START1); rTimeStart(FILE_READ_START1); rComputeTime com; FILE* fpData = NULL; FILE* fpOut = NULL; if ((fpOut=fopen(outFile,"w"))==NULL) { rError("Cannot open out file " << outFile); } rParameter param; param.setDefaultParameter(parameterType); if (isParameter) { FILE* fpParameter = NULL; if ((fpParameter=fopen(paraFile,"r"))==NULL) { fprintf(Display,"Cannot open parameter file %s \n", paraFile); exit(0); } else { param.readFile(fpParameter); fclose(fpParameter); } } // param.display(Display); if ((fpData=fopen(dataFile,"r"))==NULL) { rError("Cannot open data file " << dataFile); } char titleAndComment[LengthOfBuffer]; int m; time_t ltime; time( <ime ); fprintf(fpOut,"SDPA start at %s",ctime(<ime)); rIO::read(fpData,fpOut,m,titleAndComment); fprintf(fpOut,"data is %s\n",dataFile); if (paraFile) { fprintf(fpOut,"parameter is %s\n",paraFile); } if (initFile) { fprintf(fpOut,"initial is %s\n",initFile); } fprintf(fpOut,"out is %s\n",outFile); int nBlock; rIO::read(fpData,nBlock); int* blockStruct = NULL; blockStruct = new int[nBlock]; if (blockStruct==NULL) { rError("Memory exhausted about blockStruct"); } rIO::read(fpData,nBlock,blockStruct); int nDim = 0; for (int l=0; l<nBlock; ++l) { nDim += abs(blockStruct[l]); } // rMessage("b has not been read yet , m = " << m); rVector b(m); rIO::read(fpData,b); // rMessage("b has been read"); rBlockSparseMatrix C; rBlockSparseMatrix* A = NULL; A = new rBlockSparseMatrix[m]; if (A==NULL) { rError("Memory exhausted about blockStruct"); } long position = ftell(fpData); // C,A must be accessed "twice". // count numbers of elements of C and A int* CNonZeroCount = NULL; CNonZeroCount = new int[nBlock]; if (CNonZeroCount==NULL) { rError("Memory exhausted about blockStruct"); } int* ANonZeroCount = NULL; ANonZeroCount = new int[nBlock*m]; if (ANonZeroCount==NULL) { rError("Memory exhausted about blockStruct"); } // initialize C and A rIO::read(fpData,m,nBlock,blockStruct, CNonZeroCount,ANonZeroCount,isDataSparse); // rMessage(" C and A count over"); C.initialize(nBlock,blockStruct); for (int l=0; l<nBlock; ++l) { int size = blockStruct[l]; if (size > 0) { C.ele[l].initialize(size,size,rSparseMatrix::SPARSE, CNonZeroCount[l]); } else { C.ele[l].initialize(-size,-size,rSparseMatrix::DIAGONAL, -size); } } for (int k=0; k<m; ++k) { A[k].initialize(nBlock,blockStruct); for (int l=0; l<nBlock; ++l) { int size = blockStruct[l]; if (size > 0) { A[k].ele[l].initialize(size,size,rSparseMatrix::SPARSE, ANonZeroCount[k*nBlock+l]); } else { A[k].ele[l].initialize(-size,-size,rSparseMatrix::DIAGONAL, -size); } } } delete[] CNonZeroCount; CNonZeroCount = NULL; delete[] ANonZeroCount; ANonZeroCount = NULL; // rMessage(" C and A initialize over"); rIO::read(fpData, C, A, m, nBlock, blockStruct, position, isDataSparse); // rMessage(" C and A have been read"); fclose(fpData); #if 0 fprintf(Display,"C = \n"); C.display(Display); for (int k=0; k<m; ++k) { fprintf(Display,"A[%d] = \n",k); A[k].display(Display); } #endif #if 0 // write C and A in SDPA sparse data format to file ofstream output; output.open("dumped.rsdpa.dat-s"); if (output.fail()) { rError("Cannot Open dumped.rsdpa.dat-s"); } output << m << endl; output << nBlock << endl; for (l = 0; l<nBlock ; ++l) { output << blockStruct[l] << " " ; } output << endl; for (k=0; k<m; ++k) { output << b.ele[k] << " "; } output << endl; int index=0; for (l=0; l<nBlock; ++l) { switch (C.ele[l].Sp_De_Di) { case rSparseMatrix::SPARSE: for (index = 0; index < C.ele[l].NonZeroCount; ++index) { int i = C.ele[l].row_index[index]; int j = C.ele[l].column_index[index]; double value = C.ele[l].sp_ele[index]; if (value!=0.0) { output << "0 " << l+1 << " " << i+1 << " " << j+1 << " " << -value << endl; } } break; case rSparseMatrix::DENSE: break; case rSparseMatrix::DIAGONAL: for (int index = 0; index < C.ele[l].nRow; ++index) { double value = C.ele[l].di_ele[index]; if (value!=0.0) { output << "0 " << l+1 << " " << index+1 << " " << index+1 << " " << -value << endl; } } break; } // end of switch }// end of 'for (int l)' for (k=0; k<m; ++k) { for (int l=0; l<nBlock; ++l) { switch (A[k].ele[l].Sp_De_Di) { case rSparseMatrix::SPARSE: for (index = 0; index < A[k].ele[l].NonZeroCount; ++index) { int i = A[k].ele[l].row_index[index]; int j = A[k].ele[l].column_index[index]; double value = A[k].ele[l].sp_ele[index]; if (value!=0.0) { output << k+1 << " " << l+1 << " " << i+1 << " " << j+1 << " " << value << endl; } } break; case rSparseMatrix::DENSE: break; case rSparseMatrix::DIAGONAL: for (int index = 0; index < A[k].ele[l].nRow; ++index) { double value = A[k].ele[l].di_ele[index]; if (value!=0.0) { output << k+1 << " " << l+1 << " " << index+1 << " " << index+1 << " " << value << endl; } } break; } // end of switch } // end of 'for (int l)' } // end of 'for (int k)' output.close(); #endif #if 0 rTimeStart(FILE_CHECK_START1); // check whether C,A are symmetric or not. int lin,iin,jin; if (C.sortSparseIndex(lin,iin,jin)==FAILURE) { fprintf(Display,"C is not symmetric, block %d," "(%d,%d) ", lin+1,iin+1,jin+1); exit(0); } for (int k=0; k<m; ++k) { if (A[k].sortSparseIndex(lin,iin,jin)==FAILURE) { fprintf(Display,"A[%d] is not symmetric, block %d," "(%d,%d) ", k+1,lin+1,iin+1,jin+1); exit(0); } } rTimeEnd(FILE_CHECK_END1); com.FileCheck += rTimeCal(FILE_CHECK_START1, FILE_CHECK_END1); #endif #if 1 rTimeStart(FILE_CHANGE_START1); // if possible , change C and A to Dense C.changeToDense(); for (int k=0; k<m; ++k) { A[k].changeToDense(); } rTimeEnd(FILE_CHANGE_END1); com.FileChange += rTimeCal(FILE_CHANGE_START1, FILE_CHANGE_END1); #endif // rMessage("C = "); // C.display(Display); // for (int k=0; k<m; ++k) { // rMessage("A["<<k<<"] = "); // A[k].display(Display); // } // the end of initialization of C and A // set initial solutions. rSolutions initPt; rSolutions currentPt; if (isInitFile) { initPt.initializeZero(m,nBlock,blockStruct,com); FILE* fpInit = NULL; if ((fpInit=fopen(initFile,"r"))==NULL) { rError("Cannot open init file " << initFile); } rIO::read(fpInit,initPt.xMat,initPt.yVec,initPt.zMat, nBlock, blockStruct, isInitSparse); fclose(fpInit); initPt.initializeResetup(m,nBlock,blockStruct,com); currentPt.copyFrom(initPt); } else { initPt.initialize(m,nBlock,blockStruct,param.lambdaStar,com); currentPt.initialize(m,nBlock,blockStruct,param.lambdaStar,com); } // rMessage("initial pt = "); // initPt.display(Display); // rMessage("current pt = "); // currentPt.display(Display); rTimeEnd(FILE_READ_END1); com.FileRead += rTimeCal(FILE_READ_START1, FILE_READ_END1); // ------------------------------------------------------------- // the end of file read // ------------------------------------------------------------- rResiduals initRes(m, nBlock, blockStruct, b, C, A, currentPt); rResiduals currentRes; currentRes.copyFrom(initRes); // rMessage("initial currentRes = "); // currentRes.display(Display); rNewton newton(m, nBlock, blockStruct); newton.computeFormula(m,A,0.0,KAPPA); rStepLength alpha(1.0,1.0,nBlock, blockStruct); rDirectionParameter beta(param.betaStar); rSwitch reduction(rSwitch::ON); rAverageComplementarity mu(param.lambdaStar); rLanczos lanczos(nBlock,blockStruct); // rMessage("init mu"); // mu.display(); if (isInitFile) { mu.initialize(nDim, initPt); } rRatioInitResCurrentRes theta(param, initRes); rSolveInfo solveInfo(nDim, b, C, A, initPt, mu.initial, param.omegaStar); rPhase phase(initRes, solveInfo, param, nDim); int pIteration = 0; rIO::printHeader(fpOut, Display); // ----------------------------------------------------- // Here is MAINLOOP // ----------------------------------------------------- rTimeStart(MAIN_LOOP_START1); // explicit maxIteration // param.maxIteration = 2; while (phase.updateCheck(currentRes, solveInfo, param) && pIteration < param.maxIteration) { // rMessage(" turn hajimari " << pIteration ); // Mehrotra's Predictor rTimeStart(MEHROTRA_PREDICTOR_START1); // set variable of Mehrotra reduction.MehrotraPredictor(phase); beta.MehrotraPredictor(phase, reduction, param); // rMessage("reduction = "); // reduction.display(); // rMessage("phase = "); // phase.display(); // rMessage("beta.predictor.value = " << beta.value); // rMessage("xMat = "); // currentPt.xMat.display(); // rMessage("zMat = "); // currentPt.zMat.display(); // rMessage(" mu = " << mu.current); bool isSuccessCholesky; isSuccessCholesky = newton.Mehrotra(rNewton::PREDICTOR, m, A, C, mu, beta, reduction, phase, currentPt, currentRes, com); if (isSuccessCholesky == false) { break; } // rMessage("newton predictor = "); // newton.display(); // rMessage("newton Dy predictor = "); // newton.DyVec.display(); // newton.bMat.display(); rTimeEnd(MEHROTRA_PREDICTOR_END1); com.Predictor += rTimeCal(MEHROTRA_PREDICTOR_START1, MEHROTRA_PREDICTOR_END1); rTimeStart(STEP_PRE_START1); alpha.MehrotraPredictor(b,C,A, currentPt, phase, newton, lanczos, com); // rMessage("xMat = "); // currentPt.xMat.display(); // rMessage("zMat = "); // currentPt.zMat.display(); // rMessage("alpha predictor = "); // alpha.display(); // phase.display(); // rMessage("newton predictor = "); // newton.display(); // rMessage("currentPt = "); // currentPt.display(); rTimeStart(STEP_PRE_END1); com.StepPredictor += rTimeCal(STEP_PRE_START1,STEP_PRE_END1); // rMessage("alphaStar = " << param.alphaStar); // Mehrotra's Corrector // rMessage(" Corrector "); rTimeStart(CORRECTOR_START1); beta.MehrotraCorrector(nDim,phase,alpha,currentPt, newton,mu,param); // rMessage("beta corrector = " << beta.value); newton.Mehrotra(rNewton::CORRECTOR,m,A,C,mu,beta,reduction, phase, currentPt, currentRes, com); // rMessage("currentPt = "); // currentPt.display(); // rMessage("newton corrector = "); // newton.display(); // rMessage("newton Dy corrector = "); // newton.DyVec.display(); rTimeEnd(CORRECTOR_END1); com.Corrector += rTimeCal(CORRECTOR_START1, CORRECTOR_END1); rTimeStart(CORRECTOR_STEP_START1); alpha.MehrotraCorrector(nDim, b, C, A, currentPt, phase, reduction, newton, mu, theta, lanczos, param, com); // rMessage("alpha corrector = "); // alpha.display(); rTimeEnd(CORRECTOR_STEP_END1); com.StepCorrector += rTimeCal(CORRECTOR_STEP_START1, CORRECTOR_STEP_END1); // the end of Corrector rIO::printOneIteration(pIteration, mu, theta, solveInfo, alpha, beta, currentRes, fpOut, Display); if (currentPt.update(alpha,newton,com)==false) { // if step length is too short, // we finish algorithm rMessage("cannot move"); pIteration++; break; } // rMessage("currentPt = "); // currentPt.display(); // rMessage("newton = "); // newton.display(); // rMessage("updated"); theta.update(reduction,alpha); mu.update(nDim,currentPt); currentRes.update(m,nBlock,blockStruct,b,C,A, initRes, theta, currentPt, phase, mu,com); theta.update_exact(initRes,currentRes); solveInfo.update(nDim, b, C, initPt, currentPt, currentRes, mu, theta, param); pIteration++; } // end of MAIN_LOOP rTimeEnd(MAIN_LOOP_END1); com.MainLoop = rTimeCal(MAIN_LOOP_START1, MAIN_LOOP_END1); currentPt.update_last(com); currentRes.compute(m,nBlock,blockStruct,b,C,A,currentPt,mu); rTimeEnd(TOTAL_TIME_END1); com.TotalTime = rTimeCal(TOTAL_TIME_START1, TOTAL_TIME_END1); #if REVERSE_PRIMAL_DUAL phase.reverse(); #endif #if 1 rIO::printLastInfo(pIteration, mu, theta, solveInfo, alpha, beta, currentRes, phase, currentPt, com.TotalTime, nDim, b, C, A, com, param, fpOut, Display); #endif // com.display(fpOut); if (blockStruct) { delete[] blockStruct; blockStruct = NULL; } C.~rBlockSparseMatrix(); for (int k=0; k<m; ++k) { A[k].~rBlockSparseMatrix(); } delete[] A; A = NULL; fprintf(Display, " main loop time = %.6f\n",com.MainLoop); fprintf(fpOut, " main loop time = %.6f\n",com.MainLoop); fprintf(Display, " total time = %.6f\n",com.TotalTime); fprintf(fpOut, " total time = %.6f\n",com.TotalTime); #if 0 fprintf(Display, "file check time = %.6f\n",com.FileCheck); fprintf(fpOut, " file check time = %.6f\n",com.FileCheck); fprintf(Display, "file change time = %.6f\n",com.FileChange); fprintf(fpOut, " file change time = %.6f\n",com.FileChange); #endif fprintf(Display, "file read time = %.6f\n",com.FileRead); fprintf(fpOut, " file read time = %.6f\n",com.FileRead); fclose(fpOut); return true; }
int main() { // Creation of a sample matrix: int N = 10; Matrix mat(N, N); int n = 1; for(int i=0;i<N;i++) for(int j=0;j<=i;j++) mat(i,j) = n++; std::cout << std::endl << "Printing matrix\n"; std::cout << "--------------------------------\n\n"; std::cout << mat << std::endl; typedef ietl::vectorspace<Vector> Vecspace; typedef boost::lagged_fibonacci607 Gen; Vecspace vec(N); Gen mygen; ietl::lanczos<Matrix,Vecspace> lanczos(mat,vec); int max_iter = 2*N; std::cout << "Computation of eigenvalues with fixed size of T-matrix\n\n"; std::cout << "-----------------------------------\n\n"; std::vector<double> eigen; std::vector<double> err; std::vector<int> multiplicity; ietl::fixed_lanczos_iteration<double> iter(max_iter); try { lanczos.calculate_eigenvalues(iter,mygen); eigen = lanczos.eigenvalues(); err = lanczos.errors(); multiplicity = lanczos.multiplicities(); } catch (std::runtime_error& e) { std::cout << e.what() << std::endl; } // Printing eigenvalues with error & multiplicities: std::cout << "# eigenvalue error multiplicity\n"; std::cout.precision(10); for (int i=0;i<eigen.size();++i) std::cout << i << "\t" << eigen[i] << "\t" << err[i] << "\t" << multiplicity[i] << "\n"; // call of eigenvectors function follows: std::cout << "\nEigen vectors computations for 3 lowest eigenvalues:\n\n"; std::vector<double>::iterator start = eigen.begin(); std::vector<double>::iterator end = eigen.begin()+3; std::vector<Vector> eigenvectors; // for storing the eigen vector. ietl::Info<double> info; // (m1, m2, ma, eigenvalue, residual, status). try { lanczos.eigenvectors(start, end, std::back_inserter(eigenvectors),info,mygen); } catch (std::runtime_error& e) { std::cout << e.what() << std::endl; } std::cout << "Printing eigen Vectors:" << std::endl << std::endl; for(std::vector<Vector>::iterator it = eigenvectors.begin(); it != eigenvectors.end(); it++){ std::copy((it)->begin(),(it)->end(),std::ostream_iterator<value_type>(std::cout,"\n")); std::cout << "\n\n"; } std::cout << " Information about the eigen vectors computations:\n\n"; for(int i = 0; i < info.size(); i++) { std::cout << " m1(" << i+1 << "): " << info.m1(i) << ", m2(" << i+1 << "): " << info.m2(i) << ", ma(" << i+1 << "): " << info.ma(i) << " eigenvalue(" << i+1 << "): " << info.eigenvalue(i) << " residual(" << i+1 << "): " << info.residual(i) << " error_info(" << i+1 << "): " << info.error_info(i) << std::endl << std::endl; } return 0; }
int main() { // Creation of an example matrix: int N = 10; Matrix mat(N, N); int n = 1; for(int i=0;i<N;i++) for(int j=0;j<=i;j++) mat(i,j) = mat(j, i) = n++; std::cout << "\n" << "Printing matrix\n"; std::cout << "--------------------------------\n\n"; std::cout << mat << std::endl; typedef ietl::vectorspace<Vector> Vecspace; typedef boost::lagged_fibonacci607 Gen; Vecspace vec(N); Gen mygen; ietl::lanczos<Matrix,Vecspace> lanczos(mat,vec); // Creation of an iteration object: int max_iter = 10*N; double rel_tol = 500*std::numeric_limits<double>::epsilon(); double abs_tol = std::pow(std::numeric_limits<double>::epsilon(),2./3); std::cout << "Computation of 2 lowest converged eigenvalues\n\n"; std::cout << "-----------------------------------\n\n"; int n_lowest_eigenval = 2; std::vector<double> eigen; std::vector<double> err; std::vector<int> multiplicity; ietl::lanczos_iteration_nlowest<double> iter(max_iter, n_lowest_eigenval, rel_tol, abs_tol); try{ lanczos.calculate_eigenvalues(iter,mygen); eigen = lanczos.eigenvalues(); err = lanczos.errors(); multiplicity = lanczos.multiplicities(); std::cout<<"number of iterations: "<<iter.iterations()<<"\n"; } catch (std::runtime_error& e) { std::cout << e.what() << "\n"; } // Printing eigenvalues with error & multiplicities: std::cout << "# eigenvalue error multiplicity\n"; std::cout.precision(10); for (int i=0;i<eigen.size();++i) std::cout << i << "\t" << eigen[i] << "\t" << err[i] << "\t" << multiplicity[i] << "\n"; // Another set of computations for more converged eigenvalues: std::cout << "\nMore converged eigenvalues\n\n"; std::cout << "-----------------------------------\n\n"; n_lowest_eigenval = 7; try { ietl::lanczos_iteration_nlowest<double> iter(max_iter, n_lowest_eigenval, rel_tol, abs_tol); lanczos.more_eigenvalues(iter); eigen = lanczos.eigenvalues(); err = lanczos.errors(); multiplicity = lanczos.multiplicities(); std::cout<<"number of iterations: "<<iter.iterations()<<"\n"; } catch (std::runtime_error& e) { std::cout << e.what() << "\n"; } std::cout << "# eigenvalue error multiplicity\n"; for (int i=0;i<eigen.size();++i) std::cout << i << "\t" << eigen[i] << "\t" << err[i] << "\t" << multiplicity[i] << "\n"; // call of eigenvectors function follows: std::cout << "\nEigen vectors computations for 3 lowest eigenvalues:\n\n"; std::vector<double>::iterator start = eigen.begin(); std::vector<double>::iterator end = eigen.begin()+3; std::vector<Vector> eigenvectors; // for storing the eigen vectors. ietl::Info<double> info; // (m1, m2, ma, eigenvalue, residualm, status). try { lanczos.eigenvectors(start,end,std::back_inserter(eigenvectors),info,mygen); } catch (std::runtime_error& e) { std::cout << e.what() << "\n"; } std::cout << "Printing eigenvectors:\n\n"; for(std::vector<Vector>::iterator it = eigenvectors.begin();it!=eigenvectors.end();it++){ std::cout << *it << "\n\n"; } std::cout << " Information about the eigenvector computations:\n\n"; for(int i = 0; i < info.size(); i++) { std::cout << " m1(" << i+1 << "): " << info.m1(i) << ", m2(" << i+1 << "): " << info.m2(i) << ", ma(" << i+1 << "): " << info.ma(i) << " eigenvalue(" << i+1 << "): " << info.eigenvalue(i) << " residual(" << i+1 << "): " << info.residual(i) << " error_info(" << i+1 << "): " << info.error_info(i) << "\n\n"; } return 0; }