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