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