diff scripts/sparse/cgs.m @ 9279:1673a0dc019f

fix & improve preconditioning strategy in cgs & bicgstab
author Jaroslav Hajek <highegg@gmail.com>
date Thu, 28 May 2009 07:37:13 +0200
parents 2b35cb600d50
children be55736a0783
line wrap: on
line diff
--- a/scripts/sparse/cgs.m
+++ b/scripts/sparse/cgs.m
@@ -65,19 +65,19 @@
   endif
 
   ## Left preconditioner.
-  precon = [];
   if (nargin == 5)
-    precon = M1;
+    if (isnumeric (M1))
+      precon = @(x) M1 \ x;
+    endif
   elseif (nargin > 5)
-    if (isparse(M1) && issparse(M2))
-      precon = @(x) M1 * (M2 * x);
+    if (issparse (M1) && issparse (M2))
+      precon = @(x) M2 \ (M1 \ x);
     else
-      precon = M1 * M2;
+      M = M1*M2;
+      precon = @(x) M \ x;
     endif
-  endif
-
-  if (nargin > 4 && isnumeric(precon) )
-    precon = inv(precon);
+  else
+    precon = @(x) x;
   endif
 
   ## Specifies initial estimate x0.
@@ -96,15 +96,7 @@
   flag = 1;
   for iter = 1 : maxit
 
-    if (nargin > 4 && isnumeric (precon))
-      z = precon * res;
-    elseif (nargin > 4)
-      ## Our preconditioner is a function.
-      z = feval (precon, res);
-    else
-      ## We don't use preconditioning.
-      z = res;
-    endif
+    z = precon (res);
 
     ## Cache.
     ro_old = ro;