| Stan Math Library 2.12.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

assign.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_ASSIGN_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_ASSIGN_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/scal/err/invalid_argument.hpp>
6 #include <stan/math/prim/scal/err/check_size_match.hpp>
7 #include <stan/math/prim/arr/err/check_matching_sizes.hpp>
8 #include <stan/math/prim/mat/err/check_matching_dims.hpp>
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 #include <string>
13 #include <vector>
14
15 namespace stan {
16 namespace math {
17
25 inline void print_mat_size(int n, std::ostream& o) {
26 if (n == Eigen::Dynamic)
27 o << "dynamically sized";
28 else
29 o << n;
30 }
31
46 template <typename LHS, typename RHS>
47 inline void
48 assign(LHS& lhs, const RHS& rhs) {
49 lhs = rhs;
50 }
51
72 template <typename LHS, typename RHS, int R1, int C1, int R2, int C2>
73 inline void
74 assign(Eigen::Matrix<LHS, R1, C1>& x,
75 const Eigen::Matrix<RHS, R2, C2>& y) {
76 std::stringstream ss;
77 ss << "shapes must match, but found"
78 << " left-hand side rows=";
79 print_mat_size(R1, ss);
80 ss << "; left-hand side cols=";
81 print_mat_size(C1, ss);
82 ss << "; right-hand side rows=";
83 print_mat_size(R2, ss);
84 ss << "; right-hand side cols=";
85 print_mat_size(C2, ss);
86 std::string ss_str(ss.str());
87 invalid_argument("assign(Eigen::Matrix, Eigen::Matrix)",
88 "", "", ss_str.c_str());
89 }
90
108 template <typename LHS, typename RHS, int R, int C>
109 inline void
110 assign(Eigen::Matrix<LHS, R, C>& x,
111 const Eigen::Matrix<RHS, R, C>& y) {
112 check_matching_dims("assign",
113 "left-hand-side", x,
114 "right-hand-side", y);
115 for (int i = 0; i < x.size(); ++i)
116 assign(x(i), y(i));
117 }
118
137 template <typename LHS, typename RHS, int R, int C>
138 inline void
139 assign(Eigen::Block<LHS> x,
140 const Eigen::Matrix<RHS, R, C>& y) {
141 check_size_match("assign",
142 "left-hand side rows", x.rows(),
143 "right-hand side rows", y.rows());
144 check_size_match("assign",
145 "left-hand side cols", x.cols(),
146 "right-hand side cols", y.cols());
147 for (int n = 0; n < y.cols(); ++n)
148 for (int m = 0; m < y.rows(); ++m)
149 assign(x(m, n), y(m, n));
150 }
151
171 template <typename LHS, typename RHS>
172 inline void
173 assign(std::vector<LHS>& x, const std::vector<RHS>& y) {
174 check_matching_sizes("assign",
175 "left-hand side", x,
176 "right-hand side", y);
177 for (size_t i = 0; i < x.size(); ++i)
178 assign(x[i], y[i]);
179 }
180
181 }
182 }
183 #endif
check_matching_dims.hpp
check_size_match.hpp
stan
Definition: log_sum_exp.hpp:8
invalid_argument.hpp
check_matching_sizes.hpp
stan::math::check_matching_dims
bool check_matching_dims(const char *function, const char *name1, const Eigen::Matrix< T1, R1, C1 > &y1, const char *name2, const Eigen::Matrix< T2, R2, C2 > &y2)
Return true if the two matrices are of the same size.
Definition: check_matching_dims.hpp:37
stan::math::invalid_argument
void invalid_argument(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw an invalid_argument exception with a consistently formatted message.
Definition: invalid_argument.hpp:31
stan::math::check_size_match
bool check_size_match(const char *function, const char *name_i, T_size1 i, const char *name_j, T_size2 j)
Return true if the provided sizes match.
Definition: check_size_match.hpp:30
stan::math::check_matching_sizes
bool check_matching_sizes(const char *function, const char *name1, const T_y1 &y1, const char *name2, const T_y2 &y2)
Return true if two structures at the same size.
Definition: check_matching_sizes.hpp:29
Eigen.hpp
stan::math::assign
void assign(LHS &lhs, const RHS &rhs)
Copy the right-hand side's value to the left-hand side variable.
Definition: assign.hpp:48
stan::math::print_mat_size
void print_mat_size(int n, std::ostream &o)
Helper function to return the matrix size as either "dynamic" or "1".
Definition: assign.hpp:25

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