Automatic Differentiation
 
Loading...
Searching...
No Matches
matrix_power.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_PRIM_MATRIX_POWER_HPP
2#define STAN_MATH_OPENCL_PRIM_MATRIX_POWER_HPP
3#ifdef STAN_OPENCL
9
10namespace stan {
11namespace math {
12
25template <typename T_m,
26 require_all_kernel_expressions_and_none_scalar_t<T_m>* = nullptr>
27inline plain_type_t<T_m> matrix_power(T_m&& M, const int n) {
28 const char* function = "matrix_power(OpenCL)";
29 check_square(function, "M", M);
30 check_nonnegative(function, "n", n);
31 plain_type_t<T_m> MM = std::forward<T_m>(M);
32 check_cl(function, "M", value_of(MM), "finite") = isfinite(value_of(MM));
33 if (n == 0)
34 return diag_matrix(constant(1.0, M.rows(), 1));
35 plain_type_t<T_m> result = MM;
36 for (int nn = n - 1; nn > 0; nn /= 2) {
37 if (nn % 2 == 1) {
38 result = result * MM;
39 --nn;
40 }
41 MM = MM * MM;
42 }
43 return result;
44}
45} // namespace math
46} // namespace stan
47
48#endif
49#endif
isfinite_< as_operation_cl_t< T > > isfinite(T &&a)
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
Definition check_cl.hpp:219
auto constant(const T a, int rows, int cols)
Matrix of repeated values in kernel generator expressions.
Definition constant.hpp:130
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void check_nonnegative(const char *function, const char *name, const T_y &y)
Check if y is non-negative.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
plain_type_t< T_m > matrix_power(T_m &&M, const int n)
Returns the nth power of the specific matrix.
auto diag_matrix(T_x &&x)
Return a square diagonal matrix with the specified vector of coefficients as the diagonal values.
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...