slope 0.29.0
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <Eigen/Core>
9#include <Eigen/SparseCore>
10#include <algorithm>
11#include <numeric>
12#include <set>
13#include <string>
14#include <unordered_set>
15#include <vector>
16
17namespace slope {
18
35template<typename T>
36void
37sort(T& v, const bool descending = false)
38{
39 if (descending) {
40 std::sort(v.data(), v.data() + v.size(), std::greater<double>());
41 } else {
42 std::sort(v.data(), v.data() + v.size(), std::less<double>());
43 }
44}
45
58template<typename T>
59std::vector<int>
60which(const T& x)
61{
62 std::vector<int> out;
63 for (int i = 0; i < x.size(); i++) {
64 if (x[i]) {
65 out.emplace_back(i);
66 }
67 }
68
69 return out;
70}
71
87template<typename T>
88std::vector<int>
89sortIndex(T& v, const bool descending = false)
90{
91 using namespace std;
92
93 vector<int> idx(v.size());
94 iota(idx.begin(), idx.end(), 0);
95
96 if (descending) {
97 sort(idx.begin(), idx.end(), [&v](int i, int j) { return v[i] > v[j]; });
98 } else {
99 sort(idx.begin(), idx.end(), [&v](int i, int j) { return v[i] < v[j]; });
100 }
101
102 return idx;
103}
104
116template<typename T>
117void
118permute(T& values, const std::vector<int>& ind)
119{
123 T out(values.size());
124
128 for (int i = 0; i < values.size(); ++i)
129 out[i] = std::move(values[ind[i]]);
130
134 values = std::move(out);
135}
136
150template<typename T>
151void
152inversePermute(T& values, const std::vector<int>& ind)
153{
154 T out(values.size());
156 for (int i = 0; i < values.size(); ++i)
157 out[ind[i]] = std::move(values[i]);
158
159 values = std::move(out);
160}
161
174template<typename T>
175void
176move_elements(std::vector<T>& v, const int from, const int to, const int size)
177{
178 if (from > to) {
179 std::rotate(v.begin() + to, v.begin() + from, v.begin() + from + size);
180 } else {
181 std::rotate(v.begin() + from, v.begin() + from + size, v.begin() + to + 1);
182 }
183}
184
197void
198validateOption(const std::string& value,
199 const std::set<std::string>& valid_options,
200 const std::string& parameter_name);
201
213Eigen::MatrixXd
214subset(const Eigen::MatrixXd& x, const std::vector<int>& indices);
215
228Eigen::SparseMatrix<double>
229subset(const Eigen::SparseMatrix<double>& x, const std::vector<int>& indices);
230
241Eigen::MatrixXd
242subsetCols(const Eigen::MatrixXd& x, const std::vector<int>& indices);
243
256Eigen::SparseMatrix<double>
257subsetCols(const Eigen::SparseMatrix<double>& x,
258 const std::vector<int>& indices);
259
271inline std::unordered_set<double>
272unique(const Eigen::MatrixXd& x)
273{
274 std::unordered_set<double> unique;
275 for (Eigen::Index j = 0; j < x.cols(); j++) {
276 for (Eigen::Index i = 0; i < x.rows(); i++) {
277 unique.insert(x(i, j));
278 }
279 }
280
281 return unique;
282}
283
284} // namespace slope
Namespace containing SLOPE regression implementation.
Definition clusters.cpp:5
Eigen::MatrixXd subsetCols(const Eigen::MatrixXd &x, const std::vector< int > &indices)
Extract specified columns from a dense matrix.
Definition utils.cpp:54
std::unordered_set< double > unique(const Eigen::MatrixXd &x)
Create a set of unique values from an Eigen matrix.
Definition utils.h:272
void move_elements(std::vector< T > &v, const int from, const int to, const int size)
Definition utils.h:176
std::vector< int > which(const T &x)
Definition utils.h:60
void validateOption(const std::string &value, const std::set< std::string > &valid_options, const std::string &parameter_name)
Validates if a given value exists in a set of valid options.
Definition utils.cpp:7
Eigen::MatrixXd subset(const Eigen::MatrixXd &x, const std::vector< int > &indices)
Extract a subset of rows from an Eigen matrix.
Definition utils.cpp:26
std::vector< int > sortIndex(T &v, const bool descending=false)
Definition utils.h:89
void permute(T &values, const std::vector< int > &ind)
Definition utils.h:118
void sort(T &v, const bool descending=false)
Definition utils.h:37
void inversePermute(T &values, const std::vector< int > &ind)
Definition utils.h:152