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