All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Groups
multiply.hpp
Go to the documentation of this file.
#ifndef STAN_MATH_FWD_MAT_FUN_MULTIPLY_HPP
#define STAN_MATH_FWD_MAT_FUN_MULTIPLY_HPP
#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/fun/typedefs.hpp>
#include <stan/math/prim/mat/err/check_multiplicable.hpp>
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/mat/fun/typedefs.hpp>
#include <stan/math/fwd/mat/fun/to_fvar.hpp>
#include <stan/math/fwd/mat/fun/dot_product.hpp>
#include <boost/math/tools/promotion.hpp>
#include <vector>
namespace stan {
namespace math {
template<typename T, int R1, int C1>
inline
Eigen::Matrix<fvar<T>, R1, C1>
multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m, const fvar<T>& c) {
Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.cols(); j++)
res(i, j) = c * m(i, j);
}
return res;
}
template<typename T, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R2, C2>
multiply(const Eigen::Matrix<fvar<T>, R2, C2>& m, const double c) {
Eigen::Matrix<fvar<T>, R2, C2> res(m.rows(), m.cols());
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.cols(); j++)
res(i, j) = c * m(i, j);
}
return res;
}
template<typename T, int R1, int C1>
inline
Eigen::Matrix<fvar<T>, R1, C1>
multiply(const Eigen::Matrix<double, R1, C1>& m, const fvar<T>& c) {
Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.cols(); j++)
res(i, j) = c * m(i, j);
}
return res;
}
template<typename T, int R1, int C1>
inline
Eigen::Matrix<fvar<T>, R1, C1>
multiply(const fvar<T>& c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
return multiply(m, c);
}
template<typename T, int R1, int C1>
inline
Eigen::Matrix<fvar<T>, R1, C1>
multiply(const double c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
return multiply(m, c);
}
template<typename T, int R1, int C1>
inline
Eigen::Matrix<fvar<T>, R1, C1>
multiply(const fvar<T>& c, const Eigen::Matrix<double, R1, C1>& m) {
return multiply(m, c);
}
template<typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
stan::math::check_multiplicable("multiply",
"m1", m1,
"m2", m2);
Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
for (size_type i = 0; i < m1.rows(); i++) {
Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
for (size_type j = 0; j < m2.cols(); j++) {
Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
result(i, j) = stan::math::dot_product(crow, ccol);
}
}
return result;
}
template<typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
const Eigen::Matrix<double, R2, C2>& m2) {
stan::math::check_multiplicable("multiply",
"m1", m1,
"m2", m2);
Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
for (size_type i = 0; i < m1.rows(); i++) {
Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
for (size_type j = 0; j < m2.cols(); j++) {
Eigen::Matrix<double, R2, 1> ccol = m2.col(j);
result(i, j) = stan::math::dot_product(crow, ccol);
}
}
return result;
}
template<typename T, int R1, int C1, int R2, int C2>
inline
Eigen::Matrix<fvar<T>, R1, C2>
multiply(const Eigen::Matrix<double, R1, C1>& m1,
const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
stan::math::check_multiplicable("multiply",
"m1", m1,
"m2", m2);
Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
for (size_type i = 0; i < m1.rows(); i++) {
Eigen::Matrix<double, 1, C1> crow = m1.row(i);
for (size_type j = 0; j < m2.cols(); j++) {
Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
result(i, j) = stan::math::dot_product(crow, ccol);
}
}
return result;
}
template <typename T, int C1, int R2>
inline
fvar<T>
multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
const Eigen::Matrix<fvar<T>, R2, 1>& v) {
if (rv.size() != v.size())
throw std::domain_error("row vector and vector must be same length "
"in multiply");
return dot_product(rv, v);
}
template <typename T, int C1, int R2>
inline
fvar<T>
multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
const Eigen::Matrix<double, R2, 1>& v) {
if (rv.size() != v.size())
throw std::domain_error("row vector and vector must be same length "
"in multiply");
return dot_product(rv, v);
}
template <typename T, int C1, int R2>
inline
fvar<T>
multiply(const Eigen::Matrix<double, 1, C1>& rv,
const Eigen::Matrix<fvar<T>, R2, 1>& v) {
if (rv.size() != v.size())
throw std::domain_error("row vector and vector must be same length "
"in multiply");
return dot_product(rv, v);
}
}
}
#endif
typedefs.hpp
core.hpp
check_multiplicable.hpp
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
stan::math::size_type
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:13
stan::math::check_multiplicable
bool check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Return true if the matrices can be multiplied.
Definition: check_multiplicable.hpp:33
dot_product.hpp
stan::math::dot_product
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20
stan::math::domain_error
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.
Definition: domain_error.hpp:32
Eigen.hpp
to_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
typedefs.hpp

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