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