Loading...
Searching...
No Matches
Vec.h
Go to the documentation of this file.
1#ifndef __VEC_H__
2#define __VEC_H__
3
4#include <iostream>
5using std::istream;
6using std::ostream;
7// class istream;
8// class ostream;
9
11
17
18class Vec {
19public:
21 Vec() {};
23 Vec(double x0, double x1, double x2);
25 double operator*(const Vec &v) const;
27 Vec operator*(const double &s) const;
29 Vec operator+(const Vec &v) const;
31 Vec operator-(const Vec &v) const;
33 Vec operator-() const;
35 Vec &operator+=(const Vec &v);
37 Vec &operator-=(const Vec &v);
39 Vec &operator*=(double s);
41 Vec &operator/=(double s);
43 double operator[](int n) const;
45 double &operator[](int n);
47 friend Vec Cross(const Vec &v1, const Vec &v2);
49 friend double Length2(const Vec &v);
51 friend void Vaxpy(double a, const Vec &x, Vec &y);
53 friend ostream &operator<<(ostream &out, const Vec &v);
55 friend istream &operator>>(istream &in, Vec &v);
56
57public:
58 double x[3];
59};
60
61inline Vec::Vec(double x0, double x1, double x2) {
62 x[0] = x0;
63 x[1] = x1;
64 x[2] = x2;
65}
66
67inline double Vec::operator*(const Vec &v) const {
68 return x[0] * v.x[0] + x[1] * v.x[1] + x[2] * v.x[2];
69}
70
71inline Vec Vec::operator*(const double &s) const {
72 return Vec(s * x[0], s * x[1], s * x[2]);
73}
74
75inline Vec Vec::operator+(const Vec &v) const {
76 return Vec(x[0] + v.x[0], x[1] + v.x[1], x[2] + v.x[2]);
77}
78
79inline Vec Vec::operator-(const Vec &v) const {
80 return Vec(x[0] - v.x[0], x[1] - v.x[1], x[2] - v.x[2]);
81}
82
83inline Vec Vec::operator-() const { return Vec(-x[0], -x[1], -x[2]); }
84
85inline Vec &Vec::operator+=(const Vec &v) {
86 x[0] += v.x[0];
87 x[1] += v.x[1];
88 x[2] += v.x[2];
89 return *this;
90}
91
92inline Vec &Vec::operator-=(const Vec &v) {
93 x[0] -= v.x[0];
94 x[1] -= v.x[1];
95 x[2] -= v.x[2];
96 return *this;
97}
98
99inline Vec &Vec::operator*=(double s) {
100 x[0] *= s;
101 x[1] *= s;
102 x[2] *= s;
103 return *this;
104}
105
106inline Vec &Vec::operator/=(double s) {
107 x[0] /= s;
108 x[1] /= s;
109 x[2] /= s;
110 return *this;
111}
112
113inline double Vec::operator[](int n) const { return x[n]; }
114
115inline double &Vec::operator[](int n) { return x[n]; }
116
117inline Vec Cross(const Vec &v1, const Vec &v2) {
118 return Vec(v1.x[1] * v2.x[2] - v1.x[2] * v2.x[1],
119 v1.x[2] * v2.x[0] - v1.x[0] * v2.x[2],
120 v1.x[0] * v2.x[1] - v1.x[1] * v2.x[0]);
121}
122
123inline void Vaxpy(double a, const Vec &x, Vec &y) {
124 y.x[0] += a * x.x[0];
125 y.x[1] += a * x.x[1];
126 y.x[2] += a * x.x[2];
127}
128
129inline double Length2(const Vec &v) {
130 return v.x[0] * v.x[0] + v.x[1] * v.x[1] + v.x[2] * v.x[2];
131}
132
133#endif // __VEC_H__
double Length2(const Vec &v)
Definition Vec.h:129
void Vaxpy(double a, const Vec &x, Vec &y)
Definition Vec.h:123
Vec Cross(const Vec &v1, const Vec &v2)
Definition Vec.h:117
A 3-vector useful for postions etc.
Definition Vec.h:18
friend double Length2(const Vec &v)
The length of a Vec.
Definition Vec.h:129
Vec & operator-=(const Vec &v)
Subtract a vec from this one.
Definition Vec.h:92
Vec operator+(const Vec &v) const
Add two Vecs.
Definition Vec.h:75
friend ostream & operator<<(ostream &out, const Vec &v)
Print a Vec.
friend void Vaxpy(double a, const Vec &x, Vec &y)
Increment y with a times x.
Definition Vec.h:123
Vec & operator*=(double s)
Multiply this vec with a scalar.
Definition Vec.h:99
friend Vec Cross(const Vec &v1, const Vec &v2)
Cross product of two Vecs.
Definition Vec.h:117
Vec operator-() const
Unary minus.
Definition Vec.h:83
Vec & operator/=(double s)
Divide this Vec with a scalar.
Definition Vec.h:106
Vec & operator+=(const Vec &v)
Add a Vec to this one.
Definition Vec.h:85
friend istream & operator>>(istream &in, Vec &v)
Read a Vec.
double operator[](int n) const
const indexing
Definition Vec.h:113
double operator*(const Vec &v) const
Dot product.
Definition Vec.h:67
Vec()
Dummy constructor needed by STL containers.
Definition Vec.h:21
double x[3]
The actual data.
Definition Vec.h:58