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