File: promote_scalar.hpp

package info (click to toggle)
r-cran-stanheaders 2.21.0-7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 14,372 kB
  • sloc: cpp: 94,620; ansic: 52,526; f90: 2,136; sh: 13; makefile: 5
file content (101 lines) | stat: -rw-r--r-- 3,571 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#ifndef STAN_MATH_PRIM_MAT_FUN_PROMOTE_SCALAR_HPP
#define STAN_MATH_PRIM_MAT_FUN_PROMOTE_SCALAR_HPP

#include <stan/math/prim/scal/fun/promote_scalar.hpp>
#include <stan/math/prim/mat/fun/promote_scalar_type.hpp>
#include <stan/math/prim/mat/fun/Eigen.hpp>

namespace stan {
namespace math {

/**
 * Struct to hold static function for promoting underlying scalar
 * types.  This specialization is for Eigen matrix inputs.
 *
 * @tparam T return scalar type
 * @tparam S input matrix scalar type for static nested function, which must
 * have a scalar type assignable to T
 */
template <typename T, typename S>
struct promote_scalar_struct<T, Eigen::Matrix<S, -1, -1> > {
  /**
   * Return the matrix consisting of the recursive promotion of
   * the elements of the input matrix to the scalar type specified
   * by the return template parameter.
   *
   * @param x input matrix.
   * @return matrix with values promoted from input vector.
   */
  static Eigen::Matrix<typename promote_scalar_type<T, S>::type, -1, -1> apply(
      const Eigen::Matrix<S, -1, -1>& x) {
    Eigen::Matrix<typename promote_scalar_type<T, S>::type, -1, -1> y(x.rows(),
                                                                      x.cols());
    for (int i = 0; i < x.size(); ++i) {
      y(i) = promote_scalar_struct<T, S>::apply(x(i));
    }
    return y;
  }
};

/**
 * Struct to hold static function for promoting underlying scalar
 * types.  This specialization is for Eigen column vector inputs.
 *
 * @tparam T return scalar type
 * @tparam S input matrix scalar type for static nested function, which must
 * have a scalar type assignable to T
 */
template <typename T, typename S>
struct promote_scalar_struct<T, Eigen::Matrix<S, 1, -1> > {
  /**
   * Return the column vector consisting of the recursive promotion of
   * the elements of the input column vector to the scalar type specified
   * by the return template parameter.
   *
   * @param x input column vector.
   * @return column vector with values promoted from input vector.
   */
  static Eigen::Matrix<typename promote_scalar_type<T, S>::type, 1, -1> apply(
      const Eigen::Matrix<S, 1, -1>& x) {
    Eigen::Matrix<typename promote_scalar_type<T, S>::type, 1, -1> y(x.rows(),
                                                                     x.cols());
    for (int i = 0; i < x.size(); ++i) {
      y(i) = promote_scalar_struct<T, S>::apply(x(i));
    }
    return y;
  }
};

/**
 * Struct to hold static function for promoting underlying scalar
 * types.  This specialization is for Eigen row vector inputs.
 *
 * @tparam T return scalar type
 * @tparam S input matrix scalar type for static nested function, which must
 * have a scalar type assignable to T
 */
template <typename T, typename S>
struct promote_scalar_struct<T, Eigen::Matrix<S, -1, 1> > {
  /**
   * Return the row vector consisting of the recursive promotion of
   * the elements of the input row vector to the scalar type specified
   * by the return template parameter.
   *
   * @param x input row vector.
   * @return row vector with values promoted from input vector.
   */
  static Eigen::Matrix<typename promote_scalar_type<T, S>::type, -1, 1> apply(
      const Eigen::Matrix<S, -1, 1>& x) {
    Eigen::Matrix<typename promote_scalar_type<T, S>::type, -1, 1> y(x.rows(),
                                                                     x.cols());
    for (int i = 0; i < x.size(); ++i) {
      y(i) = promote_scalar_struct<T, S>::apply(x(i));
    }
    return y;
  }
};

}  // namespace math
}  // namespace stan

#endif