-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.cpp
More file actions
107 lines (90 loc) · 2.23 KB
/
numeric.cpp
File metadata and controls
107 lines (90 loc) · 2.23 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "types.h"
#include <algorithm>
#define EPSILON 1e-12
#define min(a, b) (a<=b?a:b)
void AddArray(Array &a, Array &b, Array &c) {
//assert(a.size() == b.size());
int N = min(a.size(), b.size());
for (int i = 0; i < N; i++) {
c[i] = a[i] + b[i];
}
}
void AddArray(Array &a, Array &b) {
//assert(a.size() == b.size());
int N = min(a.size(), b.size());
for (int i = 0; i < N; i++) {
a[i] = a[i] + b[i];
}
}
void SquareArray(Array &a) {
for (size_t i = 0; i < a.size(); i++) {
a[i] = a[i] * a[i];
}
}
void SubtractArray(const Array &a, const Array &b, Array &c) {
//assert(a.size() == b.size());
int N = min(a.size(), b.size());
for (int i = 0; i < N; i++) {
c[i] = a[i] - b[i];
}
}
void SubtractArray(Array &a, const Array &b) {
//assert(a.size() == b.size());
int N = min(a.size(), b.size());
for (int i = 0; i < N; i++) {
a[i] = a[i] - b[i];
}
}
void ElementDivide(const Array &a, const Array &b, Array &c) {
//assert(a.size() == b.size());
int N = min(a.size(), b.size());
for (int i = 0; i < N; i++) {
if(b[i]>EPSILON) {
c[i] = a[i] / b[i];
} else {
c[i] = 1.0;
}
}
}
Array& zeroArray(int length) {
static Array zeros;
zeros.resize(length);
return zeros;
}
Array& unitArray(int length) {
static Array ones;
ones.resize(length, 1.0);
return ones;
}
void zero_array(Array &arr) {
for (unsigned int i = 0; i < arr.size(); i++) {
arr[i] = 0;
}
}
void zero_matrix(Matrix &mat) {
for (unsigned int i = 0; i < mat.size(); i++) {
for (unsigned int j = 0; j < mat[i].size(); j++) {
mat[i][j] = 0;
}
}
}
int findMinimaIndex(const Array &arr) {
double min = arr[0];
//cout<<"find_minima: "<<clusterPruningError[0]<<", ";
int min_index = 0;
for (size_t i = 1; i < arr.size(); i++) {
//cout<<clusterPruningError[i]<<", ";
if (arr[i] < min) {
min = arr[i];
min_index = i;
}
}
return min_index;
}
int median(vector<int> v) {
int m = -1;
int mid = v.size()/2;
//cout<<mid<<endl;
std::sort(v.begin(), v.end());
return v[mid];
}