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 |
|
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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1296
|
24 #pragma implementation |
|
25 #endif |
|
26 |
237
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
237
|
29 #endif |
|
30 |
|
31 #include "MArray.h" |
|
32 #include "lo-error.h" |
|
33 |
1989
|
34 #include "MArray-defs.h" |
1213
|
35 |
1360
|
36 // One dimensional array with math ops. |
237
|
37 |
|
38 // Element by element MArray by scalar ops. |
|
39 |
|
40 template <class T> |
1213
|
41 MArray<T>& |
1230
|
42 operator += (MArray<T>& a, const T& s) |
237
|
43 { |
4646
|
44 DO_VS_OP2 (T, a, +=, s) |
1230
|
45 return a; |
237
|
46 } |
|
47 |
|
48 template <class T> |
1213
|
49 MArray<T>& |
1230
|
50 operator -= (MArray<T>& a, const T& s) |
237
|
51 { |
4646
|
52 DO_VS_OP2 (T, a, -=, s) |
1230
|
53 return a; |
237
|
54 } |
|
55 |
|
56 // Element by element MArray by MArray ops. |
|
57 |
|
58 template <class T> |
1213
|
59 MArray<T>& |
1230
|
60 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
61 { |
1230
|
62 int l = a.length (); |
1213
|
63 if (l > 0) |
237
|
64 { |
2383
|
65 int bl = b.length (); |
|
66 if (l != bl) |
|
67 gripe_nonconformant ("operator +=", l, bl); |
1213
|
68 else |
4646
|
69 DO_VV_OP2 (T, a, +=, b); |
237
|
70 } |
1230
|
71 return a; |
237
|
72 } |
|
73 |
|
74 template <class T> |
1213
|
75 MArray<T>& |
1230
|
76 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
77 { |
1230
|
78 int l = a.length (); |
1213
|
79 if (l > 0) |
237
|
80 { |
2383
|
81 int bl = b.length (); |
|
82 if (l != bl) |
|
83 gripe_nonconformant ("operator -=", l, bl); |
1213
|
84 else |
4646
|
85 DO_VV_OP2 (T, a, -=, b); |
237
|
86 } |
1230
|
87 return a; |
237
|
88 } |
|
89 |
1213
|
90 // Element by element MArray by scalar ops. |
|
91 |
|
92 #define MARRAY_AS_OP(OP) \ |
|
93 template <class T> \ |
|
94 MArray<T> \ |
|
95 operator OP (const MArray<T>& a, const T& s) \ |
|
96 { \ |
3504
|
97 MArray<T> result (a.length ()); \ |
|
98 T *r = result.fortran_vec (); \ |
|
99 int l = a.length (); \ |
|
100 const T *v = a.data (); \ |
|
101 DO_VS_OP (r, l, v, OP, s); \ |
|
102 return result; \ |
1213
|
103 } |
237
|
104 |
1213
|
105 MARRAY_AS_OP (+) |
|
106 MARRAY_AS_OP (-) |
|
107 MARRAY_AS_OP (*) |
|
108 MARRAY_AS_OP (/) |
|
109 |
|
110 // Element by element scalar by MArray ops. |
237
|
111 |
1213
|
112 #define MARRAY_SA_OP(OP) \ |
|
113 template <class T> \ |
|
114 MArray<T> \ |
|
115 operator OP (const T& s, const MArray<T>& a) \ |
|
116 { \ |
3504
|
117 MArray<T> result (a.length ()); \ |
|
118 T *r = result.fortran_vec (); \ |
|
119 int l = a.length (); \ |
|
120 const T *v = a.data (); \ |
|
121 DO_SV_OP (r, l, s, OP, v); \ |
|
122 return result; \ |
|
123 } |
237
|
124 |
1213
|
125 MARRAY_SA_OP(+) |
|
126 MARRAY_SA_OP(-) |
|
127 MARRAY_SA_OP(*) |
|
128 MARRAY_SA_OP(/) |
|
129 |
|
130 // Element by element MArray by MArray ops. |
237
|
131 |
2383
|
132 #define MARRAY_AA_OP(FCN, OP) \ |
1213
|
133 template <class T> \ |
|
134 MArray<T> \ |
|
135 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
136 { \ |
|
137 int l = a.length (); \ |
2383
|
138 int bl = b.length (); \ |
|
139 if (l != bl) \ |
1213
|
140 { \ |
2383
|
141 gripe_nonconformant (#FCN, l, bl); \ |
1213
|
142 return MArray<T> (); \ |
|
143 } \ |
|
144 if (l == 0) \ |
|
145 return MArray<T> (); \ |
3504
|
146 MArray<T> result (l); \ |
|
147 T *r = result.fortran_vec (); \ |
|
148 const T *x = a.data (); \ |
|
149 const T *y = b.data (); \ |
|
150 DO_VV_OP (r, l, x, OP, y); \ |
|
151 return result; \ |
1213
|
152 } |
237
|
153 |
2383
|
154 MARRAY_AA_OP (operator +, +) |
|
155 MARRAY_AA_OP (operator -, -) |
|
156 MARRAY_AA_OP (product, *) |
|
157 MARRAY_AA_OP (quotient, /) |
237
|
158 |
|
159 // Unary MArray ops. |
|
160 |
|
161 template <class T> |
|
162 MArray<T> |
3574
|
163 operator + (const MArray<T>& a) |
|
164 { |
|
165 return a; |
|
166 } |
|
167 |
|
168 template <class T> |
|
169 MArray<T> |
237
|
170 operator - (const MArray<T>& a) |
|
171 { |
3504
|
172 int l = a.length (); |
|
173 MArray<T> result (l); |
|
174 T *r = result.fortran_vec (); |
|
175 const T *x = a.data (); |
|
176 NEG_V (r, l, x); |
|
177 return result; |
237
|
178 } |
|
179 |
|
180 /* |
|
181 ;;; Local Variables: *** |
|
182 ;;; mode: C++ *** |
|
183 ;;; End: *** |
|
184 */ |