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