-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGD.cpp
More file actions
100 lines (68 loc) · 2.05 KB
/
Copy pathGD.cpp
File metadata and controls
100 lines (68 loc) · 2.05 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
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
double maxAbs(double arr[], int arrSize){
double m = std::abs(arr[0]);
for(int i = 1; i < arrSize; i++){
if(std::abs(arr[i]) > m){
m = std::abs(arr[i]);
}
}
return m;
}
std::vector<double> scalMalt(double arr[], int arrSize, double c){
std::vector<double> ans(arr, arr + arrSize);
for(int i = 0; i < arrSize; i++){
ans.data()[i] *= c;
}
return ans;
}
std::vector<double> subtract(double arr1[], double arr2[], int arrSize){
std::vector<double> ans(arrSize);
for(int i = 0; i < arrSize; i++){
ans[i] = arr1[i] - arr2[i];
}
return ans;
}
std::vector<double> grad(double (*func)(double*), double arr[], int arrSize){
double h = 1e-6;
std::vector<double> ans(arrSize);
std::vector<double> tempLeft(arr, arr + arrSize);
std::vector<double> tempRight(arr, arr + arrSize);
for(int i = 0; i < arrSize; i++){
tempLeft[i] += h;
tempRight[i] -= h;
double di = (func(tempLeft.data()) - func(tempRight.data())) / (2 * h);
ans[i] = di;
tempLeft[i] -= h;
tempRight[i] += h;
}
return ans;
}
std::vector<double> GD(double (*func)(double*), double startP[], int arrSize, double step, double tol=1e-6, int maxIter=10000){
std::vector<double> ans(startP, startP + arrSize);
int i = 0;
while(i < maxIter){
std::vector<double> vec = grad(func, ans.data(), arrSize);
if(maxAbs(vec.data(), arrSize) <= tol){
break;
}
vec = scalMalt(vec.data(), arrSize, step);
ans = subtract(ans.data(), vec.data(), arrSize);
i++;
}
return ans;
}
double f(double x[]){
double ans = pow(x[0], 2) + pow(x[1], 4) + 5 * x[0] - x[1];
return ans;
}
int main(){
double vec0[] = {1.4, 6.78};
double vec1[] = {3.53, 2.65};
double scal = 1.33;
std::vector<double> res = GD(f, vec1, 2, 0.01);
std::cout << "[" << res[0] << ", " << res[1] << "]\n";
return 0;
}