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