forked from aroeszler/hcpusher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_ops.hpp
71 lines (62 loc) · 1.42 KB
/
vector_ops.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* Header file implementing simple arithmetic vector operations
* for std::vector<double> type.
**/
#pragma once
#include <vector>
#include <cassert>
#include <sstream>
using Vec3D = std::vector<double>;
std::string vec_print (const Vec3D& v)
{
std::stringstream ss;
ss << "[";
for( unsigned i = 0; i < v.size(); ++i )
{
if(i != 0)
ss << ", ";
ss << v[i];
}
ss << "]";
return ss.str();
}
Vec3D vec_add (const Vec3D& v1, const Vec3D& v2)
{
assert( v1.size() == v2.size() );
Vec3D vec_sum( v1.size() );
for ( unsigned i=0; i < v1.size(); ++i )
{
vec_sum[i] = v1[i] + v2[i];
}
return vec_sum;
}
Vec3D scalmultip (const Vec3D& v1, const double& d1)
{
assert( v1.size() == 3 );
Vec3D vec_scal( v1.size() );
for ( unsigned i=0; i < v1.size(); ++i )
{
vec_scal[i] = v1[i] * d1;
}
return vec_scal;
}
double innerprod (const Vec3D& v1, const Vec3D& v2)
{
assert( v1.size() == v2.size() );
double scal_prod = 0;
for ( unsigned i=0; i < v1.size(); ++i )
{
scal_prod += v1[i] * v2[i];
}
return scal_prod;
}
Vec3D crossprod (const Vec3D& v1, const Vec3D& v2)
{
assert( v1.size() == v2.size() );
assert( v1.size() == 3 );
Vec3D vec_prod( v1.size() );
vec_prod[0] = v1[1] * v2[2] - v1[2] * v2[1];
vec_prod[1] = v1[2] * v2[0] - v1[0] * v2[2];
vec_prod[2] = v1[0] * v2[1] - v1[1] * v2[0];
return vec_prod;
}