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 |
1716
|
741 int int_or_inf_or_nan = (all_elements_are_int_or_inf_or_nan (rp) |
|
742 && all_elements_are_int_or_inf_or_nan (ip)); |
1658
|
743 |
|
744 Matrix r_m_abs = abs (rp); |
|
745 double r_max_abs = pr_max_internal (r_m_abs); |
|
746 double r_min_abs = pr_min_internal (r_m_abs); |
|
747 |
|
748 Matrix i_m_abs = abs (ip); |
|
749 double i_max_abs = pr_max_internal (i_m_abs); |
|
750 double i_min_abs = pr_min_internal (i_m_abs); |
|
751 |
|
752 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
753 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
754 |
|
755 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
756 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
757 |
|
758 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
759 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
760 |
|
761 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
1715
|
762 inf_or_nan, int_or_inf_or_nan, r_fw, i_fw); |
1658
|
763 } |
|
764 |
1
|
765 static int |
164
|
766 all_elements_are_ints (const Range& r) |
1
|
767 { |
1282
|
768 // If the base and increment are ints, the final value in the range |
|
769 // will also be an integer, even if the limit is not. |
1
|
770 |
|
771 double b = r.base (); |
|
772 double i = r.inc (); |
|
773 |
1086
|
774 return (! (xisnan (b) || xisnan (i)) |
|
775 && (double) NINT (b) == b && (double) NINT (i) == i); |
1
|
776 } |
|
777 |
|
778 static inline void |
164
|
779 set_format (const ComplexMatrix& cm) |
1
|
780 { |
|
781 int r_fw, i_fw; |
|
782 set_format (cm, r_fw, i_fw); |
|
783 } |
|
784 |
|
785 static void |
1658
|
786 set_range_format (int sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
787 { |
278
|
788 static char fmt_buf[128]; |
1
|
789 |
|
790 int prec = user_pref.output_precision; |
|
791 |
|
792 int ld, rd; |
|
793 |
|
794 if (bank_format) |
|
795 { |
|
796 int digits = x_max > x_min ? x_max : x_min; |
|
797 fw = sign + digits < 0 ? 4 : digits + 3; |
|
798 rd = 2; |
|
799 } |
1282
|
800 else if (hex_format) |
|
801 { |
|
802 fw = 2 * sizeof (double); |
|
803 rd = 0; |
|
804 } |
1309
|
805 else if (bit_format) |
|
806 { |
|
807 fw = 8 * sizeof (double); |
|
808 rd = 0; |
|
809 } |
1658
|
810 else if (all_ints) |
1
|
811 { |
|
812 int digits = x_max > x_min ? x_max : x_min; |
|
813 fw = sign + digits; |
|
814 rd = 0; |
|
815 } |
|
816 else |
|
817 { |
|
818 int ld_max, rd_max; |
|
819 if (x_max > 0) |
|
820 { |
|
821 ld_max = x_max; |
1658
|
822 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
823 x_max++; |
|
824 } |
|
825 else |
|
826 { |
|
827 ld_max = 1; |
1658
|
828 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
829 x_max = -x_max + 1; |
|
830 } |
|
831 |
|
832 int ld_min, rd_min; |
|
833 if (x_min > 0) |
|
834 { |
|
835 ld_min = x_min; |
1658
|
836 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
837 x_min++; |
|
838 } |
|
839 else |
|
840 { |
|
841 ld_min = 1; |
1658
|
842 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
843 x_min = -x_min + 1; |
|
844 } |
|
845 |
|
846 ld = ld_max > ld_min ? ld_max : ld_min; |
|
847 rd = rd_max > rd_min ? rd_max : rd_min; |
|
848 |
|
849 fw = sign + ld + 1 + rd; |
|
850 } |
|
851 |
1309
|
852 if (! (bank_format || hex_format || bit_format) |
1282
|
853 && (fw > user_pref.output_max_field_width || print_e)) |
1
|
854 { |
|
855 int exp_field = 4; |
|
856 if (x_max > 100 || x_min > 100) |
|
857 exp_field++; |
|
858 |
|
859 fw = sign + 2 + prec + exp_field; |
|
860 |
|
861 if (print_big_e) |
|
862 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
863 else |
|
864 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
865 } |
|
866 else |
|
867 { |
|
868 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
869 } |
|
870 |
|
871 curr_real_fmt = &fmt_buf[0]; |
|
872 } |
|
873 |
1658
|
874 static void |
|
875 set_format (const Range& r, int& fw) |
|
876 { |
|
877 curr_real_fmt = 0; |
|
878 curr_imag_fmt = 0; |
|
879 |
|
880 if (free_format) |
|
881 return; |
|
882 |
|
883 double r_min = r.base (); |
|
884 double r_max = r.limit (); |
|
885 |
|
886 if (r_max < r_min) |
|
887 { |
|
888 double tmp = r_max; |
|
889 r_max = r_min; |
|
890 r_min = tmp; |
|
891 } |
|
892 |
|
893 int sign = (r_min < 0.0); |
|
894 |
|
895 int all_ints = all_elements_are_ints (r); |
|
896 |
|
897 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
898 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
899 |
|
900 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
901 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
902 |
|
903 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
904 } |
|
905 |
1
|
906 static inline void |
164
|
907 set_format (const Range& r) |
1
|
908 { |
|
909 int fw; |
|
910 set_format (r, fw); |
|
911 } |
|
912 |
1282
|
913 union equiv |
|
914 { |
|
915 double d; |
|
916 unsigned char i[sizeof (double)]; |
|
917 }; |
|
918 |
1309
|
919 #define PRINT_CHAR_BITS(os, c) \ |
|
920 do \ |
|
921 { \ |
|
922 unsigned char ctmp = c; \ |
|
923 char stmp[9]; \ |
1488
|
924 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
925 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
926 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
927 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
928 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
929 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
930 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
931 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
932 stmp[8] = '\0'; \ |
|
933 os.form ("%s", stmp); \ |
|
934 } \ |
|
935 while (0) |
|
936 |
|
937 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
938 do \ |
|
939 { \ |
|
940 unsigned char ctmp = c; \ |
|
941 char stmp[9]; \ |
1488
|
942 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
943 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
944 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
945 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
946 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
947 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
948 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
949 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
950 stmp[8] = '\0'; \ |
|
951 os.form ("%s", stmp); \ |
|
952 } \ |
|
953 while (0) |
|
954 |
1
|
955 static inline void |
626
|
956 pr_any_float (const char *fmt, ostream& os, double d, int fw = 0) |
1
|
957 { |
|
958 if (d == -0.0) |
|
959 d = 0.0; |
|
960 |
529
|
961 if (fmt) |
1
|
962 { |
1282
|
963 if (hex_format) |
|
964 { |
|
965 equiv tmp; |
|
966 tmp.d = d; |
|
967 |
|
968 // Unless explicitly asked for, always print in big-endian |
|
969 // format. |
|
970 |
|
971 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
972 // formats and not for Cray? |
|
973 |
|
974 if (hex_format > 1 |
|
975 || native_float_format == OCTAVE_IEEE_BIG |
|
976 || native_float_format == OCTAVE_CRAY |
|
977 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
978 { |
1322
|
979 for (size_t i = 0; i < sizeof (double); i++) |
1282
|
980 os.form ("%02x", (int) tmp.i[i]); |
|
981 } |
|
982 else |
|
983 { |
1328
|
984 for (int i = sizeof (double) - 1; i >= 0; i--) |
1282
|
985 os.form ("%02x", (int) tmp.i[i]); |
|
986 } |
|
987 } |
1309
|
988 else if (bit_format) |
|
989 { |
|
990 equiv tmp; |
|
991 tmp.d = d; |
|
992 |
|
993 // Unless explicitly asked for, always print in big-endian |
|
994 // format. |
|
995 |
|
996 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
997 // formats and not for Cray? |
|
998 |
|
999 if (native_float_format == OCTAVE_IEEE_BIG |
|
1000 || native_float_format == OCTAVE_CRAY |
|
1001 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
1002 { |
1322
|
1003 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1004 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1005 } |
|
1006 else |
|
1007 { |
|
1008 if (bit_format > 1) |
|
1009 { |
1328
|
1010 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1011 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1012 } |
|
1013 else |
|
1014 { |
|
1015 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1016 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1017 } |
|
1018 } |
|
1019 } |
1282
|
1020 else if (xisinf (d)) |
1
|
1021 { |
|
1022 char *s; |
|
1023 if (d < 0.0) |
|
1024 s = "-Inf"; |
|
1025 else |
|
1026 s = "Inf"; |
|
1027 |
|
1028 if (fw > 0) |
|
1029 os.form ("%*s", fw, s); |
|
1030 else |
|
1031 os << s; |
|
1032 } |
|
1033 else if (xisnan (d)) |
|
1034 { |
|
1035 if (fw > 0) |
|
1036 os.form ("%*s", fw, "NaN"); |
|
1037 else |
|
1038 os << "NaN"; |
|
1039 } |
|
1040 else |
|
1041 os.form (fmt, d); |
|
1042 } |
529
|
1043 else |
|
1044 os << d; |
1
|
1045 } |
|
1046 |
|
1047 static inline void |
626
|
1048 pr_float (ostream& os, double d, int fw = 0) |
1
|
1049 { |
|
1050 pr_any_float (curr_real_fmt, os, d, fw); |
|
1051 } |
|
1052 |
|
1053 static inline void |
626
|
1054 pr_imag_float (ostream& os, double d, int fw = 0) |
1
|
1055 { |
|
1056 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1057 } |
|
1058 |
|
1059 static inline void |
626
|
1060 pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
1061 { |
|
1062 double r = c.real (); |
|
1063 pr_float (os, r, r_fw); |
|
1064 if (! bank_format) |
|
1065 { |
|
1066 double i = c.imag (); |
1309
|
1067 if (! (hex_format || bit_format) && i < 0) |
1
|
1068 { |
|
1069 os << " - "; |
|
1070 i = -i; |
|
1071 pr_imag_float (os, i, i_fw); |
|
1072 } |
|
1073 else |
|
1074 { |
1309
|
1075 if (hex_format || bit_format) |
1282
|
1076 os << " "; |
|
1077 else |
|
1078 os << " + "; |
|
1079 |
1
|
1080 pr_imag_float (os, i, i_fw); |
|
1081 } |
|
1082 os << "i"; |
|
1083 } |
|
1084 } |
|
1085 |
626
|
1086 static void |
|
1087 print_empty_matrix (ostream& os, int nr, int nc, int pr_as_read_syntax) |
|
1088 { |
|
1089 assert (nr == 0 || nc == 0); |
|
1090 |
|
1091 if (pr_as_read_syntax) |
|
1092 { |
|
1093 if (nr == 0 && nc == 0) |
|
1094 os << "[]"; |
|
1095 else |
|
1096 os << "zeros (" << nr << ", " << nc << ")"; |
|
1097 } |
|
1098 else |
|
1099 { |
|
1100 os << "[]"; |
|
1101 if (user_pref.print_empty_dimensions) |
|
1102 os << "(" << nr << "x" << nc << ")"; |
|
1103 os << "\n"; |
|
1104 } |
|
1105 } |
|
1106 |
1186
|
1107 static void |
|
1108 pr_col_num_header (ostream& os, int total_width, int max_width, |
|
1109 int lim, int col) |
|
1110 { |
|
1111 if (total_width > max_width && user_pref.split_long_rows) |
|
1112 { |
|
1113 if (col != 0 && ! compact_format) |
|
1114 os << "\n"; |
|
1115 |
|
1116 int num_cols = lim - col; |
|
1117 |
|
1118 if (num_cols == 1) |
|
1119 os << " Column " << col + 1 << ":\n"; |
|
1120 else if (num_cols == 2) |
|
1121 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1122 else |
|
1123 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
|
1124 |
|
1125 if (! compact_format) |
|
1126 os << "\n"; |
|
1127 } |
|
1128 } |
|
1129 |
1
|
1130 void |
719
|
1131 octave_print_internal (ostream& os, double d, int pr_as_read_syntax) |
1
|
1132 { |
|
1133 if (plus_format) |
|
1134 { |
|
1135 if (d == 0.0) |
|
1136 os << " "; |
|
1137 else |
|
1138 os << "+"; |
|
1139 } |
|
1140 else |
|
1141 { |
|
1142 set_format (d); |
|
1143 if (free_format) |
|
1144 os << d; |
|
1145 else |
|
1146 pr_float (os, d); |
|
1147 } |
626
|
1148 |
|
1149 if (! pr_as_read_syntax) |
|
1150 os << "\n"; |
1
|
1151 } |
|
1152 |
|
1153 void |
719
|
1154 octave_print_internal (ostream& os, const Matrix& m, int pr_as_read_syntax) |
1
|
1155 { |
|
1156 int nr = m.rows (); |
|
1157 int nc = m.columns (); |
|
1158 |
626
|
1159 if (nr == 0 || nc == 0) |
|
1160 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1161 else if (plus_format && ! pr_as_read_syntax) |
1
|
1162 { |
|
1163 for (int i = 0; i < nr; i++) |
|
1164 { |
|
1165 for (int j = 0; j < nc; j++) |
|
1166 { |
|
1167 if (j == 0) |
|
1168 os << " "; |
|
1169 |
|
1170 if (m.elem (i, j) == 0.0) |
|
1171 os << " "; |
|
1172 else |
|
1173 os << "+"; |
|
1174 } |
|
1175 os << "\n"; |
|
1176 } |
|
1177 } |
|
1178 else |
|
1179 { |
|
1180 int fw; |
|
1181 set_format (m, fw); |
|
1182 int column_width = fw + 2; |
|
1183 int total_width = nc * column_width; |
|
1184 int max_width = terminal_columns (); |
|
1185 |
626
|
1186 if (pr_as_read_syntax) |
|
1187 max_width -= 4; |
|
1188 |
1
|
1189 if (free_format) |
|
1190 { |
626
|
1191 if (pr_as_read_syntax) |
|
1192 os << "[\n"; |
|
1193 |
1
|
1194 os << m; |
626
|
1195 |
|
1196 if (pr_as_read_syntax) |
|
1197 os << "]"; |
|
1198 |
1
|
1199 return; |
|
1200 } |
|
1201 |
|
1202 int inc = nc; |
|
1203 if (total_width > max_width && user_pref.split_long_rows) |
|
1204 { |
|
1205 inc = max_width / column_width; |
|
1206 if (inc == 0) |
|
1207 inc++; |
|
1208 } |
|
1209 |
626
|
1210 if (pr_as_read_syntax) |
1
|
1211 { |
|
1212 for (int i = 0; i < nr; i++) |
|
1213 { |
626
|
1214 int col = 0; |
|
1215 while (col < nc) |
1
|
1216 { |
626
|
1217 int lim = col + inc < nc ? col + inc : nc; |
|
1218 |
|
1219 for (int j = col; j < lim; j++) |
|
1220 { |
|
1221 if (i == 0 && j == 0) |
|
1222 os << "[ "; |
|
1223 else |
|
1224 { |
|
1225 if (j > col && j < lim) |
|
1226 os << ", "; |
|
1227 else |
|
1228 os << " "; |
|
1229 } |
|
1230 |
|
1231 pr_float (os, m.elem (i, j)); |
|
1232 } |
|
1233 |
|
1234 col += inc; |
|
1235 |
|
1236 if (col >= nc) |
|
1237 { |
|
1238 if (i == nr - 1) |
|
1239 os << " ]"; |
|
1240 else |
|
1241 os << ";\n"; |
|
1242 } |
|
1243 else |
|
1244 os << " ...\n"; |
1
|
1245 } |
|
1246 } |
626
|
1247 } |
|
1248 else |
|
1249 { |
|
1250 for (int col = 0; col < nc; col += inc) |
|
1251 { |
|
1252 int lim = col + inc < nc ? col + inc : nc; |
|
1253 |
1186
|
1254 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1255 |
|
1256 for (int i = 0; i < nr; i++) |
|
1257 { |
|
1258 for (int j = col; j < lim; j++) |
|
1259 { |
|
1260 os << " "; |
|
1261 |
|
1262 pr_float (os, m.elem (i, j), fw); |
|
1263 } |
|
1264 |
|
1265 os << "\n"; |
|
1266 } |
|
1267 } |
1
|
1268 } |
|
1269 } |
|
1270 } |
|
1271 |
|
1272 void |
626
|
1273 octave_print_internal (ostream& os, const Complex& c, |
|
1274 int pr_as_read_syntax) |
1
|
1275 { |
|
1276 if (plus_format) |
|
1277 { |
|
1278 if (c == 0.0) |
|
1279 os << " "; |
|
1280 else |
|
1281 os << "+"; |
|
1282 } |
|
1283 else |
|
1284 { |
|
1285 set_format (c); |
|
1286 if (free_format) |
|
1287 os << c; |
|
1288 else |
|
1289 pr_complex (os, c); |
|
1290 } |
626
|
1291 |
|
1292 if (! pr_as_read_syntax) |
|
1293 os << "\n"; |
1
|
1294 } |
|
1295 |
|
1296 void |
626
|
1297 octave_print_internal (ostream& os, const ComplexMatrix& cm, |
|
1298 int pr_as_read_syntax) |
1
|
1299 { |
|
1300 int nr = cm.rows (); |
|
1301 int nc = cm.columns (); |
|
1302 |
626
|
1303 if (nr == 0 || nc == 0) |
|
1304 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1305 else if (plus_format && ! pr_as_read_syntax) |
1
|
1306 { |
|
1307 for (int i = 0; i < nr; i++) |
|
1308 { |
|
1309 for (int j = 0; j < nc; j++) |
|
1310 { |
|
1311 if (j == 0) |
|
1312 os << " "; |
|
1313 |
|
1314 if (cm.elem (i, j) == 0.0) |
|
1315 os << " "; |
|
1316 else |
|
1317 os << "+"; |
|
1318 } |
|
1319 os << "\n"; |
|
1320 } |
|
1321 } |
|
1322 else |
|
1323 { |
|
1324 int r_fw, i_fw; |
|
1325 set_format (cm, r_fw, i_fw); |
|
1326 int column_width = i_fw + r_fw; |
1309
|
1327 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1328 int total_width = nc * column_width; |
|
1329 int max_width = terminal_columns (); |
|
1330 |
626
|
1331 if (pr_as_read_syntax) |
|
1332 max_width -= 4; |
|
1333 |
1
|
1334 if (free_format) |
|
1335 { |
626
|
1336 if (pr_as_read_syntax) |
|
1337 os << "[\n"; |
|
1338 |
1
|
1339 os << cm; |
626
|
1340 |
|
1341 if (pr_as_read_syntax) |
|
1342 os << "]"; |
|
1343 |
1
|
1344 return; |
|
1345 } |
|
1346 |
|
1347 int inc = nc; |
|
1348 if (total_width > max_width && user_pref.split_long_rows) |
|
1349 { |
|
1350 inc = max_width / column_width; |
|
1351 if (inc == 0) |
|
1352 inc++; |
|
1353 } |
|
1354 |
626
|
1355 if (pr_as_read_syntax) |
1
|
1356 { |
|
1357 for (int i = 0; i < nr; i++) |
|
1358 { |
626
|
1359 int col = 0; |
|
1360 while (col < nc) |
1
|
1361 { |
626
|
1362 int lim = col + inc < nc ? col + inc : nc; |
|
1363 |
|
1364 for (int j = col; j < lim; j++) |
|
1365 { |
|
1366 if (i == 0 && j == 0) |
|
1367 os << "[ "; |
|
1368 else |
|
1369 { |
|
1370 if (j > col && j < lim) |
|
1371 os << ", "; |
|
1372 else |
|
1373 os << " "; |
|
1374 } |
|
1375 |
|
1376 pr_complex (os, cm.elem (i, j)); |
|
1377 } |
|
1378 |
|
1379 col += inc; |
|
1380 |
|
1381 if (col >= nc) |
|
1382 { |
|
1383 if (i == nr - 1) |
|
1384 os << " ]"; |
|
1385 else |
|
1386 os << ";\n"; |
|
1387 } |
1
|
1388 else |
626
|
1389 os << " ...\n"; |
1
|
1390 } |
|
1391 } |
626
|
1392 } |
|
1393 else |
|
1394 { |
|
1395 for (int col = 0; col < nc; col += inc) |
|
1396 { |
|
1397 int lim = col + inc < nc ? col + inc : nc; |
|
1398 |
1186
|
1399 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1400 |
|
1401 for (int i = 0; i < nr; i++) |
|
1402 { |
|
1403 for (int j = col; j < lim; j++) |
|
1404 { |
|
1405 os << " "; |
|
1406 |
|
1407 pr_complex (os, cm.elem (i, j)); |
|
1408 } |
|
1409 os << "\n"; |
|
1410 } |
|
1411 } |
1
|
1412 } |
|
1413 } |
|
1414 } |
|
1415 |
|
1416 void |
626
|
1417 octave_print_internal (ostream& os, const Range& r, |
|
1418 int pr_as_read_syntax) |
1
|
1419 { |
626
|
1420 double base = r.base (); |
1
|
1421 double increment = r.inc (); |
626
|
1422 double limit = r.limit (); |
1
|
1423 int num_elem = r.nelem (); |
|
1424 |
626
|
1425 if (plus_format && ! pr_as_read_syntax) |
1
|
1426 { |
|
1427 os << " "; |
|
1428 for (int i = 0; i < num_elem; i++) |
|
1429 { |
626
|
1430 double val = base + i * increment; |
1
|
1431 if (val == 0.0) |
|
1432 os << " "; |
|
1433 else |
|
1434 os << "+"; |
|
1435 } |
|
1436 } |
|
1437 else |
|
1438 { |
|
1439 int fw; |
|
1440 set_format (r, fw); |
|
1441 |
626
|
1442 if (pr_as_read_syntax) |
1
|
1443 { |
626
|
1444 if (free_format) |
|
1445 { |
|
1446 os << base << " : "; |
|
1447 if (increment != 1.0) |
|
1448 os << increment << " : "; |
|
1449 os << limit; |
|
1450 } |
|
1451 else |
|
1452 { |
|
1453 pr_float (os, base, fw); |
|
1454 os << " : "; |
|
1455 if (increment != 1.0) |
|
1456 { |
|
1457 pr_float (os, increment, fw); |
|
1458 os << " : "; |
|
1459 } |
|
1460 pr_float (os, limit, fw); |
|
1461 } |
1
|
1462 } |
626
|
1463 else |
|
1464 { |
|
1465 int column_width = fw + 2; |
|
1466 int total_width = num_elem * column_width; |
|
1467 int max_width = terminal_columns (); |
1
|
1468 |
626
|
1469 if (free_format) |
|
1470 { |
|
1471 os << r; |
|
1472 return; |
|
1473 } |
1
|
1474 |
626
|
1475 int inc = num_elem; |
1
|
1476 if (total_width > max_width && user_pref.split_long_rows) |
|
1477 { |
626
|
1478 inc = max_width / column_width; |
|
1479 if (inc == 0) |
|
1480 inc++; |
1
|
1481 } |
|
1482 |
626
|
1483 int col = 0; |
|
1484 while (col < num_elem) |
1
|
1485 { |
626
|
1486 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1487 |
1186
|
1488 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1489 |
|
1490 for (int i = col; i < lim; i++) |
|
1491 { |
|
1492 double val = base + i * increment; |
|
1493 os << " "; |
|
1494 pr_float (os, val, fw); |
|
1495 } |
|
1496 |
|
1497 os << "\n"; |
|
1498 |
|
1499 col += inc; |
1
|
1500 } |
|
1501 } |
|
1502 } |
|
1503 } |
|
1504 |
1343
|
1505 void |
1572
|
1506 octave_print_internal (ostream& os, const charMatrix& chm, |
|
1507 int pr_as_read_syntax, int pr_as_string) |
1343
|
1508 { |
1572
|
1509 if (pr_as_string) |
|
1510 { |
|
1511 int nstr = chm.rows (); |
1343
|
1512 |
1572
|
1513 if (pr_as_read_syntax && nstr > 1) |
|
1514 os << "[ "; |
1343
|
1515 |
1572
|
1516 for (int i = 0; i < nstr; i++) |
1343
|
1517 { |
1588
|
1518 char *row = chm.row_as_string (i); |
|
1519 |
1572
|
1520 if (pr_as_read_syntax) |
|
1521 { |
1588
|
1522 char *tmp = undo_string_escapes (row); |
|
1523 |
|
1524 os << "\"" << tmp << "\""; |
|
1525 |
|
1526 delete [] tmp; |
1343
|
1527 |
1572
|
1528 if (i < nstr - 1) |
|
1529 os << "; "; |
|
1530 } |
|
1531 else |
1588
|
1532 os << row << "\n"; |
|
1533 |
|
1534 delete [] row; |
1343
|
1535 } |
1572
|
1536 |
|
1537 if (pr_as_read_syntax && nstr > 1) |
|
1538 os << " ]"; |
1343
|
1539 } |
1572
|
1540 else |
|
1541 { |
|
1542 os << "sorry, printing char matrices not implemented yet\n"; |
|
1543 } |
1343
|
1544 } |
|
1545 |
1488
|
1546 DEFUN ("disp", Fdisp, Sdisp, 10, |
529
|
1547 "disp (X): display value without name tag") |
|
1548 { |
|
1549 Octave_object retval; |
|
1550 |
|
1551 int nargin = args.length (); |
|
1552 |
712
|
1553 if (nargin == 1) |
|
1554 args(0).eval (1); |
529
|
1555 else |
|
1556 print_usage ("disp"); |
|
1557 |
|
1558 return retval; |
|
1559 } |
|
1560 |
1
|
1561 static void |
|
1562 init_format_state (void) |
|
1563 { |
|
1564 free_format = 0; |
|
1565 plus_format = 0; |
|
1566 bank_format = 0; |
1282
|
1567 hex_format = 0; |
1309
|
1568 bit_format = 0; |
1
|
1569 print_e = 0; |
|
1570 print_big_e = 0; |
|
1571 } |
|
1572 |
|
1573 static void |
|
1574 set_output_prec_and_fw (int prec, int fw) |
|
1575 { |
529
|
1576 tree_constant *tmp = 0; |
1
|
1577 |
|
1578 tmp = new tree_constant ((double) prec); |
195
|
1579 bind_builtin_variable ("output_precision", tmp); |
1
|
1580 |
|
1581 tmp = new tree_constant ((double) fw); |
195
|
1582 bind_builtin_variable ("output_max_field_width", tmp); |
1
|
1583 } |
|
1584 |
|
1585 void |
|
1586 set_format_style (int argc, char **argv) |
|
1587 { |
|
1588 if (--argc > 0) |
|
1589 { |
|
1590 argv++; |
|
1591 if (*argv[0]) |
|
1592 { |
|
1593 if (strcmp (*argv, "short") == 0) |
|
1594 { |
|
1595 if (--argc > 0) |
|
1596 { |
|
1597 argv++; |
|
1598 if (strcmp (*argv, "e") == 0) |
|
1599 { |
|
1600 init_format_state (); |
|
1601 print_e = 1; |
|
1602 } |
|
1603 else if (strcmp (*argv, "E") == 0) |
|
1604 { |
|
1605 init_format_state (); |
|
1606 print_e = 1; |
|
1607 print_big_e = 1; |
|
1608 } |
|
1609 else |
|
1610 { |
217
|
1611 error ("format: unrecognized option `short %s'", *argv); |
1
|
1612 return; |
|
1613 } |
|
1614 } |
|
1615 else |
|
1616 init_format_state (); |
|
1617 |
|
1618 set_output_prec_and_fw (3, 8); |
|
1619 } |
|
1620 else if (strcmp (*argv, "long") == 0) |
|
1621 { |
|
1622 if (--argc > 0) |
|
1623 { |
|
1624 argv++; |
|
1625 if (strcmp (*argv, "e") == 0) |
|
1626 { |
|
1627 init_format_state (); |
|
1628 print_e = 1; |
|
1629 } |
|
1630 else if (strcmp (*argv, "E") == 0) |
|
1631 { |
|
1632 init_format_state (); |
|
1633 print_e = 1; |
|
1634 print_big_e = 1; |
|
1635 } |
|
1636 else |
|
1637 { |
217
|
1638 error ("format: unrecognized option `long %s'", *argv); |
1
|
1639 return; |
|
1640 } |
|
1641 } |
|
1642 else |
|
1643 init_format_state (); |
|
1644 |
|
1645 set_output_prec_and_fw (15, 24); |
|
1646 } |
|
1647 else if (strcmp (*argv, "hex") == 0) |
1282
|
1648 { |
|
1649 init_format_state (); |
|
1650 hex_format = 1; |
|
1651 } |
|
1652 else if (strcmp (*argv, "native-hex") == 0) |
|
1653 { |
|
1654 init_format_state (); |
|
1655 hex_format = 2; |
|
1656 } |
1309
|
1657 else if (strcmp (*argv, "bit") == 0) |
|
1658 { |
|
1659 init_format_state (); |
|
1660 bit_format = 1; |
|
1661 } |
|
1662 else if (strcmp (*argv, "native-bit") == 0) |
|
1663 { |
|
1664 init_format_state (); |
|
1665 bit_format = 2; |
|
1666 } |
1282
|
1667 else if (strcmp (*argv, "+") == 0 || strcmp (*argv, "plus") == 0) |
1
|
1668 { |
|
1669 init_format_state (); |
|
1670 plus_format = 1; |
|
1671 } |
|
1672 else if (strcmp (*argv, "bank") == 0) |
|
1673 { |
|
1674 init_format_state (); |
|
1675 bank_format = 1; |
|
1676 } |
|
1677 else if (strcmp (*argv, "free") == 0) |
|
1678 { |
|
1679 init_format_state (); |
|
1680 free_format = 1; |
|
1681 } |
|
1682 else if (strcmp (*argv, "none") == 0) |
|
1683 { |
|
1684 init_format_state (); |
|
1685 free_format = 1; |
|
1686 } |
|
1687 else if (strcmp (*argv, "compact") == 0) |
1186
|
1688 { |
|
1689 compact_format = 1; |
|
1690 } |
1
|
1691 else if (strcmp (*argv, "loose") == 0) |
1186
|
1692 { |
|
1693 compact_format = 0; |
|
1694 } |
1
|
1695 else |
217
|
1696 error ("format: unrecognized format state `%s'", *argv); |
1
|
1697 } |
|
1698 else |
|
1699 usage ("format [format_state]"); |
|
1700 } |
|
1701 else |
|
1702 { |
|
1703 init_format_state (); |
|
1704 set_output_prec_and_fw (5, 10); |
|
1705 } |
|
1706 } |
|
1707 |
1488
|
1708 DEFUN_TEXT ("format", Fformat, Sformat, 10, |
529
|
1709 "format [style]\n\ |
|
1710 \n\ |
|
1711 set output formatting style") |
|
1712 { |
|
1713 Octave_object retval; |
|
1714 |
|
1715 DEFINE_ARGV("format"); |
|
1716 |
|
1717 set_format_style (argc, argv); |
|
1718 |
|
1719 DELETE_ARGV; |
|
1720 |
|
1721 return retval; |
|
1722 } |
|
1723 |
1
|
1724 /* |
|
1725 ;;; Local Variables: *** |
|
1726 ;;; mode: C++ *** |
|
1727 ;;; page-delimiter: "^/\\*" *** |
|
1728 ;;; End: *** |
|
1729 */ |