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