MatrixSpaceI.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  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 #include <type_traits>
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
34 (
35  const Foam::zero
36 )
37 :
39 {}
40 
41 
42 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
43 template<class Form2, class Cmpt2>
45 (
47 )
48 :
50 {}
51 
52 
53 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
54 template
55 <
56  template<class, Foam::direction, Foam::direction> class Block2,
57  Foam::direction BRowStart,
58  Foam::direction BColStart
59 >
61 (
62  const Block2<Form, BRowStart, BColStart>& block
63 )
64 {
65  for (direction i=0; i<Mrows; ++i)
66  {
67  for (direction j=0; j<Ncols; ++j)
68  {
69  operator()(i, j) = block(i, j);
70  }
71  }
72 }
73 
74 
75 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
77 :
78  MatrixSpace::vsType(is)
79 {}
80 
81 
82 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
83 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
86 ConstBlock(const msType& matrix)
87 :
88  matrix_(matrix)
89 {
90  static_assert
91  (
92  msType::mRows >= BRowStart + mRows,
93  "Rows in block > rows in matrix"
94  );
95  static_assert
96  (
97  msType::nCols >= BColStart + nCols,
98  "Columns in block > columns in matrix"
99  );
100 }
101 
102 
103 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
104 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
107 Block(msType& matrix)
108 :
109  matrix_(matrix)
110 {
111  static_assert
112  (
113  msType::mRows >= BRowStart + mRows,
114  "Rows in block > rows in matrix"
115  );
116  static_assert
117  (
118  msType::nCols >= BColStart + nCols,
119  "Columns in block > columns in matrix"
120  );
121 }
122 
123 
124 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
125 
126 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
127 template<Foam::direction Row, Foam::direction Col>
129 {
130  static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
131  return this->v_[Row*Ncols + Col];
132 }
133 
134 
135 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
136 template<Foam::direction Row, Foam::direction Col>
138 {
139  static_assert(Row < Mrows && Col < Ncols, "Address outside matrix");
140  return this->v_[Row*Ncols + Col];
141 }
142 
143 
144 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
146 {
147  return elmt<0, 0>();
148 }
149 
150 
151 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
153 {
154  return elmt<0, 0>();
155 }
156 
157 
158 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
160 {
161  return elmt<0,1>();
162 }
163 
164 
165 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
167 {
168  return elmt<0,1>();
169 }
170 
171 
172 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
174 {
175  return elmt<0,2>();
176 }
177 
178 
179 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
181 {
182  return elmt<0,2>();
183 }
184 
185 
186 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
188 {
189  return elmt<1,0>();
190 }
191 
192 
193 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
195 {
196  return elmt<1,0>();
197 }
198 
199 
200 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
202 {
203  return elmt<1,1>();
204 }
205 
206 
207 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
209 {
210  return elmt<1,1>();
211 }
212 
213 
214 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
216 {
217  return elmt<1,2>();
218 }
219 
220 
221 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
223 {
224  return elmt<1,2>();
225 }
226 
227 
228 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
230 {
231  return elmt<2,0>();
232 }
233 
234 
235 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
237 {
238  return elmt<2,0>();
239 }
240 
241 
242 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
244 {
245  return elmt<2,1>();
246 }
247 
248 
249 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
251 {
252  return elmt<2,1>();
253 }
254 
255 
256 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
258 {
259  return elmt<2,2>();
260 }
261 
262 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
264 {
265  return elmt<2,2>();
266 }
267 
268 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
271 {
272  static_assert(Mrows == Ncols, "Matrix is not square");
273  msType result(Zero);
274 
275  for (direction i=0; i<Ncols; ++i)
276  {
277  result(i, i) = 1;
278  }
279 
280  return result;
281 }
282 
283 
284 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
287 {
288  typename typeOfTranspose<Cmpt, Form>::type result;
289 
290  for (direction i=0; i<Mrows; ++i)
291  {
292  for (direction j=0; j<Ncols; ++j)
293  {
294  result(j,i) = operator()(i, j);
295  }
296  }
297 
298  return result;
299 }
300 
301 
302 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
303 template
304 <
305  class SubTensor,
306  Foam::direction BRowStart,
307  Foam::direction BColStart
308 >
310  ConstBlock<SubTensor, BRowStart, BColStart>
312 {
313  return *this;
314 }
315 
316 
317 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
318 template
319 <
320  class SubTensor,
321  Foam::direction BRowStart,
322  Foam::direction BColStart
323 >
324 inline
326  Block<SubTensor, BRowStart, BColStart>
328 {
329  return *this;
330 }
331 
332 
333 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
334 
335 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
337 (
338  const direction& i,
339  const direction& j
340 ) const
341 {
342  #ifdef FULLDEBUG
343  if (i >= Mrows || j >= Ncols)
344  {
346  << "indices out of range"
347  << abort(FatalError);
348  }
349  #endif
350 
351  return this->v_[i*Ncols + j];
352 }
353 
354 
355 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
357 (
358  const direction& i,
359  const direction& j
360 )
361 {
362  #ifdef FULLDEBUG
363  if (i >= Mrows || j >= Ncols)
364  {
366  << "indices out of range"
367  << abort(FatalError);
368  }
369  #endif
370 
371  return this->v_[i*Ncols + j];
372 }
373 
374 
375 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
376 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
377 inline SubTensor
380 operator()() const
381 {
382  return *this;
383 }
384 
385 
386 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
387 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
388 inline const Cmpt&
391 operator()(const direction i, const direction j) const
392 {
393  return matrix_(BRowStart + i, BColStart + j);
394 }
395 
396 
397 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
398 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
399 inline SubTensor
402 operator()() const
403 {
404  SubTensor st;
405 
406  for (direction i=0; i<SubTensor::mRows; ++i)
407  {
408  for (direction j=0; j<SubTensor::nCols; ++j)
409  {
410  st[i*SubTensor::nCols + j] = operator()(i, j);
411  }
412  }
413 
414  return st;
415 }
416 
417 
418 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
419 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
420 inline const Cmpt&
423 operator()(const direction i, const direction j) const
424 {
425  return matrix_(BRowStart + i, BColStart + j);
426 }
427 
428 
429 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
430 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
431 inline Cmpt&
434 operator()(const direction i, const direction j)
435 {
436  return matrix_(BRowStart + i, BColStart + j);
437 }
438 
439 
440 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
442 (
443  const Foam::zero
444 )
445 {
447 }
448 
449 
450 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
451 template<class Form2>
453 (
455 )
456 {
457  *this = *this & matrix;
458 }
459 
460 
461 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
462 template
463 <
464  template<class, Foam::direction, Foam::direction> class Block2,
465  Foam::direction BRowStart,
466  Foam::direction BColStart
467 >
469 (
470  const Block2<Form, BRowStart, BColStart>& block
471 )
472 {
473  for (direction i = 0; i < Mrows; ++i)
474  {
475  for (direction j = 0; j < Ncols; ++j)
476  {
477  operator()(i, j) = block(i, j);
478  }
479  }
480 }
481 
482 
483 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
484 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
485 template<class Form2>
486 inline void
488 Block<SubTensor, BRowStart, BColStart>::
489 operator=
490 (
492 )
493 {
494  for (direction i=0; i<mRows; ++i)
495  {
496  for (direction j=0; j<nCols; ++j)
497  {
498  operator()(i,j) = matrix(i,j);
499  }
500  }
501 }
502 
503 
504 template<class Form, class Cmpt, Foam::direction Mrows, Foam::direction Ncols>
505 template<class SubTensor, Foam::direction BRowStart, Foam::direction BColStart>
506 template<class VSForm>
507 inline void
510 operator=
511 (
513 )
514 {
515  static_assert(nCols == 1, "Matrix must have a single column");
516 
517  for (direction i=0; i<SubTensor::mRows; ++i)
518  {
519  operator()(i,0) = v[i];
520  }
521 }
522 
523 
524 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
525 
526 namespace Foam
527 {
528 
529 // * * * * * * * * * * * * * * * Friend Functions * * * * * * * * * * * * * //
530 
531 template<class Form, class Cmpt, direction Mrows, direction Ncols>
532 inline typename typeOfTranspose<Cmpt, Form>::type T
533 (
535 )
536 {
537  return matrix.T();
538 }
539 
540 
541 template<class Form, class Cmpt, direction Ncmpts>
542 inline typename typeOfTranspose<Cmpt, Form>::type T
543 (
545 )
546 {
547  typename typeOfTranspose<Cmpt, Form>::type result;
548 
549  for (direction i=0; i<Ncmpts; ++i)
550  {
551  result[i] = v[i];
552  }
553 
554  return result;
555 }
556 
557 
558 // * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
559 
560 template
561 <
562  class Form1,
563  class Form2,
564  class Cmpt,
565  direction Mrows1,
566  direction Ncols1,
567  direction Mrows2,
568  direction Ncols2
569 >
570 inline typename typeOfInnerProduct<Cmpt, Form1, Form2>::type operator&
571 (
574 )
575 {
576  static_assert
577  (
578  Ncols1 == Mrows2,
579  "Number of columns in matrix 1 != number of rows in matrix 2"
580  );
581 
583 
584  for (direction i=0; i<Mrows1; ++i)
585  {
586  for (direction j=0; j<Ncols2; ++j)
587  {
588  for (direction k=0; k<Mrows2; k++)
589  {
590  result(i, j) += matrix1(i, k)*matrix2(k, j);
591  }
592  }
593  }
594 
595  return result;
596 }
597 
598 
599 template<class Form, class VSForm, class Cmpt, direction Mrows, direction Ncols>
600 inline typename typeOfInnerProduct<Cmpt, Form, VSForm>::type operator&
601 (
604 )
605 {
607 
608  for (direction i=0; i<Mrows; ++i)
609  {
610  for (direction j=0; j<Ncols; ++j)
611  {
612  result[i] += matrix(i, j)*v[j];
613  }
614  }
615 
616  return result;
617 }
618 
619 
620 template
621 <
622  class Form1,
623  class Form2,
624  class Cmpt,
625  direction Ncmpts1,
626  direction Ncmpts2
627 >
628 inline typename typeOfOuterProduct<Cmpt, Form1, Form2>::type operator*
629 (
632 )
633 {
635 
636  for (direction i=0; i<Ncmpts1; ++i)
637  {
638  for (direction j=0; j<Ncmpts2; ++j)
639  {
640  result(i, j) = v1[i]*v2[j];
641  }
642  }
643 
644  return result;
645 }
646 
647 
648 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
649 
650 } // End namespace Foam
651 
652 // ************************************************************************* //
Foam::MatrixSpace::zz
const Cmpt & zz() const
Definition: MatrixSpaceI.H:257
Foam::MatrixSpace::MatrixSpace
MatrixSpace()=default
Default construct.
Foam::block
Creates a single block of cells from point coordinates, numbers of cells in each direction and an exp...
Definition: block.H:58
Foam::MatrixSpace::xx
const Cmpt & xx() const
Definition: MatrixSpaceI.H:145
Foam::Zero
static constexpr const zero Zero
Global zero (0)
Definition: zero.H:131
Foam::MatrixSpace::elmt
const Cmpt & elmt() const
Fast const element access using compile-time addressing.
Definition: MatrixSpaceI.H:128
Foam::typeOfTranspose
Abstract template class to provide the transpose form of a form.
Definition: products.H:62
Foam::MatrixSpace::T
typeOfTranspose< Cmpt, Form >::type T() const
Return the transpose of the matrix.
Definition: MatrixSpaceI.H:286
Foam::MatrixSpace::Block::nCols
static const direction nCols
Definition: MatrixSpace.H:150
Foam::MatrixSpace::ConstBlock::mRows
static const direction mRows
Definition: MatrixSpace.H:105
Foam::MatrixSpace::zy
const Cmpt & zy() const
Definition: MatrixSpaceI.H:243
Foam::MatrixSpace::mRows
static constexpr direction mRows
Definition: MatrixSpace.H:72
Foam::MatrixSpace::ConstBlock::nCols
static const direction nCols
Definition: MatrixSpace.H:106
Foam::MatrixSpace::yy
const Cmpt & yy() const
Definition: MatrixSpaceI.H:201
Foam::VectorSpace< Form, Cmpt, Mrows *Ncols >::v_
Cmpt v_[Ncmpts]
The components of this vector space.
Definition: VectorSpace.H:83
Foam::VectorSpace
Templated vector space.
Definition: VectorSpace.H:56
Foam::MatrixSpace::nCols
static constexpr direction nCols
Definition: MatrixSpace.H:73
Foam::MatrixSpace
Templated matrix space.
Definition: MatrixSpace.H:58
Foam::MatrixSpace::yz
const Cmpt & yz() const
Definition: MatrixSpaceI.H:215
Foam::Istream
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
Definition: Istream.H:61
Foam::MatrixSpace::Block
Sub-block type.
Definition: MatrixSpace.H:142
Foam::FatalError
error FatalError
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::abort
errorManip< error > abort(error &err)
Definition: errorManip.H:144
FatalErrorInFunction
#define FatalErrorInFunction
Report an error message using Foam::FatalError.
Definition: error.H:453
Foam::MatrixSpace::block
ConstBlock< SubTensor, BRowStart, BColStart > block() const
Return a const sub-block corresponding to the specified type.
Foam::typeOfOuterProduct
Definition: products.H:57
Foam::typeOfInnerProduct
Definition: products.H:51
k
label k
Boltzmann constant.
Definition: LISASMDCalcMethod2.H:41
Foam::direction
uint8_t direction
Definition: direction.H:52
Foam::MatrixSpace::operator()
const Cmpt & operator()(const direction &i, const direction &j) const
(i, j) const element access operator
Definition: MatrixSpaceI.H:337
Foam::roots::type
type
Types of root.
Definition: Roots.H:54
Foam::MatrixSpace::zx
const Cmpt & zx() const
Definition: MatrixSpaceI.H:229
Foam::MatrixSpace::identity
static msType identity()
An identity matrix for square matrix-spaces.
Definition: MatrixSpaceI.H:270
Foam::MatrixSpace::yx
const Cmpt & yx() const
Definition: MatrixSpaceI.H:187
Foam::MatrixSpace::xy
const Cmpt & xy() const
Definition: MatrixSpaceI.H:159
Foam::VectorSpace::operator=
void operator=(const VectorSpace< Form, Cmpt, Ncmpts > &)
Definition: VectorSpaceI.H:338
Foam::MatrixSpace::Block::mRows
static const direction mRows
Definition: MatrixSpace.H:149
Foam::VectorSpace< Form, Cmpt, Mrows *Ncols >::operator
friend Ostream & operator(Ostream &, const VectorSpace< Form, Cmpt, Ncmpts > &)
Foam::MatrixSpace::xz
const Cmpt & xz() const
Definition: MatrixSpaceI.H:173
Foam::zero
A class representing the concept of 0 (zero) that can be used to avoid manipulating objects known to ...
Definition: zero.H:62