1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
<doc  58
      59
      60
      61

      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
     100
     101
     102
     103
     104
     105
     106
<doc 107
     108
<doc 109
     110

     111
<doc 112
     113
     114

     115
<doc 116
     117
<doc 118
     119
<doc 120
     121

     122
<doc 123
     124

     125
<doc 126
     127

     128
     129
     130
<doc 131
     132

     133
<doc 134
     135

     136
<doc 137
     138
     139

     140
<doc 141
     142
     143

     144
<doc 145
     146
     147

     148
<doc 149
     150

     151
<doc 152
     153

     154
<doc 155
     156
     157
     158

     159
<doc 160
     161

     162
<doc 163
     164

     165
     166
<doc 167
     168

     169
<doc 170
     171

     172
<doc 173
     174

     175
     176
<doc 177
     178

     179
<doc 180
     181

     182
<doc 183
     184

     185
<doc 186
     187

     188
<doc 189
     190

     191
<doc 192
     193

     194
<doc 195
     196

     197
<doc 198
     199

     200
<doc 201
     202

     203
<doc 204
     205

     206
<doc 207
     208

     209
<doc 210
     211

     212
<doc 213
     214

     215
<doc 216
     217

     218
<doc 219
     220

     221
<doc 222
     223
     224
     225

     226
<doc 227
     228
     229
     230
     231

     232
     233
     234
<doc 235
     236

     237
<doc 238
     239

     240
     241
<doc 242
     243

     244
<doc 245
     246

     247
     248
<doc 249
     250

     251
<doc 252
     253

     254
     255
<doc 256
     257

     258
<doc 259
     260

     261
     262
<doc 263
     264
     265

     266
<doc 267
     268
     269

     270
     271
<doc 272
     273
     274

     275
<doc 276
     277
     278

     279
     280
<doc 281
     282
     283

     284
<doc 285
     286
     287

     288
     289
<doc 290
     291

     292
<doc 293
     294

     295
<doc 296
     297

     298
<doc 299
     300

     301
     302
<doc 303
     304

     305
<doc 306
     307

     308
<doc 309
     310

     311
<doc 312
     313

     314
     315
<doc 316
     317

     318
<doc 319
     320

     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
<doc 332
     333

     334
<doc 335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
<doc 348
     349
     350

     351
     352
     353
     354
     355
     356
     357
<doc 358
     359
     360

     361
     362
     363
     364
     365
     366
     367
     368
     369
<doc 370
     371
     372

     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
<doc 385
     386
     387
     388

     389
     390
     391
     392
