1 | /* -*- mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | // vim:sts=8:sw=8:ts=8:noet:sr:cino=>s,f0,{0,g0,(0,\:0,t0,+0,=s |
---|
3 | /* linbox/blackbox/polynomial.h |
---|
4 | * Copyright (C) 2005 Cl'ement Pernet |
---|
5 | * |
---|
6 | * Written by Cl'ement Pernet <Clement.Pernet@imag.fr> |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the GNU Lesser General Public |
---|
10 | * License as published by the Free Software Foundation; either |
---|
11 | * version 2 of the License, or (at your option) any later version. |
---|
12 | * |
---|
13 | * This library is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | * Lesser General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU Lesser General Public |
---|
19 | * License along with this library; if not, write to the |
---|
20 | * Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, |
---|
21 | * Boston, MA 02110-1301, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #ifndef __LINBOX_bb_polynomial_H |
---|
25 | #define __LINBOX_bb_polynomial_H |
---|
26 | |
---|
27 | #include "linbox/blackbox/blackbox-interface.h" |
---|
28 | #include "linbox/vector/vector-domain.h" |
---|
29 | // Namespace in which all LinBox library code resides |
---|
30 | namespace LinBox |
---|
31 | { |
---|
32 | template <class Blackbox, class Poly> |
---|
33 | class PolynomialBB ; |
---|
34 | template <class Blackbox, class Poly> |
---|
35 | class PolynomialBBOwner ; |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | namespace LinBox |
---|
40 | { |
---|
41 | |
---|
42 | /** \brief represent the matrix P(A) where A is a blackbox and P a polynomial |
---|
43 | |
---|
44 | \ingroup blackbox |
---|
45 | |
---|
46 | */ |
---|
47 | template <class Blackbox, class Poly> |
---|
48 | class PolynomialBB : public BlackboxInterface { |
---|
49 | public: |
---|
50 | |
---|
51 | typedef typename Blackbox::Field Field; |
---|
52 | typedef typename Blackbox::Element Element; |
---|
53 | typedef Poly Polynomial; |
---|
54 | typedef PolynomialBB<Blackbox,Polynomial> Self_t; |
---|
55 | |
---|
56 | /** Constructor from a black box and a polynomial. |
---|
57 | */ |
---|
58 | PolynomialBB (const Blackbox& A, const Polynomial& P) : |
---|
59 | _A_ptr(&A), _P_ptr(&P), _VD(A.field()) |
---|
60 | {} |
---|
61 | |
---|
62 | PolynomialBB (const Blackbox *A_ptr, const Polynomial * P_ptr) |
---|
63 | : _A_ptr(A_ptr), _P_ptr(P_ptr), _VD(A_ptr->field()) |
---|
64 | { |
---|
65 | } |
---|
66 | |
---|
67 | /** Copy constructor. |
---|
68 | * Creates new black box objects in dynamic memory. |
---|
69 | * @param M constant reference to compose black box matrix |
---|
70 | */ |
---|
71 | PolynomialBB (const PolynomialBB<Blackbox, Polynomial> &Mat) : |
---|
72 | _A_ptr(Mat._A_ptr), _P_ptr(Mat._P_ptr), _VD(Mat._VD) |
---|
73 | { |
---|
74 | } |
---|
75 | |
---|
76 | /// Destructor |
---|
77 | ~PolynomialBB (void) |
---|
78 | { |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | /** Application of BlackBox matrix. |
---|
83 | * <code>y = P(A)x</code> |
---|
84 | * Requires one vector conforming to the \ref LinBox |
---|
85 | * vector @link Archetypes archetype@endlink. |
---|
86 | * Required by abstract base class. |
---|
87 | * @return reference to vector y containing output. |
---|
88 | * @param x constant reference to vector to contain input |
---|
89 | * @param y |
---|
90 | */ |
---|
91 | template <class Vector1, class Vector2> |
---|
92 | inline Vector1 &apply (Vector1 &y, const Vector2 &x) const |
---|
93 | { |
---|
94 | Vector2 u (x); |
---|
95 | Vector2 v(u.size()); |
---|
96 | _VD.mul( y, x, _P_ptr->operator[](0) ); |
---|
97 | for (size_t i=1; i<_P_ptr->size(); ++i){ |
---|
98 | _A_ptr->apply( v, u ); |
---|
99 | _VD.axpyin( y, _P_ptr->operator[](i), v); |
---|
100 | u=v; |
---|
101 | } |
---|
102 | return y; |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /** Application of BlackBox matrix transpose. |
---|
107 | * <code>y= transpose(A*B)*x</code>. |
---|
108 | * Requires one vector conforming to the \ref LinBox |
---|
109 | * vector @link Archetypes archetype@endlink. |
---|
110 | * Required by abstract base class. |
---|
111 | * @return reference to vector y containing output. |
---|
112 | * @param x constant reference to vector to contain input |
---|
113 | * @param y |
---|
114 | */ |
---|
115 | template <class Vector1, class Vector2> |
---|
116 | inline Vector1 &applyTranspose (Vector1 &y, const Vector2 &x) const |
---|
117 | { |
---|
118 | Vector2 u( x ); |
---|
119 | Vector2 v(u.size()); |
---|
120 | _VD.mul( y, x, _P_ptr->operator[](0)); |
---|
121 | for (size_t i=1; i<_P_ptr->size(); ++i){ |
---|
122 | _A_ptr->applyTranspose( v, u ); |
---|
123 | _VD.axpyin( y, _P_ptr->operator[](i), v); |
---|
124 | u=v; |
---|
125 | } |
---|
126 | return y; |
---|
127 | } |
---|
128 | |
---|
129 | |
---|
130 | template<typename _Tp1, class Poly1 = typename Polynomial::template rebind<_Tp1>::other> |
---|
131 | struct rebind { |
---|
132 | typedef PolynomialBBOwner<typename Blackbox::template rebind<_Tp1>::other, Poly1> other; |
---|
133 | |
---|
134 | void operator() (other & Ap, const Self_t& A, const _Tp1& F) { |
---|
135 | typename Polynomial::template rebind<_Tp1>() (Ap.getDataPolynomial(), *A.getPolynomial(), F); |
---|
136 | typename Blackbox::template rebind<_Tp1>() (Ap.getDataBlackbox(), *A.getBlackbox(),F); |
---|
137 | |
---|
138 | } |
---|
139 | }; |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | /** Retreive row dimensions of BlackBox matrix. |
---|
144 | * This may be needed for applying preconditioners. |
---|
145 | * Required by abstract base class. |
---|
146 | * @return integer number of rows of black box matrix. |
---|
147 | */ |
---|
148 | size_t rowdim (void) const |
---|
149 | { |
---|
150 | if (_A_ptr != 0) |
---|
151 | return _A_ptr->rowdim (); |
---|
152 | else |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | /** Retreive column dimensions of BlackBox matrix. |
---|
157 | * Required by abstract base class. |
---|
158 | * @return integer number of columns of black box matrix. |
---|
159 | */ |
---|
160 | size_t coldim (void) const |
---|
161 | { |
---|
162 | if (_A_ptr != 0) |
---|
163 | return _A_ptr->coldim (); |
---|
164 | else |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | |
---|
169 | const Polynomial* getPolynomial () const { return _P_ptr; } |
---|
170 | const Blackbox* getBlackbox () const { return _A_ptr; } |
---|
171 | const Field& field () const {return _A_ptr->field();} |
---|
172 | private: |
---|
173 | |
---|
174 | // Pointers to A and P |
---|
175 | const Blackbox *_A_ptr; |
---|
176 | const Polynomial *_P_ptr; |
---|
177 | const VectorDomain<Field> _VD; |
---|
178 | |
---|
179 | }; |
---|
180 | |
---|
181 | } // namespace LinBox |
---|
182 | |
---|
183 | // Namespace in which all LinBox library code resides |
---|
184 | namespace LinBox |
---|
185 | { |
---|
186 | |
---|
187 | /** \brief represent the matrix P(A) where A is a blackbox and P a polynomial |
---|
188 | |
---|
189 | \ingroup blackbox |
---|
190 | |
---|
191 | */ |
---|
192 | template <class Blackbox, class Poly> |
---|
193 | class PolynomialBBOwner : public BlackboxInterface { |
---|
194 | public: |
---|
195 | |
---|
196 | typedef typename Blackbox::Field Field; |
---|
197 | typedef typename Blackbox::Element Element; |
---|
198 | typedef Poly Polynomial; |
---|
199 | typedef PolynomialBBOwner<Blackbox,Polynomial> Self_t; |
---|
200 | |
---|
201 | /** Constructor from a black box and a polynomial. |
---|
202 | */ |
---|
203 | PolynomialBBOwner (const Blackbox& A, const Polynomial& P) : |
---|
204 | _A_data(A), _P_data(P), _VD(A.field()) |
---|
205 | {} |
---|
206 | |
---|
207 | PolynomialBBOwner (const Blackbox *A_data, const Polynomial * P_data) : |
---|
208 | _A_data(*A_data), _P_data(*P_data), _VD(A_data->field()) |
---|
209 | { |
---|
210 | } |
---|
211 | |
---|
212 | /** Copy constructor. |
---|
213 | * Creates new black box objects in dynamic memory. |
---|
214 | * @param M constant reference to compose black box matrix |
---|
215 | */ |
---|
216 | PolynomialBBOwner (const PolynomialBBOwner<Blackbox, Polynomial> &Mat) : |
---|
217 | _A_data(Mat._A_data), _P_data(Mat._P_data), _VD(Mat._VD) |
---|
218 | { |
---|
219 | } |
---|
220 | |
---|
221 | /// Destructor |
---|
222 | ~PolynomialBBOwner (void) |
---|
223 | { |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | /** Application of BlackBox matrix. |
---|
228 | * <code>y = P(A)x</code> |
---|
229 | * Requires one vector conforming to the \ref LinBox |
---|
230 | * vector @link Archetypes archetype@endlink. |
---|
231 | * Required by abstract base class. |
---|
232 | * @return reference to vector y containing output. |
---|
233 | * @param x constant reference to vector to contain input |
---|
234 | * @param y |
---|
235 | */ |
---|
236 | template <class Vector1, class Vector2> |
---|
237 | inline Vector1 &apply (Vector1 &y, const Vector2 &x) const |
---|
238 | { |
---|
239 | Vector2 u (x); |
---|
240 | Vector2 v(u.size()); |
---|
241 | _VD.mul( y, x, _P_data[0] ); |
---|
242 | for (size_t i=1; i<_P_data.size(); ++i){ |
---|
243 | _A_data.apply( v, u ); |
---|
244 | _VD.axpyin( y, _P_data[i], v); |
---|
245 | u=v; |
---|
246 | } |
---|
247 | return y; |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | /** Application of BlackBox matrix transpose. |
---|
252 | * <code>y= transpose(A*B)*x</code>. |
---|
253 | * Requires one vector conforming to the \ref LinBox |
---|
254 | * vector @link Archetypes archetype@endlink. |
---|
255 | * Required by abstract base class. |
---|
256 | * @return reference to vector y containing output. |
---|
257 | * @param x constant reference to vector to contain input |
---|
258 | * @param y |
---|
259 | */ |
---|
260 | template <class Vector1, class Vector2> |
---|
261 | inline Vector1 &applyTranspose (Vector1 &y, const Vector2 &x) const |
---|
262 | { |
---|
263 | Vector2 u( x ); |
---|
264 | Vector2 v(u.size()); |
---|
265 | _VD.mul( y, x, _P_data[0]); |
---|
266 | for (size_t i=1; i<_P_data.size(); ++i){ |
---|
267 | _A_data.applyTranspose( v, u ); |
---|
268 | _VD.axpyin( y, _P_data[i], v); |
---|
269 | u=v; |
---|
270 | } |
---|
271 | return y; |
---|
272 | } |
---|
273 | |
---|
274 | |
---|
275 | template<typename _Tp1, class Poly1 = typename Polynomial::template rebind<_Tp1>::other> |
---|
276 | struct rebind { |
---|
277 | typedef PolynomialBBOwner<typename Blackbox::template rebind<_Tp1>::other, Poly1> other; |
---|
278 | |
---|
279 | void operator() (other & Ap, const Self_t& A, const _Tp1& F) { |
---|
280 | typename Polynomial::template rebind<_Tp1>() (Ap.getDataPolynomial(), A.getDataPolynomial(), F); |
---|
281 | typename Blackbox::template rebind<_Tp1>() (Ap.getDataBlackbox(), A.getDataPolynomial(),F); |
---|
282 | |
---|
283 | } |
---|
284 | }; |
---|
285 | |
---|
286 | template<typename _BBt, typename _Polt, typename Field> |
---|
287 | PolynomialBBOwner (const PolynomialBB<_BBt, _Polt> &Mat, const Field& F) : |
---|
288 | _VD(F), |
---|
289 | _A_data(*(Mat.getBlackbox()), F), |
---|
290 | _P_data(*(Mat.getPolynomial()), F) |
---|
291 | { |
---|
292 | typename _BBt::template rebind<Field>()(_A_data, *(Mat.getBlackbox()), F); |
---|
293 | typename _Polt::template rebind<Field>()(_P_data, *(Mat.getPolynomial()), F); |
---|
294 | } |
---|
295 | |
---|
296 | template<typename _BBt, typename _Polt, typename Field> |
---|
297 | PolynomialBBOwner (const PolynomialBBOwner<_BBt, _Polt> &Mat, const Field& F) : |
---|
298 | _A_data(Mat.getDataBlackbox(), F), |
---|
299 | _P_data(Mat.getDataPolynomial(), F) |
---|
300 | { |
---|
301 | typename _BBt::template rebind<Field>()(_A_data, Mat.getDataBlackbox(), F); |
---|
302 | typename _Polt::template rebind<Field>()(_P_data, Mat.getDataPolynomial(), F); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | /** Retreive row dimensions of BlackBox matrix. |
---|
308 | * This may be needed for applying preconditioners. |
---|
309 | * Required by abstract base class. |
---|
310 | * @return integer number of rows of black box matrix. |
---|
311 | */ |
---|
312 | size_t rowdim (void) const |
---|
313 | { |
---|
314 | return _A_data.rowdim (); |
---|
315 | } |
---|
316 | |
---|
317 | /** Retreive column dimensions of BlackBox matrix. |
---|
318 | * Required by abstract base class. |
---|
319 | * @return integer number of columns of black box matrix. |
---|
320 | */ |
---|
321 | size_t coldim (void) const |
---|
322 | { |
---|
323 | return _A_data.coldim (); |
---|
324 | } |
---|
325 | |
---|
326 | |
---|
327 | const Polynomial& getDataPolynomial () const { return _P_data; } |
---|
328 | const Blackbox& getDataBlackbox () const { return _A_data; } |
---|
329 | const Field& field () const {return _A_data.field();} |
---|
330 | private: |
---|
331 | |
---|
332 | const VectorDomain<Field> _VD; |
---|
333 | // Matrix A and polynomial P |
---|
334 | Blackbox _A_data; |
---|
335 | Polynomial _P_data; |
---|
336 | |
---|
337 | }; |
---|
338 | |
---|
339 | } // namespace LinBox |
---|
340 | |
---|
341 | #endif // __LINBOX_bb_polynomial_H |
---|
342 | |
---|