IMP logo
IMP Reference Guide  develop.74ba496d88,2026/11/03
The Integrative Modeling Platform
Matrix.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/Matrix.h
3  * \brief Matrices used to store image data
4  * Copyright 2007-2026 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_MATRIX_H
8 #define IMPEM2D_MATRIX_H
9 
10 #include <IMP/em2d/em2d_config.h>
12 #include <IMP/exception.h>
13 #include <Eigen/Dense>
14 #include <cereal/access.hpp>
15 #include <cereal/cereal.hpp>
16 #include <cstdint>
17 #include <limits>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 
22 IMPEM2D_BEGIN_NAMESPACE
23 
24 //! Matrix of doubles, used to store the data of an image
25 /** Elements are accessed as m(row, column), and stored in row-major order.
26  */
27 typedef Eigen::Array<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
29 
30 //! Matrix of ints, used for masks and labels
31 typedef Eigen::Array<int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>
33 
34 //! Prints a matrix
35 IMPEM2DEXPORT void show(const DoubleMatrix &m, std::ostream &out = std::cout);
36 
37 //! Quick and dirty way of writing a matrix to a Spider image
38 IMPEM2DEXPORT void write_matrix(const DoubleMatrix &m, std::string name);
39 
40 IMPEM2D_END_NAMESPACE
41 
42 #ifndef SWIG
43 IMPEM2D_BEGIN_INTERNAL_NAMESPACE
44 
45 //! A matrix of elements of type T, stored in row-major order
46 template <class T>
47 using RowMajorArray =
48  Eigen::Array<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
49 
50 IMPEM2D_END_INTERNAL_NAMESPACE
51 
52 namespace cereal {
53 //! Serialize a matrix as its number of rows and columns, then its data
54 template <class Archive, class T>
55 inline void save(Archive &ar,
56  const IMP::em2d::internal::RowMajorArray<T> &m) {
57  std::int64_t rows = m.rows(), cols = m.cols();
58  ar(rows, cols);
59  ar(cereal::binary_data(const_cast<T *>(m.data()), m.size() * sizeof(T)));
60 }
61 
62 template <class Archive, class T>
63 inline void load(Archive &ar, IMP::em2d::internal::RowMajorArray<T> &m) {
64  std::int64_t rows, cols;
65  ar(rows, cols);
66  if (rows < 0 || cols < 0 ||
67  (cols > 0 &&
68  rows > std::numeric_limits<Eigen::Index>::max() / cols)) {
69  IMP_THROW("Invalid matrix size in serialized data: " << rows << "x"
70  << cols,
72  }
73  m.resize(rows, cols);
74  ar(cereal::binary_data(m.data(), m.size() * sizeof(T)));
75 }
76 } // namespace cereal
77 #endif // SWIG
78 
79 #endif /* IMPEM2D_MATRIX_H */
2D transformations. Copyright 2007-2026 IMP Inventors. All rights reserved.
void write_matrix(const DoubleMatrix &m, std::string name)
Quick and dirty way of writing a matrix to a Spider image.
Exception definitions and assertions.
void show(Hierarchy h, std::ostream &out=std::cout)
Print out a molecular hierarchy.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
Eigen::Array< int, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > IntMatrix
Matrix of ints, used for masks and labels.
Definition: Matrix.h:32
Eigen::Array< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > DoubleMatrix
Matrix of doubles, used to store the data of an image.
Definition: Matrix.h:28
An exception for an invalid value being passed to IMP.
Definition: exception.h:136