ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
Eigen.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File Eigen.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
47/*--------------------------------------------------------------------------*/
48/*----------------------------- DEFINITIONS --------------------------------*/
49/*--------------------------------------------------------------------------*/
50
51#ifndef __ADS_interop_eigen_Eigen
52 #define __ADS_interop_eigen_Eigen
53/* self-identification: #endif constant */
54
55/*--------------------------------------------------------------------------*/
56/*------------------------------ INCLUDES ----------------------------------*/
57/*--------------------------------------------------------------------------*/
58
59#if ! __has_include( <Eigen/SparseCore> )
60 #error "the interoperation with Eigen needs the headers of Eigen"
61#endif
62
63#include "ADS/Contract.h"
66
67#include <Eigen/Core>
68#include <Eigen/SparseCore>
69
70#include <algorithm>
71#include <concepts>
72#include <cstddef>
73#include <cstdint>
74#include <limits>
75#include <tuple>
76#include <utility>
77#include <vector>
78
79/*--------------------------------------------------------------------------*/
80/*--------------------------- NAMESPACE ------------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83namespace ADS::eigen
84{
85
86/*--------------------------------------------------------------------------*/
87/*--------------------------------- TYPES ----------------------------------*/
88/*--------------------------------------------------------------------------*/
89
91template< class T >
92using RowMatrix =
93 Eigen::Matrix< T , Eigen::Dynamic , Eigen::Dynamic , Eigen::RowMajor >;
94
96template< class T , class I = int >
97using RowSparse = Eigen::SparseMatrix< T , Eigen::RowMajor , I >;
98
99/*--------------------------------------------------------------------------*/
100/*---------------------------------- VIEWS ---------------------------------*/
101/*--------------------------------------------------------------------------*/
102
104template< class T >
105[[nodiscard]] Eigen::Map< RowMatrix< T > > map( DenseMatrix< T > & A )
106{
107 return( Eigen::Map< RowMatrix< T > >( A.data() , Eigen::Index( A.rows() ) ,
108 Eigen::Index( A.cols() ) ) );
109 }
110
113template< class T >
114[[nodiscard]] Eigen::Map< const RowMatrix< T > >
116{
117 return( Eigen::Map< const RowMatrix< T > >(
118 A.data() , Eigen::Index( A.rows() ) , Eigen::Index( A.cols() ) ) );
119 }
120
124template< class T , std::signed_integral I >
125[[nodiscard]] Eigen::Map< const RowSparse< T , I > >
127{
128 return( Eigen::Map< const RowSparse< T , I > >(
129 Eigen::Index( A.rows() ) , Eigen::Index( A.cols() ) ,
130 Eigen::Index( A.nonzeros() ) , A.rowStart() , A.columns() , A.values() ) );
131 }
132
133/*--------------------------------------------------------------------------*/
134/*---------------------------------- COPIES --------------------------------*/
135/*--------------------------------------------------------------------------*/
136
141template< class S = int , class T , class I , class P >
142[[nodiscard]] RowSparse< T , S >
144{
145 ADS_PRE( ( A.rows() <= std::size_t( std::numeric_limits< S >::max() ) ) &&
146 ( A.cols() <= std::size_t( std::numeric_limits< S >::max() ) ) &&
147 ( A.nonzeros() <= std::size_t( std::numeric_limits< S >::max() ) ) );
148 RowSparse< T , S > E( Eigen::Index( A.rows() ) , Eigen::Index( A.cols() ) );
149 E.resizeNonZeros( Eigen::Index( A.nonzeros() ) );
150 std::transform( A.rowStart() , A.rowStart() + A.rows() + 1 ,
151 E.outerIndexPtr() ,
152 []( P p ) { return( S( p ) ); } );
153 std::transform( A.columns() , A.columns() + A.nonzeros() , E.innerIndexPtr() ,
154 []( I j ) { return( S( j ) ); } );
155 std::copy( A.values() , A.values() + A.nonzeros() , E.valuePtr() );
156 return( E );
157 }
158
162template< class I = std::uint32_t , class P = detail::CsrOffset< I > ,
163 class D >
165fromEigen( const Eigen::SparseMatrixBase< D > & S )
166{
167 using T = typename D::Scalar;
168 const RowSparse< T , typename D::StorageIndex > R( S.derived() );
169 std::vector< typename BasicCsrMatrix< T , I , P >::Triplet > E;
170 E.reserve( std::size_t( R.nonZeros() ) );
171 for( Eigen::Index i = 0 ; i < R.outerSize() ; ++i )
173 R , i ) ;
174 it ; ++it )
175 E.emplace_back( std::size_t( it.row() ) , std::size_t( it.col() ) ,
176 it.value() );
178 std::size_t( R.rows() ) , std::size_t( R.cols() ) , std::move( E ) ) );
179 }
180
181/*--------------------------------------------------------------------------*/
182
183} // end( namespace ADS::eigen )
184
185/*--------------------------------------------------------------------------*/
186
187#endif /* Eigen.h included */
188
189/*--------------------------------------------------------------------------*/
190/*---------------------------- End File Eigen.h ----------------------------*/
191/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
A sparse matrix in compressed sparse rows (CSR), beyond the book: the nonzeros of each row,...
The dense matrices by rows, beyond the book: DenseMatrix keeps the entries by rows in one contiguous...
a sparse matrix of values T in compressed sparse rows, the columns of the type I and the offsets of ...
Definition CsrMatrix.h:129
an matrix by rows in one contiguous vector
Definition DenseMatrix.h:187
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
RowSparse< T, S > toEigen(const BasicCsrMatrix< T, I, P > &A)
copied into a sparse matrix of Eigen by rows of the index type S, in time, for the layouts that map(...
Definition Eigen.h:143
Eigen::Map< RowMatrix< T > > map(DenseMatrix< T > &A)
the entries of as a dense matrix of Eigen, with no copy
Definition Eigen.h:105
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > RowMatrix
a dense matrix of Eigen by rows, the layout of DenseMatrix
Definition Eigen.h:92
BasicCsrMatrix< typename D::Scalar, I, P > fromEigen(const Eigen::SparseMatrixBase< D > &S)
the sparse expression of Eigen copied into a BasicCsrMatrix of the columns I and the offsets P,...
Definition Eigen.h:165
Eigen::SparseMatrix< T, Eigen::RowMajor, I > RowSparse
a sparse matrix of Eigen by rows, of the index type I
Definition Eigen.h:97
Definition Eigen.h:84