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