MatrixI.H 6.79 KB
Newer Older
1 2 3 4
/*---------------------------------------------------------------------------*\
  =========                 |
  \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
   \\    /   O peration     |
OpenFOAM bot's avatar
OpenFOAM bot committed
5
    \\  /    A nd           |
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
// * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //

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


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


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

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


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


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


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


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


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


119
template<class Form, class Type>
120 121 122 123 124 125 126 127 128 129 130 131 132
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_;
}


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


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


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


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


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


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


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


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


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


293 294
// * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //

295 296 297 298 299 300
template<class Form, class Type>
inline const Type& Foam::Matrix<Form, Type>::operator()
(
    const label i,
    const label j
) const
301 302
{
    checki(i);
303
    checkj(j);
304 305 306 307 308 309 310 311 312 313 314 315
    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);
316
    checkj(j);
317
    return v_[i*nCols_ + j];
318 319 320
}


321 322
template<class Form, class Type>
inline const Type* Foam::Matrix<Form, Type>::operator[](const label i) const
323 324
{
    checki(i);
325 326 327 328 329 330 331 332 333
    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_;
334 335 336 337
}


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