237
|
1 /* |
|
2 |
7017
|
3 Copyright (C) 1993, 1995, 1996, 1997, 2000, 2002, 2003, 2004, 2005, |
|
4 2007 John W. Eaton |
237
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
7016
|
10 Free Software Foundation; either version 3 of the License, or (at your |
|
11 option) any later version. |
237
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
7016
|
19 along with Octave; see the file COPYING. If not, see |
|
20 <http://www.gnu.org/licenses/>. |
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 |
6508
|
36 template <class T> |
|
37 double |
|
38 MArray<T>::norm (double) const |
|
39 { |
|
40 (*current_liboctave_error_handler) |
|
41 ("norm: only implemented for double and complex values"); |
|
42 |
|
43 return 0; |
|
44 } |
|
45 |
237
|
46 // Element by element MArray by scalar ops. |
|
47 |
|
48 template <class T> |
1213
|
49 MArray<T>& |
1230
|
50 operator += (MArray<T>& a, const T& s) |
237
|
51 { |
4646
|
52 DO_VS_OP2 (T, a, +=, s) |
1230
|
53 return a; |
237
|
54 } |
|
55 |
|
56 template <class T> |
1213
|
57 MArray<T>& |
1230
|
58 operator -= (MArray<T>& a, const T& s) |
237
|
59 { |
4646
|
60 DO_VS_OP2 (T, a, -=, s) |
1230
|
61 return a; |
237
|
62 } |
|
63 |
|
64 // Element by element MArray by MArray ops. |
|
65 |
|
66 template <class T> |
1213
|
67 MArray<T>& |
1230
|
68 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
69 { |
5275
|
70 octave_idx_type l = a.length (); |
1213
|
71 if (l > 0) |
237
|
72 { |
5275
|
73 octave_idx_type bl = b.length (); |
2383
|
74 if (l != bl) |
|
75 gripe_nonconformant ("operator +=", l, bl); |
1213
|
76 else |
4646
|
77 DO_VV_OP2 (T, a, +=, b); |
237
|
78 } |
1230
|
79 return a; |
237
|
80 } |
|
81 |
|
82 template <class T> |
1213
|
83 MArray<T>& |
1230
|
84 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
85 { |
5275
|
86 octave_idx_type l = a.length (); |
1213
|
87 if (l > 0) |
237
|
88 { |
5275
|
89 octave_idx_type bl = b.length (); |
2383
|
90 if (l != bl) |
|
91 gripe_nonconformant ("operator -=", l, bl); |
1213
|
92 else |
4646
|
93 DO_VV_OP2 (T, a, -=, b); |
237
|
94 } |
1230
|
95 return a; |
237
|
96 } |
|
97 |
1213
|
98 // Element by element MArray by scalar ops. |
|
99 |
|
100 #define MARRAY_AS_OP(OP) \ |
|
101 template <class T> \ |
|
102 MArray<T> \ |
|
103 operator OP (const MArray<T>& a, const T& s) \ |
|
104 { \ |
3504
|
105 MArray<T> result (a.length ()); \ |
|
106 T *r = result.fortran_vec (); \ |
5275
|
107 octave_idx_type l = a.length (); \ |
3504
|
108 const T *v = a.data (); \ |
|
109 DO_VS_OP (r, l, v, OP, s); \ |
|
110 return result; \ |
1213
|
111 } |
237
|
112 |
1213
|
113 MARRAY_AS_OP (+) |
|
114 MARRAY_AS_OP (-) |
|
115 MARRAY_AS_OP (*) |
|
116 MARRAY_AS_OP (/) |
|
117 |
|
118 // Element by element scalar by MArray ops. |
237
|
119 |
1213
|
120 #define MARRAY_SA_OP(OP) \ |
|
121 template <class T> \ |
|
122 MArray<T> \ |
|
123 operator OP (const T& s, const MArray<T>& a) \ |
|
124 { \ |
3504
|
125 MArray<T> result (a.length ()); \ |
|
126 T *r = result.fortran_vec (); \ |
5275
|
127 octave_idx_type l = a.length (); \ |
3504
|
128 const T *v = a.data (); \ |
|
129 DO_SV_OP (r, l, s, OP, v); \ |
|
130 return result; \ |
|
131 } |
237
|
132 |
1213
|
133 MARRAY_SA_OP(+) |
|
134 MARRAY_SA_OP(-) |
|
135 MARRAY_SA_OP(*) |
|
136 MARRAY_SA_OP(/) |
|
137 |
|
138 // Element by element MArray by MArray ops. |
237
|
139 |
2383
|
140 #define MARRAY_AA_OP(FCN, OP) \ |
1213
|
141 template <class T> \ |
|
142 MArray<T> \ |
|
143 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
144 { \ |
5275
|
145 octave_idx_type l = a.length (); \ |
|
146 octave_idx_type bl = b.length (); \ |
2383
|
147 if (l != bl) \ |
1213
|
148 { \ |
2383
|
149 gripe_nonconformant (#FCN, l, bl); \ |
1213
|
150 return MArray<T> (); \ |
|
151 } \ |
|
152 if (l == 0) \ |
|
153 return MArray<T> (); \ |
3504
|
154 MArray<T> result (l); \ |
|
155 T *r = result.fortran_vec (); \ |
|
156 const T *x = a.data (); \ |
|
157 const T *y = b.data (); \ |
|
158 DO_VV_OP (r, l, x, OP, y); \ |
|
159 return result; \ |
1213
|
160 } |
237
|
161 |
2383
|
162 MARRAY_AA_OP (operator +, +) |
|
163 MARRAY_AA_OP (operator -, -) |
|
164 MARRAY_AA_OP (product, *) |
|
165 MARRAY_AA_OP (quotient, /) |
237
|
166 |
|
167 // Unary MArray ops. |
|
168 |
|
169 template <class T> |
|
170 MArray<T> |
3574
|
171 operator + (const MArray<T>& a) |
|
172 { |
|
173 return a; |
|
174 } |
|
175 |
|
176 template <class T> |
|
177 MArray<T> |
237
|
178 operator - (const MArray<T>& a) |
|
179 { |
5275
|
180 octave_idx_type l = a.length (); |
3504
|
181 MArray<T> result (l); |
|
182 T *r = result.fortran_vec (); |
|
183 const T *x = a.data (); |
|
184 NEG_V (r, l, x); |
|
185 return result; |
237
|
186 } |
|
187 |
|
188 /* |
|
189 ;;; Local Variables: *** |
|
190 ;;; mode: C++ *** |
|
191 ;;; End: *** |
|
192 */ |