From bug-octave-request at bevo dot che dot wisc dot edu Tue Nov 18 13:06:45 2003 Subject: "format +" documentation From: "John W. Eaton" To: taltman at lbl dot gov Cc: bug-octave at bevo dot che dot wisc dot edu Date: Tue, 18 Nov 2003 13:06:39 -0600 On 26-Oct-2003, taltman at lbl dot gov wrote: | I'm using Octave 2.1.40, and I've noticed that 'format +' not only | prints '+' and ' ' characters, but also '-'. I guess it's '+' for | positive matrix values, '-' for negative matrix values, and ' ' for | zero values. I think the documentation should be updated to include | the '-' convention, and perhaps Octave could use some other | place-holder for zero entries? It took me a while to realize that my | entire bottom row was all zeros. ;-) How about the following patch? It makes the default behavior Matlab compatible and allows you to choose the characters you would prefer for positive, negative, and zero values. jwe src/ChangeLog: 2003-11-18 John W. Eaton * pr-output.cc (octave_print_internal): Don't indent rows for plus_format. Use pr_plus_format for Range type with plus_format. (plus_format_chars): New static variable. (set_format_style): Allow optional arg for plus format to set it. (pr_plus_format): Use it. Index: src/pr-output.cc =================================================================== RCS file: /usr/local/cvsroot/octave/src/pr-output.cc,v retrieving revision 1.114 diff -u -r1.114 pr-output.cc --- src/pr-output.cc 31 Oct 2003 05:57:44 -0000 1.114 +++ src/pr-output.cc 18 Nov 2003 19:04:00 -0000 at @ -82,6 +82,9 @@ // TRUE means print plus sign for nonzero, blank for zero. static bool plus_format = false; +// First char for > 0, second for < 0, third for == 0. +static std::string plus_format_chars = "+ "; + // TRUE means always print like dollars and cents. static bool bank_format = false; at @ -1341,12 +1344,12 @@ static inline void pr_plus_format (std::ostream& os, double d) { - if (d == 0.0) - os << " "; + if (d > 0.0) + os << plus_format_chars[0]; else if (d < 0.0) - os << "-"; + os << plus_format_chars[1]; else - os << "+"; + os << plus_format_chars[2]; } void at @ -1383,9 +1386,6 @@ { OCTAVE_QUIT; - if (j == 0) - os << " "; - pr_plus_format (os, m(i,j)); } at @ -1644,9 +1644,6 @@ { OCTAVE_QUIT; - if (j == 0) - os << " "; - pr_plus_format (os, cm(i,j)); } at @ -1794,16 +1791,13 @@ if (plus_format && ! pr_as_read_syntax) { - os << " "; for (int i = 0; i < num_elem; i++) { OCTAVE_QUIT; double val = base + i * increment; - if (val == 0.0) - os << " "; - else - os << "+"; + + pr_plus_format (os, val); } } else at @ -2210,6 +2204,21 @@ } else if (arg == "+" || arg == "plus") { + if (--argc > 0) + { + arg = argv[idx++]; + + if (arg.length () == 3) + plus_format_chars = arg; + else + { + error ("format: invalid option for plus format"); + return; + } + } + else + plus_format_chars = "+ "; + init_format_state (); plus_format = true; } at @ -2336,10 +2345,27 @@ point.\n\ \n\ at item +\n\ + at itemx + @var{chars}\n\ + at itemx plus\n\ + at itemx plus @var{chars}\n\ Print a at samp{+} symbol for nonzero matrix elements and a space for zero\n\ matrix elements. This format can be very useful for examining the\n\ structure of a large matrix.\n\ \n\ +The optional argument at var{chars} specifies a list of 3 characters to use\n\ +for printing values greater than zero, less than zero and equal to zero.\n\ +For example, with the at samp{+ \"+-.\"} format, @code{[1, 0, -1; -1, 0, 1]}\n\ +is displayed as\n\ +\n\ + at example\n\ + at group\n\ +ans =\n\ +\n\ ++.-\n\ +-.+\n\ + at end group\n\ + at end example\n\ +\n\ at item hex\n\ Print the hexadecimal representation numbers as they are stored in\n\ memory. For example, on a workstation which stores 8 byte real values\n\ ------------------------------------------------------------- Octave is freely available under the terms of the GNU GPL. Octave's home on the web: http://www.octave.org How to fund new projects: http://www.octave.org/funding.html Subscription information: http://www.octave.org/archive.html -------------------------------------------------------------