237
|
1 /* |
|
2 |
1882
|
3 Copyright (C) 1996 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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
237
|
20 |
|
21 */ |
|
22 |
1296
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
237
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
237
|
29 #endif |
|
30 |
|
31 #include "MArray.h" |
|
32 #include "lo-error.h" |
|
33 |
1989
|
34 #include "MArray-defs.h" |
1213
|
35 |
1360
|
36 // One dimensional array with math ops. |
237
|
37 |
|
38 // Element by element MArray by scalar ops. |
|
39 |
|
40 template <class T> |
1213
|
41 MArray<T>& |
1230
|
42 operator += (MArray<T>& a, const T& s) |
237
|
43 { |
1213
|
44 DO_VS_OP2 (+=) |
1230
|
45 return a; |
237
|
46 } |
|
47 |
|
48 template <class T> |
1213
|
49 MArray<T>& |
1230
|
50 operator -= (MArray<T>& a, const T& s) |
237
|
51 { |
1213
|
52 DO_VS_OP2 (-=) |
1230
|
53 return a; |
237
|
54 } |
|
55 |
|
56 // Element by element MArray by MArray ops. |
|
57 |
|
58 template <class T> |
1213
|
59 MArray<T>& |
1230
|
60 operator += (MArray<T>& a, const MArray<T>& b) |
237
|
61 { |
1230
|
62 int l = a.length (); |
1213
|
63 if (l > 0) |
237
|
64 { |
1230
|
65 if (l != b.length ()) |
1213
|
66 (*current_liboctave_error_handler) \ |
|
67 ("nonconformant += array operation attempted"); \ |
|
68 else |
|
69 DO_VV_OP2 (+=); |
237
|
70 } |
1230
|
71 return a; |
237
|
72 } |
|
73 |
|
74 template <class T> |
1213
|
75 MArray<T>& |
1230
|
76 operator -= (MArray<T>& a, const MArray<T>& b) |
237
|
77 { |
1230
|
78 int l = a.length (); |
1213
|
79 if (l > 0) |
237
|
80 { |
1230
|
81 if (l != b.length ()) |
1213
|
82 (*current_liboctave_error_handler) \ |
|
83 ("nonconformant -= array operation attempted"); \ |
|
84 else |
|
85 DO_VV_OP2 (-=); |
237
|
86 } |
1230
|
87 return a; |
237
|
88 } |
|
89 |
1213
|
90 // Element by element MArray by scalar ops. |
|
91 |
|
92 #define MARRAY_AS_OP(OP) \ |
|
93 template <class T> \ |
|
94 MArray<T> \ |
|
95 operator OP (const MArray<T>& a, const T& s) \ |
|
96 { \ |
|
97 DO_VS_OP (OP); \ |
|
98 return MArray<T> (result, l); \ |
|
99 } |
237
|
100 |
1213
|
101 MARRAY_AS_OP (+) |
|
102 MARRAY_AS_OP (-) |
|
103 MARRAY_AS_OP (*) |
|
104 MARRAY_AS_OP (/) |
|
105 |
|
106 // Element by element scalar by MArray ops. |
237
|
107 |
1213
|
108 #define MARRAY_SA_OP(OP) \ |
|
109 template <class T> \ |
|
110 MArray<T> \ |
|
111 operator OP (const T& s, const MArray<T>& a) \ |
|
112 { \ |
|
113 DO_SV_OP (OP); \ |
|
114 return MArray<T> (result, l); \ |
|
115 } |
237
|
116 |
1213
|
117 MARRAY_SA_OP(+) |
|
118 MARRAY_SA_OP(-) |
|
119 MARRAY_SA_OP(*) |
|
120 MARRAY_SA_OP(/) |
|
121 |
|
122 // Element by element MArray by MArray ops. |
237
|
123 |
1213
|
124 #define MARRAY_AA_OP(FCN, OP, OP_STR) \ |
|
125 template <class T> \ |
|
126 MArray<T> \ |
|
127 FCN (const MArray<T>& a, const MArray<T>& b) \ |
|
128 { \ |
|
129 int l = a.length (); \ |
|
130 if (l != b.length ()) \ |
|
131 { \ |
|
132 (*current_liboctave_error_handler) \ |
|
133 ("nonconformant array " OP_STR " attempted"); \ |
|
134 return MArray<T> (); \ |
|
135 } \ |
|
136 if (l == 0) \ |
|
137 return MArray<T> (); \ |
|
138 DO_VV_OP (OP); \ |
|
139 return MArray<T> (result, l); \ |
|
140 } |
237
|
141 |
1213
|
142 MARRAY_AA_OP (operator +, +, "addition") |
|
143 MARRAY_AA_OP (operator -, -, "subtraction") |
|
144 MARRAY_AA_OP (product, *, "multiplication") |
|
145 MARRAY_AA_OP (quotient, /, "division") |
237
|
146 |
|
147 // Unary MArray ops. |
|
148 |
|
149 template <class T> |
|
150 MArray<T> |
|
151 operator - (const MArray<T>& a) |
|
152 { |
|
153 NEG_V; |
|
154 return MArray<T> (result, l); |
|
155 } |
|
156 |
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |