WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
append_as_triplets.hpp
Go to the documentation of this file.
1// Copyright (c) Sleipnir contributors
2
3#pragma once
4
5#include <initializer_list>
6
7#include <Eigen/SparseCore>
9
10/// Appends sparse matrices to list of triplets at the given offset.
11///
12/// The triplets are appended in column-major order (e.g., first column of mat1,
13/// first column of mat2 underneath first column of mat1, second column of mat1,
14/// second column of mat2 underneath second column of mat1).
15///
16/// @tparam Scalar Scalar type.
17/// @param triplets List of triplets.
18/// @param row_offset Row offset for first matrix.
19/// @param col_offset Column offset for first matrix.
20/// @param mats Sparse matrices to append.
21template <typename Scalar>
23 gch::small_vector<Eigen::Triplet<Scalar>>& triplets, int row_offset,
24 int col_offset, std::initializer_list<Eigen::SparseMatrix<Scalar>> mats) {
25 // Compute row offset for each matrix
26 gch::small_vector<int> mat_row_offsets;
27 int mat_row_offset = 0;
28 for (const auto& mat : mats) {
29 mat_row_offsets.emplace_back(mat_row_offset);
30 mat_row_offset += mat.rows();
31 }
32
33 // Append elements in column-major order
34 for (int col = 0; col < mats.begin()[0].cols(); ++col) {
35 for (size_t i = 0; i < mats.size(); ++i) {
36 for (typename Eigen::SparseMatrix<Scalar>::InnerIterator it{
37 mats.begin()[i], col};
38 it; ++it) {
39 triplets.emplace_back(row_offset + mat_row_offsets[i] + it.row(),
40 col_offset + it.col(), it.value());
41 }
42 }
43 }
44}
45
46/// Append diagonal matrix to list of triplets at the given offset.
47///
48/// @tparam Scalar Scalar type.
49/// @param triplets List of triplets.
50/// @param row_offset Row offset for first matrix.
51/// @param col_offset Column offset for first matrix.
52/// @param diag Diagonal of matrix.
53template <typename Scalar>
55 gch::small_vector<Eigen::Triplet<Scalar>>& triplets, int row_offset,
56 int col_offset, const Eigen::Vector<Scalar, Eigen::Dynamic>& diag) {
57 for (int row = 0; row < diag.rows(); ++row) {
58 triplets.emplace_back(row_offset + row, col_offset + row, diag[row]);
59 }
60}
void append_as_triplets(gch::small_vector< Eigen::Triplet< Scalar > > &triplets, int row_offset, int col_offset, std::initializer_list< Eigen::SparseMatrix< Scalar > > mats)
Appends sparse matrices to list of triplets at the given offset.
Definition append_as_triplets.hpp:22
void append_diagonal_as_triplets(gch::small_vector< Eigen::Triplet< Scalar > > &triplets, int row_offset, int col_offset, const Eigen::Vector< Scalar, Eigen::Dynamic > &diag)
Append diagonal matrix to list of triplets at the given offset.
Definition append_as_triplets.hpp:54
wpi::util::SmallVector< T > small_vector
Definition small_vector.hpp:10