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