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
      58
      59
      60
<doc  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
     107
     108
     109
<doc 110
     111
<doc 112
     113
<doc 114
     115

     116
<doc 117
     118

     119
<doc 120
     121
<doc 122
     123
<doc 124
     125

     126
<doc 127
     128

     129
<doc 130
     131

     132
<doc 133
     134
     135

     136
<doc 137
     138
     139
     140

     141
     142
<doc 143
     144

     145
<doc 146
     147

     148
<doc 149
     150
     151

     152
<doc 153
     154
     155

     156
<doc 157
     158
     159

     160
<doc 161
     162

     163
<doc 164
     165

     166
<doc 167
     168

     169
<doc 170
     171

     172
<doc 173
     174

     175
<doc 176
     177

     178
<doc 179
     180
     181
     182

     183
<doc 184
     185

     186
<doc 187
     188

     189
     190
<doc 191
     192

     193
<doc 194
     195

     196
<doc 197
     198

     199
<doc 200
     201

     202
<doc 203
     204

     205
<doc 206
     207

     208
<doc 209
     210

     211
<doc 212
     213

     214
<doc 215
     216

     217
<doc 218
     219

     220
<doc 221
     222

     223
<doc 224
     225

     226
<doc 227
     228
     229
     230

     231
<doc 232
     233
     234
     235
     236

     237
<doc 238
     239

     240
<doc 241
     242

     243
     244
     245
<doc 246
     247

     248
<doc 249
     250

     251
     252
<doc 253
     254

     255
<doc 256
     257

     258
     259
<doc 260
     261

     262
<doc 263
     264

     265
     266
<doc 267
     268

     269
<doc 270
     271

     272
     273
<doc 274
     275

     276
<doc 277
     278

     279
<doc 280
     281

     282
<doc 283
     284

     285
<doc 286
     287

     288
<doc 289
     290

     291
<doc 292
     293

     294
<doc 295
     296

     297
<doc 298
     299

     300
<doc 301
     302

     303
<doc 304
     305

     306
<doc 307
     308

     309
     310
<doc 311
     312
     313

     314
<doc 315
     316
     317

     318
<doc 319
     320
     321

     322
<doc 323
     324
     325

     326
     327
<doc 328
     329
     330

     331
<doc 332
     333
     334

     335
     336
<doc 337
     338
     339

     340
<doc 341
     342
     343

     344
     345
<doc 346
     347

     348
<doc 349
     350

     351
<doc 352
     353

     354
<doc 355
     356

     357
     358
<doc 359
     360

     361
<doc 362
     363

     364
<doc 365
     366

     367
<doc 368
     369

     370
     371
<doc 372
     373

     374
<doc 375
     376

     377
     378
     379
     380
     381
     382
     383
     384
     385
<doc 386
     387

     388
<doc 389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
<doc 402
     403
     404

     405
     406
     407
     408
     409
     410
     411
<doc 412
     413
     414

     415
     416
     417
     418
     419
     420
     421
     422
     423
<doc 424
     425
     426

     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
<doc 438
     439
     440

     441
<doc 442
     443
     444
     445

     446
     447
     448
     449
<doc 450
     451
     452

     453
<doc 454
     455
     456
     457

     458
     459
     460
     461
     462
<doc 463
     464
     465
     466

     467
     468
     469
     470
