3431
|
1 ## Copyright (C) 1998 Auburn University. All rights reserved. |
|
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 the |
|
7 ## Free Software Foundation; either version 2, or (at your option) any |
|
8 ## later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but WITHOUT |
|
11 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
12 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
13 ## for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. |
|
18 |
|
19 ## -*- texinfo -*- |
3501
|
20 ## @deftypefn {Function File} {} com2str (@var{zz}, @var{flg}) |
3431
|
21 ## |
|
22 ## convert complex number to a string |
|
23 ## @strong{Inputs} |
|
24 ## @table @var |
|
25 ## @item zz |
|
26 ## complex number |
|
27 ## @item flg |
|
28 ## format flag |
|
29 ## 0 (default): -1, 0, 1, 1i, 1 + 0.5i |
|
30 ## 1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i |
|
31 ## @end table |
|
32 ## @end deftypefn |
|
33 |
|
34 function retval = com2str (zz, flg) |
|
35 |
3457
|
36 if (nargin < 1 || nargin > 2) |
|
37 usage ("com2str (zz, flg)"); |
3431
|
38 endif |
3457
|
39 if (nargin == 1) |
3431
|
40 flg = 0; |
|
41 endif |
|
42 |
3457
|
43 if (! (is_scalar (zz) && is_scalar (flg))) |
3458
|
44 error ("com2str: arguments must be a scalar"); |
3431
|
45 endif |
|
46 |
3457
|
47 if (flg != 0 && flg != 1) |
|
48 error ("invalid flg value: %d", flg); |
3431
|
49 endif |
|
50 |
|
51 sgns = "+-"; |
3457
|
52 rz = real (zz); |
|
53 iz = imag (zz); |
|
54 az = abs (zz); |
|
55 if (iz == 0) |
3431
|
56 ## strictly a real number |
3457
|
57 switch (flg) |
|
58 case(0) |
|
59 retval = num2str (rz); |
|
60 case(1) |
|
61 retval = [sgns(1+(rz<0)), " ", num2str(abs(rz))]; |
3431
|
62 endswitch |
3457
|
63 elseif (rz == 0) |
3431
|
64 ## strictly an imaginary number |
3457
|
65 switch (flg) |
|
66 case(0) |
|
67 retval = num2str (iz); |
|
68 case(1) |
|
69 retval = [sgns(1+(iz<0)), " ", num2str(abs(iz)), "i"]; |
3431
|
70 endswitch |
|
71 else |
|
72 ## complex number |
|
73 ## strictly an imaginary number |
3457
|
74 switch (flg) |
|
75 case(0) |
|
76 retval = [num2str(rz), " ", com2str(i*iz,1)]; |
|
77 case(1) |
|
78 retval = [sgns(1+(rz<0)), " ", num2str(abs(rz)), " ", com2str(i*iz,1)]; |
3431
|
79 endswitch |
|
80 endif |
|
81 |
|
82 endfunction |