changeset 13747:e8564e8b0043

Restore random number state after %!demos or %!tests * griddata3.m, onenormest.m, trimesh.m, triplot.m, trisurf.m, svds.m: Restore random number state after %!demos or %!tests.
author Rik <octave@nomad.inbox5.com>
date Tue, 25 Oct 2011 10:56:02 -0700
parents 7ff0bdc3dc4c
children 77857d6fe074
files scripts/geometry/griddata3.m scripts/linear-algebra/onenormest.m scripts/plot/trimesh.m scripts/plot/triplot.m scripts/plot/trisurf.m scripts/sparse/svds.m
diffstat 6 files changed, 62 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/geometry/griddata3.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/geometry/griddata3.m	Tue Oct 25 10:56:02 2011 -0700
@@ -53,28 +53,32 @@
 
   vi = griddatan ([x(:), y(:), z(:)], v(:), [xi(:), yi(:), zi(:)], varargin{:});
   vi = reshape (vi, size (xi));
+
 endfunction
 
+
 %!testif HAVE_QHULL
-%! rand('state', 0);
-%! x = 2 * rand(1000, 1) - 1;
-%! y = 2 * rand(1000, 1) - 1;
-%! z = 2 * rand(1000, 1) - 1;
+%! old_state = rand ("state");
+%! restore_state = onCleanup (@() rand ("state", old_state));
+%! rand ("state", 0);
+%! x = 2 * rand (1000, 1) - 1;
+%! y = 2 * rand (1000, 1) - 1;
+%! z = 2 * rand (1000, 1) - 1;
 %! v = x.^2 + y.^2 + z.^2;
 %! [xi, yi, zi] = meshgrid (-0.8:0.2:0.8);
-%! ##vi = reshape (griddatan([x(:), y(:), z(:)], v, [xi(:), yi(:), zi(:)], 'linear'), size (xi));
 %! vi = griddata3 (x, y, z, v, xi, yi, zi, 'linear');
 %! vv = vi - xi.^2 - yi.^2 - zi.^2;
-%! assert (max(abs(vv(:))), 0, 0.1)
+%! assert (max (abs (vv(:))), 0, 0.1);
 
 %!testif HAVE_QHULL
-%! rand('state', 0);
-%! x = 2 * rand(1000, 1) - 1;
-%! y = 2 * rand(1000, 1) - 1;
-%! z = 2 * rand(1000, 1) - 1;
+%! old_state = rand ("state");
+%! restore_state = onCleanup (@() rand ("state", old_state));
+%! rand ("state", 0);
+%! x = 2 * rand (1000, 1) - 1;
+%! y = 2 * rand (1000, 1) - 1;
+%! z = 2 * rand (1000, 1) - 1;
 %! v = x.^2 + y.^2 + z.^2;
 %! [xi, yi, zi] = meshgrid (-0.8:0.2:0.8);
-%! ##vi = reshape (griddatan([x(:), y(:), z(:)], v, [xi(:), yi(:), zi(:)], 'linear'), size (xi));
 %! vi = griddata3 (x, y, z, v, xi, yi, zi, 'nearest');
 %! vv = vi - xi.^2 - yi.^2 - zi.^2;
-%! assert (max(abs(vv(:))), 0, 0.1)
+%! assert (max (abs (vv(:))), 0, 0.1)
--- a/scripts/linear-algebra/onenormest.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/linear-algebra/onenormest.m	Tue Oct 25 10:56:02 2011 -0700
@@ -277,8 +277,10 @@
 
 ## Only likely to be within a factor of 10.
 %!test
+%!  old_state = rand ("state");
+%!  restore_state = onCleanup (@() rand ("state", old_state));
+%!  rand ('state', 42);  % Initialize to guarantee reproducible results
 %!  N = 100;
