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