forked from GeorgyBorisochev/PNM-Generator-and-Solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
102 lines (88 loc) · 2.36 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
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
//utils.cpp
//various functions for io etc
#include "utils.h"
#include "framework.h"
#include "CSVRead.h"
//
//UTILS
//
bool classUtils::is_number(const std::string& s)
{
char* end = nullptr;
double val = strtod(s.c_str(), &end);
return end != s.c_str() && *end == '\0' && val != HUGE_VAL;
}
void classUtils::writeLine(string string)
{
cout << string << "\n";
}
void classUtils::emptyLine()
{
cout << "\n";
}
std::vector<std::vector<string> > classUtils::transpose(const std::vector<std::vector<string> > data) {
// this assumes that all inner vectors have the same size and
// allocates space for the complete result in advance
std::vector<std::vector<string> > result(data[0].size(),
std::vector<string>(data.size()));
for (std::vector<string>::size_type i = 0; i < data[0].size(); i++)
for (std::vector<string>::size_type j = 0; j < data.size(); j++) {
result[i][j] = data[j][i];
}
return result;
}
double classUtils::Cardano_min(double a, double b, double c, double d)
{
double Pi = 3.1415926;
double p = (3 * a * c - pow(b, 2)) / 3 / pow(a, 2);
double q = 2 * pow(b, 3) / 27 / pow(a, 3) - b * c / 3 / pow(a, 2) + d / a;
double discrim = pow((q / 2), 2) + pow((p / 3), 3);
double cardano;
if (discrim < 0)
{
double phi = acos(-q / 2 / sqrt(pow((-p / 3), 3)));
double y1 = 2 * sqrt(-p / 3) * cos(phi / 3);
double y2 = 2 * sqrt(-p / 3) * cos(phi / 3 + 2 * Pi / 3);
double y3 = 2 * sqrt(-p / 3) * cos(phi / 3 + 4 * Pi / 3);
double x1 = y1 - b / 3 / a;
cardano = x1;
double x2 = y2 - b / 3 / a;
if (x2 < x1)
{
cardano = x2;
}
double x3 = y3 - b / 3 / a;
if (x3 < x2)
{
cardano = x3;
}
}
else if (discrim > 0)
{
double u = pow((-q / 2 + sqrt(discrim)), (1.0 / 3));
double v = pow((-q / 2 - sqrt(discrim)), (1.0 / 3));
double y1 = u + v;
double x1 = y1 - b / 3 / a;
cardano = x1;
}
else
{
double y1 = pow(-4 * q, 1.0 / 3);
double y2 = pow(q / 2, 1.0 / 3);
double x1 = y1 - b / 3 / a;
cardano = x1;
double x2 = y2 - b / 3 / a;
if (x2 < x1)
{
cardano = x2;
}
}
return cardano;
}
std::vector<std::vector<std::string> > classUtils::csvRead(string filePath)
{
CSVReader reader(filePath);
// Get the data from CSV File
std::vector<std::vector<std::string> > dataList = reader.getData();
return dataList;
}