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