changeset 4878:4eaf35cfdb11

[project @ 2004-04-23 15:42:10 by jwe]
author jwe
date Fri, 23 Apr 2004 15:42:10 +0000
parents 0ab4fd8ded30
children 013350fee837
files scripts/ChangeLog scripts/deprecated/com2str.m scripts/general/int2str.m scripts/general/num2str.m scripts/strings/com2str.m
diffstat 5 files changed, 180 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Thu Apr 22 19:59:44 2004 +0000
+++ b/scripts/ChangeLog	Fri Apr 23 15:42:10 2004 +0000
@@ -1,3 +1,14 @@
+2004-04-23  David Bateman  <dbateman@free.fr>
+
+	* general/int2str.m: Treat only real part of argument, and treat
+	NDArrays by stacking the slices through the matrix vertically.
+
+	* general/num2str.m: Improve format of integer matrices, and the
+	conversion of complex matrices added. Treat NDArrays by stacking
+	the slices through the matrix vertically.
+
+	* deprecated/com2str.m: Moved here from general subdirectory.
+
 2004-04-22  John W. Eaton  <jwe@octave.org>
 
 	* quaternion/qtransvmat.m: Use continuation characters to make
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/deprecated/com2str.m	Fri Apr 23 15:42:10 2004 +0000
@@ -0,0 +1,83 @@
+## Copyright (C) 1998 Auburn University.  All rights reserved.
+##
+## This file is part of Octave.
+##
+## Octave is free software; you can redistribute it and/or modify it
+## under the terms of the GNU General Public License as published by the
+## Free Software Foundation; either version 2, or (at your option) any
+## later version.
+##
+## Octave is distributed in the hope that it will be useful, but WITHOUT
+## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+## for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with Octave; see the file COPYING.  If not, write to the Free
+## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
+
+## -*- texinfo -*-
+## @deftypefn {Function File} {} com2str (@var{zz}, @var{flg})
+## This function has been deprecated.  Use num2str instead.
+##
+## Convert complex number to a string.
+## @strong{Inputs}
+## @table @var
+## @item zz
+## complex number
+## @item flg
+## format flag
+## 0 (default):            -1, 0, 1,   1i,   1 + 0.5i
+## 1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i
+## @end table
+## @end deftypefn
+
+function retval = com2str (zz, flg)
+
+  if (nargin < 1 || nargin > 2)
+    usage ("com2str (zz, flg)");
+  endif
+  if (nargin == 1)
+    flg = 0;
+  endif
+
+  if (! (isscalar (zz) && isscalar (flg)))
+    error ("com2str: arguments must be a scalar");
+  endif
+
+  if (flg != 0 && flg != 1)
+    error ("invalid flg value: %d", flg);
+  endif
+
+  sgns = "+-";
+  rz = real (zz);
+  iz = imag (zz);
+  az = abs (zz);
+  if (iz == 0)
+    ## strictly a real number
+    switch (flg)
+      case(0)
+	retval = num2str (rz);
+      case(1)
+	retval = [sgns(1+(rz<0)), " ", num2str(abs(rz))];
+    endswitch
+  elseif (rz == 0)
+    ## strictly an imaginary number
+    switch (flg)
+      case(0)
+	retval = num2str (iz);
+      case(1)
+	retval = [sgns(1+(iz<0)), " ", num2str(abs(iz)), "i"];
+    endswitch
+  else
+    ## complex number
+    ## strictly an imaginary number
+    switch (flg)
+      case(0)
+	retval = [num2str(rz), " ", com2str(i*iz,1)];
+      case(1)
+	retval = [sgns(1+(rz<0)), " ", num2str(abs(rz)), " ", com2str(i*iz,1)];
+    endswitch
+  endif
+
+endfunction
--- a/scripts/general/int2str.m	Thu Apr 22 19:59:44 2004 +0000
+++ b/scripts/general/int2str.m	Fri Apr 23 15:42:10 2004 +0000
@@ -32,16 +32,24 @@
 function retval = int2str (x)
 
   if (nargin == 1)
-    x = round (x);
+    x = round (real(x));
+    sz = size(x);
+    nd = ndims (x);
     nc = columns (x);
     if (nc > 1)
-      ifmt = get_fmt (x(:,1), 0);
-      rfmt = get_fmt (x(:,2:end), 2);
+      idx = cell ();
+      for i = 1:nd
+	idx {i} = 1:sz(i);
+      endfor
+      idx(2) = 1;
+      ifmt = get_fmt (x(idx{:}), 0);
+      idx(2) = 2:sz(2);
+      rfmt = get_fmt (x(idx{:}), 2);
       fmt = strcat (ifmt, repmat (rfmt, 1, nc-1), "\n")
     else
       fmt = strcat (get_fmt (x, 0), "\n");
     endif
-    tmp = sprintf (fmt, round (x.'));
+    tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd]));
     tmp(end) = "";
     retval = split (tmp, "\n");
   else
--- a/scripts/general/num2str.m	Thu Apr 22 19:59:44 2004 +0000
+++ b/scripts/general/num2str.m	Fri Apr 23 15:42:10 2004 +0000
@@ -31,7 +31,74 @@
 
 function retval = num2str (x, arg)
 
