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 : /** namespace tarch */
9 : namespace tarch {
10 : /** namespace la */
11 : namespace la {
12 : /** this file contains some methods for vector types.
13 : * @brief adds two vectors and returns the result
14 : * @tparam T Data type
15 : * @tparam size Vector size
16 : * @author Philipp Neumann
17 : * @return result resulting vector
18 : */
19 5660528 : template <int size, class T> tarch::la::Vector<size, T> operator+(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
20 5660528 : tarch::la::Vector<size, T> result;
21 22642112 : for (int i = 0; i < size; i++) {
22 16981584 : result[i] = l[i] + r[i];
23 : }
24 5660528 : return result;
25 : }
26 :
27 : /** this file contains some methods for vector types.
28 : * @brief subtracts two vectors and returns the result
29 : * @tparam T Data type
30 : * @tparam size Vector size
31 : * @author Philipp Neumann
32 : * @return result resulting vector
33 : */
34 4790328 : template <int size, class T> tarch::la::Vector<size, T> operator-(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
35 4790328 : tarch::la::Vector<size, T> result;
36 19161312 : for (int i = 0; i < size; i++) {
37 14370984 : result[i] = l[i] - r[i];
38 : }
39 4790328 : return result;
40 : }
41 :
42 : /** this file contains some methods for vector types.
43 : * @brief multiplies a vector to a scalar and returns the resulting vector
44 : * @tparam T Data type
45 : * @tparam size Vector size
46 : * @author Philipp Neumann
47 : * @return result resulting vector
48 : */
49 72 : template <int size, class T> tarch::la::Vector<size, T> operator*(const tarch::la::Vector<size, T>& v, const T& s) {
50 72 : tarch::la::Vector<size, T> result;
51 288 : for (int i = 0; i < size; i++) {
52 216 : result[i] = v[i] * s;
53 : }
54 72 : return result;
55 : }
56 :
57 : /** this file contains some methods for vector types.
58 : * @brief multiplies a scalar to a vector and returns the resulting vector
59 : * @tparam T Data type
60 : * @tparam size Vector size
61 : * @author Philipp Neumann
62 : * @return result resulting vector
63 : */
64 51140 : template <int size, class T> tarch::la::Vector<size, T> operator*(const T& s, const tarch::la::Vector<size, T>& v) {
65 51140 : tarch::la::Vector<size, T> result;
66 204560 : for (int i = 0; i < size; i++) {
67 153420 : result[i] = v[i] * s;
68 : }
69 51140 : return result;
70 : }
71 :
72 : /** this file contains some methods for vector types.
73 : * @brief devides a vector over a scaler, i.e. this operator devides all
74 : * entries of vector over a scalar and returns the result.
75 : * @tparam T Data type
76 : * @tparam size Vector size
77 : * @author Niklas Wittmer
78 : * @return result resulting vector
79 : */
80 83016 : template <int size, class T> tarch::la::Vector<size, T> operator/(const tarch::la::Vector<size, T>& v, const T& s) {
81 83016 : tarch::la::Vector<size, T> result;
82 332064 : for (int i = 0; i < size; i++) {
83 249048 : result[i] = v[i] / s;
84 : }
85 83016 : return result;
86 : }
87 :
88 : /** this file contains some methods for vector types.
89 : * @brief computes the inner product of two vectors
90 : * @tparam T Data type
91 : * @tparam size Vector size
92 : * @author Philipp Neumann
93 : * @return t resulting scalar
94 : */
95 0 : template <int size, class T> T dot(const tarch::la::Vector<size, T>& l, const tarch::la::Vector<size, T>& r) {
96 0 : T t = l[0] * r[0];
97 0 : for (int i = 1; i < size; i++) {
98 0 : t += l[i] * r[i];
99 : }
100 0 : return t;
101 : }
102 :
103 : /** This operator overloading compares two vectors and determins if they are equal.
104 : * @brief for comparison of two vectors
105 : * @tparam T Data type
106 : * @tparam size Vector size
107 : * @author Philipp Neumann
108 : * @return equal result; true: if they are equal, false otherwise.
109 : */
110 1378068 : template <int size, class T> bool operator==(const Vector<size, T>& l, const Vector<size, T>& r) {
111 1378068 : bool equal = true;
112 5512124 : for (int i = 0; i < size; i++) {
113 4530672 : equal = equal && (l[i] == r[i]);
114 : }
115 1378068 : return equal;
116 : }
117 :
118 : /** This operator overloading compares two vectors and determins if they are NOT equal.
119 : * @brief for comparison of two vectors
120 : * @tparam T Data type
121 : * @tparam size Vector size
122 : * @author Philipp Neumann
123 : * @return true: if they are NOT equal, false otherwise.
124 : */
125 160 : template <int size, class T> bool operator!=(const Vector<size, T>& l, const Vector<size, T>& r) {
126 612 : for (int i = 0; i < size; i++) {
127 462 : if (l[i] != r[i])
128 : return true;
129 : }
130 : return false;
131 : }
132 :
133 : /** this file contains some methods for vector types.
134 : * @brief computes the L2-norm
135 : * @tparam T Data type
136 : * @tparam size Vector size
137 : * @author Philipp Neumann
138 : * @return the square root of the inner product (dot product) of a vector
139 : *with itself
140 : */
141 : template <int size, class T> T norm2(const Vector<size, T>& v) { return sqrt(tarch::la::dot(v, v)); }
142 :
143 : /** this file contains some methods for vector types.
144 : * @brief operator overloading for output purposes
145 : * @tparam T Data type
146 : * @tparam size Vector size
147 : * @author Philipp Neumann
148 : */
149 144 : template <int size, class T> std::ostream& operator<<(std::ostream& os, const tarch::la::Vector<size, T>& v) {
150 432 : for (int i = 0; i < size - 1; i++) {
151 288 : os << v[i] << " , ";
152 : }
153 144 : os << v[size - 1];
154 144 : return os;
155 : }
156 :
157 : } // namespace la
158 : } // namespace tarch
159 : #endif
|