changeset 13208:0257eb36e15a

Simplify a computation in sprandsym
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Sat, 24 Sep 2011 13:06:24 -0500
parents 658aa9fd8665
children 9eff72139c05
files scripts/sparse/sprandsym.m
diffstat 1 files changed, 2 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/sparse/sprandsym.m	Sat Sep 24 10:38:19 2011 -0400
+++ b/scripts/sparse/sprandsym.m	Sat Sep 24 13:06:24 2011 -0500
@@ -65,13 +65,8 @@
   ondiag = randperm (n, r);
   offdiag = randperm (n*(n - 1)/2, m);
 
-  ## Do five Newton iterations to solve n(n - 1)/2 = offdiag (this is the
-  ## row index)
-  x = sqrt (offdiag);
-  for ii = 1:5
-    x = x - (x.^2 - x - 2*offdiag)./(2*x - 1);
-  endfor
-  i = floor(x);
+  ## solve with quadratic formula n^2 - n - 2*offdiag = 0 to get the row index
+  i = floor((1 + sqrt(1 + 8*offdiag))/2);
   i(i.^2 - i - 2*offdiag != 0) += 1;
 
   ## Column index