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