# HG changeset patch # User Jordi GutiƩrrez Hermoso # Date 1360167397 18000 # Node ID 9978c44ab12cce04c56bb2fae704e6ac6daacad8 # Parent 369b173d6c165838e60c69994239c812113291ab Style fixes on printd.m and stemleaf.m * printd.m, stemleaf.m: Whitespace fixes, changing single-quoted strings to double-quoted, use octothorpe (#) consistently for comments instead of percentage sign (%). diff -r 369b173d6c16 -r 9978c44ab12c scripts/plot/printd.m --- a/scripts/plot/printd.m Tue Feb 05 19:56:29 2013 -0500 +++ b/scripts/plot/printd.m Wed Feb 06 11:16:37 2013 -0500 @@ -18,7 +18,6 @@ ## License along with Octave; see the file COPYING. If not, ## see . - ## -*- texinfo -*- ## @deftypefn {Function File} {} printd (@var{obj}, @var{filename}) ## @@ -34,64 +33,67 @@ ## Description: Convert objects into other file formats. function printd (obj, filename) -% Convert any object acceptable to disp() into various display formats. -% obj is the input object. -% filename is the output file (with required suffix). + ## Convert any object acceptable to disp() into various display formats. + ## obj is the input object. + ## filename is the output file (with required suffix). -% Extract .suffix from filename - if ((sufix = rindex (filename, '.')) <= 0) - error ('The output filename: %s requires a suffix.\nOptions are: pdf ps eps txt jpg jpeg', filename); + ## Extract .suffix from filename + if ((sufix = rindex (filename, ".")) <= 0) + error ("The output filename: %s requires a suffix.\nOptions are: pdf ps eps txt jpg jpeg", filename); endif opt = substr (filename, sufix+1); - [pf, tempf, mag] = mkstemp ('oct-XXXXXX', 1); % Safe version of tmpnam() - fprintf (pf, '%s', disp (obj)); + [pf, tempf, mag] = mkstemp ("oct-XXXXXX", 1); # Safe version of tmpnam() + fprintf (pf, "%s", disp (obj)); frewind (pf); -% It seems best to only use convert for image output. Its ps and pdf are badly rendered. + + ## It seems best to only use convert for image output. Its ps and pdf + ## are badly rendered. opt = lower (opt); switch opt case {"pdf"} enscr = sprintf ( - 'enscript --no-header -o %s.ps %s ; ps2pdf %s.ps %s.pdf; mv %s.pdf %s;exit',... - tempf, tempf, tempf, tempf, tempf, filename); + "enscript --no-header -o %s.ps %s ; ps2pdf %s.ps %s.pdf; mv %s.pdf %s;exit",... + tempf, tempf, tempf, tempf, tempf, filename); system (enscr); - delete ([tempf '.ps']); + delete ([tempf ".ps"]); case {"ps"} - enscr = sprintf ('enscript --no-header -o %s %s ; exit', filename, tempf); + enscr = sprintf ("enscript --no-header -o %s %s ; exit", filename, tempf); system (enscr); case {"eps"} enscr = sprintf ( - 'enscript --no-header -o %s.ps %s ; ps2eps --ignoreBB %s.ps; mv %s.eps %s; exit',... - tempf, tempf, tempf, tempf, filename); + "enscript --no-header -o %s.ps %s ; ps2eps --ignoreBB %s.ps; mv %s.eps %s; exit",... + tempf, tempf, tempf, tempf, filename); system (enscr); - delete ([tempf '.ps']); + delete ([tempf ".ps"]); case {"txt"} - enscr = sprintf ('cp %s %s', tempf, filename); + enscr = sprintf ("cp %s %s", tempf, filename); system (enscr); case {"jpg" "jpeg"} - enscr = sprintf ('convert -trim txt:%s jpg:%s', tempf, filename); + enscr = sprintf ("convert -trim txt:%s jpg:%s", tempf, filename); system (enscr); otherwise fclose (pf); delete (tempf); - error ('Unknown conversion type: %s.\nOptions are: pdf ps eps txt jpg jpeg', opt); - endswitch - fclose (pf); - delete (tempf); - printf ('%s file %s written\n', opt, filename); + error ("Unknown conversion type: %s.\nOptions are: pdf ps eps txt jpg jpeg", opt); + + endswitch + fclose (pf); + delete (tempf); + printf ("%s file %s written\n", opt, filename); endfunction %!demo %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 12, hi: 42\n";... %! " 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"]; -%! printd (r2, 'test_p.txt'); +%! printd (r2, "test_p.txt"); %! system ("cat test_p.txt"); %! delete ("test_p.txt"); %!test %! r2 = ["stem step: 10, data: unsorted.\nHinges: lo: 12, hi: 42\n";... %! " 1 | 22118";" 2 | 28";" 3 | 98";" 4 | 244";" 5 | 2"]; -%! printd (r2, 'test_p.txt'); -%! r4 = fileread ('test_p.txt'); +%! printd (r2, "test_p.txt"); +%! r4 = fileread ("test_p.txt"); %! delete ("test_p.txt"); %! r2 = disp (r2); %! assert (r4, r2) diff -r 369b173d6c16 -r 9978c44ab12c scripts/plot/stemleaf.m --- a/scripts/plot/stemleaf.m Tue Feb 05 19:56:29 2013 -0500 +++ b/scripts/plot/stemleaf.m Wed Feb 06 11:16:37 2013 -0500 @@ -44,54 +44,55 @@ ## Description: Compute stem and leaf plot function varargout = stemleaf (x, stem_unit) -% Compute and display a stem and leaf plot of the vector x. -% The x vector is converted to integer by x = fix(x). -% If an output argument is provided, the plot is returned as -% an array of strings. The first element is the heading -% followed by an element for each stem. -% The default stem step is 10. If stem_unit is provided -% the stem step is set to: 10^(stem_unit+1) -% The x vector should be integers. It will be treated so that -% the last digit is the leaf value and the other digits are -% the stems. + ## Compute and display a stem and leaf plot of the vector x. The x + ## vector is converted to integer by x = fix(x). If an output argument + ## is provided, the plot is returned as an array of strings. The + ## first element is the heading followed by an element for each stem. + ## + ## The default stem step is 10. If stem_unit is provided the stem + ## step is set to: 10^(stem_unit+1) The x vector should be integers. + ## It will be treated so that the last digit is the leaf value and the + ## other digits are the stems. + ## + ## When we first implemented stem and leaf plots in the early 1960's + ## there was some discussion about sorting vs. leaving the leaf + ## entries in the original order in the data. We decided in favor or + ## sorting the leaves for most purposes. This is the choice + ## implemented in the SNAP/IEDA system that was written at that time. + ## + ## SNAP/IEDA and particularly its stem and leaf plotting were further + ## developed by Hale Trotter, David Hoagland (at Princeton and MIT) + ## and others. + ## + ## Tukey, in EDA, generally uses unsorted leaves. In addition, he + ## described a wide range of additional display formats. This + ## implementation does not sort the leaves, but if the x vector is + ## sorted then the leaves come out sorted. A simple display format is + ## used. + ## + ## I doubt if providing other options is worthwhile. The code can + ## quite easily be modified to provide specific display results. Or, + ## the returned output string can be edited. The returned output is an + ## array of strings with each row containing a line of the plot + ## preceded by the lines of header text as the first row. This + ## facilitates annotation. + ## + ## Note that the code has some added complexity due to the need to + ## distinguish both + and - 0 stems. The +- stem values are essential + ## for all plots which span 0. After dealing with +-0 stems, the added + ## complexity of putting +- data values in the correct stem is minor, + ## but the sign of 0 leaves must be checked. And, the cases where the + ## stems start or end at +- 0 must also be considered. + ## + ## The fact that IEEE floating point defines +- 0 helps make this + ## easier. + ## + ## + ## Michael D. Godfrey January 2013 -% When we first implemented stem and leaf plots in the early -% 1960's there was some discussion about sorting vs. leaving -% the leaf entries in the original order in the data. We -% decided in favor or sorting the leaves for most purposes. -% This is the choice implemented in the SNAP/IEDA system that -% was written at that time. -% SNAP/IEDA and particularly its stem and leaf plotting were further -% developed by Hale Trotter, David Hoagland (at Princeton and MIT) -% and others. -% Tukey, in EDA, generally uses unsorted leaves. In addition, -% he described a wide range of additional display formats. This -% implementation does not sort the leaves, but if the x vector is -% sorted then the leaves come out sorted. A simple display -% format is used. -% I doubt if providing other options is worthwhile. The -% code can quite easily be modified to provide specific -% display results. Or, the returned output string can be edited. -% The returned output is an array of strings with each row -% containing a line of the plot preceded by the lines of header -% text as the first row. This facilitates annotation. -% -% Note that the code has some added complexity due to the need -% to distinguish both + and - 0 stems. -% The +- stem values are essential for all plots which span 0. -% After dealing with +-0 stems, the added complexity of putting -% +- data values in the correct stem is minor, but the sign of -% 0 leaves must be checked. And, the cases where the stems start -% or end at +- 0 must also be considered. -% -% The fact that IEEE floating point defines +- 0 helps make this -% easier. -% -% Michael D. Godfrey January 2013 - -% More could be implemented for better data scaling. And, of course, -% other options for the kinds of plots described by Tukey could be -% provided. This may best be left to users. + ## More could be implemented for better data scaling. And, of course, + ## other options for the kinds of plots described by Tukey could be + ## provided. This may best be left to users. if (nargin >= 2) stem_step = 10^(stem_unit+1); @@ -102,31 +103,32 @@ printf ('Input vector truncated to integer values.\n') x = fix (x); endif -% Avoid use of int32 due to: -% floor (int32 (-44)/10) == -4 and floor (int32 (-46)/10) = -5 !!! -% x = sort (fix (x)); % User can decide about sorting x. -% x = fix (x); -% Adjust scale if too small. -% while any(abs((fix(x) - x)) >= abs(x/100)) -% x =10*x; -% endwhile -% Note that IEEE 754 states that -+ 0 should compare equal. -% This has led to C sort (and therefore Octave) treating them -% as equal. Thus, sort([ -1 0 -0 1]) yields: -1 0 -0 1. -% and, sort([-1 -0 0 1]) yields: -1 -0 0 1. -% This means that stem-and-leaf plotting cannot rely on sort -% to order the data as needed for display. + + ## Avoid use of int32 due to: -% + ## floor (int32 (-44)/10) == -4 and floor (int32 (-46)/10) = -5 !!! + ## x = sort (fix (x)); % User can decide about sorting x. + ## x = fix (x); + ## %Adjust scale if too small. + ## while any(abs((fix(x) - x)) >= abs(x/100)) + ## x =10*x; + ## endwhile + + ## Note that IEEE 754 states that -+ 0 should compare equal. This has + ## led to C sort (and therefore Octave) treating them as equal. Thus, + ## sort([ -1 0 -0 1]) yields: -1 0 -0 1. and, sort([-1 -0 0 1]) + ## yields: -1 -0 0 1. This means that stem-and-leaf plotting cannot + ## rely on sort to order the data as needed for display. + if (all((sort(x) == x)) == 1) hsort = 'sorted.'; else hsort = 'unsorted.'; endif nx = max (size (x)); -% Determine stem values + ## Determine stem values if (min(x) < 0) - if (signbit(max(x)) == 0) % max is positive + if (signbit(max(x)) == 0) # max is positive stems = [fix(min(x)/stem_step)-1 : -1 -0]; stems = [stems 0 : fix(max(x)/stem_step)+1 ]; else @@ -137,18 +139,18 @@ stems = [stems 0 : fix(max(x)/stem_step)]; endif endif - else % All stems are > 0 + else # All stems are > 0 stems = [fix(min(x)/stem_step) : fix(max(x)/stem_step) + 1]; endif -%stems -%x + ##stems + ##x nstems = max(size(stems)); -% compute hinges at +- 1.5 * quartiles -% this requires sorted data! - xs = sort (x); % Note that sort preserves -0 + ## compute hinges at +- 1.5 * quartiles + ## this requires sorted data! + xs = sort (x); # Note that sort preserves -0 threeh = 1.5; two = 2.0; - j = idivide(nx, 4, "fix") + 1; % Use F95 truncation. + j = idivide(nx, 4, "fix") + 1; # Use F95 truncation. k = nx - j + 1; hl = xs (j); hu = xs (k); @@ -156,88 +158,97 @@ hl = (xs (j + 1) + hl) / two; hu = (xs (k - 1) + hu) / two; endif -% -% :::::::: determine h-spread (dh) and fences :::::::: + + ## :::::::: determine h-spread (dh) and fences :::::::: dh = hu - hl; fu = hu + threeh * dh; fl = hl - threeh * dh; -% :::::::: find value adjacent to lower fence :::::::: + ## :::::::: find value adjacent to lower fence :::::::: for i = 1:j - if ( xs (i) >= fl ) continue; endif + if ( xs (i) >= fl ) + continue; + endif endfor ilow = i; xlo = xs (ilow); -% -% :::::::: find value adjacent to upper fence :::::::: + + ## :::::::: find value adjacent to upper fence :::::::: for i = 1:j - if ( xs (nx -i + 1) <= fu )continue; endif + if ( xs (nx -i + 1) <= fu ) + continue; + endif endfor -% + ihi = nx - i + 1; xhi = xs (ihi); -% -% Heading for output: - plot_out = ['']; - plot_out = [plot_out sprintf('stem step: %i, data: %s\nHinges: lo: %g, hi: %g\n', - stem_step, hsort, xlo, xhi)]; -% This may appear to be a good place to use vectorization using the stem and data arrays -% but the necessary special case treatment of 0 and -0 seems to result in little reduction -% of complexity, and since this algorithm is for small data vectors only there would be -% practically no performance improvement. + + ## Heading for output: + plot_out = ""; + plot_out = [plot_out sprintf("stem step: %i, data: %s\nHinges: lo: %g, hi: %g\n", + stem_step, hsort, xlo, xhi)]; + + ## This may appear to be a good place to use vectorization using the + ## stem and data arrays but the necessary special case treatment of 0 + ## and -0 seems to result in little reduction of complexity, and since + ## this algorithm is for small data vectors only there would be + ## practically no performance improvement. + + ## Determine leaves for each stem: + for kx = 2:nstems + line_out = ""; + steml = ""; + ## Build a string of leaf digits for stem(kx) if stem(kx) <= 0, or + ## stem(kx-1) if stem(kx) > 0 -% Determine leaves for each stem: - for kx = 2:nstems - line_out = ['']; - steml = ['']; -% Build a string of leaf digits for stem(kx) if stem(kx) <= 0, or stem(kx-1) if stem(kx) > 0 -% stems -+ 0 have to be handled as special cases. - for xi = 1:nx - if(signbit(stems(kx)) == 1) - t1 = ((x(xi) <= stems(kx)*10) && (x(xi) > (stems(kx-1)*10))); + ## stems -+ 0 have to be handled as special cases. + for xi = 1:nx + if(signbit(stems(kx)) == 1) + t1 = ((x(xi) <= stems(kx)*10) && (x(xi) > (stems(kx-1)*10))); + else + t1 = ((x(xi) < stems(kx)*10) && (x(xi) >= (stems(kx-1)*10))); + endif + ## Special tests for stem -+ 0 + if ((stems(kx) == 0) && signbit(stems(kx)) && (x(xi) == 0)) && !signbit(x(xi)) + t1 = 0; + endif + if ((stems(kx-1) == 0) && !signbit(stems(kx-1)) && (x(xi) == 0)) && signbit(x(xi)) + t1 = 0; + endif + ## Create line as a string + if t1 + if (stems(kx) <= 0) + xz = abs (x(xi) - stems(kx)*10); else - t1 = ((x(xi) < stems(kx)*10) && (x(xi) >= (stems(kx-1)*10))); - endif -% Special tests for stem -+ 0 - if ((stems(kx) == 0) && signbit(stems(kx)) && (x(xi) == 0)) && !signbit(x(xi)) - t1 = 0; + xz = abs (x(xi) - stems(kx-1)*10); endif - if ((stems(kx-1) == 0) && !signbit(stems(kx-1)) && (x(xi) == 0)) && signbit(x(xi)) - t1 = 0; + if ((stems(kx) == 0) && signbit(stems(kx))) + steml = [steml sprintf("%d", abs(x(xi) - stems(kx)*10))]; + else + steml = [steml sprintf("%d", xz)]; endif -% Create line as a string - if t1 - if (stems(kx) <= 0) - xz = abs (x(xi) - stems(kx)*10); - else - xz = abs (x(xi) - stems(kx-1)*10); - endif - if ((stems(kx) == 0) && signbit(stems(kx))) - steml = [steml sprintf('%d', abs(x(xi) - stems(kx)*10))]; - else - steml = [steml sprintf('%d', xz)]; - endif - endif % t1 - endfor % xi = 1:nx -% Set correct -0 - if ((stems(kx) == 0) && signbit(stems(kx))) - line_out = [line_out sprintf(' -0 | %s', steml)]; % -0 stem. + endif % t1 + endfor % xi = 1:nx + + ## Set correct -0 + if ((stems(kx) == 0) && signbit(stems(kx))) + line_out = [line_out sprintf(" -0 | %s", steml)]; % -0 stem. + else + if( stems(kx) < 0) + line_out = [line_out sprintf("%4d | %s", stems(kx), steml)]; else - if( stems(kx) < 0) - line_out = [line_out sprintf('%4d | %s', stems(kx), steml)]; - else - if stems(kx) > 0 - line_out = [line_out sprintf('%4d | %s', stems(kx-1), steml)]; - endif + if stems(kx) > 0 + line_out = [line_out sprintf("%4d | %s", stems(kx-1), steml)]; endif endif - plot_out = [plot_out; line_out]; - endfor % kx = 2:nstems + endif + plot_out = [plot_out; line_out]; + endfor % kx = 2:nstems if (nargout == 0) rows = size (plot_out)(1); cols = size (plot_out)(2); for k = 1:rows - printf('%s\n', plot_out(k,1:cols)); + printf("%s\n", plot_out(k,1:cols)); endfor else varargout{1} = plot_out;