Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Utils.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2026, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_UTILS_HH
14#define OPTIMIST_UTILS_HH
15
16#include "Optimist.hh"
17
18namespace Optimist {
19
25 namespace Utils {
26
30 template <typename Vector>
32 Eigen::Matrix<typename TypeTrait<Vector>::Scalar, Eigen::Dynamic, 1>;
33
37 template <typename Vector>
38 using DenseMatrix = Eigen::Matrix<typename TypeTrait<Vector>::Scalar,
39 Eigen::Dynamic,
40 Eigen::Dynamic>;
41
47 template <typename Vector>
48 DenseVector<Vector> to_dense(const Vector &x) {
49 return DenseVector<Vector>(x);
50 }
51
58 template <typename Vector, typename Derived>
59 Vector from_dense(const Eigen::MatrixBase<Derived> &x) {
60 if constexpr (TypeTrait<Vector>::IsSparse) {
61 Vector out(x.size());
62 out.reserve(x.size());
63 for (Eigen::Index i{0}; i < x.size(); ++i) {
64 out.coeffRef(i) = x(i);
65 }
66 return out;
67 } else {
68 return Vector(x);
69 }
70 }
71
78 template <typename Matrix, typename Derived>
79 Matrix from_dense_matrix(const Eigen::MatrixBase<Derived> &A) {
80 if constexpr (TypeTrait<Matrix>::IsSparse) {
81 return Matrix(A.sparseView());
82 } else {
83 return Matrix(A);
84 }
85 }
86
93 template <typename Vector>
94 typename TypeTrait<Vector>::Scalar coeff(const Vector &x, const Integer i) {
95 if constexpr (TypeTrait<Vector>::IsSparse) {
96 return x.coeff(i);
97 } else {
98 return x(i);
99 }
100 }
101
102 } // namespace Utils
103
104} // namespace Optimist
105
106#endif // OPTIMIST_UTILS_HH
Helpers to move data between the input types accepted by the solvers (fixed-size, dynamic-size and sp...
Definition Utils.hh:25
Vector from_dense(const Eigen::MatrixBase< Derived > &x)
Definition Utils.hh:59
Eigen::Matrix< typename TypeTrait< Vector >::Scalar, Eigen::Dynamic, Eigen::Dynamic > DenseMatrix
Definition Utils.hh:38
Matrix from_dense_matrix(const Eigen::MatrixBase< Derived > &A)
Definition Utils.hh:79
Eigen::Matrix< typename TypeTrait< Vector >::Scalar, Eigen::Dynamic, 1 > DenseVector
Definition Utils.hh:31
TypeTrait< Vector >::Scalar coeff(const Vector &x, const Integer i)
Definition Utils.hh:94
DenseVector< Vector > to_dense(const Vector &x)
Definition Utils.hh:48
Namespace for the Optimist library.
Definition Optimist.hh:90
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:98
Definition Optimist.hh:105