Mercurial > hg > octave-nkf
annotate scripts/strings/mat2str.m @ 7812:c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
author | kimhanse@gmail.com |
---|---|
date | Wed, 21 May 2008 12:26:57 +0200 |
parents | a1dbe9d80eee |
children | 502e58a0d44f |
rev | line source |
---|---|
7017 | 1 ## Copyright (C) 2002, 2006, 2007 Rolf Fabian |
5837 | 2 ## |
3 ## This file is part of Octave. | |
4 ## | |
5 ## Octave is free software; you can redistribute it and/or modify it | |
6 ## under the terms of the GNU General Public License as published by | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5837 | 9 ## |
10 ## Octave is distributed in the hope that it will be useful, but | |
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 ## General Public License for more details. | |
14 ## | |
15 ## You should have received a copy of the GNU General Public License | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5837 | 18 |
19 ## -*- texinfo -*- | |
20 ## @deftypefn {Function File} {@var{s} =} mat2str (@var{x}, @var{n}) | |
21 ## @deftypefnx {Function File} {@var{s} =} mat2str (@dots{}, 'class') | |
22 ## | |
7001 | 23 ## Format real/complex numerical matrices as strings. This function |
5837 | 24 ## returns values that are suitable for the use of the @code{eval} |
25 ## function. | |
26 ## | |
27 ## The precision of the values is given by @var{n}. If @var{n} is a | |
28 ## scalar then both real and imaginary parts of the matrix are printed | |
29 ## to the same precision. Otherwise @code{@var{n} (1)} defines the | |
30 ## precision of the real part and @code{@var{n} (2)} defines the | |
31 ## precision of the imaginary part. The default for @var{n} is 17. | |
32 ## | |
33 ## If the argument 'class' is given, then the class of @var{x} is | |
34 ## included in the string in such a way that the eval will result in the | |
35 ## construction of a matrix of the same class. | |
36 ## | |
37 ## @example | |
38 ## @group | |
39 ## mat2str( [ -1/3 + i/7; 1/3 - i/7 ], [4 2] ) | |
40 ## @result{} '[-0.3333+0.14i;0.3333-0.14i]' | |
41 ## mat2str( [ -1/3 +i/7; 1/3 -i/7 ], [4 2] ) | |
42 ## @result{} '[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]' | |
43 ## mat2str( int16([1 -1]), 'class') | |
44 ## @result{} 'int16([1,-1])' | |
45 ## @end group | |
46 ## @end example | |
47 ## | |
48 ## @seealso{sprintf, int2str} | |
49 ## @end deftypefn | |
50 | |
7016 | 51 ## Author: Rolf Fabian <fabian@tu-cottbus.de> |
52 | |
5838 | 53 function s = mat2str (x, n, cls) |
5837 | 54 |
5838 | 55 if (nargin < 2 || isempty (n)) |
56 ## Default precision | |
57 n = 17; | |
5837 | 58 endif |
59 | |
60 if (nargin < 3) | |
5838 | 61 if (ischar (n)) |
5837 | 62 cls = n; |
63 n = 17; | |
64 else | |
5838 | 65 cls = ""; |
5837 | 66 endif |
67 endif | |
68 | |
5838 | 69 if (nargin < 1 || nargin > 3 || ! isnumeric (x)) |
70 print_usage (); | |
5837 | 71 endif |
72 | |
73 if (ndims (x) > 2) | |
5838 | 74 error ("mat2str: X must be two dimensional"); |
5837 | 75 endif |
76 | |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
77 x_iscomplex = iscomplex (x); |
5838 | 78 |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
79 if (! x_iscomplex) |
5838 | 80 fmt = sprintf ("%%.%dg", n(1)); |
5837 | 81 else |
5838 | 82 if (length (n) == 1 ) |
5837 | 83 n = [n, n]; |
84 endif | |
5838 | 85 fmt = sprintf ("%%.%dg%%+.%dgi", n(1), n(2)); |
5837 | 86 endif |
87 | |
5838 | 88 nel = numel (x); |
5837 | 89 |
5838 | 90 if (nel == 0) |
91 ## Empty, only print brackets | |
5837 | 92 s = "[]"; |
5838 | 93 elseif (nel == 1) |
94 ## Scalar X, don't print brackets | |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
95 if (! x_iscomplex) |
5838 | 96 s = sprintf (fmt, x); |
5837 | 97 else |
5838 | 98 s = sprintf (fmt, real (x), imag (x)); |
5837 | 99 endif |
5838 | 100 else |
101 ## Non-scalar X, print brackets | |
102 fmt = [fmt, ","]; | |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
103 if (! x_iscomplex) |
5838 | 104 s = sprintf (fmt, x.'); |
5837 | 105 else |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
106 t = x.'; |
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
107 s = sprintf (fmt, [real(t(:))'; imag(t(:))']); |
5837 | 108 endif |
109 | |
110 s = ["[", s]; | |
5838 | 111 s(end) = "]"; |
112 ind = find (s == ","); | |
5946 | 113 nc = columns (x); |
5838 | 114 s(ind(nc:nc:end)) = ";"; |
5837 | 115 endif |
116 | |
117 if (strcmp ("class", cls)) | |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
118 s = [class(x), "(", s, ")"]; |
5837 | 119 endif |
120 endfunction | |
7812
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
121 |
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
122 %!assert (mat2str ([-1/3 + i/7; 1/3 - i/7], [4 2]), "[-0.3333+0.14i;0.3333-0.14i]") |
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
123 %!assert (mat2str ([-1/3 +i/7; 1/3 -i/7], [4 2]), "[-0.3333+0i,0+0.14i;0.3333+0i,-0-0.14i]") |
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
124 %!assert (mat2str (int16 ([1 -1]), 'class'), "int16([1,-1])") |
c25094267486
strings/mat2str.m: Change is_complex to iscomplex, add tests, add missing ;
kimhanse@gmail.com
parents:
7017
diff
changeset
|
125 |