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