Line data Source code
1 : // Copyright (C) 2015 Technische Universitaet Muenchen
2 : // This file is part of the Mamico project. For conditions of distribution
3 : // and use, please see the copyright notice in Mamico's main folder, or at
4 : // www5.in.tum.de/mamico
5 : #ifndef _TARCH_LA_VECTOROPERATIONS_H_
6 : #define _TARCH_LA_VECTOROPERATIONS_H_
7 :
8 : #include <Kokkos_Core.hpp>
9 :
10 : /** namespace tarch */
11 : namespace tarch {
12 : /** namespace la */
13 : namespace la {
14 : /** this file contains some methods for vector types.
15 : * @brief adds two vectors and returns the result
16 : * @tparam T Data type
17 : * @tparam size Vector size
18 : * @author Philipp Neumann
19 : * @return result resulting vector
20 : */
21 5658612 : template <int size, class T> KOKKOS_FUNCTION tarch::la::Vector<size, T> operator+(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
22 5658612 : tarch::la::Vector<size, T> result;
23 22634448 : for (int i = 0; i < size; i++) {
24 16975836 : result[i] = l[i] + r[i];
25 : }
26 5658612 : return result;
27 : }
28 :
29 : /** this file contains some methods for vector types.
30 : * @brief subtracts two vectors and returns the result
31 : * @tparam T Data type
32 : * @tparam size Vector size
33 : * @author Philipp Neumann
34 : * @return result resulting vector
35 : */
36 4790484 : template <int size, class T> KOKKOS_FUNCTION tarch::la::Vector<size, T> operator-(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
37 4790484 : tarch::la::Vector<size, T> result;
38 19161936 : for (int i = 0; i < size; i++) {
39 14371452 : result[i] = l[i] - r[i];
40 : }
41 4790484 : return result;
42 : }
43 :
44 : /** this file contains some methods for vector types.
45 : * @brief multiplies a vector to a scalar and returns the resulting vector
46 : * @tparam T Data type
47 : * @tparam size Vector size
48 : * @author Philipp Neumann
49 : * @return result resulting vector
50 : */
51 72 : template <int size, class T> KOKKOS_FUNCTION tarch::la::Vector<size, T> operator*(const tarch::la::Vector<size, T>& v, const T& s) {
52 72 : tarch::la::Vector<size, T> result;
53 288 : for (int i = 0; i < size; i++) {
54 216 : result[i] = v[i] * s;
55 : }
56 72 : return result;
57 : }
58 :
59 : /** this file contains some methods for vector types.
60 : * @brief multiplies a scalar to a vector and returns the resulting vector
61 : * @tparam T Data type
62 : * @tparam size Vector size
63 : * @author Philipp Neumann
64 : * @return result resulting vector
65 : */
66 244396 : template <int size, class T> KOKKOS_FUNCTION tarch::la::Vector<size, T> operator*(const T& s, const tarch::la::Vector<size, T>& v) {
67 244396 : tarch::la::Vector<size, T> result;
68 977584 : for (int i = 0; i < size; i++) {
69 733188 : result[i] = v[i] * s;
70 : }
71 244396 : return result;
72 : }
73 :
74 : /** this file contains some methods for vector types.
75 : * @brief devides a vector over a scaler, i.e. this operator devides all
76 : * entries of vector over a scalar and returns the result.
77 : * @tparam T Data type
78 : * @tparam size Vector size
79 : * @author Niklas Wittmer
80 : * @return result resulting vector
81 : */
82 72 : template <int size, class T> KOKKOS_FUNCTION tarch::la::Vector<size, T> operator/(const tarch::la::Vector<size, T>& v, const T& s) {
83 72 : tarch::la::Vector<size, T> result;
84 288 : for (int i = 0; i < size; i++) {
85 216 : result[i] = v[i] / s;
86 : }
87 72 : return result;
88 : }
89 :
90 : /** this file contains some methods for vector types.
91 : * @brief computes the inner product of two vectors
92 : * @tparam T Data type
93 : * @tparam size Vector size
94 : * @author Philipp Neumann
95 : * @return t resulting scalar
96 : */
97 0 : template <int size, class T> KOKKOS_FUNCTION T dot(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
98 0 : T t = l[0] * r[0];
99 0 : for (int i = 1; i < size; i++) {
100 0 : t += l[i] * r[i];
101 : }
102 0 : return t;
103 : }
104 :
105 : /** This operator overloading compares two vectors and determins if they are equal.
106 : * @brief for comparison of two vectors
107 : * @tparam T Data type
108 : * @tparam size Vector size
109 : * @author Philipp Neumann
110 : * @return equal result; true: if they are equal, false otherwise.
111 : */
112 1378068 : template <int size, class T> KOKKOS_FUNCTION bool operator==(const Vector<size, T>& l, const Vector<size, T>& r) {
113 1378068 : bool equal = true;
114 5512124 : for (int i = 0; i < size; i++) {
115 7919464 : equal = equal && (l[i] == r[i]);
116 : }
117 1378068 : return equal;
118 : }
119 :
120 : /** This operator overloading compares two vectors and determins if they are NOT equal.
121 : * @brief for comparison of two vectors
122 : * @tparam T Data type
123 : * @tparam size Vector size
124 : * @author Philipp Neumann
125 : * @return true: if they are NOT equal, false otherwise.
126 : */
127 16660 : template <int size, class T> KOKKOS_FUNCTION bool operator!=(const Vector<size, T>& l, const Vector<size, T>& r) {
128 66612 : for (int i = 0; i < size; i++) {
129 49962 : if (l[i] != r[i])
130 : return true;
131 : }
132 : return false;
133 : }
134 :
135 : /** this file contains some methods for vector types.
136 : * @brief computes the L2-norm
137 : * @tparam T Data type
138 : * @tparam size Vector size
139 : * @author Philipp Neumann
140 : * @return the square root of the inner product (dot product) of a vector
141 : *with itself
142 : */
143 : template <int size, class T> KOKKOS_FUNCTION T norm2(const Vector<size, T>& v) { return sqrt(tarch::la::dot(v, v)); }
144 :
145 : /** this file contains some methods for vector types.
146 : * @brief operator overloading for output purposes
147 : * @tparam T Data type
148 : * @tparam size Vector size
149 : * @author Philipp Neumann
150 : */
151 224 : template <int size, class T> std::ostream& operator<<(std::ostream& os, const tarch::la::Vector<size, T>& v) {
152 672 : for (int i = 0; i < size - 1; i++) {
153 448 : os << v[i] << " , ";
154 : }
155 224 : os << v[size - 1];
156 224 : return os;
157 : }
158 :
159 : } // namespace la
160 : } // namespace tarch
161 : #endif
|