comparison scripts/strings/com2str.m @ 3431:99ab64f4a09d

[project @ 2000-01-14 03:53:03 by jwe]
author jwe
date Fri, 14 Jan 2000 04:12:41 +0000
parents
children e031284eea27
comparison
equal deleted inserted replaced
3430:65b3519ac3a1 3431:99ab64f4a09d
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 -*-
20 ## @deftypefn {Function File} {@var{retval} =} com2str(@var{zz}[,@var{flg}])
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
36 if (nargin < 1 | nargin > 2)
37 usage("com2str(zz{,flg})");
38 endif
39 if(nargin == 1)
40 flg = 0;
41 endif
42
43 if( !(is_scalar(zz) & is_scalar(flg) ) )
44 error("com2str: arguments must be a scalar.");
45 endif
46
47 if(flg != 0 & flg != 1)
48 error(["invalid flg value: ",num2str(flg)]);
49 endif
50
51 sgns = "+-";
52 rz = real(zz);
53 iz = imag(zz);
54 az = abs(zz);
55 if(iz == 0)
56 ## strictly a real number
57 switch(flg)
58 case(0)
59 retval = num2str(rz);
60 case(1)
61 retval = [ sgns(1+(rz< 0))," ", num2str(abs(rz))];
62 endswitch
63 elseif(rz == 0)
64 ## strictly an imaginary number
65 switch(flg)
66 case(0)
67 retval = num2str(iz);
68 case(1)
69 retval = [ sgns(1+(iz< 0))," ", num2str(abs(iz)),"i"];
70 endswitch
71 else
72 ## complex number
73 ## strictly an imaginary number
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)];
79 endswitch
80 endif
81
82 endfunction