-
Notifications
You must be signed in to change notification settings - Fork 11
feat: Heisenberg matrix reduction and workspace refactor #84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9bfbba3
feat: Hessenberg Matrix Reduction and workspace refactoring
mahmudsudo 7728257
feat: Hessenberg Matrix Reduction and workspace refactoring
mahmudsudo ed45311
feat: Hessenberg Matrix Reduction and workspace refactoring
mahmudsudo e0dd8f3
Merge branch 'main' into heisenberg
mahmudsudo c3affb2
update workflows to use workspaces and simplify hessenberg with reduc…
dawnandrew100 7770293
Remove redundant just variables
dawnandrew100 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| [workspace] | ||
| members = [ | ||
| "spindalis", | ||
| "spindalis_core", | ||
| "spindalis_macros", | ||
| ] | ||
| resolver = "2" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| use crate::solvers::SolverError; | ||
| use crate::utils::Arr2D; | ||
|
|
||
| /// Computes the Hessenberg reduction of a square matrix A. | ||
| /// Returns (H, Q) such that H = Q^T * A * Q, where H is upper Hessenberg and Q is orthogonal. | ||
| pub fn hessenberg_reduction(matrix: &Arr2D<f64>) -> Result<(Arr2D<f64>, Arr2D<f64>), SolverError> { | ||
| if matrix.height != matrix.width { | ||
| return Err(SolverError::NonSquareMatrix); | ||
| } | ||
| let n = matrix.height; | ||
| if n <= 2 { | ||
| return Ok((matrix.clone(), Arr2D::identity(n))); | ||
| } | ||
|
|
||
| let mut h = matrix.clone(); | ||
| let mut q = Arr2D::identity(n); | ||
|
|
||
| for k in 0..n - 2 { | ||
| // x = h[k+1..n, k] | ||
| let mut x = Vec::with_capacity(n - (k + 1)); | ||
| for i in k + 1..n { | ||
| x.push(h[(i, k)]); | ||
| } | ||
|
|
||
| let norm_x = x.iter().map(|&val| val * val).sum::<f64>().sqrt(); | ||
| if norm_x == 0.0 { | ||
| continue; | ||
| } | ||
|
|
||
| // v = x + sign(x[0]) * ||x|| * e1 | ||
| let sign = if x[0] >= 0.0 { 1.0 } else { -1.0 }; | ||
| let mut v = x; | ||
| v[0] += sign * norm_x; | ||
|
|
||
| // normalize v | ||
| let norm_v = v.iter().map(|&val| val * val).sum::<f64>().sqrt(); | ||
| if norm_v == 0.0 { | ||
| continue; | ||
| } | ||
| for val in v.iter_mut() { | ||
| *val /= norm_v; | ||
| } | ||
|
|
||
| // Apply H_k = I - 2vv^T to A from the left: A = H_k * A | ||
| // This affects rows k+1..n | ||
| for col in k..n { | ||
| let mut dot = 0.0; | ||
| for i in 0..v.len() { | ||
| dot += v[i] * h[(k + 1 + i, col)]; | ||
| } | ||
| for i in 0..v.len() { | ||
| h[(k + 1 + i, col)] -= 2.0 * v[i] * dot; | ||
| } | ||
| } | ||
|
|
||
| // Apply H_k to A from the right: A = A * H_k | ||
| // This affects columns k+1..n | ||
| for row in 0..n { | ||
| let mut dot = 0.0; | ||
| for j in 0..v.len() { | ||
| dot += v[j] * h[(row, k + 1 + j)]; | ||
| } | ||
| for j in 0..v.len() { | ||
| h[(row, k + 1 + j)] -= 2.0 * v[j] * dot; | ||
| } | ||
| } | ||
|
|
||
| // Accumulate Q = Q * H_k | ||
| // This affects columns k+1..n of Q | ||
| for row in 0..n { | ||
| let mut dot = 0.0; | ||
| for j in 0..v.len() { | ||
| dot += v[j] * q[(row, k + 1 + j)]; | ||
| } | ||
| for j in 0..v.len() { | ||
| q[(row, k + 1 + j)] -= 2.0 * v[j] * dot; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Ok((h, q)) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::utils::Rounding; | ||
|
|
||
| #[test] | ||
| fn test_hessenberg_2x2() { | ||
| let mat = Arr2D::from(&[[1.0, 2.0], [3.0, 4.0]]); | ||
| let (h, q) = hessenberg_reduction(&mat).unwrap(); | ||
| assert_eq!(h.round_to_decimal(10), mat.round_to_decimal(10)); | ||
| assert_eq!( | ||
| q.round_to_decimal(10), | ||
| Arr2D::identity(2).round_to_decimal(10) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_hessenberg_3x3() { | ||
| // Simple 3x3 matrix | ||
| let mat = Arr2D::from(&[[1.0, 5.0, 7.0], [3.0, 0.0, 6.0], [4.0, 3.0, 1.0]]); | ||
| let (h, q) = hessenberg_reduction(&mat).unwrap(); | ||
|
|
||
| // Check if H is upper Hessenberg | ||
| assert!(h[(2, 0)].abs() < 1e-10); | ||
|
|
||
| // Check if Q is orthogonal: Q * Q^T = I | ||
| let qt = q.transpose(); | ||
| let i = q.dot(&qt).unwrap(); | ||
| assert_eq!( | ||
| i.round_to_decimal(10), | ||
| Arr2D::identity(3).round_to_decimal(10) | ||
| ); | ||
|
|
||
| // Check if A = Q * H * Q^T | ||
| let reconstructed = q.dot(&h).unwrap().dot(&qt).unwrap(); | ||
| assert_eq!(reconstructed.round_to_decimal(10), mat.round_to_decimal(10)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_hessenberg_4x4() { | ||
| let mat = Arr2D::from(&[ | ||
| [1.0, 2.0, 3.0, 4.0], | ||
| [2.0, 1.0, 2.0, 3.0], | ||
| [3.0, 2.0, 1.0, 2.0], | ||
| [4.0, 3.0, 2.0, 1.0], | ||
| ]); | ||
| let (h, q) = hessenberg_reduction(&mat).unwrap(); | ||
|
|
||
| // Check upper Hessenberg form (zeros below subdiagonal) | ||
| assert!(h[(2, 0)].abs() < 1e-10); | ||
| assert!(h[(3, 0)].abs() < 1e-10); | ||
| assert!(h[(3, 1)].abs() < 1e-10); | ||
|
|
||
| // Check orthogonality | ||
| let qt = q.transpose(); | ||
| let i = q.dot(&qt).unwrap(); | ||
| assert_eq!( | ||
| i.round_to_decimal(10), | ||
| Arr2D::identity(4).round_to_decimal(10) | ||
| ); | ||
|
|
||
| // Check A = Q H Q^T | ||
| let reconstructed = q.dot(&h).unwrap().dot(&qt).unwrap(); | ||
| assert_eq!(reconstructed.round_to_decimal(10), mat.round_to_decimal(10)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
|
|
||
| pub mod hessenberg; | ||
| pub use hessenberg::hessenberg_reduction; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.