20 #ifndef JDFTX_CORE_VECTOR3_H 21 #define JDFTX_CORE_VECTOR3_H 26 #include <core/scalar.h> 30 #define LOOP3(code) { for(int k=0; k<3; k++) { code } } 33 template<
typename scalar=
double>
class vector3 38 __hostanddev__ scalar& operator[](
int k) {
return v[k]; }
39 __hostanddev__
const scalar& operator[](
int k)
const {
return v[k]; }
40 __hostanddev__ scalar& x() {
return v[0]; }
41 __hostanddev__ scalar& y() {
return v[1]; }
42 __hostanddev__ scalar& z() {
return v[2]; }
43 __hostanddev__
const scalar& x()
const {
return v[0]; }
44 __hostanddev__
const scalar& y()
const {
return v[1]; }
45 __hostanddev__
const scalar& z()
const {
return v[2]; }
48 __hostanddev__
explicit vector3(scalar a=0, scalar b=0, scalar c=0) { v[0]=a; v[1]=b; v[2]=c; }
49 vector3(std::vector<scalar> a) { LOOP3(v[k]=a[k];) }
53 __hostanddev__
vector3 operator+(
const vector3 &a)
const {
return vector3(v[0]+a[0], v[1]+a[1], v[2]+a[2]); }
54 __hostanddev__
vector3 operator+=(
const vector3 &a) { LOOP3( v[k]+=a[k]; )
return *
this; }
55 __hostanddev__
vector3 operator+(
const scalar a)
const {
return vector3(v[0]+a, v[1]+a, v[2]+a); }
56 __hostanddev__
vector3 operator+=(
const scalar a) { LOOP3( v[k]+=a; )
return *
this; }
58 __hostanddev__
vector3 operator-()
const {
return vector3(-v[0],-v[1],-v[2]); }
59 __hostanddev__
vector3 operator-(
const vector3 &a)
const {
return vector3(v[0]-a[0], v[1]-a[1], v[2]-a[2]); }
60 __hostanddev__
vector3 operator-=(
const vector3 &a) { LOOP3( v[k]-=a[k]; )
return *
this; }
62 __hostanddev__
vector3 operator/(scalar s)
const {
return (*
this)*(1.0/s); }
63 __hostanddev__
vector3& operator/=(scalar s) {
return (*
this)*=(1.0/s); }
65 __hostanddev__ scalar length_squared()
const {
return v[0]*v[0] + v[1]*v[1] + v[2]*v[2]; }
66 __hostanddev__ scalar length()
const {
return sqrt(length_squared()); }
68 void print(FILE* fp,
const char *format)
const { std::fprintf(fp,
"[ "); LOOP3( fprintf(fp, format, v[k]); ) std::fprintf(fp,
" ]\n"); }
69 __hostanddev__
bool operator==(
const vector3& w)
const { LOOP3(
if(v[k] != w[k])
return false; )
return true; }
70 __hostanddev__
bool operator<(
const vector3& w)
const { LOOP3(
if(v[k]!=w[k])
return v[k]<w[k]; )
return false; }
85 __hostanddev__
vector3<>& operator*=(
vector3<>& a,
double s) { LOOP3(a[k]*=s;)
return a; }
86 __hostanddev__
vector3<>& operator*=(
vector3<>& a,
int s) { LOOP3(a[k]*=s;)
return a; }
106 template<
typename scalar> __hostanddev__ scalar
dot(
const vector3<int>& a,
const vector3<scalar>& b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; }
108 template<
typename scalar> __hostanddev__ scalar
dot(
const vector3<scalar>& a,
const vector3<int>& b) {
return a[0]*b[0] + a[1]*b[1] + a[2]*b[2]; }
117 a[1]*b[2] - a[2]*b[1],
118 a[2]*b[0] - a[0]*b[2],
119 a[0]*b[1] - a[1]*b[0] );
130 LOOP3( out[k] =
round(v[k]); )
131 if(err) *err = (v+(-out)).length();
139 {
return (cis(2*M_PI*a[0]) - cis(2*M_PI*b[0])).norm()
140 + (cis(2*M_PI*a[1]) - cis(2*M_PI*b[1])).norm()
141 + (cis(2*M_PI*a[2]) - cis(2*M_PI*b[2])).norm();
146 template<
typename T> T
gcd(T x, T y)
157 { T g =
gcd(
gcd(d[0], d[1]), d[2]);
172 { LOOP3( vArr[k][i] = v[k]; )
176 { LOOP3( vArr[k][i] += v[k]; )
ScalarField sqrt(const ScalarField &)
Elementwise square root (preserve input)
complex dot(const Tptr &X, const Tptr &Y)
Definition: Operators.h:196
__hostanddev__ vector3< scalar > cross(const vector3< scalar > &a, const vector3< scalar > &b)
cross product
Definition: vector3.h:115
vector3< T > gcdReduce(const vector3< T > &d)
Reduce an integer vector by its gcd.
Definition: vector3.h:156
T gcd(T x, T y)
GCD of integers, templated over integer types.
Definition: vector3.h:146
__hostanddev__ void storeVector(const vector3< scalar > &v, vector3< scalar * > &vArr, int i)
Store vector to a vector field.
Definition: vector3.h:171
__hostanddev__ void accumVector(const vector3< scalar > &v, vector3< scalar * > &vArr, int i)
Accumulate vector onto a vector field.
Definition: vector3.h:175
__hostanddev__ scalar box(const vector3< scalar > &a, const vector3< scalar > &b, const vector3< scalar > &c)
box product / triple product
Definition: vector3.h:123
__hostanddev__ vector3< scalar > loadVector(const vector3< const scalar * > &vArr, int i)
Load vector from a constant vector field.
Definition: vector3.h:163
__hostanddev__ vector3< int > round(const vector3<> &v, double *err=0)
Round vector3<> to vector3<int> (and optionally retrieve error)
Definition: vector3.h:128
__hostanddev__ double circDistanceSquared(const vector3<> &a, const vector3<> &b)
Definition: vector3.h:138
Complex number (need to define our own because we need operators for gpu code as well) ...
Definition: scalar.h:49
Generic 3-vector.
Definition: vector3.h:33