{"id":13419184,"url":"https://github.com/yixuan/spectra","last_synced_at":"2025-04-13T06:17:43.868Z","repository":{"id":2017641,"uuid":"42243348","full_name":"yixuan/spectra","owner":"yixuan","description":"A header-only C++ library for large scale eigenvalue problems","archived":false,"fork":false,"pushed_at":"2025-01-02T06:42:36.000Z","size":34928,"stargazers_count":779,"open_issues_count":46,"forks_count":132,"subscribers_count":36,"default_branch":"master","last_synced_at":"2025-04-13T06:17:29.198Z","etag":null,"topics":["arpack","eigenvalues","header-only","linear-algebra","spectra"],"latest_commit_sha":null,"homepage":"https://spectralib.org","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yixuan.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.md","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-09-10T12:34:38.000Z","updated_at":"2025-04-08T11:44:30.000Z","dependencies_parsed_at":"2023-12-15T20:07:13.107Z","dependency_job_id":"edb1158c-577b-4628-a2d1-c85268c7a857","html_url":"https://github.com/yixuan/spectra","commit_stats":{"total_commits":662,"total_committers":19,"mean_commits":34.8421052631579,"dds":"0.25075528700906347","last_synced_commit":"931af5c474e9ff035585de2cd8c05859a56ad1e7"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yixuan%2Fspectra","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yixuan%2Fspectra/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yixuan%2Fspectra/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yixuan%2Fspectra/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yixuan","download_url":"https://codeload.github.com/yixuan/spectra/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248670437,"owners_count":21142905,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arpack","eigenvalues","header-only","linear-algebra","spectra"],"created_at":"2024-07-30T22:01:12.467Z","updated_at":"2025-04-13T06:17:43.838Z","avatar_url":"https://github.com/yixuan.png","language":"C++","readme":"# \u003ca href=\"https://spectralib.org\"\u003e\u003cimg src=\"https://spectralib.org/img/logo.png\" width=\"200px\" /\u003e\u003c/a\u003e\n\n![Basic CI](https://github.com/yixuan/spectra/workflows/Basic%20CI/badge.svg) [![codecov](https://codecov.io/gh/yixuan/spectra/branch/master/graph/badge.svg)](https://codecov.io/gh/yixuan/spectra)\n\n\u003e **NOTE**: Spectra 1.0.0 was released in 2021-07-01, with a lot of\n\u003e API-breaking changes. Please see the [migration guide](MIGRATION.md)\n\u003e for a smooth transition to the new version.\n\n\u003e **NOTE**: If you are interested in the future development of Spectra, please join\n\u003e [this thread](https://github.com/yixuan/spectra/issues/92) to share your comments and suggestions.\n\n[**Spectra**](https://spectralib.org) stands for **Sp**arse **E**igenvalue **C**omputation **T**oolkit\nas a **R**edesigned **A**RPACK. It is a C++ library for large scale eigenvalue\nproblems, built on top of [Eigen](http://eigen.tuxfamily.org),\nan open source linear algebra library.\n\n**Spectra** is implemented as a header-only C++ library, whose only dependency,\n**Eigen**, is also header-only. Hence **Spectra** can be easily embedded in\nC++ projects that require calculating eigenvalues of large matrices.\n\n## Relation to ARPACK\n\n[ARPACK](http://www.caam.rice.edu/software/ARPACK/) is a software package written in\nFORTRAN for solving large scale eigenvalue problems. The development of\n**Spectra** is much inspired by ARPACK, and as the full name indicates,\n**Spectra** is a redesign of the ARPACK library using the C++ language.\n\nIn fact, **Spectra** is based on the algorithm described in the\n[ARPACK Users' Guide](http://www.caam.rice.edu/software/ARPACK/UG/ug.html),\nthe implicitly restarted Arnoldi/Lanczos method. However,\nit does not use the ARPACK code, and it is **NOT** a clone of ARPACK for C++.\nIn short, **Spectra** implements the major algorithms in ARPACK,\nbut **Spectra** provides a completely different interface, and it does not\ndepend on ARPACK.\n\n## Common Usage\n\n**Spectra** is designed to calculate a specified number (`k`) of eigenvalues\nof a large square matrix (`A`). Usually `k` is much smaller than the size of the matrix\n(`n`), so that only a few eigenvalues and eigenvectors are computed, which\nin general is more efficient than calculating the whole spectral decomposition.\nUsers can choose eigenvalue selection rules to pick the eigenvalues of interest,\nsuch as the largest `k` eigenvalues, or eigenvalues with largest real parts, etc.\n\nTo use the eigen solvers in this library, the user does not need to directly\nprovide the whole matrix, but instead, the algorithm only requires certain operations\ndefined on `A`. In the basic setting, it is simply the matrix-vector\nmultiplication. Therefore, if the matrix-vector product `A * x` can be computed\nefficiently, which is the case when `A` is sparse, **Spectra**\nwill be very powerful for large scale eigenvalue problems.\n\nThere are two major steps to use the **Spectra** library:\n\n1. Define a class that implements a certain matrix operation, for example the\nmatrix-vector multiplication `y = A * x` or the shift-solve operation\n`y = inv(A - σ * I) * x`. **Spectra** has defined a number of\nhelper classes to quickly create such operations from a matrix object.\nSee the documentation of\n[DenseGenMatProd](https://spectralib.org/doc/classSpectra_1_1DenseGenMatProd.html),\n[DenseSymShiftSolve](https://spectralib.org/doc/classSpectra_1_1DenseSymShiftSolve.html), etc.\n2. Create an object of one of the eigen solver classes, for example\n[SymEigsSolver](https://spectralib.org/doc/classSpectra_1_1SymEigsSolver.html)\nfor symmetric matrices, and\n[GenEigsSolver](https://spectralib.org/doc/classSpectra_1_1GenEigsSolver.html)\nfor general matrices. Member functions\nof this object can then be called to conduct the computation and retrieve the\neigenvalues and/or eigenvectors.\n\nBelow is a list of the available eigen solvers in **Spectra**:\n\n- [SymEigsSolver](https://spectralib.org/doc/classSpectra_1_1SymEigsSolver.html):\nFor real symmetric matrices\n- [GenEigsSolver](https://spectralib.org/doc/classSpectra_1_1GenEigsSolver.html):\nFor general real matrices\n- [SymEigsShiftSolver](https://spectralib.org/doc/classSpectra_1_1SymEigsShiftSolver.html):\nFor real symmetric matrices using the shift-and-invert mode\n- [GenEigsRealShiftSolver](https://spectralib.org/doc/classSpectra_1_1GenEigsRealShiftSolver.html):\nFor general real matrices using the shift-and-invert mode,\nwith a real-valued shift\n- [GenEigsComplexShiftSolver](https://spectralib.org/doc/classSpectra_1_1GenEigsComplexShiftSolver.html):\nFor general real matrices using the shift-and-invert mode,\nwith a complex-valued shift\n- [SymGEigsSolver](https://spectralib.org/doc/classSpectra_1_1SymGEigsSolver.html):\nFor generalized eigen solver with real symmetric matrices\n- [SymGEigsShiftSolver](https://spectralib.org/doc/classSpectra_1_1SymGEigsShiftSolver.html):\nFor generalized eigen solver with real symmetric matrices, using the shift-and-invert mode\n- [DavidsonSymEigsSolver](https://spectralib.org/doc/classSpectra_1_1DavidsonSymEigsSolver.html):\nJacobi-Davidson eigen solver for real symmetric matrices, with the DPR correction method\n- [HermEigsSolver](https://spectralib.org/doc/classSpectra_1_1HermEigsSolver.html):\nFor complex Hermitian matrices\n\n## Examples\n\nBelow is an example that demonstrates the use of the eigen solver for symmetric\nmatrices.\n\n```cpp\n#include \u003cEigen/Core\u003e\n#include \u003cSpectra/SymEigsSolver.h\u003e\n// \u003cSpectra/MatOp/DenseSymMatProd.h\u003e is implicitly included\n#include \u003ciostream\u003e\n\nusing namespace Spectra;\n\nint main()\n{\n    // We are going to calculate the eigenvalues of M\n    Eigen::MatrixXd A = Eigen::MatrixXd::Random(10, 10);\n    Eigen::MatrixXd M = A + A.transpose();\n\n    // Construct matrix operation object using the wrapper class DenseSymMatProd\n    DenseSymMatProd\u003cdouble\u003e op(M);\n\n    // Construct eigen solver object, requesting the largest three eigenvalues\n    SymEigsSolver\u003cDenseSymMatProd\u003cdouble\u003e\u003e eigs(op, 3, 6);\n\n    // Initialize and compute\n    eigs.init();\n    int nconv = eigs.compute(SortRule::LargestAlge);\n\n    // Retrieve results\n    Eigen::VectorXd evalues;\n    if(eigs.info() == CompInfo::Successful)\n        evalues = eigs.eigenvalues();\n\n    std::cout \u003c\u003c \"Eigenvalues found:\\n\" \u003c\u003c evalues \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\nSparse matrix is supported via classes such as `SparseGenMatProd`\nand `SparseSymMatProd`.\n\n```cpp\n#include \u003cEigen/Core\u003e\n#include \u003cEigen/SparseCore\u003e\n#include \u003cSpectra/GenEigsSolver.h\u003e\n#include \u003cSpectra/MatOp/SparseGenMatProd.h\u003e\n#include \u003ciostream\u003e\n\nusing namespace Spectra;\n\nint main()\n{\n    // A band matrix with 1 on the main diagonal, 2 on the below-main subdiagonal,\n    // and 3 on the above-main subdiagonal\n    const int n = 10;\n    Eigen::SparseMatrix\u003cdouble\u003e M(n, n);\n    M.reserve(Eigen::VectorXi::Constant(n, 3));\n    for(int i = 0; i \u003c n; i++)\n    {\n        M.insert(i, i) = 1.0;\n        if(i \u003e 0)\n            M.insert(i - 1, i) = 3.0;\n        if(i \u003c n - 1)\n            M.insert(i + 1, i) = 2.0;\n    }\n\n    // Construct matrix operation object using the wrapper class SparseGenMatProd\n    SparseGenMatProd\u003cdouble\u003e op(M);\n\n    // Construct eigen solver object, requesting the largest three eigenvalues\n    GenEigsSolver\u003cSparseGenMatProd\u003cdouble\u003e\u003e eigs(op, 3, 6);\n\n    // Initialize and compute\n    eigs.init();\n    int nconv = eigs.compute(SortRule::LargestMagn);\n\n    // Retrieve results\n    Eigen::VectorXcd evalues;\n    if(eigs.info() == CompInfo::Successful)\n        evalues = eigs.eigenvalues();\n\n    std::cout \u003c\u003c \"Eigenvalues found:\\n\" \u003c\u003c evalues \u003c\u003c std::endl;\n\n    return 0;\n}\n```\n\nAnd here is an example for user-supplied matrix operation class.\n\n```cpp\n#include \u003cEigen/Core\u003e\n#include \u003cSpectra/SymEigsSolver.h\u003e\n#include \u003ciostream\u003e\n\nusing namespace Spectra;\n\n// M = diag(1, 2, ..., 10)\nclass MyDiagonalTen\n{\npublic:\n    using Scalar = double;  // A typedef named \"Scalar\" is required\n    int rows() const { return 10; }\n    int cols() const { return 10; }\n    // y_out = M * x_in\n    void perform_op(const double *x_in, double *y_out) const\n    {\n        for(int i = 0; i \u003c rows(); i++)\n        {\n            y_out[i] = x_in[i] * (i + 1);\n        }\n    }\n};\n\nint main()\n{\n    MyDiagonalTen op;\n    SymEigsSolver\u003cMyDiagonalTen\u003e eigs(op, 3, 6);\n    eigs.init();\n    eigs.compute(SortRule::LargestAlge);\n    if(eigs.info() == CompInfo::Successful)\n    {\n        Eigen::VectorXd evalues = eigs.eigenvalues();\n        std::cout \u003c\u003c \"Eigenvalues found:\\n\" \u003c\u003c evalues \u003c\u003c std::endl;\n    }\n\n    return 0;\n}\n```\n\n## Shift-and-invert Mode\n\nWhen it is needed to find eigenvalues that are closest to a number `σ`,\nfor example to find the smallest eigenvalues of a positive definite matrix\n(in which case `σ = 0`), it is advised to use the shift-and-invert mode\nof eigen solvers.\n\nIn the shift-and-invert mode, selection rules are applied to `1/(λ - σ)`\nrather than `λ`, where `λ` are eigenvalues of `A`.\nTo use this mode, users need to define the shift-solve matrix operation. See\nthe documentation of\n[SymEigsShiftSolver](https://spectralib.org/doc/classSpectra_1_1SymEigsShiftSolver.html)\nfor details.\n\n## Documentation\n\nThe [API reference](https://spectralib.org/doc/) page contains the documentation\nof **Spectra** generated by [Doxygen](http://www.doxygen.org/),\nincluding all the background knowledge, example code and class APIs.\n\nMore information can be found in the project page [https://spectralib.org](https://spectralib.org).\n\n## Installation\n\nAn optional CMake installation is supported, if you have CMake with at least v3.10 installed. You can install the headers using the following commands:\n\n```bash\nmkdir build \u0026\u0026 cd build\ncmake .. -DCMAKE_INSTALL_PREFIX='intended installation directory' -DCMAKE_PREFIX_PATH='path where the installation of Eigen3 can be found' -DBUILD_TESTS=TRUE\nmake all \u0026\u0026 make test \u0026\u0026 make install\n```\n\nBy installing **Spectra** in this way, you also create a CMake target `Spectra::Spectra` that can be used in subsequent build procedures for other programs.\n\n## License\n\n**Spectra** is an open source project licensed under\n[MPL2](https://www.mozilla.org/MPL/2.0/), the same license used by **Eigen**.\n","funding_links":[],"categories":["TODO scan for Android support in followings","Math","C++","Mathematics"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyixuan%2Fspectra","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyixuan%2Fspectra","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyixuan%2Fspectra/lists"}