]> gitweb.michael.orlitzky.com - numerical-analysis.git/commitdiff
Add comments to Linear.QR about non-convergence.
authorMichael Orlitzky <michael@orlitzky.com>
Tue, 4 Feb 2014 22:40:59 +0000 (17:40 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Tue, 4 Feb 2014 22:40:59 +0000 (17:40 -0500)
src/Linear/QR.hs

index b628c125e2b0f3b3fc44c449917e8045d72f2dd7..13782c4fa9073728bfdf5d4a0de8b4a2788a67e4 100644 (file)
@@ -171,6 +171,9 @@ qr matrix =
 --   iterated QR algorithm (see Golub and Van Loan, \"Matrix
 --   Computations\").
 --
 --   iterated QR algorithm (see Golub and Van Loan, \"Matrix
 --   Computations\").
 --
+--   Warning: this may not converge if there are repeated eigenvalues
+--   (in magnitude).
+--
 --   Examples:
 --
 --   >>> import Linear.Matrix ( Col2, Col3, Mat2, Mat3 )
 --   Examples:
 --
 --   >>> import Linear.Matrix ( Col2, Col3, Mat2, Mat3 )
@@ -198,12 +201,20 @@ eigenvalues :: forall m a. (Arity m, Algebraic.C a, Eq a)
              => Int
              -> Mat (S m) (S m) a
              -> Col (S m) a
              => Int
              -> Mat (S m) (S m) a
              -> Col (S m) a
-eigenvalues iterations matrix =
-  diagonal (ut_approximation iterations)
-  where
-    ut_approximation :: Int -> Mat (S m) (S m) a
-    ut_approximation 0 = matrix
-    ut_approximation k = rk*qk where (qk,rk) = qr (ut_approximation (k-1))
+eigenvalues iterations matrix
+  | iterations <  0 = error "negative iterations requested"
+  | iterations == 0 = diagonal matrix
+  | otherwise =
+      diagonal (ut_approximation (iterations - 1))
+      where
+        ut_approximation :: Int -> Mat (S m) (S m) a
+        ut_approximation 0 = matrix
+        ut_approximation k = ut_next
+          where
+            ut_prev = ut_approximation (k-1)
+            (qk,rk) = qr ut_prev
+            ut_next = rk*qk
+
 
 
 -- | Compute the eigenvalues and eigenvectors of a symmetric matrix
 
 
 -- | Compute the eigenvalues and eigenvectors of a symmetric matrix
@@ -214,6 +225,9 @@ eigenvalues iterations matrix =
 --   references see Goluv and Van Loan, \"Matrix Computations\", or
 --   \"Calculation of Gauss Quadrature Rules\" by Golub and Welsch.
 --
 --   references see Goluv and Van Loan, \"Matrix Computations\", or
 --   \"Calculation of Gauss Quadrature Rules\" by Golub and Welsch.
 --
+--   Warning: this may not converge if there are repeated eigenvalues
+--   (in magnitude).
+--
 --   Examples:
 --
 --   >>> import Linear.Matrix ( Col2, Col3, Mat2, Mat3 )
 --   Examples:
 --
 --   >>> import Linear.Matrix ( Col2, Col3, Mat2, Mat3 )
@@ -268,8 +282,7 @@ eigenvectors_symmetric iterations matrix
                     where
                       (t_prev, p_prev) = tp_pair (k-1)
                       (qk,rk) = qr t_prev
                     where
                       (t_prev, p_prev) = tp_pair (k-1)
                       (qk,rk) = qr t_prev
-                      tk = rk*qk
                       pk = p_prev*qk
                       pk = p_prev*qk
-
+                      tk = rk*qk
 
         (values, vectors) = (first diagonal) (tp_pair iterations)
 
         (values, vectors) = (first diagonal) (tp_pair iterations)