MatrixI.H 13 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
5
    \\  /    A nd           | Copyright (C) 2019 OpenCFD Ltd.
6
     \\/     M anipulation  |
OpenFOAM bot's avatar
OpenFOAM bot committed
7 8
-------------------------------------------------------------------------------
                            | Copyright (C) 2011-2016 OpenFOAM Foundation
9 10 11 12
-------------------------------------------------------------------------------
License
    This file is part of OpenFOAM.

13 14 15 16
    OpenFOAM is free software: you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
17 18 19 20 21 22 23

    OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License
24
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
25 26 27

\*---------------------------------------------------------------------------*/

28 29
#include "MatrixBlock.H"

30 31 32 33 34 35 36 37 38 39 40 41 42 43
// * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //

template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::doAlloc()
{
    const label len = size();

    if (len)
    {
        v_ = new Type[len];
    }
}


44 45
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

46 47
template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix()
48
:
49
    mRows_(0),
50
    nCols_(0),
51
    v_(nullptr)
52 53 54
{}


55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix(const labelPair& dims)
:
    Matrix<Form, Type>(dims.first(), dims.second())
{}


template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix(const labelPair& dims, const zero)
:
    Matrix<Form, Type>(dims.first(), dims.second(), Zero)
{}


template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix(const labelPair& dims, const Type& val)
:
    Matrix<Form, Type>(dims.first(), dims.second(), val)
{}


76
template<class Form, class Type>
77 78
inline Foam::autoPtr<Foam::Matrix<Form, Type>>
Foam::Matrix<Form, Type>::clone() const
79
{
80
    return autoPtr<Matrix<Form, Type>>::New(*this);
81 82 83 84 85
}


// * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //

86 87 88
template<class Form, class Type>
inline const Foam::Matrix<Form, Type>& Foam::Matrix<Form, Type>::null()
{
89
    return NullObjectRef<Matrix<Form, Type>>();
90 91 92
}


93
template<class Form, class Type>
94
inline Foam::label Foam::Matrix<Form, Type>::m() const noexcept
95
{
96
    return mRows_;
97 98 99
}


100
template<class Form, class Type>
101
inline Foam::label Foam::Matrix<Form, Type>::n() const noexcept
102
{
103
    return nCols_;
104 105 106
}


107 108
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::size() const
109
{
110 111 112 113
    return mRows_ * nCols_;
}


114 115 116 117 118 119 120
template<class Form, class Type>
inline Foam::labelPair Foam::Matrix<Form, Type>::sizes() const
{
    return labelPair(mRows_, nCols_);
}


121 122 123 124
template<class Form, class Type>
inline bool Foam::Matrix<Form, Type>::empty() const noexcept
{
    return !mRows_ || !nCols_;
125 126 127
}


128 129
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checki(const label i) const
130
{
131
    if (!mRows_ || !nCols_)
132
    {
133
        FatalErrorInFunction
134
            << "Attempt to access element from empty matrix"
135 136
            << abort(FatalError);
    }
137
    if (i < 0 || mRows_ <= i)
138
    {
139
        FatalErrorInFunction
140
            << "Index " << i << " out of range 0 ... " << mRows_-1
141 142 143 144 145
            << abort(FatalError);
    }
}


146 147
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checkj(const label j) const
148
{
149
    if (!mRows_ || !nCols_)
150
    {
151
        FatalErrorInFunction
152
            << "Attempt to access element from empty matrix"
153 154
            << abort(FatalError);
    }
155
    if (j < 0 || nCols_ <= j)
156
    {
157
        FatalErrorInFunction
158
            << "index " << j << " out of range 0 ... " << nCols_-1
159 160 161 162 163
            << abort(FatalError);
    }
}


164
template<class Form, class Type>
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
inline void Foam::Matrix<Form, Type>::checkSize() const
{
    if (mRows_ < 0 || nCols_ < 0)
    {
        FatalErrorInFunction
            << "Incorrect size (" << mRows_ << ", " << nCols_ << ')' << nl
            << abort(FatalError);
    }
    // Could also check for odd sizes, like (0, N) and make (0,0)
}


template<class Form, class Type>
inline bool Foam::Matrix<Form, Type>::uniform() const
{
    const label len = size();

    if (len == 0)
    {
        return false;
    }

187
    for (label idx = 1; idx < len; ++idx)
188 189 190 191 192 193 194 195 196 197 198 199 200
    {
        if (v_[0] != v_[idx])
        {
            return false;
        }
    }

    return true;
}


