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