1 /*
  2  *   Copyright (c) 2007, Michael Lehn
  3  *
  4  *   All rights reserved.
  5  *
  6  *   Redistribution and use in source and binary forms, with or without
  7  *   modification, are permitted provided that the following conditions
  8  *   are met:
  9  *
 10  *   1) Redistributions of source code must retain the above copyright
 11  *      notice, this list of conditions and the following disclaimer.
 12  *   2) Redistributions in binary form must reproduce the above copyright
 13  *      notice, this list of conditions and the following disclaimer in
 14  *      the documentation and/or other materials provided with the
 15  *      distribution.
 16  *   3) Neither the name of the FLENS development group nor the names of
 17  *      its contributors may be used to endorse or promote products derived
 18  *      from this software without specific prior written permission.
 19  *
 20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  */
 32 
 33 #ifndef FLENS_MATRIXTYPES_GENERAL_IMPL_GEMATRIX_H
 34 #define FLENS_MATRIXTYPES_GENERAL_IMPL_GEMATRIX_H 1
 35 
 36 #include <flens/aux/range.h>
 37 #include <flens/aux/underscore.h>
 38 #include <flens/matrixtypes/general/generalmatrix.h>
 39 #include <flens/matrixtypes/general/impl/ge/constelementclosure.h>
 40 #include <flens/matrixtypes/general/impl/ge/elementclosure.h>
 41 #include <flens/matrixtypes/general/impl/ge/initializer.h>
 42 #include <flens/scalartypes/impl/indexvariable.h>
 43 #include <flens/typedefs.h>
 44 
 45 namespace flens {
 46 
 47 // forward declarations
 48 template <typename A>
 49     class DenseVector;
 50 
 51 template <typename FS>
 52     class HeMatrix;
 53 
 54 template <typename FS>
 55     class SyMatrix;
 56 
 57 template <typename FS>
 58     class TrMatrix;
 59 
 60 template <typename FS>
 61 class GeMatrix
 62     : public GeneralMatrix<GeMatrix<FS> >
 63 {
 64     public:
 65         typedef FS                                      Engine;
 66         typedef typename Engine::ElementType            ElementType;
 67         typedef typename Engine::IndexType              IndexType;
 68 
 69         // view types from Engine
 70         typedef typename Engine::ConstView              EngineConstView;
 71         typedef typename Engine::View                   EngineView;
 72         typedef typename Engine::NoView                 EngineNoView;
 73 
 74         typedef typename Engine::ConstArrayView         ConstArrayView;
 75         typedef typename Engine::ArrayView              ArrayView;
 76         typedef typename Engine::Array                  Array;
 77 
 78         // view types
 79         typedef DenseVector<ConstArrayView>             ConstVectorView;
 80         typedef DenseVector<ArrayView>                  VectorView;
 81         typedef DenseVector<Array>                      Vector;
 82 
 83         typedef GeMatrix<EngineConstView>               ConstView;
 84         typedef GeMatrix<EngineView>                    View;
 85         typedef GeMatrix<EngineNoView>                  NoView;
 86 
 87         typedef HeMatrix<EngineConstView>               ConstHermitianView;
 88         typedef HeMatrix<EngineView>                    HermitianView;
 89         typedef HeMatrix<EngineNoView>                  HermitianNoView;
 90 
 91         typedef SyMatrix<EngineConstView>               ConstSymmetricView;
 92         typedef SyMatrix<EngineView>                    SymmetricView;
 93         typedef SyMatrix<EngineNoView>                  SymmetricNoView;
 94 
 95         typedef TrMatrix<EngineConstView>               ConstTriangularView;
 96         typedef TrMatrix<EngineView>                    TriangularView;
 97         typedef TrMatrix<EngineNoView>                  TriangularNoView;
 98 
 99     private:
100         typedef GeMatrix                                GE;
101 
102     public:
103         typedef flens::IndexVariable<IndexType>         IndexVariable;
104         typedef gematrix::ConstElementClosure<GE>       ConstElementClosure;
105         typedef gematrix::ElementClosure<GE>            ElementClosure;
106         typedef gematrix::Initializer<GE>               Initializer;
107 
108         // -- constructors -----------------------------------------------------
109         GeMatrix();
110 
111         GeMatrix(IndexType numRows, IndexType numCols);
112 
113         GeMatrix(IndexType numRows, IndexType numCols,
114                  IndexType firstRow, IndexType firstCol);
115 
116         GeMatrix(const Range<IndexType> &rowRange,
117                  const Range<IndexType> &colRange);
118 
119         GeMatrix(const Engine &engine);
120 
121         GeMatrix(const GeMatrix &rhs);
122 
123         template <typename RHS>
124             GeMatrix(const GeMatrix<RHS> &rhs);
125 
126         template <typename RHS>
127             GeMatrix(GeMatrix<RHS> &rhs);
128 
129         template <typename RHS>
130             GeMatrix(const Matrix<RHS> &rhs);
131 
132         template <typename VECTOR>
133             GeMatrix(IndexType numRows, IndexType numCols,
134                      VECTOR &&rhs);
135 
136         template <typename VECTOR>
137             GeMatrix(IndexType numRows, IndexType numCols,
138                      VECTOR &&rhs,
139                      IndexType leadingDimension);
140 
141         // -- operators --------------------------------------------------------
142         Initializer
143         operator=(const ElementType &value);
144 
145         GeMatrix &
146         operator=(const GeMatrix &rhs);
147 
148         template <typename RHS>
149             GeMatrix &
150             operator=(const Matrix<RHS> &rhs);
151 
152         template <typename RHS>
153             GeMatrix &
154             operator+=(const Matrix<RHS> &rhs);
155 
156         template <typename RHS>
157             GeMatrix &
158             operator-=(const Matrix<RHS> &rhs);
159 
160         GeMatrix<FS> &
161         operator+=(const ElementType &alpha);
162 
163         GeMatrix<FS> &
164         operator-=(const ElementType &alpha);
165 
166         GeMatrix &
167         operator*=(const ElementType &alpha);
168 
169         GeMatrix &
170         operator/=(const ElementType &alpha);
171 
172         const ElementType &
173         operator()(IndexType row, IndexType col) const;
174 
175         ElementType &
176         operator()(IndexType row, IndexType col);
177 
178         template <typename S>
179             const gematrix::ConstElementClosure<GeMatrix,
180                                                 typename Scalar<S>::Impl>
181             operator()(const Scalar<S> &row, const Scalar<S> &col) const;
182 
183         const ConstElementClosure
184         operator()(const IndexVariable &row, const IndexVariable &col) const;
185 
186         ElementClosure
187         operator()(IndexVariable &row, IndexVariable &col);
188 
189         // -- methods ----------------------------------------------------------
190         IndexType
191         numRows() const;
192 
193         IndexType
194         numCols() const;
195 
196         IndexType
197         firstRow() const;
198 
199         IndexType
200         lastRow() const;
201 
202         IndexType
203         firstCol() const;
204 
205         IndexType
206         lastCol() const;
207 
208         Range<IndexType>
209         rows() const;
210 
211         Range<IndexType>
212         cols() const;
213 
214         const ElementType *
215         data() const;
216 
217         ElementType *
218         data();
219 
220         IndexType
221         leadingDimension() const;
222 
223         StorageOrder
224         order() const;
225 
226         template <typename RHS>
227             bool
228             resize(const GeMatrix<RHS> &rhs,
229                    const ElementType &value = ElementType());
230 
231         bool
232         resize(IndexType numRows, IndexType numCols,
233                IndexType firstRowIndex = Engine::defaultIndexBase,
234                IndexType firstColIndex = Engine::defaultIndexBase,
235                const ElementType &value = ElementType());
236 
237         bool
238         fill(const ElementType &value = ElementType(0));
239 
240         void
241         changeIndexBase(IndexType firstRowIndex, IndexType firstColIndex);
242 
243         // -- views ------------------------------------------------------------
244         // vectorize matrix
245         const ConstVectorView
246         vectorView() const;
247 
248         VectorView
249         vectorView();
250 
251         // vectorize matrix and select range
252         const ConstVectorView
253         vectorView(IndexType from, IndexType to) const;
254 
255         VectorView
256         vectorView(IndexType from, IndexType to);
257 
258         // diag views
259         const ConstVectorView
260         diag(IndexType d) const;
261 
262         VectorView
263         diag(IndexType d);
264 
265         // triangular views
266         const ConstTriangularView
267         upper() const;
268 
269         TriangularView
270         upper();
271 
272         const ConstTriangularView
273         upperUnit() const;
274 
275         TriangularView
276         upperUnit();
277 
278         const ConstTriangularView
279         strictUpper() const;
280 
281         TriangularView
282         strictUpper();
283 
284         const ConstTriangularView
285         lower() const;
286 
287         TriangularView
288         lower();
289 
290         const ConstTriangularView
291         lowerUnit() const;
292 
293         TriangularView
294         lowerUnit();
295 
296         const ConstTriangularView
297         strictLower() const;
298 
299         TriangularView
300         strictLower();
301 
302         // rectangular views
303         const ConstView
304         operator()(const Range<IndexType> &rows,
305                    const Range<IndexType> &cols) const;
306 
307         View
308         operator()(const Range<IndexType> &rows,
309                    const Range<IndexType> &cols);
310 
311         template <typename RHS>
312             const ConstView
313             operator()(const GeMatrix<RHS> &A) const;
314 
315         template <typename RHS>
316             View
317             operator()(const GeMatrix<RHS> &A);
318 
319         // rectangular views (all rows selected)
320         const ConstView
321         operator()(const Underscore<IndexType> &,
322                    const Range<IndexType> &cols) const;
323 
324         View
325         operator()(const Underscore<IndexType> &,
326                    const Range<IndexType> &cols);
327 
328         // rectangular views (all columns selected)
329         const ConstView
330         operator()(const Range<IndexType> &rows,
331                    const Underscore<IndexType> &) const;
332 
333         View
334         operator()(const Range<IndexType> &rows,
335                    const Underscore<IndexType> &);
336 
337         // row view (vector view)
338         const ConstVectorView
339         operator()(IndexType row, const Underscore<IndexType> &) const;
340 
341         VectorView
342         operator()(IndexType row, const Underscore<IndexType> &);
343 
344         const ConstVectorView
345         operator()(IndexType row, const Range<IndexType> &cols) const;
346 
347         VectorView
348         operator()(IndexType row, const Range<IndexType> &cols);
349 
350         // column view (vector view)
351         const ConstVectorView
352         operator()(const Underscore<IndexType> &, IndexType col) const;
353 
354         VectorView
355         operator()(const Underscore<IndexType> &, IndexType col);
356 
357         const ConstVectorView
358         operator()(const Range<IndexType> &rows, IndexType col) const;
359 
360         VectorView
361         operator()(const Range<IndexType> &rows, IndexType col);
362 
363         // -- implementation ---------------------------------------------------
364         const Engine &
365         engine() const;
366 
367         Engine &
368         engine();
369 
370     private:
371         Engine _engine;
372 };
373 
374 // namespace flens
375 
376 #endif // FLENS_MATRIXTYPES_GENERAL_IMPL_GEMATRIX_H