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