changeset 33515:786d203458bd

sqrtm.cc: Clarify code by using liboctave functions rather than hand-rolled for loop (bug #60797). * sqrtm.cc (sqrtm_utri_inplace): Use ".diag (). max ()(0)" to calculate the maximum value on the diagonal of the matrix rather than a custom for loop. Use "diagonal" boolean variable in for loop conditional rather than testing explicitly for "if (! diagonal)" and having to break out of loop.
author Rik <rik@octave.org>
date Thu, 02 May 2024 16:12:36 -0700
parents ec79204cfe38
children ffc7bb75ea3e
files libinterp/corefcn/sqrtm.cc
diffstat 1 files changed, 5 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/corefcn/sqrtm.cc	Thu May 02 15:28:32 2024 -0700
+++ b/libinterp/corefcn/sqrtm.cc	Thu May 02 16:12:36 2024 -0700
@@ -58,25 +58,21 @@
   const octave_idx_type n = m.rows ();
   real_matrix_type abs_m = m.abs ();
 
-  real_elt_type max_abs_diag = 0;
-  for (octave_idx_type i = 0; i < n; i++)
-    max_abs_diag = std::max (max_abs_diag, abs_m(i,i));
+  real_elt_type max_abs_diag = abs_m.diag ().max ()(0);
 
   const real_elt_type tol = n * max_abs_diag
                             * std::numeric_limits<real_elt_type>::epsilon ();
 
-   for (octave_idx_type j = 0; j < n; j++)
-     {
-       for (octave_idx_type i = j-1; i >= 0; i--)
-         {
+  for (octave_idx_type j = 0; j < n && diagonal; j++)
+    {
+      for (octave_idx_type i = j-1; i >= 0; i--)
+        {
           if (abs_m(i,j) > tol)
             {
               diagonal = false;
               break;
             }
         }
-      if (! diagonal)
-        break;
     }
 
   element_type *mp = m.rwdata ();