MatrixI.H 12.2 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
    #ifdef FULLDEBUG
132
    if (!mRows_ || !nCols_)
133
    {
134
        FatalErrorInFunction
135
            << "Attempt to access element from empty matrix"
136 137
            << abort(FatalError);
    }
138
    if (i < 0 || i >= mRows_)
139
    {
140
        FatalErrorInFunction
141
            << "Index " << i << " out of range 0 ... " << mRows_-1
142 143
            << abort(FatalError);
    }
144
    #endif
145 146 147
}


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


168
template<class Form, class Type>
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
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;
    }

191
    for (label idx = 1; idx < len; ++idx)
192 193 194 195 196 197 198 199 200 201 202 203 204
    {
        if (v_[0] != v_[idx])
        {
            return false;
        }
    }

    return true;
}


template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::cdata() const noexcept
205 206 207 208 209 210
{
    return v_;
}


template<class Form, class Type>
211
inline Type* Foam::Matrix<Form, Type>::data() noexcept
212 213 214 215 216
{
    return v_;
}


217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
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
    if (idx < 0 || idx >= this->size())
    {
        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
    if (idx < 0 || idx >= this->size())
    {
        FatalErrorInFunction
            << "index " << idx << " out of range 0 ... " << this->size()
            << abort(FatalError);
    }
    #endif
    return (v_ + idx);
}


267
template<class Form, class Type>
268
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
269 270 271 272 273 274 275 276
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
) const
{
277
    return ConstMatrixBlock<mType>
278 279 280 281 282 283 284 285 286 287 288 289
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
290
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
291 292 293 294 295 296
Foam::Matrix<Form, Type>::block
(
    const label mStart,
    const label nStart
) const
{
297
    return ConstMatrixBlock<mType>
298 299
    (
        *this,
300
        VectorSpace::mRows,
301 302 303 304 305 306 307 308
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
309
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
310 311 312 313 314 315
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart
) const
{
316
    return ConstMatrixBlock<mType>
317 318 319 320 321 322 323 324 325 326 327
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
328
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
329 330 331 332 333 334 335
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
) const
{
336
    return ConstMatrixBlock<mType>
337 338 339 340 341 342 343 344 345 346 347
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


template<class Form, class Type>
348
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
349 350 351 352 353 354 355 356
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
)
{
357
    return MatrixBlock<mType>
358 359 360 361 362 363 364 365 366 367 368 369
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
370
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
371 372
Foam::Matrix<Form, Type>::block(const label mStart, const label nStart)
{
373
    return MatrixBlock<mType>
374 375
    (
        *this,
376
        VectorSpace::mRows,
377 378 379 380 381 382 383 384
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
385
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
386 387
Foam::Matrix<Form, Type>::col(const label m, const label mStart)
{
388
    return MatrixBlock<mType>
389 390 391 392 393 394 395 396 397 398 399
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
400
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
401 402 403 404 405 406 407
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
)
{
408
    return MatrixBlock<mType>
409 410 411 412 413 414 415 416 417 418
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


419 420 421 422 423 424 425
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::setSize(const label m, const label n)
{
    resize(m, n);
}


426 427 428 429 430 431 432 433
template<class Form, class Type>
void Foam::Matrix<Form, Type>::shallowResize(const label m, const label n)
{
    mRows_ = m;
    nCols_ = n;
}


434
template<class Form, class Type>
435
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
436
(
437
    const UList<Type>& x
438 439
) const
{
440
    return this->AmulImpl(x);
441 442 443 444 445
}


template<class Form, class Type>
template<class Addr>
446
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Amul
447
(
448
    const IndirectListBase<Type, Addr>& x
449 450
) const
{
451
    return this->AmulImpl(x);
452 453 454 455
}


template<class Form, class Type>
456
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
457
(
458
    const UList<Type>& x
459 460
) const
{
461
    return this->TmulImpl(x);
462 463 464 465 466
}


template<class Form, class Type>
template<class Addr>
467
inline Foam::tmp<Foam::Field<Type>> Foam::Matrix<Form, Type>::Tmul
468
(
469
    const IndirectListBase<Type, Addr>& x
470 471
) const
{
472
    return this->TmulImpl(x);
473 474 475
}


476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
// * * * * * * * * * * * * * * * * * 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_);
}


526 527
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

528 529 530
template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::operator()
(
531 532
    const label irow,
    const label jcol
533
) const
534
{
535 536 537
    checki(irow);
    checkj(jcol);
    return v_[irow*nCols_ + jcol];
538 539 540 541 542 543
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::operator()
(
544 545
    const label irow,
    const label jcol
546 547
)
{
548 549 550
    checki(irow);
    checkj(jcol);
    return v_[irow*nCols_ + jcol];
551 552 553
}


554
template<class Form, class Type>
555
inline const Type* Foam::Matrix<Form, Type>::operator[](const label irow) const
556
{
557 558
    checki(irow);
    return v_ + irow*nCols_;
559 560 561 562
}


template<class Form, class Type>
563
inline Type* Foam::Matrix<Form, Type>::operator[](const label irow)
564
{
565 566
    checki(irow);
    return v_ + irow*nCols_;
567 568 569
}


570 571 572 573 574 575 576
template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const Matrix<Form, Type>& mat,
    const UList<Type>& x
)
{
577
    return mat.Amul(x);
578 579 580 581 582 583 584 585 586 587
}


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
)
{
588
    return mat.Amul(x);
589 590 591 592 593 594 595 596 597 598
}


template<class Form, class Type>
inline Foam::tmp<Foam::Field<Type>> Foam::operator*
(
    const UList<Type>& x,
    const Matrix<Form, Type>& mat
)
{
599
    return mat.Tmul(x);
600 601 602 603 604 605 606 607 608 609
}


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
)
{
610
    return mat.Tmul(x);
611 612 613
}


614
// ************************************************************************* //