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