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