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, |
|
315 int inf_or_nan, int all_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 } |
1658
|
342 else if (all_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 |
|
428 int all_inf_or_nan |
|
429 = inf_or_nan ? all_elements_are_int_or_inf_or_nan (m) : 0; |
|
430 |
|
431 Matrix m_abs = abs (m); |
|
432 double max_abs = pr_max_internal (m_abs); |
|
433 double min_abs = pr_min_internal (m_abs); |
|
434 |
|
435 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
436 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
437 |
|
438 set_real_matrix_format (sign, x_max, x_min, inf_or_nan, |
|
439 all_inf_or_nan, fw); |
|
440 } |
|
441 |
1
|
442 static inline void |
164
|
443 set_format (const Matrix& m) |
1
|
444 { |
|
445 int fw; |
|
446 set_format (m, fw); |
|
447 } |
|
448 |
|
449 static void |
1658
|
450 set_complex_format (int sign, int x_max, int x_min, int r_x, |
|
451 int inf_or_nan, int int_only, int& r_fw, int& i_fw) |
1
|
452 { |
278
|
453 static char r_fmt_buf[128]; |
|
454 static char i_fmt_buf[128]; |
1
|
455 |
|
456 int prec = user_pref.output_precision; |
|
457 |
|
458 int ld, rd; |
|
459 |
|
460 if (bank_format) |
|
461 { |
|
462 int digits = r_x; |
|
463 i_fw = 0; |
|
464 r_fw = digits <= 0 ? 4 : digits + 3; |
|
465 if (inf_or_nan && r_fw < 3) |
|
466 r_fw = 3; |
|
467 r_fw += sign; |
|
468 rd = 2; |
|
469 } |
1282
|
470 else if (hex_format) |
|
471 { |
|
472 r_fw = 2 * sizeof (double); |
|
473 i_fw = 2 * sizeof (double); |
|
474 rd = 0; |
|
475 } |
1309
|
476 else if (bit_format) |
|
477 { |
|
478 r_fw = 8 * sizeof (double); |
|
479 i_fw = 8 * sizeof (double); |
|
480 rd = 0; |
|
481 } |
1658
|
482 else if (inf_or_nan || int_only) |
1
|
483 { |
|
484 int digits = x_max > x_min ? x_max : x_min; |
|
485 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
486 if (inf_or_nan && i_fw < 3) |
|
487 i_fw = r_fw = 3; |
|
488 r_fw += sign; |
|
489 rd = 0; |
|
490 } |
|
491 else |
|
492 { |
|
493 int ld_max, rd_max; |
|
494 if (x_max > 0) |
|
495 { |
|
496 ld_max = x_max; |
1658
|
497 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
498 x_max++; |
|
499 } |
|
500 else |
|
501 { |
|
502 ld_max = 1; |
1658
|
503 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
504 x_max = -x_max + 1; |
|
505 } |
|
506 |
|
507 int ld_min, rd_min; |
|
508 if (x_min > 0) |
|
509 { |
|
510 ld_min = x_min; |
1658
|
511 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
512 x_min++; |
|
513 } |
|
514 else |
|
515 { |
|
516 ld_min = 1; |
1658
|
517 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
518 x_min = -x_min + 1; |
|
519 } |
|
520 |
|
521 ld = ld_max > ld_min ? ld_max : ld_min; |
|
522 rd = rd_max > rd_min ? rd_max : rd_min; |
|
523 |
|
524 i_fw = r_fw = ld + 1 + rd; |
|
525 if (inf_or_nan && i_fw < 3) |
|
526 i_fw = r_fw = 3; |
|
527 r_fw += sign; |
|
528 } |
|
529 |
1309
|
530 if (! (bank_format || hex_format || bit_format) |
1282
|
531 && (r_fw > user_pref.output_max_field_width || print_e)) |
1
|
532 { |
|
533 int exp_field = 4; |
|
534 if (x_max > 100 || x_min > 100) |
|
535 exp_field++; |
|
536 |
|
537 i_fw = r_fw = 1 + prec + exp_field; |
|
538 if (inf_or_nan && i_fw < 3) |
|
539 i_fw = r_fw = 3; |
|
540 r_fw += sign; |
|
541 |
|
542 if (print_big_e) |
|
543 { |
|
544 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
545 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
546 } |
|
547 else |
|
548 { |
|
549 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
550 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
551 } |
|
552 } |
|
553 else |
|
554 { |
|
555 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
556 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
557 } |
|
558 |
|
559 curr_real_fmt = &r_fmt_buf[0]; |
|
560 curr_imag_fmt = &i_fmt_buf[0]; |
|
561 } |
|
562 |
1658
|
563 static void |
|
564 set_format (const Complex& c, int& r_fw, int& i_fw) |
|
565 { |
|
566 curr_real_fmt = 0; |
|
567 curr_imag_fmt = 0; |
|
568 |
|
569 if (free_format) |
|
570 return; |
|
571 |
|
572 double rp = c.real (); |
|
573 double ip = c.imag (); |
|
574 |
|
575 int sign = (rp < 0.0); |
|
576 |
|
577 int inf_or_nan = (xisinf (c) || xisnan (c)); |
|
578 |
|
579 int int_only = (D_NINT (rp) == rp && D_NINT (ip) == ip); |
|
580 |
|
581 double r_abs = rp < 0.0 ? -rp : rp; |
|
582 double i_abs = ip < 0.0 ? -ip : ip; |
|
583 |
|
584 int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0); |
|
585 int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0); |
|
586 |
|
587 int x_max, x_min; |
|
588 |
|
589 if (r_x > i_x) |
|
590 { |
|
591 x_max = r_x; |
|
592 x_min = i_x; |
|
593 } |
|
594 else |
|
595 { |
|
596 x_max = i_x; |
|
597 x_min = r_x; |
|
598 } |
|
599 |
|
600 set_complex_format (sign, x_max, x_min, r_x, inf_or_nan, int_only, |
|
601 r_fw, i_fw); |
|
602 } |
|
603 |
1
|
604 static inline void |
164
|
605 set_format (const Complex& c) |
1
|
606 { |
|
607 int r_fw, i_fw; |
|
608 set_format (c, r_fw, i_fw); |
|
609 } |
|
610 |
|
611 static void |
1658
|
612 set_complex_matrix_format (int sign, int x_max, int x_min, |
|
613 int r_x_max, int r_x_min, int inf_or_nan, |
|
614 int all_inf_or_nan, int& r_fw, int& i_fw) |
1
|
615 { |
278
|
616 static char r_fmt_buf[128]; |
|
617 static char i_fmt_buf[128]; |
1
|
618 |
|
619 int prec = user_pref.output_precision; |
|
620 |
|
621 int ld, rd; |
|
622 |
|
623 if (bank_format) |
|
624 { |
|
625 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
626 i_fw = 0; |
|
627 r_fw = digits <= 0 ? 4 : digits + 3; |
|
628 if (inf_or_nan && i_fw < 3) |
|
629 i_fw = r_fw = 3; |
|
630 r_fw += sign; |
|
631 rd = 2; |
|
632 } |
1282
|
633 else if (hex_format) |
|
634 { |
|
635 r_fw = 2 * sizeof (double); |
|
636 i_fw = 2 * sizeof (double); |
|
637 rd = 0; |
|
638 } |
1309
|
639 else if (bit_format) |
|
640 { |
|
641 r_fw = 8 * sizeof (double); |
|
642 i_fw = 8 * sizeof (double); |
|
643 rd = 0; |
|
644 } |
1658
|
645 else if (all_inf_or_nan) |
1
|
646 { |
|
647 int digits = x_max > x_min ? x_max : x_min; |
|
648 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
649 if (inf_or_nan && i_fw < 3) |
|
650 i_fw = r_fw = 3; |
|
651 r_fw += sign; |
|
652 rd = 0; |
|
653 } |
|
654 else |
|
655 { |
|
656 int ld_max, rd_max; |
|
657 if (x_max > 0) |
|
658 { |
|
659 ld_max = x_max; |
1658
|
660 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
661 x_max++; |
|
662 } |
|
663 else |
|
664 { |
|
665 ld_max = 1; |
1658
|
666 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
667 x_max = -x_max + 1; |
|
668 } |
|
669 |
|
670 int ld_min, rd_min; |
|
671 if (x_min > 0) |
|
672 { |
|
673 ld_min = x_min; |
1658
|
674 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
675 x_min++; |
|
676 } |
|
677 else |
|
678 { |
|
679 ld_min = 1; |
1658
|
680 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
681 x_min = -x_min + 1; |
|
682 } |
|
683 |
|
684 ld = ld_max > ld_min ? ld_max : ld_min; |
|
685 rd = rd_max > rd_min ? rd_max : rd_min; |
|
686 |
|
687 i_fw = r_fw = ld + 1 + rd; |
|
688 if (inf_or_nan && i_fw < 3) |
|
689 i_fw = r_fw = 3; |
|
690 r_fw += sign; |
|
691 } |
|
692 |
1309
|
693 if (! (bank_format || hex_format || bit_format) |
1282
|
694 && (r_fw > user_pref.output_max_field_width || print_e)) |
1
|
695 { |
|
696 int exp_field = 4; |
|
697 if (x_max > 100 || x_min > 100) |
|
698 exp_field++; |
|
699 |
|
700 i_fw = r_fw = 1 + prec + exp_field; |
|
701 if (inf_or_nan && i_fw < 3) |
|
702 i_fw = r_fw = 3; |
|
703 r_fw += sign; |
|
704 |
|
705 if (print_big_e) |
|
706 { |
|
707 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
708 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
709 } |
|
710 else |
|
711 { |
|
712 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
713 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
714 } |
|
715 } |
|
716 else |
|
717 { |
|
718 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
719 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
720 } |
|
721 |
|
722 curr_real_fmt = &r_fmt_buf[0]; |
|
723 curr_imag_fmt = &i_fmt_buf[0]; |
|
724 } |
|
725 |
1658
|
726 static void |
|
727 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw) |
|
728 { |
|
729 curr_real_fmt = 0; |
|
730 curr_imag_fmt = 0; |
|
731 |
|
732 if (free_format) |
|
733 return; |
|
734 |
|
735 Matrix rp = real (cm); |
|
736 Matrix ip = imag (cm); |
|
737 |
|
738 int sign = any_element_is_negative (rp); |
|
739 |
|
740 int inf_or_nan = any_element_is_inf_or_nan (cm); |
|
741 |
|
742 int all_inf_or_nan |
|
743 = inf_or_nan ? (all_elements_are_int_or_inf_or_nan (rp) |
|
744 && all_elements_are_int_or_inf_or_nan (ip)) : 0; |
|
745 |
|
746 Matrix r_m_abs = abs (rp); |
|
747 double r_max_abs = pr_max_internal (r_m_abs); |
|
748 double r_min_abs = pr_min_internal (r_m_abs); |
|
749 |
|
750 Matrix i_m_abs = abs (ip); |
|
751 double i_max_abs = pr_max_internal (i_m_abs); |
|
752 double i_min_abs = pr_min_internal (i_m_abs); |
|
753 |
|
754 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
755 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
756 |
|
757 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
758 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
759 |
|
760 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
761 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
762 |
|
763 set_complex_matrix_format (sign, x_max, x_min, r_x_max, r_x_min, |
|
764 inf_or_nan, all_inf_or_nan, r_fw, i_fw); |
|
765 } |
|
766 |
1
|
767 static int |
164
|
768 all_elements_are_ints (const Range& r) |
1
|
769 { |
1282
|
770 // If the base and increment are ints, the final value in the range |
|
771 // will also be an integer, even if the limit is not. |
1
|
772 |
|
773 double b = r.base (); |
|
774 double i = r.inc (); |
|
775 |
1086
|
776 return (! (xisnan (b) || xisnan (i)) |
|
777 && (double) NINT (b) == b && (double) NINT (i) == i); |
1
|
778 } |
|
779 |
|
780 static inline void |
164
|
781 set_format (const ComplexMatrix& cm) |
1
|
782 { |
|
783 int r_fw, i_fw; |
|
784 set_format (cm, r_fw, i_fw); |
|
785 } |
|
786 |
|
787 static void |
1658
|
788 set_range_format (int sign, int x_max, int x_min, int all_ints, int& fw) |
1
|
789 { |
278
|
790 static char fmt_buf[128]; |
1
|
791 |
|
792 int prec = user_pref.output_precision; |
|
793 |
|
794 int ld, rd; |
|
795 |
|
796 if (bank_format) |
|
797 { |
|
798 int digits = x_max > x_min ? x_max : x_min; |
|
799 fw = sign + digits < 0 ? 4 : digits + 3; |
|
800 rd = 2; |
|
801 } |
1282
|
802 else if (hex_format) |
|
803 { |
|
804 fw = 2 * sizeof (double); |
|
805 rd = 0; |
|
806 } |
1309
|
807 else if (bit_format) |
|
808 { |
|
809 fw = 8 * sizeof (double); |
|
810 rd = 0; |
|
811 } |
1658
|
812 else if (all_ints) |
1
|
813 { |
|
814 int digits = x_max > x_min ? x_max : x_min; |
|
815 fw = sign + digits; |
|
816 rd = 0; |
|
817 } |
|
818 else |
|
819 { |
|
820 int ld_max, rd_max; |
|
821 if (x_max > 0) |
|
822 { |
|
823 ld_max = x_max; |
1658
|
824 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
825 x_max++; |
|
826 } |
|
827 else |
|
828 { |
|
829 ld_max = 1; |
1658
|
830 rd_max = prec > x_max ? prec - x_max : prec; |
1
|
831 x_max = -x_max + 1; |
|
832 } |
|
833 |
|
834 int ld_min, rd_min; |
|
835 if (x_min > 0) |
|
836 { |
|
837 ld_min = x_min; |
1658
|
838 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
839 x_min++; |
|
840 } |
|
841 else |
|
842 { |
|
843 ld_min = 1; |
1658
|
844 rd_min = prec > x_min ? prec - x_min : prec; |
1
|
845 x_min = -x_min + 1; |
|
846 } |
|
847 |
|
848 ld = ld_max > ld_min ? ld_max : ld_min; |
|
849 rd = rd_max > rd_min ? rd_max : rd_min; |
|
850 |
|
851 fw = sign + ld + 1 + rd; |
|
852 } |
|
853 |
1309
|
854 if (! (bank_format || hex_format || bit_format) |
1282
|
855 && (fw > user_pref.output_max_field_width || print_e)) |
1
|
856 { |
|
857 int exp_field = 4; |
|
858 if (x_max > 100 || x_min > 100) |
|
859 exp_field++; |
|
860 |
|
861 fw = sign + 2 + prec + exp_field; |
|
862 |
|
863 if (print_big_e) |
|
864 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
865 else |
|
866 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
867 } |
|
868 else |
|
869 { |
|
870 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
871 } |
|
872 |
|
873 curr_real_fmt = &fmt_buf[0]; |
|
874 } |
|
875 |
1658
|
876 static void |
|
877 set_format (const Range& r, int& fw) |
|
878 { |
|
879 curr_real_fmt = 0; |
|
880 curr_imag_fmt = 0; |
|
881 |
|
882 if (free_format) |
|
883 return; |
|
884 |
|
885 double r_min = r.base (); |
|
886 double r_max = r.limit (); |
|
887 |
|
888 if (r_max < r_min) |
|
889 { |
|
890 double tmp = r_max; |
|
891 r_max = r_min; |
|
892 r_min = tmp; |
|
893 } |
|
894 |
|
895 int sign = (r_min < 0.0); |
|
896 |
|
897 int all_ints = all_elements_are_ints (r); |
|
898 |
|
899 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
900 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
901 |
|
902 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
903 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
904 |
|
905 set_range_format (sign, x_max, x_min, all_ints, fw); |
|
906 } |
|
907 |
1
|
908 static inline void |
164
|
909 set_format (const Range& r) |
1
|
910 { |
|
911 int fw; |
|
912 set_format (r, fw); |
|
913 } |
|
914 |
1282
|
915 union equiv |
|
916 { |
|
917 double d; |
|
918 unsigned char i[sizeof (double)]; |
|
919 }; |
|
920 |
1309
|
921 #define PRINT_CHAR_BITS(os, c) \ |
|
922 do \ |
|
923 { \ |
|
924 unsigned char ctmp = c; \ |
|
925 char stmp[9]; \ |
1488
|
926 stmp[0] = (ctmp & 0x80) ? '1' : '0'; \ |
|
927 stmp[1] = (ctmp & 0x40) ? '1' : '0'; \ |
|
928 stmp[2] = (ctmp & 0x20) ? '1' : '0'; \ |
|
929 stmp[3] = (ctmp & 0x10) ? '1' : '0'; \ |
|
930 stmp[4] = (ctmp & 0x08) ? '1' : '0'; \ |
|
931 stmp[5] = (ctmp & 0x04) ? '1' : '0'; \ |
|
932 stmp[6] = (ctmp & 0x02) ? '1' : '0'; \ |
|
933 stmp[7] = (ctmp & 0x01) ? '1' : '0'; \ |
1309
|
934 stmp[8] = '\0'; \ |
|
935 os.form ("%s", stmp); \ |
|
936 } \ |
|
937 while (0) |
|
938 |
|
939 #define PRINT_CHAR_BITS_SWAPPED(os, c) \ |
|
940 do \ |
|
941 { \ |
|
942 unsigned char ctmp = c; \ |
|
943 char stmp[9]; \ |
1488
|
944 stmp[0] = (ctmp & 0x01) ? '1' : '0'; \ |
|
945 stmp[1] = (ctmp & 0x02) ? '1' : '0'; \ |
|
946 stmp[2] = (ctmp & 0x04) ? '1' : '0'; \ |
|
947 stmp[3] = (ctmp & 0x08) ? '1' : '0'; \ |
|
948 stmp[4] = (ctmp & 0x10) ? '1' : '0'; \ |
|
949 stmp[5] = (ctmp & 0x20) ? '1' : '0'; \ |
|
950 stmp[6] = (ctmp & 0x40) ? '1' : '0'; \ |
|
951 stmp[7] = (ctmp & 0x80) ? '1' : '0'; \ |
1309
|
952 stmp[8] = '\0'; \ |
|
953 os.form ("%s", stmp); \ |
|
954 } \ |
|
955 while (0) |
|
956 |
1
|
957 static inline void |
626
|
958 pr_any_float (const char *fmt, ostream& os, double d, int fw = 0) |
1
|
959 { |
|
960 if (d == -0.0) |
|
961 d = 0.0; |
|
962 |
529
|
963 if (fmt) |
1
|
964 { |
1282
|
965 if (hex_format) |
|
966 { |
|
967 equiv tmp; |
|
968 tmp.d = d; |
|
969 |
|
970 // Unless explicitly asked for, always print in big-endian |
|
971 // format. |
|
972 |
|
973 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
974 // formats and not for Cray? |
|
975 |
|
976 if (hex_format > 1 |
|
977 || native_float_format == OCTAVE_IEEE_BIG |
|
978 || native_float_format == OCTAVE_CRAY |
|
979 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
980 { |
1322
|
981 for (size_t i = 0; i < sizeof (double); i++) |
1282
|
982 os.form ("%02x", (int) tmp.i[i]); |
|
983 } |
|
984 else |
|
985 { |
1328
|
986 for (int i = sizeof (double) - 1; i >= 0; i--) |
1282
|
987 os.form ("%02x", (int) tmp.i[i]); |
|
988 } |
|
989 } |
1309
|
990 else if (bit_format) |
|
991 { |
|
992 equiv tmp; |
|
993 tmp.d = d; |
|
994 |
|
995 // Unless explicitly asked for, always print in big-endian |
|
996 // format. |
|
997 |
|
998 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
999 // formats and not for Cray? |
|
1000 |
|
1001 if (native_float_format == OCTAVE_IEEE_BIG |
|
1002 || native_float_format == OCTAVE_CRAY |
|
1003 || native_float_format == OCTAVE_UNKNOWN_FLT_FMT) |
|
1004 { |
1322
|
1005 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1006 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1007 } |
|
1008 else |
|
1009 { |
|
1010 if (bit_format > 1) |
|
1011 { |
1328
|
1012 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
1013 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
1014 } |
|
1015 else |
|
1016 { |
|
1017 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
1018 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
1019 } |
|
1020 } |
|
1021 } |
1282
|
1022 else if (xisinf (d)) |
1
|
1023 { |
|
1024 char *s; |
|
1025 if (d < 0.0) |
|
1026 s = "-Inf"; |
|
1027 else |
|
1028 s = "Inf"; |
|
1029 |
|
1030 if (fw > 0) |
|
1031 os.form ("%*s", fw, s); |
|
1032 else |
|
1033 os << s; |
|
1034 } |
|
1035 else if (xisnan (d)) |
|
1036 { |
|
1037 if (fw > 0) |
|
1038 os.form ("%*s", fw, "NaN"); |
|
1039 else |
|
1040 os << "NaN"; |
|
1041 } |
|
1042 else |
|
1043 os.form (fmt, d); |
|
1044 } |
529
|
1045 else |
|
1046 os << d; |
1
|
1047 } |
|
1048 |
|
1049 static inline void |
626
|
1050 pr_float (ostream& os, double d, int fw = 0) |
1
|
1051 { |
|
1052 pr_any_float (curr_real_fmt, os, d, fw); |
|
1053 } |
|
1054 |
|
1055 static inline void |
626
|
1056 pr_imag_float (ostream& os, double d, int fw = 0) |
1
|
1057 { |
|
1058 pr_any_float (curr_imag_fmt, os, d, fw); |
|
1059 } |
|
1060 |
|
1061 static inline void |
626
|
1062 pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
1063 { |
|
1064 double r = c.real (); |
|
1065 pr_float (os, r, r_fw); |
|
1066 if (! bank_format) |
|
1067 { |
|
1068 double i = c.imag (); |
1309
|
1069 if (! (hex_format || bit_format) && i < 0) |
1
|
1070 { |
|
1071 os << " - "; |
|
1072 i = -i; |
|
1073 pr_imag_float (os, i, i_fw); |
|
1074 } |
|
1075 else |
|
1076 { |
1309
|
1077 if (hex_format || bit_format) |
1282
|
1078 os << " "; |
|
1079 else |
|
1080 os << " + "; |
|
1081 |
1
|
1082 pr_imag_float (os, i, i_fw); |
|
1083 } |
|
1084 os << "i"; |
|
1085 } |
|
1086 } |
|
1087 |
626
|
1088 static void |
|
1089 print_empty_matrix (ostream& os, int nr, int nc, int pr_as_read_syntax) |
|
1090 { |
|
1091 assert (nr == 0 || nc == 0); |
|
1092 |
|
1093 if (pr_as_read_syntax) |
|
1094 { |
|
1095 if (nr == 0 && nc == 0) |
|
1096 os << "[]"; |
|
1097 else |
|
1098 os << "zeros (" << nr << ", " << nc << ")"; |
|
1099 } |
|
1100 else |
|
1101 { |
|
1102 os << "[]"; |
|
1103 if (user_pref.print_empty_dimensions) |
|
1104 os << "(" << nr << "x" << nc << ")"; |
|
1105 os << "\n"; |
|
1106 } |
|
1107 } |
|
1108 |
1186
|
1109 static void |
|
1110 pr_col_num_header (ostream& os, int total_width, int max_width, |
|
1111 int lim, int col) |
|
1112 { |
|
1113 if (total_width > max_width && user_pref.split_long_rows) |
|
1114 { |
|
1115 if (col != 0 && ! compact_format) |
|
1116 os << "\n"; |
|
1117 |
|
1118 int num_cols = lim - col; |
|
1119 |
|
1120 if (num_cols == 1) |
|
1121 os << " Column " << col + 1 << ":\n"; |
|
1122 else if (num_cols == 2) |
|
1123 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1124 else |
|
1125 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
|
1126 |
|
1127 if (! compact_format) |
|
1128 os << "\n"; |
|
1129 } |
|
1130 } |
|
1131 |
1
|
1132 void |
719
|
1133 octave_print_internal (ostream& os, double d, int pr_as_read_syntax) |
1
|
1134 { |
|
1135 if (plus_format) |
|
1136 { |
|
1137 if (d == 0.0) |
|
1138 os << " "; |
|
1139 else |
|
1140 os << "+"; |
|
1141 } |
|
1142 else |
|
1143 { |
|
1144 set_format (d); |
|
1145 if (free_format) |
|
1146 os << d; |
|
1147 else |
|
1148 pr_float (os, d); |
|
1149 } |
626
|
1150 |
|
1151 if (! pr_as_read_syntax) |
|
1152 os << "\n"; |
1
|
1153 } |
|
1154 |
|
1155 void |
719
|
1156 octave_print_internal (ostream& os, const Matrix& m, int pr_as_read_syntax) |
1
|
1157 { |
|
1158 int nr = m.rows (); |
|
1159 int nc = m.columns (); |
|
1160 |
626
|
1161 if (nr == 0 || nc == 0) |
|
1162 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1163 else if (plus_format && ! pr_as_read_syntax) |
1
|
1164 { |
|
1165 for (int i = 0; i < nr; i++) |
|
1166 { |
|
1167 for (int j = 0; j < nc; j++) |
|
1168 { |
|
1169 if (j == 0) |
|
1170 os << " "; |
|
1171 |
|
1172 if (m.elem (i, j) == 0.0) |
|
1173 os << " "; |
|
1174 else |
|
1175 os << "+"; |
|
1176 } |
|
1177 os << "\n"; |
|
1178 } |
|
1179 } |
|
1180 else |
|
1181 { |
|
1182 int fw; |
|
1183 set_format (m, fw); |
|
1184 int column_width = fw + 2; |
|
1185 int total_width = nc * column_width; |
|
1186 int max_width = terminal_columns (); |
|
1187 |
626
|
1188 if (pr_as_read_syntax) |
|
1189 max_width -= 4; |
|
1190 |
1
|
1191 if (free_format) |
|
1192 { |
626
|
1193 if (pr_as_read_syntax) |
|
1194 os << "[\n"; |
|
1195 |
1
|
1196 os << m; |
626
|
1197 |
|
1198 if (pr_as_read_syntax) |
|
1199 os << "]"; |
|
1200 |
1
|
1201 return; |
|
1202 } |
|
1203 |
|
1204 int inc = nc; |
|
1205 if (total_width > max_width && user_pref.split_long_rows) |
|
1206 { |
|
1207 inc = max_width / column_width; |
|
1208 if (inc == 0) |
|
1209 inc++; |
|
1210 } |
|
1211 |
626
|
1212 if (pr_as_read_syntax) |
1
|
1213 { |
|
1214 for (int i = 0; i < nr; i++) |
|
1215 { |
626
|
1216 int col = 0; |
|
1217 while (col < nc) |
1
|
1218 { |
626
|
1219 int lim = col + inc < nc ? col + inc : nc; |
|
1220 |
|
1221 for (int j = col; j < lim; j++) |
|
1222 { |
|
1223 if (i == 0 && j == 0) |
|
1224 os << "[ "; |
|
1225 else |
|
1226 { |
|
1227 if (j > col && j < lim) |
|
1228 os << ", "; |
|
1229 else |
|
1230 os << " "; |
|
1231 } |
|
1232 |
|
1233 pr_float (os, m.elem (i, j)); |
|
1234 } |
|
1235 |
|
1236 col += inc; |
|
1237 |
|
1238 if (col >= nc) |
|
1239 { |
|
1240 if (i == nr - 1) |
|
1241 os << " ]"; |
|
1242 else |
|
1243 os << ";\n"; |
|
1244 } |
|
1245 else |
|
1246 os << " ...\n"; |
1
|
1247 } |
|
1248 } |
626
|
1249 } |
|
1250 else |
|
1251 { |
|
1252 for (int col = 0; col < nc; col += inc) |
|
1253 { |
|
1254 int lim = col + inc < nc ? col + inc : nc; |
|
1255 |
1186
|
1256 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1257 |
|
1258 for (int i = 0; i < nr; i++) |
|
1259 { |
|
1260 for (int j = col; j < lim; j++) |
|
1261 { |
|
1262 os << " "; |
|
1263 |
|
1264 pr_float (os, m.elem (i, j), fw); |
|
1265 } |
|
1266 |
|
1267 os << "\n"; |
|
1268 } |
|
1269 } |
1
|
1270 } |
|
1271 } |
|
1272 } |
|
1273 |
|
1274 void |
626
|
1275 octave_print_internal (ostream& os, const Complex& c, |
|
1276 int pr_as_read_syntax) |
1
|
1277 { |
|
1278 if (plus_format) |
|
1279 { |
|
1280 if (c == 0.0) |
|
1281 os << " "; |
|
1282 else |
|
1283 os << "+"; |
|
1284 } |
|
1285 else |
|
1286 { |
|
1287 set_format (c); |
|
1288 if (free_format) |
|
1289 os << c; |
|
1290 else |
|
1291 pr_complex (os, c); |
|
1292 } |
626
|
1293 |
|
1294 if (! pr_as_read_syntax) |
|
1295 os << "\n"; |
1
|
1296 } |
|
1297 |
|
1298 void |
626
|
1299 octave_print_internal (ostream& os, const ComplexMatrix& cm, |
|
1300 int pr_as_read_syntax) |
1
|
1301 { |
|
1302 int nr = cm.rows (); |
|
1303 int nc = cm.columns (); |
|
1304 |
626
|
1305 if (nr == 0 || nc == 0) |
|
1306 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1307 else if (plus_format && ! pr_as_read_syntax) |
1
|
1308 { |
|
1309 for (int i = 0; i < nr; i++) |
|
1310 { |
|
1311 for (int j = 0; j < nc; j++) |
|
1312 { |
|
1313 if (j == 0) |
|
1314 os << " "; |
|
1315 |
|
1316 if (cm.elem (i, j) == 0.0) |
|
1317 os << " "; |
|
1318 else |
|
1319 os << "+"; |
|
1320 } |
|
1321 os << "\n"; |
|
1322 } |
|
1323 } |
|
1324 else |
|
1325 { |
|
1326 int r_fw, i_fw; |
|
1327 set_format (cm, r_fw, i_fw); |
|
1328 int column_width = i_fw + r_fw; |
1309
|
1329 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1330 int total_width = nc * column_width; |
|
1331 int max_width = terminal_columns (); |
|
1332 |
626
|
1333 if (pr_as_read_syntax) |
|
1334 max_width -= 4; |
|
1335 |
1
|
1336 if (free_format) |
|
1337 { |
626
|
1338 if (pr_as_read_syntax) |
|
1339 os << "[\n"; |
|
1340 |
1
|
1341 os << cm; |
626
|
1342 |
|
1343 if (pr_as_read_syntax) |
|
1344 os << "]"; |
|
1345 |
1
|
1346 return; |
|
1347 } |
|
1348 |
|
1349 int inc = nc; |
|
1350 if (total_width > max_width && user_pref.split_long_rows) |
|
1351 { |
|
1352 inc = max_width / column_width; |
|
1353 if (inc == 0) |
|
1354 inc++; |
|
1355 } |
|
1356 |
626
|
1357 if (pr_as_read_syntax) |
1
|
1358 { |
|
1359 for (int i = 0; i < nr; i++) |
|
1360 { |
626
|
1361 int col = 0; |
|
1362 while (col < nc) |
1
|
1363 { |
626
|
1364 int lim = col + inc < nc ? col + inc : nc; |
|
1365 |
|
1366 for (int j = col; j < lim; j++) |
|
1367 { |
|
1368 if (i == 0 && j == 0) |
|
1369 os << "[ "; |
|
1370 else |
|
1371 { |
|
1372 if (j > col && j < lim) |
|
1373 os << ", "; |
|
1374 else |
|
1375 os << " "; |
|
1376 } |
|
1377 |
|
1378 pr_complex (os, cm.elem (i, j)); |
|
1379 } |
|
1380 |
|
1381 col += inc; |
|
1382 |
|
1383 if (col >= nc) |
|
1384 { |
|
1385 if (i == nr - 1) |
|
1386 os << " ]"; |
|
1387 else |
|
1388 os << ";\n"; |
|
1389 } |
1
|
1390 else |
626
|
1391 os << " ...\n"; |
1
|
1392 } |
|
1393 } |
626
|
1394 } |
|
1395 else |
|
1396 { |
|
1397 for (int col = 0; col < nc; col += inc) |
|
1398 { |
|
1399 int lim = col + inc < nc ? col + inc : nc; |
|
1400 |
1186
|
1401 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1402 |
|
1403 for (int i = 0; i < nr; i++) |
|
1404 { |
|
1405 for (int j = col; j < lim; j++) |
|
1406 { |
|
1407 os << " "; |
|
1408 |
|
1409 pr_complex (os, cm.elem (i, j)); |
|
1410 } |
|
1411 os << "\n"; |
|
1412 } |
|
1413 } |
1
|
1414 } |
|
1415 } |
|
1416 } |
|
1417 |
|
1418 void |
626
|
1419 octave_print_internal (ostream& os, const Range& r, |
|
1420 int pr_as_read_syntax) |
1
|
1421 { |
626
|
1422 double base = r.base (); |
1
|
1423 double increment = r.inc (); |
626
|
1424 double limit = r.limit (); |
1
|
1425 int num_elem = r.nelem (); |
|
1426 |
626
|
1427 if (plus_format && ! pr_as_read_syntax) |
1
|
1428 { |
|
1429 os << " "; |
|
1430 for (int i = 0; i < num_elem; i++) |
|
1431 { |
626
|
1432 double val = base + i * increment; |
1
|
1433 if (val == 0.0) |
|
1434 os << " "; |
|
1435 else |
|
1436 os << "+"; |
|
1437 } |
|
1438 } |
|
1439 else |
|
1440 { |
|
1441 int fw; |
|
1442 set_format (r, fw); |
|
1443 |
626
|
1444 if (pr_as_read_syntax) |
1
|
1445 { |
626
|
1446 if (free_format) |
|
1447 { |
|
1448 os << base << " : "; |
|
1449 if (increment != 1.0) |
|
1450 os << increment << " : "; |
|
1451 os << limit; |
|
1452 } |
|
1453 else |
|
1454 { |
|
1455 pr_float (os, base, fw); |
|
1456 os << " : "; |
|
1457 if (increment != 1.0) |
|
1458 { |
|
1459 pr_float (os, increment, fw); |
|
1460 os << " : "; |
|
1461 } |
|
1462 pr_float (os, limit, fw); |
|
1463 } |
1
|
1464 } |
626
|
1465 else |
|
1466 { |
|
1467 int column_width = fw + 2; |
|
1468 int total_width = num_elem * column_width; |
|
1469 int max_width = terminal_columns (); |
1
|
1470 |
626
|
1471 if (free_format) |
|
1472 { |
|
1473 os << r; |
|
1474 return; |
|
1475 } |
1
|
1476 |
626
|
1477 int inc = num_elem; |
1
|
1478 if (total_width > max_width && user_pref.split_long_rows) |
|
1479 { |
626
|
1480 inc = max_width / column_width; |
|
1481 if (inc == 0) |
|
1482 inc++; |
1
|
1483 } |
|
1484 |
626
|
1485 int col = 0; |
|
1486 while (col < num_elem) |
1
|
1487 { |
626
|
1488 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1489 |
1186
|
1490 pr_col_num_header (os, total_width, max_width, lim, col); |
626
|
1491 |
|
1492 for (int i = col; i < lim; i++) |
|
1493 { |
|
1494 double val = base + i * increment; |
|
1495 os << " "; |
|
1496 pr_float (os, val, fw); |
|
1497 } |
|
1498 |
|
1499 os << "\n"; |
|
1500 |
|
1501 col += inc; |
1
|
1502 } |
|
1503 } |
|
1504 } |
|
1505 } |
|
1506 |
1343
|
1507 void |
1572
|
1508 octave_print_internal (ostream& os, const charMatrix& chm, |
|
1509 int pr_as_read_syntax, int pr_as_string) |
1343
|
1510 { |
1572
|
1511 if (pr_as_string) |
|
1512 { |
|
1513 int nstr = chm.rows (); |
1343
|
1514 |
1572
|
1515 if (pr_as_read_syntax && nstr > 1) |
|
1516 os << "[ "; |
1343
|
1517 |
1572
|
1518 for (int i = 0; i < nstr; i++) |
1343
|
1519 { |
1588
|
1520 char *row = chm.row_as_string (i); |
|
1521 |
1572
|
1522 if (pr_as_read_syntax) |
|
1523 { |
1588
|
1524 char *tmp = undo_string_escapes (row); |
|
1525 |
|
1526 os << "\"" << tmp << "\""; |
|
1527 |
|
1528 delete [] tmp; |
1343
|
1529 |
1572
|
1530 if (i < nstr - 1) |
|
1531 os << "; "; |
|
1532 } |
|
1533 else |
1588
|
1534 os << row << "\n"; |
|
1535 |
|
1536 delete [] row; |
1343
|
1537 } |
1572
|
1538 |
|
1539 if (pr_as_read_syntax && nstr > 1) |
|
1540 os << " ]"; |
1343
|
1541 } |
1572
|
1542 else |
|
1543 { |
|
1544 os << "sorry, printing char matrices not implemented yet\n"; |
|
1545 } |
1343
|
1546 } |
|
1547 |
1488
|
1548 DEFUN ("disp", Fdisp, Sdisp, 10, |
529
|
1549 "disp (X): display value without name tag") |
|
1550 { |
|
1551 Octave_object retval; |
|
1552 |
|
1553 int nargin = args.length (); |
|
1554 |
712
|
1555 if (nargin == 1) |
|
1556 args(0).eval (1); |
529
|
1557 else |
|
1558 print_usage ("disp"); |
|
1559 |
|
1560 return retval; |
|
1561 } |
|
1562 |
1
|
1563 static void |
|
1564 init_format_state (void) |
|
1565 { |
|
1566 free_format = 0; |
|
1567 plus_format = 0; |
|
1568 bank_format = 0; |
1282
|
1569 hex_format = 0; |
1309
|
1570 bit_format = 0; |
1
|
1571 print_e = 0; |
|
1572 print_big_e = 0; |
|
1573 } |
|
1574 |
|
1575 static void |
|
1576 set_output_prec_and_fw (int prec, int fw) |
|
1577 { |
529
|
1578 tree_constant *tmp = 0; |
1
|
1579 |
|
1580 tmp = new tree_constant ((double) prec); |
195
|
1581 bind_builtin_variable ("output_precision", tmp); |
1
|
1582 |
|
1583 tmp = new tree_constant ((double) fw); |
195
|
1584 bind_builtin_variable ("output_max_field_width", tmp); |
1
|
1585 } |
|
1586 |
|
1587 void |
|
1588 set_format_style (int argc, char **argv) |
|
1589 { |
|
1590 if (--argc > 0) |
|
1591 { |
|
1592 argv++; |
|
1593 if (*argv[0]) |
|
1594 { |
|
1595 if (strcmp (*argv, "short") == 0) |
|
1596 { |
|
1597 if (--argc > 0) |
|
1598 { |
|
1599 argv++; |
|
1600 if (strcmp (*argv, "e") == 0) |
|
1601 { |
|
1602 init_format_state (); |
|
1603 print_e = 1; |
|
1604 } |
|
1605 else if (strcmp (*argv, "E") == 0) |
|
1606 { |
|
1607 init_format_state (); |
|
1608 print_e = 1; |
|
1609 print_big_e = 1; |
|
1610 } |
|
1611 else |
|
1612 { |
217
|
1613 error ("format: unrecognized option `short %s'", *argv); |
1
|
1614 return; |
|
1615 } |
|
1616 } |
|
1617 else |
|
1618 init_format_state (); |
|
1619 |
|
1620 set_output_prec_and_fw (3, 8); |
|
1621 } |
|
1622 else if (strcmp (*argv, "long") == 0) |
|
1623 { |
|
1624 if (--argc > 0) |
|
1625 { |
|
1626 argv++; |
|
1627 if (strcmp (*argv, "e") == 0) |
|
1628 { |
|
1629 init_format_state (); |
|
1630 print_e = 1; |
|
1631 } |
|
1632 else if (strcmp (*argv, "E") == 0) |
|
1633 { |
|
1634 init_format_state (); |
|
1635 print_e = 1; |
|
1636 print_big_e = 1; |
|
1637 } |
|
1638 else |
|
1639 { |
217
|
1640 error ("format: unrecognized option `long %s'", *argv); |
1
|
1641 return; |
|
1642 } |
|
1643 } |
|
1644 else |
|
1645 init_format_state (); |
|
1646 |
|
1647 set_output_prec_and_fw (15, 24); |
|
1648 } |
|
1649 else if (strcmp (*argv, "hex") == 0) |
1282
|
1650 { |
|
1651 init_format_state (); |
|
1652 hex_format = 1; |
|
1653 } |
|
1654 else if (strcmp (*argv, "native-hex") == 0) |
|
1655 { |
|
1656 init_format_state (); |
|
1657 hex_format = 2; |
|
1658 } |
1309
|
1659 else if (strcmp (*argv, "bit") == 0) |
|
1660 { |
|
1661 init_format_state (); |
|
1662 bit_format = 1; |
|
1663 } |
|
1664 else if (strcmp (*argv, "native-bit") == 0) |
|
1665 { |
|
1666 init_format_state (); |
|
1667 bit_format = 2; |
|
1668 } |
1282
|
1669 else if (strcmp (*argv, "+") == 0 || strcmp (*argv, "plus") == 0) |
1
|
1670 { |
|
1671 init_format_state (); |
|
1672 plus_format = 1; |
|
1673 } |
|
1674 else if (strcmp (*argv, "bank") == 0) |
|
1675 { |
|
1676 init_format_state (); |
|
1677 bank_format = 1; |
|
1678 } |
|
1679 else if (strcmp (*argv, "free") == 0) |
|
1680 { |
|
1681 init_format_state (); |
|
1682 free_format = 1; |
|
1683 } |
|
1684 else if (strcmp (*argv, "none") == 0) |
|
1685 { |
|
1686 init_format_state (); |
|
1687 free_format = 1; |
|
1688 } |
|
1689 else if (strcmp (*argv, "compact") == 0) |
1186
|
1690 { |
|
1691 compact_format = 1; |
|
1692 } |
1
|
1693 else if (strcmp (*argv, "loose") == 0) |
1186
|
1694 { |
|
1695 compact_format = 0; |
|
1696 } |
1
|
1697 else |
217
|
1698 error ("format: unrecognized format state `%s'", *argv); |
1
|
1699 } |
|
1700 else |
|
1701 usage ("format [format_state]"); |
|
1702 } |
|
1703 else |
|
1704 { |
|
1705 init_format_state (); |
|
1706 set_output_prec_and_fw (5, 10); |
|
1707 } |
|
1708 } |
|
1709 |
1488
|
1710 DEFUN_TEXT ("format", Fformat, Sformat, 10, |
529
|
1711 "format [style]\n\ |
|
1712 \n\ |
|
1713 set output formatting style") |
|
1714 { |
|
1715 Octave_object retval; |
|
1716 |
|
1717 DEFINE_ARGV("format"); |
|
1718 |
|
1719 set_format_style (argc, argv); |
|
1720 |
|
1721 DELETE_ARGV; |
|
1722 |
|
1723 return retval; |
|
1724 } |
|
1725 |
1
|
1726 /* |
|
1727 ;;; Local Variables: *** |
|
1728 ;;; mode: C++ *** |
|
1729 ;;; page-delimiter: "^/\\*" *** |
|
1730 ;;; End: *** |
|
1731 */ |