-  if (nargin == 1 || nargin == 2)
+  if (nargin != 1 && nargin != 2)
+    usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)");
+  endif
+
+  if (isstr (x))
+    retval = x;
+  endif
+
+  if (iscomplex (x))
+    if (nargin == 2)
+      if (isstr (arg))
+	fmt = strcat (arg, "%-+", arg(2:end), "i");
+      else
+	fmt = sprintf ("%%.%dg%%-+.%dgi", arg);
+      endif
+    else
+      ## Setup a suitable format string
+      if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
+	dgt1 = ceil (log10 (max (max (abs (real (x(:)))),
+			     max (abs (imag (x(:))))))) + 1;
+	dgt2 = dgt1 - (min (real (x(:))) >= 0);
+	fmt = sprintf("%%%dd%%+-%ddi  ", dgt2, dgt1);
+      elseif (isscalar (x))
+	fmt = "%.4g%-+.4gi";
+      else
+	fmt = "%11.4g%-+11.4gi";
+      endif
+    endif
+
+    ## Manipulate the complex value to have real values in the odd
+    ## columns and imaginary values in the even columns
+    sz = size (x);
+    nc = sz (2);
+    nd = ndims (x);
+    perm = fix ([1 : 0.5 : nc + 0.5]);
+    perm (2 : 2 : 2*nc) = perm(2 : 2 : 2*nc) + nc;
+    idx = cell ();
+    for i = 1:nd
+      idx {i} = 1:sz(i);
+    endfor
+    idx {2} = perm;
+    x = horzcat (real(x), imag(x));
+    x = x (idx{:});
+    
+    fmt = strcat (deblank(repmat (fmt, 1, nc)), "\n");
+    tmp = sprintf (fmt, permute (x, [2, 1, 3 : nd]));
+
+    ## Put the "i"'s where they are supposed to be.
+    while (true)
+      tmp2 = strrep (tmp, " i\n", "i\n");
+      if (length(tmp) == length(tmp2))
+	break;
+      else
+	tmp = tmp2;
+      endif
+    endwhile
+    while (true)
+      tmp2 = strrep (tmp, " i", "i ");
+      if (tmp == tmp2)
+	break;
+      else
+	tmp = tmp2;
+      endif
+    endwhile
+
+    tmp(length (tmp)) = "";
+    retval = split (tmp, "\n");
+  else
     if (nargin == 2)
       if (isstr (arg))
 	fmt = arg;
@@ -40,25 +107,18 @@
       endif
     else
       if (isnumeric (x) && round (x) == x && abs (x) < 1e10)
-	fmt = "%d";
+	dgt = ceil(log10(max(abs(x(:)))))+ (min (real (x(:))) < 0);
+	fmt = sprintf("%%%dd  ",dgt);
       elseif (isscalar (x))
 	fmt = "%.4g";
       else
 	fmt = "%11.4g";
       endif
     endif
-    if (isstr (x))
-      retval = x;
-    elseif (iscomplex (x))
-      error ("num2str: sorry, can't handle complex numbers yet");
-    else
-      fmt = strcat (repmat (fmt, 1, columns (x)), "\n");
-      tmp = sprintf (fmt, x.');
-      tmp(length (tmp)) = "";
-      retval = split (tmp, "\n");
-    endif
-  else
-    usage ("num2str (x) or num2str (x, precision) or num2str (x, fmt)");
+    fmt = strcat (deblank (repmat (fmt, 1, columns (x))), "\n");
+    tmp = sprintf (fmt, permute (x, [2, 1, 3 : ndims(x)]));
+    tmp(length (tmp)) = "";
+    retval = split (tmp, "\n");
   endif
 
 endfunction
--- a/scripts/strings/com2str.m	Thu Apr 22 19:59:44 2004 +0000
+++ b/scripts/strings/com2str.m	Fri Apr 23 15:42:10 2004 +0000
@@ -1,82 +0,0 @@
-## Copyright (C) 1998 Auburn University.  All rights reserved.
-##
-## This file is part of Octave.
-##
-## Octave is free software; you can redistribute it and/or modify it
-## under the terms of the GNU General Public License as published by the
-## Free Software Foundation; either version 2, or (at your option) any
-## later version.
-##
-## Octave is distributed in the hope that it will be useful, but WITHOUT
-## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-## FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-## for more details.
-##
-## You should have received a copy of the GNU General Public License
-## along with Octave; see the file COPYING.  If not, write to the Free
-## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
-
-## -*- texinfo -*-
-## @deftypefn {Function File} {} com2str (@var{zz}, @var{flg})
-##
-## convert complex number to a string
-## @strong{Inputs}
-## @table @var
-## @item zz
-## complex number
-## @item flg
-## format flag
-## 0 (default):            -1, 0, 1,   1i,   1 + 0.5i
-## 1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i
-## @end table
-## @end deftypefn
-
-function retval = com2str (zz, flg)
-
-  if (nargin < 1 || nargin > 2)
-    usage ("com2str (zz, flg)");
-  endif
-  if (nargin == 1)
-    flg = 0;
-  endif
-
-  if (! (isscalar (zz) && isscalar (flg)))
-    error ("com2str: arguments must be a scalar");
-  endif
-
-  if (flg != 0 && flg != 1)
-    error ("invalid flg value: %d", flg);
-  endif
-
-  sgns = "+-";
-  rz = real (zz);
-  iz = imag (zz);
-  az = abs (zz);
-  if (iz == 0)
-    ## strictly a real number
-    switch (flg)
-      case(0)
-	retval = num2str (rz);
-      case(1)
-	retval = [sgns(1+(rz<0)), " ", num2str(abs(rz))];
-    endswitch
-  elseif (rz == 0)
-    ## strictly an imaginary number
-    switch (flg)
-      case(0)
-	retval = num2str (iz);
-      case(1)
-	retval = [sgns(1+(iz<0)), " ", num2str(abs(iz)), "i"];
-    endswitch
-  else
-    ## complex number
-    ## strictly an imaginary number
-    switch (flg)
-      case(0)
-	retval = [num2str(rz), " ", com2str(i*iz,1)];
-      case(1)
-	retval = [sgns(1+(rz<0)), " ", num2str(abs(rz)), " ", com2str(i*iz,1)];
-    endswitch
-  endif
-
-endfunction