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