template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::cdata() const noexcept
201 202 203 204 205 206
{
    return v_;
}


template<class Form, class Type>
207
inline Type* Foam::Matrix<Form, Type>::data() noexcept
208 209 210 211 212
{
    return v_;
}


213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::rowData(const label irow) const
{
    #ifdef FULLDEBUG
    checki(irow);
    #endif
    return (v_ + irow*nCols_);
}


template<class Form, class Type>
inline Type* Foam::Matrix<Form, Type>::rowData(const label irow)
{
    #ifdef FULLDEBUG
    checki(irow);
    #endif
    return (v_ + irow*nCols_);
}


template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::at(const label idx) const
{
    #ifdef FULLDEBUG
237
    if (idx < 0 || this->size() <= idx)
238 239 240 241 242 243 244 245 246 247 248 249 250 251
    {
        FatalErrorInFunction
            << "index " << idx << " out of range 0 ... " << this->size()
            << abort(FatalError);
    }
    #endif
    return (v_ + idx);
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::at(const label idx)
{
    #ifdef FULLDEBUG
252
    if (idx < 0 || this->size() <= idx)
253 254 255 256 257 258 259 260 261 262
    {
        FatalErrorInFunction
            << "index " << idx << " out of range 0 ... " << this->size()
            << abort(FatalError);
    }
    #endif
    return (v_ + idx);
}


263
template<class Form, class Type>
264
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
265
Foam::Matrix<Form, Type>::subColumn
266
(
267 268 269
    const label colIndex,
    const label rowIndex,
    label len
270 271
) const
{
272 273 274 275 276
    if (len < 0)
    {
        len = mRows_ - rowIndex;
    }

277
    return ConstMatrixBlock<mType>
278 279
    (
        *this,
280 281 282 283
        len, // rows
        1,
        rowIndex,
        colIndex
284 285 286 287 288
    );
}


template<class Form, class Type>
289
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
290
Foam::Matrix<Form, Type>::subRow
291
(
292 293 294
    const label rowIndex,
    const label colIndex,
    label len
295 296
) const
{
297 298 299 300 301
    if (len < 0)
    {
        len = nCols_ - colIndex;
    }

302
    return ConstMatrixBlock<mType>
303 304
    (
        *this,
305 306 307 308
        1,
        len, // columns
        rowIndex,
        colIndex
309 310 311 312 313
    );
}


template<class Form, class Type>
314
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
315
Foam::Matrix<Form, Type>::subMatrix
316
(
317 318 319 320
    const label rowIndex,
    const label colIndex,
    label szRows,
    label szCols
321 322
) const
{
323 324 325
    if (szRows < 0) szRows = mRows_ - rowIndex;
    if (szCols < 0) szCols = nCols_ - colIndex;

326
    return ConstMatrixBlock<mType>
327 328
    (
        *this,
329 330 331 332
        szRows,
        szCols,
        rowIndex,
        colIndex
333 334 335 336 337
    );
}


template<class Form, class Type>
338
template<class VectorSpace>
339
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
340
Foam::Matrix<Form, Type>::block
341
(
342 343
    const label rowIndex,
    const label colIndex
344 345
) const
{
346
    return ConstMatrixBlock<mType>
347 348
    (
        *this,
349 350 351 352
        VectorSpace::mRows,
        VectorSpace::nCols,
        rowIndex,
        colIndex
353 354 355 356 357
    );
}


template<class Form, class Type>
358
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
359
Foam::Matrix<Form, Type>::subColumn
360
(
361 362 363
    const label colIndex,
    const label rowIndex,
    label len
364 365
)
{
366 367 368 369 370
    if (len < 0)
    {
        len = mRows_ - rowIndex;
    }

371
    return MatrixBlock<mType>
372 373
    (
        *this,
374 375 376 377
        len, // rows
        1,
        rowIndex,
        colIndex
378 379 380 381 382
    );
}


template<class Form, class Type>
383
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
384 385 386 387 388 389
Foam::Matrix<Form, Type>::subRow
(
    const label rowIndex,
    const label colIndex,
    label len
)
390
{
391 392 393 394 395
    if (len < 0)
    {
        len = nCols_ - colIndex;
    }

396
    return MatrixBlock<mType>
397 398
    (
        *this,
399 400 401 402
        1,
        len, // columns
        rowIndex,
        colIndex
403 404 405 406 407
    );
}


template<class Form, class Type>
408
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
409 410 411 412 413 414 415
Foam::Matrix<Form, Type>::subMatrix
(
    const label rowIndex,
    const label colIndex,
    label szRows,
    label szCols
)
416
{
417 418 419
    if (szRows < 0) szRows = mRows_ - rowIndex;
    if (szCols < 0) szCols = nCols_ - colIndex;

420
    return MatrixBlock<mType>
421 422
    (
        *this,
423 424 425 426
        szRows,
        szCols,
        rowIndex,
        colIndex
427 428 429 430 431
    );
}


template<class Form, class Type>
432
template<class VectorSpace>
433
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
434
Foam::Matrix<Form, Type>::block
435
(
436 437
    const label rowIndex,
    const label colIndex
438 439
)
{
440
    return MatrixBlock<mType>
441 442
    (
        *this,
443 444 445 446
        VectorSpace::mRows,
        VectorSpace::nCols,
        rowIndex,
        colIndex
447 448 449 450
    );
}


451 452 453 454 455 456 457
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::setSize(const label m, const label n)
{
    resize(m, n);
}


458 459 460 461 462 463 464 465
template<class Form, class Type>
void Foam::Matrix<Form, Type>::shallowResize(const label m, const label n)
{
    mRows_ = m;
    nCols_ = n;
}


466
template<class Form, class Type>
467
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
468
(
469
    const UList<Type>& x
470 471
) const
{
472
    return this->AmulImpl(x);
473 474 475 476 477
}


template<class Form, class Type>
template<class Addr>
478
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
479
(
480
    const IndirectListBase<Type, Addr>& x
481 482
) const
{
483
    return this->AmulImpl(x);
484 485 486 487
}


template<class Form, class Type>
488
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
489
(
490
    const UList<Type>& x
491 492
) const
{
493
    return this->TmulImpl(x);
494 495 496 497 498
}


template<class Form, class Type>
template<class Addr>
499
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
500
(
501
    const IndirectListBase<Type, Addr>& x
502 503
) const
{
504
    return this->TmulImpl(x);
505 506 507
}


508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
// * * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * //

template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::iterator
Foam::Matrix<Form, Type>::begin()
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::iterator
Foam::Matrix<Form, Type>::end()
{
    return v_ + (mRows_ * nCols_);
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::cbegin() const
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::cend() const
{
    return v_ + (mRows_ * nCols_);
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::begin() const
{
    return v_;
}


template<class Form, class Type>
inline typename Foam::Matrix<Form, Type>::const_iterator
Foam::Matrix<Form, Type>::end() const
{
    return v_ + (mRows_ * nCols_);
}


558 559
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

560 561 562
template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::operator()
(
563 564
    const label irow,
    const label jcol
565
) const
566
{
567
    #ifdef FULLDEBUG
568 569
    checki(irow);
    checkj(jcol);
570
    #endif
571
    return v_[irow*nCols_ + jcol];
572 573 574 575 576 577
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::operator()
(
578 579
    const label irow,
    const label jcol
580 581
)
{
582
    #ifdef FULLDEBUG
583 584
    checki(irow);
    checkj(jcol);
585
    #endif
586
    return v_[irow*nCols_ + jcol];
587 588 589
}


590
template<class Form, class Type>
591
inline const Type* Foam::Matrix<Form, Type>::operator[](const label irow) const
592
{
593
    #ifdef FULLDEBUG
594
    checki(irow);
595
    #endif
596
    return v_ + irow*nCols_;
597 598 599 600
}


template<class Form, class Type>
601
inline Type* Foam::Matrix<Form, Type>::operator[](const label irow)
602
{
603
    #ifdef FULLDEBUG
604
    checki(irow);
605
    #endif
606
    return v_ + irow*nCols_;
607 608 609
}


610 611 612 613 614 615 616
template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const Matrix<Form, Type>& mat,
    const UList<Type>& x
)
{
617
    return mat.Amul(x);
618 619 620 621 622 623 624 625 626 627
}


template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const Matrix<Form, Type>& mat,
    const IndirectListBase<Type, Addr>& x
)
{
628
    return mat.Amul(x);
629 630 631 632 633 634 635 636 637 638
}


template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const UList<Type>& x,
    const Matrix<Form, Type>& mat
)
{
639
    return mat.Tmul(x);
640 641 642 643 644 645 646 647 648 649
}


template<class Form, class Type, class Addr>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const IndirectListBase<Type, Addr>& x,
    const Matrix<Form, Type>& mat
)
{
650
    return mat.Tmul(x);
651 652 653
}


654
// ************************************************************************* //