ZonoOpt 2.5.0
Loading...
Searching...
No Matches
SparseMatrixUtilities.hpp
Go to the documentation of this file.
1#pragma once
2
14#include <vector>
15#include <stdexcept>
16
17#include "Eigen/Sparse"
18
19namespace ZonoOpt::detail
20{
21
22 template <typename T>
23 Eigen::SparseMatrix<T> hcat(const Eigen::SparseMatrix<T> &A, const Eigen::SparseMatrix<T> &B)
24 {
25 if (A.rows() != B.rows())
26 {
27 throw std::invalid_argument("hcat: number of rows must match.");
28 }
29
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());
33
34 for (int k=0; k<A.outerSize(); ++k)
35 {
36 for (typename Eigen::SparseMatrix<T>::InnerIterator it(A, k); it; ++it)
37 {
38 tripvec.emplace_back(static_cast<int>(it.row()), static_cast<int>(it.col()), it.value());
39 }
40 }
41
42 for (int k=0; k<B.outerSize(); ++k)
43 {
44 for (typename Eigen::SparseMatrix<T>::InnerIterator it(B, k); it; ++it)
45 {
46 tripvec.emplace_back(static_cast<int>(it.row()), static_cast<int>(it.col()+A.cols()), it.value());
47 }
48 }
49
50 C.setFromTriplets(tripvec.begin(), tripvec.end());
51 return C;
52 }
53
54
55 template<typename T>
56 Eigen::SparseMatrix<T> vcat(const Eigen::SparseMatrix<T> &A, const Eigen::SparseMatrix<T> &B)
57 {
58 if (A.cols() != B.cols())
59 {
60 throw std::invalid_argument("vcat: number of columns must match.");
61 }
62
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());
66
67 for (int k=0; k<A.outerSize(); ++k)
68 {
69 for (typename Eigen::SparseMatrix<T>::InnerIterator it(A, k); it; ++it)
70 {
71 tripvec.emplace_back(static_cast<int>(it.row()), static_cast<int>(it.col()), it.value());
72 }
73 }
74
75 for (int k=0; k<B.outerSize(); ++k)
76 {
77 for (typename Eigen::SparseMatrix<T>::InnerIterator it(B, k); it; ++it)
78 {
79 tripvec.emplace_back(static_cast<int>(it.row()+A.rows()), static_cast<int>(it.col()), it.value());
80 }
81 }
82
83 C.setFromTriplets(tripvec.begin(), tripvec.end());
84 return C;
85 }
86
87 // get triplets for matrix
88 template <typename T>
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)
91 {
92 // check validity
93 if (i_offset < 0 || j_offset < 0)
94 {
95 throw std::invalid_argument("get_triplets_offset: offsets must be non-negative.");
96 }
97
98 // get triplets
99 for (int k=0; k<mat.outerSize(); ++k)
100 {
101 for (typename Eigen::SparseMatrix<T>::InnerIterator it(mat, k); it; ++it)
102 {
103 triplets.emplace_back(static_cast<int>(it.row() + i_offset), static_cast<int>(it.col() + j_offset), it.value());
104 }
105 }
106 }
107
108 // overloads accepting any compressed sparse expression (e.g., column-block views) without materializing a copy
109 template <typename T, typename Derived>
110 Eigen::SparseMatrix<T> hcat(const Eigen::SparseCompressedBase<Derived> &A, const Eigen::SparseCompressedBase<Derived> &B)
111 {
112 if (A.rows() != B.rows())
113 {
114 throw std::invalid_argument("hcat: number of rows must match.");
115 }
116
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());
120
121 for (int k=0; k<A.derived().outerSize(); ++k)
122 {
123 for (typename Derived::InnerIterator it(A.derived(), k); it; ++it)
124 {
125 tripvec.emplace_back(static_cast<int>(it.row()), static_cast<int>(it.col()), it.value());
126 }
127 }
128
129 for (int k=0; k<B.derived().outerSize(); ++k)
130 {
131 for (typename Derived::InnerIterator it(B.derived(), k); it; ++it)
132 {
133 tripvec.emplace_back(static_cast<int>(it.row()), static_cast<int>(it.col()+A.cols()), it.value());
134 }
135 }
136
137 C.setFromTriplets(tripvec.begin(), tripvec.end());
138 return C;
139 }
140
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)
144 {
145 if (i_offset < 0 || j_offset < 0)
146 {
147 throw std::invalid_argument("get_triplets_offset: offsets must be non-negative.");
148 }
149
150 for (int k=0; k<mat.derived().outerSize(); ++k)
151 {
152 for (typename Derived::InnerIterator it(mat.derived(), k); it; ++it)
153 {
154 triplets.emplace_back(static_cast<int>(it.row() + i_offset), static_cast<int>(it.col() + j_offset), it.value());
155 }
156 }
157 }
158
159 // append triplets of a matrix whose columns are ordered [continuous | binary] into a combined matrix with
160 // the same ordering. Continuous columns are shifted by shift_c and binary columns by shift_b.
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)
164 {
165 for (int k=0; k<mat.outerSize(); ++k)
166 {
167 const int col = k < nGc ? k + shift_c : k + shift_b;
168 for (typename Eigen::SparseMatrix<T>::InnerIterator it(mat, k); it; ++it)
169 {
170 triplets.emplace_back(static_cast<int>(it.row()) + i_offset, col, it.value());
171 }
172 }
173 }
174
175 // remove redundant constraints, A*x = b
176 template <typename T>
177 void remove_redundant_constraints(Eigen::SparseMatrix<T>& A, Eigen::Vector<T,-1>& b)
178 {
179 // check for empty input matrix
180 if (A.rows() == 0 || A.cols() == 0)
181 {
182 A.resize(0, A.cols());
183 b.resize(0);
184 return;
185 }
186
187 // transpose
188 Eigen::SparseMatrix<T> At = A.transpose();
189
190 // compute QR decomposition
191 Eigen::SparseQR<Eigen::SparseMatrix<T>, Eigen::COLAMDOrdering<int>> qr;
192 qr.analyzePattern(At);
193 qr.factorize(At);
194
195 // get the permutation matrix and its indices
196 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> P = qr.colsPermutation();
197 Eigen::VectorXi P_indices = P.indices();
198
199 // QR solver puts linearly dependent rows at end
200 std::vector<Eigen::Triplet<T>> tripvec;
201 for (int i=0; i<qr.rank(); i++)
202 {
203 tripvec.emplace_back(P_indices(i), i, static_cast<T>(1.0));
204 }
205
206 Eigen::SparseMatrix<T> P_full (At.cols(), qr.rank());
207 P_full.setFromTriplets(tripvec.begin(), tripvec.end());
208
209 // remove redundant constraints
210 A = (At * P_full).transpose();
211 b = (b.transpose() * P_full).transpose();
212 }
213
214 // get triplets for row of row-major sparse matrix
215 template <typename T>
216 std::vector<Eigen::Triplet<T>> get_triplets_row(const Eigen::SparseMatrix<T, Eigen::RowMajor> &mat, int row)
217 {
218 std::vector<Eigen::Triplet<T>> triplets;
219 if (row < 0 || row >= mat.rows())
220 {
221 throw std::out_of_range("get_triplets_row: row index out of range.");
222 }
223
224 for (typename Eigen::SparseMatrix<T, Eigen::RowMajor>::InnerIterator it(mat, row); it; ++it)
225 {
226 triplets.emplace_back(row, static_cast<int>(it.col()), it.value());
227 }
228
229 return triplets;
230 }
231
232 template <typename T>
233 struct QREqConsData
234 {
235 Eigen::SparseMatrix<T> R;
236 Eigen::Vector<T, -1> b_tilde;
237 Eigen::PermutationMatrix<Eigen::Dynamic, Eigen::Dynamic> P;
238 };
239
240 template <typename T>
241 void QREqCons(const Eigen::SparseMatrix<T>& A, const Eigen::Vector<T, -1>& b, QREqConsData<T>& data)
242 {
243 Eigen::SparseQR<Eigen::SparseMatrix<T>, Eigen::COLAMDOrdering<int>> qr;
244 qr.compute(A);
245 data.R = qr.matrixR();
246 data.b_tilde = qr.matrixQ().transpose()*b;
247 data.P = qr.colsPermutation();
248 }
249
250} // namespace ZonoOpt::detail
251// end namespace ZonoOpt