-%!  rand ('state', 42);  % Initialize to guarantee reproducible results
 %!  A = rand (N);
 %!  [nm1, v1, w1] = onenormest (A);
 %!  [nminf, vinf, winf] = onenormest (A', 6);
--- a/scripts/plot/trimesh.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/plot/trimesh.m	Tue Oct 25 10:56:02 2011 -0700
@@ -39,28 +39,29 @@
     triplot (tri, x, y, z, varargin{:});
   else
     newplot ();
-    if (nargout > 0)
-      h = patch ("Vertices", [x(:), y(:), z(:)], "Faces", tri,
-                 "FaceColor", "none", "EdgeColor", __next_line_color__(),
-                 varargin{:});
-    else
-      patch ("Vertices", [x(:), y(:), z(:)], "Faces", tri,
-             "FaceColor", "none", "EdgeColor", __next_line_color__(),
-             varargin{:});
-    endif
-
+    handle = patch ("Vertices", [x(:), y(:), z(:)], "Faces", tri,
+                    "FaceColor", "none", "EdgeColor", __next_line_color__(),
+                    varargin{:});
     if (! ishold ())
       set (gca(), "view", [-37.5, 30],
            "xgrid", "on", "ygrid", "on", "zgrid", "on");
     endif
+    if (nargout > 0)
+      h = handle;
+    endif
   endif
+
 endfunction
 
+
 %!demo
+%! old_state = rand ("state");
+%! restore_state = onCleanup (@() rand ("state", old_state));
+%! rand ("state", 10);
 %! N = 10;
-%! rand ('state', 10)
 %! x = 3 - 6 * rand (N, N);
 %! y = 3 - 6 * rand (N, N);
 %! z = peaks (x, y);
 %! tri = delaunay (x(:), y(:));
 %! trimesh (tri, x(:), y(:), z(:));
+
--- a/scripts/plot/triplot.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/plot/triplot.m	Tue Oct 25 10:56:02 2011 -0700
@@ -22,7 +22,7 @@
 ## @deftypefnx {Function File} {@var{h} =} triplot (@dots{})
 ## Plot a triangular mesh in 2D@.  The variable @var{tri} is the triangular
 ## meshing of the points @code{(@var{x}, @var{y})} which is returned from
-## @code{delaunay}.  If given, the @var{linespec} determines the properties
+## @code{delaunay}.  If given, @var{linespec} determines the properties
 ## to use for the lines.  The output argument @var{h} is the graphic handle
 ## of the plot.
 ## @seealso{plot, trimesh, trisurf, delaunay}
@@ -35,19 +35,24 @@
   endif
 
   idx = tri(:, [1, 2, 3, 1]).';
-  nt = size (tri, 1);
+  nt = rows (tri);
+  handle = plot ([x(idx); NaN(1, nt)](:),
+                 [y(idx); NaN(1, nt)](:), varargin{:});
+
   if (nargout > 0)
-    h = plot ([x(idx); NaN(1, nt)](:),
-              [y(idx); NaN(1, nt)](:), varargin{:});
-  else
-    plot ([x(idx); NaN(1, nt)](:),
-          [y(idx); NaN(1, nt)](:), varargin{:});
+    h = handle;
   endif
+
 endfunction
 
+
 %!demo
-%! rand ('state', 2)
-%! x = rand (20, 1);
-%! y = rand (20, 1);
+%! old_state = rand ("state");
+%! restore_state = onCleanup (@() rand ("state", old_state));
+%! rand ("state", 2);
+%! N = 20;
+%! x = rand (N, 1);
+%! y = rand (N, 1);
 %! tri = delaunay (x, y);
 %! triplot (tri, x, y);
+
--- a/scripts/plot/trisurf.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/plot/trisurf.m	Tue Oct 25 10:56:02 2011 -0700
@@ -27,7 +27,7 @@
 ## @seealso{triplot, trimesh, delaunay3}
 ## @end deftypefn
 
-function varargout = trisurf (tri, x, y, z, varargin)
+function h = trisurf (tri, x, y, z, varargin)
 
   if (nargin < 3)
     print_usage ();
@@ -55,11 +55,11 @@
       varargin(end+(1:2)) = {"EdgeColor", "none"};
     endif
     newplot ();
-    h = patch ("Faces", tri, "Vertices", [x(:), y(:), z(:)],
-               "FaceVertexCData", reshape (c, numel (c), 1),
-               varargin{:});
+    handle = patch ("Faces", tri, "Vertices", [x(:), y(:), z(:)],
+                    "FaceVertexCData", reshape (c, numel (c), 1),
+                    varargin{:});
     if (nargout > 0)
-      varargout = {h};
+      h = handle;
     endif
 
     if (! ishold ())
@@ -67,11 +67,15 @@
            "xgrid", "on", "ygrid", "on", "zgrid", "on");
     endif
   endif
+
 endfunction
 
+
 %!demo
+%! old_state = rand ("state");
+%! restore_state = onCleanup (@() rand ("state", old_state));
+%! rand ("state", 10);
 %! N = 10;
-%! rand ('state', 10)
 %! x = 3 - 6 * rand (N, N);
 %! y = 3 - 6 * rand (N, N);
 %! z = peaks (x, y);
@@ -99,4 +103,3 @@
 %! tri = delaunay (x, y);
 %! trisurf (tri, x, y, z, "facecolor", "interp", "edgecolor", "k")
 
-
--- a/scripts/sparse/svds.m	Tue Oct 25 10:17:23 2011 -0700
+++ b/scripts/sparse/svds.m	Tue Oct 25 10:56:02 2011 -0700
@@ -251,8 +251,12 @@
 %! s = s(idx);
 %! u = u(:,idx);
 %! v = v(:,idx);
+%! old_state1 = randn ("state");
+%! restore_state1 = onCleanup (@() randn ("state", old_state1));
+%! old_state2 = rand ("state");
+%! restore_state2 = onCleanup (@() rand ("state", old_state2));
 %! randn ('state', 42);      % Initialize to make normest function reproducible
-%! rand ('state', 42)
+%! rand ('state', 42);
 %! opts.v0 = rand (2*n,1); % Initialize eigs ARPACK starting vector
 %!                         % to guarantee reproducible results
 %!test
@@ -279,3 +283,4 @@
 %!test
 %! s = svds (speye (10));
 %! assert (s, ones (6, 1), 2*eps);
+