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
<doc 136
     137
     138
     139

     140
<doc 141
     142
     143
     144
     145
     146

     147
     148
<doc 149
     150

     151
<doc 152
     153

     154
<doc 155
     156
     157

     158
<doc 159
     160
     161

     162
<doc 163
     164
     165

     166
<doc 167
     168

     169
<doc 170
     171

     172
<doc 173
     174

     175
<doc 176
     177

     178
<doc 179
     180

     181
<doc 182
     183

     184
<doc 185
     186
     187
     188

     189
<doc 190
     191

     192
<doc 193
     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
<doc 230
     231

     232
<doc 233
     234

     235
<doc 236
     237

     238
<doc 239
     240
     241
     242

     243
<doc 244
     245
     246
     247
     248

     249
<doc 250
     251

     252
<doc 253
     254

     255
     256
     257
<doc 258
     259

     260
<doc 261
     262

     263
     264
<doc 265
     266

     267
<doc 268
     269

     270
     271
<doc 272
     273

     274
<doc 275
     276

     277
     278
<doc 279
     280

     281
<doc 282
     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
<doc 310
     311

     312
<doc 313
     314

     315
<doc 316
     317

     318
<doc 319
     320

     321
     322
<doc 323
     324
     325

     326
<doc 327
     328
     329

     330
<doc 331
     332
     333

     334
<doc 335
     336
     337

     338
     339
<doc 340
     341
     342

     343
<doc 344
     345
     346

     347
     348
<doc 349
     350
     351

     352
<doc 353
     354
     355

     356
     357
<doc 358
     359

     360
<doc 361
     362

     363
<doc 364
     365

     366
<doc 367
     368

     369
     370
<doc 371
     372

     373
<doc 374
     375

     376
<doc 377
     378

     379
<doc 380
     381

     382
     383
<doc 384
     385

     386
<doc 387
     388

     389
     390
     391
     392
     393
     394
     395
     396
     397
<doc 398
     399

     400
<doc 401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
<doc 414
     415
     416

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

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

     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
<doc 450
     451
     452

     453
<doc 454
     455
     456
     457

     458
     459
     460
     461
<doc 462
     463
     464

     465
<doc 466
     467
     468
     469

     470
     471
     472
     473
     474
<doc 475
     476
     477
     478

     479
     480
     481
     482
/*
 *   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);

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

        // -- 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;

        IndexType
        strideRow() const;

        IndexType
        strideCol() 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