-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.cpp
56 lines (48 loc) · 1.55 KB
/
utils.cpp
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
#include <math.h>
#include <vector>
#include <iostream>
#include <fstream> // std::ifstream
#include <sstream> // std::istringstream
#include <algorithm> // std::replace
// custom headers
#include "utils.h"
using std::vector;
using std::string;
using std::stod;
vector<double> utils::Delta(vector<double> dF){
double sum;
for (auto& n : dF)
sum += pow(n,2);
sum=sqrt(sum);
for(vector<double>::iterator it=dF.begin(); it!=dF.end(); ++it){
*it=-(*it)/sum;
}
return dF;
}
std::unordered_map<string, double> utils::cfg_parsing(string cfg_filename){
std::unordered_map<string, double> param_map;
string line;
string key;
string value;
std::ifstream filestream(cfg_filename);
if (filestream.is_open()) {
while (std::getline(filestream, line)) {
std::replace(line.begin(), line.end(), '=', ' ');
std::istringstream linestream(line);
while (linestream >> key >> value){
if (key == "x0"){
std::replace(value.begin(), value.end(), ',', ' ');
std::istringstream valuestream(value);
string x_value1, x_value2;
while (valuestream >> x_value1 >> x_value2){
param_map["x0:1"]=stod(x_value1);
param_map["x0:2"]=stod(x_value2);
}
}
else
param_map[key]=stod(value);
}
}
}
return param_map;
}