1
|
1 // pr-output.cc -*- C++ -*- |
|
2 /* |
|
3 |
296
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
240
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
1
|
26 #endif |
|
27 |
|
28 #include <iostream.h> |
|
29 #include <strstream.h> |
|
30 #include <string.h> |
|
31 #include <math.h> |
|
32 #include <float.h> |
|
33 #include <Complex.h> |
|
34 |
453
|
35 #include "dMatrix.h" |
|
36 #include "CMatrix.h" |
1
|
37 #include "Range.h" |
|
38 |
|
39 #include "tree-const.h" |
|
40 #include "variables.h" |
|
41 #include "user-prefs.h" |
|
42 #include "pr-output.h" |
|
43 #include "mappers.h" |
|
44 #include "pager.h" |
542
|
45 #include "help.h" |
1
|
46 #include "error.h" |
|
47 #include "utils.h" |
529
|
48 #include "defun.h" |
1
|
49 |
|
50 // Current format string for real numbers and the real part of complex |
|
51 // numbers. |
529
|
52 static char *curr_real_fmt = 0; |
1
|
53 |
|
54 // Current format string for the imaginary part of complex numbers. |
529
|
55 static char *curr_imag_fmt = 0; |
1
|
56 |
|
57 // Nonzero means don\'t do any fancy formatting. |
|
58 static int free_format = 0; |
|
59 |
|
60 // Nonzero means print plus sign for nonzero, blank for zero. |
|
61 static int plus_format = 0; |
|
62 |
|
63 // Nonzero means always print like dollars and cents. |
|
64 static int bank_format = 0; |
|
65 |
|
66 // Nonzero means use an e format. |
|
67 static int print_e = 0; |
|
68 |
|
69 // Nonzero means print E instead of e for exponent field. |
|
70 static int print_big_e = 0; |
|
71 |
|
72 static int |
|
73 any_element_is_negative (const Matrix& a) |
|
74 { |
|
75 int nr = a.rows (); |
|
76 int nc = a.columns (); |
|
77 for (int j = 0; j < nc; j++) |
|
78 for (int i = 0; i < nr; i++) |
|
79 if (a.elem (i, j) < 0.0) |
|
80 return 1; |
|
81 return 0; |
|
82 } |
|
83 |
|
84 static int |
|
85 any_element_is_inf_or_nan (const Matrix& a) |
|
86 { |
|
87 int nr = a.rows (); |
|
88 int nc = a.columns (); |
|
89 for (int j = 0; j < nc; j++) |
|
90 for (int i = 0; i < nr; i++) |
|
91 { |
|
92 double val = a.elem (i, j); |
|
93 if (xisinf (val) || xisnan (val)) |
|
94 return 1; |
|
95 } |
|
96 return 0; |
|
97 } |
|
98 |
|
99 static int |
|
100 any_element_is_inf_or_nan (const ComplexMatrix& 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 Complex val = a.elem (i, j); |
|
108 if (xisinf (val) || xisnan (val)) |
|
109 return 1; |
|
110 } |
|
111 return 0; |
|
112 } |
|
113 |
|
114 static int |
|
115 all_elements_are_int_or_inf_or_nan (const Matrix& 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 double val = a.elem (i, j); |
|
123 if (xisnan (val) || D_NINT (val) == val) |
|
124 continue; |
|
125 else |
|
126 return 0; |
|
127 } |
|
128 return 1; |
|
129 } |
|
130 |
|
131 static Matrix |
|
132 abs (const Matrix& a) |
|
133 { |
|
134 int nr = a.rows (); |
|
135 int nc = a.columns (); |
|
136 Matrix retval (nr, nc); |
|
137 for (int j = 0; j < nc; j++) |
|
138 for (int i = 0; i < nr; i++) |
|
139 retval.elem (i, j) = fabs (a.elem (i, j)); |
|
140 return retval; |
|
141 } |
|
142 |
|
143 static double |
164
|
144 pr_max_internal (const Matrix& m) |
1
|
145 { |
|
146 int nr = m.rows (); |
|
147 int nc = m.columns (); |
|
148 |
|
149 double result = DBL_MIN; |
|
150 |
|
151 for (int j = 0; j < nc; j++) |
|
152 for (int i = 0; i < nr; i++) |
|
153 { |
|
154 double val = m.elem (i, j); |
|
155 if (xisinf (val) || xisnan (val)) |
|
156 continue; |
|
157 |
|
158 if (val > result) |
|
159 result = val; |
|
160 } |
|
161 return result; |
|
162 } |
|
163 |
|
164 static double |
164
|
165 pr_min_internal (const Matrix& m) |
1
|
166 { |
|
167 int nr = m.rows (); |
|
168 int nc = m.columns (); |
|
169 |
|
170 double result = DBL_MAX; |
|
171 |
|
172 for (int j = 0; j < nc; j++) |
|
173 for (int i = 0; i < nr; i++) |
|
174 { |
|
175 double val = m.elem (i, j); |
|
176 if (xisinf (val) || xisnan (val)) |
|
177 continue; |
|
178 |
|
179 if (val < result) |
|
180 result = val; |
|
181 } |
|
182 return result; |
|
183 } |
|
184 |
|
185 static void |
|
186 set_format (double d, int& fw) |
|
187 { |
529
|
188 curr_real_fmt = 0; |
|
189 curr_imag_fmt = 0; |
1
|
190 |
|
191 if (free_format) |
|
192 return; |
|
193 |
278
|
194 static char fmt_buf[128]; |
1
|
195 |
|
196 int sign = (d < 0.0); |
|
197 |
|
198 int inf_or_nan = (xisinf (d) || xisnan (d)); |
|
199 |
|
200 double d_abs = d < 0.0 ? -d : d; |
|
201 |
575
|
202 int digits = (inf_or_nan || d_abs == 0.0) ? 0 |
|
203 : (int) floor (log10 (d_abs) + 1.0); |
1
|
204 |
|
205 int prec = user_pref.output_precision; |
|
206 |
|
207 int ld, rd; |
|
208 |
|
209 if (bank_format) |
|
210 { |
|
211 fw = digits < 0 ? 4 : digits + 3; |
|
212 if (inf_or_nan && fw < 3) |
|
213 fw = 3; |
|
214 fw += sign; |
|
215 rd = 2; |
|
216 } |
|
217 else if (xisnan (d) || D_NINT (d) == d) |
|
218 { |
|
219 fw = digits; |
|
220 if (inf_or_nan && fw < 3) |
|
221 fw = 3; |
|
222 fw += sign; |
|
223 rd = 0; |
|
224 } |
|
225 else |
|
226 { |
|
227 if (digits > 0) |
|
228 { |
|
229 ld = digits; |
|
230 rd = prec - digits; |
|
231 digits++; |
|
232 } |
|
233 else |
|
234 { |
|
235 ld = 1; |
|
236 rd = prec - digits; |
|
237 digits = -digits + 1; |
|
238 } |
|
239 |
|
240 fw = ld + 1 + rd; |
|
241 if (inf_or_nan && fw < 3) |
|
242 fw = 3; |
|
243 fw += sign; |
|
244 } |
|
245 |
|
246 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
247 { |
|
248 int exp_field = 4; |
|
249 if (digits > 100) |
|
250 exp_field++; |
|
251 |
|
252 fw = 2 + prec + exp_field; |
|
253 if (inf_or_nan && fw < 3) |
|
254 fw = 3; |
|
255 fw += sign; |
|
256 |
|
257 if (print_big_e) |
|
258 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
259 else |
|
260 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
261 } |
|
262 else |
|
263 { |
|
264 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
265 } |
|
266 |
|
267 curr_real_fmt = &fmt_buf[0]; |
|
268 } |
|
269 |
|
270 static inline void |
|
271 set_format (double d) |
|
272 { |
|
273 int fw; |
|
274 set_format (d, fw); |
|
275 } |
|
276 |
|
277 static void |
164
|
278 set_format (const Matrix& m, int& fw) |
1
|
279 { |
529
|
280 curr_real_fmt = 0; |
|
281 curr_imag_fmt = 0; |
1
|
282 |
|
283 if (free_format) |
|
284 return; |
|
285 |
278
|
286 static char fmt_buf[128]; |
1
|
287 |
|
288 int sign = any_element_is_negative (m); |
|
289 |
|
290 int inf_or_nan = any_element_is_inf_or_nan (m); |
|
291 |
|
292 Matrix m_abs = abs (m); |
|
293 double max_abs = pr_max_internal (m_abs); |
|
294 double min_abs = pr_min_internal (m_abs); |
|
295 |
|
296 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
297 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
298 |
|
299 int prec = user_pref.output_precision; |
|
300 |
|
301 int ld, rd; |
|
302 |
|
303 if (bank_format) |
|
304 { |
|
305 int digits = x_max > x_min ? x_max : x_min; |
|
306 fw = digits <= 0 ? 4 : digits + 3; |
|
307 if (inf_or_nan && fw < 3) |
|
308 fw = 3; |
|
309 fw += sign; |
|
310 rd = 2; |
|
311 } |
|
312 else if (all_elements_are_int_or_inf_or_nan (m)) |
|
313 { |
|
314 int digits = x_max > x_min ? x_max : x_min; |
|
315 fw = digits <= 0 ? 1 : digits; |
|
316 if (inf_or_nan && fw < 3) |
|
317 fw = 3; |
|
318 fw += sign; |
|
319 rd = 0; |
|
320 } |
|
321 else |
|
322 { |
|
323 int ld_max, rd_max; |
|
324 if (x_max > 0) |
|
325 { |
|
326 ld_max = x_max; |
|
327 rd_max = prec - x_max; |
|
328 x_max++; |
|
329 } |
|
330 else |
|
331 { |
|
332 ld_max = 1; |
|
333 rd_max = prec - x_max; |
|
334 x_max = -x_max + 1; |
|
335 } |
|
336 |
|
337 int ld_min, rd_min; |
|
338 if (x_min > 0) |
|
339 { |
|
340 ld_min = x_min; |
|
341 rd_min = prec - x_min; |
|
342 x_min++; |
|
343 } |
|
344 else |
|
345 { |
|
346 ld_min = 1; |
|
347 rd_min = prec - x_min; |
|
348 x_min = -x_min + 1; |
|
349 } |
|
350 |
|
351 ld = ld_max > ld_min ? ld_max : ld_min; |
|
352 rd = rd_max > rd_min ? rd_max : rd_min; |
|
353 |
|
354 fw = ld + 1 + rd; |
|
355 if (inf_or_nan && fw < 3) |
|
356 fw = 3; |
|
357 fw += sign; |
|
358 } |
|
359 |
|
360 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
361 { |
|
362 int exp_field = 4; |
|
363 if (x_max > 100 || x_min > 100) |
|
364 exp_field++; |
|
365 |
|
366 fw = 2 + prec + exp_field; |
|
367 if (inf_or_nan && fw < 3) |
|
368 fw = 3; |
|
369 fw += sign; |
|
370 |
|
371 if (print_big_e) |
|
372 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
373 else |
|
374 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
375 } |
|
376 else |
|
377 { |
|
378 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
379 } |
|
380 |
|
381 curr_real_fmt = &fmt_buf[0]; |
|
382 } |
|
383 |
|
384 static inline void |
164
|
385 set_format (const Matrix& m) |
1
|
386 { |
|
387 int fw; |
|
388 set_format (m, fw); |
|
389 } |
|
390 |
|
391 static void |
164
|
392 set_format (const Complex& c, int& r_fw, int& i_fw) |
1
|
393 { |
529
|
394 curr_real_fmt = 0; |
|
395 curr_imag_fmt = 0; |
1
|
396 |
|
397 if (free_format) |
|
398 return; |
|
399 |
278
|
400 static char r_fmt_buf[128]; |
|
401 static char i_fmt_buf[128]; |
1
|
402 |
|
403 double rp = c.real (); |
|
404 double ip = c.imag (); |
|
405 |
|
406 int sign = (rp < 0.0); |
|
407 |
|
408 int inf_or_nan = (xisinf (c) || xisnan (c)); |
|
409 |
|
410 double r_abs = rp < 0.0 ? -rp : rp; |
|
411 double i_abs = ip < 0.0 ? -ip : ip; |
|
412 |
|
413 int r_x = r_abs == 0.0 ? 0 : (int) floor (log10 (r_abs) + 1.0); |
|
414 int i_x = i_abs == 0.0 ? 0 : (int) floor (log10 (i_abs) + 1.0); |
|
415 |
|
416 int x_max, x_min; |
|
417 |
|
418 if (r_x > i_x) |
|
419 { |
|
420 x_max = r_x; |
|
421 x_min = i_x; |
|
422 } |
|
423 else |
|
424 { |
|
425 x_max = i_x; |
|
426 x_min = r_x; |
|
427 } |
|
428 |
|
429 int prec = user_pref.output_precision; |
|
430 |
|
431 int ld, rd; |
|
432 |
|
433 if (bank_format) |
|
434 { |
|
435 int digits = r_x; |
|
436 i_fw = 0; |
|
437 r_fw = digits <= 0 ? 4 : digits + 3; |
|
438 if (inf_or_nan && r_fw < 3) |
|
439 r_fw = 3; |
|
440 r_fw += sign; |
|
441 rd = 2; |
|
442 } |
|
443 else if (inf_or_nan || (D_NINT (rp) == rp && D_NINT (ip) == ip)) |
|
444 { |
|
445 int digits = x_max > x_min ? x_max : x_min; |
|
446 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
447 if (inf_or_nan && i_fw < 3) |
|
448 i_fw = r_fw = 3; |
|
449 r_fw += sign; |
|
450 rd = 0; |
|
451 } |
|
452 else |
|
453 { |
|
454 int ld_max, rd_max; |
|
455 if (x_max > 0) |
|
456 { |
|
457 ld_max = x_max; |
|
458 rd_max = prec - x_max; |
|
459 x_max++; |
|
460 } |
|
461 else |
|
462 { |
|
463 ld_max = 1; |
|
464 rd_max = prec - x_max; |
|
465 x_max = -x_max + 1; |
|
466 } |
|
467 |
|
468 int ld_min, rd_min; |
|
469 if (x_min > 0) |
|
470 { |
|
471 ld_min = x_min; |
|
472 rd_min = prec - x_min; |
|
473 x_min++; |
|
474 } |
|
475 else |
|
476 { |
|
477 ld_min = 1; |
|
478 rd_min = prec - x_min; |
|
479 x_min = -x_min + 1; |
|
480 } |
|
481 |
|
482 ld = ld_max > ld_min ? ld_max : ld_min; |
|
483 rd = rd_max > rd_min ? rd_max : rd_min; |
|
484 |
|
485 i_fw = r_fw = ld + 1 + rd; |
|
486 if (inf_or_nan && i_fw < 3) |
|
487 i_fw = r_fw = 3; |
|
488 r_fw += sign; |
|
489 } |
|
490 |
|
491 if (! bank_format && (r_fw > user_pref.output_max_field_width || print_e)) |
|
492 { |
|
493 int exp_field = 4; |
|
494 if (x_max > 100 || x_min > 100) |
|
495 exp_field++; |
|
496 |
|
497 i_fw = r_fw = 1 + prec + exp_field; |
|
498 if (inf_or_nan && i_fw < 3) |
|
499 i_fw = r_fw = 3; |
|
500 r_fw += sign; |
|
501 |
|
502 if (print_big_e) |
|
503 { |
|
504 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
505 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
506 } |
|
507 else |
|
508 { |
|
509 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
510 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
511 } |
|
512 } |
|
513 else |
|
514 { |
|
515 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
516 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
517 } |
|
518 |
|
519 curr_real_fmt = &r_fmt_buf[0]; |
|
520 curr_imag_fmt = &i_fmt_buf[0]; |
|
521 } |
|
522 |
|
523 static inline void |
164
|
524 set_format (const Complex& c) |
1
|
525 { |
|
526 int r_fw, i_fw; |
|
527 set_format (c, r_fw, i_fw); |
|
528 } |
|
529 |
|
530 static void |
164
|
531 set_format (const ComplexMatrix& cm, int& r_fw, int& i_fw) |
1
|
532 { |
529
|
533 curr_real_fmt = 0; |
|
534 curr_imag_fmt = 0; |
1
|
535 |
|
536 if (free_format) |
|
537 return; |
|
538 |
278
|
539 static char r_fmt_buf[128]; |
|
540 static char i_fmt_buf[128]; |
1
|
541 |
|
542 Matrix rp = real (cm); |
|
543 Matrix ip = imag (cm); |
|
544 |
|
545 int sign = any_element_is_negative (rp); |
|
546 |
|
547 int inf_or_nan = any_element_is_inf_or_nan (cm); |
|
548 |
|
549 Matrix r_m_abs = abs (rp); |
|
550 double r_max_abs = pr_max_internal (r_m_abs); |
|
551 double r_min_abs = pr_min_internal (r_m_abs); |
|
552 |
|
553 Matrix i_m_abs = abs (ip); |
|
554 double i_max_abs = pr_max_internal (i_m_abs); |
|
555 double i_min_abs = pr_min_internal (i_m_abs); |
|
556 |
|
557 int r_x_max = r_max_abs == 0.0 ? 0 : (int) floor (log10 (r_max_abs) + 1.0); |
|
558 int r_x_min = r_min_abs == 0.0 ? 0 : (int) floor (log10 (r_min_abs) + 1.0); |
|
559 |
|
560 int i_x_max = i_max_abs == 0.0 ? 0 : (int) floor (log10 (i_max_abs) + 1.0); |
|
561 int i_x_min = i_min_abs == 0.0 ? 0 : (int) floor (log10 (i_min_abs) + 1.0); |
|
562 |
|
563 int x_max = r_x_max > i_x_max ? r_x_max : i_x_max; |
|
564 int x_min = r_x_min > i_x_min ? r_x_min : i_x_min; |
|
565 |
|
566 int prec = user_pref.output_precision; |
|
567 |
|
568 int ld, rd; |
|
569 |
|
570 if (bank_format) |
|
571 { |
|
572 int digits = r_x_max > r_x_min ? r_x_max : r_x_min; |
|
573 i_fw = 0; |
|
574 r_fw = digits <= 0 ? 4 : digits + 3; |
|
575 if (inf_or_nan && i_fw < 3) |
|
576 i_fw = r_fw = 3; |
|
577 r_fw += sign; |
|
578 rd = 2; |
|
579 } |
|
580 else if (all_elements_are_int_or_inf_or_nan (rp) |
|
581 && all_elements_are_int_or_inf_or_nan (ip)) |
|
582 { |
|
583 int digits = x_max > x_min ? x_max : x_min; |
|
584 i_fw = r_fw = digits <= 0 ? 1 : digits; |
|
585 if (inf_or_nan && i_fw < 3) |
|
586 i_fw = r_fw = 3; |
|
587 r_fw += sign; |
|
588 rd = 0; |
|
589 } |
|
590 else |
|
591 { |
|
592 int ld_max, rd_max; |
|
593 if (x_max > 0) |
|
594 { |
|
595 ld_max = x_max; |
|
596 rd_max = prec - x_max; |
|
597 x_max++; |
|
598 } |
|
599 else |
|
600 { |
|
601 ld_max = 1; |
|
602 rd_max = prec - x_max; |
|
603 x_max = -x_max + 1; |
|
604 } |
|
605 |
|
606 int ld_min, rd_min; |
|
607 if (x_min > 0) |
|
608 { |
|
609 ld_min = x_min; |
|
610 rd_min = prec - x_min; |
|
611 x_min++; |
|
612 } |
|
613 else |
|
614 { |
|
615 ld_min = 1; |
|
616 rd_min = prec - x_min; |
|
617 x_min = -x_min + 1; |
|
618 } |
|
619 |
|
620 ld = ld_max > ld_min ? ld_max : ld_min; |
|
621 rd = rd_max > rd_min ? rd_max : rd_min; |
|
622 |
|
623 i_fw = r_fw = ld + 1 + rd; |
|
624 if (inf_or_nan && i_fw < 3) |
|
625 i_fw = r_fw = 3; |
|
626 r_fw += sign; |
|
627 } |
|
628 |
|
629 if (! bank_format && (r_fw > user_pref.output_max_field_width || print_e)) |
|
630 { |
|
631 int exp_field = 4; |
|
632 if (x_max > 100 || x_min > 100) |
|
633 exp_field++; |
|
634 |
|
635 i_fw = r_fw = 1 + prec + exp_field; |
|
636 if (inf_or_nan && i_fw < 3) |
|
637 i_fw = r_fw = 3; |
|
638 r_fw += sign; |
|
639 |
|
640 if (print_big_e) |
|
641 { |
|
642 sprintf (r_fmt_buf, "%%%d.%dE", r_fw, prec - 1); |
|
643 sprintf (i_fmt_buf, "%%%d.%dE", i_fw, prec - 1); |
|
644 } |
|
645 else |
|
646 { |
|
647 sprintf (r_fmt_buf, "%%%d.%de", r_fw, prec - 1); |
|
648 sprintf (i_fmt_buf, "%%%d.%de", i_fw, prec - 1); |
|
649 } |
|
650 } |
|
651 else |
|
652 { |
|
653 sprintf (r_fmt_buf, "%%%d.%df", r_fw, rd); |
|
654 sprintf (i_fmt_buf, "%%%d.%df", i_fw, rd); |
|
655 } |
|
656 |
|
657 curr_real_fmt = &r_fmt_buf[0]; |
|
658 curr_imag_fmt = &i_fmt_buf[0]; |
|
659 } |
|
660 |
|
661 static int |
164
|
662 all_elements_are_ints (const Range& r) |
1
|
663 { |
|
664 // If the base and increment are ints, the final value in the range |
|
665 // will also be an integer, even if the limit is not. |
|
666 |
|
667 double b = r.base (); |
|
668 double i = r.inc (); |
|
669 |
|
670 return ((double) NINT (b) == b && (double) NINT (i) == i); |
|
671 } |
|
672 |
|
673 static inline void |
164
|
674 set_format (const ComplexMatrix& cm) |
1
|
675 { |
|
676 int r_fw, i_fw; |
|
677 set_format (cm, r_fw, i_fw); |
|
678 } |
|
679 |
|
680 static void |
164
|
681 set_format (const Range& r, int& fw) |
1
|
682 { |
529
|
683 curr_real_fmt = 0; |
|
684 curr_imag_fmt = 0; |
1
|
685 |
|
686 if (free_format) |
|
687 return; |
|
688 |
278
|
689 static char fmt_buf[128]; |
1
|
690 |
|
691 double r_min = r.base (); |
|
692 double r_max = r.limit (); |
|
693 |
|
694 if (r_max < r_min) |
|
695 { |
|
696 double tmp = r_max; |
|
697 r_max = r_min; |
|
698 r_min = tmp; |
|
699 } |
|
700 |
|
701 int sign = (r_min < 0.0); |
|
702 |
|
703 double max_abs = r_max < 0.0 ? -r_max : r_max; |
|
704 double min_abs = r_min < 0.0 ? -r_min : r_min; |
|
705 |
|
706 int x_max = max_abs == 0.0 ? 0 : (int) floor (log10 (max_abs) + 1.0); |
|
707 int x_min = min_abs == 0.0 ? 0 : (int) floor (log10 (min_abs) + 1.0); |
|
708 |
|
709 int prec = user_pref.output_precision; |
|
710 |
|
711 int ld, rd; |
|
712 |
|
713 if (bank_format) |
|
714 { |
|
715 int digits = x_max > x_min ? x_max : x_min; |
|
716 fw = sign + digits < 0 ? 4 : digits + 3; |
|
717 rd = 2; |
|
718 } |
|
719 else if (all_elements_are_ints (r)) |
|
720 { |
|
721 int digits = x_max > x_min ? x_max : x_min; |
|
722 fw = sign + digits; |
|
723 rd = 0; |
|
724 } |
|
725 else |
|
726 { |
|
727 int ld_max, rd_max; |
|
728 if (x_max > 0) |
|
729 { |
|
730 ld_max = x_max; |
|
731 rd_max = prec - x_max; |
|
732 x_max++; |
|
733 } |
|
734 else |
|
735 { |
|
736 ld_max = 1; |
|
737 rd_max = prec - x_max; |
|
738 x_max = -x_max + 1; |
|
739 } |
|
740 |
|
741 int ld_min, rd_min; |
|
742 if (x_min > 0) |
|
743 { |
|
744 ld_min = x_min; |
|
745 rd_min = prec - x_min; |
|
746 x_min++; |
|
747 } |
|
748 else |
|
749 { |
|
750 ld_min = 1; |
|
751 rd_min = prec - x_min; |
|
752 x_min = -x_min + 1; |
|
753 } |
|
754 |
|
755 ld = ld_max > ld_min ? ld_max : ld_min; |
|
756 rd = rd_max > rd_min ? rd_max : rd_min; |
|
757 |
|
758 fw = sign + ld + 1 + rd; |
|
759 } |
|
760 |
|
761 if (! bank_format && (fw > user_pref.output_max_field_width || print_e)) |
|
762 { |
|
763 int exp_field = 4; |
|
764 if (x_max > 100 || x_min > 100) |
|
765 exp_field++; |
|
766 |
|
767 fw = sign + 2 + prec + exp_field; |
|
768 |
|
769 if (print_big_e) |
|
770 sprintf (fmt_buf, "%%%d.%dE", fw, prec - 1); |
|
771 else |
|
772 sprintf (fmt_buf, "%%%d.%de", fw, prec - 1); |
|
773 } |
|
774 else |
|
775 { |
|
776 sprintf (fmt_buf, "%%%d.%df", fw, rd); |
|
777 } |
|
778 |
|
779 curr_real_fmt = &fmt_buf[0]; |
|
780 } |
|
781 |
|
782 static inline void |
164
|
783 set_format (const Range& r) |
1
|
784 { |
|
785 int fw; |
|
786 set_format (r, fw); |
|
787 } |
|
788 |
|
789 static inline void |
164
|
790 pr_any_float (const char *fmt, ostrstream& os, double d, int fw = 0) |
1
|
791 { |
|
792 if (d == -0.0) |
|
793 d = 0.0; |
|
794 |
529
|
795 if (fmt) |
1
|
796 { |
|
797 if (xisinf (d)) |
|
798 { |
|
799 char *s; |
|
800 if (d < 0.0) |
|
801 s = "-Inf"; |
|
802 else |
|
803 s = "Inf"; |
|
804 |
|
805 if (fw > 0) |
|
806 os.form ("%*s", fw, s); |
|
807 else |
|
808 os << s; |
|
809 } |
|
810 else if (xisnan (d)) |
|
811 { |
|
812 if (fw > 0) |
|
813 os.form ("%*s", fw, "NaN"); |
|
814 else |
|
815 os << "NaN"; |
|
816 } |
|
817 else |
|
818 os.form (fmt, d); |
|
819 } |
529
|
820 else |
|
821 os << d; |
1
|
822 } |
|
823 |
|
824 static inline void |
|
825 pr_float (ostrstream& os, double d, int fw = 0) |
|
826 { |
|
827 pr_any_float (curr_real_fmt, os, d, fw); |
|
828 } |
|
829 |
|
830 static inline void |
|
831 pr_imag_float (ostrstream& os, double d, int fw = 0) |
|
832 { |
|
833 pr_any_float (curr_imag_fmt, os, d, fw); |
|
834 } |
|
835 |
|
836 static inline void |
164
|
837 pr_complex (ostrstream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
838 { |
|
839 double r = c.real (); |
|
840 pr_float (os, r, r_fw); |
|
841 if (! bank_format) |
|
842 { |
|
843 double i = c.imag (); |
|
844 if (i < 0) |
|
845 { |
|
846 os << " - "; |
|
847 i = -i; |
|
848 pr_imag_float (os, i, i_fw); |
|
849 } |
|
850 else |
|
851 { |
|
852 os << " + "; |
|
853 pr_imag_float (os, i, i_fw); |
|
854 } |
|
855 os << "i"; |
|
856 } |
|
857 } |
|
858 |
|
859 void |
|
860 octave_print_internal (ostrstream& os, double d) |
|
861 { |
|
862 if (plus_format) |
|
863 { |
|
864 if (d == 0.0) |
|
865 os << " "; |
|
866 else |
|
867 os << "+"; |
|
868 } |
|
869 else |
|
870 { |
|
871 set_format (d); |
|
872 if (free_format) |
|
873 os << d; |
|
874 else |
|
875 pr_float (os, d); |
|
876 } |
|
877 os << "\n"; |
|
878 } |
|
879 |
|
880 void |
164
|
881 octave_print_internal (ostrstream& os, const Matrix& m) |
1
|
882 { |
|
883 int nr = m.rows (); |
|
884 int nc = m.columns (); |
|
885 |
|
886 if (plus_format) |
|
887 { |
|
888 for (int i = 0; i < nr; i++) |
|
889 { |
|
890 for (int j = 0; j < nc; j++) |
|
891 { |
|
892 if (j == 0) |
|
893 os << " "; |
|
894 |
|
895 if (m.elem (i, j) == 0.0) |
|
896 os << " "; |
|
897 else |
|
898 os << "+"; |
|
899 } |
|
900 os << "\n"; |
|
901 } |
|
902 } |
|
903 else |
|
904 { |
|
905 int fw; |
|
906 set_format (m, fw); |
|
907 int column_width = fw + 2; |
|
908 int total_width = nc * column_width; |
|
909 int max_width = terminal_columns (); |
|
910 |
|
911 if (free_format) |
|
912 { |
|
913 os << m; |
|
914 return; |
|
915 } |
|
916 |
|
917 int inc = nc; |
|
918 if (total_width > max_width && user_pref.split_long_rows) |
|
919 { |
|
920 inc = max_width / column_width; |
|
921 if (inc == 0) |
|
922 inc++; |
|
923 } |
|
924 |
|
925 int col = 0; |
|
926 while (col < nc) |
|
927 { |
|
928 int lim = col + inc < nc ? col + inc : nc; |
|
929 |
|
930 if (total_width > max_width && user_pref.split_long_rows) |
|
931 { |
|
932 if (col != 0) |
|
933 os << "\n"; |
|
934 |
|
935 int num_cols = lim - col; |
|
936 if (num_cols == 1) |
|
937 os << " Column " << col + 1 << ":\n\n"; |
|
938 else if (num_cols == 2) |
|
939 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
940 else |
|
941 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
942 } |
|
943 |
|
944 for (int i = 0; i < nr; i++) |
|
945 { |
|
946 for (int j = col; j < lim; j++) |
|
947 { |
|
948 os << " "; |
|
949 pr_float(os, m.elem (i, j), fw); |
|
950 } |
|
951 os << "\n"; |
|
952 } |
|
953 col += inc; |
|
954 } |
|
955 } |
|
956 } |
|
957 |
|
958 void |
164
|
959 octave_print_internal (ostrstream& os, const Complex& c) |
1
|
960 { |
|
961 if (plus_format) |
|
962 { |
|
963 if (c == 0.0) |
|
964 os << " "; |
|
965 else |
|
966 os << "+"; |
|
967 } |
|
968 else |
|
969 { |
|
970 set_format (c); |
|
971 if (free_format) |
|
972 os << c; |
|
973 else |
|
974 pr_complex (os, c); |
|
975 } |
|
976 os << "\n"; |
|
977 } |
|
978 |
|
979 void |
164
|
980 octave_print_internal (ostrstream& os, const ComplexMatrix& cm) |
1
|
981 { |
|
982 int nr = cm.rows (); |
|
983 int nc = cm.columns (); |
|
984 |
|
985 if (plus_format) |
|
986 { |
|
987 for (int i = 0; i < nr; i++) |
|
988 { |
|
989 for (int j = 0; j < nc; j++) |
|
990 { |
|
991 if (j == 0) |
|
992 os << " "; |
|
993 |
|
994 if (cm.elem (i, j) == 0.0) |
|
995 os << " "; |
|
996 else |
|
997 os << "+"; |
|
998 } |
|
999 os << "\n"; |
|
1000 } |
|
1001 } |
|
1002 else |
|
1003 { |
|
1004 int r_fw, i_fw; |
|
1005 set_format (cm, r_fw, i_fw); |
|
1006 int column_width = i_fw + r_fw; |
|
1007 column_width += bank_format ? 2 : 7; |
|
1008 int total_width = nc * column_width; |
|
1009 int max_width = terminal_columns (); |
|
1010 |
|
1011 if (free_format) |
|
1012 { |
|
1013 os << cm; |
|
1014 return; |
|
1015 } |
|
1016 |
|
1017 int inc = nc; |
|
1018 if (total_width > max_width && user_pref.split_long_rows) |
|
1019 { |
|
1020 inc = max_width / column_width; |
|
1021 if (inc == 0) |
|
1022 inc++; |
|
1023 } |
|
1024 |
|
1025 int col = 0; |
|
1026 while (col < nc) |
|
1027 { |
|
1028 int lim = col + inc < nc ? col + inc : nc; |
|
1029 |
|
1030 if (total_width > max_width && user_pref.split_long_rows) |
|
1031 { |
|
1032 if (col != 0) |
|
1033 os << "\n"; |
|
1034 |
|
1035 int num_cols = lim - col; |
|
1036 if (num_cols == 1) |
|
1037 os << " Column " << col + 1 << ":\n\n"; |
|
1038 else if (num_cols == 2) |
|
1039 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
1040 else |
|
1041 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
1042 } |
|
1043 |
|
1044 for (int i = 0; i < nr; i++) |
|
1045 { |
|
1046 for (int j = col; j < lim; j++) |
|
1047 { |
|
1048 if (bank_format) |
|
1049 os << " "; |
|
1050 else |
|
1051 os << " "; |
|
1052 pr_complex (os, cm.elem (i, j), r_fw, i_fw); |
|
1053 } |
|
1054 os << "\n"; |
|
1055 } |
|
1056 col += inc; |
|
1057 } |
|
1058 } |
|
1059 } |
|
1060 |
|
1061 void |
164
|
1062 octave_print_internal (ostrstream& os, const Range& r) |
1
|
1063 { |
|
1064 double b = r.base (); |
|
1065 double increment = r.inc (); |
|
1066 int num_elem = r.nelem (); |
|
1067 |
|
1068 if (plus_format) |
|
1069 { |
|
1070 os << " "; |
|
1071 for (int i = 0; i < num_elem; i++) |
|
1072 { |
|
1073 double val = b + i * increment; |
|
1074 if (val == 0.0) |
|
1075 os << " "; |
|
1076 else |
|
1077 os << "+"; |
|
1078 } |
|
1079 } |
|
1080 else |
|
1081 { |
|
1082 int fw; |
|
1083 set_format (r, fw); |
|
1084 int column_width = fw + 2; |
|
1085 int total_width = num_elem * column_width; |
|
1086 int max_width = terminal_columns (); |
|
1087 |
|
1088 if (free_format) |
|
1089 { |
|
1090 os << r; |
|
1091 return; |
|
1092 } |
|
1093 |
|
1094 int inc = num_elem; |
|
1095 if (total_width > max_width && user_pref.split_long_rows) |
|
1096 { |
|
1097 inc = max_width / column_width; |
|
1098 if (inc == 0) |
|
1099 inc++; |
|
1100 } |
|
1101 |
|
1102 int col = 0; |
|
1103 while (col < num_elem) |
|
1104 { |
|
1105 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1106 |
|
1107 if (total_width > max_width && user_pref.split_long_rows) |
|
1108 { |
|
1109 if (col != 0) |
|
1110 os << "\n"; |
|
1111 |
|
1112 int num_cols = lim - col; |
|
1113 if (num_cols == 1) |
|
1114 os << " Column " << col + 1 << ":\n\n"; |
|
1115 else if (num_cols == 2) |
|
1116 os << " Columns " << col + 1 << " and " << lim << ":\n\n"; |
|
1117 else |
|
1118 os << " Columns " << col + 1 << " through " << lim << ":\n\n"; |
|
1119 } |
|
1120 |
|
1121 for (int i = col; i < lim; i++) |
|
1122 { |
|
1123 double val = b + i * increment; |
|
1124 os << " "; |
|
1125 pr_float (os, val, fw); |
|
1126 } |
|
1127 |
|
1128 os << "\n"; |
|
1129 |
|
1130 col += inc; |
|
1131 } |
|
1132 } |
|
1133 } |
|
1134 |
529
|
1135 DEFUN ("disp", Fdisp, Sdisp, 3, 1, |
|
1136 "disp (X): display value without name tag") |
|
1137 { |
|
1138 Octave_object retval; |
|
1139 |
|
1140 int nargin = args.length (); |
|
1141 |
|
1142 if (nargin == 2) |
|
1143 args(1).eval (1); |
|
1144 else |
|
1145 print_usage ("disp"); |
|
1146 |
|
1147 return retval; |
|
1148 } |
|
1149 |
1
|
1150 static void |
|
1151 init_format_state (void) |
|
1152 { |
|
1153 free_format = 0; |
|
1154 plus_format = 0; |
|
1155 bank_format = 0; |
|
1156 print_e = 0; |
|
1157 print_big_e = 0; |
|
1158 } |
|
1159 |
|
1160 static void |
|
1161 set_output_prec_and_fw (int prec, int fw) |
|
1162 { |
529
|
1163 tree_constant *tmp = 0; |
1
|
1164 |
|
1165 tmp = new tree_constant ((double) prec); |
195
|
1166 bind_builtin_variable ("output_precision", tmp); |
1
|
1167 |
|
1168 tmp = new tree_constant ((double) fw); |
195
|
1169 bind_builtin_variable ("output_max_field_width", tmp); |
1
|
1170 } |
|
1171 |
|
1172 void |
|
1173 set_format_style (int argc, char **argv) |
|
1174 { |
|
1175 if (--argc > 0) |
|
1176 { |
|
1177 argv++; |
|
1178 if (*argv[0]) |
|
1179 { |
|
1180 if (strcmp (*argv, "short") == 0) |
|
1181 { |
|
1182 if (--argc > 0) |
|
1183 { |
|
1184 argv++; |
|
1185 if (strcmp (*argv, "e") == 0) |
|
1186 { |
|
1187 init_format_state (); |
|
1188 print_e = 1; |
|
1189 } |
|
1190 else if (strcmp (*argv, "E") == 0) |
|
1191 { |
|
1192 init_format_state (); |
|
1193 print_e = 1; |
|
1194 print_big_e = 1; |
|
1195 } |
|
1196 else |
|
1197 { |
217
|
1198 error ("format: unrecognized option `short %s'", *argv); |
1
|
1199 return; |
|
1200 } |
|
1201 } |
|
1202 else |
|
1203 init_format_state (); |
|
1204 |
|
1205 set_output_prec_and_fw (3, 8); |
|
1206 } |
|
1207 else if (strcmp (*argv, "long") == 0) |
|
1208 { |
|
1209 if (--argc > 0) |
|
1210 { |
|
1211 argv++; |
|
1212 if (strcmp (*argv, "e") == 0) |
|
1213 { |
|
1214 init_format_state (); |
|
1215 print_e = 1; |
|
1216 } |
|
1217 else if (strcmp (*argv, "E") == 0) |
|
1218 { |
|
1219 init_format_state (); |
|
1220 print_e = 1; |
|
1221 print_big_e = 1; |
|
1222 } |
|
1223 else |
|
1224 { |
217
|
1225 error ("format: unrecognized option `long %s'", *argv); |
1
|
1226 return; |
|
1227 } |
|
1228 } |
|
1229 else |
|
1230 init_format_state (); |
|
1231 |
|
1232 set_output_prec_and_fw (15, 24); |
|
1233 } |
|
1234 else if (strcmp (*argv, "hex") == 0) |
217
|
1235 error ("format: format state `hex' not implemented yet"); |
1
|
1236 else if (strcmp (*argv, "+") == 0) |
|
1237 { |
|
1238 init_format_state (); |
|
1239 plus_format = 1; |
|
1240 } |
|
1241 else if (strcmp (*argv, "bank") == 0) |
|
1242 { |
|
1243 init_format_state (); |
|
1244 bank_format = 1; |
|
1245 } |
|
1246 else if (strcmp (*argv, "free") == 0) |
|
1247 { |
|
1248 init_format_state (); |
|
1249 free_format = 1; |
|
1250 } |
|
1251 else if (strcmp (*argv, "none") == 0) |
|
1252 { |
|
1253 init_format_state (); |
|
1254 free_format = 1; |
|
1255 } |
|
1256 else if (strcmp (*argv, "compact") == 0) |
217
|
1257 error ("format: format state `compact' not implemented yet"); |
1
|
1258 else if (strcmp (*argv, "loose") == 0) |
217
|
1259 error ("format: format state `loose' not implemented yet"); |
1
|
1260 else |
217
|
1261 error ("format: unrecognized format state `%s'", *argv); |
1
|
1262 } |
|
1263 else |
|
1264 usage ("format [format_state]"); |
|
1265 } |
|
1266 else |
|
1267 { |
|
1268 init_format_state (); |
|
1269 set_output_prec_and_fw (5, 10); |
|
1270 } |
|
1271 } |
|
1272 |
529
|
1273 DEFUN_TEXT ("format", Fformat, Sformat, -1, 1, |
|
1274 "format [style]\n\ |
|
1275 \n\ |
|
1276 set output formatting style") |
|
1277 { |
|
1278 Octave_object retval; |
|
1279 |
|
1280 DEFINE_ARGV("format"); |
|
1281 |
|
1282 set_format_style (argc, argv); |
|
1283 |
|
1284 DELETE_ARGV; |
|
1285 |
|
1286 return retval; |
|
1287 } |
|
1288 |
1
|
1289 /* |
|
1290 ;;; Local Variables: *** |
|
1291 ;;; mode: C++ *** |
|
1292 ;;; page-delimiter: "^/\\*" *** |
|
1293 ;;; End: *** |
|
1294 */ |