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