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