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