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