/*
 *   Copyright (c) 2007, Michael Lehn
 *
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions
 *   are met:
 *
 *   1) Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2) Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in
 *      the documentation and/or other materials provided with the
 *      distribution.
 *   3) Neither the name of the FLENS development group nor the names of
 *      its contributors may be used to endorse or promote products derived
 *      from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef FLENS_MATRIXTYPES_TRIANGULAR_IMPL_TRMATRIX_H
#define FLENS_MATRIXTYPES_TRIANGULAR_IMPL_TRMATRIX_H 1

#include <flens/auxiliary/auxiliary.h>
#include <flens/matrixtypes/triangular/triangularmatrix.h>
#include <flens/matrixtypes/triangular/impl/tr/constelementclosure.h>
#include <flens/matrixtypes/triangular/impl/tr/elementclosure.h>
#include <flens/scalartypes/impl/indexvariable.h>
#include <flens/typedefs.h>

namespace flens {

// forward declarations
template <typename A>
    class DenseVector;

template <typename FS>
    class GeMatrix;

template <typename FS>
    class HeMatrix;

template <typename FS>
    class SyMatrix;

template <typename FS>
class TrMatrix
    : public TriangularMatrix<TrMatrix<FS> >
{
    public:
        typedef FS                                  Engine;
        typedef typename Engine::ElementType        ElementType;
        typedef typename Engine::IndexType          IndexType;

        // view types from Engine
        typedef typename Engine::ConstView          EngineConstView;
        typedef typename Engine::View               EngineView;
        typedef typename Engine::NoView             EngineNoView;

        typedef typename Engine::ConstArrayView     ConstArrayView;
        typedef typename Engine::ArrayView          ArrayView;
        typedef typename Engine::Array              Array;

        // view types for
        typedef DenseVector<ConstArrayView>         ConstVectorView;
        typedef DenseVector<ArrayView>              VectorView;
        typedef DenseVector<Array>                  Vector;

        typedef GeMatrix<EngineConstView>           ConstGeneralView;
        typedef GeMatrix<EngineView>                GeneralView;
        typedef GeMatrix<EngineNoView>              GeneralNoView;

        typedef HeMatrix<EngineConstView>           ConstHermitianView;
        typedef HeMatrix<EngineView>                HermitianView;
        typedef HeMatrix<EngineNoView>              HermitianNoView;

        typedef SyMatrix<EngineConstView>           ConstSymmetricView;
        typedef SyMatrix<EngineView>                SymmetricView;
        typedef SyMatrix<EngineNoView>              SymmetricNoView;

        typedef TrMatrix<EngineConstView>           ConstView;
        typedef TrMatrix<EngineView>                View;
        typedef TrMatrix<EngineNoView>              NoView;

    private:
        typedef TrMatrix                            TR;

    public:
        typedef flens::IndexVariable<IndexType>     IndexVariable;
        typedef trmatrix::ConstElementClosure<TR>   ConstElementClosure;
        typedef trmatrix::ElementClosure<TR>        ElementClosure;

        // -- constructors -----------------------------------------------------

        TrMatrix(IndexType dim, StorageUpLo upLo, Diag diag = NonUnit);

        TrMatrix(IndexType numRows, IndexType numCols,
                 StorageUpLo upLo, Diag diag = NonUnit);

        TrMatrix(IndexType numRows, IndexType numCols,
                 IndexType firstRow, IndexType firstCol,
                 StorageUpLo upLo, Diag diag = NonUnit);

        TrMatrix(const Engine &engine, StorageUpLo upLo, Diag diag = NonUnit);

        TrMatrix(const TrMatrix &rhs);

        template <typename RHS>
            TrMatrix(const TrMatrix<RHS> &rhs);

        template <typename RHS>
            TrMatrix(TrMatrix<RHS> &rhs);

        template <typename RHS>
            TrMatrix(const Matrix<RHS> &rhs);

        // -- operators --------------------------------------------------------

        void
        operator=(const ElementType &value);

        TrMatrix &
        operator=(const TrMatrix &rhs);

        template <typename RHS>
            TrMatrix &
            operator=(const Matrix<RHS> &rhs);

        template <typename RHS>
            TrMatrix &
            operator+=(const Matrix<RHS> &rhs);

        template <typename RHS>
            TrMatrix &
            operator-=(const Matrix<RHS> &rhs);

        const ElementType &
        operator()(IndexType row, IndexType col) const;

        ElementType &
        operator()(IndexType row, IndexType col);

        template <typename S>
            const trmatrix::ConstElementClosure<TrMatrix,
                                                typename Scalar<S>::Impl>
            operator()(const Scalar<S> &row, const Scalar<S> &col) const;

        const ConstElementClosure
        operator()(const IndexVariable &row, const IndexVariable &col) const;

        ElementClosure
        operator()(IndexVariable &row, IndexVariable &col);

        // -- methods ----------------------------------------------------------
        IndexType
        numRows() const;

        IndexType
        numCols() const;

        IndexType
        dim() const;

        // for element access
        IndexType
        firstRow() const;

        IndexType
        lastRow() const;

        IndexType
        firstCol() const;

        IndexType
        lastCol() const;

        StorageUpLo
        upLo() const;

        StorageUpLo &
        upLo();

        Diag
        diag() const;

        Diag &
        diag();

        const ElementType *
        data() const;

        ElementType *
        data();

        IndexType
        leadingDimension() const;

        IndexType
        strideRow() const;

        IndexType
        strideCol() const;

        StorageOrder
        order() const;

        bool
        fill(const ElementType &value = ElementType(0));

        template <typename RHS>
            bool
            resize(const TrMatrix<RHS> &rhs,
                   const ElementType &value = ElementType());

        bool
        resize(IndexType numRows, IndexType numCols,
               IndexType firstRowIndex = Engine::defaultIndexBase,
               IndexType firstColIndex = Engine::defaultIndexBase,
               const ElementType &value = ElementType());

        // -- views ------------------------------------------------------------
        // diag views
        const ConstVectorView
        diag(IndexType d) const;

        VectorView
        diag(IndexType d);

        // general views
        const ConstGeneralView
        general() const;

        GeneralView
        general();

        // hermitian views
        const ConstHermitianView
        hermitian() const;

        HermitianView
        hermitian();

        // symmetric views
        const ConstSymmetricView
        symmetric() const;

        SymmetricView
        symmetric();

        // rectangular views
        const ConstGeneralView
        operator()(const Range<IndexType> &rows,
                   const Range<IndexType> &cols) const;

        GeneralView
        operator()(const Range<IndexType> &rows,
                   const Range<IndexType> &cols);

        // rectangular views (all rows selected)
        const ConstGeneralView
        operator()(const Underscore<IndexType> &,
                   const Range<IndexType> &cols) const;

        GeneralView
        operator()(const Underscore<IndexType> &,
                   const Range<IndexType> &cols);

        // rectangular views (all columns selected)
        const ConstGeneralView
        operator()(const Range<IndexType> &rows,
                   const Underscore<IndexType> &) const;

        GeneralView
        operator()(const Range<IndexType> &rows,
                   const Underscore<IndexType> &);

        // row view (vector view)
        const ConstVectorView
        operator()(IndexType row, const Underscore<IndexType> &) const;

        VectorView
        operator()(IndexType row, const Underscore<IndexType> &);

        const ConstVectorView
        operator()(IndexType row, const Range<IndexType> &cols) const;

        VectorView
        operator()(IndexType row, const Range<IndexType> &cols);

        // column view (vector view)
        const ConstVectorView
        operator()(const Underscore<IndexType> &, IndexType col) const;

        VectorView
        operator()(const Underscore<IndexType> &, IndexType col);

        const ConstVectorView
        operator()(const Range<IndexType> &rows, IndexType col) const;

        VectorView
        operator()(const Range<IndexType> &rows, IndexType col);

        // -- implementation ---------------------------------------------------
        const Engine &
        engine() const;

        Engine &
        engine();

    private:
        Engine       engine_;
        StorageUpLo  upLo_;
        Diag         diag_;
};

//-- Traits --------------------------------------------------------------------
//
//  IsTrMatrix
//
struct TrMatrixChecker_
{

    struct Two {
        char x;
        char y;
    };

    static Two
    check(AnyConversion_);

    template <typename Any>
        static char
        check(TrMatrix<Any>);
};

template <typename T>
struct IsTrMatrix
{
    static T var;
    static const bool value = sizeof(TrMatrixChecker_::check(var))==1;
};

//
//  IsRealTrMatrix
//
template <typename T>
struct IsRealTrMatrix
{
    typedef typename std::remove_reference<T>::type  TT;

    static const bool value = IsTrMatrix<TT>::value
                           && IsNotComplex<typename TT::ElementType>::value;
};

//
//  IsComplexTrMatrix
//
template <typename T>
struct IsComplexTrMatrix
{
    typedef typename std::remove_reference<T>::type  TT;

    static const bool value = IsTrMatrix<TT>::value
                           && IsComplex<typename TT::ElementType>::value;
};

//-- TrMatrix specific functions -----------------------------------------------

//
//  fillRandom
//

template <typename MA>
    typename RestrictTo<IsTrMatrix<MA>::value,
             bool>::Type
    fillRandom(MA &&A);

// namespace flens

#endif // FLENS_MATRIXTYPES_TRIANGULAR_IMPL_TRMATRIX_H