/*
 *   Copyright (c) 2007-2012, 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_GENERAL_IMPL_GEMATRIX_H
#define FLENS_MATRIXTYPES_GENERAL_IMPL_GEMATRIX_H 1

#include <flens/auxiliary/auxiliary.h>
#include <flens/matrixtypes/general/generalmatrix.h>
#include <flens/matrixtypes/general/impl/ge/constelementclosure.h>
#include <flens/matrixtypes/general/impl/ge/elementclosure.h>
#include <flens/matrixtypes/general/impl/ge/initializer.h>
#include <flens/matrixtypes/general/impl/imagmatrixclosure.h>
#include <flens/matrixtypes/general/impl/realmatrixclosure.h>
#include <flens/scalartypes/impl/indexvariable.h>
#include <flens/typedefs.h>

namespace flens {

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

template <typename FS>
    class HeMatrix;

template <typename FS>
    class SyMatrix;

template <typename FS>
    class TrMatrix;

template <typename FS>
class GeMatrix
    : public GeneralMatrix<GeMatrix<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
        typedef DenseVector<ConstArrayView>             ConstVectorView;
        typedef DenseVector<ArrayView>                  VectorView;
        typedef DenseVector<Array>                      Vector;

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

        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>               ConstTriangularView;
        typedef TrMatrix<EngineView>                    TriangularView;
        typedef TrMatrix<EngineNoView>                  TriangularNoView;

    private:
        typedef GeMatrix                                GE;

    public:
        typedef flens::IndexVariable<IndexType>         IndexVariable;
        typedef gematrix::ConstElementClosure<GE>       ConstElementClosure;
        typedef gematrix::ElementClosure<GE>            ElementClosure;
        typedef gematrix::Initializer<GE>               Initializer;

        // -- constructors -----------------------------------------------------
        GeMatrix();

        GeMatrix(IndexType numRows, IndexType numCols);

        GeMatrix(IndexType numRows, IndexType numCols,
                 IndexType firstRow, IndexType firstCol);

        GeMatrix(const Range<IndexType> &rowRange,
                 const Range<IndexType> &colRange);

        GeMatrix(const Engine &engine);

        GeMatrix(const GeMatrix &rhs);

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

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

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

        template <typename VECTOR>
            GeMatrix(IndexType numRows, IndexType numCols,
                     VECTOR &&rhs);

        template <typename VECTOR>
            GeMatrix(IndexType numRows, IndexType numCols,
                     VECTOR &&rhs,
                     IndexType leadingDimension);

        // -- operators --------------------------------------------------------
        Initializer
        operator=(const ElementType &value);

        GeMatrix &
        operator=(const GeMatrix &rhs);

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

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

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

        GeMatrix &
        operator+=(const ElementType &alpha);

        GeMatrix &
        operator-=(const ElementType &alpha);

        GeMatrix &
        operator*=(const ElementType &alpha);

        GeMatrix &
        operator/=(const ElementType &alpha);

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

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

        template <typename S>
            const gematrix::ConstElementClosure<GeMatrix,
                                                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
        firstRow() const;

        IndexType
        lastRow() const;

        IndexType
        firstCol() const;

        IndexType
        lastCol() const;

        Range<IndexType>
        rows() const;

        Range<IndexType>
        cols() const;

        const ElementType *
        data() const;

        ElementType *
        data();

        IndexType
        leadingDimension() const;

        StorageOrder
        order() const;

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

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

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

        void
        changeIndexBase(IndexType firstRowIndex, IndexType firstColIndex);

        // -- views ------------------------------------------------------------
        // vectorize matrix
        const ConstVectorView
        vectorView() const;

        VectorView
        vectorView();

        // vectorize matrix and select range
        const ConstVectorView
        vectorView(IndexType from, IndexType to) const;

        VectorView
        vectorView(IndexType from, IndexType to);

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

        VectorView
        diag(IndexType d);

        // anti diag views
        const ConstVectorView
        antiDiag(IndexType d) const;

        VectorView
        antiDiag(IndexType d);

        // triangular views
        const ConstTriangularView
        upper() const;

        TriangularView
        upper();

        const ConstTriangularView
        upperUnit() const;

        TriangularView
        upperUnit();

        const ConstTriangularView
        strictUpper() const;

        TriangularView
        strictUpper();

        const ConstTriangularView
        lower() const;

        TriangularView
        lower();

        const ConstTriangularView
        lowerUnit() const;

        TriangularView
        lowerUnit();

        const ConstTriangularView
        strictLower() const;

        TriangularView
        strictLower();

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

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

        template <typename RHS>
            const ConstView
            operator()(const GeMatrix<RHS> &A) const;

        template <typename RHS>
            View
            operator()(const GeMatrix<RHS> &A);

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

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

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

        View
        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;
};

//-- Traits --------------------------------------------------------------------
//
//  IsGeMatrix
//
struct _GeMatrixChecker
{

    struct Two {
        char x;
        char y;
    };

    static Two
    check(_AnyConversion);

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

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

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

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

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

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

//-- GeMatrix specific functions -----------------------------------------------

//
//  imag
//
template <typename MZ>
    ImagConstMatrixClosure<GeMatrix<MZ> >
    imag(const GeMatrix<MZ> &Z);

template <typename MZ>
    typename RestrictTo<IsGeMatrix<MZ>::value,
             ImagMatrixClosure<MZ> >::Type
    imag(MZ &&Z);

//
//  real
//
template <typename MZ>
    RealConstMatrixClosure<GeMatrix<MZ> >
    real(const GeMatrix<MZ> &Z);

template <typename MZ>
    typename RestrictTo<IsGeMatrix<MZ>::value,
             RealMatrixClosure<MZ> >::Type
    real(MZ &&Z);

//
//  fillRandom
//

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

// namespace flens

#endif // FLENS_MATRIXTYPES_GENERAL_IMPL_GEMATRIX_H