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