Stan Math Library  2.20.0
reverse mode automatic differentiation
mdivide_left_tri.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_MDIVIDE_LEFT_TRI_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_MDIVIDE_LEFT_TRI_HPP
3 
4 #include <boost/math/tools/promotion.hpp>
9 #ifdef STAN_OPENCL
15 #endif
16 namespace stan {
17 namespace math {
18 
35 template <int TriView, typename T1, typename T2, int R1, int C1, int R2, int C2>
36 inline Eigen::Matrix<typename boost::math::tools::promote_args<T1, T2>::type,
37  R1, C2>
38 mdivide_left_tri(const Eigen::Matrix<T1, R1, C1> &A,
39  const Eigen::Matrix<T2, R2, C2> &b) {
40  check_square("mdivide_left_tri", "A", A);
41  check_multiplicable("mdivide_left_tri", "A", A, "b", b);
42  return promote_common<Eigen::Matrix<T1, R1, C1>, Eigen::Matrix<T2, R1, C1> >(
43  A)
44  .template triangularView<TriView>()
45  .solve(
46  promote_common<Eigen::Matrix<T1, R2, C2>, Eigen::Matrix<T2, R2, C2> >(
47  b));
48 }
49 
59 template <int TriView, typename T, int R1, int C1>
60 inline Eigen::Matrix<T, R1, C1> mdivide_left_tri(
61  const Eigen::Matrix<T, R1, C1> &A) {
62  check_square("mdivide_left_tri", "A", A);
63  int n = A.rows();
64  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> b;
65  b.setIdentity(n, n);
66  A.template triangularView<TriView>().solveInPlace(b);
67  return b;
68 }
69 
85 template <int TriView, int R1, int C1, int R2, int C2>
86 inline Eigen::Matrix<double, R1, C2> mdivide_left_tri(
87  const Eigen::Matrix<double, R1, C1> &A,
88  const Eigen::Matrix<double, R2, C2> &b) {
89  check_square("mdivide_left_tri", "A", A);
90  check_multiplicable("mdivide_left_tri", "A", A, "b", b);
91 #ifdef STAN_OPENCL
92  if (A.rows()
94  matrix_cl A_cl(A);
95  matrix_cl b_cl(b);
96  matrix_cl A_inv_cl(A.rows(), A.cols());
97  if (TriView == Eigen::Lower) {
98  A_inv_cl = tri_inverse<TriangularViewCL::Lower>(A_cl);
99  } else {
100  A_inv_cl = tri_inverse<TriangularViewCL::Upper>(A_cl);
101  }
102  matrix_cl C_cl = A_inv_cl * b_cl;
103  return from_matrix_cl(C_cl);
104  } else {
105 #endif
106  return A.template triangularView<TriView>().solve(b);
107 #ifdef STAN_OPENCL
108  }
109 #endif
110 }
111 
123 template <int TriView, int R1, int C1>
124 inline Eigen::Matrix<double, R1, C1> mdivide_left_tri(
125  const Eigen::Matrix<double, R1, C1> &A) {
126  check_square("mdivide_left_tri", "A", A);
127  const int n = A.rows();
128 #ifdef STAN_OPENCL
129  if (A.rows()
131  matrix_cl A_cl(A);
132  if (TriView == Eigen::Lower) {
133  A_cl = tri_inverse<TriangularViewCL::Lower>(A_cl);
134  } else {
135  A_cl = tri_inverse<TriangularViewCL::Upper>(A_cl);
136  }
137  return from_matrix_cl(A_cl);
138  } else {
139 #endif
140  Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> b;
141  b.setIdentity(n, n);
142  A.template triangularView<TriView>().solveInPlace(b);
143  return b;
144 #ifdef STAN_OPENCL
145  }
146 #endif
147 }
148 
149 } // namespace math
150 } // namespace stan
151 #endif
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > from_matrix_cl(const matrix_cl &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix...
Definition: copy.hpp:70
void check_square(const char *function, const char *name, const matrix_cl &y)
Check if the matrix_cl is square.
The API to access the methods and values in opencl_context_base.
opencl_context_base::tuning_struct & tuning_opts()
Returns the thread block size for the Cholesky Decompositions L_11.
common_type< T1, T2 >::type promote_common(const F &u)
Return the result of promoting either a scalar or the scalar elements of a container to either of two...
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left_tri(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Returns the solution of the system Ax=b when A is triangular.
Represents a matrix on the OpenCL device.
Definition: matrix_cl.hpp:29
Initialization for OpenCL:
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.

     [ Stan Home Page ] © 2011–2018, Stan Development Team.