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