This repository has been archived by the owner on Jun 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.hpp
73 lines (64 loc) · 2.07 KB
/
misc.hpp
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
//
// misc.hpp
// plink
//
// Created by Shing Wan Choi on 18/08/2016.
// Copyright © 2016 Shing Wan Choi. All rights reserved.
//
#ifndef misc_hpp
#define misc_hpp
#include <stdio.h>
#include <stdexcept>
#include <cmath>
#define _USE_MATH_DEFINES
#include <math.h>
#include <limits>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
namespace misc{
//Functions from R
double dnorm(double x, double mu=0.0, double sigma=1.0, bool log=false);
double qnorm(double p, double mu=0.0, double sigma=1.0, bool lower_tail=true, bool log_p=false);
// codes from stackoverflow
std::vector<std::string> split(const std::string seq, const std::string separators="\t ");
template <typename T> inline
T convert(const std::string& str){
std::istringstream iss(str);
T obj;
iss >> std::ws >> obj >> std::ws;
if(!iss.eof())
throw std::runtime_error("Unable to convert the input");
return obj;
}
// trim from start (in place)
void ltrim(std::string &s);
// trim from end (in place)
void rtrim(std::string &s);
// trim from both ends (in place)
void trim(std::string &s);
// trim from start (copying)
std::string ltrimmed(std::string s);
// trim from end (copying)
std::string rtrimmed(std::string s);
// trim from both ends (copying)
std::string trimmed(std::string s);
/*!
* \brief Function to check if two double are equal from
* https://stackoverflow.com/a/4010279/1441789
* \param a the first double
* \param b the second double
* \param error_factor level of error, should be of no concern to us at the
* moment
* \return True if two double are equal
*/
inline bool logically_equal(double a, double b, double error_factor = 1.0)
{
return ((a == b)
|| (std::abs(a - b) < std::abs(std::min(a, b))
* std::numeric_limits<double>::epsilon()
* error_factor));
}
}
#endif /* misc_hpp */