2376
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2376
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
|
27 #ifdef HAVE_CONFIG_H |
|
28 #include <config.h> |
|
29 #endif |
|
30 |
3503
|
31 #include <iostream> |
2901
|
32 |
2376
|
33 #include "lo-ieee.h" |
|
34 |
|
35 #include "oct-obj.h" |
2410
|
36 #include "ops.h" |
2376
|
37 #include "ov-complex.h" |
3223
|
38 #include "ov-base.h" |
|
39 #include "ov-base-scalar.h" |
|
40 #include "ov-base-scalar.cc" |
2423
|
41 #include "ov-cx-mat.h" |
2410
|
42 #include "ov-scalar.h" |
2376
|
43 #include "gripes.h" |
|
44 #include "pr-output.h" |
|
45 |
3223
|
46 template class octave_base_scalar<Complex>; |
|
47 |
3219
|
48 DEFINE_OCTAVE_ALLOCATOR (octave_complex); |
2376
|
49 |
3219
|
50 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_complex, "complex scalar"); |
2376
|
51 |
2410
|
52 octave_value * |
|
53 octave_complex::try_narrowing_conversion (void) |
|
54 { |
|
55 octave_value *retval = 0; |
|
56 |
|
57 if (imag (scalar) == 0.0) |
3775
|
58 retval = new octave_scalar (real (scalar)); |
2410
|
59 |
|
60 return retval; |
|
61 } |
|
62 |
2376
|
63 static inline bool |
|
64 valid_scalar_indices (const octave_value_list& args) |
|
65 { |
|
66 int nargin = args.length (); |
|
67 |
|
68 for (int i = 0; i < nargin; i++) |
|
69 if (! args(i).valid_as_scalar_index ()) |
|
70 return false; |
|
71 |
|
72 return true; |
|
73 } |
|
74 |
|
75 octave_value |
2974
|
76 octave_complex::do_index_op (const octave_value_list& idx) |
2376
|
77 { |
|
78 octave_value retval; |
|
79 |
|
80 if (valid_scalar_indices (idx)) |
|
81 retval = scalar; |
|
82 else |
|
83 { |
|
84 // XXX FIXME XXX -- this doesn't solve the problem of |
|
85 // |
|
86 // a = i; a([1,1], [1,1], [1,1]) |
|
87 // |
|
88 // and similar constructions. Hmm... |
|
89 |
2423
|
90 // XXX FIXME XXX -- using this constructor avoids narrowing the |
|
91 // 1x1 matrix back to a scalar value. Need a better solution |
|
92 // to this problem. |
|
93 |
|
94 octave_value tmp (new octave_complex_matrix (complex_matrix_value ())); |
2376
|
95 |
2962
|
96 retval = tmp.do_index_op (idx); |
2376
|
97 } |
|
98 |
|
99 return retval; |
|
100 } |
|
101 |
|
102 double |
|
103 octave_complex::double_value (bool force_conversion) const |
|
104 { |
|
105 double retval = octave_NaN; |
|
106 |
|
107 int flag = force_conversion; |
|
108 |
|
109 if (! flag) |
|
110 flag = Vok_to_lose_imaginary_part; |
|
111 |
|
112 if (flag < 0) |
|
113 gripe_implicit_conversion ("complex scalar", "real scalar"); |
|
114 |
|
115 if (flag) |
3775
|
116 retval = std::real (scalar); |
2376
|
117 else |
|
118 gripe_invalid_conversion ("complex scalar", "real scalar"); |
|
119 |
|
120 return retval; |
|
121 } |
|
122 |
|
123 Matrix |
|
124 octave_complex::matrix_value (bool force_conversion) const |
|
125 { |
|
126 Matrix retval; |
|
127 |
|
128 int flag = force_conversion; |
|
129 |
|
130 if (! flag) |
|
131 flag = Vok_to_lose_imaginary_part; |
|
132 |
|
133 if (flag < 0) |
|
134 gripe_implicit_conversion ("complex scalar", "real matrix"); |
|
135 |
|
136 if (flag) |
3775
|
137 retval = Matrix (1, 1, std::real (scalar)); |
2376
|
138 else |
|
139 gripe_invalid_conversion ("complex scalar", "real matrix"); |
|
140 |
|
141 return retval; |
|
142 } |
|
143 |
|
144 Complex |
|
145 octave_complex::complex_value (bool) const |
|
146 { |
|
147 return scalar; |
|
148 } |
|
149 |
|
150 |
|
151 ComplexMatrix |
|
152 octave_complex::complex_matrix_value (bool) const |
|
153 { |
|
154 return ComplexMatrix (1, 1, scalar); |
|
155 } |
|
156 |
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |