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