237
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
237
|
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 |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
237
|
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 |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
237
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
1192
|
24 #include <config.h> |
237
|
25 #endif |
|
26 |
|
27 #include "MArray.h" |
4669
|
28 #include "Array-util.h" |
237
|
29 #include "lo-error.h" |
|
30 |
1989
|
31 #include "MArray-defs.h" |
1213
|
32 |
1360
|
33 // One dimensional array with math ops. |
237
|
34 |
6508
|
35 template <class T> |
|
36 double |
|
37 MArray<T>::norm (double) const |
|
38 { |
|
39 (*current_liboctave_error_handler) |
|
40 ("norm: only implemented for double and complex values"); |
|
41 |
|
42 return 0; |
|
43 } |
|
44 |
237
|
45 // Element by element MArray by scalar ops. |
|
46 |
|
47 template <class T> |
1213
|
48 MArray<T>& |
1230
|
49 operator += (MArray<T>& a, const T& s) |
237
|
50 { |
4646
|
51 DO_VS_OP2 (T, a, +=, s) |
1230
|
52 return a; |
237
|
53 } |
|
54 |
|
55 template <class T> |
1213
|
56 MArray<T>& |
1230
|
57 operator -= (MArray<T>& a, const T& s) |
237
|
58 { |
4646
|
59 DO_VS_OP2 (T, a, -=, s) |
1230
|
60 return a; |
237
|
61 } |
|
62 |
|
63 // Element by element MArray by MArray ops. |
|
64 |
|
65 template <class T> |
1213
|
66 MArray<T>& |
1230
|
67 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
68 { |
5275
|
69 octave_idx_type l = a.length (); |
1213
|
70 if (l > 0) |
237
|
71 { |
5275
|
72 octave_idx_type bl = b.length (); |
2383
|
73 if (l != bl) |
|
74 gripe_nonconformant ("operator +=", l, bl); |
1213
|
75 else |
4646
|
76 DO_VV_OP2 (T, a, +=, b); |
237
|
77 } |
1230
|
78 return a; |
237
|
79 } |
|
80 |
|
81 template <class T> |
1213
|
82 MArray<T>& |
1230
|
83 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
84 { |
5275
|
85 octave_idx_type l = a.length (); |
1213
|
86 if (l > 0) |
237
|
87 { |
5275
|
88 octave_idx_type bl = b.length (); |
2383
|
89 if (l != bl) |
|
90 gripe_nonconformant ("operator -=", l, bl); |
1213
|
91 else |
4646
|
92 DO_VV_OP2 (T, a, -=, b); |
237
|
93 } |
1230
|
94 return a; |
237
|
95 } |
|
96 |
1213
|
97 // Element by element MArray by scalar ops. |
|
98 |
|
99 #define MARRAY_AS_OP(OP) \ |
|
100 template <class T> \ |
|
101 MArray<T> \ |
|
102 operator OP (const MArray<T>& a, const T& s) \ |
|
103 { \ |
3504
|
104 MArray<T> result (a.length ()); \ |
|
105 T *r = result.fortran_vec (); \ |
5275
|
106 octave_idx_type l = a.length (); \ |
3504
|
107 const T *v = a.data (); \ |
|
108 DO_VS_OP (r, l, v, OP, s); \ |
|
109 return result; \ |
1213
|
110 } |
237
|
111 |
1213
|
112 MARRAY_AS_OP (+) |
|
113 MARRAY_AS_OP (-) |
|
114 MARRAY_AS_OP (*) |
|
115 MARRAY_AS_OP (/) |
|
116 |
|
117 // Element by element scalar by MArray ops. |
237
|
118 |
1213
|
119 #define MARRAY_SA_OP(OP) \ |
|
120 template <class T> \ |
|
121 MArray<T> \ |
|
122 operator OP (const T& s, const MArray<T>& a) \ |
|
123 { \ |
3504
|
124 MArray<T> result (a.length ()); \ |
|
125 T *r = result.fortran_vec (); \ |
5275
|
126 octave_idx_type l = a.length (); \ |
3504
|
127 const T *v = a.data (); \ |
|
128 DO_SV_OP (r, l, s, OP, v); \ |
|
129 return result; \ |
|
130 } |
237
|
131 |
1213
|
132 MARRAY_SA_OP(+) |
|
133 MARRAY_SA_OP(-) |
|
134 MARRAY_SA_OP(*) |
|
135 MARRAY_SA_OP(/) |
|
136 |
|
137 // Element by element MArray by MArray ops. |
237
|
138 |
2383
|
139 #define MARRAY_AA_OP(FCN, OP) \ |
1213
|
140 template <class T> \ |
|
141 MArray<T> \ |
|
142 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
143 { \ |
5275
|
144 octave_idx_type l = a.length (); \ |
|
145 octave_idx_type bl = b.length (); \ |
2383
|
146 if (l != bl) \ |
1213
|
147 { \ |
2383
|
148 gripe_nonconformant (#FCN, l, bl); \ |
1213
|
149 return MArray<T> (); \ |
|
150 } \ |
|
151 if (l == 0) \ |
|
152 return MArray<T> (); \ |
3504
|
153 MArray<T> result (l); \ |
|
154 T *r = result.fortran_vec (); \ |
|
155 const T *x = a.data (); \ |
|
156 const T *y = b.data (); \ |
|
157 DO_VV_OP (r, l, x, OP, y); \ |
|
158 return result; \ |
1213
|
159 } |
237
|
160 |
2383
|
161 MARRAY_AA_OP (operator +, +) |
|
162 MARRAY_AA_OP (operator -, -) |
|
163 MARRAY_AA_OP (product, *) |
|
164 MARRAY_AA_OP (quotient, /) |
237
|
165 |
|
166 // Unary MArray ops. |
|
167 |
|
168 template <class T> |
|
169 MArray<T> |
3574
|
170 operator + (const MArray<T>& a) |
|
171 { |
|
172 return a; |
|
173 } |
|
174 |
|
175 template <class T> |
|
176 MArray<T> |
237
|
177 operator - (const MArray<T>& a) |
|
178 { |
5275
|
179 octave_idx_type l = a.length (); |
3504
|
180 MArray<T> result (l); |
|
181 T *r = result.fortran_vec (); |
|
182 const T *x = a.data (); |
|
183 NEG_V (r, l, x); |
|
184 return result; |
237
|
185 } |
|
186 |
|
187 /* |
|
188 ;;; Local Variables: *** |
|
189 ;;; mode: C++ *** |
|
190 ;;; End: *** |
|
191 */ |