From 8c4177e546093431cb2341baa70ad13633f14513 Mon Sep 17 00:00:00 2001 From: TianKai Ma Date: Wed, 3 Jan 2024 17:08:12 +0800 Subject: [PATCH] fix: BidiagonalizationMethod --- lib/HouseholderMethod/Bidiagonalization.cpp | 5 ++--- main.cpp | 12 ++++-------- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/lib/HouseholderMethod/Bidiagonalization.cpp b/lib/HouseholderMethod/Bidiagonalization.cpp index 4973d78..5b1c6a6 100644 --- a/lib/HouseholderMethod/Bidiagonalization.cpp +++ b/lib/HouseholderMethod/Bidiagonalization.cpp @@ -6,8 +6,7 @@ Bidiagonalization_Result BidiagonalizationMethod(const Matrix &matrix) { #ifdef DEBUG - // TODO: I'm limiting rows > cols + 1 for now, should have been rows >= cols - if (matrix.rows <= matrix.cols + 1) { + if (matrix.rows < matrix.cols) { throw std::invalid_argument("BidiagonalizationMethod requires rows >= cols"); } #endif @@ -36,7 +35,7 @@ Bidiagonalization_Result BidiagonalizationMethod(const Matrix &matrix) { auto Q = Matrix::identity(n); auto Q_sub = Matrix::identity(n - k - 1) - product(v, v) * beta; Q.set(k + 1, n, k + 1, n, Q_sub); - V = Q * V; + V = V * Q; auto A_sub = B.sub_matrix(k, m, k + 1, n); A_sub = A_sub - A_sub * product(v, v) * beta; diff --git a/main.cpp b/main.cpp index a08f317..5aadfa4 100644 --- a/main.cpp +++ b/main.cpp @@ -2,16 +2,12 @@ // homework 8 (SVD): int main() { - auto A = Matrix(8, 5, 1, 10); - A = BidiagonalizationMethod(A).B; - A.matrix[1][0] = 5; - - A.print(); - - auto r = WilkinsonShiftIteration(A); + auto A = Matrix(8, 8, 1, 10); + auto r = BidiagonalizationMethod(A); r.B.print(); - (r.P * A * r.Q).print(); + + (r.U * A * r.V).print(); return 0; } \ No newline at end of file