Automatic Differentiation
 
Loading...
Searching...
No Matches
to_array_1d.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_TO_ARRAY_1D_HPP
2#define STAN_MATH_PRIM_FUN_TO_ARRAY_1D_HPP
3
6#include <vector>
7
8namespace stan {
9namespace math {
10
11// real[] to_array_1d(matrix)
12// real[] to_array_1d(row_vector)
13// real[] to_array_1d(vector)
14template <typename EigMat, require_eigen_t<EigMat>* = nullptr>
15inline std::vector<value_type_t<EigMat>> to_array_1d(const EigMat& matrix) {
16 using T_val = value_type_t<EigMat>;
17 std::vector<T_val> result(matrix.size());
18 Eigen::Map<Eigen::Matrix<T_val, EigMat::RowsAtCompileTime,
19 EigMat::ColsAtCompileTime>>(
20 result.data(), matrix.rows(), matrix.cols())
21 = matrix;
22 return result;
23}
24
25// real[] to_array_1d(...)
26template <typename T>
27inline std::vector<T> to_array_1d(const std::vector<T>& x) {
28 return x;
29}
30
31// real[] to_array_1d(...)
32template <typename T>
33inline std::vector<typename scalar_type<T>::type> to_array_1d(
34 const std::vector<std::vector<T>>& x) {
35 size_t size1 = x.size();
36 size_t size2 = 0;
37 if (size1 != 0) {
38 size2 = x[0].size();
39 }
40 std::vector<T> y(size1 * size2);
41 for (size_t i = 0, ij = 0; i < size1; i++) {
42 for (size_t j = 0; j < size2; j++, ij++) {
43 y[ij] = x[i][j];
44 }
45 }
46 return to_array_1d(y);
47}
48
49} // namespace math
50} // namespace stan
51#endif
auto to_array_1d(T_x &&x)
Returns input matrix reshaped into a vector.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9