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