42 #include <Eigen/Eigen>
46 #include "linearSystemHandler.hpp"
52 template<
typename T_Type,
template<
typename>
class T_Matrix,
template<
typename>
class T_Vector>
59 CODI_T(Eigen::Matrix<Type, 2, 2>));
61 CODI_T(Eigen::Matrix<Type, 2, 1>));
63 using Real =
typename Type::Real;
67 CODI_T(Eigen::Matrix<Real, 2, 2>));
69 CODI_T(Eigen::Matrix<Real, 2, 1>));
72 CODI_T(Eigen::Matrix<Identifier, 2, 2>));
74 CODI_T(Eigen::Matrix<Identifier, 2, 1>));
79 template<
typename T_Type,
template<
typename>
class T_Matrix,
template<
typename>
class T_Vector>
93 using Index =
typename Matrix::Index;
102 return new MatrixReal(mat->rows(), mat->cols());
149 template<
typename Func,
typename MatrixA,
typename MatrixB>
151 Index rows = matA->rows();
152 Index cols = matA->cols();
154 for (
int i = 0; i < rows; i += 1) {
155 for (
int j = 0; j < cols; j += 1) {
156 func(matA->coeffRef(i, j), matB->coeffRef(i, j));
162 template<
typename Func,
typename MatrixA,
typename MatrixB,
typename MatrixC>
163 void iterateMatrix(Func func, MatrixA* matA, MatrixB* matB, MatrixC* matC) {
164 Index rows = matA->rows();
165 Index cols = matA->cols();
167 for (
int i = 0; i < rows; i += 1) {
168 for (
int j = 0; j < cols; j += 1) {
169 func(matA->coeffRef(i, j), matB->coeffRef(i, j), matC->coeffRef(i, j));
175 template<
typename Func,
typename VectorA,
typename VectorB>
177 Index size = vecA->size();
179 for (
int i = 0; i < size; i += 1) {
180 func(vecA->coeffRef(i), vecB->coeffRef(i));
185 template<
typename Func,
typename VectorA,
typename VectorB,
typename VectorC>
186 void iterateVector(Func func, VectorA* vecA, VectorB* vecB, VectorC* vecC) {
187 Index size = vecA->size();
189 for (
int i = 0; i < size; i += 1) {
190 func(vecA->coeffRef(i), vecB->coeffRef(i), vecC->coeffRef(i));
195 template<
typename Func,
typename VectorA,
typename VectorB,
typename VectorC,
typename VectorD>
196 void iterateVector(Func func, VectorA* vecA, VectorB* vecB, VectorC* vecC, VectorD* vecD) {
197 Index size = vecA->size();
199 for (
int i = 0; i < size; i += 1) {
200 func(vecA->coeffRef(i), vecB->coeffRef(i), vecC->coeffRef(i), vecD->coeffRef(i));
220 *A_v_trans = A_v->transpose();
226 template<
typename Func>
228 Index rows = mat_id->rows();
229 Index cols = mat_id->cols();
231 for (
int i = 0; i < rows; i += 1) {
232 for (
int j = 0; j < cols; j += 1) {
233 func(mat_id->coeffRef(i, j), x_v->coeffRef(j), b_b->coeffRef(i));
245 *t = *b_d - *A_d * *x;
253 template<
typename T_Type,
template<
typename>
class T_Matrix,
template<
typename>
class T_Vector>
267 using Real =
typename Type::Real;
270 using Index =
typename Matrix::Index;
275 template<
typename R,
typename T,
typename M>
276 R* cloneMatrix(M* mat) {
277 R* r =
new R(mat->rows(), mat->cols());
279 std::vector<Eigen::Triplet<T>> entries(mat->nonZeros());
281 Index outerSize = mat->outerSize();
283 for (
int k = 0; k < outerSize; ++k) {
284 typename M::InnerIterator iter(*mat, k);
286 entries.push_back(Eigen::Triplet<T>(iter.row(), iter.col(), T()));
290 r->setFromTriplets(entries.begin(), entries.end());
304 return cloneMatrix<MatrixReal, Real>(mat);
310 return cloneMatrix<MatrixIdentifier, Identifier>(mat);
319 template<
typename Func,
typename MatrixA,
typename MatrixB>
321 Index outerSize = matA->outerSize();
323 for (
int k = 0; k < outerSize; ++k) {
324 typename MatrixA::InnerIterator iterA(*matA, k);
325 typename MatrixB::InnerIterator iterB(*matB, k);
327 func(iterA.valueRef(), iterB.valueRef());
336 template<
typename Func,
typename MatrixA,
typename MatrixB,
typename MatrixC>
337 void iterateMatrix(Func func, MatrixA* matA, MatrixB* matB, MatrixC* matC) {
338 Index outerSize = matA->outerSize();
340 for (
int k = 0; k < outerSize; ++k) {
341 typename MatrixA::InnerIterator iterA(*matA, k);
342 typename MatrixB::InnerIterator iterB(*matB, k);
343 typename MatrixC::InnerIterator iterC(*matC, k);
345 func(iterA.valueRef(), iterB.valueRef(), iterC.valueRef());
360 template<
typename Func>
362 Index outerSize = mat_id->outerSize();
364 for (
int k = 0; k < outerSize; ++k) {
365 typename MatrixIdentifier::InnerIterator iterId(*mat_id, k);
367 func(iterId.valueRef(), x_v->coeffRef(iterId.col()), b_b->coeffRef(iterId.row()));
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:111
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Eigen definition for the LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:53
T_Type Type
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:56
T_Matrix< Real > MatrixReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:66
T_Vector< Type > Vector
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:60
typename Type::Real Real
See LhsExpressionInterface.
Definition eigenLinearSystem.hpp:63
T_Matrix< Identifier > MatrixIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:71
T_Matrix< Type > Matrix
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:58
T_Vector< Real > VectorReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:68
typename Type::Identifier Identifier
See LhsExpressionInterface.
Definition eigenLinearSystem.hpp:64
T_Vector< Identifier > VectorIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:73
Definition eigenLinearSystem.hpp:80
void solveSystem(MatrixReal const *A, VectorReal const *b, VectorReal *x)
Needs to be implemented by the user.
void iterateVector(Func func, VectorA *vecA, VectorB *vecB, VectorC *vecC)
Iterate over all elements in the vectors at the same time.
Definition eigenLinearSystem.hpp:186
typename InterfaceTypes::MatrixReal MatrixReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:87
void deleteMatrixReal(MatrixReal *A_v)
Delete a real matrix.
Definition eigenLinearSystem.hpp:124
typename Matrix::Index Index
Index of an Eigen matrix.
Definition eigenLinearSystem.hpp:93
void deleteMatrixIdentifier(MatrixIdentifier *A_id)
Delete an identifier matrix.
Definition eigenLinearSystem.hpp:129
typename InterfaceTypes::Matrix Matrix
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:86
typename InterfaceTypes::VectorIdentifier VectorIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:91
void iterateVector(Func func, VectorA *vecA, VectorB *vecB, VectorC *vecC, VectorD *vecD)
Iterate over all elements in the vectors at the same time.
Definition eigenLinearSystem.hpp:196
void iterateVector(Func func, VectorA *vecA, VectorB *vecB)
Iterate over all elements in the vectors at the same time.
Definition eigenLinearSystem.hpp:176
MatrixReal * transposeMatrix(MatrixReal *A_v)
Create a transposed matrix.
Definition eigenLinearSystem.hpp:218
MatrixIdentifier * createMatrixIdentifier(M *mat)
Definition eigenLinearSystem.hpp:107
void deleteVectorReal(VectorReal *vec_v)
Delete a real Vector.
Definition eigenLinearSystem.hpp:134
typename InterfaceTypes::MatrixIdentifier MatrixIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:88
typename InterfaceTypes::VectorReal VectorReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:90
typename InterfaceTypes::Type Type
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:84
void iterateDyadic(Func func, MatrixIdentifier *mat_id, VectorReal *x_v, VectorReal *b_b)
Definition eigenLinearSystem.hpp:227
void iterateMatrix(Func func, MatrixA *matA, MatrixB *matB)
Iterate over all elements in the matrices at the same time.
Definition eigenLinearSystem.hpp:150
void iterateMatrix(Func func, MatrixA *matA, MatrixB *matB, MatrixC *matC)
Iterate over all elements in the matrices at the same time.
Definition eigenLinearSystem.hpp:163
VectorIdentifier * createVectorIdentifier(V *vec)
Definition eigenLinearSystem.hpp:119
void deleteVectorIdentifier(VectorIdentifier *vec_id)
Delete an identifier vector.
Definition eigenLinearSystem.hpp:139
void subtractMultiply(VectorReal *t, VectorReal const *b_d, MatrixReal const *A_d, VectorReal const *x)
Computes t = b_d - A_d * x.
Definition eigenLinearSystem.hpp:244
MatrixReal * createMatrixReal(M *mat)
Definition eigenLinearSystem.hpp:101
typename InterfaceTypes::Vector Vector
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:89
VectorReal * createVectorReal(V *vec)
Definition eigenLinearSystem.hpp:113
Definition linearSystemInterface.hpp:109
Definition eigenLinearSystem.hpp:254
typename InterfaceTypes::VectorIdentifier VectorIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:265
void iterateDyadic(Func func, MatrixIdentifier *mat_id, VectorReal *x_v, VectorReal *b_b)
Definition eigenLinearSystem.hpp:361
void iterateMatrix(Func func, MatrixA *matA, MatrixB *matB, MatrixC *matC)
Iterate over all elements in the matrices at the same time.
Definition eigenLinearSystem.hpp:337
MatrixReal * createMatrixReal(M *mat)
Definition eigenLinearSystem.hpp:303
typename Type::Real Real
See LhsExpressionInterface.
Definition eigenLinearSystem.hpp:267
typename Type::Identifier Identifier
See LhsExpressionInterface.
Definition eigenLinearSystem.hpp:268
typename InterfaceTypes::MatrixIdentifier MatrixIdentifier
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:262
typename InterfaceTypes::VectorReal VectorReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:264
typename InterfaceTypes::Type Type
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:258
typename InterfaceTypes::Vector Vector
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:263
typename InterfaceTypes::Matrix Matrix
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:260
MatrixIdentifier * createMatrixIdentifier(M *mat)
Definition eigenLinearSystem.hpp:309
typename InterfaceTypes::MatrixReal MatrixReal
See LinearSystemInterfaceTypes.
Definition eigenLinearSystem.hpp:261
void iterateMatrix(Func func, MatrixA *matA, MatrixB *matB)
Iterate over all elements in the matrices at the same time.
Definition eigenLinearSystem.hpp:320
typename Matrix::Index Index
Index of an Eigen matrix.
Definition eigenLinearSystem.hpp:270