Add mdspan basic elementwise math
[alexxy/gromacs.git] / src / gromacs / mdspan / extensions.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2019, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 /*! \libinternal
36  * \file
37  * \brief GROMACS extensions to mdspan.
38  *
39  * \author Christian Blau <cblau@gwdg.de>
40  * \inlibraryapi
41  * \ingroup module_mdspan
42  */
43
44 #ifndef GMX_MDSPAN_EXTENSIONS_H_
45 #define GMX_MDSPAN_EXTENSIONS_H_
46
47 #include <algorithm>
48 #include <functional>
49
50 #include "gromacs/mdspan/mdspan.h"
51
52 namespace gmx
53 {
54
55 /*! \brief
56  * Free begin function addressing memory of a contiguously laid out basic_mdspan.
57  *
58  * \note Changing the elements that basic_mdspan views does not change
59  *       the view itself, so a single begin that takes a const view suffices.
60  */
61 template <class BasicMdspan>
62 constexpr typename std::enable_if<BasicMdspan::is_always_contiguous(),
63                                   typename BasicMdspan::pointer>::type
64 begin(const BasicMdspan &basicMdspan)
65 {
66     return basicMdspan.data();
67 }
68
69 /*! \brief
70  * Free end function addressing memory of a contiguously laid out basic_mdspan.
71  *
72  * \note Changing the elements that basic_mdspan views does not change
73  *       the view itself, so a single end that takes a const view suffices.
74  */
75 template <class BasicMdspan>
76 constexpr typename std::enable_if<BasicMdspan::is_always_contiguous(),
77                                   typename BasicMdspan::pointer>::type
78 end(const BasicMdspan &basicMdspan)
79 {
80     return basicMdspan.data() + basicMdspan.mapping().required_span_size();
81 }
82
83 //! Convenience type for often-used three dimensional extents
84 using dynamicExtents3D = extents<dynamic_extent, dynamic_extent, dynamic_extent>;
85
86 //! Elementwise addition
87 template <class BasicMdspan>
88 constexpr BasicMdspan addElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
89 {
90     BasicMdspan result(span1);
91     std::transform(begin(span1), end(span1), begin(span2),
92                    begin(result), std::plus<typename BasicMdspan::element_type>());
93     return result;
94 }
95
96 //! Elementwise subtraction - left minus right
97 template <class BasicMdspan>
98 constexpr BasicMdspan subtractElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
99 {
100     BasicMdspan result(span1);
101     std::transform(begin(span1), end(span1), begin(span2),
102                    begin(result), std::minus<typename BasicMdspan::element_type>());
103     return result;
104 }
105
106 //! Elementwise multiplication
107 template <class BasicMdspan>
108 constexpr BasicMdspan multiplyElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
109 {
110     BasicMdspan result(span1);
111     std::transform(begin(span1), end(span1), begin(span2),
112                    begin(result), std::multiplies<typename BasicMdspan::element_type>());
113     return result;
114 }
115
116 //! Elementwise division - left / right
117 template <class BasicMdspan>
118 constexpr BasicMdspan divideElementwise(const BasicMdspan &span1, const BasicMdspan &span2)
119 {
120     BasicMdspan result(span1);
121     std::transform(begin(span1), end(span1), begin(span2),
122                    begin(result), std::divides<typename BasicMdspan::element_type>());
123     return result;
124 }
125
126 }      // namespace gmx
127
128 #endif // GMX_MDSPAN_EXTENSIONS_H_