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