1 /*---------------------------------------------------------------------------*\
3 \\ / F ield | foam-extend: Open Source CFD
4 \\ / O peration | Version: 3.2
5 \\ / A nd | Web: http://www.foam-extend.org
6 \\/ M anipulation | For copyright notice see file Copyright
7 -------------------------------------------------------------------------------
9 This file is part of foam-extend.
11 foam-extend is free software: you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by the
13 Free Software Foundation, either version 3 of the License, or (at your
14 option) any later version.
16 foam-extend is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
28 #include "VectorSpaceM.H"
32 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
37 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
39 template<class Form, class Cmpt, int nCmpt>
40 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace()
44 template<class Form, class Cmpt, int nCmpt>
45 inline VectorSpace<Form, Cmpt, nCmpt>::VectorSpace
47 const VectorSpace<Form, Cmpt, nCmpt>& vs
50 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
54 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
56 template<class Form, class Cmpt, int nCmpt>
57 inline label VectorSpace<Form, Cmpt, nCmpt>::size() const
63 template<class Form, class Cmpt, int nCmpt>
64 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
74 "VectorSpace<Form, Cmpt, nCmpt>::component(direction) const"
75 ) << "index out of range"
84 template<class Form, class Cmpt, int nCmpt>
85 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::component
93 FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::component(direction)")
94 << "index out of range"
103 template<class Form, class Cmpt, int nCmpt>
104 inline void VectorSpace<Form, Cmpt, nCmpt>::component
115 "VectorSpace<Form, Cmpt, nCmpt>::component(Cmpt&, direction) const"
116 ) << "index out of range"
117 << abort(FatalError);
125 template<class Form, class Cmpt, int nCmpt>
126 inline void VectorSpace<Form, Cmpt, nCmpt>::replace
137 "VectorSpace<Form, Cmpt, nCmpt>::"
138 "replace(direction, const Cmpt&) const"
139 ) << "index out of range"
140 << abort(FatalError);
148 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
150 template<class Form, class Cmpt, int nCmpt>
151 inline const Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
161 "VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d) const"
162 ) << "index out of range"
163 << abort(FatalError);
171 template<class Form, class Cmpt, int nCmpt>
172 inline Cmpt& VectorSpace<Form, Cmpt, nCmpt>::operator[]
180 FatalErrorIn("VectorSpace<Form, Cmpt, nCmpt>::operator[](direction d)")
181 << "index out of range"
182 << abort(FatalError);
190 template<class Form, class Cmpt, int nCmpt>
191 inline void VectorSpace<Form, Cmpt, nCmpt>::operator=
193 const VectorSpace<Form, Cmpt, nCmpt>& vs
196 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, eqOp<Cmpt>());
200 template<class Form, class Cmpt, int nCmpt>
201 inline void VectorSpace<Form, Cmpt, nCmpt>::operator+=
203 const VectorSpace<Form, Cmpt, nCmpt>& vs
206 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, plusEqOp<Cmpt>());
210 template<class Form, class Cmpt, int nCmpt>
211 inline void VectorSpace<Form, Cmpt, nCmpt>::operator-=
213 const VectorSpace<Form, Cmpt, nCmpt>& vs
216 VectorSpaceOps<nCmpt,0>::eqOp(*this, vs, minusEqOp<Cmpt>());
220 template<class Form, class Cmpt, int nCmpt>
221 inline void VectorSpace<Form, Cmpt, nCmpt>::operator*=
226 VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, multiplyEqOp2<Cmpt, scalar>());
230 template<class Form, class Cmpt, int nCmpt>
231 inline void VectorSpace<Form, Cmpt, nCmpt>::operator/=
236 VectorSpaceOps<nCmpt,0>::eqOpS(*this, s, divideEqOp2<Cmpt, scalar>());
240 // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
242 template<class Form, class Cmpt, int nCmpt>
243 inline Cmpt& setComponent
245 VectorSpace<Form, Cmpt, nCmpt>& vs,
249 return vs.component(d);
253 template<class Form, class Cmpt, int nCmpt>
254 inline const Cmpt& component
256 const VectorSpace<Form, Cmpt, nCmpt>& vs,
260 return vs.component(d);
265 // Equivalent to outer-products between the Form and itself
267 template<class Form, class Cmpt, int nCmpt>
268 inline typename powProduct<Form, 0>::type pow
270 const VectorSpace<Form, Cmpt, nCmpt>&,
271 typename powProduct<Form, 0>::type
272 = pTraits<typename powProduct<Form, 0>::type>::zero
279 template<class Form, class Cmpt, int nCmpt>
280 inline typename powProduct<Form, 1>::type pow
282 const VectorSpace<Form, Cmpt, nCmpt>& v,
283 typename powProduct<Form, 1>::type
284 = pTraits<typename powProduct<Form, 1>::type>::zero
287 return static_cast<const Form&>(v);
291 // Form^2 = sqr(Form)
292 template<class Form, class Cmpt, int nCmpt>
293 inline typename powProduct<Form, 2>::type pow
295 const VectorSpace<Form, Cmpt, nCmpt>& v,
296 typename powProduct<Form, 2>::type
297 = pTraits<typename powProduct<Form, 2>::type>::zero
300 return sqr(static_cast<const Form&>(v));
304 template<class Form, class Cmpt, int nCmpt>
307 const VectorSpace<Form, Cmpt, nCmpt>& vs
310 scalar ms = magSqr(vs.v_[0]);
311 VectorSpaceOps<nCmpt,1>::SeqOp(ms, vs, plusEqMagSqrOp2<scalar, Cmpt>());
316 template<class Form, class Cmpt, int nCmpt>
319 const VectorSpace<Form, Cmpt, nCmpt>& vs
322 return ::sqrt(magSqr(static_cast<const Form&>(vs)));
326 template<class Form, class Cmpt, int nCmpt>
327 inline VectorSpace<Form, Cmpt, nCmpt> cmptMultiply
329 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
330 const VectorSpace<Form, Cmpt, nCmpt>& vs2
334 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptMultiplyOp<Cmpt>());
339 template<class Form, class Cmpt, int nCmpt>
340 inline VectorSpace<Form, Cmpt, nCmpt> cmptDivide
342 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
343 const VectorSpace<Form, Cmpt, nCmpt>& vs2
347 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, cmptDivideOp<Cmpt>());
352 template<class Form, class Cmpt, int nCmpt>
353 inline Cmpt cmptSumMultiply
355 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
356 const VectorSpace<Form, Cmpt, nCmpt>& vs2
359 return cmptSum(cmptMultiply(vs1, vs2));
363 template<class Form, class Cmpt, int nCmpt>
364 inline VectorSpace<Form, Cmpt, nCmpt> stabilise
366 const VectorSpace<Form, Cmpt, nCmpt>& vs,
371 VectorSpaceOps<nCmpt,0>::opVS(v, vs, small, stabiliseOp<Cmpt>());
376 template<class Form, class Cmpt, int nCmpt>
379 const VectorSpace<Form, Cmpt, nCmpt>& vs
382 Cmpt cMax = vs.v_[0];
383 VectorSpaceOps<nCmpt,1>::SeqOp(cMax, vs, maxEqOp<Cmpt>());
388 template<class Form, class Cmpt, int nCmpt>
391 const VectorSpace<Form, Cmpt, nCmpt>& vs
394 Cmpt cMin = vs.v_[0];
395 VectorSpaceOps<nCmpt,1>::SeqOp(cMin, vs, minEqOp<Cmpt>());
400 template<class Form, class Cmpt, int nCmpt>
403 const VectorSpace<Form, Cmpt, nCmpt>& vs
407 VectorSpaceOps<nCmpt,1>::SeqOp(sum, vs, plusEqOp<Cmpt>());
412 template<class Form, class Cmpt, int nCmpt>
415 const VectorSpace<Form, Cmpt, nCmpt>& vs
418 return cmptSum(vs)/nCmpt;
422 template<class Form, class Cmpt, int nCmpt>
425 const VectorSpace<Form, Cmpt, nCmpt>& vs
429 VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMagOp<Cmpt>());
434 template<class Form, class Cmpt, int nCmpt>
435 inline Form cmptStabilise
437 const VectorSpace<Form, Cmpt, nCmpt>& vs,
443 for (int i=0; i<nCmpt; i++)
445 if (mag(vs.v_[i]) < small)
447 v.v_[i] = sign(vs.v_[i])*value;
454 template<class Form, class Cmpt, int nCmpt>
457 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
458 const VectorSpace<Form, Cmpt, nCmpt>& vs2
462 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, maxOp<Cmpt>());
467 template<class Form, class Cmpt, int nCmpt>
470 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
471 const VectorSpace<Form, Cmpt, nCmpt>& vs2
475 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minOp<Cmpt>());
480 template<class Form, class Cmpt, int nCmpt>
483 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
484 const VectorSpace<Form, Cmpt, nCmpt>& vs2
488 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minModOp<Cmpt>());
494 inline Type dot(const scalar s, const Type& t)
501 inline Type dot(const Type& t, const scalar s)
509 class Form1, class Cmpt1, int nCmpt1,
510 class Form2, class Cmpt2, int nCmpt2
512 inline typename innerProduct<Form1, Form2>::type dot
514 const VectorSpace<Form1, Cmpt1, nCmpt1>& t1,
515 const VectorSpace<Form2, Cmpt2, nCmpt2>& t2
518 return static_cast<const Form1&>(t1) & static_cast<const Form2&>(t2);
522 // * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
524 template<class Form, class Cmpt, int nCmpt>
525 inline Form operator-
527 const VectorSpace<Form, Cmpt, nCmpt>& vs
531 VectorSpaceOps<nCmpt,0>::eqOp(v, vs, eqMinusOp<Cmpt>());
536 template<class Form, class Cmpt, int nCmpt>
537 inline Form operator+
539 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
540 const VectorSpace<Form, Cmpt, nCmpt>& vs2
544 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, plusOp<Cmpt>());
549 template<class Form, class Cmpt, int nCmpt>
550 inline Form operator-
552 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
553 const VectorSpace<Form, Cmpt, nCmpt>& vs2
557 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, minusOp<Cmpt>());
562 template<class Form, class Cmpt, int nCmpt>
563 inline Form operator*
566 const VectorSpace<Form, Cmpt, nCmpt>& vs
570 VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, multiplyOp3<Cmpt, scalar, Cmpt>());
575 template<class Form, class Cmpt, int nCmpt>
576 inline Form operator*
578 const VectorSpace<Form, Cmpt, nCmpt>& vs,
583 VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, multiplyOp3<Cmpt, Cmpt, scalar>());
588 template<class Form, class Cmpt, int nCmpt>
589 inline Form operator/
591 const VectorSpace<Form, Cmpt, nCmpt>& vs,
596 VectorSpaceOps<nCmpt,0>::opVS(v, vs, s, divideOp3<Cmpt, Cmpt, scalar>());
601 template<class Form, class Cmpt, int nCmpt>
602 inline Form operator/
604 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
605 const VectorSpace<Form, Cmpt, nCmpt>& vs2
609 VectorSpaceOps<nCmpt,0>::op(v, vs1, vs2, divideOp<Cmpt>());
614 template<class Form, class Cmpt, int nCmpt>
615 inline Form operator/
618 const VectorSpace<Form, Cmpt, nCmpt>& vs
622 VectorSpaceOps<nCmpt,0>::opSV(v, s, vs, divideOp2<scalar, Cmpt>());
628 template<class Form, class Cmpt, int nCmpt>
629 inline Cmpt operator&&
631 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
632 const VectorSpace<Form, Cmpt, nCmpt>& vs2
635 Cmpt ddProd = vs1.v_[0]*vs2.v_[0];
636 for (int i=1; i<nCmpt; ++i)
638 ddProd += vs1.v_[i]*vs2.v_[i];
644 template<class Form, class Cmpt, int nCmpt>
645 inline bool operator==
647 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
648 const VectorSpace<Form, Cmpt, nCmpt>& vs2
652 for (int i=0; i<nCmpt; ++i)
654 if (!(eq &= (equal(vs1.v_[i], vs2.v_[i])))) break;
660 template<class Form, class Cmpt, int nCmpt>
661 inline bool operator!=
663 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
664 const VectorSpace<Form, Cmpt, nCmpt>& vs2
667 return !(vs1 == vs2);
671 template<class Form, class Cmpt, int nCmpt>
672 inline bool operator>
674 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
675 const VectorSpace<Form, Cmpt, nCmpt>& vs2
679 for (int i=0; i<nCmpt; ++i)
681 if (!(gt &= vs1.v_[i] > vs2.v_[i])) break;
687 template<class Form, class Cmpt, int nCmpt>
688 inline bool operator<
690 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
691 const VectorSpace<Form, Cmpt, nCmpt>& vs2
695 for (int i=0; i<nCmpt; ++i)
697 if (!(lt &= vs1.v_[i] < vs2.v_[i])) break;
703 template<class Form, class Cmpt, int nCmpt>
704 inline bool operator>=
706 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
707 const VectorSpace<Form, Cmpt, nCmpt>& vs2
714 template<class Form, class Cmpt, int nCmpt>
715 inline bool operator<=
717 const VectorSpace<Form, Cmpt, nCmpt>& vs1,
718 const VectorSpace<Form, Cmpt, nCmpt>& vs2
725 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
727 } // End namespace Foam
729 // ************************************************************************* //