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

11 12 13 14
    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.
15 16 17 18 19 20 21

    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
22
    along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
23 24 25

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

26 27
#include "MatrixBlock.H"

28 29
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

30 31
template<class Form, class Type>
inline Foam::Matrix<Form, Type>::Matrix()
32
:
33
    mRows_(0),
34
    nCols_(0),
35
    v_(nullptr)
36 37 38
{}


39
template<class Form, class Type>
40
inline Foam::autoPtr<Foam::Matrix<Form, Type>> Foam::Matrix<Form, Type>::
41
clone() const
42
{
43
    return autoPtr<Matrix<Form, Type>>::New(*this);
44 45 46 47 48
}


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

49 50 51
template<class Form, class Type>
inline const Foam::Matrix<Form, Type>& Foam::Matrix<Form, Type>::null()
{
52
    return NullObjectRef<Matrix<Form, Type>>();
53 54 55
}


56
template<class Form, class Type>
57
inline Foam::label Foam::Matrix<Form, Type>::m() const
58
{
59
    return mRows_;
60 61 62
}


63
template<class Form, class Type>
64
inline Foam::label Foam::Matrix<Form, Type>::n() const
65
{
66
    return nCols_;
67 68 69
}


70 71
template<class Form, class Type>
inline Foam::label Foam::Matrix<Form, Type>::size() const
72
{
73
    return mRows_*nCols_;
74 75 76
}


77 78
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checki(const label i) const
79
{
80
    #ifdef FULLDEBUG
81
    if (!mRows_ || !nCols_)
82
    {
83
        FatalErrorInFunction
84
            << "Attempt to access element from empty matrix"
85 86
            << abort(FatalError);
    }
87
    else if (i<0 || i>=mRows_)
88
    {
89
        FatalErrorInFunction
90
            << "Index " << i << " out of range 0 ... " << mRows_-1
91 92
            << abort(FatalError);
    }
93
    #endif
94 95 96
}


97 98
template<class Form, class Type>
inline void Foam::Matrix<Form, Type>::checkj(const label j) const
99
{
100
    #ifdef FULLDEBUG
101
    if (!mRows_ || !nCols_)
102
    {
103
        FatalErrorInFunction
104
            << "Attempt to access element from empty matrix"
105 106
            << abort(FatalError);
    }
107
    else if (j<0 || j>=nCols_)
108
    {
109
        FatalErrorInFunction
110
            << "index " << j << " out of range 0 ... " << nCols_-1
111 112
            << abort(FatalError);
    }
113
    #endif
114 115 116
}


117
template<class Form, class Type>
118 119 120 121 122 123 124 125 126 127 128 129 130
inline const Type* Foam::Matrix<Form, Type>::v() const
{
    return v_;
}


template<class Form, class Type>
inline Type* Foam::Matrix<Form, Type>::v()
{
    return v_;
}


131
template<class Form, class Type>
132
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
133 134 135 136 137 138 139 140
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
) const
{
141
    return ConstMatrixBlock<mType>
142 143 144 145 146 147 148 149 150 151 152 153
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
154
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
155 156 157 158 159 160
Foam::Matrix<Form, Type>::block
(
    const label mStart,
    const label nStart
) const
{
161
    return ConstMatrixBlock<mType>
162 163
    (
        *this,
164
        VectorSpace::mRows,
165 166 167 168 169 170 171 172
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
173
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
174 175 176 177 178 179
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart
) const
{
180
    return ConstMatrixBlock<mType>
181 182 183 184 185 186 187 188 189 190 191
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
192
inline Foam::ConstMatrixBlock<Foam::Matrix<Form, Type>>
193 194 195 196 197 198 199
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
) const
{
200
    return ConstMatrixBlock<mType>
201 202 203 204 205 206 207 208 209 210 211
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


template<class Form, class Type>
212
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
213 214 215 216 217 218 219 220
Foam::Matrix<Form, Type>::block
(
    const label m,
    const label n,
    const label mStart,
    const label nStart
)
{
221
    return MatrixBlock<mType>
222 223 224 225 226 227 228 229 230 231 232 233
    (
        *this,
        m,
        n,
        mStart,
        nStart
    );
}


template<class Form, class Type>
template<class VectorSpace>
234
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
235 236
Foam::Matrix<Form, Type>::block(const label mStart, const label nStart)
{
237
    return MatrixBlock<mType>
238 239
    (
        *this,
240
        VectorSpace::mRows,
241 242 243 244 245 246 247 248
        VectorSpace::nCols,
        mStart,
        nStart
    );
}


template<class Form, class Type>
249
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
250 251
Foam::Matrix<Form, Type>::col(const label m, const label mStart)
{
252
    return MatrixBlock<mType>
253 254 255 256 257 258 259 260 261 262 263
    (
        *this,
        m,
        1,
        mStart,
        0
    );
}


template<class Form, class Type>
264
inline Foam::MatrixBlock<Foam::Matrix<Form, Type>>
265 266 267 268 269 270 271
Foam::Matrix<Form, Type>::col
(
    const label m,
    const label mStart,
    const label nStart
)
{
272
    return MatrixBlock<mType>
273 274 275 276 277 278 279 280 281 282
    (
        *this,
        m,
        1,
        mStart,
        nStart
    );
}


283 284 285 286 287 288 289 290
template<class Form, class Type>
void Foam::Matrix<Form, Type>::shallowResize(const label m, const label n)
{
    mRows_ = m;
    nCols_ = n;
}


291 292
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

293 294 295 296 297 298
template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::operator()
(
    const label i,
    const label j
) const
299 300
{
    checki(i);
301
    checkj(j);
302 303 304 305 306 307 308 309 310 311 312 313
    return v_[i*nCols_ + j];
}


template<class Form, class Type>
inline Type& Foam::Matrix<Form, Type>::operator()
(
    const label i,
    const label j
)
{
    checki(i);
314
    checkj(j);
315
    return v_[i*nCols_ + j];
316 317 318
}


319 320
template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::operator[](const label i) const
321 322
{
    checki(i);
323 324 325 326 327 328 329 330 331
    return v_ + i*nCols_;
}


template<class Form, class Type>
inline Type* Foam::Matrix<Form, Type>::operator[](const label i)
{
    checki(i);
    return v_ + i*nCols_;
332 333 334 335
}


// ************************************************************************* //