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