32 template <
class T,
unsigned int N>
40 Array(
const T locs[N]);
41 const T&
get(
const unsigned int n)
const {
return vals[n];}
42 void set(
const unsigned int n,
const T a){vals[n]=a;}
43 unsigned int len()
const {
return N;};
46 const T&
operator[](
const unsigned int i)
const {
return vals[i];};
49 T*
end(){
return vals + N;};
63 template <
class T,
unsigned int N>
73 const T&
get(
const unsigned int n)
const {
return vals[n];}
74 void set(
const unsigned int n,
const T a){vals[n]=a;}
75 unsigned int len()
const {
return N;};
79 template <
class U>
NVector& operator*=(
const U rhs);
80 template <
class U>
NVector& operator/=(
const U rhs);
85 const T&
operator[](
const unsigned int i)
const{
return vals[i];};
92 T*
end(){
return vals + N;};
95 template <
class U,
unsigned int M>
96 friend ostream& operator<<(ostream& out, const NVector<U, M> v);
106 template <
class T,
unsigned int N>
109 inline NumVector() {
for(
unsigned int i=0; i<N; i++) NVector<T,N>::vals[i]=0;}
111 for(
unsigned int i=0; i<N; i++) NVector<T,N>::vals[i]=rhs.
get(i);}
113 for(
unsigned int i=0; i<N; i++) NVector<T,N>::vals[i]=rhs[i];}
116 inline T
sq()
const {
return dot(*
this);};
117 inline T
mag()
const {
return sqrt(sq());};
122 inline T distance(
const NumVector &rhs)
const;
138 template <
class U,
unsigned int M>
139 friend ostream& operator<<(ostream& out, const NumVector<U, M> v);
155 Vector3(
const T a,
const T b,
const T c) {setx(a); sety(b); setz(c);}
172 inline void set(
const T a,
const T b,
const T c){
175 return Vector3(-getx(),-gety(),-getz());}
181 return (getx()*rhs.
getx() + gety()*rhs.
gety() + getz()*rhs.
getz());}
183 return Vector3(getx()*rhs,gety()*rhs,getz()*rhs);}
185 return Vector3(getx()/rhs,gety()/rhs,getz()/rhs);}
190 template <
class U>
Vector3& operator*=(
const U rhs);
191 template <
class U>
Vector3& operator/=(
const U rhs);
201 return acos(dx1.
dot(dx2) / dx1.
mag() / dx2.
mag());
211 Vector3 dx1 = x1 - x2, dx2 = x3 - x2;
212 return acos(dx1.
dot(dx2) / dx1.
mag() / dx2.mag());
239 Vector3 dx1 = x2 - x1, dx2 = x3 - x2, dx3 = x4 - x3;
240 return atan2(dx1.
dot(dx2.cross(dx3))*dx2.mag(),
241 (dx1.
cross(dx2).
dot(dx2.cross(dx3))));
246 friend ostream& operator<<(ostream& out, const Vector3<U> v);
263 Vector2(
const T a,
const T b) {setx(a); sety(b);}
274 inline void set(
const T a,
const T b){
277 return Vector2(-getx(),-gety());}
283 return (getx()*rhs.
getx() + gety()*rhs.
gety());}
285 return Vector2(getx()*rhs,gety()*rhs);}
287 return Vector2(getx()/rhs,gety()/rhs);}
298 template <
class U>
Vector2& operator*=(
const U rhs);
299 template <
class U>
Vector2& operator/=(
const U rhs);
312 if((i / 4) % 2 == 1)
return flip().rotate(i%4);
321 if((i / 4) % 2 == 0)
return inv;
332 return acos(dx1.
dot(dx2) / dx1.
mag() / dx2.
mag());
342 Vector2 dx1 = x1 - x2, dx2 = x3 - x2;
343 return acos(dx1.
dot(dx2) / dx1.
mag() / dx2.mag());
348 friend ostream& operator<<(ostream& out, const Vector2<U> v);
371 return 2*M[0][1]*M[0][2]*M[1][2] + M[0][0]*M[1][1]*M[2][2]
372 - M[0][2]*M[0][2]*M[1][1] - M[0][0]*M[1][2]*M[1][2]
373 - M[0][1]*M[0][1]*M[2][2];
381 I[0][0] = (M[1][1]*M[2][2]-M[1][2]*M[1][2]) / d;
382 I[0][1] = (M[0][2]*M[1][2]-M[0][1]*M[2][2]) / d;
384 I[0][2] = (M[0][1]*M[1][2]-M[0][2]*M[1][1]) / d;
386 I[1][1] = (M[0][0]*M[2][2]-M[0][2]*M[0][2]) / d;
387 I[1][2] = (M[0][1]*M[0][2]-M[0][0]*M[1][2]) / d;
389 I[2][2] = (M[0][0]*M[1][1]-M[0][1]*M[0][1]) / d;
395 return Vector3<C>(this->
get(0).dot(vec),this->
get(1).dot(vec),this->
get(2).dot(vec));
398 template <
class T,
unsigned int N>
400 for(
unsigned int i=0; i < N; i++){
401 vals[i] = rhs.
get(i);
405 template <
class T,
unsigned int N>
408 template <
class T,
unsigned int N>
410 for(
unsigned int i=0; i < N; i++) vals[i] = locs[i];
413 template <
class T,
unsigned int N>
template<
class U>
415 for(
unsigned int i=0; i < N; i++){
416 vals[i] = T(rhs.
get(i));
420 template <
class T,
unsigned int N>
422 for(
unsigned int i=0; i < N; i++){
423 vals[i] = rhs.
get(i);
427 template <
class T,
unsigned int N>
430 template <
class T,
unsigned int N>
432 for(
unsigned int i=0; i < N; i++) vals[i] = locs[i];
435 template <
class T,
unsigned int N>
template<
class U>
437 for(
unsigned int i=0; i < N; i++){
438 vals[i] = T(rhs.
get(i));
442 template <
class T,
unsigned int N>
449 template <
class T,
unsigned int N>
451 for(
unsigned int i=0; i < N; i++){
452 vals[i] -= rhs.
get(i);
457 template <
class T,
unsigned int N>
459 for(
unsigned int i=0; i < N; i++){
460 vals[i] += rhs.
get(i);
465 template <
class T,
unsigned int N>
template <
class U>
467 for(
unsigned int i=0; i<N; i++){
468 vals[i] = vals[i] * rhs;
474 template <
class T,
unsigned int N>
template <
class U>
476 for(
unsigned int i=0; i<N; i++){
477 vals[i] = T(vals[i] / rhs);
482 template <
class U,
unsigned int M>
483 ostream& operator<< (ostream& out, const NVector<U,M> &v){
484 out <<
"[" << v.get(0);
485 for(
unsigned int i = 1; i < M; i++)
486 out <<
',' << v.get(i);
490 template <
class T,
unsigned int N>
493 for(
unsigned int i=0; i<N; i++){
499 template <
class T,
unsigned int N>
502 return (*
this) - other * ((dot(other)) / other.
sq());
506 template <
class T,
unsigned int N>
512 template <
class T,
unsigned int N>
519 template <
class T,
unsigned int N>
522 for(
unsigned int i=0; i<N; i++){
531 double x=
get(0) - rhs.
get(0), y=
get(1) - rhs.
get(1);
538 long double x=
get(0) - rhs.
get(0), y=
get(1) - rhs.
get(1);
545 double x=
get(0)-rhs.
get(0), y=
get(1)-rhs.
get(1), z=
get(2)-rhs.
get(2);
546 return sqrt(x*x + y*y + z*z);
552 long double x=
get(0)-rhs.
get(0), y=
get(1)-rhs.
get(1), z=
get(2)-rhs.
get(2);
553 return sqrtl(x*x + y*y + z*z);
559 double x=
get(0), y=
get(1);
566 long double x=
get(0), y=
get(1);
573 double x=
get(0), y=
get(1), z=
get(2);
574 return sqrt(x*x + y*y + z*z);
580 long double x=
get(0), y=
get(1), z=
get(2);
581 return sqrtl(x*x + y*y + z*z);
585 template <
class U,
unsigned int M>
586 ostream& operator<< (ostream& out, const NumVector<U,M> &v){
587 out <<
"[" << v.get(0);
588 for(
int i = 1; i < M; i++)
589 out <<
',' << v.get(i);
595 T newx = gety()*rhs.
getz() - rhs.
gety()*getz();
596 T newy = getz()*rhs.
getx() - rhs.
getz()*getx();
597 T newz = getx()*rhs.
gety() - rhs.
getx()*gety();
601 template <
class T>
template <
class U>
609 template <
class T>
template <
class U>
618 ostream& operator<< (ostream& out, const Vector3<U> v){
619 out <<
"{" << v.
get(0);
620 for(
int i = 1; i < 3; i++)
621 out <<
',' << v.get(i);
625 template <
class T>
template <
class U>
632 template <
class T>
template <
class U>
641 if(i % 4 == 0)
return (*
this);
642 else if(i % 4 == 3)
return Vector2(gety(), -getx());
643 else if(i % 4 == 2)
return Vector2(-getx(), -gety());
644 else return Vector2(-gety(), getx());
648 ostream& operator<< (ostream& out, const Vector2<U> v){
649 out <<
"{" << v.get(0);
650 for(
int i = 1; i < 2; i++)
651 out <<
',' << v.get(i);
void setyd(const double b)
Definition: vec.hpp:170
Vec perpto(Vec r, Vec to)
Definition: vecrand.hpp:85
Vector2(const NVector< T, 2 > rhs)
Definition: vec.hpp:265
NumVector perpto(const NumVector &other) const
Definition: vec.hpp:500
T operator*(const Vector2 &rhs) const
Definition: vec.hpp:282
Vec vec()
A one-liner for creating a Vec object, occasionally useful from within Python.
Definition: vecrand.hpp:72
C det() const
The determinant.
Definition: vec.hpp:369
NumVector(const T rhs[N])
Definition: vec.hpp:112
~Vector3()
Definition: vec.hpp:243
void setz(const T c)
Definition: vec.hpp:167
A fixed size array.
Definition: vec.hpp:33
static T angle(const Vector3 &dx1, const Vector3 &dx2)
The angle between two vectors, assuming they start at the same point (i.e. the origin).
Definition: vec.hpp:200
Vector3 & operator+=(const Vector3 &rhs)
Definition: vec.hpp:189
Vector2 flip()
Definition: vec.hpp:304
unsigned int len() const
Definition: vec.hpp:75
Vector2 & operator-=(const Vector2 &rhs)
Definition: vec.hpp:296
NVector()
Definition: vec.hpp:428
Vector3 & operator-=(const Vector3 &rhs)
Definition: vec.hpp:188
const T & get(const unsigned int n) const
Definition: vec.hpp:41
void setx(const T a)
Definition: vec.hpp:270
unsigned int uint
Definition: vec.hpp:20
Array()
Definition: vec.hpp:406
Vector2 operator/(const U rhs) const
Definition: vec.hpp:286
Vector3 operator-(const Vector3 &rhs) const
Definition: vec.hpp:178
void normalize()
Normalize in place.
Definition: vec.hpp:507
Vector3 norm() const
Definition: vec.hpp:187
T sq() const
Definition: vec.hpp:116
Vector2 operator-() const
Definition: vec.hpp:276
double getxd() const
Return x as a double. Useful with some versions of Python and long doubles.
Definition: vec.hpp:162
Vector3 operator/(const U rhs) const
Definition: vec.hpp:184
T * begin()
Definition: vec.hpp:48
Vec2 flip(Vec2 v)
Definition: vecrand.hpp:98
T distance(const NumVector &rhs) const
The magnitude of the vector, .
Definition: vec.hpp:520
Vector3 & operator/=(const U rhs)
T & operator[](const unsigned int i)
Definition: vec.hpp:45
Vector3(const NVector< T, 3 > rhs)
Definition: vec.hpp:157
An N-dimensional vector, extending addition and subtraction from the type T to the NVector class...
Definition: vec.hpp:64
T * end()
Definition: vec.hpp:49
Vector3 operator-() const
Definition: vec.hpp:174
T & operator[](const unsigned int i)
Definition: vec.hpp:84
Vector2()
Definition: vec.hpp:262
static T angle(const Vector2 &x1, const Vector2 &x2, const Vector2 &x3)
The angle between three points.
Definition: vec.hpp:341
Vector2 rotate_flip(uint i)
Rotate and flip, for .
Definition: vec.hpp:311
Vector3()
Definition: vec.hpp:154
double getyd() const
Definition: vec.hpp:163
const T gety() const
Definition: vec.hpp:267
const T getx() const
Definition: vec.hpp:266
void sety(const T b)
Definition: vec.hpp:271
void set(const unsigned int n, const T a)
Definition: vec.hpp:74
NVector operator*(const U rhs) const
Multiplication by a scalar.
Definition: vec.hpp:88
T * end()
Definition: vec.hpp:92
T mag() const
The square of the vector, .
Definition: vec.hpp:117
T operator*(const Vector3 &rhs) const
Definition: vec.hpp:180
Vector3< C > dot(Vector3< C > v) const
Definition: vec.hpp:394
double getzd() const
Definition: vec.hpp:164
NumVector()
Definition: vec.hpp:109
void setxd(const double a)
Definition: vec.hpp:272
Vector3(const NumVector< T, 3 > rhs)
Definition: vec.hpp:156
Vector2 perp() const
The vector perpendicular to this one, in the clockwise direction.
Definition: vec.hpp:293
static T Dihedral(const Vector3 &dx1, const Vector3 &dx2, const Vector3 &dx3)
The Dihedral angle between three vectors:
Definition: vec.hpp:229
~NumVector()
Definition: vec.hpp:136
NVector & operator*=(const U rhs)
Vector2 operator*(const U rhs) const
Definition: vec.hpp:284
void set(const T a, const T b, const T c)
Definition: vec.hpp:172
T dot(const NumVector &other) const
Inner product.
Definition: vec.hpp:491
const T getx() const
Definition: vec.hpp:158
Vector2(const NumVector< T, 2 > rhs)
Definition: vec.hpp:264
const T & get(const unsigned int n) const
Definition: vec.hpp:73
Vector2(const T a, const T b)
Definition: vec.hpp:263
NVector operator/(const U rhs) const
Division by a scalar.
Definition: vec.hpp:90
NumVector(const NVector< T, N > &rhs)
Definition: vec.hpp:110
const T & operator[](const unsigned int i) const
Definition: vec.hpp:46
Matrix< C > SymmetricInverse() const
The inverse of a symmetric inverse.
Definition: vec.hpp:377
Vector2 operator-(const Vector2 &rhs) const
Definition: vec.hpp:280
void setxd(const double a)
Set x with a double. Useful with some versions of Python and long doubles.
Definition: vec.hpp:169
NVector operator-(const NVector &rhs) const
Definition: vec.hpp:83
static T Dihedral(const Vector3 &x1, const Vector3 &x2, const Vector3 &x3, const Vector3 &x4)
The Dihedral angle between four points.
Definition: vec.hpp:237
A 2D physics vector, with methods for adding, subtracting, dot product, etc.
Definition: vec.hpp:260
const T gety() const
Definition: vec.hpp:159
void set(const unsigned int n, const T a)
Definition: vec.hpp:42
const T & operator[](const unsigned int i) const
Definition: vec.hpp:85
void setzd(const double c)
Definition: vec.hpp:171
const T getz() const
Definition: vec.hpp:160
~Vector2()
Definition: vec.hpp:345
Vector3 cross(const Vector3 &rhs) const
Cross product, .
Definition: vec.hpp:594
static T angle(const Vector3 &x1, const Vector3 &x2, const Vector3 &x3)
The angle between three points.
Definition: vec.hpp:210
Vector2 operator+(const Vector2 &rhs) const
Definition: vec.hpp:278
Vector3 operator*(const U rhs) const
Definition: vec.hpp:182
Vector2 norm() const
The normalized version of this vector.
Definition: vec.hpp:295
A 3x3 matrix, with methods for adding, subtracting, dot product, etc.
Definition: vec.hpp:360
void setx(const T a)
Definition: vec.hpp:165
unsigned int len() const
Definition: vec.hpp:43
~Array()
Definition: vec.hpp:50
Vector3(const T a, const T b, const T c)
Definition: vec.hpp:155
~NVector()
Definition: vec.hpp:93
T * begin()
Definition: vec.hpp:91
Vector3 operator+(const Vector3 &rhs) const
Definition: vec.hpp:176
double getyd() const
Definition: vec.hpp:269
A 3D physics vector, with methods for adding, subtracting, dot product, etc.
Definition: vec.hpp:152
void sety(const T b)
Definition: vec.hpp:166
T cross(const Vector2 &rhs) const
The 2D cross product, returning a scalar.
Definition: vec.hpp:289
Vec2 rotate(Vec2 v, uint i)
Definition: vecrand.hpp:89
NVector operator+(const NVector &rhs) const
Definition: vec.hpp:82
NumVector norm() const
Return the normalized version.
Definition: vec.hpp:513
T * iterator
Definition: vec.hpp:68
Vector3 & operator*=(const U rhs)
NVector operator-() const
Definition: vec.hpp:443
Vector2 & operator*=(const U rhs)
Vector2 cross(const T v) const
The 2D cross product with the "missing" third dimension, returning a vector.
Definition: vec.hpp:291
void setyd(const double b)
Definition: vec.hpp:273
Vec3 cross(Vec3 v1, Vec3 v2)
Definition: vecrand.hpp:80
double getxd() const
Definition: vec.hpp:268
Vector2 rotate_flip_inv(uint i)
The inverse of rotate_flip(i).
Definition: vec.hpp:319
Vector2 rotate(uint i)
Rotate by 90 degrees counter-clockwise.
Definition: vec.hpp:640
An N-dimensional physics vector, extending NVector.
Definition: vec.hpp:107
NVector & operator+=(const NVector &rhs)
Definition: vec.hpp:458
Vector2 & operator/=(const U rhs)
void set(const T a, const T b)
Definition: vec.hpp:274
NVector & operator-=(const NVector &rhs)
Definition: vec.hpp:450
NVector & operator/=(const U rhs)
Vector2 & operator+=(const Vector2 &rhs)
Definition: vec.hpp:297
static T angle(const Vector2 &dx1, const Vector2 &dx2)
The angle between two vectors, assuming they start at the same point (i.e. the origin).
Definition: vec.hpp:331