bpp-phyl3  3.0.0
DataFlowNumeric.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include <Bpp/Exceptions.h>
6 
7 #include "DataFlowNumeric.h"
8 
9 using namespace Eigen;
10 
11 namespace bpp
12 {
13 /*****************************************/
14 /* copyBppToEigen */
15 
16 
17 template void copyBppToEigen (const std::vector<ExtendedFloat>& bppVector, Eigen::RowVectorXd& eigenVector);
18 template void copyBppToEigen (const std::vector<ExtendedFloat>& bppVector, Eigen::VectorXd& eigenVector);
19 template void copyBppToEigen (const std::vector<ExtendedFloat>& bppVector, ExtendedFloatRowVectorXd& eigenVector);
20 template void copyBppToEigen (const std::vector<ExtendedFloat>& bppVector, ExtendedFloatVectorXd& eigenVector);
21 
22 template void copyBppToEigen (const bpp::Vdouble& bppVector, Eigen::RowVectorXd& eigenVector);
23 template void copyBppToEigen (const bpp::Vdouble& bppVector, Eigen::VectorXd& eigenVector);
24 template void copyBppToEigen (const bpp::Vdouble& bppVector, ExtendedFloatRowVectorXd& eigenVector);
25 template void copyBppToEigen (const bpp::Vdouble& bppVector, ExtendedFloatVectorXd& eigenVector);
26 
27 
28 // template void copyBppToEigen(bpp::Matrix<double> const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>&);
29 
30 
31 void copyBppToEigen (const std::vector<ExtendedFloatVectorXd>& bppVector, ExtendedFloatMatrixXd& eigenVector)
32 {
33  // Look for largest extendedfloat
34  ExtendedFloat::ExtType maxE = std::max_element(bppVector.begin(), bppVector.end(), [](const ExtendedFloatVectorXd& lhs, const ExtendedFloatVectorXd& rhs){
35  return lhs.exponent_part() < rhs.exponent_part();
36  })->exponent_part();
37 
38  eigenVector.exponent_part() = maxE;
39  eigenVector.float_part() = Eigen::MatrixXd::NullaryExpr(static_cast<Eigen::Index>(bppVector[0].rows()),
40  static_cast<Eigen::Index>(bppVector.size()),
41  [&maxE, &bppVector](int i, int j){
42  return bppVector[(size_t)j].float_part()(i) * bpp::constexpr_power<double> (bpp::ExtendedFloat::radix, bppVector[(size_t)j].exponent_part () - maxE);
43  });
44 }
45 
46 
47 void copyBppToEigen (const std::vector<Eigen::VectorXd>& bppVector, Eigen::MatrixXd& eigenMatrix)
48 {
49  // Look for largest extendedfloat
50  const auto eigenRows = static_cast<Eigen::Index>(bppVector[0].rows());
51  const auto eigenCols = static_cast<Eigen::Index>(bppVector.size());
52 
53  eigenMatrix.resize (eigenRows, eigenCols);
54  eigenMatrix.fill(0);
55  for (Eigen::Index j = 0; j < eigenCols; ++j)
56  {
57  for (Eigen::Index i = 0; i < eigenRows; ++i)
58  {
59  eigenMatrix (i, j) = bppVector[(std::size_t)j](i);
60  }
61  }
62 }
63 
64 
65 void copyBppToEigen (const bpp::Matrix<double>& bppMatrix, ExtendedFloatMatrixXd& eigenMatrix)
66 {
67  const auto eigenRows = static_cast<Eigen::Index>(bppMatrix.getNumberOfRows ());
68  const auto eigenCols = static_cast<Eigen::Index>(bppMatrix.getNumberOfColumns ());
69  eigenMatrix.resize (eigenRows, eigenCols);
70  eigenMatrix.fill(0);
71  for (Eigen::Index i = 0; i < eigenRows; ++i)
72  {
73  for (Eigen::Index j = 0; j < eigenCols; ++j)
74  {
75  eigenMatrix.float_part() (i, j) = bppMatrix (static_cast<std::size_t>(i), static_cast<std::size_t>(j));
76  }
77  }
78 #ifdef DEBUG
79  std::cerr << "=== copyBppToEigen(" << typeid(bppMatrix).name() << ", " << typeid(eigenMatrix).name() << ") ===" << std::endl;
80  std::cerr << &bppMatrix << std::endl;
81  std::cerr << eigenRows << "," << eigenCols << std::endl;
82  std::cerr << eigenMatrix << std::endl;
83  std::cerr << "=== end copyBppToEigen === " << std::endl;
84 #endif
85 }
86 
87 void copyBppToEigen (const bpp::Matrix<double>& bppMatrix, Eigen::MatrixXd& eigenMatrix)
88 {
89  const auto eigenRows = static_cast<Eigen::Index>(bppMatrix.getNumberOfRows ());
90  const auto eigenCols = static_cast<Eigen::Index>(bppMatrix.getNumberOfColumns ());
91  eigenMatrix.resize (eigenRows, eigenCols);
92  eigenMatrix.fill(0);
93  for (Eigen::Index i = 0; i < eigenRows; ++i)
94  {
95  for (Eigen::Index j = 0; j < eigenCols; ++j)
96  {
97  eigenMatrix (i, j) = bppMatrix (static_cast<std::size_t>(i), static_cast<std::size_t>(j));
98  }
99  }
100 #ifdef DEBUG
101  std::cerr << "copyBppToEigen(" << typeid(bppMatrix).name() << ", " << typeid(eigenMatrix).name() << ")" << std::endl;
102  std::cerr << &bppMatrix << std::endl;
103  std::cerr << eigenRows << "," << eigenCols << std::endl;
104  std::cerr << eigenMatrix << std::endl;
105 #endif
106 }
107 
108 
109 /*****************************************/
110 /* copyEigenToBpp */
111 
112 template void copyEigenToBpp(const ExtendedFloatMatrixXd& eigenMatrix, std::vector<std::vector<double>>& bppMatrix);
113 template void copyEigenToBpp(const ExtendedFloatMatrixXd& eigenMatrix, std::vector<std::vector<bpp::ExtendedFloat>>& bppMatrix);
114 
115 template void copyEigenToBpp(const Eigen::MatrixXd& eigenMatrix, std::vector<std::vector<double>>& bppMatrix);
116 template void copyEigenToBpp(const Eigen::MatrixXd& eigenMatrix, std::vector<std::vector<bpp::ExtendedFloat>>& bppMatrix);
117 
118 void copyEigenToBpp (const MatrixLik& eigenMatrix, bpp::Matrix<double>& bppMatrix)
119 {
120  const auto eigenRows = static_cast<std::size_t>(eigenMatrix.rows());
121  const auto eigenCols = static_cast<std::size_t>(eigenMatrix.cols());
122 
123  bppMatrix.resize (eigenRows, eigenCols);
124  for (size_t i = 0; i < eigenRows; ++i)
125  {
126  for (size_t j = 0; j < eigenCols; ++j)
127  {
128  bppMatrix(i, j) = convert (eigenMatrix (static_cast<Eigen::Index>(i), static_cast<Eigen::Index>(j)));
129  }
130  }
131 }
132 
133 template void copyEigenToBpp (const RowLik& eigenVector, Vdouble& bppVector);
134 template void copyEigenToBpp (const VectorLik& eigenVector, Vdouble& bppVector);
135 template void copyEigenToBpp (const RowLik& eigenVector, std::vector<ExtendedFloat>& bppVector);
136 template void copyEigenToBpp (const VectorLik& eigenVector, std::vector<ExtendedFloat>& bppVector);
137 
138 
139 /***************************************************/
140 /* to string */
141 
142 
143 std::string to_string (const NoDimension&)
144 {
145  return "()";
146 }
147 
148 std::string to_string (const MatrixDimension& dim)
149 {
150  return "(" + std::to_string (dim.rows) + "," + std::to_string (dim.cols) + ")";
151 }
152 
153 std::size_t hash (const MatrixDimension& dim)
154 {
155  std::size_t seed = 0;
156  combineHash (seed, dim.rows);
157  combineHash (seed, dim.cols);
158  return seed;
159 }
160 
161 namespace numeric
162 {
164 {
165  if (dim.rows != dim.cols)
166  {
167  throw Exception ("MatrixDimension is not square: " + std::to_string (dim.rows) + "x" +
168  std::to_string (dim.cols));
169  }
170 }
171 } // namespace numeric
172 
173 void checkRecreateWithoutDependencies (const std::type_info& contextNodeType, const NodeRefVec& deps)
174 {
175  if (!deps.empty ())
176  {
177  throw Exception (prettyTypeName (contextNodeType) +
178  "recreate called with dependencies, but node does not have dependencies");
179  }
180 }
181 
182 // Precompiled instantiations of numeric nodes
183 template class ConstantZero<uint>;
184 template class ConstantZero<double>;
185 template class ConstantZero<char>;
186 template class ConstantZero<std::string>;
187 
188 template class ConstantZero<Parameter>;
189 template class ConstantZero<TransitionFunction>;
190 
191 template class ConstantZero<Eigen::VectorXd>;
192 template class ConstantZero<Eigen::RowVectorXd>;
193 template class ConstantZero<Eigen::MatrixXd>;
194 
195 template class ConstantZero<ExtendedFloatVectorXd>;
196 template class ConstantZero<ExtendedFloatRowVectorXd>;
197 template class ConstantZero<ExtendedFloatMatrixXd>;
198 
199 
200 template class ConstantOne<uint>;
201 template class ConstantOne<double>;
202 template class ConstantOne<char>;
203 template class ConstantOne<std::string>;
204 
205 template class ConstantOne<Parameter>;
206 template class ConstantOne<TransitionFunction>;
207 
208 template class ConstantOne<Eigen::VectorXd>;
209 template class ConstantOne<Eigen::RowVectorXd>;
210 template class ConstantOne<Eigen::MatrixXd>;
211 
212 template class ConstantOne<ExtendedFloatVectorXd>;
213 template class ConstantOne<ExtendedFloatRowVectorXd>;
214 template class ConstantOne<ExtendedFloatMatrixXd>;
215 
216 
217 template class Identity<double>;
218 template class Identity<ExtendedFloatMatrixXd>;
219 template class Identity<Eigen::MatrixXd>;
220 
221 
222 template class NumericConstant<uint>;
223 template class NumericConstant<double>;
224 template class NumericConstant<size_t>;
225 template class NumericConstant<std::string>;
226 
227 template class NumericConstant<ExtendedFloatVectorXd>;
228 template class NumericConstant<ExtendedFloatRowVectorXd>;
229 template class NumericConstant<ExtendedFloatMatrixXd>;
230 
231 NumericConstant<char> NodeX('X');
232 
233 template class NumericConstant<Eigen::VectorXd>;
234 template class NumericConstant<Eigen::RowVectorXd>;
235 template class NumericConstant<Eigen::MatrixXd>;
236 
237 template class NumericMutable<uint>;
238 template class NumericMutable<double>;
239 
240 template class NumericMutable<ExtendedFloatVectorXd>;
241 template class NumericMutable<ExtendedFloatRowVectorXd>;
242 template class NumericMutable<ExtendedFloatMatrixXd>;
243 
244 template class NumericMutable<Eigen::VectorXd>;
245 template class NumericMutable<Eigen::RowVectorXd>;
246 template class NumericMutable<Eigen::MatrixXd>;
247 
248 template class Convert<double, double>;
249 
250 template class Convert<ExtendedFloatVectorXd, double>;
251 template class Convert<ExtendedFloatVectorXd, ExtendedFloatVectorXd>;
252 template class Convert<ExtendedFloatVectorXd, Eigen::VectorXd>;
253 template class Convert<ExtendedFloatVectorXd, Eigen::VectorXi>;
254 template class Convert<ExtendedFloatVectorXd, Transposed<ExtendedFloatRowVectorXd>>;
255 template class Convert<ExtendedFloatVectorXd, Transposed<Eigen::RowVectorXd>>;
256 
257 template class Convert<Eigen::VectorXd, ExtendedFloatVectorXd>;
258 
259 template class Convert<ExtendedFloatRowVectorXd, double>;
260 template class Convert<ExtendedFloatRowVectorXd, ExtendedFloatRowVectorXd>;
261 template class Convert<ExtendedFloatRowVectorXd, Eigen::RowVectorXd>;
262 template class Convert<ExtendedFloatRowVectorXd, Eigen::RowVectorXi>;
263 template class Convert<ExtendedFloatRowVectorXd, Transposed<ExtendedFloatVectorXd>>;
264 template class Convert<ExtendedFloatRowVectorXd, Transposed<Eigen::VectorXd>>;
265 
266 template class Convert<ExtendedFloatMatrixXd, double>;
267 template class Convert<ExtendedFloatMatrixXd, ExtendedFloatMatrixXd>;
268 template class Convert<ExtendedFloatMatrixXd, Eigen::MatrixXd>;
269 template class Convert<ExtendedFloatMatrixXd, Transposed<ExtendedFloatMatrixXd>>;
270 template class Convert<ExtendedFloatMatrixXd, Transposed<Eigen::MatrixXd>>;
271 
272 template class Convert<Eigen::VectorXd, double>;
273 template class Convert<Eigen::VectorXd, Eigen::VectorXd>;
274 template class Convert<Eigen::VectorXd, Eigen::VectorXi>;
275 template class Convert<Eigen::VectorXd, Transposed<Eigen::RowVectorXd>>;
276 
277 template class Convert<Eigen::RowVectorXd, double>;
278 template class Convert<Eigen::RowVectorXd, Eigen::RowVectorXd>;
279 template class Convert<Eigen::RowVectorXd, Eigen::RowVectorXi>;
280 template class Convert<Eigen::RowVectorXd, Transposed<Eigen::VectorXd>>;
281 
282 template class Convert<Eigen::MatrixXd, double>;
283 template class Convert<Eigen::MatrixXd, Eigen::MatrixXd>;
284 template class Convert<Eigen::MatrixXd, Transposed<Eigen::MatrixXd>>;
285 } // namespace bpp
virtual const ExtType & exponent_part() const
virtual const MatType & float_part() const
Eigen::Index cols() const
Eigen::Index rows() const
void resize(Eigen::Index rows, Eigen::Index cols)
virtual size_t getNumberOfColumns() const=0
virtual void resize(size_t nRows, size_t nCols)=0
virtual size_t getNumberOfRows() const=0
void checkDimensionIsSquare(const MatrixDimension &dim)
R convert(const F &from, const Dimension< R > &)
Defines the basic types of data flow nodes.
std::size_t hash(const MatrixDimension &dim)
std::vector< double > Vdouble
NumericConstant< char > NodeX('X')
template void copyEigenToBpp(const RowLik &eigenVector, std::vector< ExtendedFloat > &bppVector)
void checkRecreateWithoutDependencies(const std::type_info &contextNodeType, const NodeRefVec &deps)
std::vector< NodeRef > NodeRefVec
Alias for a dependency vector (of NodeRef).
Definition: DataFlow.h:81
std::string prettyTypeName(const std::type_info &ti)
Debug: return a readable name for a C++ type descriptor (from typeid operator).
Definition: DataFlow.cpp:43
std::string to_string(const MatrixDimension &dim)
void combineHash(std::size_t &seed, const T &t)
Combine hashable value to a hash, from Boost library.
Definition: DataFlow.h:33
void copyBppToEigen(const bpp::Matrix< double > &bppMatrix, Eigen::MatrixXd &eigenMatrix)
Basic matrix dimension type.
Empty type representing no dimensions.