Skip to content

Commit

Permalink
Davidson method requires a SpectrumTarget enum arg #6
Browse files Browse the repository at this point in the history
  • Loading branch information
felipeZ committed Jan 17, 2020
1 parent fa4d8c3 commit b1bb6cc
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 37 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## [Unreleased]

## [0.1.4]

### Changed

* The Davidson method requires a **SpectrumTarget** `enum` argument.

## [0.1.3] 17-01-2020

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "eigenvalues"
version = "0.1.3"
version = "0.1.4"
authors = ["felipe <[email protected]>"]
description = "algorithms to compute eigenvalue/eigenvectors of symmetric matrices"
readme = "README.md"
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

Eigenvalue Decomposition
========================
This package contains some implementations for computing the eigenvalues/eigenvectors of a symmetric matrix,
implemented in [Rust](https://www.rust-lang.org/).
This package contains some iterative algorithms for computing the eigenvalues/eigenvectors of a symmetric matrix, implemented in [Rust](https://www.rust-lang.org/).

Available Algorithms:
* **Davidson** using either Diagonal-Preconditioned-Residue (**DPR**) or Generalized Jacobi Davidson (**GJD**).
* **Davidson** using either Diagonal-Preconditioned-Residue (**DPR**) or Generalized Jacobi Davidson (**GJD**). See [Davidson Diagonalization Method](https://www.semanticscholar.org/paper/DAVIDSON-DIAGONALIZATION-METHOD-AND-ITS-APPLICATION-Liao/5811eaf768d1a006f505dfe24f329874a679ba59)


### Note:
Expand Down
8 changes: 5 additions & 3 deletions src/algorithms/davidson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ impl Config {
/// Choose sensible default values for the davidson algorithm, where:
/// * `nvalues` - Number of eigenvalue/eigenvector pairs to compute
/// * `dim` - dimension of the matrix to diagonalize
fn new(nvalues: usize, dim: usize, method: &str, target: Option<SpectrumTarget>) -> Self {
/// * `method` - Either DPR or GJD
/// * `target` Lowest, highest or somewhere in the middle portion of the spectrum
fn new(nvalues: usize, dim: usize, method: &str, target: SpectrumTarget) -> Self {
let max_search_space = if nvalues * 10 < dim {
nvalues * 10
} else {
Expand All @@ -57,7 +59,7 @@ impl Config {
}
Config {
method: String::from(method),
spectrum_target: target.unwrap_or_else(|| SpectrumTarget::Lowest),
spectrum_target: target,
tolerance: 1e-6,
max_iters: 100,
max_search_space: max_search_space,
Expand All @@ -81,7 +83,7 @@ impl EigenDavidson {
h: M,
nvalues: usize,
method: &str,
spectrum_target: Option<SpectrumTarget>,
spectrum_target: SpectrumTarget,
) -> Result<Self, &'static str> {
// Initial configuration
let conf = Config::new(nvalues, h.rows(), method, spectrum_target);
Expand Down
27 changes: 6 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,26 @@ extern crate nalgebra as na;
// Use the Davidson method
use eigenvalues::davidson::EigenDavidson;
use eigenvalues::SpectrumTarget;
use na::{DMatrix, DVector};
// Generate random symmetric matrix
let brr = eigenvalues::utils::generate_diagonal_dominant(10, 0.005);
// Compute the first 2 eigenvalues/eigenvectors using the DPR method
let eig = EigenDavidson::new (brr, 2, "DPR", None).unwrap();
// Compute the first 2 lowest eigenvalues/eigenvectors using the DPR method
let eig = EigenDavidson::new (brr.clone(), 2, "DPR", SpectrumTarget::Lowest).unwrap();
println!("eigenvalues:{}", eig.eigenvalues);
println!("eigenvectors:{}", eig.eigenvectors);
```
## Highest eigenvalues
By default the library compute the Lowest eigenvalues of the
spectrum. It is also, possible to compute the highest eigenvalues like:
```
extern crate eigenvalues;
extern crate nalgebra as na;
// Use the Davidson method
use eigenvalues::SpectrumTarget;
use eigenvalues::davidson::EigenDavidson;
use na::{DMatrix, DVector};
// Generate random symmetric matrix
let brr = eigenvalues::utils::generate_diagonal_dominant(10, 0.005);
// Compute the first 2 eigenvalues/eigenvectors using the DPR method
let eig = EigenDavidson::new (brr, 2, "DPR", Some(SpectrumTarget::Highest)).unwrap();
// Compute the first 2 highest eigenvalues/eigenvectors using the GJD method
let eig = EigenDavidson::new (brr, 2, "GJD", SpectrumTarget::Highest).unwrap();
println!("eigenvalues:{}", eig.eigenvalues);
println!("eigenvectors:{}", eig.eigenvectors);
```
*/

pub mod algorithms;
pub mod matrix_operations;
pub mod utils;
pub use algorithms::{SpectrumTarget, davidson};
pub use algorithms::{davidson, SpectrumTarget};
9 changes: 5 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ extern crate eigenvalues;
extern crate nalgebra as na;

use eigenvalues::davidson::EigenDavidson;
use eigenvalues::SpectrumTarget;

fn main() {
let brr = eigenvalues::utils::generate_diagonal_dominant(10, 0.05);
let eig = EigenDavidson::new (brr, 2, "GJD", None).unwrap();
println !("eigenvalues:{}", eig.eigenvalues);
println !("eigenvectors:{}", eig.eigenvectors);
let brr = eigenvalues::utils::generate_diagonal_dominant(10, 0.05);
let eig = EigenDavidson::new(brr, 2, "GJD", SpectrumTarget::Lowest).unwrap();
println!("eigenvalues:{}", eig.eigenvalues);
println!("eigenvectors:{}", eig.eigenvectors);
}
13 changes: 8 additions & 5 deletions tests/tests_davidson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ use approx::relative_eq;
use eigenvalues::algorithms::davidson::EigenDavidson;
use eigenvalues::utils::generate_diagonal_dominant;
use eigenvalues::utils::sort_eigenpairs;
use eigenvalues::SpectrumTarget;

#[test]
fn davidson_eigenvalues() {
fn test_davidson_lowest() {
let arr = generate_diagonal_dominant(10, 0.005);
let eig = sort_eigenpairs(na::linalg::SymmetricEigen::new(arr.clone()), true);

let dav_eig = EigenDavidson::new(arr.clone(), 2, "DPR", None).unwrap();
let spectrum_target = SpectrumTarget::Lowest;

let dav_eig = EigenDavidson::new(arr.clone(), 2, "DPR", spectrum_target.clone()).unwrap();
test_eigenpairs(&eig, dav_eig, 2);
let dav_eig = EigenDavidson::new(arr.clone(), 2, "GJD", None).unwrap();
let dav_eig = EigenDavidson::new(arr.clone(), 2, "GJD", spectrum_target).unwrap();
test_eigenpairs(&eig, dav_eig, 2);
}

Expand All @@ -25,7 +28,7 @@ fn test_davidson_unsorted() {
let vs = na::DVector::<f64>::from_vec(vec![3.0, 2.0, 4.0, 1.0, 5.0, 6.0, 7.0, 8.0]);
arr.set_diagonal(&vs);
let eig = sort_eigenpairs(na::linalg::SymmetricEigen::new(arr.clone()), true);
let dav_eig = EigenDavidson::new(arr, 1, "DPR", None).unwrap();
let dav_eig = EigenDavidson::new(arr, 1, "DPR", SpectrumTarget::Lowest).unwrap();
test_eigenpairs(&eig, dav_eig, 1);
}

Expand All @@ -35,7 +38,7 @@ fn test_davidson_highest() {
let arr = generate_diagonal_dominant(10, 0.005);
let eig = sort_eigenpairs(na::linalg::SymmetricEigen::new(arr.clone()), false);

let target = Some(eigenvalues::SpectrumTarget::Highest);
let target = SpectrumTarget::Highest;

let dav_eig = EigenDavidson::new(arr.clone(), 2, "DPR", target.clone()).unwrap();
test_eigenpairs(&eig, dav_eig, 2);
Expand Down

0 comments on commit b1bb6cc

Please sign in to comment.