3213
|
1 # Copyright (C) 1998 A. Scottedward Hodel |
|
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, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
18 |
|
19 function retval = com2str(zz,flg) |
|
20 # usage retval = com2str(zz{,flg}) |
|
21 # |
|
22 # convert complex number to a string |
|
23 # zz: complex number |
|
24 # flg: format flag |
|
25 # 0 (default): -1, 0, 1, 1i, 1 + 0.5i |
|
26 # 1 (for use with zpout): -1, 0, + 1, + 1i, + 1 + 0.5i |
|
27 # |
|
28 # $Revision: 1.1 $ |
|
29 |
|
30 if (nargin < 1 | nargin > 2) |
|
31 usage("com2str(zz{,flg})"); |
|
32 endif |
|
33 if(nargin == 1) |
|
34 flg = 0; |
|
35 endif |
|
36 |
|
37 if( !(is_scalar(zz) & is_scalar(flg) ) ) |
|
38 error("com2str: arguments must be a scalar."); |
|
39 endif |
|
40 |
|
41 if(flg != 0 & flg != 1) |
|
42 error(["Illegal flg value: ",num2str(flg)]); |
|
43 endif |
|
44 |
|
45 sgns = "+-"; |
|
46 rz = real(zz); |
|
47 iz = imag(zz); |
|
48 az = abs(zz); |
|
49 if(iz == 0) |
|
50 # strictly a real number |
|
51 switch(flg) |
|
52 case(0) |
|
53 retval = num2str(rz); |
|
54 case(1) |
|
55 retval = [ sgns(1+(rz< 0))," ", num2str(abs(rz))]; |
|
56 endswitch |
|
57 elseif(rz == 0) |
|
58 # strictly an imaginary number |
|
59 switch(flg) |
|
60 case(0) |
|
61 retval = num2str(iz); |
|
62 case(1) |
|
63 retval = [ sgns(1+(iz< 0))," ", num2str(abs(iz)),"i"]; |
|
64 endswitch |
|
65 else |
|
66 # complex number |
|
67 # strictly an imaginary number |
|
68 switch(flg) |
|
69 case(0) |
|
70 retval = [num2str(rz)," ",com2str(i*iz,1)]; |
|
71 case(1) |
|
72 retval = [ sgns(1+(rz< 0))," ", num2str(abs(rz))," ",com2str(i*iz,1)]; |
|
73 endswitch |
|
74 endif |
|
75 |
|
76 endfunction |