10#include <Eigen/SparseCore>
15#include <unordered_set>
31template<
typename Derived>
33nonZeros(
const Eigen::SparseMatrixBase<Derived>& x)
35 return x.derived().nonZeros();
44template<
typename Derived>
48 return (x.derived().array() != 0).count();
69sort(T& v,
const bool descending =
false)
73 v.data(), v.data() + v.size(), std::greater<typename T::value_type>());
76 v.data(), v.data() + v.size(), std::less<typename T::value_type>());
97 for (
int i = 0; i < x.size(); i++) {
127 vector<int> idx(v.size());
128 iota(idx.begin(), idx.end(), 0);
131 sort(idx.begin(), idx.end(), [&v](
int i,
int j) { return v[i] > v[j]; });
133 sort(idx.begin(), idx.end(), [&v](
int i,
int j) { return v[i] < v[j]; });
152permute(T& values,
const std::vector<int>& ind)
157 T out(values.size());
162 for (
int i = 0; i < values.size(); ++i)
163 out[i] = std::move(values[ind[i]]);
168 values = std::move(out);
188 T out(values.size());
190 for (
int i = 0; i < values.size(); ++i)
191 out[ind[i]] = std::move(values[i]);
193 values = std::move(out);
210move_elements(std::vector<T>& v,
const int from,
const int to,
const int size)
218 assert(from + size <=
static_cast<int>(v.size()));
219 std::rotate(v.begin() + to, v.begin() + from, v.begin() + from + size);
221 assert(to + size <=
static_cast<int>(v.size()));
223 v.begin() + from, v.begin() + from + size, v.begin() + to + size);
241 const std::set<std::string>& valid_options,
242 const std::string& parameter_name);
257subset(
const Eigen::EigenBase<T>& x,
const std::vector<int>& indices)
259 return subset(x.derived(), indices);
274typename Eigen::MatrixBase<T>::PlainObject
275subset(
const Eigen::DenseBase<T>& x,
const std::vector<int>& indices)
277 return x.derived()(indices, all);
295subset(
const Eigen::SparseMatrixBase<T>& x,
const std::vector<int>& indices)
297 std::vector<Eigen::Triplet<double>> triplets;
300 for (
int j = 0; j < x.cols(); ++j) {
301 for (
typename T::InnerIterator it(x.derived(), j); it; ++it) {
302 auto it_idx = std::find(indices.begin(), indices.end(), it.row());
304 if (it_idx != indices.end()) {
305 int new_row = std::distance(indices.begin(), it_idx);
306 triplets.emplace_back(new_row, j, it.value());
311 T out(indices.size(), x.cols());
312 out.setFromTriplets(triplets.begin(), triplets.end());
331subsetCols(
const Eigen::MatrixBase<T>& x,
const std::vector<int>& indices)
333 return x.derived()(all, indices);
350subsetCols(
const Eigen::SparseMatrixBase<T>& x,
const std::vector<int>& indices)
352 std::vector<Eigen::Triplet<double>> triplets;
355 for (
size_t j_idx = 0; j_idx < indices.size(); ++j_idx) {
356 int j = indices[j_idx];
357 for (
typename T::InnerIterator it(x.derived(), j); it; ++it) {
358 triplets.emplace_back(it.row(), j_idx, it.value());
362 T out(x.rows(), indices.size());
363 out.setFromTriplets(triplets.begin(), triplets.end());
379inline std::unordered_set<double>
382 std::unordered_set<double>
unique;
383 for (Eigen::Index j = 0; j < x.cols(); j++) {
384 for (Eigen::Index i = 0; i < x.rows(); i++) {
Eigen compatibility layer for version differences.
Namespace containing SLOPE regression implementation.
std::unordered_set< double > unique(const Eigen::MatrixXd &x)
Create a set of unique values from an Eigen matrix.
void move_elements(std::vector< T > &v, const int from, const int to, const int size)
std::vector< int > which(const T &x)
T subsetCols(const Eigen::MatrixBase< T > &x, const std::vector< int > &indices)
Extract specified columns from a dense matrix.
T subset(const Eigen::EigenBase< T > &x, const std::vector< int > &indices)
Extract a subset of rows from an Eigen matrix.
void validateOption(const std::string &value, const std::set< std::string > &valid_options, const std::string ¶meter_name)
Validates if a given value exists in a set of valid options.
std::vector< int > sortIndex(T &v, const bool descending=false)
void permute(T &values, const std::vector< int > &ind)
void sort(T &v, const bool descending=false)
void inversePermute(T &values, const std::vector< int > &ind)
Eigen::Index nonZeros(const Eigen::SparseMatrixBase< Derived > &x)