changeset 11476:ff7e0776ba0f

logm.m: handle scalar and diagonal matrix arguments specially; call log_pade_pf only if m > 1
author John W. Eaton <jwe@octave.org>
date Mon, 10 Jan 2011 14:48:55 -0500
parents caf1fd72f587
children a02d00dd3d5f
files scripts/ChangeLog scripts/linear-algebra/logm.m
diffstat 2 files changed, 23 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog
+++ b/scripts/ChangeLog
@@ -1,3 +1,9 @@
+2011-01-10  John W. Eaton  <jwe@octave.org>
+
+	* linear-algebra/logm.m: Handle scalar and diagonal matrix
+	arguments specially.  Call logm_pade_pf only if m > 1.  New tests.
+	Special case suggested by Marco Caliari <marco.caliari@univr.it>.
+
 2011-01-10  John W. Eaton  <jwe@octave.org>
 
 	* general/private/__isequal__.m: Use builtin ("struct", ...) to
--- a/scripts/linear-algebra/logm.m
+++ b/scripts/linear-algebra/logm.m
@@ -49,6 +49,14 @@
     error ("logm: A must be a square matrix");
   endif
 
+  if (isscalar (A))
+    s = log (A);
+    return;
+  elseif (strfind (typeinfo (A), "diagonal matrix"))
+    s = diag (log (diag (A)));
+    return;
+  endif
+
   [u, s] = schur (A);
 
   if (isreal (A))
@@ -84,7 +92,11 @@
     warning ("logm: maximum number of square roots exceeded; results may still be accurate");
   endif
 
-  s = logm_pade_pf (s - eye (size (s)), m);
+  s = s - eye (size (s));
+
+  if (m > 1)
+    s = logm_pade_pf (s, m);
+  endif
 
   s = 2^k * u * s * u';
 
@@ -144,3 +156,7 @@
 %!error logm ();
 %!error logm (1, 2, 3);
 %!error <logm: A must be a square matrix> logm([1 0;0 1; 2 2]);
+
+%!assert (logm (10), log (10))
+%!assert (full (logm (eye (3))), logm (full (eye (3))))
+%!assert (full (logm (10*eye (3))), logm (full (10*eye (3))), 8*eps)