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