28 #ifndef TOMOGRAPHER_TOOLS_EIGENUTIL_H 29 #define TOMOGRAPHER_TOOLS_EIGENUTIL_H 42 #include <Eigen/Dense> 43 #include <Eigen/StdVector> 45 #include <boost/serialization/serialization.hpp> 46 #include <boost/serialization/split_free.hpp> 47 #include <boost/serialization/complex.hpp> 71 template<
typename EigenType>
87 namespace tomo_internal {
89 template<
typename Rng,
typename RndDist,
typename Scalar>
90 struct random_generator
92 typedef Scalar result_type;
97 random_generator(Rng & rng_, RndDist & rnddist_)
98 : rng(rng_), rnddist(rnddist_)
102 template<
typename Index>
103 inline const result_type operator() (Index, Index = 0)
const {
108 template<
typename Rng,
typename RndDist,
typename RealScalar>
109 struct random_generator<Rng, RndDist, std::complex<RealScalar> >
116 random_generator(Rng & rng_, RndDist & rnddist_)
117 : rng(rng_), rnddist(rnddist_)
121 template<
typename Index>
122 inline const result_type operator() (Index, Index = 0)
const {
123 return result_type(rnddist(rng), rnddist(rng));
134 template<
typename Rng,
typename RndDist,
typename Scalar>
135 struct functor_traits<Tomographer::Tools::tomo_internal::random_generator<Rng, RndDist, Scalar> >
136 {
enum { Cost = 50 * NumTraits<Scalar>::MulCost, PacketAccess =
false, IsRepeatable =
false }; };
153 template<
typename Der,
typename Rng,
typename RndDist,
typename... IndexTypes>
154 inline auto denseRandom(Rng & rng, RndDist &rnddist, IndexTypes... sizes)
156 tomo_internal::random_generator<Rng, RndDist, typename Eigen::internal::traits<Der>::Scalar>,
160 typedef typename Der::Scalar Scalar;
163 sizes..., tomo_internal::random_generator<Rng, RndDist, Scalar>(rng, rnddist)
173 namespace tomo_internal {
175 template<
typename Scalar,
typename IndexType>
176 struct can_basis_vec_generator
178 typedef Scalar result_type;
183 can_basis_vec_generator(IndexType k_, IndexType j_ = 0)
188 inline const result_type operator() (IndexType a, IndexType b = 0)
const {
189 return (a == k) && (b == j) ? result_type(1) : result_type(0);
198 template<
typename Scalar,
typename IndexType>
199 struct functor_traits<Tomographer::Tools::tomo_internal::can_basis_vec_generator<Scalar, IndexType> >
200 {
enum { Cost = 2 * NumTraits<Scalar>::MulCost, PacketAccess =
false, IsRepeatable =
true }; };
201 template<
typename Scalar,
typename IndexType>
202 struct functor_has_linear_access<Tomographer::Tools::tomo_internal::can_basis_vec_generator<Scalar, IndexType> >
203 {
enum { ret = 0 }; };
213 template<
typename Der,
typename IndexType>
216 tomo_internal::can_basis_vec_generator<typename Eigen::internal::traits<Der>::Scalar, IndexType>,
220 typedef typename Der::Scalar Scalar;
223 size, tomo_internal::can_basis_vec_generator<Scalar, IndexType>(k)
231 template<
typename Der,
typename IndexType>
234 tomo_internal::can_basis_vec_generator<typename Eigen::internal::traits<Der>::Scalar, IndexType>,
238 typedef typename Der::Scalar Scalar;
241 rows, cols, tomo_internal::can_basis_vec_generator<Scalar, IndexType>(k, j)
252 namespace tomo_internal {
254 template<
typename Scalar>
255 struct powers_of_two_generator
259 powers_of_two_generator() { }
261 template<
typename IndexType>
262 inline const result_type operator() (IndexType a)
const {
284 template<
typename Scalar>
285 struct functor_traits<Tomographer::Tools::tomo_internal::powers_of_two_generator<Scalar> >
286 {
enum { Cost = 8 * NumTraits<Scalar>::MulCost, PacketAccess =
false, IsRepeatable =
true }; };
306 template<
typename Der,
typename... IndexTypes>
309 tomo_internal::powers_of_two_generator<typename Eigen::internal::traits<Der>::Scalar>,
313 typedef typename Der::Scalar Scalar;
316 sizes..., tomo_internal::powers_of_two_generator<Scalar>()
341 template<
int RowFactorCTime,
int ColFactorCTime,
typename Derived,
342 typename std::enable_if<(RowFactorCTime == Eigen::Dynamic || ColFactorCTime == Eigen::Dynamic),
343 bool>::type dummy =
true>
347 eigen_assert(RowFactorCTime == Eigen::Dynamic || row_factor == RowFactorCTime);
348 eigen_assert(ColFactorCTime == Eigen::Dynamic || col_factor == ColFactorCTime);
349 return x.
replicate(row_factor, col_factor);
355 template<
int RowFactorCTime,
int ColFactorCTime,
typename Derived,
356 typename std::enable_if<(RowFactorCTime != Eigen::Dynamic && ColFactorCTime != Eigen::Dynamic),
357 bool>::type dummy2 =
true>
361 eigen_assert(row_factor == RowFactorCTime); (void)row_factor;
362 eigen_assert(col_factor == ColFactorCTime); (void)col_factor;
363 return x.template replicate<RowFactorCTime, ColFactorCTime>();
380 namespace serialization {
382 template<
typename Archive,
typename Derived>
383 void eigen_helper_save(Archive & a,
const Derived & m,
unsigned int )
385 Eigen::Index rows = m.rows();
386 Eigen::Index cols = m.cols();
389 for (Eigen::Index i = 0; i < m.rows(); ++i) {
390 for (Eigen::Index j = 0; j < m.cols(); ++j) {
396 template<
typename Archive,
typename Derived>
397 void eigen_helper_load(Archive & a, Derived & m,
unsigned int )
399 Eigen::Index rows, cols;
402 m.resize(rows, cols);
403 for (Eigen::Index i = 0; i < m.rows(); ++i) {
404 for (Eigen::Index j = 0; j < m.cols(); ++j) {
412 template<
typename Archive,
413 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
415 const unsigned int version)
417 eigen_helper_save(a, m, version);
419 template<
typename Archive,
420 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
422 const unsigned int version)
424 eigen_helper_save(a, m, version);
426 template<
typename Archive,
427 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
429 const unsigned int version)
431 eigen_helper_load(a, m, version);
433 template<
typename Archive,
434 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
436 const unsigned int version)
438 eigen_helper_load(a, m, version);
441 template<
typename Archive,
442 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
444 const unsigned int version)
446 split_free(a, m, version);
448 template<
typename Archive,
449 typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols>
451 const unsigned int version)
453 split_free(a, m, version);
static const CwiseNullaryOp< CustomNullaryOp, Derived > NullaryExpr(Index rows, Index cols, const CustomNullaryOp &func)
Base namespace for the Tomographer project.
const Replicate< Derived, RowFactor, ColFactor > replicate() const