1
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
240
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
1
|
25 #endif |
|
26 |
1346
|
27 #include <cfloat> |
|
28 #include <cstring> |
1343
|
29 |
1728
|
30 #include <string> |
|
31 |
1
|
32 #include <iostream.h> |
|
33 #include <strstream.h> |
1343
|
34 |
453
|
35 #include "CMatrix.h" |
1
|
36 #include "Range.h" |
1352
|
37 #include "dMatrix.h" |
1961
|
38 #include "float-fmt.h" |
1651
|
39 #include "oct-cmplx.h" |
1769
|
40 #include "oct-math.h" |
1806
|
41 #include "oct-term.h" |
1755
|
42 #include "str-vec.h" |
1
|
43 |
1352
|
44 #include "defun.h" |
|
45 #include "error.h" |
2165
|
46 #include "gripes.h" |
1352
|
47 #include "help.h" |
1
|
48 #include "mappers.h" |
1755
|
49 #include "oct-obj.h" |
1352
|
50 #include "pager.h" |
|
51 #include "pr-output.h" |
1755
|
52 #include "pt-const.h" |
1282
|
53 #include "sysdep.h" |
1
|
54 #include "utils.h" |
1352
|
55 #include "variables.h" |
1
|
56 |
2165
|
57 // The maximum field width for a number printed by the default output |
|
58 // routines. |
|
59 static int Voutput_max_field_width; |
|
60 |
|
61 // The precision of the numbers printed by the default output |
|
62 // routines. |
|
63 static int Voutput_precision; |
|
64 |
|
65 // TRUE means that the dimensions of empty matrices should be printed |
|
66 // like this: x = [](2x0). |
|
67 static bool Vprint_empty_dimensions; |
|
68 |
|
69 // TRUE means that the rows of big matrices should be split into |
|
70 // smaller slices that fit on the screen. |
|
71 static bool Vsplit_long_rows; |
|
72 |
1
|
73 // Current format string for real numbers and the real part of complex |
|
74 // numbers. |
529
|
75 static char *curr_real_fmt = 0; |
1
|
76 |
|
77 // Current format string for the imaginary part of complex numbers. |
529
|
78 static char *curr_imag_fmt = 0; |
1
|
79 |
1186
|
80 // Nonzero means don't do any fancy formatting. |
1
|
81 static int free_format = 0; |
|
82 |
|
83 // Nonzero means print plus sign for nonzero, blank for zero. |
|
84 static int plus_format = 0; |
|
85 |
1282
|
86 // Nonzero means always print like dollars and cents. |
|
87 static int bank_format = 0; |
|
88 |
|
89 // Nonzero means print data in hexadecimal format. |
|
90 static int hex_format = 0; |
|
91 |
1309
|
92 // Nonzero means print data in binary-bit-pattern format. |
|
93 static int bit_format = 0; |
|
94 |
1186
|
95 // Nonzero means don't put newlines around the column number headers. |
|
96 static int compact_format = 0; |
|
97 |
1
|
98 // Nonzero means use an e format. |
|
99 static int print_e = 0; |
|
100 |
|
101 // Nonzero means print E instead of e for exponent field. |
|
102 static int print_big_e = 0; |
|
103 |
|
104 static int |
|
105 any_element_is_negative (const Matrix& a) |
|
106 { |
|
107 int nr = a.rows (); |
|
108 int nc = a.columns (); |
|
109 for (int j = 0; j < nc; j++) |
|
110 for (int i = 0; i < nr; i++) |
2305
|
111 if (a (i, j) < 0.0) |
1
|
112 return 1; |
|
113 return 0; |
|
114 } |
|
115 |
1309
|
116 // XXX FIXME XXX -- these should probably be somewhere else. |
|
117 |
|
118 int |
1
|
119 any_element_is_inf_or_nan (const Matrix& a) |
|
120 { |
|
121 int nr = a.rows (); |
|
122 int nc = a.columns (); |
|
123 for (int j = 0; j < nc; j++) |
|
124 for (int i = 0; i < nr; i++) |
|
125 { |
2305
|
126 double val = a (i, j); |
1
|
127 if (xisinf (val) || xisnan (val)) |
|
128 return 1; |
|
129 } |
|
130 return 0; |
|
131 } |
|
132 |
1309
|
133 int |
1
|
134 any_element_is_inf_or_nan (const ComplexMatrix& a) |
|
135 { |
|
136 int nr = a.rows (); |
|
137 int nc = a.columns (); |
|
138 for (int j = 0; j < nc; j++) |
|
139 for (int i = 0; i < nr; i++) |
|
140 { |
2305
|
141 Complex val = a (i, j); |
1
|
142 if (xisinf (val) || xisnan (val)) |
|
143 return 1; |
|
144 } |
|
145 return 0; |
|
146 } |
|
147 |
|
148 static int |
|
149 all_elements_are_int_or_inf_or_nan (const Matrix& a) |
|
150 { |
|
151 int nr = a.rows (); |
|
152 int nc = a.columns (); |
|
153 for (int j = 0; j < nc; j++) |
|
154 for (int i = 0; i < nr; i++) |
|
155 { |
2305
|
156 double val = a (i, j); |
1
|
157 if (xisnan (val) || D_NINT (val) == val) |
|
158 continue; |
|
159 else |
|
160 return 0; |
|
161 } |
|
162 return 1; |
|
163 } |
|
164 |
|
165 static Matrix |
|
166 abs (const Matrix& a) |
|
167 { |
|
168 int nr = a.rows (); |
|
169 int nc = a.columns (); |
|
170 Matrix retval (nr, nc); |
|
171 for (int j = 0; j < nc; j++) |
|
172 for (int i = 0; i < nr; i++) |
2305
|
173 retval (i, j) = fabs (a (i, j)); |
1
|
174 return retval; |
|
175 } |
|
176 |
|
177 static double |
164
|
178 pr_max_internal (const Matrix& m) |
1
|
179 { |
|
180 int nr = m.rows (); |
|
181 int nc = m.columns (); |
|
182 |
|
183 double result = DBL_MIN; |
|
184 |
|
185 for (int j = 0; j < nc; j++) |
|
186 for (int i = 0; i < nr; i++) |
|
187 { |
2305
|
188 double val = m (i, j); |
1
|
189 if (xisinf (val) || xisnan (val)) |
|
190 continue; |
|
191 |
|
192 if (val > result) |
|
193 result = val; |
|
194 } |
|
195 return result; |
|
196 } |
|
197 |
|
198 static double |
164
|
199 pr_min_internal (const Matrix& m) |
1
|
200 { |
|
201 int nr = m.rows (); |
|
202 int nc = m.columns (); |
|
203 |
|
204 double result = DBL_MAX; |
|
205 |
|
206 for (int j = 0; j < nc; j++) |
|
207 for (int i = 0; i < nr; i++) |
|
208 { |
2305
|
209 double val = m (i, j); |
1
|
210 if (xisinf (val) || xisnan (val)) |
|
211 continue; |
|
212 |
|
213 if (val < result) |
|
214 result = val; |
|
215 } |
|
216 return result; |
|
217 } |
|
218 |
1658
|
219 // XXX FIXME XXX -- it would be nice to share more code among these |
|
220 // functions,.. |
|
221 |
1
|
222 static void |
1658
|
223 set_real_format (int sign, int digits, int inf_or_nan, int nan_or_int, |
|
224 int &fw) |
1
|
225 { |
278
|
226 static char fmt_buf[128]; |
1
|
227 |
2165
|
228 int prec = Voutput_precision; |
1
|
229 |
|
230 int ld, rd; |
|
231 |
|
232 if (bank_format) |
|
233 { |
|
234 fw = digits < 0 ? 4 : digits + 3; |
|
235 if (inf_or_nan && fw < 3) |
|
236 fw = 3; |
|
237 fw += sign; |
|
238 rd = 2; |
|
239 } |
1282
|
240 else if (hex_format) |
|
241 { |
|
242 fw = 2 * sizeof (double); |
|
243 rd = 0; |
|
244 } |
1309
|
245 else if (bit_format) |
|
246 { |
|
247 fw = 8 * sizeof (double); |
|
248 rd = 0; |
|
249 } |
1658
|
250 else if (nan_or_int) |
1
|
251 { |
|
252 fw = digits; |
|
253 if (inf_or_nan && fw < 3) |
|
254 fw = 3; |
|
255 fw += sign; |
|
256 rd = 0; |
|
257 } |
|
258 else |
|
259 { |
|
260 if (digits > 0) |
|
261 { |
|
262 ld = digits; |
1658
|
263 rd = prec > digits ? prec - digits : prec; |
1
|
264 digits++; |
|
265 } |
|
266 else |
|
267 { |
|
268 ld = 1; |
1658
|
269 rd = prec > digits ? prec - digits : prec; |
1
|
270 digits = -digits + 1; |
|
271 } |
|
272 |
|
273 fw = ld + 1 + rd; |
|
274 if (inf_or_nan && fw < 3) |
|
275 fw = 3; |
|
276 fw += sign; |
|
277 } |
|
278 |
1309
|
279 if (! (bank_format || hex_format || bit_format) |
2165
|
280 && (fw > Voutput_max_field_width || print_e)) |
1
|
281 { |
|
282 int exp_field = 4; |
|
283 if (digits > 100) |
|
284 exp_field++; |
|
285 |
|
286 fw = 2 + prec + exp_field; |
|
287 if (inf_or_nan && fw < 3) |
|
288 fw = 3; |
|
289 fw += sign; |
|
290 |
|
291 if (print_big_e) |
|
292 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
293 else |
|
294 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
295 } |
|
296 else |
|
297 { |
|
298 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
299 } |
|
300 |
|
301 curr_real_fmt = &fmt_buf[0]; |
|
302 } |
|
303 |
1658
|
304 static void |
|
305 set_format (double d, int& fw) |
|
306 { |
|
307 curr_real_fmt = 0; |
|
308 curr_imag_fmt = 0; |
|
309 |
|
310 if (free_format) |
|
311 return; |
|
312 |
|
313 int sign = (d < 0.0); |
|
314 |
|
315 int inf_or_nan = (xisinf (d) || xisnan (d)); |
|
316 |
|
317 int nan_or_int = (xisnan (d) || D_NINT (d) == d); |
|
318 |
|
319 double d_abs = d < 0.0 ? -d : d; |
|
320 |
|
321 int digits = (inf_or_nan || d_abs == 0.0) ? 0 |
|
322 : (int) floor (log10 (d_abs) + 1.0); |
|
323 |
|
324 set_real_format (sign, digits, inf_or_nan, nan_or_int, fw); |
|
325 } |
|
326 |
1
|
327 static inline void |
|
328 set_format (double d) |
|
329 { |
|
330 int fw; |
|
331 set_format (d, fw); |
|
332 } |
|
333 |
|
334 static void |
1658
|
335 set_real_matrix_format (int sign, int x_max, int x_min, |
1715
|
336 int inf_or_nan, int int_or_inf_or_nan, int& fw) |
1
|
337 { |
278
|
338 static char fmt_buf[128]; |
1
|
339 |
2165
|
340 int prec = Voutput_precision; |
1
|
341 |
|
342 int ld, rd; |
|
343 |
|
344 if (bank_format) |
|
345 { |
|
346 int digits = x_max > x_min ? x_max : x_min; |
|
347 fw = digits <= 0 ? 4 : digits + 3; |
|
348 if (inf_or_nan && fw < 3) |
|
349 fw = 3; |
|
350 fw += sign; |
|
351 rd = 2; |
|
352 } |
1282
|
353 else if (hex_format) |
|
354 { |
|
355 fw = 2 * sizeof (double); |
|
356 rd = 0; |
|
357 } |
1309
|
358 else if (bit_format) |
|
359 { |
|
360 fw = 8 * sizeof (double); |
|
361 rd = 0; |
|
362 } |
1715
|
363 else if (int_or_inf_or_nan) |
1
|
364 { |
|
365 int digits = x_max > x_min ? x_max : x_min; |
|
366 fw = digits <= 0 ? 1 : digits; |
|
367 if (inf_or_nan && fw < 3) |
|
368 fw = 3; |
|
369 fw += sign; |
|
370 rd = 0; |
|
371 } |
|
372 else |
|
373 { |
|
374 int ld_max, rd_max; |
|
375 if (x_max > 0) |
|
376 { |
|
377 ld_max = x_max; |
1658
|
378 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
379 x_max++; |
|
380 } |
|
381 else |
|
382 { |
|
383 ld_max = 1; |
1658
|
384 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
385 x_max = -x_max + 1; |
|
386 } |
|
387 |
|
388 int ld_min, rd_min; |
|
389 if (x_min > 0) |
|
390 { |
|
391 ld_min = x_min; |
1658
|
392 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
393 x_min++; |
|
394 } |
|
395 else |
|
396 { |
|
397 ld_min = 1; |
1658
|
398 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
399 x_min = -x_min + 1; |
|
400 } |
|
401 |
|
402 ld = ld_max > ld_min ? ld_max : ld_min; |
|
403 rd = rd_max > rd_min ? rd_max : rd_min; |
|
404 |
|
405 fw = ld + 1 + rd; |
|
406 if (inf_or_nan && fw < 3) |
|
407 fw = 3; |
|
408 fw += sign; |
|
409 } |
|
410 |
1658
|
411 if (! (bank_format || hex_format || bit_format) |
2165
|
412 && (fw > Voutput_max_field_width || print_e)) |
1
|
413 { |
|
414 int exp_field = 4; |
|
415 if (x_max > 100 || x_min > 100) |
|
416 exp_field++; |
|
417 |
|
418 fw = 2 + prec + exp_field; |
|
419 if (inf_or_nan && fw < 3) |
|
420 fw = 3; |
|
421 fw += sign; |
|
422 |
|
423 if (print_big_e) |
|
424 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
425 else |
|
426 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
427 } |
|
428 else |
|
429 { |
|
430 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
431 } |
|
432 |
|
433 curr_real_fmt = &fmt_buf[0]; |
|
434 } |
|
435 |
1658
|
436 static void |
|
437 set_format (const Matrix& m, int& fw) |
|
438 { |
|
439 curr_real_fmt = 0; |
|
440 curr_imag_fmt = 0; |
|
441 |
|
442 if (free_format) |
|
443 return; |
|
444 |
|
445 int sign = any_element_is_negative (m); |
|
446 |
|
447 int inf_or_nan = any_element_is_inf_or_nan (m); |
|
448 |
1715
|
449 int int_or_inf_or_nan = all_elements_are_int_or_inf_or_nan (m); |
1658
|
450 |
|
451 Matrix m_abs = abs (m); |
|
452 double max_abs = pr_max_internal (m_abs); |
|
453 double min_abs = pr_min_internal (m_abs); |
|
454 |
|
455 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
456 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
457 |
|
458 set_real_matrix_format (sign, x_max, x_min, inf_or_nan, |
1715
|
459 int_or_inf_or_nan, fw); |
1658
|
460 } |
|
461 |
1
|
462 static inline void |
164
|
463 set_format (const Matrix& m) |
1
|
464 { |
|
465 int fw; |
|
466 set_format (m, fw); |
|
467 } |
|
468 |
|
469 static void |
1658
|
470 set_complex_format (int sign, int x_max, int x_min, int r_x, |
|
471 int inf_or_nan, int int_only, int& r_fw, int& i_fw) |
1
|
472 { |
278
|
473 static char r_fmt_buf[128]; |
|
474 static char i_fmt_buf[128]; |
1
|
475 |
2165
|
476 int prec = Voutput_precision; |
1
|
477 |
|
478 int ld, rd; |
|
479 |
|
480 if (bank_format) |
|
481 { |
|
482 int digits = r_x; |
|
483 i_fw = 0; |
|
484 r_fw = digits <= 0 ? 4 : digits + 3; |
|
485 if (inf_or_nan && r_fw < 3) |
|
486 r_fw = 3; |
|
487 r_fw += sign; |
|
488 rd = 2; |
|
489 } |
1282
|
490 else if (hex_format) |
|
491 { |
|
492 r_fw = 2 * sizeof (double); |
|
493 i_fw = 2 * sizeof (double); |
|
494 rd = 0; |
|
495 } |
1309
|
496 else if (bit_format) |
|
497 { |
|
498 r_fw = 8 * sizeof (double); |
|
499 i_fw = 8 * sizeof (double); |
|
500 rd = 0; |
|
501 } |
1658
|
502 else if (inf_or_nan || int_only) |
1
|
503 { |
|
504 int digits = x_max > x_min ? x_max : x_min; |
|
505 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
506 if (inf_or_nan && i_fw < 3) |
|
507 i_fw = r_fw = 3; |
|
508 r_fw += sign; |
|
509 rd = 0; |
|
510 } |
|
511 else |
|
512 { |
|
513 int ld_max, rd_max; |
|
514 if (x_max > 0) |
|
515 { |
|
516 ld_max = x_max; |
1658
|
517 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
518 x_max++; |
|
519 } |
|
520 else |
|
521 { |
|
522 ld_max = 1; |
1658
|
523 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
524 x_max = -x_max + 1; |
|
525 } |
|
526 |
|
527 int ld_min, rd_min; |
|
528 if (x_min > 0) |
|
529 { |
|
530 ld_min = x_min; |
1658
|
531 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
532 x_min++; |
|
533 } |
|
534 else |
|
535 { |
|
536 ld_min = 1; |
1658
|
537 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
538 x_min = -x_min + 1; |
|
539 } |
|
540 |
|
541 ld = ld_max > ld_min ? ld_max : ld_min; |
|
542 rd = rd_max > rd_min ? rd_max : rd_min; |
|
543 |
|
544 i_fw = r_fw = ld + 1 + rd; |
|
545 if (inf_or_nan && i_fw < 3) |
|
546 i_fw = r_fw = 3; |
|
547 r_fw += sign; |
|
548 } |
|
549 |
1309
|
550 if (! (bank_format || hex_format || bit_format) |
2165
|
551 && (r_fw > Voutput_max_field_width || print_e)) |
1
|
552 { |
|
553 int exp_field = 4; |
|
554 if (x_max > 100 || x_min > 100) |
|
555 exp_field++; |
|
556 |
|
557 i_fw = r_fw = 1 + prec + exp_field; |
|
558 if (inf_or_nan && i_fw < 3) |
|
559 i_fw = r_fw = 3; |
|
560 r_fw += sign; |
|
561 |
|
562 if (print_big_e) |
|
563 { |
|
564 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
565 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
566 } |
|
567 else |
|
568 { |
|
569 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
570 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
571 } |
|
572 } |
|
573 else |
|
574 { |
|
575 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
576 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
577 } |
|
578 |
|
579 curr_real_fmt = &r_fmt_buf[0]; |
|
580 curr_imag_fmt = &i_fmt_buf[0]; |
|
581 } |
|
582 |
1658
|
583 static void |
|
584 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
585 { |
|
586 curr_real_fmt = 0; |
|
587 curr_imag_fmt = 0; |
|
588 |
|
589 if (free_format) |
|
590 return; |
|
591 |
|
592 double rp = c.real (); |
|
593 double ip = c.imag (); |
|
594 |
|
595 int sign = (rp < 0.0); |
|
596 |
|
597 int inf_or_nan = (xisinf (c) || xisnan (c)); |
|
598 |
|
599 int int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
|
600 |
|
601 double r_abs = rp < 0.0 ? -rp : rp; |
|
602 double i_abs = ip < 0.0 ? -ip : ip; |
|
603 |
|
604 int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0); |
|
605 int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0); |
|
606 |
|
607 int x_max, x_min; |
|
608 |
|
609 if (r_x > i_x) |
|
610 { |
|
611 x_max = r_x; |
|
612 x_min = i_x; |
|
613 } |
|
614 else |
|
615 { |
|
616 x_max = i_x; |
|
617 x_min = r_x; |
|
618 } |
|
619 |
|
620 set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only, |
|
621 r_fw, i_fw); |
|
622 } |
|
623 |
1
|
624 static inline void |
164
|
625 set_format (const Complex& c) |
1
|
626 { |
|
627 int r_fw, i_fw; |
|
628 set_format (c, r_fw, i_fw); |
|
629 } |
|
630 |
|
631 static void |
1658
|
632 set_complex_matrix_format (int sign, int x_max, int x_min, |
|
633 int r_x_max, int r_x_min, int inf_or_nan, |
1715
|
634 int int_or_inf_or_nan, int& r_fw, int& i_fw) |
1
|
635 { |
278
|
636 static char r_fmt_buf[128]; |
|
637 static char i_fmt_buf[128]; |
1
|
638 |
2165
|
639 int prec = Voutput_precision; |
1
|
640 |
|
641 int ld, rd; |
|
642 |
|
643 if (bank_format) |
|
644 { |
|
645 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
646 i_fw = 0; |
|
647 r_fw = digits <= 0 ? 4 : digits + 3; |
|
648 if (inf_or_nan && i_fw < 3) |
|
649 i_fw = r_fw = 3; |
|
650 r_fw += sign; |
|
651 rd = 2; |
|
652 } |
1282
|
653 else if (hex_format) |
|
654 { |
|
655 r_fw = 2 * sizeof (double); |
|
656 i_fw = 2 * sizeof (double); |
|
657 rd = 0; |
|
658 } |
1309
|
659 else if (bit_format) |
|
660 { |
|
661 r_fw = 8 * sizeof (double); |
|
662 i_fw = 8 * sizeof (double); |
|
663 rd = 0; |
|
664 } |
1715
|
665 else if (int_or_inf_or_nan) |
1
|
666 { |
|
667 int digits = x_max > x_min ? x_max : x_min; |
|
668 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
669 if (inf_or_nan && i_fw < 3) |
|
670 i_fw = r_fw = 3; |
|
671 r_fw += sign; |
|
672 rd = 0; |
|
673 } |
|
674 else |
|
675 { |
|
676 int ld_max, rd_max; |
|
677 if (x_max > 0) |
|
678 { |
|
679 ld_max = x_max; |
1658
|
680 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
681 x_max++; |
|
682 } |
|
683 else |
|
684 { |
|
685 ld_max = 1; |
1658
|
686 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
687 x_max = -x_max + 1; |
|
688 } |
|
689 |
|
690 int ld_min, rd_min; |
|
691 if (x_min > 0) |
|
692 { |
|
693 ld_min = x_min; |
1658
|
694 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
695 x_min++; |
|
696 } |
|
697 else |
|
698 { |
|
699 ld_min = 1; |
1658
|
700 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
701 x_min = -x_min + 1; |
|
702 } |
|
703 |
|
704 ld = ld_max > ld_min ? ld_max : ld_min; |
|
705 rd = rd_max > rd_min ? rd_max : rd_min; |
|
706 |
|
707 i_fw = r_fw = ld + 1 + rd; |
|
708 if (inf_or_nan && i_fw < 3) |
|
709 i_fw = r_fw = 3; |
|
710 r_fw += sign; |
|
711 } |
|
712 |
1309
|
713 if (! (bank_format || hex_format || bit_format) |
2165
|
714 && (r_fw > Voutput_max_field_width || print_e)) |
1
|
715 { |
|
716 int exp_field = 4; |
|
717 if (x_max > 100 || x_min > 100) |
|
718 exp_field++; |
|
719 |
|
720 i_fw = r_fw = 1 + prec + exp_field; |
|
721 if (inf_or_nan && i_fw < 3) |
|
722 i_fw = r_fw = 3; |
|
723 r_fw += sign; |
|
724 |
|
725 if (print_big_e) |
|
726 { |
|
727 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
728 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
729 } |
|
730 else |
|
731 { |
|
732 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
733 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
734 } |
|
735 } |
|
736 else |
|
737 { |
|
738 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
739 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
740 } |
|
741 |
|
742 curr_real_fmt = &r_fmt_buf[0]; |
|
743 curr_imag_fmt = &i_fmt_buf[0]; |
|
744 } |
|
745 |
1658
|
746 static void |
|
747 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw) |
|
748 { |
|
749 curr_real_fmt = 0; |
|
750 curr_imag_fmt = 0; |
|
751 |
|
752 if (free_format) |
|
753 return; |
|
754 |
|
755 Matrix rp = real (cm); |
|
756 Matrix ip = imag (cm); |
|
757 |
|
758 int sign = any_element_is_negative (rp); |
|
759 |
|
760 int inf_or_nan = any_element_is_inf_or_nan (cm); |
|
761 |
1716
|
762 int int_or_inf_or_nan = (all_elements_are_int_or_inf_or_nan (rp) |
|
763 && all_elements_are_int_or_inf_or_nan (ip)); |
1658
|
764 |
|
765 Matrix r_m_abs = abs (rp); |
|
766 double r_max_abs = pr_max_internal (r_m_abs); |
|
767 double r_min_abs = pr_min_internal (r_m_abs); |
|
768 |
|
769 Matrix i_m_abs = abs (ip); |
|
770 double i_max_abs = pr_max_internal (i_m_abs); |
|
771 double i_min_abs = pr_min_internal (i_m_abs); |
|
772 |
|
773 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
774 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
775 |
|
776 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
777 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
778 |
|
779 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
780 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
781 |
|
782 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
1715
|
783 inf_or_nan, int_or_inf_or_nan, r_fw, i_fw); |
1658
|
784 } |
|
785 |
1
|
786 static int |
164
|
787 all_elements_are_ints (const Range& r) |
1
|
788 { |
1282
|
789 // If the base and increment are ints, the final value in the range |
|
790 // will also be an integer, even if the limit is not. |
1
|
791 |
|
792 double b = r.base (); |
|
793 double i = r.inc (); |
|
794 |
1086
|
795 return (! (xisnan (b) || xisnan (i)) |
|
796 && (double) NINT (b) == b && (double) NINT (i) == i); |
1
|
797 } |
|
798 |
|
799 static inline void |
164
|
800 set_format (const ComplexMatrix& cm) |
1
|
801 { |
|
802 int r_fw, i_fw; |
|
803 set_format (cm, r_fw, i_fw); |
|
804 } |
|
805 |
|
806 static void |
1658
|
807 set_range_format (int sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
808 { |
278
|
809 static char fmt_buf[128]; |
1
|
810 |
2165
|
811 int prec = Voutput_precision; |
1
|
812 |
|
813 int ld, rd; |
|
814 |
|
815 if (bank_format) |
|
816 { |
|
817 int digits = x_max > x_min ? x_max : x_min; |
|
818 fw = sign + digits < 0 ? 4 : digits + 3; |
|
819 rd = 2; |
|
820 } |
1282
|
821 else if (hex_format) |
|
822 { |
|
823 fw = 2 * sizeof (double); |
|
824 rd = 0; |
|
825 } |
1309
|
826 else if (bit_format) |
|
827 { |
|
828 fw = 8 * sizeof (double); |
|
829 rd = 0; |
|
830 } |
1658
|
831 else if (all_ints) |
1
|
832 { |
|
833 int digits = x_max > x_min ? x_max : x_min; |
|
834 fw = sign + digits; |
|
835 rd = 0; |
|
836 } |
|
837 else |
|
838 { |
|
839 int ld_max, rd_max; |
|
840 if (x_max > 0) |
|
841 { |
|
842 ld_max = x_max; |
1658
|
843 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
844 x_max++; |
|
845 } |
|
846 else |
|
847 { |
|
848 ld_max = 1; |
1658
|
849 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
850 x_max = -x_max + 1; |
|
851 } |
|
852 |
|
853 int ld_min, rd_min; |
|
854 if (x_min > 0) |
|
855 { |
|
856 ld_min = x_min; |
1658
|
857 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
858 x_min++; |
|
859 } |
|
860 else |
|
861 { |
|
862 ld_min = 1; |
1658
|
863 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
864 x_min = -x_min + 1; |
|
865 } |
|
866 |
|
867 ld = ld_max > ld_min ? ld_max : ld_min; |
|
868 rd = rd_max > rd_min ? rd_max : rd_min; |
|
869 |
|
870 fw = sign + ld + 1 + rd; |
|
871 } |
|
872 |
1309
|
873 if (! (bank_format || hex_format || bit_format) |
2165
|
874 && (fw > Voutput_max_field_width || print_e)) |
1
|
875 { |
|
876 int exp_field = 4; |
|
877 if (x_max > 100 || x_min > 100) |
|
878 exp_field++; |
|
879 |
|
880 fw = sign + 2 + prec + exp_field; |
|
881 |
|
882 if (print_big_e) |
|
883 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
884 else |
|
885 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
886 } |
|
887 else |
|
888 { |
|
889 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
890 } |
|
891 |
|
892 curr_real_fmt = &fmt_buf[0]; |
|
893 } |
|
894 |
1658
|
895 static void |
|
896 set_format (const Range& r, int& fw) |
|
897 { |
|
898 curr_real_fmt = 0; |
|
899 curr_imag_fmt = 0; |
|
900 |
|
901 if (free_format) |
|
902 return; |
|
903 |
|
904 double r_min = r.base (); |
|
905 double r_max = r.limit (); |
|
906 |
|
907 if (r_max < r_min) |
|
908 { |
|
909 double tmp = r_max; |
|
910 r_max = r_min; |
|
911 r_min = tmp; |
|
912 } |
|
913 |
|
914 int sign = (r_min < 0.0); |
|
915 |
|
916 int all_ints = all_elements_are_ints (r); |
|
917 |
|
918 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
919 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
920 |
|
921 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
922 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
923 |
|
924 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
925 } |
|
926 |
1
|
927 static inline void |
164
|
928 set_format (const Range& r) |
1
|
929 { |
|
930 int fw; |
|
931 set_format (r, fw); |
|
932 } |
|
933 |
1282
|
934 union equiv |
|
935 { |
|
936 double d; |
|
937 unsigned char i[sizeof (double)]; |
|
938 }; |
|
939 |
1309
|
940 #define PRINT_CHAR_BITS(os, c) \ |
|
941 do \ |
|
942 { \ |
|
943 unsigned char ctmp = c; \ |
|
944 char stmp[9]; \ |
1488
|
945 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
946 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
947 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
948 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
949 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
950 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
951 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
952 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
953 stmp[8] = '\0'; \ |
|
954 os.form ("%s", stmp); \ |
|
955 } \ |
|
956 while (0) |
|
957 |
|
958 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
959 do \ |
|
960 { \ |
|
961 unsigned char ctmp = c; \ |
|
962 char stmp[9]; \ |
1488
|
963 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
964 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
965 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
966 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
967 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
968 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
969 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
970 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
971 stmp[8] = '\0'; \ |
|
972 os.form ("%s", stmp); \ |
|
973 } \ |
|
974 while (0) |
|
975 |
1
|
976 static inline void |
626
|
977 pr_any_float (const char *fmt, ostream& os, double d, int fw = 0) |
1
|
978 { |
|
979 if (d == -0.0) |
|
980 d = 0.0; |
|
981 |
529
|
982 if (fmt) |
1
|
983 { |
1282
|
984 if (hex_format) |
|
985 { |
|
986 equiv tmp; |
|
987 tmp.d = d; |
|
988 |
|
989 // Unless explicitly asked for, always print in big-endian |
|
990 // format. |
|
991 |
|
992 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
993 // formats and not for Cray? |
|
994 |
|
995 if (hex_format > 1 |
|
996 || native_float_format == OCTAVE_IEEE_BIG |
|
997 || native_float_format == OCTAVE_CRAY |
|
998 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
999 { |
1322
|
1000 for (size_t i = 0; i < sizeof (double); i++) |
1282
|
1001 os.form ("%02x", (int) tmp.i[i]); |
|
1002 } |
|
1003 else |
|
1004 { |
1328
|
1005 for (int i = sizeof (double) - 1; i >= 0; i--) |
1282
|
1006 os.form ("%02x", (int) tmp.i[i]); |
|
1007 } |
|
1008 } |
1309
|
1009 else if (bit_format) |
|
1010 { |
|
1011 equiv tmp; |
|
1012 tmp.d = d; |
|
1013 |
|
1014 // Unless explicitly asked for, always print in big-endian |
|
1015 // format. |
|
1016 |
|
1017 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
1018 // formats and not for Cray? |
|
1019 |
|
1020 if (native_float_format == OCTAVE_IEEE_BIG |
|
1021 || native_float_format == OCTAVE_CRAY |
|
1022 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
1023 { |
1322
|
1024 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1025 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1026 } |
|
1027 else |
|
1028 { |
|
1029 if (bit_format > 1) |
|
1030 { |
1328
|
1031 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1032 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1033 } |
|
1034 else |
|
1035 { |
|
1036 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1037 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1038 } |
|
1039 } |
|
1040 } |
1282
|
1041 else if (xisinf (d)) |
1
|
1042 { |
|
1043 char *s; |
|
1044 if (d < 0.0) |
|
1045 s = "-Inf"; |
|
1046 else |
|
1047 s = "Inf"; |
|
1048 |
|
1049 if (fw > 0) |
|
1050 os.form ("%*s", fw, s); |
|
1051 else |
|
1052 os << s; |
|
1053 } |
|
1054 else if (xisnan (d)) |
|
1055 { |
|
1056 if (fw > 0) |
|
1057 os.form ("%*s", fw, "NaN"); |
|
1058 else |
|
1059 os << "NaN"; |
|
1060 } |
|
1061 else |
|
1062 os.form (fmt, d); |
|
1063 } |
529
|
1064 else |
|
1065 os << d; |
1
|
1066 } |
|
1067 |
|
1068 static inline void |
626
|
1069 pr_float (ostream& os, double d, int fw = 0) |
1
|
1070 { |
|
1071 pr_any_float (curr_real_fmt, os, d, fw); |
|
1072 } |
|
1073 |
|
1074 static inline void |
626
|
1075 pr_imag_float (ostream& os, double d, int fw = 0) |
1
|
1076 { |
|
1077 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1078 } |
|
1079 |
|
1080 static inline void |
626
|
1081 pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
1082 { |
|
1083 double r = c.real (); |
|
1084 pr_float (os, r, r_fw); |
|
1085 if (! bank_format) |
|
1086 { |
|
1087 double i = c.imag (); |
1309
|
1088 if (! (hex_format || bit_format) && i < 0) |
1
|
1089 { |
|
1090 os << " - "; |
|
1091 i = -i; |
|
1092 pr_imag_float (os, i, i_fw); |
|
1093 } |
|
1094 else |
|
1095 { |
1309
|
1096 if (hex_format || bit_format) |
1282
|
1097 os << " "; |
|
1098 else |
|
1099 os << " + "; |
|
1100 |
1
|
1101 pr_imag_float (os, i, i_fw); |
|
1102 } |
|
1103 os << "i"; |
|
1104 } |
|
1105 } |
|
1106 |
626
|
1107 static void |
1972
|
1108 print_empty_matrix (ostream& os, int nr, int nc, bool pr_as_read_syntax) |
626
|
1109 { |
|
1110 assert (nr == 0 || nc == 0); |
|
1111 |
|
1112 if (pr_as_read_syntax) |
|
1113 { |
|
1114 if (nr == 0 && nc == 0) |
|
1115 os << "[]"; |
|
1116 else |
|
1117 os << "zeros (" << nr << ", " << nc << ")"; |
|
1118 } |
|
1119 else |
|
1120 { |
|
1121 os << "[]"; |
2165
|
1122 if (Vprint_empty_dimensions) |
626
|
1123 os << "(" << nr << "x" << nc << ")"; |
|
1124 os << "\n"; |
|
1125 } |
|
1126 } |
|
1127 |
1186
|
1128 static void |
|
1129 pr_col_num_header (ostream& os, int total_width, int max_width, |
1972
|
1130 int lim, int col, int extra_indent) |
1186
|
1131 { |
2165
|
1132 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1133 { |
|
1134 if (col != 0 && ! compact_format) |
|
1135 os << "\n"; |
|
1136 |
|
1137 int num_cols = lim - col; |
|
1138 |
1972
|
1139 os.form ("%*s", extra_indent, ""); |
|
1140 |
1186
|
1141 if (num_cols == 1) |
|
1142 os << " Column " << col + 1 << ":\n"; |
|
1143 else if (num_cols == 2) |
|
1144 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1145 else |
|
1146 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
|
1147 |
|
1148 if (! compact_format) |
|
1149 os << "\n"; |
|
1150 } |
|
1151 } |
|
1152 |
1
|
1153 void |
1972
|
1154 octave_print_internal (ostream& os, double d, bool pr_as_read_syntax) |
1
|
1155 { |
|
1156 if (plus_format) |
|
1157 { |
|
1158 if (d == 0.0) |
|
1159 os << " "; |
|
1160 else |
|
1161 os << "+"; |
|
1162 } |
|
1163 else |
|
1164 { |
|
1165 set_format (d); |
|
1166 if (free_format) |
|
1167 os << d; |
|
1168 else |
|
1169 pr_float (os, d); |
|
1170 } |
626
|
1171 |
|
1172 if (! pr_as_read_syntax) |
|
1173 os << "\n"; |
1
|
1174 } |
|
1175 |
|
1176 void |
1972
|
1177 octave_print_internal (ostream& os, const Matrix& m, bool pr_as_read_syntax, |
|
1178 int extra_indent) |
1
|
1179 { |
|
1180 int nr = m.rows (); |
|
1181 int nc = m.columns (); |
|
1182 |
626
|
1183 if (nr == 0 || nc == 0) |
|
1184 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1185 else if (plus_format && ! pr_as_read_syntax) |
1
|
1186 { |
|
1187 for (int i = 0; i < nr; i++) |
|
1188 { |
|
1189 for (int j = 0; j < nc; j++) |
|
1190 { |
|
1191 if (j == 0) |
|
1192 os << " "; |
|
1193 |
2305
|
1194 if (m (i, j) == 0.0) |
1
|
1195 os << " "; |
|
1196 else |
|
1197 os << "+"; |
|
1198 } |
|
1199 os << "\n"; |
|
1200 } |
|
1201 } |
|
1202 else |
|
1203 { |
|
1204 int fw; |
|
1205 set_format (m, fw); |
|
1206 int column_width = fw + 2; |
|
1207 int total_width = nc * column_width; |
|
1208 int max_width = terminal_columns (); |
|
1209 |
626
|
1210 if (pr_as_read_syntax) |
|
1211 max_width -= 4; |
1972
|
1212 else |
|
1213 max_width -= extra_indent; |
|
1214 |
|
1215 if (max_width < 0) |
|
1216 max_width = 0; |
626
|
1217 |
1
|
1218 if (free_format) |
|
1219 { |
626
|
1220 if (pr_as_read_syntax) |
|
1221 os << "[\n"; |
|
1222 |
1
|
1223 os << m; |
626
|
1224 |
|
1225 if (pr_as_read_syntax) |
|
1226 os << "]"; |
|
1227 |
1
|
1228 return; |
|
1229 } |
|
1230 |
|
1231 int inc = nc; |
2165
|
1232 if (total_width > max_width && Vsplit_long_rows) |
1
|
1233 { |
|
1234 inc = max_width / column_width; |
|
1235 if (inc == 0) |
|
1236 inc++; |
|
1237 } |
|
1238 |
626
|
1239 if (pr_as_read_syntax) |
1
|
1240 { |
|
1241 for (int i = 0; i < nr; i++) |
|
1242 { |
626
|
1243 int col = 0; |
|
1244 while (col < nc) |
1
|
1245 { |
626
|
1246 int lim = col + inc < nc ? col + inc : nc; |
|
1247 |
|
1248 for (int j = col; j < lim; j++) |
|
1249 { |
|
1250 if (i == 0 && j == 0) |
|
1251 os << "[ "; |
|
1252 else |
|
1253 { |
|
1254 if (j > col && j < lim) |
|
1255 os << ", "; |
|
1256 else |
|
1257 os << " "; |
|
1258 } |
|
1259 |
2305
|
1260 pr_float (os, m (i, j)); |
626
|
1261 } |
|
1262 |
|
1263 col += inc; |
|
1264 |
|
1265 if (col >= nc) |
|
1266 { |
|
1267 if (i == nr - 1) |
|
1268 os << " ]"; |
|
1269 else |
|
1270 os << ";\n"; |
|
1271 } |
|
1272 else |
|
1273 os << " ...\n"; |
1
|
1274 } |
|
1275 } |
626
|
1276 } |
|
1277 else |
|
1278 { |
|
1279 for (int col = 0; col < nc; col += inc) |
|
1280 { |
|
1281 int lim = col + inc < nc ? col + inc : nc; |
|
1282 |
1972
|
1283 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1284 extra_indent); |
626
|
1285 |
|
1286 for (int i = 0; i < nr; i++) |
|
1287 { |
1972
|
1288 os.form ("%*s", extra_indent, ""); |
|
1289 |
626
|
1290 for (int j = col; j < lim; j++) |
|
1291 { |
|
1292 os << " "; |
|
1293 |
2305
|
1294 pr_float (os, m (i, j), fw); |
626
|
1295 } |
|
1296 |
|
1297 os << "\n"; |
|
1298 } |
|
1299 } |
1
|
1300 } |
|
1301 } |
|
1302 } |
|
1303 |
|
1304 void |
626
|
1305 octave_print_internal (ostream& os, const Complex& c, |
1972
|
1306 bool pr_as_read_syntax) |
1
|
1307 { |
|
1308 if (plus_format) |
|
1309 { |
|
1310 if (c == 0.0) |
|
1311 os << " "; |
|
1312 else |
|
1313 os << "+"; |
|
1314 } |
|
1315 else |
|
1316 { |
|
1317 set_format (c); |
|
1318 if (free_format) |
|
1319 os << c; |
|
1320 else |
|
1321 pr_complex (os, c); |
|
1322 } |
626
|
1323 |
|
1324 if (! pr_as_read_syntax) |
|
1325 os << "\n"; |
1
|
1326 } |
|
1327 |
|
1328 void |
626
|
1329 octave_print_internal (ostream& os, const ComplexMatrix& cm, |
1972
|
1330 bool pr_as_read_syntax, int extra_indent) |
1
|
1331 { |
|
1332 int nr = cm.rows (); |
|
1333 int nc = cm.columns (); |
|
1334 |
626
|
1335 if (nr == 0 || nc == 0) |
|
1336 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1337 else if (plus_format && ! pr_as_read_syntax) |
1
|
1338 { |
|
1339 for (int i = 0; i < nr; i++) |
|
1340 { |
|
1341 for (int j = 0; j < nc; j++) |
|
1342 { |
|
1343 if (j == 0) |
|
1344 os << " "; |
|
1345 |
2305
|
1346 if (cm (i, j) == 0.0) |
1
|
1347 os << " "; |
|
1348 else |
|
1349 os << "+"; |
|
1350 } |
|
1351 os << "\n"; |
|
1352 } |
|
1353 } |
|
1354 else |
|
1355 { |
|
1356 int r_fw, i_fw; |
|
1357 set_format (cm, r_fw, i_fw); |
|
1358 int column_width = i_fw + r_fw; |
1309
|
1359 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1360 int total_width = nc * column_width; |
|
1361 int max_width = terminal_columns (); |
|
1362 |
626
|
1363 if (pr_as_read_syntax) |
|
1364 max_width -= 4; |
1972
|
1365 else |
|
1366 max_width -= extra_indent; |
|
1367 |
|
1368 if (max_width < 0) |
|
1369 max_width = 0; |
626
|
1370 |
1
|
1371 if (free_format) |
|
1372 { |
626
|
1373 if (pr_as_read_syntax) |
|
1374 os << "[\n"; |
|
1375 |
1
|
1376 os << cm; |
626
|
1377 |
|
1378 if (pr_as_read_syntax) |
|
1379 os << "]"; |
|
1380 |
1
|
1381 return; |
|
1382 } |
|
1383 |
|
1384 int inc = nc; |
2165
|
1385 if (total_width > max_width && Vsplit_long_rows) |
1
|
1386 { |
|
1387 inc = max_width / column_width; |
|
1388 if (inc == 0) |
|
1389 inc++; |
|
1390 } |
|
1391 |
626
|
1392 if (pr_as_read_syntax) |
1
|
1393 { |
|
1394 for (int i = 0; i < nr; i++) |
|
1395 { |
626
|
1396 int col = 0; |
|
1397 while (col < nc) |
1
|
1398 { |
626
|
1399 int lim = col + inc < nc ? col + inc : nc; |
|
1400 |
|
1401 for (int j = col; j < lim; j++) |
|
1402 { |
|
1403 if (i == 0 && j == 0) |
|
1404 os << "[ "; |
|
1405 else |
|
1406 { |
|
1407 if (j > col && j < lim) |
|
1408 os << ", "; |
|
1409 else |
|
1410 os << " "; |
|
1411 } |
|
1412 |
2305
|
1413 pr_complex (os, cm (i, j)); |
626
|
1414 } |
|
1415 |
|
1416 col += inc; |
|
1417 |
|
1418 if (col >= nc) |
|
1419 { |
|
1420 if (i == nr - 1) |
|
1421 os << " ]"; |
|
1422 else |
|
1423 os << ";\n"; |
|
1424 } |
1
|
1425 else |
626
|
1426 os << " ...\n"; |
1
|
1427 } |
|
1428 } |
626
|
1429 } |
|
1430 else |
|
1431 { |
|
1432 for (int col = 0; col < nc; col += inc) |
|
1433 { |
|
1434 int lim = col + inc < nc ? col + inc : nc; |
|
1435 |
1972
|
1436 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1437 extra_indent); |
626
|
1438 |
|
1439 for (int i = 0; i < nr; i++) |
|
1440 { |
1972
|
1441 os.form ("%*s", extra_indent, ""); |
|
1442 |
626
|
1443 for (int j = col; j < lim; j++) |
|
1444 { |
|
1445 os << " "; |
|
1446 |
2305
|
1447 pr_complex (os, cm (i, j)); |
626
|
1448 } |
|
1449 os << "\n"; |
|
1450 } |
|
1451 } |
1
|
1452 } |
|
1453 } |
|
1454 } |
|
1455 |
|
1456 void |
626
|
1457 octave_print_internal (ostream& os, const Range& r, |
1972
|
1458 bool pr_as_read_syntax, int extra_indent) |
1
|
1459 { |
626
|
1460 double base = r.base (); |
1
|
1461 double increment = r.inc (); |
626
|
1462 double limit = r.limit (); |
1
|
1463 int num_elem = r.nelem (); |
|
1464 |
626
|
1465 if (plus_format && ! pr_as_read_syntax) |
1
|
1466 { |
|
1467 os << " "; |
|
1468 for (int i = 0; i < num_elem; i++) |
|
1469 { |
626
|
1470 double val = base + i * increment; |
1
|
1471 if (val == 0.0) |
|
1472 os << " "; |
|
1473 else |
|
1474 os << "+"; |
|
1475 } |
|
1476 } |
|
1477 else |
|
1478 { |
|
1479 int fw; |
|
1480 set_format (r, fw); |
|
1481 |
626
|
1482 if (pr_as_read_syntax) |
1
|
1483 { |
626
|
1484 if (free_format) |
|
1485 { |
|
1486 os << base << " : "; |
|
1487 if (increment != 1.0) |
|
1488 os << increment << " : "; |
|
1489 os << limit; |
|
1490 } |
|
1491 else |
|
1492 { |
|
1493 pr_float (os, base, fw); |
|
1494 os << " : "; |
|
1495 if (increment != 1.0) |
|
1496 { |
|
1497 pr_float (os, increment, fw); |
|
1498 os << " : "; |
|
1499 } |
|
1500 pr_float (os, limit, fw); |
|
1501 } |
1
|
1502 } |
626
|
1503 else |
|
1504 { |
|
1505 int column_width = fw + 2; |
|
1506 int total_width = num_elem * column_width; |
|
1507 int max_width = terminal_columns (); |
1
|
1508 |
626
|
1509 if (free_format) |
|
1510 { |
|
1511 os << r; |
|
1512 return; |
|
1513 } |
1
|
1514 |
626
|
1515 int inc = num_elem; |
2165
|
1516 if (total_width > max_width && Vsplit_long_rows) |
1
|
1517 { |
626
|
1518 inc = max_width / column_width; |
|
1519 if (inc == 0) |
|
1520 inc++; |
1
|
1521 } |
|
1522 |
1972
|
1523 max_width -= extra_indent; |
|
1524 |
|
1525 if (max_width < 0) |
|
1526 max_width = 0; |
|
1527 |
626
|
1528 int col = 0; |
|
1529 while (col < num_elem) |
1
|
1530 { |
626
|
1531 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1532 |
1972
|
1533 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1534 extra_indent); |
|
1535 |
|
1536 os.form ("%*s", extra_indent, ""); |
626
|
1537 |
|
1538 for (int i = col; i < lim; i++) |
|
1539 { |
|
1540 double val = base + i * increment; |
|
1541 os << " "; |
|
1542 pr_float (os, val, fw); |
|
1543 } |
|
1544 |
|
1545 os << "\n"; |
|
1546 |
|
1547 col += inc; |
1
|
1548 } |
|
1549 } |
|
1550 } |
|
1551 } |
|
1552 |
1343
|
1553 void |
1572
|
1554 octave_print_internal (ostream& os, const charMatrix& chm, |
1972
|
1555 bool pr_as_read_syntax, bool pr_as_string, |
|
1556 int extra_indent) |
1343
|
1557 { |
1572
|
1558 if (pr_as_string) |
|
1559 { |
|
1560 int nstr = chm.rows (); |
1343
|
1561 |
1572
|
1562 if (pr_as_read_syntax && nstr > 1) |
|
1563 os << "[ "; |
1343
|
1564 |
1572
|
1565 for (int i = 0; i < nstr; i++) |
1343
|
1566 { |
1755
|
1567 string row = chm.row_as_string (i); |
1588
|
1568 |
1572
|
1569 if (pr_as_read_syntax) |
|
1570 { |
1755
|
1571 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
1572 |
1572
|
1573 if (i < nstr - 1) |
|
1574 os << "; "; |
|
1575 } |
|
1576 else |
1588
|
1577 os << row << "\n"; |
1343
|
1578 } |
1572
|
1579 |
|
1580 if (pr_as_read_syntax && nstr > 1) |
|
1581 os << " ]"; |
1343
|
1582 } |
1572
|
1583 else |
|
1584 { |
|
1585 os << "sorry, printing char matrices not implemented yet\n"; |
|
1586 } |
1343
|
1587 } |
|
1588 |
1957
|
1589 DEFUN (disp, args, , |
529
|
1590 "disp (X): display value without name tag") |
|
1591 { |
2086
|
1592 octave_value_list retval; |
529
|
1593 |
|
1594 int nargin = args.length (); |
|
1595 |
712
|
1596 if (nargin == 1) |
|
1597 args(0).eval (1); |
529
|
1598 else |
|
1599 print_usage ("disp"); |
|
1600 |
|
1601 return retval; |
|
1602 } |
|
1603 |
1
|
1604 static void |
|
1605 init_format_state (void) |
|
1606 { |
|
1607 free_format = 0; |
|
1608 plus_format = 0; |
|
1609 bank_format = 0; |
1282
|
1610 hex_format = 0; |
1309
|
1611 bit_format = 0; |
1
|
1612 print_e = 0; |
|
1613 print_big_e = 0; |
|
1614 } |
|
1615 |
|
1616 static void |
|
1617 set_output_prec_and_fw (int prec, int fw) |
|
1618 { |
2086
|
1619 octave_value *tmp = 0; |
1
|
1620 |
2086
|
1621 tmp = new octave_value ((double) prec); |
195
|
1622 bind_builtin_variable ("output_precision", tmp); |
1
|
1623 |
2086
|
1624 tmp = new octave_value ((double) fw); |
195
|
1625 bind_builtin_variable ("output_max_field_width", tmp); |
1
|
1626 } |
|
1627 |
1755
|
1628 static void |
|
1629 set_format_style (int argc, const string_vector& argv) |
1
|
1630 { |
1755
|
1631 int idx = 1; |
|
1632 string arg = argv[idx++]; |
|
1633 |
1899
|
1634 if (--argc > 0) |
1
|
1635 { |
1755
|
1636 if (arg == "short") |
1
|
1637 { |
1755
|
1638 if (--argc > 0) |
1
|
1639 { |
1755
|
1640 arg = argv[idx++]; |
|
1641 |
|
1642 if (arg == "e") |
1
|
1643 { |
1755
|
1644 init_format_state (); |
|
1645 print_e = 1; |
|
1646 } |
|
1647 else if (arg == "E") |
|
1648 { |
|
1649 init_format_state (); |
|
1650 print_e = 1; |
|
1651 print_big_e = 1; |
1
|
1652 } |
|
1653 else |
|
1654 { |
1755
|
1655 error ("format: unrecognized option `short %s'", |
|
1656 arg.c_str ()); |
|
1657 return; |
|
1658 } |
|
1659 } |
|
1660 else |
|
1661 init_format_state (); |
|
1662 |
|
1663 set_output_prec_and_fw (3, 8); |
|
1664 } |
|
1665 else if (arg == "long") |
|
1666 { |
|
1667 if (--argc > 0) |
|
1668 { |
|
1669 arg = argv[idx++]; |
|
1670 |
|
1671 if (arg == "e") |
|
1672 { |
|
1673 init_format_state (); |
|
1674 print_e = 1; |
|
1675 } |
|
1676 else if (arg == "E") |
|
1677 { |
|
1678 init_format_state (); |
|
1679 print_e = 1; |
|
1680 print_big_e = 1; |
1
|
1681 } |
|
1682 else |
1755
|
1683 { |
|
1684 error ("format: unrecognized option `long %s'", |
|
1685 arg.c_str ()); |
|
1686 return; |
|
1687 } |
1186
|
1688 } |
1
|
1689 else |
1755
|
1690 init_format_state (); |
|
1691 |
|
1692 set_output_prec_and_fw (15, 24); |
|
1693 } |
|
1694 else if (arg == "hex") |
|
1695 { |
|
1696 init_format_state (); |
|
1697 hex_format = 1; |
|
1698 } |
|
1699 else if (arg == "native-hex") |
|
1700 { |
|
1701 init_format_state (); |
|
1702 hex_format = 2; |
|
1703 } |
|
1704 else if (arg == "bit") |
|
1705 { |
|
1706 init_format_state (); |
|
1707 bit_format = 1; |
|
1708 } |
|
1709 else if (arg == "native-bit") |
|
1710 { |
|
1711 init_format_state (); |
|
1712 bit_format = 2; |
|
1713 } |
|
1714 else if (arg == "+" || arg == "plus") |
|
1715 { |
|
1716 init_format_state (); |
|
1717 plus_format = 1; |
|
1718 } |
|
1719 else if (arg == "bank") |
|
1720 { |
|
1721 init_format_state (); |
|
1722 bank_format = 1; |
|
1723 } |
|
1724 else if (arg == "free") |
|
1725 { |
|
1726 init_format_state (); |
|
1727 free_format = 1; |
|
1728 } |
|
1729 else if (arg == "none") |
|
1730 { |
|
1731 init_format_state (); |
|
1732 free_format = 1; |
|
1733 } |
|
1734 else if (arg == "compact") |
|
1735 { |
|
1736 compact_format = 1; |
|
1737 } |
|
1738 else if (arg == "loose") |
|
1739 { |
|
1740 compact_format = 0; |
1
|
1741 } |
|
1742 else |
1755
|
1743 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
1744 } |
|
1745 else |
|
1746 { |
|
1747 init_format_state (); |
|
1748 set_output_prec_and_fw (5, 10); |
|
1749 } |
|
1750 } |
|
1751 |
1957
|
1752 DEFUN_TEXT (format, args, , |
529
|
1753 "format [style]\n\ |
|
1754 \n\ |
|
1755 set output formatting style") |
|
1756 { |
2086
|
1757 octave_value_list retval; |
529
|
1758 |
1755
|
1759 int argc = args.length () + 1; |
|
1760 |
1968
|
1761 string_vector argv = args.make_argv ("format"); |
1755
|
1762 |
|
1763 if (error_state) |
|
1764 return retval; |
529
|
1765 |
|
1766 set_format_style (argc, argv); |
|
1767 |
|
1768 return retval; |
|
1769 } |
|
1770 |
2165
|
1771 static int |
|
1772 output_max_field_width (void) |
|
1773 { |
|
1774 double val; |
|
1775 if (builtin_real_scalar_variable ("output_max_field_width", val) |
|
1776 && ! xisnan (val)) |
|
1777 { |
|
1778 int ival = NINT (val); |
|
1779 if (ival > 0 && (double) ival == val) |
|
1780 { |
|
1781 Voutput_max_field_width = ival; |
|
1782 return 0; |
|
1783 } |
|
1784 } |
|
1785 gripe_invalid_value_specified ("output_max_field_width"); |
|
1786 return -1; |
|
1787 } |
|
1788 |
|
1789 static int |
|
1790 output_precision (void) |
|
1791 { |
|
1792 double val; |
|
1793 if (builtin_real_scalar_variable ("output_precision", val) |
|
1794 && ! xisnan (val)) |
|
1795 { |
|
1796 int ival = NINT (val); |
|
1797 if (ival >= 0 && (double) ival == val) |
|
1798 { |
|
1799 Voutput_precision = ival; |
|
1800 return 0; |
|
1801 } |
|
1802 } |
|
1803 gripe_invalid_value_specified ("output_precision"); |
|
1804 return -1; |
|
1805 } |
|
1806 |
|
1807 static int |
|
1808 print_empty_dimensions (void) |
|
1809 { |
|
1810 Vprint_empty_dimensions = check_preference ("print_empty_dimensions"); |
|
1811 |
|
1812 return 0; |
|
1813 } |
|
1814 |
|
1815 static int |
|
1816 split_long_rows (void) |
|
1817 { |
|
1818 Vsplit_long_rows = check_preference ("split_long_rows"); |
|
1819 |
|
1820 return 0; |
|
1821 } |
|
1822 |
|
1823 void |
|
1824 symbols_of_pr_output (void) |
|
1825 { |
|
1826 DEFVAR (output_max_field_width, 10.0, 0, output_max_field_width, |
|
1827 "maximum width of an output field for numeric output"); |
|
1828 |
|
1829 DEFVAR (output_precision, 5.0, 0, output_precision, |
|
1830 "number of significant figures to display for numeric output"); |
|
1831 |
|
1832 DEFVAR (print_empty_dimensions, 1.0, 0, print_empty_dimensions, |
|
1833 "also print dimensions of empty matrices"); |
|
1834 |
|
1835 DEFVAR (split_long_rows, 1.0, 0, split_long_rows, |
|
1836 "split long matrix rows instead of wrapping"); |
|
1837 } |
|
1838 |
1
|
1839 /* |
|
1840 ;;; Local Variables: *** |
|
1841 ;;; mode: C++ *** |
|
1842 ;;; End: *** |
|
1843 */ |