ls1-MarDyn
ls1-MarDyn molecular dynamics code
Vector3.h
1/*
2 * Vector3.h
3 *
4 * Created on: Nov 21, 2014
5 * Author: tchipevn
6 */
7
8#ifndef VECTOR3_H_
9#define VECTOR3_H_
10
11#include <cmath>
12#include <iostream>
13
14namespace bhfmm {
15
16template<typename type>
17class Vector3;
18
21template<typename type>
22std::ostream& operator<<(std::ostream& stream, const Vector3<type>& v) {
23
24 stream << "[" << v._content[0] << "; " << v._content[1] << "; " << v._content[2] << "]";
25 return stream;
26}
27
28template<typename type>
29Vector3<type> operator*(double scalar, const Vector3<type>& v) {
30 return v * scalar;
31}
32
33template<typename type>
34class Vector3 {
35public:
36 Vector3() {
37 }
38
39 Vector3(type arg) {
40 _content[0] = arg;
41 _content[1] = arg;
42 _content[2] = arg;
43 }
44
45 Vector3(type arg0, type arg1, type arg2) {
46 _content[0] = arg0;
47 _content[1] = arg1;
48 _content[2] = arg2;
49 }
50
51 Vector3(type args[3]) {
52 _content[0] = args[0];
53 _content[1] = args[1];
54 _content[2] = args[2];
55 }
56
57 Vector3(const Vector3& other) {
58 _content[0] = other._content[0];
59 _content[1] = other._content[1];
60 _content[2] = other._content[2];
61 }
62
63 Vector3 operator+(const Vector3& rhs) const {
64 type result[3];
65 result[0] = _content[0] + rhs._content[0];
66 result[1] = _content[1] + rhs._content[1];
67 result[2] = _content[2] + rhs._content[2];
68 return Vector3(result);
69 }
70
71 void operator+=(const Vector3& rhs) {
72 _content[0] += rhs._content[0];
73 _content[1] += rhs._content[1];
74 _content[2] += rhs._content[2];
75 }
76
77 Vector3 operator-(const Vector3& rhs) const {
78 type result[3];
79 result[0] = _content[0] - rhs._content[0];
80 result[1] = _content[1] - rhs._content[1];
81 result[2] = _content[2] - rhs._content[2];
82 return Vector3(result);
83 }
84
85 Vector3 operator*(double scalar) const {
86 type result[3];
87 result[0] = _content[0] * scalar;
88 result[1] = _content[1] * scalar;
89 result[2] = _content[2] * scalar;
90 return Vector3(result);
91 }
92
93 void operator*=(double scalar) {
94 _content[0] = _content[0] * scalar;
95 _content[1] = _content[1] * scalar;
96 _content[2] = _content[2] * scalar;
97 }
98 type MaxNorm() const {
99 type norm;
100 norm = std::max(abs(_content[0]), abs(_content[1]));
101 norm = std::max(norm, abs(_content[2]));
102 return norm;
103 }
104
105 type L2NormSquare() const {
106 return _content[0] * _content[0] + _content[1] * _content[1] + _content[2] * _content[2];
107 }
108
109 double L2Norm() const {
110 return sqrt(L2NormSquare());
111 }
112
113 Vector3& operator=(const Vector3& rhs) {
114 if (this != &rhs) {
115 _content[0] = rhs._content[0];
116 _content[1] = rhs._content[1];
117 _content[2] = rhs._content[2];
118 }
119 return *this;
120 }
121
122 Vector3& operator=(type rhs) {
123 _content[0] = rhs;
124 _content[1] = rhs;
125 _content[2] = rhs;
126 return *this;
127 }
128
129 type& operator[](int i) {
130 return _content[i];
131 }
132
133 type operator[](int i) const {
134 return _content[i];
135 }
136
137 bool operator==(const Vector3& rhs) const {
138 if (_content[0] != rhs._content[0])
139 return false;
140 if (_content[1] != rhs._content[1])
141 return false;
142 if (_content[2] != rhs._content[2])
143 return false;
144 return true;
145 }
146
147 ~~Vector3() {
148 }
149
150 const type * data() const {
151 return _content;
152 }
153
154 friend std::ostream& operator<<<type>(std::ostream& stream, const Vector3& v);
155// friend Vector3 operator*<type>(double scalar, const Vector3& v);
156
157 /* used in std::map, returns true if lhs < rhs */
158 class compare {
159 public:
160 bool operator()(const Vector3& lhs, const Vector3& rhs) const {
161 return lhs[0] < rhs[0]
162 || ( lhs[0] == rhs[0] && ( lhs[1] < rhs[1]
163 || ( lhs[1] == rhs[1] && lhs[2] < rhs[2])));
164 }
165 };
166
167private:
168 type _content[3];
169};
170
171} /* namespace bhfmm */
172
173#endif /* VECTOR3_H_ */
Definition: Vector3.h:158
Definition: Vector3.h:34
Enumeration class corresponding to the type schema type.
Definition: vtk-unstructured.h:1746
Definition: L2PCellProcessor.cpp:15
SolidHarmonicsExpansion operator*(double scalar, SolidHarmonicsExpansion RHS)
Definition: SolidHarmonicsExpansion.cpp:67
std::ostream & operator<<(std::ostream &stream, const Vector3< type > &v)
Definition: Vector3.h:22