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_TRIANGULAR_IMPL_TRMATRIX_TCC
 34 #define FLENS_MATRIXTYPES_TRIANGULAR_IMPL_TRMATRIX_TCC 1
 35 
 36 #include <flens/matrixtypes/general/impl/gematrix.h>
 37 #include <flens/matrixtypes/symmetric/impl/symatrix.h>
 38 #include <flens/typedefs.h>
 39 
 40 namespace flens {
 41 
 42 template <typename FS>
 43 TrMatrix<FS>::TrMatrix()
 44 {
 45 }
 46 
 47 template <typename FS>
 48 TrMatrix<FS>::TrMatrix(IndexType dim)
 49     : _engine(dim, dim)
 50 {
 51 }
 52 
 53 template <typename FS>
 54 TrMatrix<FS>::TrMatrix(IndexType numRows, IndexType numCols)
 55     : _engine(numRows, numCols)
 56 {
 57 }
 58 
 59 template <typename FS>
 60 TrMatrix<FS>::TrMatrix(const Engine &engine, StorageUpLo upLo, Diag diag)
 61     : _engine(engine), _upLo(upLo), _diag(diag)
 62 {
 63 }
 64 
 65 template <typename FS>
 66 TrMatrix<FS>::TrMatrix(const TrMatrix &rhs)
 67     : TriangularMatrix<TrMatrix<FS> >(),
 68       _engine(rhs.engine()), _upLo(rhs.upLo()), _diag(rhs.diag())
 69 {
 70 }
 71 
 72 template <typename FS>
 73 template <typename RHS>
 74 TrMatrix<FS>::TrMatrix(const TrMatrix<RHS> &rhs)
 75     : _engine(rhs.engine()), _upLo(rhs.upLo()), _diag(rhs.diag())
 76 {
 77 }
 78 
 79 template <typename FS>
 80 template <typename RHS>
 81 TrMatrix<FS>::TrMatrix(TrMatrix<RHS> &rhs)
 82     : _engine(rhs.engine()), _upLo(rhs.upLo()), _diag(rhs.diag())
 83 {
 84 }
 85 
 86 template <typename FS>
 87 template <typename RHS>
 88 TrMatrix<FS>::TrMatrix(const Matrix<RHS> &rhs)
 89 {
 90     assign(rhs, *this);
 91 }
 92 
 93 // -- operators ----------------------------------------------------------------
 94 
 95 template <typename FS>
 96 void
 97 TrMatrix<FS>::operator=(const ElementType &value)
 98 {
 99     engine().fill(_upLo, value);
100 }
101 
102 template <typename FS>
103 TrMatrix<FS> &
104 TrMatrix<FS>::operator=(const TrMatrix &rhs)
105 {
106     if (this!=&rhs) {
107         assign(rhs, *this);
108     }
109     return *this;
110 }
111 
112 template <typename FS>
113 template <typename RHS>
114 TrMatrix<FS> &
115 TrMatrix<FS>::operator=(const Matrix<RHS> &rhs)
116 {
117     assign(rhs, *this);
118     return *this;
119 }
120 
121 template <typename FS>
122 const typename TrMatrix<FS>::ElementType &
123 TrMatrix<FS>::operator()(IndexType row, IndexType col) const
124 {
125 #   ifndef NDEBUG
126     if (_upLo==Upper) {
127         ASSERT(col-firstCol()>=row-firstRow());
128     } else {
129         ASSERT(col-firstCol()<=row-firstRow());
130     }
131     ASSERT(!((_diag==Unit) && (col==row)));
132 #   endif
133     return _engine(row, col);
134 }
135 
136 template <typename FS>
137 typename TrMatrix<FS>::ElementType &
138 TrMatrix<FS>::operator()(IndexType row, IndexType col)
139 {
140 #   ifndef NDEBUG
141     if (_upLo==Upper) {
142         ASSERT(col-firstCol()>=row-firstRow());
143     } else {
144         ASSERT(col-firstCol()<=row-firstRow());
145     }
146     ASSERT(!((_diag==Unit) && (col==row)));
147 #   endif
148     return _engine(row, col);
149 }
150 
151 // rectangular views
152 template <typename FS>
153 const typename TrMatrix<FS>::ConstGeneralView
154 TrMatrix<FS>::operator()(const Range<IndexType> &rows,
155                          const Range<IndexType> &cols) const
156 {
157     return general()(rows, cols);
158 }
159 
160 template <typename FS>
161 typename TrMatrix<FS>::GeneralView
162 TrMatrix<FS>::operator()(const Range<IndexType> &rows,
163                          const Range<IndexType> &cols)
164 {
165     return general()(rows, cols);
166 }
167 
168 // rectangular views (all rows selected)
169 template <typename FS>
170 const typename TrMatrix<FS>::ConstGeneralView
171 TrMatrix<FS>::operator()(const Underscore<IndexType> &_,
172                          const Range<IndexType> &cols) const
173 {
174     return general()(_,cols);
175 }
176 
177 template <typename FS>
178 typename TrMatrix<FS>::GeneralView
179 TrMatrix<FS>::operator()(const Underscore<IndexType> &_,
180                          const Range<IndexType> &cols)
181 {
182     return general()(_,cols);
183 }
184 
185 // rectangular views (all columns selected)
186 template <typename FS>
187 const typename TrMatrix<FS>::ConstGeneralView
188 TrMatrix<FS>::operator()(const Range<IndexType> &rows,
189                          const Underscore<IndexType> &_) const
190 {
191     return general()(rows,_);
192 }
193 
194 template <typename FS>
195 typename TrMatrix<FS>::GeneralView
196 TrMatrix<FS>::operator()(const Range<IndexType> &rows,
197                          const Underscore<IndexType> &_)
198 {
199     return general()(rows,_);
200 }
201 
202 // row view (vector view)
203 template <typename FS>
204 const typename TrMatrix<FS>::ConstVectorView
205 TrMatrix<FS>::operator()(IndexType row, const Underscore<IndexType> &_) const
206 {
207     return general()(row,_);
208 }
209 
210 template <typename FS>
211 typename TrMatrix<FS>::VectorView
212 TrMatrix<FS>::operator()(IndexType row, const Underscore<IndexType> &_)
213 {
214     return general()(row,_);
215 }
216 
217 template <typename FS>
218 const typename TrMatrix<FS>::ConstVectorView
219 TrMatrix<FS>::operator()(IndexType row, const Range<IndexType> &cols) const
220 {
221     return general()(row,cols);
222 }
223 
224 template <typename FS>
225 typename TrMatrix<FS>::VectorView
226 TrMatrix<FS>::operator()(IndexType row, const Range<IndexType> &cols)
227 {
228     return general()(row,cols);
229 }
230 
231 // column view (vector view)
232 template <typename FS>
233 const typename TrMatrix<FS>::ConstVectorView
234 TrMatrix<FS>::operator()(const Underscore<IndexType> &_, IndexType col) const
235 {
236     return general()(_,col);
237 }
238 
239 template <typename FS>
240 typename TrMatrix<FS>::VectorView
241 TrMatrix<FS>::operator()(const Underscore<IndexType> &_, IndexType col)
242 {
243     return general()(_,col);
244 }
245 
246 template <typename FS>
247 const typename TrMatrix<FS>::ConstVectorView
248 TrMatrix<FS>::operator()(const Range<IndexType> &rows, IndexType col) const
249 {
250     return general()(rows,col);
251 }
252 
253 template <typename FS>
254 typename TrMatrix<FS>::VectorView
255 TrMatrix<FS>::operator()(const Range<IndexType> &rows, IndexType col)
256 {
257     return general()(rows,col);
258 }
259 
260 // -- views ------------------------------------------------------------
261 // general views
262 template <typename FS>
263 const typename TrMatrix<FS>::ConstGeneralView
264 TrMatrix<FS>::general() const
265 {
266     return ConstGeneralView(_engine);
267 }
268 
269 template <typename FS>
270 typename TrMatrix<FS>::GeneralView
271 TrMatrix<FS>::general()
272 {
273     return GeneralView(_engine);
274 }
275 
276 // hermitian views
277 template <typename FS>
278 const typename TrMatrix<FS>::ConstHermitianView
279 TrMatrix<FS>::hermitian() const
280 {
281     ASSERT(numRows()==numCols());
282     return ConstHermitianView(_engine, upLo());
283 }
284 
285 template <typename FS>
286 typename TrMatrix<FS>::HermitianView
287 TrMatrix<FS>::hermitian()
288 {
289     ASSERT(numRows()==numCols());
290     return HermitianView(_engine, upLo());
291 }
292 
293 // symmetric views
294 template <typename FS>
295 const typename TrMatrix<FS>::ConstSymmetricView
296 TrMatrix<FS>::symmetric() const
297 {
298     ASSERT(numRows()==numCols());
299     return ConstSymmetricView(_engine, upLo());
300 }
301 
302 template <typename FS>
303 typename TrMatrix<FS>::SymmetricView
304 TrMatrix<FS>::symmetric()
305 {
306     ASSERT(numRows()==numCols());
307     return SymmetricView(_engine, upLo());
308 }
309 
310 // -- methods ------------------------------------------------------------------
311 template <typename FS>
312 typename TrMatrix<FS>::IndexType
313 TrMatrix<FS>::dim() const
314 {
315     ASSERT(_engine.numRows()==_engine.numCols());
316 
317     return _engine.numRows();
318 }
319 
320 template <typename FS>
321 typename TrMatrix<FS>::IndexType
322 TrMatrix<FS>::numRows() const
323 {
324     return _engine.numRows();
325 }
326 
327 template <typename FS>
328 typename TrMatrix<FS>::IndexType
329 TrMatrix<FS>::numCols() const
330 {
331     return _engine.numCols();
332 }
333 
334 template <typename FS>
335 typename TrMatrix<FS>::IndexType
336 TrMatrix<FS>::firstRow() const
337 {
338     return _engine.firstRow();
339 }
340 
341 template <typename FS>
342 typename TrMatrix<FS>::IndexType
343 TrMatrix<FS>::lastRow() const
344 {
345     return _engine.lastRow();
346 }
347 
348 template <typename FS>
349 typename TrMatrix<FS>::IndexType
350 TrMatrix<FS>::firstCol() const
351 {
352     return _engine.firstCol();
353 }
354 
355 template <typename FS>
356 typename TrMatrix<FS>::IndexType
357 TrMatrix<FS>::lastCol() const
358 {
359     return _engine.lastCol();
360 }
361 
362 template <typename FS>
363 const typename TrMatrix<FS>::ElementType *
364 TrMatrix<FS>::data() const
365 {
366     return _engine.data();
367 }
368 
369 template <typename FS>
370 typename TrMatrix<FS>::ElementType *
371 TrMatrix<FS>::data()
372 {
373     return _engine.data();
374 }
375 
376 template <typename FS>
377 typename TrMatrix<FS>::IndexType
378 TrMatrix<FS>::leadingDimension() const
379 {
380     return _engine.leadingDimension();
381 }
382 
383 template <typename FS>
384 StorageOrder
385 TrMatrix<FS>::order() const
386 {
387     return _engine.order;
388 }
389 
390 template <typename FS>
391 template <typename RHS>
392 bool
393 TrMatrix<FS>::resize(const TrMatrix<RHS> &rhs,
394                      const ElementType &value)
395 {
396     return _engine.resize(rhs.engine(), value);
397 }
398 
399 template <typename FS>
400 bool
401 TrMatrix<FS>::resize(IndexType numRows, IndexType numCols,
402                      IndexType firstRowIndex, IndexType firstColIndex,
403                      const ElementType &value)
404 {
405     return _engine.resize(numRows, numCols,
406                           firstRowIndex, firstColIndex,
407                           value);
408 }
409 
410 // -- implementation -----------------------------------------------------------
411 template <typename FS>
412 const typename TrMatrix<FS>::Engine &
413 TrMatrix<FS>::engine() const
414 {
415     return _engine;
416 }
417 
418 template <typename FS>
419 typename TrMatrix<FS>::Engine &
420 TrMatrix<FS>::engine()
421 {
422     return _engine;
423 }
424 
425 template <typename FS>
426 StorageUpLo
427 TrMatrix<FS>::upLo() const
428 {
429     return _upLo;
430 }
431 
432 template <typename FS>
433 StorageUpLo &
434 TrMatrix<FS>::upLo()
435 {
436     return _upLo;
437 }
438 
439 template <typename FS>
440 Diag
441 TrMatrix<FS>::diag() const
442 {
443     return _diag;
444 }
445 
446 template <typename FS>
447 Diag &
448 TrMatrix<FS>::diag()
449 {
450     return _diag;
451 }
452 
453 // namespace flens
454 
455 #endif // FLENS_MATRIXTYPES_TRIANGULAR_IMPL_TRMATRIX_TCC