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