-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv.h
44 lines (38 loc) · 1.15 KB
/
v.h
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
#pragma once
#include<vector>
#include<assert.h>
typedef std::vector<float> Vector;
namespace v{
template <class Vector1,class Vector2> inline float dot(const Vector1& x,const Vector2& y){
int m = x.size();
const float *xd = x.data(), *yd = y.data();
float sum = 0.0;
while( --m >= 0 ) sum += (*xd++ ) * (*yd++);
return sum;
}
// x = x + g * y
inline void saxpy(Vector &x,float g, const Vector& y){
assert( x.size() == y.size());
int m = x.size();
float *xd = x.data();
const float *yd = y.data();
while( --m >= 0){
*xd += g * (*yd); ++xd; ++yd;
}
}
// x = a * x + g * y
inline void saxpy(float a,Vector& x,float g, const Vector &y){
assert(x.size() == y.size());
int m = x.size();
float *xd = x.data();
const float *yd = y.data();
while( --m >=0 ){ (*xd) =a * (*xd) + g * (*yd); ++xd;++yd;}
}
inline void unit(Vector &x){
float len = ::sqrt(dot(x,x));
if(len == 0 ) return;
int m = x.size();
float *xd = x.data();
while(--m >= 0 ) (*xd++) /= len;
}
}