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