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 |
1
|
891 static inline void |
626
|
892 pr_any_float (const char *fmt, ostream& os, double d, int fw = 0) |
1
|
893 { |
|
894 if (d == -0.0) |
|
895 d = 0.0; |
|
896 |
529
|
897 if (fmt) |
1
|
898 { |
1282
|
899 if (hex_format) |
|
900 { |
|
901 equiv tmp; |
|
902 tmp.d = d; |
|
903 |
|
904 // Unless explicitly asked for, always print in big-endian |
|
905 // format. |
|
906 |
|
907 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
908 // formats and not for Cray? |
|
909 |
2317
|
910 oct_mach_info::float_format flt_fmt = |
|
911 oct_mach_info::native_float_format (); |
|
912 |
1282
|
913 if (hex_format > 1 |
2317
|
914 || flt_fmt == oct_mach_info::ieee_big_endian |
|
915 || flt_fmt == oct_mach_info::cray |
|
916 || flt_fmt == oct_mach_info::unknown) |
1282
|
917 { |
1322
|
918 for (size_t i = 0; i < sizeof (double); i++) |
1282
|
919 os.form ("%02x", (int) tmp.i[i]); |
|
920 } |
|
921 else |
|
922 { |
1328
|
923 for (int i = sizeof (double) - 1; i >= 0; i--) |
1282
|
924 os.form ("%02x", (int) tmp.i[i]); |
|
925 } |
|
926 } |
1309
|
927 else if (bit_format) |
|
928 { |
|
929 equiv tmp; |
|
930 tmp.d = d; |
|
931 |
|
932 // Unless explicitly asked for, always print in big-endian |
|
933 // format. |
|
934 |
|
935 // XXX FIXME XXX -- is it correct to swap bytes for VAX |
|
936 // formats and not for Cray? |
|
937 |
2317
|
938 oct_mach_info::float_format flt_fmt = |
|
939 oct_mach_info::native_float_format (); |
|
940 |
|
941 if (flt_fmt == oct_mach_info::ieee_big_endian |
|
942 || flt_fmt == oct_mach_info::cray |
|
943 || flt_fmt == oct_mach_info::unknown) |
1309
|
944 { |
1322
|
945 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
946 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
947 } |
|
948 else |
|
949 { |
|
950 if (bit_format > 1) |
|
951 { |
1328
|
952 for (size_t i = 0; i < sizeof (double); i++) |
1309
|
953 PRINT_CHAR_BITS_SWAPPED (os, tmp.i[i]); |
|
954 } |
|
955 else |
|
956 { |
|
957 for (int i = sizeof (double) - 1; i >= 0; i--) |
|
958 PRINT_CHAR_BITS (os, tmp.i[i]); |
|
959 } |
|
960 } |
|
961 } |
1282
|
962 else if (xisinf (d)) |
1
|
963 { |
|
964 char *s; |
|
965 if (d < 0.0) |
|
966 s = "-Inf"; |
|
967 else |
|
968 s = "Inf"; |
|
969 |
|
970 if (fw > 0) |
|
971 os.form ("%*s", fw, s); |
|
972 else |
|
973 os << s; |
|
974 } |
|
975 else if (xisnan (d)) |
|
976 { |
|
977 if (fw > 0) |
|
978 os.form ("%*s", fw, "NaN"); |
|
979 else |
|
980 os << "NaN"; |
|
981 } |
|
982 else |
|
983 os.form (fmt, d); |
|
984 } |
529
|
985 else |
|
986 os << d; |
1
|
987 } |
|
988 |
|
989 static inline void |
626
|
990 pr_float (ostream& os, double d, int fw = 0) |
1
|
991 { |
|
992 pr_any_float (curr_real_fmt, os, d, fw); |
|
993 } |
|
994 |
|
995 static inline void |
626
|
996 pr_imag_float (ostream& os, double d, int fw = 0) |
1
|
997 { |
|
998 pr_any_float (curr_imag_fmt, os, d, fw); |
|
999 } |
|
1000 |
|
1001 static inline void |
626
|
1002 pr_complex (ostream& os, const Complex& c, int r_fw = 0, int i_fw = 0) |
1
|
1003 { |
|
1004 double r = c.real (); |
|
1005 pr_float (os, r, r_fw); |
|
1006 if (! bank_format) |
|
1007 { |
|
1008 double i = c.imag (); |
1309
|
1009 if (! (hex_format || bit_format) && i < 0) |
1
|
1010 { |
|
1011 os << " - "; |
|
1012 i = -i; |
|
1013 pr_imag_float (os, i, i_fw); |
|
1014 } |
|
1015 else |
|
1016 { |
1309
|
1017 if (hex_format || bit_format) |
1282
|
1018 os << " "; |
|
1019 else |
|
1020 os << " + "; |
|
1021 |
1
|
1022 pr_imag_float (os, i, i_fw); |
|
1023 } |
|
1024 os << "i"; |
|
1025 } |
|
1026 } |
|
1027 |
626
|
1028 static void |
1972
|
1029 print_empty_matrix (ostream& os, int nr, int nc, bool pr_as_read_syntax) |
626
|
1030 { |
|
1031 assert (nr == 0 || nc == 0); |
|
1032 |
|
1033 if (pr_as_read_syntax) |
|
1034 { |
|
1035 if (nr == 0 && nc == 0) |
|
1036 os << "[]"; |
|
1037 else |
|
1038 os << "zeros (" << nr << ", " << nc << ")"; |
|
1039 } |
|
1040 else |
|
1041 { |
|
1042 os << "[]"; |
2165
|
1043 if (Vprint_empty_dimensions) |
626
|
1044 os << "(" << nr << "x" << nc << ")"; |
|
1045 os << "\n"; |
|
1046 } |
|
1047 } |
|
1048 |
1186
|
1049 static void |
|
1050 pr_col_num_header (ostream& os, int total_width, int max_width, |
1972
|
1051 int lim, int col, int extra_indent) |
1186
|
1052 { |
2165
|
1053 if (total_width > max_width && Vsplit_long_rows) |
1186
|
1054 { |
|
1055 if (col != 0 && ! compact_format) |
|
1056 os << "\n"; |
|
1057 |
|
1058 int num_cols = lim - col; |
|
1059 |
1972
|
1060 os.form ("%*s", extra_indent, ""); |
|
1061 |
1186
|
1062 if (num_cols == 1) |
|
1063 os << " Column " << col + 1 << ":\n"; |
|
1064 else if (num_cols == 2) |
|
1065 os << " Columns " << col + 1 << " and " << lim << ":\n"; |
|
1066 else |
|
1067 os << " Columns " << col + 1 << " through " << lim << ":\n"; |
|
1068 |
|
1069 if (! compact_format) |
|
1070 os << "\n"; |
|
1071 } |
|
1072 } |
|
1073 |
1
|
1074 void |
1972
|
1075 octave_print_internal (ostream& os, double d, bool pr_as_read_syntax) |
1
|
1076 { |
|
1077 if (plus_format) |
|
1078 { |
|
1079 if (d == 0.0) |
|
1080 os << " "; |
|
1081 else |
|
1082 os << "+"; |
|
1083 } |
|
1084 else |
|
1085 { |
|
1086 set_format (d); |
|
1087 if (free_format) |
|
1088 os << d; |
|
1089 else |
|
1090 pr_float (os, d); |
|
1091 } |
626
|
1092 |
|
1093 if (! pr_as_read_syntax) |
|
1094 os << "\n"; |
1
|
1095 } |
|
1096 |
|
1097 void |
1972
|
1098 octave_print_internal (ostream& os, const Matrix& m, bool pr_as_read_syntax, |
|
1099 int extra_indent) |
1
|
1100 { |
|
1101 int nr = m.rows (); |
|
1102 int nc = m.columns (); |
|
1103 |
2408
|
1104 if (nr == 0 || nc == 0) |
626
|
1105 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1106 else if (plus_format && ! pr_as_read_syntax) |
1
|
1107 { |
|
1108 for (int i = 0; i < nr; i++) |
|
1109 { |
|
1110 for (int j = 0; j < nc; j++) |
|
1111 { |
|
1112 if (j == 0) |
|
1113 os << " "; |
|
1114 |
2305
|
1115 if (m (i, j) == 0.0) |
1
|
1116 os << " "; |
|
1117 else |
|
1118 os << "+"; |
|
1119 } |
|
1120 os << "\n"; |
|
1121 } |
|
1122 } |
|
1123 else |
|
1124 { |
|
1125 int fw; |
|
1126 set_format (m, fw); |
|
1127 int column_width = fw + 2; |
|
1128 int total_width = nc * column_width; |
|
1129 int max_width = terminal_columns (); |
|
1130 |
626
|
1131 if (pr_as_read_syntax) |
|
1132 max_width -= 4; |
1972
|
1133 else |
|
1134 max_width -= extra_indent; |
|
1135 |
|
1136 if (max_width < 0) |
|
1137 max_width = 0; |
626
|
1138 |
1
|
1139 if (free_format) |
|
1140 { |
626
|
1141 if (pr_as_read_syntax) |
|
1142 os << "[\n"; |
|
1143 |
1
|
1144 os << m; |
626
|
1145 |
|
1146 if (pr_as_read_syntax) |
|
1147 os << "]"; |
|
1148 |
1
|
1149 return; |
|
1150 } |
|
1151 |
|
1152 int inc = nc; |
2165
|
1153 if (total_width > max_width && Vsplit_long_rows) |
1
|
1154 { |
|
1155 inc = max_width / column_width; |
|
1156 if (inc == 0) |
|
1157 inc++; |
|
1158 } |
|
1159 |
626
|
1160 if (pr_as_read_syntax) |
1
|
1161 { |
|
1162 for (int i = 0; i < nr; i++) |
|
1163 { |
626
|
1164 int col = 0; |
|
1165 while (col < nc) |
1
|
1166 { |
626
|
1167 int lim = col + inc < nc ? col + inc : nc; |
|
1168 |
|
1169 for (int j = col; j < lim; j++) |
|
1170 { |
|
1171 if (i == 0 && j == 0) |
|
1172 os << "[ "; |
|
1173 else |
|
1174 { |
|
1175 if (j > col && j < lim) |
|
1176 os << ", "; |
|
1177 else |
|
1178 os << " "; |
|
1179 } |
|
1180 |
2305
|
1181 pr_float (os, m (i, j)); |
626
|
1182 } |
|
1183 |
|
1184 col += inc; |
|
1185 |
|
1186 if (col >= nc) |
|
1187 { |
|
1188 if (i == nr - 1) |
|
1189 os << " ]"; |
|
1190 else |
|
1191 os << ";\n"; |
|
1192 } |
|
1193 else |
|
1194 os << " ...\n"; |
1
|
1195 } |
|
1196 } |
626
|
1197 } |
|
1198 else |
|
1199 { |
|
1200 for (int col = 0; col < nc; col += inc) |
|
1201 { |
|
1202 int lim = col + inc < nc ? col + inc : nc; |
|
1203 |
1972
|
1204 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1205 extra_indent); |
626
|
1206 |
|
1207 for (int i = 0; i < nr; i++) |
|
1208 { |
1972
|
1209 os.form ("%*s", extra_indent, ""); |
|
1210 |
626
|
1211 for (int j = col; j < lim; j++) |
|
1212 { |
|
1213 os << " "; |
|
1214 |
2305
|
1215 pr_float (os, m (i, j), fw); |
626
|
1216 } |
|
1217 |
|
1218 os << "\n"; |
|
1219 } |
|
1220 } |
1
|
1221 } |
|
1222 } |
|
1223 } |
|
1224 |
|
1225 void |
626
|
1226 octave_print_internal (ostream& os, const Complex& c, |
1972
|
1227 bool pr_as_read_syntax) |
1
|
1228 { |
|
1229 if (plus_format) |
|
1230 { |
|
1231 if (c == 0.0) |
|
1232 os << " "; |
|
1233 else |
|
1234 os << "+"; |
|
1235 } |
|
1236 else |
|
1237 { |
|
1238 set_format (c); |
|
1239 if (free_format) |
|
1240 os << c; |
|
1241 else |
|
1242 pr_complex (os, c); |
|
1243 } |
626
|
1244 |
|
1245 if (! pr_as_read_syntax) |
|
1246 os << "\n"; |
1
|
1247 } |
|
1248 |
|
1249 void |
626
|
1250 octave_print_internal (ostream& os, const ComplexMatrix& cm, |
1972
|
1251 bool pr_as_read_syntax, int extra_indent) |
1
|
1252 { |
|
1253 int nr = cm.rows (); |
|
1254 int nc = cm.columns (); |
|
1255 |
2408
|
1256 if (nr == 0 || nc == 0) |
626
|
1257 print_empty_matrix (os, nr, nc, pr_as_read_syntax); |
|
1258 else if (plus_format && ! pr_as_read_syntax) |
1
|
1259 { |
|
1260 for (int i = 0; i < nr; i++) |
|
1261 { |
|
1262 for (int j = 0; j < nc; j++) |
|
1263 { |
|
1264 if (j == 0) |
|
1265 os << " "; |
|
1266 |
2305
|
1267 if (cm (i, j) == 0.0) |
1
|
1268 os << " "; |
|
1269 else |
|
1270 os << "+"; |
|
1271 } |
|
1272 os << "\n"; |
|
1273 } |
|
1274 } |
|
1275 else |
|
1276 { |
|
1277 int r_fw, i_fw; |
|
1278 set_format (cm, r_fw, i_fw); |
|
1279 int column_width = i_fw + r_fw; |
1309
|
1280 column_width += (bank_format || hex_format|| bit_format) ? 2 : 7; |
1
|
1281 int total_width = nc * column_width; |
|
1282 int max_width = terminal_columns (); |
|
1283 |
626
|
1284 if (pr_as_read_syntax) |
|
1285 max_width -= 4; |
1972
|
1286 else |
|
1287 max_width -= extra_indent; |
|
1288 |
|
1289 if (max_width < 0) |
|
1290 max_width = 0; |
626
|
1291 |
1
|
1292 if (free_format) |
|
1293 { |
626
|
1294 if (pr_as_read_syntax) |
|
1295 os << "[\n"; |
|
1296 |
1
|
1297 os << cm; |
626
|
1298 |
|
1299 if (pr_as_read_syntax) |
|
1300 os << "]"; |
|
1301 |
1
|
1302 return; |
|
1303 } |
|
1304 |
|
1305 int inc = nc; |
2165
|
1306 if (total_width > max_width && Vsplit_long_rows) |
1
|
1307 { |
|
1308 inc = max_width / column_width; |
|
1309 if (inc == 0) |
|
1310 inc++; |
|
1311 } |
|
1312 |
626
|
1313 if (pr_as_read_syntax) |
1
|
1314 { |
|
1315 for (int i = 0; i < nr; i++) |
|
1316 { |
626
|
1317 int col = 0; |
|
1318 while (col < nc) |
1
|
1319 { |
626
|
1320 int lim = col + inc < nc ? col + inc : nc; |
|
1321 |
|
1322 for (int j = col; j < lim; j++) |
|
1323 { |
|
1324 if (i == 0 && j == 0) |
|
1325 os << "[ "; |
|
1326 else |
|
1327 { |
|
1328 if (j > col && j < lim) |
|
1329 os << ", "; |
|
1330 else |
|
1331 os << " "; |
|
1332 } |
|
1333 |
2305
|
1334 pr_complex (os, cm (i, j)); |
626
|
1335 } |
|
1336 |
|
1337 col += inc; |
|
1338 |
|
1339 if (col >= nc) |
|
1340 { |
|
1341 if (i == nr - 1) |
|
1342 os << " ]"; |
|
1343 else |
|
1344 os << ";\n"; |
|
1345 } |
1
|
1346 else |
626
|
1347 os << " ...\n"; |
1
|
1348 } |
|
1349 } |
626
|
1350 } |
|
1351 else |
|
1352 { |
|
1353 for (int col = 0; col < nc; col += inc) |
|
1354 { |
|
1355 int lim = col + inc < nc ? col + inc : nc; |
|
1356 |
1972
|
1357 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1358 extra_indent); |
626
|
1359 |
|
1360 for (int i = 0; i < nr; i++) |
|
1361 { |
1972
|
1362 os.form ("%*s", extra_indent, ""); |
|
1363 |
626
|
1364 for (int j = col; j < lim; j++) |
|
1365 { |
|
1366 os << " "; |
|
1367 |
2305
|
1368 pr_complex (os, cm (i, j)); |
626
|
1369 } |
|
1370 os << "\n"; |
|
1371 } |
|
1372 } |
1
|
1373 } |
|
1374 } |
|
1375 } |
|
1376 |
|
1377 void |
626
|
1378 octave_print_internal (ostream& os, const Range& r, |
1972
|
1379 bool pr_as_read_syntax, int extra_indent) |
1
|
1380 { |
626
|
1381 double base = r.base (); |
1
|
1382 double increment = r.inc (); |
626
|
1383 double limit = r.limit (); |
1
|
1384 int num_elem = r.nelem (); |
|
1385 |
626
|
1386 if (plus_format && ! pr_as_read_syntax) |
1
|
1387 { |
|
1388 os << " "; |
|
1389 for (int i = 0; i < num_elem; i++) |
|
1390 { |
626
|
1391 double val = base + i * increment; |
1
|
1392 if (val == 0.0) |
|
1393 os << " "; |
|
1394 else |
|
1395 os << "+"; |
|
1396 } |
|
1397 } |
|
1398 else |
|
1399 { |
|
1400 int fw; |
|
1401 set_format (r, fw); |
|
1402 |
626
|
1403 if (pr_as_read_syntax) |
1
|
1404 { |
626
|
1405 if (free_format) |
|
1406 { |
|
1407 os << base << " : "; |
|
1408 if (increment != 1.0) |
|
1409 os << increment << " : "; |
|
1410 os << limit; |
|
1411 } |
|
1412 else |
|
1413 { |
|
1414 pr_float (os, base, fw); |
|
1415 os << " : "; |
|
1416 if (increment != 1.0) |
|
1417 { |
|
1418 pr_float (os, increment, fw); |
|
1419 os << " : "; |
|
1420 } |
|
1421 pr_float (os, limit, fw); |
|
1422 } |
1
|
1423 } |
626
|
1424 else |
|
1425 { |
|
1426 int column_width = fw + 2; |
|
1427 int total_width = num_elem * column_width; |
|
1428 int max_width = terminal_columns (); |
1
|
1429 |
626
|
1430 if (free_format) |
|
1431 { |
|
1432 os << r; |
|
1433 return; |
|
1434 } |
1
|
1435 |
626
|
1436 int inc = num_elem; |
2165
|
1437 if (total_width > max_width && Vsplit_long_rows) |
1
|
1438 { |
626
|
1439 inc = max_width / column_width; |
|
1440 if (inc == 0) |
|
1441 inc++; |
1
|
1442 } |
|
1443 |
1972
|
1444 max_width -= extra_indent; |
|
1445 |
|
1446 if (max_width < 0) |
|
1447 max_width = 0; |
|
1448 |
626
|
1449 int col = 0; |
|
1450 while (col < num_elem) |
1
|
1451 { |
626
|
1452 int lim = col + inc < num_elem ? col + inc : num_elem; |
|
1453 |
1972
|
1454 pr_col_num_header (os, total_width, max_width, lim, col, |
|
1455 extra_indent); |
|
1456 |
|
1457 os.form ("%*s", extra_indent, ""); |
626
|
1458 |
|
1459 for (int i = col; i < lim; i++) |
|
1460 { |
|
1461 double val = base + i * increment; |
|
1462 os << " "; |
|
1463 pr_float (os, val, fw); |
|
1464 } |
|
1465 |
|
1466 os << "\n"; |
|
1467 |
|
1468 col += inc; |
1
|
1469 } |
|
1470 } |
|
1471 } |
|
1472 } |
|
1473 |
1343
|
1474 void |
1572
|
1475 octave_print_internal (ostream& os, const charMatrix& chm, |
1972
|
1476 bool pr_as_read_syntax, bool pr_as_string, |
|
1477 int extra_indent) |
1343
|
1478 { |
1572
|
1479 if (pr_as_string) |
|
1480 { |
|
1481 int nstr = chm.rows (); |
1343
|
1482 |
1572
|
1483 if (pr_as_read_syntax && nstr > 1) |
|
1484 os << "[ "; |
1343
|
1485 |
1572
|
1486 for (int i = 0; i < nstr; i++) |
1343
|
1487 { |
1755
|
1488 string row = chm.row_as_string (i); |
1588
|
1489 |
1572
|
1490 if (pr_as_read_syntax) |
|
1491 { |
1755
|
1492 os << "\"" << undo_string_escapes (row) << "\""; |
1343
|
1493 |
1572
|
1494 if (i < nstr - 1) |
|
1495 os << "; "; |
|
1496 } |
|
1497 else |
1588
|
1498 os << row << "\n"; |
1343
|
1499 } |
1572
|
1500 |
|
1501 if (pr_as_read_syntax && nstr > 1) |
|
1502 os << " ]"; |
1343
|
1503 } |
1572
|
1504 else |
|
1505 { |
|
1506 os << "sorry, printing char matrices not implemented yet\n"; |
|
1507 } |
1343
|
1508 } |
|
1509 |
1957
|
1510 DEFUN (disp, args, , |
529
|
1511 "disp (X): display value without name tag") |
|
1512 { |
2086
|
1513 octave_value_list retval; |
529
|
1514 |
|
1515 int nargin = args.length (); |
|
1516 |
712
|
1517 if (nargin == 1) |
2387
|
1518 args(0).print (); |
529
|
1519 else |
|
1520 print_usage ("disp"); |
|
1521 |
|
1522 return retval; |
|
1523 } |
|
1524 |
1
|
1525 static void |
|
1526 init_format_state (void) |
|
1527 { |
2387
|
1528 free_format = false; |
|
1529 plus_format = false; |
|
1530 bank_format = false; |
|
1531 hex_format = false; |
1309
|
1532 bit_format = 0; |
2387
|
1533 print_e = false; |
|
1534 print_big_e = false; |
1
|
1535 } |
|
1536 |
|
1537 static void |
|
1538 set_output_prec_and_fw (int prec, int fw) |
|
1539 { |
2387
|
1540 bind_builtin_variable ("output_precision", (double) prec); |
|
1541 bind_builtin_variable ("output_max_field_width", (double) fw); |
1
|
1542 } |
|
1543 |
1755
|
1544 static void |
|
1545 set_format_style (int argc, const string_vector& argv) |
1
|
1546 { |
1755
|
1547 int idx = 1; |
|
1548 string arg = argv[idx++]; |
|
1549 |
1899
|
1550 if (--argc > 0) |
1
|
1551 { |
1755
|
1552 if (arg == "short") |
1
|
1553 { |
1755
|
1554 if (--argc > 0) |
1
|
1555 { |
1755
|
1556 arg = argv[idx++]; |
|
1557 |
|
1558 if (arg == "e") |
1
|
1559 { |
1755
|
1560 init_format_state (); |
2387
|
1561 print_e = true; |
1755
|
1562 } |
|
1563 else if (arg == "E") |
|
1564 { |
|
1565 init_format_state (); |
2387
|
1566 print_e = true; |
|
1567 print_big_e = true; |
1
|
1568 } |
|
1569 else |
|
1570 { |
1755
|
1571 error ("format: unrecognized option `short %s'", |
|
1572 arg.c_str ()); |
|
1573 return; |
|
1574 } |
|
1575 } |
|
1576 else |
|
1577 init_format_state (); |
|
1578 |
|
1579 set_output_prec_and_fw (3, 8); |
|
1580 } |
|
1581 else if (arg == "long") |
|
1582 { |
|
1583 if (--argc > 0) |
|
1584 { |
|
1585 arg = argv[idx++]; |
|
1586 |
|
1587 if (arg == "e") |
|
1588 { |
|
1589 init_format_state (); |
2387
|
1590 print_e = true; |
1755
|
1591 } |
|
1592 else if (arg == "E") |
|
1593 { |
|
1594 init_format_state (); |
2387
|
1595 print_e = true; |
|
1596 print_big_e = true; |
1
|
1597 } |
|
1598 else |
1755
|
1599 { |
|
1600 error ("format: unrecognized option `long %s'", |
|
1601 arg.c_str ()); |
|
1602 return; |
|
1603 } |
1186
|
1604 } |
1
|
1605 else |
1755
|
1606 init_format_state (); |
|
1607 |
|
1608 set_output_prec_and_fw (15, 24); |
|
1609 } |
|
1610 else if (arg == "hex") |
|
1611 { |
|
1612 init_format_state (); |
2387
|
1613 hex_format = true; |
1755
|
1614 } |
|
1615 else if (arg == "native-hex") |
|
1616 { |
|
1617 init_format_state (); |
|
1618 hex_format = 2; |
|
1619 } |
|
1620 else if (arg == "bit") |
|
1621 { |
|
1622 init_format_state (); |
|
1623 bit_format = 1; |
|
1624 } |
|
1625 else if (arg == "native-bit") |
|
1626 { |
|
1627 init_format_state (); |
|
1628 bit_format = 2; |
|
1629 } |
|
1630 else if (arg == "+" || arg == "plus") |
|
1631 { |
|
1632 init_format_state (); |
2387
|
1633 plus_format = true; |
1755
|
1634 } |
|
1635 else if (arg == "bank") |
|
1636 { |
|
1637 init_format_state (); |
2387
|
1638 bank_format = true; |
1755
|
1639 } |
|
1640 else if (arg == "free") |
|
1641 { |
|
1642 init_format_state (); |
2387
|
1643 free_format = true; |
1755
|
1644 } |
|
1645 else if (arg == "none") |
|
1646 { |
|
1647 init_format_state (); |
2387
|
1648 free_format = true; |
1755
|
1649 } |
|
1650 else if (arg == "compact") |
|
1651 { |
2387
|
1652 compact_format = true; |
1755
|
1653 } |
|
1654 else if (arg == "loose") |
|
1655 { |
2387
|
1656 compact_format = false; |
1
|
1657 } |
|
1658 else |
1755
|
1659 error ("format: unrecognized format state `%s'", arg.c_str ()); |
1
|
1660 } |
|
1661 else |
|
1662 { |
|
1663 init_format_state (); |
|
1664 set_output_prec_and_fw (5, 10); |
|
1665 } |
|
1666 } |
|
1667 |
1957
|
1668 DEFUN_TEXT (format, args, , |
529
|
1669 "format [style]\n\ |
|
1670 \n\ |
|
1671 set output formatting style") |
|
1672 { |
2086
|
1673 octave_value_list retval; |
529
|
1674 |
1755
|
1675 int argc = args.length () + 1; |
|
1676 |
1968
|
1677 string_vector argv = args.make_argv ("format"); |
1755
|
1678 |
|
1679 if (error_state) |
|
1680 return retval; |
529
|
1681 |
|
1682 set_format_style (argc, argv); |
|
1683 |
|
1684 return retval; |
|
1685 } |
|
1686 |
2165
|
1687 static int |
|
1688 output_max_field_width (void) |
|
1689 { |
|
1690 double val; |
|
1691 if (builtin_real_scalar_variable ("output_max_field_width", val) |
|
1692 && ! xisnan (val)) |
|
1693 { |
|
1694 int ival = NINT (val); |
|
1695 if (ival > 0 && (double) ival == val) |
|
1696 { |
|
1697 Voutput_max_field_width = ival; |
|
1698 return 0; |
|
1699 } |
|
1700 } |
|
1701 gripe_invalid_value_specified ("output_max_field_width"); |
|
1702 return -1; |
|
1703 } |
|
1704 |
|
1705 static int |
|
1706 output_precision (void) |
|
1707 { |
|
1708 double val; |
|
1709 if (builtin_real_scalar_variable ("output_precision", val) |
|
1710 && ! xisnan (val)) |
|
1711 { |
|
1712 int ival = NINT (val); |
|
1713 if (ival >= 0 && (double) ival == val) |
|
1714 { |
|
1715 Voutput_precision = ival; |
|
1716 return 0; |
|
1717 } |
|
1718 } |
|
1719 gripe_invalid_value_specified ("output_precision"); |
|
1720 return -1; |
|
1721 } |
|
1722 |
|
1723 static int |
|
1724 print_empty_dimensions (void) |
|
1725 { |
|
1726 Vprint_empty_dimensions = check_preference ("print_empty_dimensions"); |
|
1727 |
|
1728 return 0; |
|
1729 } |
|
1730 |
|
1731 static int |
|
1732 split_long_rows (void) |
|
1733 { |
|
1734 Vsplit_long_rows = check_preference ("split_long_rows"); |
|
1735 |
|
1736 return 0; |
|
1737 } |
|
1738 |
|
1739 void |
|
1740 symbols_of_pr_output (void) |
|
1741 { |
|
1742 DEFVAR (output_max_field_width, 10.0, 0, output_max_field_width, |
|
1743 "maximum width of an output field for numeric output"); |
|
1744 |
|
1745 DEFVAR (output_precision, 5.0, 0, output_precision, |
|
1746 "number of significant figures to display for numeric output"); |
|
1747 |
|
1748 DEFVAR (print_empty_dimensions, 1.0, 0, print_empty_dimensions, |
|
1749 "also print dimensions of empty matrices"); |
|
1750 |
|
1751 DEFVAR (split_long_rows, 1.0, 0, split_long_rows, |
|
1752 "split long matrix rows instead of wrapping"); |
|
1753 } |
|
1754 |
1
|
1755 /* |
|
1756 ;;; Local Variables: *** |
|
1757 ;;; mode: C++ *** |
|
1758 ;;; End: *** |
|
1759 */ |