17#include "Eigen/Sparse"
19namespace ZonoOpt::detail
23 Eigen::SparseMatrix<T> hcat(
const Eigen::SparseMatrix<T> &A,
const Eigen::SparseMatrix<T> &B)
25 if (A.rows() != B.rows())
27 throw std::invalid_argument(
"hcat: number of rows must match.");
30 Eigen::SparseMatrix<T> C(A.rows(), A.cols() + B.cols());
31 std::vector<Eigen::Triplet<T>> tripvec;
32 tripvec.reserve(A.nonZeros() + B.nonZeros());
34 for (
int k=0; k<A.outerSize(); ++k)
36 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(A, k); it; ++it)
38 tripvec.emplace_back(
static_cast<int>(it.row()),
static_cast<int>(it.col()), it.value());
42 for (
int k=0; k<B.outerSize(); ++k)
44 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(B, k); it; ++it)
46 tripvec.emplace_back(
static_cast<int>(it.row()),
static_cast<int>(it.col()+A.cols()), it.value());
50 C.setFromTriplets(tripvec.begin(), tripvec.end());
56 Eigen::SparseMatrix<T> vcat(
const Eigen::SparseMatrix<T> &A,
const Eigen::SparseMatrix<T> &B)
58 if (A.cols() != B.cols())
60 throw std::invalid_argument(
"vcat: number of columns must match.");
63 Eigen::SparseMatrix<T> C(A.rows() + B.rows(), A.cols());
64 std::vector<Eigen::Triplet<T>> tripvec;
65 tripvec.reserve(A.nonZeros() + B.nonZeros());
67 for (
int k=0; k<A.outerSize(); ++k)
69 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(A, k); it; ++it)
71 tripvec.emplace_back(
static_cast<int>(it.row()),
static_cast<int>(it.col()), it.value());
75 for (
int k=0; k<B.outerSize(); ++k)
77 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(B, k); it; ++it)
79 tripvec.emplace_back(
static_cast<int>(it.row()+A.rows()),
static_cast<int>(it.col()), it.value());
83 C.setFromTriplets(tripvec.begin(), tripvec.end());
89 void get_triplets_offset(
const Eigen::SparseMatrix<T> &mat, std::vector<Eigen::Triplet<T>> &triplets,
90 const int i_offset,
const int j_offset)
93 if (i_offset < 0 || j_offset < 0)
95 throw std::invalid_argument(
"get_triplets_offset: offsets must be non-negative.");
99 for (
int k=0; k<mat.outerSize(); ++k)
101 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(mat, k); it; ++it)
103 triplets.emplace_back(
static_cast<int>(it.row() + i_offset),
static_cast<int>(it.col() + j_offset), it.value());
109 template <
typename T,
typename Derived>
110 Eigen::SparseMatrix<T> hcat(
const Eigen::SparseCompressedBase<Derived> &A,
const Eigen::SparseCompressedBase<Derived> &B)
112 if (A.rows() != B.rows())
114 throw std::invalid_argument(
"hcat: number of rows must match.");
117 Eigen::SparseMatrix<T> C(A.rows(), A.cols() + B.cols());
118 std::vector<Eigen::Triplet<T>> tripvec;
119 tripvec.reserve(A.nonZeros() + B.nonZeros());
121 for (
int k=0; k<A.derived().outerSize(); ++k)
123 for (
typename Derived::InnerIterator it(A.derived(), k); it; ++it)
125 tripvec.emplace_back(
static_cast<int>(it.row()),
static_cast<int>(it.col()), it.value());
129 for (
int k=0; k<B.derived().outerSize(); ++k)
131 for (
typename Derived::InnerIterator it(B.derived(), k); it; ++it)
133 tripvec.emplace_back(
static_cast<int>(it.row()),
static_cast<int>(it.col()+A.cols()), it.value());
137 C.setFromTriplets(tripvec.begin(), tripvec.end());
141 template <
typename T,
typename Derived>
142 void get_triplets_offset(
const Eigen::SparseCompressedBase<Derived> &mat, std::vector<Eigen::Triplet<T>> &triplets,
143 const int i_offset,
const int j_offset)
145 if (i_offset < 0 || j_offset < 0)
147 throw std::invalid_argument(
"get_triplets_offset: offsets must be non-negative.");
150 for (
int k=0; k<mat.derived().outerSize(); ++k)
152 for (
typename Derived::InnerIterator it(mat.derived(), k); it; ++it)
154 triplets.emplace_back(
static_cast<int>(it.row() + i_offset),
static_cast<int>(it.col() + j_offset), it.value());
161 template <
typename T>
162 void append_factor_triplets(
const Eigen::SparseMatrix<T> &mat,
const int nGc,
const int shift_c,
const int shift_b,
163 const int i_offset, std::vector<Eigen::Triplet<T>> &triplets)
165 for (
int k=0; k<mat.outerSize(); ++k)
167 const int col = k < nGc ? k + shift_c : k + shift_b;
168 for (
typename Eigen::SparseMatrix<T>::InnerIterator it(mat, k); it; ++it)
170 triplets.emplace_back(
static_cast<int>(it.row()) + i_offset, col, it.value());
176 template <
typename T>
177 void remove_redundant_constraints(Eigen::SparseMatrix<T>& A, Eigen::Vector<T,-1>& b)
180 if (A.rows() == 0 || A.cols() == 0)
182 A.resize(0, A.cols());
188 Eigen::SparseMatrix<T> At = A.transpose();
191 Eigen::SparseQR<Eigen::SparseMatrix<T>, Eigen::COLAMDOrdering<int>> qr;
192 qr.analyzePattern(At);
196 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> P = qr.colsPermutation();
197 Eigen::VectorXi P_indices = P.indices();
200 std::vector<Eigen::Triplet<T>> tripvec;
201 for (
int i=0; i<qr.rank(); i++)
203 tripvec.emplace_back(P_indices(i), i,
static_cast<T
>(1.0));
206 Eigen::SparseMatrix<T> P_full (At.cols(), qr.rank());
207 P_full.setFromTriplets(tripvec.begin(), tripvec.end());
210 A = (At * P_full).transpose();
211 b = (b.transpose() * P_full).transpose();
215 template <
typename T>
216 std::vector<Eigen::Triplet<T>> get_triplets_row(
const Eigen::SparseMatrix<T, Eigen::RowMajor> &mat,
int row)
218 std::vector<Eigen::Triplet<T>> triplets;
219 if (row < 0 || row >= mat.rows())
221 throw std::out_of_range(
"get_triplets_row: row index out of range.");
224 for (
typename Eigen::SparseMatrix<T, Eigen::RowMajor>::InnerIterator it(mat, row); it; ++it)
226 triplets.emplace_back(row,
static_cast<int>(it.col()), it.value());
232 template <
typename T>
235 Eigen::SparseMatrix<T> R;
236 Eigen::Vector<T, -1> b_tilde;
237 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> P;
240 template <
typename T>
241 void QREqCons(
const Eigen::SparseMatrix<T>& A,
const Eigen::Vector<T, -1>& b, QREqConsData<T>& data)
243 Eigen::SparseQR<Eigen::SparseMatrix<T>, Eigen::COLAMDOrdering<int>> qr;
245 data.R = qr.matrixR();
246 data.b_tilde = qr.matrixQ().transpose()*b;
247 data.P = qr.colsPermutation();