From 956a9bbd10ddb7b6c93e5a79ea31410404a5ab51 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Tue, 1 Dec 2015 13:02:35 -0500 Subject: [PATCH 01/36] scalar unary vectorization w. mock tests --- .../fwd/mat/vectorize/apply_scalar_unary.hpp | 42 +++++ .../prim/mat/vectorize/apply_scalar_unary.hpp | 160 ++++++++++++++++++ .../rev/mat/vectorize/apply_scalar_unary.hpp | 39 +++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 114 +++++++++++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 128 ++++++++++++++ .../mat/vectorize/expect_match_return_t.hpp | 31 ++++ test/unit/math/prim/mat/vectorize/foo_fun.hpp | 28 +++ .../mat/vectorize/apply_scalar_unary_test.cpp | 114 +++++++++++++ 8 files changed, 656 insertions(+) create mode 100644 stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp create mode 100644 stan/math/prim/mat/vectorize/apply_scalar_unary.hpp create mode 100644 stan/math/rev/mat/vectorize/apply_scalar_unary.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp create mode 100644 test/unit/math/prim/mat/vectorize/foo_fun.hpp create mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp diff --git a/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp b/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp new file mode 100644 index 00000000000..a27727a4465 --- /dev/null +++ b/stan/math/fwd/mat/vectorize/apply_scalar_unary.hpp @@ -0,0 +1,42 @@ +#ifndef STAN_MATH_FWD_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP +#define STAN_MATH_FWD_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP + +#include +#include + +namespace stan { + + namespace math { + + /** + * Template specialization to fvar for vectorizing a unary scalar + * function. This is a base scalar specialization. It applies + * the function specified by the template parameter to the + * argument. + * + * @tparam F Type of function to apply. + * @tparam T Value and tangent type for for forward-mode + * autodiff variable. + */ + template + struct apply_scalar_unary > { + /** + * Function return type, which is same as the argument type for + * the function, fvar<T>. + */ + typedef stan::math::fvar return_t; + + /** + * Apply the function specified by F to the specified argument. + * + * @param x Argument variable. + * @return Function applied to the variable. + */ + static inline return_t apply(const stan::math::fvar& x) { + return F::fun(x); + } + }; + + } +} +#endif diff --git a/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp new file mode 100644 index 00000000000..cb93efc1c00 --- /dev/null +++ b/stan/math/prim/mat/vectorize/apply_scalar_unary.hpp @@ -0,0 +1,160 @@ +#ifndef STAN_MATH_PRIM_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP +#define STAN_MATH_PRIM_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP + +#include +#include + +namespace stan { + + namespace math { + + /** + * Base template class for vectorization of unary scalar functions + * defined by a template class F to a scalar, + * standard library vector, or Eigen dense matrix expression + * template. + * + *

The base class applies to any Eigen dense matrix expression + * template. Specializations define applications to scalars + * (primitive or autodiff in the corresponding autodiff library + * directories) or to standard library vectors of vectorizable + * types (primitives, Eigen dense matrix expressions, or further + * standard vectors). + * + *

Each specialization must define the typedef + * return_t for the vectorized return type and the + * function apply which defines the vectorization or + * base application of the function defined statically by the + * class F. The function definition class F defines a static + * function fun(), which defines the function's + * behavior on scalars. + * + * @tparam F Type of function to apply. + * @tparam T Type of argument to which function is applied. + */ + template + struct apply_scalar_unary { + /** + * Type of underlying scalar for the matrix type T. + */ + typedef typename Eigen::internal::traits::Scalar scalar_t; + + /** + * Return type for applying the function elementwise to a matrix + * expression template of type T. + */ + typedef Eigen::Matrix + return_t; + + /** + * Return the result of applying the function defined by the + * template parameter F to the specified matrix argument. + * + * @param x Matrix to which operation is applied. + * @return Componentwise application of the function specified + * by F to the specified matrix. + */ + static inline return_t apply(const T& x) { + return_t result(x.rows(), x.cols()); + for (int j = 0; j < x.cols(); ++j) + for (int i = 0; i < x.rows(); ++i) + result(i, j) = apply_scalar_unary::apply(x(i, j)); + return result; + } + }; + + /** + * Template specialization for vectorized functions applying to + * double arguments. + * + * @tparam F Type of function defining static apply function. + */ + template + struct apply_scalar_unary { + /** + * The return type, double. + */ + typedef double return_t; + + /** + * Apply the function specified by F to the specified argument. + * This is defined through a direct application of + * F::fun(), which must be defined for double + * arguments. + * + * @param x Argument scalar. + * @return Result of applying F to the scalar. + */ + static inline return_t apply(double x) { + return F::fun(x); + } + }; + + /** + * Template specialization for vectorized functions applying to + * integer arguments. Although the argument is integer, the + * return type is specified as double. This allows promotion of + * integers to doubles in vectorized functions, or in containers. + * + * @tparam F Type of function defining static apply function. + */ + template + struct apply_scalar_unary { + /** + * The return type, double. + */ + typedef double return_t; + + /** + * Apply the function specified by F to the specified argument. + * This is defined through a direct application of + * F::fun(), which must be defined for double + * arguments. + * + * @param x Argument scalar. + * @return Result of applying F to the scalar. + */ + static inline return_t apply(int x) { + return F::fun(static_cast(x)); + } + }; + + /** + * Template specialization for vectorized functions applying to + * standard vector containers. The lowest-level scalar type of + * the argument will determine the return type. Integers are + * promoted to double values. + * + * @tparam F Class defining a static apply function. + * @tparam T Type of element contained in standard vector. + */ + template + struct apply_scalar_unary > { + /** + * Return type, which is calculated recursively as a standard + * vector of the return type of the contained type T. + */ + typedef typename std::vector::return_t> + return_t; + + /** + * Apply the function specified by F elementwise to the + * specified argument. This is defined recursively through this + * class applied to elements of type T. + * + * @param x Argument container. + * @return Elementwise application of F to the elements of the + * container. + */ + static inline return_t apply(const std::vector& x) { + return_t fx(x.size()); + for (size_t i = 0; i < x.size(); ++i) + fx[i] = apply_scalar_unary::apply(x[i]); + return fx; + } + }; + + } +} +#endif diff --git a/stan/math/rev/mat/vectorize/apply_scalar_unary.hpp b/stan/math/rev/mat/vectorize/apply_scalar_unary.hpp new file mode 100644 index 00000000000..d16755de213 --- /dev/null +++ b/stan/math/rev/mat/vectorize/apply_scalar_unary.hpp @@ -0,0 +1,39 @@ +#ifndef STAN_MATH_REV_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP +#define STAN_MATH_REV_MAT_VECTORIZE_APPLY_UNARY_SCALAR_HPP + +#include +#include + +namespace stan { + + namespace math { + + /** + * Template specialization to var for vectorizing a unary scalar + * function. This is a base scalar specialization. It applies + * the function specified by the template parameter to the + * argument. + * + * @tparam F Type of function to apply. + */ + template + struct apply_scalar_unary { + /** + * Function return type, which is var. + */ + typedef stan::math::var return_t; + + /** + * Apply the function specified by F to the specified argument. + * + * @param x Argument variable. + * @return Function applied to the variable. + */ + static inline return_t apply(const stan::math::var& x) { + return F::fun(x); + } + }; + + } +} +#endif diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp new file mode 100644 index 00000000000..562f79d6aaf --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +TEST(RevMatVectorize,applyScalarUnary) { + using stan::math::fvar; + using stan::math::foo; + using stan::test::expect_match_return_t; + fvar three_var = 3; + fvar exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t, fvar >(); + expect_match_return_t >, std::vector > >(); +} + +TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { + using stan::math::foo; + using std::vector; + using stan::math::fvar; + using stan::math::foo; + + vector > y; + y.push_back(1.3); + y.push_back(-2.9); + + vector > fy = foo(y); + EXPECT_EQ(2, fy.size()); + EXPECT_FLOAT_EQ(std::exp(y[0].val()), fy[0].val()); + EXPECT_FLOAT_EQ(std::exp(y[1].val()), fy[1].val()); + + vector > z1; + z1.push_back(1.1); + z1.push_back(1.2); + vector > z2; + z2.push_back(2.1); + z2.push_back(2.2); + vector > > z; + z.push_back(z1); + z.push_back(z2); + vector > > fz = foo(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(z[i][j].val()), fz[i][j].val()); + } +} +TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { + using stan::math::fvar; + using stan::math::foo; + typedef Eigen::Matrix , Eigen::Dynamic, Eigen::Dynamic> MatrixXvar; + + MatrixXvar a(3,2); + a << 1, 2, 3, 4, 5, 6; + MatrixXvar fa = foo(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(a(i).val()), fa(i).val()); + + MatrixXvar fab = foo(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(std::exp(a(1,1).val()), fab(0,0).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryVector) { + using stan::math::fvar; + using stan::math::foo; + typedef Eigen::Matrix , Eigen::Dynamic, 1> VectorXvar; + + VectorXvar b(6); + b << 1, 2, 3, 4, 5, 6; + VectorXvar fb = foo(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(b(i).val()), fb(i).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { + using stan::math::fvar; + using stan::math::foo; + typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; + + RowVectorXvar c(6); + c << 1, 2, 3, 4, 5, 6; + RowVectorXvar fc = foo(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(c(i).val()), fc(i).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { + using stan::math::fvar; + using stan::math::foo; + using std::vector; + typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; + + RowVectorXvar d1(3); + d1 << 1, 2, 3; + RowVectorXvar d2(3); + d2 << 10, 20, 30; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = foo(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(d[i](j).val()), fd[i](j).val()); + } +} diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp new file mode 100644 index 00000000000..b82f9b06307 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include + +TEST(MathPrimMatVectorize, applyScalarUnaryReturnT) { + using stan::test::expect_match_return_t; + expect_match_return_t(); + typedef stan::math::apply_scalar_unary >::return_t + vec_double_return_t; + vec_double_return_t f; + f.push_back(3.7); + EXPECT_FLOAT_EQ(3.7, f[0]); + expect_match_return_t, std::vector >(); +} + +TEST(MathPrimMatVectorize, applyScalarUnaryScalar) { + using stan::math::foo; + + double exp_3 = foo(3); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3); + + double exp_4_d = foo(4.0); + EXPECT_FLOAT_EQ(std::exp(4.0), exp_4_d); + + EXPECT_FLOAT_EQ(std::exp(2.3), foo(2.3)); +} +TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { + using stan::math::foo; + using std::vector; + + vector y; + y.push_back(1.3); + y.push_back(-2.9); + + vector fy = foo(y); + EXPECT_EQ(2, fy.size()); + EXPECT_FLOAT_EQ(std::exp(y[0]), fy[0]); + EXPECT_FLOAT_EQ(std::exp(y[1]), fy[1]); + + vector z1; + z1.push_back(1.1); + z1.push_back(1.2); + vector z2; + z2.push_back(2.1); + z2.push_back(2.2); + vector > z; + z.push_back(z1); + z.push_back(z2); + vector > fz = foo(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(z[i][j]), fz[i][j]); + } + + vector u1; + u1.push_back(2); + u1.push_back(3); + vector u2 = foo(u1); + EXPECT_EQ(2, u2.size()); + for (int i = 0; i < u2.size(); ++i) { + EXPECT_FLOAT_EQ(std::exp(u1[i]), u2[i]); + } +} +TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { + using stan::math::foo; + using Eigen::MatrixXd; + + MatrixXd a(3,2); + a << 1, 2, 3, 4, 5, 6; + MatrixXd fa = foo(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(a(i)), fa(i)); + + MatrixXd fab = foo(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(std::exp(a(1,1)), fab(0,0)); +} +TEST(MathPrimMatVectorize, applyScalarUnaryVector) { + using stan::math::foo; + using Eigen::VectorXd; + VectorXd b(6); + b << 1, 2, 3, 4, 5, 6; + VectorXd fb = foo(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(b(i)), fb(i)); +} +TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { + using stan::math::foo; + using Eigen::RowVectorXd; + RowVectorXd c(6); + c << 1, 2, 3, 4, 5, 6; + RowVectorXd fc = foo(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(c(i)), fc(i)); +} +TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { + using stan::math::foo; + using std::vector; + using Eigen::MatrixXd; + using Eigen::VectorXd; + using Eigen::RowVectorXd; + + RowVectorXd d1(3); + d1 << 1, 2, 3; + RowVectorXd d2(3); + d2 << 10, 20, 30; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = foo(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(d[i](j)), fd[i](j)); + } +} + + diff --git a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp new file mode 100644 index 00000000000..7efbac90fd8 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp @@ -0,0 +1,31 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP + +#include +#include +#include +#include + + +namespace stan { + namespace test { + template + void expect_match_return_t() { + using stan::math::foo_fun; + using stan::math::apply_scalar_unary; + using boost::is_convertible; + + typedef typename apply_scalar_unary::return_t result_t; + + bool T1_to_T2 + = is_convertible::value; + EXPECT_TRUE(T1_to_T2); + + bool T1_from_T2 + = is_convertible::value; + EXPECT_TRUE(T1_from_T2); + } + + } +} +#endif diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp new file mode 100644 index 00000000000..9ab69fbd78a --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -0,0 +1,28 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP + +#include + +namespace stan { + + namespace math { + + // mock class static function definition for exp() + struct foo_fun { + template + static inline T fun(const T& x) { + using std::exp; + return exp(x); + } + }; + + template + inline typename apply_scalar_unary::return_t + foo(const T& x) { + return apply_scalar_unary::apply(x); + } + + } +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp new file mode 100644 index 00000000000..04f46617dd3 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -0,0 +1,114 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +TEST(RevMatVectorize,applyScalarUnary) { + using stan::math::var; + using stan::math::foo; + using stan::test::expect_match_return_t; + var three_var = 3; + var exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t(); + expect_match_return_t, std::vector >(); +} + +TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { + using stan::math::foo; + using std::vector; + using stan::math::var; + using stan::math::foo; + + vector y; + y.push_back(1.3); + y.push_back(-2.9); + + vector fy = foo(y); + EXPECT_EQ(2, fy.size()); + EXPECT_FLOAT_EQ(std::exp(y[0].val()), fy[0].val()); + EXPECT_FLOAT_EQ(std::exp(y[1].val()), fy[1].val()); + + vector z1; + z1.push_back(1.1); + z1.push_back(1.2); + vector z2; + z2.push_back(2.1); + z2.push_back(2.2); + vector > z; + z.push_back(z1); + z.push_back(z2); + vector > fz = foo(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(z[i][j].val()), fz[i][j].val()); + } +} +TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { + using stan::math::var; + using stan::math::foo; + typedef Eigen::Matrix MatrixXvar; + + MatrixXvar a(3,2); + a << 1, 2, 3, 4, 5, 6; + MatrixXvar fa = foo(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(a(i).val()), fa(i).val()); + + MatrixXvar fab = foo(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(std::exp(a(1,1).val()), fab(0,0).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryVector) { + using stan::math::var; + using stan::math::foo; + typedef Eigen::Matrix VectorXvar; + + VectorXvar b(6); + b << 1, 2, 3, 4, 5, 6; + VectorXvar fb = foo(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(b(i).val()), fb(i).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { + using stan::math::var; + using stan::math::foo; + typedef Eigen::Matrix RowVectorXvar; + + RowVectorXvar c(6); + c << 1, 2, 3, 4, 5, 6; + RowVectorXvar fc = foo(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(std::exp(c(i).val()), fc(i).val()); +} +TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { + using stan::math::var; + using stan::math::foo; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + RowVectorXvar d1(3); + d1 << 1, 2, 3; + RowVectorXvar d2(3); + d2 << 10, 20, 30; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = foo(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(std::exp(d[i](j).val()), fd[i](j).val()); + } +} From 64166c64c49b6034459da43ef1c7d8f580cf6197 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Tue, 1 Dec 2015 18:48:18 -0500 Subject: [PATCH 02/36] inv() scalar type for vectorization --- stan/math/prim/scal/fun/inv.hpp | 13 ++++++++----- test/unit/math/prim/mat/fun/inv_test.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 test/unit/math/prim/mat/fun/inv_test.cpp diff --git a/stan/math/prim/scal/fun/inv.hpp b/stan/math/prim/scal/fun/inv.hpp index eb3f0f17acc..e6642619f1a 100644 --- a/stan/math/prim/scal/fun/inv.hpp +++ b/stan/math/prim/scal/fun/inv.hpp @@ -6,13 +6,16 @@ namespace stan { namespace math { - template - inline - typename boost::math::tools::promote_args::type - inv(const T x) { + /** + * Return the inverse of the specified value. The inverse of x is + * defined as 1/x. + * + * @param x Value to invert. + * @return Inverse of value. + */ + inline double inv(double x) { return 1.0 / x; } - } } diff --git a/test/unit/math/prim/mat/fun/inv_test.cpp b/test/unit/math/prim/mat/fun/inv_test.cpp new file mode 100644 index 00000000000..dc682e852d0 --- /dev/null +++ b/test/unit/math/prim/mat/fun/inv_test.cpp @@ -0,0 +1,15 @@ +#include +#include +#include + +TEST(primMatFun, inv) { + using stan::math::inv; + std::vector x; + x.push_back(2); + x.push_back(4); + std::vector y = inv(x); + EXPECT_EQ(x.size(), y.size()); + EXPECT_FLOAT_EQ(0.5, y[0]); + EXPECT_FLOAT_EQ(0.25, y[1]); + +} From 41a9254ca116ca2c96831e7d72d180db0e25d709 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Mon, 14 Dec 2015 16:22:34 -0500 Subject: [PATCH 03/36] vectorize test framework --- .../mat/vectorize/apply_scalar_unary_test.cpp | 133 +++------------ .../math/prim/mat/vectorize/expect_values.hpp | 155 ++++++++++++++++++ 2 files changed, 178 insertions(+), 110 deletions(-) create mode 100644 test/unit/math/prim/mat/vectorize/expect_values.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index b82f9b06307..0e31f532372 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,128 +1,41 @@ #include #include #include +#include #include -#include +#include #include #include -TEST(MathPrimMatVectorize, applyScalarUnaryReturnT) { - using stan::test::expect_match_return_t; - expect_match_return_t(); - typedef stan::math::apply_scalar_unary >::return_t - vec_double_return_t; - vec_double_return_t f; - f.push_back(3.7); - EXPECT_FLOAT_EQ(3.7, f[0]); - expect_match_return_t, std::vector >(); -} - -TEST(MathPrimMatVectorize, applyScalarUnaryScalar) { - using stan::math::foo; - - double exp_3 = foo(3); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3); - - double exp_4_d = foo(4.0); - EXPECT_FLOAT_EQ(std::exp(4.0), exp_4_d); - - EXPECT_FLOAT_EQ(std::exp(2.3), foo(2.3)); -} -TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { - using stan::math::foo; - using std::vector; - - vector y; - y.push_back(1.3); - y.push_back(-2.9); +struct foo_base_test { + typedef stan::math::foo_fun fun_t; - vector fy = foo(y); - EXPECT_EQ(2, fy.size()); - EXPECT_FLOAT_EQ(std::exp(y[0]), fy[0]); - EXPECT_FLOAT_EQ(std::exp(y[1]), fy[1]); + static double constrain(double x) { + return x; // exp(x) for (0,inf), inv_logit(x) for (0,1), etc. + } + + static bool has_illegal() { + return false; + } - vector z1; - z1.push_back(1.1); - z1.push_back(1.2); - vector z2; - z2.push_back(2.1); - z2.push_back(2.2); - vector > z; - z.push_back(z1); - z.push_back(z2); - vector > fz = foo(z); - EXPECT_EQ(z.size(), fz.size()); - for (size_t i = 0; i < fz.size(); ++i) { - EXPECT_EQ(z[i].size(), fz[i].size()); - for (size_t j = 0; j < z[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(z[i][j]), fz[i][j]); + static double transform_illegal(double x) { + return 0; } - vector u1; - u1.push_back(2); - u1.push_back(3); - vector u2 = foo(u1); - EXPECT_EQ(2, u2.size()); - for (int i = 0; i < u2.size(); ++i) { - EXPECT_FLOAT_EQ(std::exp(u1[i]), u2[i]); + template + static R apply(const T& x) { + using stan::math::foo; + return foo(x); } -} -TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { - using stan::math::foo; - using Eigen::MatrixXd; - MatrixXd a(3,2); - a << 1, 2, 3, 4, 5, 6; - MatrixXd fa = foo(a); - EXPECT_EQ(a.size(), fa.size()); - for (int i = 0; i < fa.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(a(i)), fa(i)); + static double apply_base(double x) { + return apply(x); + } +}; - MatrixXd fab = foo(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(std::exp(a(1,1)), fab(0,0)); -} -TEST(MathPrimMatVectorize, applyScalarUnaryVector) { - using stan::math::foo; - using Eigen::VectorXd; - VectorXd b(6); - b << 1, 2, 3, 4, 5, 6; - VectorXd fb = foo(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(b(i)), fb(i)); -} -TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { - using stan::math::foo; - using Eigen::RowVectorXd; - RowVectorXd c(6); - c << 1, 2, 3, 4, 5, 6; - RowVectorXd fc = foo(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(c(i)), fc(i)); -} -TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { - using stan::math::foo; - using std::vector; - using Eigen::MatrixXd; - using Eigen::VectorXd; - using Eigen::RowVectorXd; - RowVectorXd d1(3); - d1 << 1, 2, 3; - RowVectorXd d2(3); - d2 << 10, 20, 30; - vector d; - d.push_back(d1); - d.push_back(d2); - vector fd = foo(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(d[i](j)), fd[i](j)); - } +TEST(MathPrimMatVectorize, applyScalarUnaryMock) { + expect_values(); } diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..52f49a0db85 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_values.hpp @@ -0,0 +1,155 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include + +// TODO(carpenter): figure out some way to deal with constraints (transforms) +// including testing out of support values +// ?? wrap this all up in a general transform framework + +template +void expect_scalar_unary_return_type() { + using stan::test::expect_match_return_t; + expect_match_return_t(); + + typedef typename stan::math::apply_scalar_unary >::return_t + vec_double_return_t; + vec_double_return_t f; + f.push_back(3.7); + EXPECT_FLOAT_EQ(3.7, f[0]); + + expect_match_return_t, std::vector >(); +} + +template +void expect_scalar_value() { + double v = F::template apply(3); + EXPECT_FLOAT_EQ(F::apply_base(3.0), v); + + double v2 = F::template apply(4.0); + EXPECT_FLOAT_EQ(F::apply_base(4.0), v2); + + EXPECT_FLOAT_EQ(F::apply_base(2.3), F::template apply(2.3)); +} + +template +void expect_std_vectors_value() { + using stan::math::foo; + using std::vector; + + vector y; + y.push_back(1.3); + y.push_back(-2.9); + + vector fy = F::template apply >(y); + EXPECT_EQ(2, fy.size()); + EXPECT_FLOAT_EQ(F::apply_base(y[0]), fy[0]); + EXPECT_FLOAT_EQ(F::apply_base(y[1]), fy[1]); + + vector z1; + z1.push_back(1.1); + z1.push_back(1.2); + vector z2; + z2.push_back(2.1); + z2.push_back(2.2); + vector > z; + z.push_back(z1); + z.push_back(z2); + vector > fz = F::template apply > >(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); + } + + vector u1; + u1.push_back(2); + u1.push_back(3); + vector u2 = F::template apply >(u1); + EXPECT_EQ(2, u2.size()); + for (int i = 0; i < u2.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(u1[i]), u2[i]); + } +} + +template +void expect_matrix_value() { + using Eigen::MatrixXd; + + MatrixXd a(3,2); + a << 1, 2, 3, 4, 5, 6; + MatrixXd fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); + + MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(std::exp(a(1,1)), fab(0,0)); +} + +template +void expect_vector_value() { + using Eigen::VectorXd; + + VectorXd b(6); + b << 1, 2, 3, 4, 5, 6; + VectorXd fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); +} + +template +void expect_row_vector_value() { + using Eigen::RowVectorXd; + + RowVectorXd c(6); + c << 1, 2, 3, 4, 5, 6; + RowVectorXd fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); +} + +template +void expect_std_vector_row_vector_value() { + using std::vector; + using Eigen::RowVectorXd; + + RowVectorXd d1(3); + d1 << 1, 2, 3; + RowVectorXd d2(3); + d2 << 10, 20, 30; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_values() { + expect_scalar_unary_return_type(); + expect_scalar_value(); + expect_std_vectors_value(); + expect_matrix_value(); + expect_vector_value(); + expect_row_vector_value(); + expect_std_vector_row_vector_value(); +} + +#endif From 74d405b49d0f5cb6fa39e00def9a89ec573e977a Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Tue, 29 Dec 2015 14:31:41 -0500 Subject: [PATCH 04/36] updated tests for vectorization --- .../mat/vectorize/apply_scalar_unary_test.cpp | 30 ++++-- .../math/prim/mat/vectorize/expect_values.hpp | 102 +++++++++++++++--- 2 files changed, 106 insertions(+), 26 deletions(-) diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 0e31f532372..0d46c4fa657 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,25 +1,17 @@ #include #include -#include #include #include -#include #include #include struct foo_base_test { - typedef stan::math::foo_fun fun_t; - static double constrain(double x) { return x; // exp(x) for (0,inf), inv_logit(x) for (0,1), etc. } - - static bool has_illegal() { - return false; - } - static double transform_illegal(double x) { - return 0; + static std::vector illegal_inputs() { + return std::vector(); } template @@ -34,8 +26,24 @@ struct foo_base_test { }; +// this tests that the expect_values test works on a mock function TEST(MathPrimMatVectorize, applyScalarUnaryMock) { expect_values(); } - +// this tests that the return types work +template +void expect_scalar_unary_return_type() { + using stan::test::expect_match_return_t; + expect_match_return_t(); + + typedef typename + stan::math::apply_scalar_unary >::return_t + vec_double_return_t; + vec_double_return_t f; + f.push_back(3.7); + EXPECT_FLOAT_EQ(3.7, f[0]); + + expect_match_return_t, std::vector >(); +} diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp index 52f49a0db85..8d0406a38c6 100644 --- a/test/unit/math/prim/mat/vectorize/expect_values.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_values.hpp @@ -11,20 +11,7 @@ // including testing out of support values // ?? wrap this all up in a general transform framework -template -void expect_scalar_unary_return_type() { - using stan::test::expect_match_return_t; - expect_match_return_t(); - - typedef typename stan::math::apply_scalar_unary >::return_t - vec_double_return_t; - vec_double_return_t f; - f.push_back(3.7); - EXPECT_FLOAT_EQ(3.7, f[0]); - - expect_match_return_t, std::vector >(); -} + template void expect_scalar_value() { @@ -138,18 +125,103 @@ void expect_std_vector_row_vector_value() { } } +template +void expect_std_vector_vector_value() { + using std::vector; + using Eigen::VectorXd; + + VectorXd d1(3); + d1 << 1, 2, 3; + VectorXd d2(3); + d2 << 10, 20, 30; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +template +void expect_std_vector_matrix_value() { + using std::vector; + using Eigen::MatrixXd; + + MatrixXd d1(3, 2); + d1 << 1, 2, 3, 4, 5, 6; + MatrixXd d2(3, 2); + d2 << 10, 20, 30, 40, 50, 60; + vector d; + d.push_back(d1); + d.push_back(d2); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].rows(), fd[i].rows()); + EXPECT_EQ(d[i].cols(), fd[i].cols()); + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + + +template +void expect_std_vector_std_vector_matrix_value() { + using std::vector; + using Eigen::MatrixXd; + + MatrixXd d1(3, 2); + d1 << 1, 2, 3, 4, 5, 6; + MatrixXd d2(3, 2); + d2 << 10, 20, 30, 40, 50, 60; + vector d; + d.push_back(d1); + d.push_back(d2); + + MatrixXd e1(3, 2); + e1 << -10, -100, -1000, -10000, -100000, -1000000; + MatrixXd e2(3, 2); + e2 << 2, 4, 8, 16, 32, 64; + vector e; + e.push_back(e1); + e.push_back(e2); + + vector > x; + x.push_back(d); + x.push_back(e); + + vector > fx = F::template apply > >(x); + EXPECT_EQ(x.size(), fx.size()); + for (size_t i = 0; i < x.size(); ++i) { + EXPECT_EQ(x[i].size(), fx[i].size()); + for (size_t j = 0; j < x[i].size(); ++j) { + EXPECT_EQ(x[i][j].rows(), fx[i][j].rows()); + EXPECT_EQ(x[i][j].cols(), fx[i][j].cols()); + for (int k = 0; k < x[i][j].size(); ++k) + EXPECT_FLOAT_EQ(F::apply_base(x[i][j](k)), fx[i][j](k)); + } + } +} + // CALL THIS TO TEST EVERYTHING // see: apply_scalar_unary_test.cpp for an example template void expect_values() { - expect_scalar_unary_return_type(); expect_scalar_value(); expect_std_vectors_value(); expect_matrix_value(); expect_vector_value(); expect_row_vector_value(); expect_std_vector_row_vector_value(); + expect_std_vector_vector_value(); + expect_std_vector_matrix_value(); + expect_std_vector_std_vector_matrix_value(); } #endif From 1f6d0fc8a99a85b16bf40d4d220a1cc0abbcd9c4 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 26 Jan 2016 14:57:48 -0500 Subject: [PATCH 05/36] Updated primitive tests so thatvalues are specified in the functor and invalid values are checked --- .../vectorize/apply_scalar_unary_test.d.1471 | 7 + .../vectorize/apply_scalar_unary_test.d.3332 | 7 + .../vectorize/apply_scalar_unary_test.d.5193 | 7 + .../vectorize/apply_scalar_unary_test.d.95594 | 0 .../vectorize/apply_scalar_unary_test.d.99349 | 7 + .../math/prim/mat/vectorize/expect_errors.hpp | 132 +++++++++++ .../mat/vectorize/expect_match_return_t.hpp | 1 - .../math/prim/mat/vectorize/expect_values.hpp | 214 +++++++----------- test/unit/math/prim/mat/vectorize/foo_fun.hpp | 3 + .../vectorize/vectorize_test_framework.hpp | 39 ++++ 10 files changed, 280 insertions(+), 137 deletions(-) create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 create mode 100644 test/unit/math/prim/mat/vectorize/expect_errors.hpp create mode 100644 test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 new file mode 100644 index 00000000000..6f6cba2184e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 @@ -0,0 +1,7 @@ +apply_scalar_unary_test.o: \ + test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ + stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ + test/unit/math/prim/mat/vectorize/foo_fun.hpp \ + test/unit/math/prim/mat/vectorize/expect_values.hpp \ + test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ + test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 new file mode 100644 index 00000000000..6f6cba2184e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 @@ -0,0 +1,7 @@ +apply_scalar_unary_test.o: \ + test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ + stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ + test/unit/math/prim/mat/vectorize/foo_fun.hpp \ + test/unit/math/prim/mat/vectorize/expect_values.hpp \ + test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ + test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 new file mode 100644 index 00000000000..6f6cba2184e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 @@ -0,0 +1,7 @@ +apply_scalar_unary_test.o: \ + test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ + stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ + test/unit/math/prim/mat/vectorize/foo_fun.hpp \ + test/unit/math/prim/mat/vectorize/expect_values.hpp \ + test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ + test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 new file mode 100644 index 00000000000..6f6cba2184e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 @@ -0,0 +1,7 @@ +apply_scalar_unary_test.o: \ + test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ + stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ + test/unit/math/prim/mat/vectorize/foo_fun.hpp \ + test/unit/math/prim/mat/vectorize/expect_values.hpp \ + test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ + test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/expect_errors.hpp b/test/unit/math/prim/mat/vectorize/expect_errors.hpp new file mode 100644 index 00000000000..de903616180 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_errors.hpp @@ -0,0 +1,132 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include + + +template +void expect_scalar_error() { + using std::vector; + + vector illegal_inputs = F::illegal_inputs(); + vector int_illegal_inputs = F::int_illegal_inputs(); + + for (size_t i = 0; i < int_illegal_inputs.size(); ++i) { + int input = int_illegal_inputs[i]; + EXPECT_THROW(F::template apply(input), std::domain_error); + EXPECT_THROW(F::apply_base(static_cast(input)), + std::domain_error); + } + + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + EXPECT_THROW(F::apply_base(illegal_inputs[i]), + std::domain_error); + } +} + +template +void expect_std_vectors_error() { + using stan::math::foo; + using std::vector; + + vector illegal_inputs = F::illegal_inputs(); + + EXPECT_THROW(F::template apply >(illegal_inputs), + std::domain_error); + + vector > z; + z.push_back(illegal_inputs); + z.push_back(illegal_inputs); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); + + vector int_illegal_inputs = F::int_illegal_inputs(); + EXPECT_THROW(F::template apply >(int_illegal_inputs), + std::domain_error); + vector > z1; + z1.push_back(int_illegal_inputs); + z1.push_back(int_illegal_inputs); + EXPECT_THROW( + F::template apply > >(z1), std::domain_error); +} + +template +void expect_matrix_error() { + using Eigen::MatrixXd; + using Eigen::RowVectorXd; + using std::vector; + + vector illegal_inputs = F::illegal_inputs(); + RowVectorXd eigen_illegal_inputs = + RowVectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); + size_t num_rows = 3; + MatrixXd a(num_rows, illegal_inputs.size()); + + for (size_t i = 0; i < num_rows; i++) { + a.row(i) << eigen_illegal_inputs; + } + + EXPECT_THROW(F::template apply(a), std::domain_error); + + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), std::domain_error); +} + +template +void expect_vector_error() { + using Eigen::VectorXd; + using std::vector; + + std::vector illegal_inputs = F::illegal_inputs(); + + VectorXd b = VectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), std::domain_error); +} + +template +void expect_row_vector_error() { + using Eigen::RowVectorXd; + using std::vector; + + std::vector illegal_inputs = F::illegal_inputs(); + + RowVectorXd c = + RowVectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); + + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + expect_scalar_error(); + expect_std_vectors_error(); + expect_matrix_error(); + expect_vector_error(); + expect_row_vector_error(); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp index 7efbac90fd8..16a38ea9d66 100644 --- a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp @@ -6,7 +6,6 @@ #include #include - namespace stan { namespace test { template diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp index 8d0406a38c6..8f8cc8fbdef 100644 --- a/test/unit/math/prim/mat/vectorize/expect_values.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_values.hpp @@ -15,39 +15,40 @@ template void expect_scalar_value() { - double v = F::template apply(3); - EXPECT_FLOAT_EQ(F::apply_base(3.0), v); + using std::vector; - double v2 = F::template apply(4.0); - EXPECT_FLOAT_EQ(F::apply_base(4.0), v2); + vector valid_inputs = F::valid_inputs(); + vector int_valid_inputs = F::int_valid_inputs(); - EXPECT_FLOAT_EQ(F::apply_base(2.3), F::template apply(2.3)); + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + int input = int_valid_inputs[i]; + double v = F::template apply(input); + EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); + } + + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), + F::template apply(valid_inputs[i])); + } } template void expect_std_vectors_value() { - using stan::math::foo; using std::vector; - vector y; - y.push_back(1.3); - y.push_back(-2.9); - - vector fy = F::template apply >(y); - EXPECT_EQ(2, fy.size()); - EXPECT_FLOAT_EQ(F::apply_base(y[0]), fy[0]); - EXPECT_FLOAT_EQ(F::apply_base(y[1]), fy[1]); - - vector z1; - z1.push_back(1.1); - z1.push_back(1.2); - vector z2; - z2.push_back(2.1); - z2.push_back(2.2); + vector valid_inputs = F::valid_inputs(); + + vector fy = F::template apply >(valid_inputs); + EXPECT_EQ(valid_inputs.size(), fy.size()); + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), fy[i]); + } + vector > z; - z.push_back(z1); - z.push_back(z2); - vector > fz = F::template apply > >(z); + z.push_back(valid_inputs); + z.push_back(valid_inputs); + vector > fz = + F::template apply > >(z); EXPECT_EQ(z.size(), fz.size()); for (size_t i = 0; i < fz.size(); ++i) { EXPECT_EQ(z[i].size(), fz[i].size()); @@ -55,70 +56,46 @@ void expect_std_vectors_value() { EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); } - vector u1; - u1.push_back(2); - u1.push_back(3); - vector u2 = F::template apply >(u1); - EXPECT_EQ(2, u2.size()); - for (int i = 0; i < u2.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(u1[i]), u2[i]); + vector int_valid_inputs = F::int_valid_inputs(); + vector u2 = F::template apply >(int_valid_inputs); + EXPECT_EQ(int_valid_inputs.size(), u2.size()); + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(int_valid_inputs[i]), u2[i]); } } template void expect_matrix_value() { using Eigen::MatrixXd; + using Eigen::RowVectorXd; + using std::vector; + + vector valid_inputs = F::valid_inputs(); + RowVectorXd eigen_valid_inputs = + RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); + size_t num_rows = 3; + MatrixXd a(num_rows, valid_inputs.size()); + + for (size_t i = 0; i < num_rows; i++) { + a.row(i) << eigen_valid_inputs; + } - MatrixXd a(3,2); - a << 1, 2, 3, 4, 5, 6; MatrixXd fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); for (int i = 0; i < fa.size(); ++i) EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(std::exp(a(1,1)), fab(0,0)); -} - -template -void expect_vector_value() { - using Eigen::VectorXd; + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)), fab(0,0)); - VectorXd b(6); - b << 1, 2, 3, 4, 5, 6; - VectorXd fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); -} - -template -void expect_row_vector_value() { - using Eigen::RowVectorXd; - - RowVectorXd c(6); - c << 1, 2, 3, 4, 5, 6; - RowVectorXd fc = F::template apply(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); -} - -template -void expect_std_vector_row_vector_value() { - using std::vector; - using Eigen::RowVectorXd; - - RowVectorXd d1(3); - d1 << 1, 2, 3; - RowVectorXd d2(3); - d2 << 10, 20, 30; - vector d; - d.push_back(d1); - d.push_back(d2); - vector fd = F::template apply >(d); + vector d; + d.push_back(a); + d.push_back(a); + vector fd = F::template apply >(d); EXPECT_EQ(d.size(), fd.size()); for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].rows(), fd[i].rows()); + EXPECT_EQ(d[i].cols(), fd[i].cols()); EXPECT_EQ(d[i].size(), fd[i].size()); for (int j = 0; j < fd[i].size(); ++j) EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); @@ -126,17 +103,21 @@ void expect_std_vector_row_vector_value() { } template -void expect_std_vector_vector_value() { - using std::vector; +void expect_vector_value() { using Eigen::VectorXd; + using std::vector; + + std::vector valid_inputs = F::valid_inputs(); + + VectorXd b = VectorXd::Map(valid_inputs.data(), valid_inputs.size()); + VectorXd fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); - VectorXd d1(3); - d1 << 1, 2, 3; - VectorXd d2(3); - d2 << 10, 20, 30; vector d; - d.push_back(d1); - d.push_back(d2); + d.push_back(b); + d.push_back(b); vector fd = F::template apply >(d); EXPECT_EQ(d.size(), fd.size()); for (size_t i = 0; i < fd.size(); ++i) { @@ -147,68 +128,33 @@ void expect_std_vector_vector_value() { } template -void expect_std_vector_matrix_value() { +void expect_row_vector_value() { + using Eigen::RowVectorXd; using std::vector; - using Eigen::MatrixXd; - MatrixXd d1(3, 2); - d1 << 1, 2, 3, 4, 5, 6; - MatrixXd d2(3, 2); - d2 << 10, 20, 30, 40, 50, 60; - vector d; - d.push_back(d1); - d.push_back(d2); - vector fd = F::template apply >(d); + std::vector valid_inputs = F::valid_inputs(); + + RowVectorXd c = + RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); + + RowVectorXd fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); + + vector d; + d.push_back(c); + d.push_back(c); + + vector fd = F::template apply >(d); EXPECT_EQ(d.size(), fd.size()); for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].rows(), fd[i].rows()); - EXPECT_EQ(d[i].cols(), fd[i].cols()); EXPECT_EQ(d[i].size(), fd[i].size()); for (int j = 0; j < fd[i].size(); ++j) EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); } } - -template -void expect_std_vector_std_vector_matrix_value() { - using std::vector; - using Eigen::MatrixXd; - - MatrixXd d1(3, 2); - d1 << 1, 2, 3, 4, 5, 6; - MatrixXd d2(3, 2); - d2 << 10, 20, 30, 40, 50, 60; - vector d; - d.push_back(d1); - d.push_back(d2); - - MatrixXd e1(3, 2); - e1 << -10, -100, -1000, -10000, -100000, -1000000; - MatrixXd e2(3, 2); - e2 << 2, 4, 8, 16, 32, 64; - vector e; - e.push_back(e1); - e.push_back(e2); - - vector > x; - x.push_back(d); - x.push_back(e); - - vector > fx = F::template apply > >(x); - EXPECT_EQ(x.size(), fx.size()); - for (size_t i = 0; i < x.size(); ++i) { - EXPECT_EQ(x[i].size(), fx[i].size()); - for (size_t j = 0; j < x[i].size(); ++j) { - EXPECT_EQ(x[i][j].rows(), fx[i][j].rows()); - EXPECT_EQ(x[i][j].cols(), fx[i][j].cols()); - for (int k = 0; k < x[i][j].size(); ++k) - EXPECT_FLOAT_EQ(F::apply_base(x[i][j](k)), fx[i][j](k)); - } - } -} - - // CALL THIS TO TEST EVERYTHING // see: apply_scalar_unary_test.cpp for an example template @@ -218,10 +164,6 @@ void expect_values() { expect_matrix_value(); expect_vector_value(); expect_row_vector_value(); - expect_std_vector_row_vector_value(); - expect_std_vector_vector_value(); - expect_std_vector_matrix_value(); - expect_std_vector_std_vector_matrix_value(); } #endif diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp index 9ab69fbd78a..045fd1af95e 100644 --- a/test/unit/math/prim/mat/vectorize/foo_fun.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -2,6 +2,8 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP #include +#include +#include namespace stan { @@ -12,6 +14,7 @@ namespace stan { template static inline T fun(const T& x) { using std::exp; + stan::math::check_less_or_equal("vectorize", "x", x, 5); return exp(x); } }; diff --git a/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp b/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp new file mode 100644 index 00000000000..6d5fe8ea940 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp @@ -0,0 +1,39 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTORIZE_TEST_FRAMEWORK_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTORIZE_TEST_FRAMEWORK_HPP + +#include +#include +#include +#include + + +namespace stan { + namespace test { + + template + struct test_vectorize { + + static inline void test_function { + + using stan::math::foo; + + double exp_val = foo(val); + EXPECT_FLOAT_EQ(std::exp(val), exp_val); + + EXPECT_FLOAT_EQ(std::exp(val), foo(val)); + } + }; + + template + struct test_vectorize { + + static inline void test_function { + + using stan::math::foo; + + + } + }; + } +} +#endif From 514db2b46177c7a7f0651cec9d835e5c147a2dea Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 26 Jan 2016 15:06:50 -0500 Subject: [PATCH 06/36] Added reverse mode autodiff tests --- .../mat/vectorize/apply_scalar_unary_test.cpp | 139 ++++------ .../vectorize/apply_scalar_unary_test.d.31969 | 0 .../vectorize/apply_scalar_unary_test.d.33834 | 0 .../math/rev/mat/vectorize/expect_errors.hpp | 113 ++++++++ .../mat/vectorize/expect_match_return_t.hpp | 30 +++ .../math/rev/mat/vectorize/expect_values.hpp | 241 ++++++++++++++++++ 6 files changed, 428 insertions(+), 95 deletions(-) create mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 create mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 create mode 100644 test/unit/math/rev/mat/vectorize/expect_errors.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_values.hpp diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 04f46617dd3..0aa9d6cc65f 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,114 +1,63 @@ -#include -#include #include +#include +#include #include -#include #include -#include #include #include +#include -TEST(RevMatVectorize,applyScalarUnary) { - using stan::math::var; - using stan::math::foo; - using stan::test::expect_match_return_t; - var three_var = 3; - var exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); +struct foo_base_test { - expect_match_return_t(); - expect_match_return_t, std::vector >(); -} + template + static R apply(const T& x) { + using stan::math::foo; + return foo(x); + } -TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { - using stan::math::foo; - using std::vector; - using stan::math::var; - using stan::math::foo; + static stan::math::var apply_base(stan::math::var x) { + return apply(x); + } - vector y; - y.push_back(1.3); - y.push_back(-2.9); + static std::vector valid_inputs() { + using std::vector; - vector fy = foo(y); - EXPECT_EQ(2, fy.size()); - EXPECT_FLOAT_EQ(std::exp(y[0].val()), fy[0].val()); - EXPECT_FLOAT_EQ(std::exp(y[1].val()), fy[1].val()); + vector valid_inputs; + valid_inputs.push_back(1.3); + valid_inputs.push_back(-2.6); - vector z1; - z1.push_back(1.1); - z1.push_back(1.2); - vector z2; - z2.push_back(2.1); - z2.push_back(2.2); - vector > z; - z.push_back(z1); - z.push_back(z2); - vector > fz = foo(z); - EXPECT_EQ(z.size(), fz.size()); - for (size_t i = 0; i < fz.size(); ++i) { - EXPECT_EQ(z[i].size(), fz[i].size()); - for (size_t j = 0; j < z[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(z[i][j].val()), fz[i][j].val()); + return valid_inputs; } -} -TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { - using stan::math::var; - using stan::math::foo; - typedef Eigen::Matrix MatrixXvar; - MatrixXvar a(3,2); - a << 1, 2, 3, 4, 5, 6; - MatrixXvar fa = foo(a); - EXPECT_EQ(a.size(), fa.size()); - for (int i = 0; i < fa.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(a(i).val()), fa(i).val()); + static std::vector illegal_inputs() { + using std::vector; - MatrixXvar fab = foo(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(std::exp(a(1,1).val()), fab(0,0).val()); -} -TEST(MathPrimMatVectorize, applyScalarUnaryVector) { - using stan::math::var; - using stan::math::foo; - typedef Eigen::Matrix VectorXvar; + vector illegal_inputs; + illegal_inputs.push_back(10.6); + illegal_inputs.push_back(25.7); - VectorXvar b(6); - b << 1, 2, 3, 4, 5, 6; - VectorXvar fb = foo(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(b(i).val()), fb(i).val()); -} -TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { - using stan::math::var; - using stan::math::foo; - typedef Eigen::Matrix RowVectorXvar; + return illegal_inputs; + } +}; - RowVectorXvar c(6); - c << 1, 2, 3, 4, 5, 6; - RowVectorXvar fc = foo(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(c(i).val()), fc(i).val()); + +// this tests that the expect_values test works on a mock function +TEST(MathRevMatVectorize, applyScalarUnaryMock) { + expect_values(); + if (foo_base_test::illegal_inputs().size() > 0) + expect_errors(); } -TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { - using stan::math::var; - using stan::math::foo; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - RowVectorXvar d1(3); - d1 << 1, 2, 3; - RowVectorXvar d2(3); - d2 << 10, 20, 30; - vector d; - d.push_back(d1); - d.push_back(d2); - vector fd = foo(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(d[i](j).val()), fd[i](j).val()); - } +// this tests that the return types work +template +void expect_scalar_unary_return_type() { + using stan::math::var; + using stan::math::foo; + using stan::test::expect_match_return_t; + var three_var = 3; + var exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t(); + expect_match_return_t, std::vector >(); } diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/unit/math/rev/mat/vectorize/expect_errors.hpp b/test/unit/math/rev/mat/vectorize/expect_errors.hpp new file mode 100644 index 00000000000..c82091f87b9 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_errors.hpp @@ -0,0 +1,113 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +void expect_std_vectors_error() { + using std::vector; + using stan::math::var; + + vector illegal_inputs = F::illegal_inputs(); + vector y; + + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + EXPECT_THROW(F::template apply(illegal_inputs[i]), + std::domain_error); + y.push_back(illegal_inputs[i]); + } + + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); +} + +template +void expect_matrix_error() { + using std::vector; + using stan::math::var; + typedef Eigen::Matrix MatrixXvar; + + vector illegal_inputs = F::illegal_inputs(); + size_t num_rows = 3; + MatrixXvar a(num_rows, illegal_inputs.size()); + + for (size_t i = 0; i < num_rows; ++i) { + for (size_t j = 0; j < illegal_inputs.size(); ++j) + a(i, j) = illegal_inputs[j]; + } + + EXPECT_THROW(F::template apply(a), std::domain_error); + + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_vector_error() { + using std::vector; + using stan::math::var; + typedef Eigen::Matrix VectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + VectorXvar b = VectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + b(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_row_vector_error() { + using std::vector; + using stan::math::var; + typedef Eigen::Matrix RowVectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + c(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + expect_std_vectors_error(); + expect_matrix_error(); + expect_vector_error(); + expect_row_vector_error(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp new file mode 100644 index 00000000000..268072275f0 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp @@ -0,0 +1,30 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP + +#include +#include +#include +#include + +namespace stan { + namespace test { + template + void expect_match_return_t() { + using stan::math::foo_fun; + using stan::math::apply_scalar_unary; + using boost::is_convertible; + + typedef typename apply_scalar_unary::return_t result_t; + + bool T1_to_T2 + = is_convertible::value; + EXPECT_TRUE(T1_to_T2); + + bool T1_from_T2 + = is_convertible::value; + EXPECT_TRUE(T1_from_T2); + } + + } +} +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..0d80f70646b --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_values.hpp @@ -0,0 +1,241 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +static inline Eigen::Matrix +build_valid_var_matrix(const T& x) { + Eigen::Matrix var_matrix(x.rows(), x.cols()); + std::vector inputs = F::valid_inputs(); + for (int i = 0; i < x.size(); ++i) { + var_matrix(i) = inputs[(i % inputs.size())]; + } + return var_matrix; +} + +template +static inline std::vector +build_valid_var_vector() { + + std::vector inputs = F::valid_inputs(); + std::vector var_vector; + + for (int i = 0; i < inputs.size(); ++i) { + var_vector.push_back(inputs[i]); + } + + return var_vector; +} + +template +static inline void test_autodiff(double test_val, double test_adj) { + using stan::math::var; + + var x = test_val; + F::apply_base(x).grad(); + EXPECT_FLOAT_EQ(x.adj(), test_adj); +} + +template +void expect_scalar_value() { + using stan::math::var; + + std::vector y = build_valid_var_vector(); + for (size_t i = 0; i < y.size(); ++i) { + var fy = F::template apply(y[i]); + + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); + + fy.grad(); + test_autodiff(y[i].val(), y[i].adj()); + } +} + +template +void expect_std_vectors_value() { + using std::vector; + using stan::math::var; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + + vector y = build_valid_var_vector(); + vector fy = F::template apply >(y); + + EXPECT_EQ(y.size(), fy.size()); + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); + + fy[i].grad(); + test_autodiff(y[i].val(), y[i].adj()); + } +} + +template +void expect_std_vector_vectors_value() { + using std::vector; + using stan::math::var; + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + + for (size_t j = 0; j < F::valid_inputs().size(); ++j) { + + vector > z; + for (size_t i = 0; i < vector_vector_size; ++i) { + z.push_back(build_valid_var_vector()); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); + + fz[i][j].grad(); + test_autodiff(z[i][j].val(), z[i][j].adj()); + } + } +} + +template +void expect_matrix_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix MatrixXvar; + + size_t num_cols = 3; + size_t num_inputs = F::valid_inputs().size(); + MatrixXvar template_matrix(num_inputs, num_cols); + + for (size_t i = 0; i < template_matrix.size(); ++i) { + MatrixXvar a = build_valid_var_matrix(template_matrix); + MatrixXvar fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); + + fa(i).grad(); + test_autodiff(a(i).val(), a(i).adj()); + } + + size_t vector_matrix_size = 2; + for (size_t i = 0; i < vector_matrix_size; ++i) { + for (size_t j = 0; j < template_matrix.size(); ++j) { + + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) + b.push_back(build_valid_var_matrix(template_matrix)); + vector fb = F::template apply >(b); + + EXPECT_EQ(b[i].size(), fb[i].size()); + EXPECT_EQ(b[i].rows(), fb[i].rows()); + EXPECT_EQ(b[i].cols(), fb[i].cols()); + EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); + + fb[i](j).grad(); + test_autodiff(b[i](j).val(), b[i](j).adj()); + } + } + + MatrixXvar a = build_valid_var_matrix(template_matrix); + MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); + fab(0,0).grad(); + test_autodiff(a(1,1).val(), a(1,1).adj()); +} + +template +void expect_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar b = build_valid_var_matrix(template_vector); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + test_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_valid_var_matrix(template_vector)); + vector fvb = F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + test_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +template +void expect_row_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar b = build_valid_var_matrix(template_vector); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + test_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_valid_var_matrix(template_vector)); + vector fvb = + F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + test_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +template +void expect_values() { + expect_scalar_value(); + expect_std_vectors_value(); + expect_std_vector_vectors_value(); + expect_matrix_value(); + expect_vector_value(); + expect_row_vector_value(); +} + +#endif From 91a0a5d943ca176641d671cbe378f05bc236b8ee Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 2 Feb 2016 15:41:56 -0500 Subject: [PATCH 07/36] Updated fwd mode tests to test valid and invalid inputs specified in the functor --- .../mat/vectorize/apply_scalar_unary_test.cpp | 141 ++++------ .../math/fwd/mat/vectorize/expect_errors.hpp | 114 ++++++++ .../mat/vectorize/expect_match_return_t.hpp | 31 +++ .../math/fwd/mat/vectorize/expect_values.hpp | 260 ++++++++++++++++++ 4 files changed, 451 insertions(+), 95 deletions(-) create mode 100644 test/unit/math/fwd/mat/vectorize/expect_errors.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_values.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index 562f79d6aaf..25c4b8fb916 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,114 +1,65 @@ -#include -#include #include +#include +#include #include -#include #include -#include #include #include +#include -TEST(RevMatVectorize,applyScalarUnary) { - using stan::math::fvar; - using stan::math::foo; - using stan::test::expect_match_return_t; - fvar three_var = 3; - fvar exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); +struct foo_base_test { - expect_match_return_t, fvar >(); - expect_match_return_t >, std::vector > >(); -} + template + static R apply(const T& x) { + using stan::math::foo; + return foo(x); + } -TEST(MathPrimMatVectorize, applyScalarUnaryStdVector) { - using stan::math::foo; - using std::vector; - using stan::math::fvar; - using stan::math::foo; + template + static stan::math::fvar apply_base(stan::math::fvar x) { + return apply >(x); + } - vector > y; - y.push_back(1.3); - y.push_back(-2.9); + static std::vector valid_inputs() { + using std::vector; - vector > fy = foo(y); - EXPECT_EQ(2, fy.size()); - EXPECT_FLOAT_EQ(std::exp(y[0].val()), fy[0].val()); - EXPECT_FLOAT_EQ(std::exp(y[1].val()), fy[1].val()); + vector valid_inputs; + valid_inputs.push_back(1.3); + valid_inputs.push_back(-2.6); - vector > z1; - z1.push_back(1.1); - z1.push_back(1.2); - vector > z2; - z2.push_back(2.1); - z2.push_back(2.2); - vector > > z; - z.push_back(z1); - z.push_back(z2); - vector > > fz = foo(z); - EXPECT_EQ(z.size(), fz.size()); - for (size_t i = 0; i < fz.size(); ++i) { - EXPECT_EQ(z[i].size(), fz[i].size()); - for (size_t j = 0; j < z[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(z[i][j].val()), fz[i][j].val()); + return valid_inputs; } -} -TEST(MathPrimMatVectorize, applyScalarUnaryMatrix) { - using stan::math::fvar; - using stan::math::foo; - typedef Eigen::Matrix , Eigen::Dynamic, Eigen::Dynamic> MatrixXvar; - MatrixXvar a(3,2); - a << 1, 2, 3, 4, 5, 6; - MatrixXvar fa = foo(a); - EXPECT_EQ(a.size(), fa.size()); - for (int i = 0; i < fa.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(a(i).val()), fa(i).val()); + static std::vector illegal_inputs() { + using std::vector; - MatrixXvar fab = foo(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(std::exp(a(1,1).val()), fab(0,0).val()); -} -TEST(MathPrimMatVectorize, applyScalarUnaryVector) { - using stan::math::fvar; - using stan::math::foo; - typedef Eigen::Matrix , Eigen::Dynamic, 1> VectorXvar; + vector illegal_inputs; + illegal_inputs.push_back(10.6); + illegal_inputs.push_back(25.7); - VectorXvar b(6); - b << 1, 2, 3, 4, 5, 6; - VectorXvar fb = foo(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(b(i).val()), fb(i).val()); -} -TEST(MathPrimMatVectorize, applyScalarUnaryRowVector) { - using stan::math::fvar; - using stan::math::foo; - typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; + return illegal_inputs; + } +}; - RowVectorXvar c(6); - c << 1, 2, 3, 4, 5, 6; - RowVectorXvar fc = foo(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(std::exp(c(i).val()), fc(i).val()); + +// this tests that the expect_values test works on a mock function +TEST(MathFwdMatVectorize, applyScalarUnaryMock) { + expect_values(); + if (foo_base_test::illegal_inputs().size() > 0) + expect_errors(); } -TEST(MathPrimMatVectorize, applyScalarUnaryMixedMatrixStdVector) { - using stan::math::fvar; - using stan::math::foo; - using std::vector; - typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; - RowVectorXvar d1(3); - d1 << 1, 2, 3; - RowVectorXvar d2(3); - d2 << 10, 20, 30; - vector d; - d.push_back(d1); - d.push_back(d2); - vector fd = foo(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(std::exp(d[i](j).val()), fd[i](j).val()); - } +// this tests that the return types work +template +void expect_scalar_unary_return_type() { + using stan::math::fvar; + using stan::math::foo; + using stan::test::expect_match_return_t; + fvar three_var = 3; + fvar exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t, fvar >(); + expect_match_return_t >, + std::vector > >(); } diff --git a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp new file mode 100644 index 00000000000..b76c3da6f0c --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp @@ -0,0 +1,114 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +void expect_std_vectors_error() { + using std::vector; + using stan::math::fvar; + + vector illegal_inputs = F::illegal_inputs(); + vector > y; + + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + EXPECT_THROW(F::template apply >(illegal_inputs[i]), + std::domain_error); + y.push_back(illegal_inputs[i]); + } + + EXPECT_THROW(F::template apply > >(y), std::domain_error); + + vector > > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > > >(z), + std::domain_error); +} + +template +void expect_matrix_error() { + using std::vector; + using stan::math::fvar; + typedef + Eigen::Matrix , Eigen::Dynamic, Eigen::Dynamic> MatrixXvar; + + vector illegal_inputs = F::illegal_inputs(); + size_t num_rows = 3; + MatrixXvar a(num_rows, illegal_inputs.size()); + + for (size_t i = 0; i < num_rows; ++i) { + for (size_t j = 0; j < illegal_inputs.size(); ++j) + a(i, j) = illegal_inputs[j]; + } + + EXPECT_THROW(F::template apply(a), std::domain_error); + + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_vector_error() { + using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix, Eigen::Dynamic, 1> VectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + VectorXvar b = VectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + b(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_row_vector_error() { + using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + c(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + expect_std_vectors_error(); + expect_matrix_error(); + expect_vector_error(); + expect_row_vector_error(); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp new file mode 100644 index 00000000000..8613848419d --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp @@ -0,0 +1,31 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP + +#include +#include +#include +#include + + +namespace stan { + namespace test { + template + void expect_match_return_t() { + using stan::math::foo_fun; + using stan::math::apply_scalar_unary; + using boost::is_convertible; + + typedef typename apply_scalar_unary::return_t result_t; + + bool T1_to_T2 + = is_convertible::value; + EXPECT_TRUE(T1_to_T2); + + bool T1_from_T2 + = is_convertible::value; + EXPECT_TRUE(T1_from_T2); + } + + } +} +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..1e05e5b3815 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_values.hpp @@ -0,0 +1,260 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +static inline std::vector > +build_valid_fvar_vector(std::vector > fvar_vector, + int seed_index = -1) { + std::vector inputs = F::valid_inputs(); + + for (int i = 0; i < inputs.size(); ++i) { + if (seed_index == i) + fvar_vector.push_back(stan::math::fvar(inputs[i], 1)); + else + fvar_vector.push_back(stan::math::fvar(inputs[i])); + } + return fvar_vector; +} + +template +static inline std::vector > +build_valid_fvar_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector template_vector = + build_valid_fvar_vector(vector(), seed_index); + + for (int i = 0; i < template_vector.size(); ++i) { + if (seed_index == i) + fvar_vector.push_back( + fvar(template_vector[i], template_vector[i])); + else + fvar_vector.push_back(fvar(template_vector[i])); + } + return fvar_vector; +} + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_valid_fvar_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_valid_fvar_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_valid_fvar_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +static inline void test_fvar(double exp_var, double test_var) { + EXPECT_FLOAT_EQ(exp_var, test_var); +} + +template +static inline void test_fvar(V exp_var, V test_var) { + test_fvar(exp_var.val(), test_var.val()); + test_fvar(exp_var.d_, test_var.d_); +} + +template +void expect_scalar_value() { + using stan::math::fvar; + using stan::test::expect_match_return_t; + using std::vector; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + V fy = F::template apply(y[i]); + V exp_y = F::apply_base(y[i]); + test_fvar(exp_y, fy); + } + + expect_match_return_t(); + expect_match_return_t, + std::vector >(); + +} + +template +void expect_std_vector_values() { + using stan::math::foo; + using std::vector; + using stan::math::fvar; + using stan::math::foo; + + size_t num_inputs = F::valid_inputs().size(); + + for (size_t i = 0; i < num_inputs; ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector fy = F::template apply >(y); + EXPECT_EQ(y.size(), fy.size()); + test_fvar(F::apply_base(y[i]), fy[i]); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + + vector > z; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) + z.push_back(build_valid_fvar_vector(vector(), j)); + else + z.push_back(build_valid_fvar_vector(vector())); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + test_fvar(F::apply_base(z[i][j]), fz[i][j]); + } + } +} + +template +void expect_matrix_values() { + using stan::math::fvar; + using std::vector; + typedef + Eigen::Matrix MatrixXvar; + + size_t num_inputs = F::valid_inputs().size(); + size_t num_cols = 3; + MatrixXvar template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + MatrixXvar a = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + test_fvar(F::apply_base(a(i)), fa(i)); + } + + size_t vector_matrix_size = 2; + for (int i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector b; + for (int k = 0; k < vector_matrix_size; ++k) + if (k == i) + b.push_back(build_valid_fvar_matrix(template_matrix, j)); + else + b.push_back(build_valid_fvar_matrix(template_matrix)); + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(b[i](j)), fb[i](j)); + } + } + + MatrixXvar a = build_valid_fvar_matrix(template_matrix, 3); + MatrixXvar fab = foo(a.block(1, 1, 1, 1)); + test_fvar(F::apply_base(a(1,1)), fab(0,0)); +} + +template +void expect_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar b = build_valid_fvar_matrix(template_vector, i); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_valid_fvar_matrix(template_vector, j)); + else + c.push_back(build_valid_fvar_matrix(template_vector)); + vector fc = F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + test_fvar(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +template +void expect_row_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_valid_fvar_matrix(template_vector, j)); + else + c.push_back(build_valid_fvar_matrix(template_vector)); + vector fc = + F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + test_fvar(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +template +void expect_values() { + using stan::math::fvar; + using std::vector; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_values >(); + expect_std_vector_values > >(); + expect_matrix_values >(); + expect_matrix_values > >(); + expect_vector_values >(); + expect_vector_values > >(); + expect_row_vector_values >(); + expect_row_vector_values > >(); +} From 5e00192ae37dc9e8de140238b6f4a5ef7275eb22 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 4 Feb 2016 11:34:17 -0500 Subject: [PATCH 08/36] Added mixed vectorize tests --- .../mat/vectorize/apply_scalar_unary_test.cpp | 68 ++++ .../math/mix/mat/vectorize/expect_errors.hpp | 122 +++++++ .../mat/vectorize/expect_match_return_t.hpp | 31 ++ .../math/mix/mat/vectorize/expect_values.hpp | 306 ++++++++++++++++++ 4 files changed, 527 insertions(+) create mode 100644 test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_errors.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_values.hpp diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp new file mode 100644 index 00000000000..743ab828ad2 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct foo_base_test { + + template + static R apply(const T& x) { + using stan::math::foo; + return foo(x); + } + + template + static stan::math::fvar apply_base(stan::math::fvar x) { + return apply >(x); + } + + static std::vector valid_inputs() { + using std::vector; + + vector valid_inputs; + valid_inputs.push_back(1.3); + valid_inputs.push_back(-2.6); + valid_inputs.push_back(0); + valid_inputs.push_back(-0.2); + + return valid_inputs; + } + + static std::vector illegal_inputs() { + using std::vector; + + vector illegal_inputs(2, 10.6); + illegal_inputs.push_back(25.7); + illegal_inputs.push_back(100.25); + + return illegal_inputs; + } +}; + + +// this tests that the expect_values test works on a mock function +TEST(MathFwdMatVectorize, applyScalarUnaryMock) { + expect_values(); + if (foo_base_test::illegal_inputs().size() > 0) + expect_errors(); +} + +// this tests that the return types work +template +void expect_scalar_unary_return_type() { + using stan::math::fvar; + using stan::math::foo; + using stan::test::expect_match_return_t; + fvar three_var = 3; + fvar exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t, fvar >(); + expect_match_return_t >, + std::vector > >(); +} diff --git a/test/unit/math/mix/mat/vectorize/expect_errors.hpp b/test/unit/math/mix/mat/vectorize/expect_errors.hpp new file mode 100644 index 00000000000..9c900497a30 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_errors.hpp @@ -0,0 +1,122 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +template +void expect_std_vectors_error() { + using std::vector; + using stan::math::fvar; + + vector illegal_inputs = F::illegal_inputs(); + vector y; + + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + EXPECT_THROW(F::template apply(illegal_inputs[i]), + std::domain_error); + y.push_back(illegal_inputs[i]); + } + + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); +} + +template +void expect_matrix_error() { + using std::vector; + using stan::math::fvar; + typedef + Eigen::Matrix MatrixXvar; + + vector illegal_inputs = F::illegal_inputs(); + size_t num_rows = 3; + MatrixXvar a(num_rows, illegal_inputs.size()); + + for (size_t i = 0; i < num_rows; ++i) { + for (size_t j = 0; j < illegal_inputs.size(); ++j) + a(i, j) = illegal_inputs[j]; + } + + EXPECT_THROW(F::template apply(a), std::domain_error); + + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_vector_error() { + using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix VectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + VectorXvar b = VectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + b(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +template +void expect_row_vector_error() { + using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix RowVectorXvar; + + std::vector illegal_inputs = F::illegal_inputs(); + + RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + c(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + using stan::math::fvar; + using stan::math::var; + + expect_std_vectors_error >(); + expect_std_vectors_error > >(); + expect_matrix_error >(); + expect_matrix_error > >(); + expect_vector_error >(); + expect_vector_error > >(); + expect_row_vector_error >(); + expect_row_vector_error > >(); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp new file mode 100644 index 00000000000..8613848419d --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp @@ -0,0 +1,31 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP + +#include +#include +#include +#include + + +namespace stan { + namespace test { + template + void expect_match_return_t() { + using stan::math::foo_fun; + using stan::math::apply_scalar_unary; + using boost::is_convertible; + + typedef typename apply_scalar_unary::return_t result_t; + + bool T1_to_T2 + = is_convertible::value; + EXPECT_TRUE(T1_to_T2); + + bool T1_from_T2 + = is_convertible::value; + EXPECT_TRUE(T1_from_T2); + } + + } +} +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_values.hpp b/test/unit/math/mix/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..3e2fa722c41 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_values.hpp @@ -0,0 +1,306 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +static inline std::vector +build_valid_fvar_vector(std::vector var_vector, + int seed_index = -1) { + std::vector inputs = F::valid_inputs(); + for (int i = 0; i < inputs.size(); ++i) { + var_vector.push_back(inputs[i]); + } + return var_vector; +} + +template +static inline std::vector > +build_valid_fvar_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector val_vector = + build_valid_fvar_vector(vector(), seed_index); + vector d_vector; + if (seed_index != -1) + d_vector = build_valid_fvar_vector(vector(), seed_index); + + for (int i = 0; i < val_vector.size(); ++i) { + if (seed_index == i) + fvar_vector.push_back( + fvar(val_vector[i], d_vector[i])); + else + fvar_vector.push_back(fvar(val_vector[i])); + } + return fvar_vector; +} + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_valid_fvar_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_valid_fvar_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_valid_fvar_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +static inline void test_fvar(stan::math::var exp_var, + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { + EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); + AVEC exp_y = createAVEC(base_exp_var); + VEC exp_g; + exp_var.grad(exp_y, exp_g); + AVEC test_y = createAVEC(base_test_var); + VEC test_g; + test_var.grad(test_y, test_g); + EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); +} + +template +static inline void test_fvar(V exp_var, V base_exp_var, + V test_var, V base_test_var) { + test_fvar(exp_var.val(), base_exp_var.val(), + test_var.val(), base_test_var.val()); + test_fvar(exp_var.d_, base_exp_var.d_, + test_var.d_, base_test_var.d_); +} + +template +void expect_scalar_value() { + using stan::math::fvar; + using stan::test::expect_match_return_t; + using std::vector; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector z = + build_valid_fvar_vector(vector(), i); + V fz = F::template apply(z[i]); + test_fvar(F::apply_base(y[i]), y[i], fz, z[i]); + } + expect_match_return_t(); + expect_match_return_t, + std::vector >(); +} + +template +void expect_std_vector_values() { + using std::vector; + using stan::math::fvar; + + size_t num_inputs = F::valid_inputs().size(); + + for (size_t i = 0; i < num_inputs; ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector z = + build_valid_fvar_vector(vector(), i); + vector fz = F::template apply >(z); + EXPECT_EQ(z.size(), fz.size()); + test_fvar(F::apply_base(y[i]), y[i], fz[i], z[i]); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + + vector > a; + vector > b; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) { + a.push_back(build_valid_fvar_vector(vector(), j)); + b.push_back(build_valid_fvar_vector(vector(), j)); + } + else { + a.push_back(build_valid_fvar_vector(vector())); + b.push_back(build_valid_fvar_vector(vector())); + } + } + vector > fb = + F::template apply > >(b); + + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); + } + } +} + +template +void expect_matrix_values() { + using stan::math::fvar; + using std::vector; + typedef + Eigen::Matrix MatrixXvar; + + size_t num_inputs = F::valid_inputs().size(); + size_t num_cols = 3; + MatrixXvar template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + MatrixXvar y = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar z = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar fz = F::template apply(z); + EXPECT_EQ(z.size(), fz.size()); + test_fvar(F::apply_base(y(i)), y(i), fz(i), z(i)); + } + + size_t vector_matrix_size = 2; + for (int i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector a; + vector b; + for (int k = 0; k < vector_matrix_size; ++k) + if (k == i) { + a.push_back(build_valid_fvar_matrix(template_matrix, j)); + b.push_back(build_valid_fvar_matrix(template_matrix, j)); + } + else { + a.push_back(build_valid_fvar_matrix(template_matrix)); + b.push_back(build_valid_fvar_matrix(template_matrix)); + } + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); + } + } + + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + MatrixXvar c = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar d = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar fab = foo(d.block(block_i, block_j, 1, 1)); + test_fvar(F::apply_base(c(block_i, block_j)), c(block_i, block_j), + fab(0,0), d(block_i, block_j)); +} + +template +void expect_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar a = build_valid_fvar_matrix(template_vector, i); + VectorXvar b = build_valid_fvar_matrix(template_vector, i); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_valid_fvar_matrix(template_vector, j)); + d.push_back(build_valid_fvar_matrix(template_vector, j)); + } + else { + c.push_back(build_valid_fvar_matrix(template_vector)); + d.push_back(build_valid_fvar_matrix(template_vector)); + } + vector fd = F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +template +void expect_row_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar a = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_valid_fvar_matrix(template_vector, j)); + d.push_back(build_valid_fvar_matrix(template_vector, j)); + } + else { + c.push_back(build_valid_fvar_matrix(template_vector)); + d.push_back(build_valid_fvar_matrix(template_vector)); + } + vector fd = + F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +template +void expect_values() { + using stan::math::fvar; + using stan::math::var; + using std::vector; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_values >(); + expect_std_vector_values > >(); + expect_matrix_values >(); + expect_matrix_values > >(); + expect_vector_values >(); + expect_vector_values > >(); + expect_row_vector_values >(); + expect_row_vector_values > >(); +} + +#endif From cc95a4536e47ad1c5e3355cbb03f4cb35156df12 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 4 Feb 2016 11:35:22 -0500 Subject: [PATCH 09/36] Updated fwd tests to test more cases and have a better code framework --- .../mat/vectorize/apply_scalar_unary_test.cpp | 6 ++- .../vectorize/apply_scalar_unary_test.d.15772 | 0 .../math/fwd/mat/vectorize/expect_errors.hpp | 38 +++++++++++-------- .../math/fwd/mat/vectorize/expect_values.hpp | 27 +++++++------ 4 files changed, 39 insertions(+), 32 deletions(-) create mode 100644 test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index 25c4b8fb916..03a57273fdb 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -26,6 +26,8 @@ struct foo_base_test { vector valid_inputs; valid_inputs.push_back(1.3); valid_inputs.push_back(-2.6); + valid_inputs.push_back(0); + valid_inputs.push_back(-0.2); return valid_inputs; } @@ -33,9 +35,9 @@ struct foo_base_test { static std::vector illegal_inputs() { using std::vector; - vector illegal_inputs; - illegal_inputs.push_back(10.6); + vector illegal_inputs(2, 10.6); illegal_inputs.push_back(25.7); + illegal_inputs.push_back(100.7); return illegal_inputs; } diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp index b76c3da6f0c..1d54099ba8e 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp @@ -9,35 +9,35 @@ #include #include -template +template void expect_std_vectors_error() { using std::vector; using stan::math::fvar; vector illegal_inputs = F::illegal_inputs(); - vector > y; + vector y; for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::template apply >(illegal_inputs[i]), + EXPECT_THROW(F::template apply(illegal_inputs[i]), std::domain_error); y.push_back(illegal_inputs[i]); } - EXPECT_THROW(F::template apply > >(y), std::domain_error); + EXPECT_THROW(F::template apply >(y), std::domain_error); - vector > > z; + vector > z; z.push_back(y); z.push_back(y); - EXPECT_THROW(F::template apply > > >(z), + EXPECT_THROW(F::template apply > >(z), std::domain_error); } -template +template void expect_matrix_error() { using std::vector; using stan::math::fvar; typedef - Eigen::Matrix , Eigen::Dynamic, Eigen::Dynamic> MatrixXvar; + Eigen::Matrix MatrixXvar; vector illegal_inputs = F::illegal_inputs(); size_t num_rows = 3; @@ -60,11 +60,11 @@ void expect_matrix_error() { std::domain_error); } -template +template void expect_vector_error() { using std::vector; using stan::math::fvar; - typedef Eigen::Matrix, Eigen::Dynamic, 1> VectorXvar; + typedef Eigen::Matrix VectorXvar; std::vector illegal_inputs = F::illegal_inputs(); @@ -80,11 +80,11 @@ void expect_vector_error() { std::domain_error); } -template +template void expect_row_vector_error() { using std::vector; using stan::math::fvar; - typedef Eigen::Matrix , 1, Eigen::Dynamic> RowVectorXvar; + typedef Eigen::Matrix RowVectorXvar; std::vector illegal_inputs = F::illegal_inputs(); @@ -105,10 +105,16 @@ void expect_row_vector_error() { // see: apply_scalar_unary_test.cpp for an example template void expect_errors() { - expect_std_vectors_error(); - expect_matrix_error(); - expect_vector_error(); - expect_row_vector_error(); + using stan::math::fvar; + + expect_std_vectors_error >(); + expect_std_vectors_error > >(); + expect_matrix_error >(); + expect_matrix_error > >(); + expect_vector_error >(); + expect_vector_error > >(); + expect_row_vector_error >(); + expect_row_vector_error > >(); } #endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp index 1e05e5b3815..c58d2c039bf 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_values.hpp @@ -1,5 +1,7 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP + #include -#include #include #include #include @@ -9,18 +11,10 @@ #include template -static inline std::vector > -build_valid_fvar_vector(std::vector > fvar_vector, +static inline std::vector +build_valid_fvar_vector(std::vector double_vector, int seed_index = -1) { - std::vector inputs = F::valid_inputs(); - - for (int i = 0; i < inputs.size(); ++i) { - if (seed_index == i) - fvar_vector.push_back(stan::math::fvar(inputs[i], 1)); - else - fvar_vector.push_back(stan::math::fvar(inputs[i])); - } - return fvar_vector; + return F::valid_inputs(); } template @@ -168,8 +162,11 @@ void expect_matrix_values() { } } - MatrixXvar a = build_valid_fvar_matrix(template_matrix, 3); - MatrixXvar fab = foo(a.block(1, 1, 1, 1)); + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + MatrixXvar a = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar fab = foo(a.block(block_i, block_j, 1, 1)); test_fvar(F::apply_base(a(1,1)), fab(0,0)); } @@ -258,3 +255,5 @@ void expect_values() { expect_row_vector_values >(); expect_row_vector_values > >(); } + +#endif From 0f2ef546f924c60efe7d155060b5878fd4d6f2b0 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 4 Feb 2016 11:36:17 -0500 Subject: [PATCH 10/36] Updated rev tests to test more cases --- .../unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp | 6 ++++-- test/unit/math/rev/mat/vectorize/expect_values.hpp | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 0aa9d6cc65f..7d89bc0b579 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -25,6 +25,8 @@ struct foo_base_test { vector valid_inputs; valid_inputs.push_back(1.3); valid_inputs.push_back(-2.6); + valid_inputs.push_back(0); + valid_inputs.push_back(-0.7); return valid_inputs; } @@ -32,9 +34,9 @@ struct foo_base_test { static std::vector illegal_inputs() { using std::vector; - vector illegal_inputs; - illegal_inputs.push_back(10.6); + vector illegal_inputs(2, 10.6); illegal_inputs.push_back(25.7); + illegal_inputs.push_back(100.25); return illegal_inputs; } diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp index 0d80f70646b..e67db37936a 100644 --- a/test/unit/math/rev/mat/vectorize/expect_values.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_values.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP #include #include From 5512a4080d98293122be6397b7d7da11850eaac1 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 4 Feb 2016 11:37:08 -0500 Subject: [PATCH 11/36] Updated prim tests to test more cases --- .../mat/vectorize/apply_scalar_unary_test.cpp | 81 ++++++++++++++----- test/unit/math/prim/mat/vectorize/foo_fun.hpp | 3 +- 2 files changed, 63 insertions(+), 21 deletions(-) diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 0d46c4fa657..17a04379646 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,18 +1,12 @@ #include -#include #include +#include +#include #include #include #include struct foo_base_test { - static double constrain(double x) { - return x; // exp(x) for (0,inf), inv_logit(x) for (0,1), etc. - } - - static std::vector illegal_inputs() { - return std::vector(); - } template static R apply(const T& x) { @@ -23,27 +17,74 @@ struct foo_base_test { static double apply_base(double x) { return apply(x); } + + static std::vector valid_inputs() { + using std::vector; + + vector valid_inputs; + valid_inputs.push_back(1.3); + valid_inputs.push_back(-2.6); + valid_inputs.push_back(0); + valid_inputs.push_back(0.2); + + return valid_inputs; + } + + static std::vector int_valid_inputs() { + using std::vector; + + vector valid_inputs; + valid_inputs.push_back(1); + valid_inputs.push_back(-2); + valid_inputs.push_back(0); + valid_inputs.push_back(3); + + return valid_inputs; + } + + static std::vector illegal_inputs() { + using std::vector; + + vector illegal_inputs; + illegal_inputs.push_back(10.6); + illegal_inputs.push_back(25.7); + illegal_inputs.push_back(100.6); + illegal_inputs.push_back(50.7); + + return illegal_inputs; + } + + static std::vector int_illegal_inputs() { + using std::vector; + + vector illegal_inputs; + illegal_inputs.push_back(10); + illegal_inputs.push_back(25); + illegal_inputs.push_back(100); + illegal_inputs.push_back(50); + + return illegal_inputs; + } }; // this tests that the expect_values test works on a mock function TEST(MathPrimMatVectorize, applyScalarUnaryMock) { expect_values(); + if (foo_base_test::illegal_inputs().size() > 0) + expect_errors(); } // this tests that the return types work template void expect_scalar_unary_return_type() { - using stan::test::expect_match_return_t; - expect_match_return_t(); - - typedef typename - stan::math::apply_scalar_unary >::return_t - vec_double_return_t; - vec_double_return_t f; - f.push_back(3.7); - EXPECT_FLOAT_EQ(3.7, f[0]); - - expect_match_return_t, std::vector >(); + using stan::math::var; + using stan::math::foo; + using stan::test::expect_match_return_t; + var three_var = 3; + var exp_3_v = foo(three_var); + EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); + + expect_match_return_t(); + expect_match_return_t, std::vector >(); } diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp index 045fd1af95e..42ee4b81147 100644 --- a/test/unit/math/prim/mat/vectorize/foo_fun.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -3,7 +3,8 @@ #include #include -#include +#include +#include namespace stan { From 3a743e84580b363c9ca32aeb2ebdb31fd461c635 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Thu, 11 Feb 2016 13:23:09 -0500 Subject: [PATCH 12/36] removing erroneous .d. checkins --- .../math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 | 0 .../math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 | 7 ------- .../math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 | 7 ------- .../math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 | 7 ------- .../prim/mat/vectorize/apply_scalar_unary_test.d.95594 | 0 .../prim/mat/vectorize/apply_scalar_unary_test.d.99349 | 7 ------- 6 files changed, 28 deletions(-) delete mode 100644 test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.d.15772 deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 deleted file mode 100644 index 6f6cba2184e..00000000000 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.1471 +++ /dev/null @@ -1,7 +0,0 @@ -apply_scalar_unary_test.o: \ - test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ - stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ - test/unit/math/prim/mat/vectorize/foo_fun.hpp \ - test/unit/math/prim/mat/vectorize/expect_values.hpp \ - test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ - test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 deleted file mode 100644 index 6f6cba2184e..00000000000 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.3332 +++ /dev/null @@ -1,7 +0,0 @@ -apply_scalar_unary_test.o: \ - test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ - stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ - test/unit/math/prim/mat/vectorize/foo_fun.hpp \ - test/unit/math/prim/mat/vectorize/expect_values.hpp \ - test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ - test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 deleted file mode 100644 index 6f6cba2184e..00000000000 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.5193 +++ /dev/null @@ -1,7 +0,0 @@ -apply_scalar_unary_test.o: \ - test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ - stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ - test/unit/math/prim/mat/vectorize/foo_fun.hpp \ - test/unit/math/prim/mat/vectorize/expect_values.hpp \ - test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ - test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.95594 deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 deleted file mode 100644 index 6f6cba2184e..00000000000 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.d.99349 +++ /dev/null @@ -1,7 +0,0 @@ -apply_scalar_unary_test.o: \ - test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp \ - stan/math/prim/mat/vectorize/apply_scalar_unary.hpp \ - test/unit/math/prim/mat/vectorize/foo_fun.hpp \ - test/unit/math/prim/mat/vectorize/expect_values.hpp \ - test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp \ - test/unit/math/prim/mat/vectorize/expect_errors.hpp From c7dce1da133039a05e2d60e62ef57045e2a540d8 Mon Sep 17 00:00:00 2001 From: Bob Carpenter Date: Thu, 11 Feb 2016 13:24:00 -0500 Subject: [PATCH 13/36] removing more erroneous .d. checkins --- test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 | 0 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 delete mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.31969 deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.d.33834 deleted file mode 100644 index e69de29bb2d..00000000000 From 35a67493f9ec2a2724dfe023b0ed1ca76c814fe8 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 11 Feb 2016 16:57:23 -0500 Subject: [PATCH 14/36] Updated so that foo_base_test is pulled out into its own file and the expect_values function is named differently for each test type --- .../mat/vectorize/apply_scalar_unary_test.cpp | 42 +-- .../fwd/mat/vectorize/fwd_expect_values.hpp | 259 +++++++++++++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 43 +-- .../mix/mat/vectorize/mix_expect_values.hpp | 306 ++++++++++++++++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 67 +--- .../math/prim/mat/vectorize/foo_base_test.hpp | 73 +++++ .../prim/mat/vectorize/prim_expect_values.hpp | 169 ++++++++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 41 +-- .../rev/mat/vectorize/rev_expect_values.hpp | 240 ++++++++++++++ 9 files changed, 1055 insertions(+), 185 deletions(-) create mode 100644 test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp create mode 100644 test/unit/math/mix/mat/vectorize/mix_expect_values.hpp create mode 100644 test/unit/math/prim/mat/vectorize/foo_base_test.hpp create mode 100644 test/unit/math/prim/mat/vectorize/prim_expect_values.hpp create mode 100644 test/unit/math/rev/mat/vectorize/rev_expect_values.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index 03a57273fdb..ed88e9da837 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,49 +1,11 @@ -#include -#include +#include #include -#include +#include #include #include #include #include -struct foo_base_test { - - template - static R apply(const T& x) { - using stan::math::foo; - return foo(x); - } - - template - static stan::math::fvar apply_base(stan::math::fvar x) { - return apply >(x); - } - - static std::vector valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1.3); - valid_inputs.push_back(-2.6); - valid_inputs.push_back(0); - valid_inputs.push_back(-0.2); - - return valid_inputs; - } - - static std::vector illegal_inputs() { - using std::vector; - - vector illegal_inputs(2, 10.6); - illegal_inputs.push_back(25.7); - illegal_inputs.push_back(100.7); - - return illegal_inputs; - } -}; - - // this tests that the expect_values test works on a mock function TEST(MathFwdMatVectorize, applyScalarUnaryMock) { expect_values(); diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp new file mode 100644 index 00000000000..a47593b623a --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp @@ -0,0 +1,259 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include +#include + +template +static inline std::vector +build_valid_fvar_vector(std::vector double_vector, + int seed_index = -1) { + return F::valid_inputs(); +} + +template +static inline std::vector > +build_valid_fvar_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector template_vector = + build_valid_fvar_vector(vector(), seed_index); + + for (int i = 0; i < template_vector.size(); ++i) { + if (seed_index == i) + fvar_vector.push_back( + fvar(template_vector[i], template_vector[i])); + else + fvar_vector.push_back(fvar(template_vector[i])); + } + return fvar_vector; +} + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_valid_fvar_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_valid_fvar_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_valid_fvar_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +static inline void test_fvar(double exp_var, double test_var) { + EXPECT_FLOAT_EQ(exp_var, test_var); +} + +template +static inline void test_fvar(V exp_var, V test_var) { + test_fvar(exp_var.val(), test_var.val()); + test_fvar(exp_var.d_, test_var.d_); +} + +template +void expect_scalar_value() { + using stan::math::fvar; + using stan::test::expect_match_return_t; + using std::vector; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + V fy = F::template apply(y[i]); + V exp_y = F::apply_base(y[i]); + test_fvar(exp_y, fy); + } + + expect_match_return_t(); + expect_match_return_t, + std::vector >(); + +} + +template +void expect_std_vector_values() { + using stan::math::foo; + using std::vector; + using stan::math::fvar; + using stan::math::foo; + + size_t num_inputs = F::valid_inputs().size(); + + for (size_t i = 0; i < num_inputs; ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector fy = F::template apply >(y); + EXPECT_EQ(y.size(), fy.size()); + test_fvar(F::apply_base(y[i]), fy[i]); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + + vector > z; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) + z.push_back(build_valid_fvar_vector(vector(), j)); + else + z.push_back(build_valid_fvar_vector(vector())); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + test_fvar(F::apply_base(z[i][j]), fz[i][j]); + } + } +} + +template +void expect_matrix_values() { + using stan::math::fvar; + using std::vector; + typedef + Eigen::Matrix MatrixXvar; + + size_t num_inputs = F::valid_inputs().size(); + size_t num_cols = 3; + MatrixXvar template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + MatrixXvar a = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + test_fvar(F::apply_base(a(i)), fa(i)); + } + + size_t vector_matrix_size = 2; + for (int i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector b; + for (int k = 0; k < vector_matrix_size; ++k) + if (k == i) + b.push_back(build_valid_fvar_matrix(template_matrix, j)); + else + b.push_back(build_valid_fvar_matrix(template_matrix)); + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(b[i](j)), fb[i](j)); + } + } + + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + MatrixXvar a = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar fab = foo(a.block(block_i, block_j, 1, 1)); + test_fvar(F::apply_base(a(1,1)), fab(0,0)); +} + +template +void expect_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar b = build_valid_fvar_matrix(template_vector, i); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_valid_fvar_matrix(template_vector, j)); + else + c.push_back(build_valid_fvar_matrix(template_vector)); + vector fc = F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + test_fvar(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +template +void expect_row_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_valid_fvar_matrix(template_vector, j)); + else + c.push_back(build_valid_fvar_matrix(template_vector)); + vector fc = + F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + test_fvar(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +template +void expect_values() { + using stan::math::fvar; + using std::vector; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_values >(); + expect_std_vector_values > >(); + expect_matrix_values >(); + expect_matrix_values > >(); + expect_vector_values >(); + expect_vector_values > >(); + expect_row_vector_values >(); + expect_row_vector_values > >(); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp index 743ab828ad2..620840ae856 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,50 +1,11 @@ -#include -#include -#include +#include #include -#include +#include #include #include #include #include -struct foo_base_test { - - template - static R apply(const T& x) { - using stan::math::foo; - return foo(x); - } - - template - static stan::math::fvar apply_base(stan::math::fvar x) { - return apply >(x); - } - - static std::vector valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1.3); - valid_inputs.push_back(-2.6); - valid_inputs.push_back(0); - valid_inputs.push_back(-0.2); - - return valid_inputs; - } - - static std::vector illegal_inputs() { - using std::vector; - - vector illegal_inputs(2, 10.6); - illegal_inputs.push_back(25.7); - illegal_inputs.push_back(100.25); - - return illegal_inputs; - } -}; - - // this tests that the expect_values test works on a mock function TEST(MathFwdMatVectorize, applyScalarUnaryMock) { expect_values(); diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp new file mode 100644 index 00000000000..b1e2e0e0c0a --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp @@ -0,0 +1,306 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +static inline std::vector +build_valid_fvar_vector(std::vector var_vector, + int seed_index = -1) { + std::vector inputs = F::valid_inputs(); + for (int i = 0; i < inputs.size(); ++i) { + var_vector.push_back(inputs[i]); + } + return var_vector; +} + +template +static inline std::vector > +build_valid_fvar_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector val_vector = + build_valid_fvar_vector(vector(), seed_index); + vector d_vector; + if (seed_index != -1) + d_vector = build_valid_fvar_vector(vector(), seed_index); + + for (int i = 0; i < val_vector.size(); ++i) { + if (seed_index == i) + fvar_vector.push_back( + fvar(val_vector[i], d_vector[i])); + else + fvar_vector.push_back(fvar(val_vector[i])); + } + return fvar_vector; +} + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_valid_fvar_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_valid_fvar_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_valid_fvar_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +static inline void test_fvar(stan::math::var exp_var, + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { + EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); + AVEC exp_y = createAVEC(base_exp_var); + VEC exp_g; + exp_var.grad(exp_y, exp_g); + AVEC test_y = createAVEC(base_test_var); + VEC test_g; + test_var.grad(test_y, test_g); + EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); +} + +template +static inline void test_fvar(V exp_var, V base_exp_var, + V test_var, V base_test_var) { + test_fvar(exp_var.val(), base_exp_var.val(), + test_var.val(), base_test_var.val()); + test_fvar(exp_var.d_, base_exp_var.d_, + test_var.d_, base_test_var.d_); +} + +template +void expect_scalar_value() { + using stan::math::fvar; + using stan::test::expect_match_return_t; + using std::vector; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector z = + build_valid_fvar_vector(vector(), i); + V fz = F::template apply(z[i]); + test_fvar(F::apply_base(y[i]), y[i], fz, z[i]); + } + expect_match_return_t(); + expect_match_return_t, + std::vector >(); +} + +template +void expect_std_vector_values() { + using std::vector; + using stan::math::fvar; + + size_t num_inputs = F::valid_inputs().size(); + + for (size_t i = 0; i < num_inputs; ++i) { + vector y = + build_valid_fvar_vector(vector(), i); + vector z = + build_valid_fvar_vector(vector(), i); + vector fz = F::template apply >(z); + EXPECT_EQ(z.size(), fz.size()); + test_fvar(F::apply_base(y[i]), y[i], fz[i], z[i]); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + + vector > a; + vector > b; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) { + a.push_back(build_valid_fvar_vector(vector(), j)); + b.push_back(build_valid_fvar_vector(vector(), j)); + } + else { + a.push_back(build_valid_fvar_vector(vector())); + b.push_back(build_valid_fvar_vector(vector())); + } + } + vector > fb = + F::template apply > >(b); + + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); + } + } +} + +template +void expect_matrix_values() { + using stan::math::fvar; + using std::vector; + typedef + Eigen::Matrix MatrixXvar; + + size_t num_inputs = F::valid_inputs().size(); + size_t num_cols = 3; + MatrixXvar template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + MatrixXvar y = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar z = build_valid_fvar_matrix(template_matrix, i); + MatrixXvar fz = F::template apply(z); + EXPECT_EQ(z.size(), fz.size()); + test_fvar(F::apply_base(y(i)), y(i), fz(i), z(i)); + } + + size_t vector_matrix_size = 2; + for (int i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector a; + vector b; + for (int k = 0; k < vector_matrix_size; ++k) + if (k == i) { + a.push_back(build_valid_fvar_matrix(template_matrix, j)); + b.push_back(build_valid_fvar_matrix(template_matrix, j)); + } + else { + a.push_back(build_valid_fvar_matrix(template_matrix)); + b.push_back(build_valid_fvar_matrix(template_matrix)); + } + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + test_fvar(F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); + } + } + + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + MatrixXvar c = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar d = build_valid_fvar_matrix(template_matrix, seed_i); + MatrixXvar fab = foo(d.block(block_i, block_j, 1, 1)); + test_fvar(F::apply_base(c(block_i, block_j)), c(block_i, block_j), + fab(0,0), d(block_i, block_j)); +} + +template +void expect_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar a = build_valid_fvar_matrix(template_vector, i); + VectorXvar b = build_valid_fvar_matrix(template_vector, i); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_valid_fvar_matrix(template_vector, j)); + d.push_back(build_valid_fvar_matrix(template_vector, j)); + } + else { + c.push_back(build_valid_fvar_matrix(template_vector)); + d.push_back(build_valid_fvar_matrix(template_vector)); + } + vector fd = F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +template +void expect_row_vector_values() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar a = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_valid_fvar_matrix(template_vector, j)); + d.push_back(build_valid_fvar_matrix(template_vector, j)); + } + else { + c.push_back(build_valid_fvar_matrix(template_vector)); + d.push_back(build_valid_fvar_matrix(template_vector)); + } + vector fd = + F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +template +void expect_values() { + using stan::math::fvar; + using stan::math::var; + using std::vector; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_values >(); + expect_std_vector_values > >(); + expect_matrix_values >(); + expect_matrix_values > >(); + expect_vector_values >(); + expect_vector_values > >(); + expect_row_vector_values >(); + expect_row_vector_values > >(); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 17a04379646..96fdadf47c3 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,73 +1,10 @@ -#include -#include +#include #include -#include +#include #include #include #include -struct foo_base_test { - - template - static R apply(const T& x) { - using stan::math::foo; - return foo(x); - } - - static double apply_base(double x) { - return apply(x); - } - - static std::vector valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1.3); - valid_inputs.push_back(-2.6); - valid_inputs.push_back(0); - valid_inputs.push_back(0.2); - - return valid_inputs; - } - - static std::vector int_valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1); - valid_inputs.push_back(-2); - valid_inputs.push_back(0); - valid_inputs.push_back(3); - - return valid_inputs; - } - - static std::vector illegal_inputs() { - using std::vector; - - vector illegal_inputs; - illegal_inputs.push_back(10.6); - illegal_inputs.push_back(25.7); - illegal_inputs.push_back(100.6); - illegal_inputs.push_back(50.7); - - return illegal_inputs; - } - - static std::vector int_illegal_inputs() { - using std::vector; - - vector illegal_inputs; - illegal_inputs.push_back(10); - illegal_inputs.push_back(25); - illegal_inputs.push_back(100); - illegal_inputs.push_back(50); - - return illegal_inputs; - } -}; - - // this tests that the expect_values test works on a mock function TEST(MathPrimMatVectorize, applyScalarUnaryMock) { expect_values(); diff --git a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp new file mode 100644 index 00000000000..5faca17ba2f --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp @@ -0,0 +1,73 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP + +#include +#include +#include +#include + +struct foo_base_test { + + template + static R apply(const T& x) { + using stan::math::foo; + return foo(x); + } + + static double apply_base(const int x) { + return apply(x); + } + + template + static T apply_base(const T& x) { + return apply(x); + } + + static std::vector valid_inputs() { + using std::vector; + + vector valid_inputs; + valid_inputs.push_back(1.3); + valid_inputs.push_back(-2.6); + valid_inputs.push_back(0); + valid_inputs.push_back(-0.2); + + return valid_inputs; + } + + static std::vector illegal_inputs() { + using std::vector; + + vector illegal_inputs(2, 10.6); + illegal_inputs.push_back(25.7); + illegal_inputs.push_back(100.25); + + return illegal_inputs; + } + + static std::vector int_valid_inputs() { + using std::vector; + + vector valid_inputs; + valid_inputs.push_back(1); + valid_inputs.push_back(-2); + valid_inputs.push_back(0); + valid_inputs.push_back(3); + + return valid_inputs; + } + + static std::vector int_illegal_inputs() { + using std::vector; + + vector illegal_inputs; + illegal_inputs.push_back(10); + illegal_inputs.push_back(25); + illegal_inputs.push_back(100); + illegal_inputs.push_back(50); + + return illegal_inputs; + } + +}; +#endif diff --git a/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp b/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp new file mode 100644 index 00000000000..17b5702581e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp @@ -0,0 +1,169 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include + +// TODO(carpenter): figure out some way to deal with constraints (transforms) +// including testing out of support values +// ?? wrap this all up in a general transform framework + + + +template +void expect_scalar_value() { + using std::vector; + + vector valid_inputs = F::valid_inputs(); + vector int_valid_inputs = F::int_valid_inputs(); + + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + int input = int_valid_inputs[i]; + double v = F::template apply(input); + EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); + } + + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), + F::template apply(valid_inputs[i])); + } +} + +template +void expect_std_vectors_value() { + using std::vector; + + vector valid_inputs = F::valid_inputs(); + + vector fy = F::template apply >(valid_inputs); + EXPECT_EQ(valid_inputs.size(), fy.size()); + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), fy[i]); + } + + vector > z; + z.push_back(valid_inputs); + z.push_back(valid_inputs); + vector > fz = + F::template apply > >(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); + } + + vector int_valid_inputs = F::int_valid_inputs(); + vector u2 = F::template apply >(int_valid_inputs); + EXPECT_EQ(int_valid_inputs.size(), u2.size()); + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(int_valid_inputs[i]), u2[i]); + } +} + +template +void expect_matrix_value() { + using Eigen::MatrixXd; + using Eigen::RowVectorXd; + using std::vector; + + vector valid_inputs = F::valid_inputs(); + RowVectorXd eigen_valid_inputs = + RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); + size_t num_rows = 3; + MatrixXd a(num_rows, valid_inputs.size()); + + for (size_t i = 0; i < num_rows; i++) { + a.row(i) << eigen_valid_inputs; + } + + MatrixXd fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); + + MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)), fab(0,0)); + + vector d; + d.push_back(a); + d.push_back(a); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].rows(), fd[i].rows()); + EXPECT_EQ(d[i].cols(), fd[i].cols()); + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +template +void expect_vector_value() { + using Eigen::VectorXd; + using std::vector; + + std::vector valid_inputs = F::valid_inputs(); + + VectorXd b = VectorXd::Map(valid_inputs.data(), valid_inputs.size()); + VectorXd fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); + + vector d; + d.push_back(b); + d.push_back(b); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +template +void expect_row_vector_value() { + using Eigen::RowVectorXd; + using std::vector; + + std::vector valid_inputs = F::valid_inputs(); + + RowVectorXd c = + RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); + + RowVectorXd fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); + + vector d; + d.push_back(c); + d.push_back(c); + + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_values() { + expect_scalar_value(); + expect_std_vectors_value(); + expect_matrix_value(); + expect_vector_value(); + expect_row_vector_value(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 7d89bc0b579..b586e8e1f8a 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,48 +1,11 @@ -#include -#include +#include +#include #include -#include #include #include #include #include -struct foo_base_test { - - template - static R apply(const T& x) { - using stan::math::foo; - return foo(x); - } - - static stan::math::var apply_base(stan::math::var x) { - return apply(x); - } - - static std::vector valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1.3); - valid_inputs.push_back(-2.6); - valid_inputs.push_back(0); - valid_inputs.push_back(-0.7); - - return valid_inputs; - } - - static std::vector illegal_inputs() { - using std::vector; - - vector illegal_inputs(2, 10.6); - illegal_inputs.push_back(25.7); - illegal_inputs.push_back(100.25); - - return illegal_inputs; - } -}; - - // this tests that the expect_values test works on a mock function TEST(MathRevMatVectorize, applyScalarUnaryMock) { expect_values(); diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp new file mode 100644 index 00000000000..67277e95e8b --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp @@ -0,0 +1,240 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include + +template +static inline Eigen::Matrix +build_valid_var_matrix(const T& x) { + Eigen::Matrix var_matrix(x.rows(), x.cols()); + std::vector inputs = F::valid_inputs(); + for (int i = 0; i < x.size(); ++i) { + var_matrix(i) = inputs[(i % inputs.size())]; + } + return var_matrix; +} + +template +static inline std::vector +build_valid_var_vector() { + + std::vector inputs = F::valid_inputs(); + std::vector var_vector; + + for (int i = 0; i < inputs.size(); ++i) { + var_vector.push_back(inputs[i]); + } + + return var_vector; +} + +template +static inline void test_autodiff(double test_val, double test_adj) { + using stan::math::var; + + var x = test_val; + F::apply_base(x).grad(); + EXPECT_FLOAT_EQ(x.adj(), test_adj); +} + +template +void expect_scalar_value() { + using stan::math::var; + + std::vector y = build_valid_var_vector(); + for (size_t i = 0; i < y.size(); ++i) { + var fy = F::template apply(y[i]); + + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); + + fy.grad(); + test_autodiff(y[i].val(), y[i].adj()); + } +} + +template +void expect_std_vectors_value() { + using std::vector; + using stan::math::var; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + + vector y = build_valid_var_vector(); + vector fy = F::template apply >(y); + + EXPECT_EQ(y.size(), fy.size()); + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); + + fy[i].grad(); + test_autodiff(y[i].val(), y[i].adj()); + } +} + +template +void expect_std_vector_vectors_value() { + using std::vector; + using stan::math::var; + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + + for (size_t j = 0; j < F::valid_inputs().size(); ++j) { + + vector > z; + for (size_t i = 0; i < vector_vector_size; ++i) { + z.push_back(build_valid_var_vector()); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); + + fz[i][j].grad(); + test_autodiff(z[i][j].val(), z[i][j].adj()); + } + } +} + +template +void expect_matrix_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix MatrixXvar; + + size_t num_cols = 3; + size_t num_inputs = F::valid_inputs().size(); + MatrixXvar template_matrix(num_inputs, num_cols); + + for (size_t i = 0; i < template_matrix.size(); ++i) { + MatrixXvar a = build_valid_var_matrix(template_matrix); + MatrixXvar fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); + + fa(i).grad(); + test_autodiff(a(i).val(), a(i).adj()); + } + + size_t vector_matrix_size = 2; + for (size_t i = 0; i < vector_matrix_size; ++i) { + for (size_t j = 0; j < template_matrix.size(); ++j) { + + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) + b.push_back(build_valid_var_matrix(template_matrix)); + vector fb = F::template apply >(b); + + EXPECT_EQ(b[i].size(), fb[i].size()); + EXPECT_EQ(b[i].rows(), fb[i].rows()); + EXPECT_EQ(b[i].cols(), fb[i].cols()); + EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); + + fb[i](j).grad(); + test_autodiff(b[i](j).val(), b[i](j).adj()); + } + } + + MatrixXvar a = build_valid_var_matrix(template_matrix); + MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); + fab(0,0).grad(); + test_autodiff(a(1,1).val(), a(1,1).adj()); +} + +template +void expect_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar b = build_valid_var_matrix(template_vector); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + test_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_valid_var_matrix(template_vector)); + vector fvb = F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + test_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +template +void expect_row_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar b = build_valid_var_matrix(template_vector); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + test_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_valid_var_matrix(template_vector)); + vector fvb = + F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + test_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +template +void expect_values() { + expect_scalar_value(); + expect_std_vectors_value(); + expect_std_vector_vectors_value(); + expect_matrix_value(); + expect_vector_value(); + expect_row_vector_value(); +} + +#endif From db1ffe2056d4a7900af65e3172e3716363abe972 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Fri, 12 Feb 2016 13:39:12 -0500 Subject: [PATCH 15/36] Updated to use one expect_error header file and clean up expect_values files --- .../mat/vectorize/apply_scalar_unary_test.cpp | 2 +- .../math/fwd/mat/vectorize/expect_errors.hpp | 120 ------- .../math/fwd/mat/vectorize/expect_values.hpp | 259 --------------- .../fwd/mat/vectorize/fwd_expect_errors.hpp | 30 ++ .../fwd/mat/vectorize/fwd_expect_values.hpp | 2 - .../mat/vectorize/apply_scalar_unary_test.cpp | 2 +- .../math/mix/mat/vectorize/expect_errors.hpp | 122 ------- .../math/mix/mat/vectorize/expect_values.hpp | 306 ------------------ .../mix/mat/vectorize/mix_expect_errors.hpp | 25 ++ .../mat/vectorize/apply_scalar_unary_test.cpp | 2 +- .../math/prim/mat/vectorize/expect_errors.hpp | 134 ++++---- .../prim/mat/vectorize/prim_expect_errors.hpp | 25 ++ .../mat/vectorize/apply_scalar_unary_test.cpp | 2 +- .../math/rev/mat/vectorize/expect_errors.hpp | 113 ------- .../math/rev/mat/vectorize/expect_values.hpp | 241 -------------- .../rev/mat/vectorize/rev_expect_errors.hpp | 20 ++ 16 files changed, 177 insertions(+), 1228 deletions(-) delete mode 100644 test/unit/math/fwd/mat/vectorize/expect_errors.hpp delete mode 100644 test/unit/math/fwd/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/expect_errors.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp create mode 100644 test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp delete mode 100644 test/unit/math/rev/mat/vectorize/expect_errors.hpp delete mode 100644 test/unit/math/rev/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index ed88e9da837..c44df4d003e 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp deleted file mode 100644 index 1d54099ba8e..00000000000 --- a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp +++ /dev/null @@ -1,120 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP - -#include -#include -#include -#include -#include -#include -#include - -template -void expect_std_vectors_error() { - using std::vector; - using stan::math::fvar; - - vector illegal_inputs = F::illegal_inputs(); - vector y; - - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::template apply(illegal_inputs[i]), - std::domain_error); - y.push_back(illegal_inputs[i]); - } - - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); -} - -template -void expect_matrix_error() { - using std::vector; - using stan::math::fvar; - typedef - Eigen::Matrix MatrixXvar; - - vector illegal_inputs = F::illegal_inputs(); - size_t num_rows = 3; - MatrixXvar a(num_rows, illegal_inputs.size()); - - for (size_t i = 0; i < num_rows; ++i) { - for (size_t j = 0; j < illegal_inputs.size(); ++j) - a(i, j) = illegal_inputs[j]; - } - - EXPECT_THROW(F::template apply(a), std::domain_error); - - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix VectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - VectorXvar b = VectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - b(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_row_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix RowVectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - c(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_errors() { - using stan::math::fvar; - - expect_std_vectors_error >(); - expect_std_vectors_error > >(); - expect_matrix_error >(); - expect_matrix_error > >(); - expect_vector_error >(); - expect_vector_error > >(); - expect_row_vector_error >(); - expect_row_vector_error > >(); -} - -#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp deleted file mode 100644 index c58d2c039bf..00000000000 --- a/test/unit/math/fwd/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,259 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include -#include -#include - -template -static inline std::vector -build_valid_fvar_vector(std::vector double_vector, - int seed_index = -1) { - return F::valid_inputs(); -} - -template -static inline std::vector > -build_valid_fvar_vector(std::vector > fvar_vector, - int seed_index = -1) { - using std::vector; - using stan::math::fvar; - - vector template_vector = - build_valid_fvar_vector(vector(), seed_index); - - for (int i = 0; i < template_vector.size(); ++i) { - if (seed_index == i) - fvar_vector.push_back( - fvar(template_vector[i], template_vector[i])); - else - fvar_vector.push_back(fvar(template_vector[i])); - } - return fvar_vector; -} - -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_valid_fvar_matrix(const T& x, int seed_index = -1) { - - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); - size_t num_inputs = F::valid_inputs().size(); - for (int i = 0; i < x.size(); ++i) { - std::vector inputs; - if (seed_index == i) - inputs = build_valid_fvar_vector(std::vector(), - (seed_index % num_inputs)); - else - inputs = build_valid_fvar_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; - } - return fvar_matrix; -} - -static inline void test_fvar(double exp_var, double test_var) { - EXPECT_FLOAT_EQ(exp_var, test_var); -} - -template -static inline void test_fvar(V exp_var, V test_var) { - test_fvar(exp_var.val(), test_var.val()); - test_fvar(exp_var.d_, test_var.d_); -} - -template -void expect_scalar_value() { - using stan::math::fvar; - using stan::test::expect_match_return_t; - using std::vector; - - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - V fy = F::template apply(y[i]); - V exp_y = F::apply_base(y[i]); - test_fvar(exp_y, fy); - } - - expect_match_return_t(); - expect_match_return_t, - std::vector >(); - -} - -template -void expect_std_vector_values() { - using stan::math::foo; - using std::vector; - using stan::math::fvar; - using stan::math::foo; - - size_t num_inputs = F::valid_inputs().size(); - - for (size_t i = 0; i < num_inputs; ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - vector fy = F::template apply >(y); - EXPECT_EQ(y.size(), fy.size()); - test_fvar(F::apply_base(y[i]), fy[i]); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - - vector > z; - for (size_t k = 0; k < num_inputs; ++k) { - if (i == k) - z.push_back(build_valid_fvar_vector(vector(), j)); - else - z.push_back(build_valid_fvar_vector(vector())); - } - vector > fz = - F::template apply > >(z); - - EXPECT_EQ(z.size(), fz.size()); - EXPECT_EQ(z[i].size(), fz[i].size()); - test_fvar(F::apply_base(z[i][j]), fz[i][j]); - } - } -} - -template -void expect_matrix_values() { - using stan::math::fvar; - using std::vector; - typedef - Eigen::Matrix MatrixXvar; - - size_t num_inputs = F::valid_inputs().size(); - size_t num_cols = 3; - MatrixXvar template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar a = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - test_fvar(F::apply_base(a(i)), fa(i)); - } - - size_t vector_matrix_size = 2; - for (int i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - vector b; - for (int k = 0; k < vector_matrix_size; ++k) - if (k == i) - b.push_back(build_valid_fvar_matrix(template_matrix, j)); - else - b.push_back(build_valid_fvar_matrix(template_matrix)); - vector fb = F::template apply >(b); - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(b[i](j)), fb[i](j)); - } - } - - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - MatrixXvar a = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar fab = foo(a.block(block_i, block_j, 1, 1)); - test_fvar(F::apply_base(a(1,1)), fab(0,0)); -} - -template -void expect_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar b = build_valid_fvar_matrix(template_vector, i); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(b(i)), fb(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) - c.push_back(build_valid_fvar_matrix(template_vector, j)); - else - c.push_back(build_valid_fvar_matrix(template_vector)); - vector fc = F::template apply >(c); - - EXPECT_EQ(c.size(), fc.size()); - EXPECT_EQ(c[i].size(), fc[i].size()); - test_fvar(F::apply_base(c[i](j)), fc[i](j)); - } - } -} - -template -void expect_row_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(b(i)), fb(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) - c.push_back(build_valid_fvar_matrix(template_vector, j)); - else - c.push_back(build_valid_fvar_matrix(template_vector)); - vector fc = - F::template apply >(c); - - EXPECT_EQ(c.size(), fc.size()); - EXPECT_EQ(c[i].size(), fc[i].size()); - test_fvar(F::apply_base(c[i](j)), fc[i](j)); - } - } -} - -template -void expect_values() { - using stan::math::fvar; - using std::vector; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_values >(); - expect_std_vector_values > >(); - expect_matrix_values >(); - expect_matrix_values > >(); - expect_vector_values >(); - expect_vector_values > >(); - expect_row_vector_values >(); - expect_row_vector_values > >(); -} - -#endif diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp new file mode 100644 index 00000000000..4ce64353422 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp @@ -0,0 +1,30 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include +#include + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + using stan::math::fvar; + + expect_scalar_error >(); + expect_scalar_error > >(); + expect_std_vectors_error >(); + expect_std_vectors_error > >(); + expect_matrix_error >(); + expect_matrix_error > >(); + expect_vector_error >(); + expect_vector_error > >(); + expect_row_vector_error >(); + expect_row_vector_error > >(); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp index a47593b623a..7a57edea059 100644 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp @@ -2,8 +2,6 @@ #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP #include -#include -#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp index 620840ae856..8df68809bdd 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/expect_errors.hpp b/test/unit/math/mix/mat/vectorize/expect_errors.hpp deleted file mode 100644 index 9c900497a30..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_errors.hpp +++ /dev/null @@ -1,122 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP - -#include -#include -#include -#include -#include -#include -#include -#include - -template -void expect_std_vectors_error() { - using std::vector; - using stan::math::fvar; - - vector illegal_inputs = F::illegal_inputs(); - vector y; - - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::template apply(illegal_inputs[i]), - std::domain_error); - y.push_back(illegal_inputs[i]); - } - - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); -} - -template -void expect_matrix_error() { - using std::vector; - using stan::math::fvar; - typedef - Eigen::Matrix MatrixXvar; - - vector illegal_inputs = F::illegal_inputs(); - size_t num_rows = 3; - MatrixXvar a(num_rows, illegal_inputs.size()); - - for (size_t i = 0; i < num_rows; ++i) { - for (size_t j = 0; j < illegal_inputs.size(); ++j) - a(i, j) = illegal_inputs[j]; - } - - EXPECT_THROW(F::template apply(a), std::domain_error); - - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix VectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - VectorXvar b = VectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - b(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_row_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix RowVectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - c(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_errors() { - using stan::math::fvar; - using stan::math::var; - - expect_std_vectors_error >(); - expect_std_vectors_error > >(); - expect_matrix_error >(); - expect_matrix_error > >(); - expect_vector_error >(); - expect_vector_error > >(); - expect_row_vector_error >(); - expect_row_vector_error > >(); -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_values.hpp b/test/unit/math/mix/mat/vectorize/expect_values.hpp deleted file mode 100644 index 3e2fa722c41..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,306 +0,0 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -template -static inline std::vector -build_valid_fvar_vector(std::vector var_vector, - int seed_index = -1) { - std::vector inputs = F::valid_inputs(); - for (int i = 0; i < inputs.size(); ++i) { - var_vector.push_back(inputs[i]); - } - return var_vector; -} - -template -static inline std::vector > -build_valid_fvar_vector(std::vector > fvar_vector, - int seed_index = -1) { - using std::vector; - using stan::math::fvar; - - vector val_vector = - build_valid_fvar_vector(vector(), seed_index); - vector d_vector; - if (seed_index != -1) - d_vector = build_valid_fvar_vector(vector(), seed_index); - - for (int i = 0; i < val_vector.size(); ++i) { - if (seed_index == i) - fvar_vector.push_back( - fvar(val_vector[i], d_vector[i])); - else - fvar_vector.push_back(fvar(val_vector[i])); - } - return fvar_vector; -} - -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_valid_fvar_matrix(const T& x, int seed_index = -1) { - - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); - size_t num_inputs = F::valid_inputs().size(); - for (int i = 0; i < x.size(); ++i) { - std::vector inputs; - if (seed_index == i) - inputs = build_valid_fvar_vector(std::vector(), - (seed_index % num_inputs)); - else - inputs = build_valid_fvar_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; - } - return fvar_matrix; -} - -static inline void test_fvar(stan::math::var exp_var, - stan::math::var base_exp_var, - stan::math::var test_var, - stan::math::var base_test_var) { - EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); - AVEC exp_y = createAVEC(base_exp_var); - VEC exp_g; - exp_var.grad(exp_y, exp_g); - AVEC test_y = createAVEC(base_test_var); - VEC test_g; - test_var.grad(test_y, test_g); - EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); -} - -template -static inline void test_fvar(V exp_var, V base_exp_var, - V test_var, V base_test_var) { - test_fvar(exp_var.val(), base_exp_var.val(), - test_var.val(), base_test_var.val()); - test_fvar(exp_var.d_, base_exp_var.d_, - test_var.d_, base_test_var.d_); -} - -template -void expect_scalar_value() { - using stan::math::fvar; - using stan::test::expect_match_return_t; - using std::vector; - - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - vector z = - build_valid_fvar_vector(vector(), i); - V fz = F::template apply(z[i]); - test_fvar(F::apply_base(y[i]), y[i], fz, z[i]); - } - expect_match_return_t(); - expect_match_return_t, - std::vector >(); -} - -template -void expect_std_vector_values() { - using std::vector; - using stan::math::fvar; - - size_t num_inputs = F::valid_inputs().size(); - - for (size_t i = 0; i < num_inputs; ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - vector z = - build_valid_fvar_vector(vector(), i); - vector fz = F::template apply >(z); - EXPECT_EQ(z.size(), fz.size()); - test_fvar(F::apply_base(y[i]), y[i], fz[i], z[i]); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - - vector > a; - vector > b; - for (size_t k = 0; k < num_inputs; ++k) { - if (i == k) { - a.push_back(build_valid_fvar_vector(vector(), j)); - b.push_back(build_valid_fvar_vector(vector(), j)); - } - else { - a.push_back(build_valid_fvar_vector(vector())); - b.push_back(build_valid_fvar_vector(vector())); - } - } - vector > fb = - F::template apply > >(b); - - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); - } - } -} - -template -void expect_matrix_values() { - using stan::math::fvar; - using std::vector; - typedef - Eigen::Matrix MatrixXvar; - - size_t num_inputs = F::valid_inputs().size(); - size_t num_cols = 3; - MatrixXvar template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar y = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar z = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar fz = F::template apply(z); - EXPECT_EQ(z.size(), fz.size()); - test_fvar(F::apply_base(y(i)), y(i), fz(i), z(i)); - } - - size_t vector_matrix_size = 2; - for (int i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - vector a; - vector b; - for (int k = 0; k < vector_matrix_size; ++k) - if (k == i) { - a.push_back(build_valid_fvar_matrix(template_matrix, j)); - b.push_back(build_valid_fvar_matrix(template_matrix, j)); - } - else { - a.push_back(build_valid_fvar_matrix(template_matrix)); - b.push_back(build_valid_fvar_matrix(template_matrix)); - } - vector fb = F::template apply >(b); - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); - } - } - - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - MatrixXvar c = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar d = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar fab = foo(d.block(block_i, block_j, 1, 1)); - test_fvar(F::apply_base(c(block_i, block_j)), c(block_i, block_j), - fab(0,0), d(block_i, block_j)); -} - -template -void expect_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar a = build_valid_fvar_matrix(template_vector, i); - VectorXvar b = build_valid_fvar_matrix(template_vector, i); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - vector d; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) { - c.push_back(build_valid_fvar_matrix(template_vector, j)); - d.push_back(build_valid_fvar_matrix(template_vector, j)); - } - else { - c.push_back(build_valid_fvar_matrix(template_vector)); - d.push_back(build_valid_fvar_matrix(template_vector)); - } - vector fd = F::template apply >(d); - - EXPECT_EQ(d.size(), fd.size()); - EXPECT_EQ(d[i].size(), fd[i].size()); - test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); - } - } -} - -template -void expect_row_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar a = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - vector d; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) { - c.push_back(build_valid_fvar_matrix(template_vector, j)); - d.push_back(build_valid_fvar_matrix(template_vector, j)); - } - else { - c.push_back(build_valid_fvar_matrix(template_vector)); - d.push_back(build_valid_fvar_matrix(template_vector)); - } - vector fd = - F::template apply >(d); - - EXPECT_EQ(d.size(), fd.size()); - EXPECT_EQ(d[i].size(), fd[i].size()); - test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); - } - } -} - -template -void expect_values() { - using stan::math::fvar; - using stan::math::var; - using std::vector; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_values >(); - expect_std_vector_values > >(); - expect_matrix_values >(); - expect_matrix_values > >(); - expect_vector_values >(); - expect_vector_values > >(); - expect_row_vector_values >(); - expect_row_vector_values > >(); -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp new file mode 100644 index 00000000000..fb319be0e87 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp @@ -0,0 +1,25 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP + +#include +#include +#include + +template +void expect_errors() { + using stan::math::fvar; + using stan::math::var; + + expect_scalar_error >(); + expect_scalar_error > >(); + expect_std_vectors_error >(); + expect_std_vectors_error > >(); + expect_matrix_error >(); + expect_matrix_error > >(); + expect_vector_error >(); + expect_vector_error > >(); + expect_row_vector_error >(); + expect_row_vector_error > >(); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 96fdadf47c3..bdaae517703 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/test/unit/math/prim/mat/vectorize/expect_errors.hpp b/test/unit/math/prim/mat/vectorize/expect_errors.hpp index de903616180..25b0c92a9ad 100644 --- a/test/unit/math/prim/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_errors.hpp @@ -2,131 +2,143 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP #include -#include #include #include -#include -#include - template -void expect_scalar_error() { +void expect_int_scalar_error() { using std::vector; - vector illegal_inputs = F::illegal_inputs(); vector int_illegal_inputs = F::int_illegal_inputs(); for (size_t i = 0; i < int_illegal_inputs.size(); ++i) { int input = int_illegal_inputs[i]; - EXPECT_THROW(F::template apply(input), std::domain_error); - EXPECT_THROW(F::apply_base(static_cast(input)), - std::domain_error); + EXPECT_THROW(F::template apply(input), std::domain_error); + EXPECT_THROW(F::apply_base(input), std::domain_error); } +} + + +template +void expect_scalar_error() { + using std::vector; + + vector illegal_inputs = F::illegal_inputs(); for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::apply_base(illegal_inputs[i]), - std::domain_error); + V input = illegal_inputs[i]; + EXPECT_THROW(F::apply_base(input), std::domain_error); + EXPECT_THROW(F::template apply(input), std::domain_error); } } template -void expect_std_vectors_error() { - using stan::math::foo; +void expect_int_std_vectors_error() { using std::vector; + using stan::math::fvar; - vector illegal_inputs = F::illegal_inputs(); + vector illegal_inputs = F::int_illegal_inputs(); EXPECT_THROW(F::template apply >(illegal_inputs), - std::domain_error); + std::domain_error); - vector > z; + vector > z; z.push_back(illegal_inputs); z.push_back(illegal_inputs); EXPECT_THROW(F::template apply > >(z), std::domain_error); +} - vector int_illegal_inputs = F::int_illegal_inputs(); - EXPECT_THROW(F::template apply >(int_illegal_inputs), - std::domain_error); - vector > z1; - z1.push_back(int_illegal_inputs); - z1.push_back(int_illegal_inputs); - EXPECT_THROW( - F::template apply > >(z1), std::domain_error); +template +void expect_std_vectors_error() { + using std::vector; + using stan::math::fvar; + + vector illegal_inputs = F::illegal_inputs(); + vector y; + + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + EXPECT_THROW(F::template apply(illegal_inputs[i]), + std::domain_error); + y.push_back(illegal_inputs[i]); + } + + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); } -template +template void expect_matrix_error() { - using Eigen::MatrixXd; - using Eigen::RowVectorXd; using std::vector; + using stan::math::fvar; + typedef + Eigen::Matrix MatrixXvar; vector illegal_inputs = F::illegal_inputs(); - RowVectorXd eigen_illegal_inputs = - RowVectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); size_t num_rows = 3; - MatrixXd a(num_rows, illegal_inputs.size()); + MatrixXvar a(num_rows, illegal_inputs.size()); - for (size_t i = 0; i < num_rows; i++) { - a.row(i) << eigen_illegal_inputs; + for (size_t i = 0; i < num_rows; ++i) { + for (size_t j = 0; j < illegal_inputs.size(); ++j) + a(i, j) = illegal_inputs[j]; } - EXPECT_THROW(F::template apply(a), std::domain_error); + EXPECT_THROW(F::template apply(a), std::domain_error); - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), std::domain_error); - vector d; + vector d; d.push_back(a); d.push_back(a); - EXPECT_THROW(F::template apply >(d), std::domain_error); + EXPECT_THROW(F::template apply >(d), + std::domain_error); } -template +template void expect_vector_error() { - using Eigen::VectorXd; using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix VectorXvar; std::vector illegal_inputs = F::illegal_inputs(); - VectorXd b = VectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); - EXPECT_THROW(F::template apply(b), std::domain_error); + VectorXvar b = VectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + b(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); - vector d; + vector d; d.push_back(b); d.push_back(b); - EXPECT_THROW(F::template apply >(d), std::domain_error); + EXPECT_THROW(F::template apply >(d), + std::domain_error); } -template +template void expect_row_vector_error() { - using Eigen::RowVectorXd; using std::vector; + using stan::math::fvar; + typedef Eigen::Matrix RowVectorXvar; std::vector illegal_inputs = F::illegal_inputs(); - RowVectorXd c = - RowVectorXd::Map(illegal_inputs.data(), illegal_inputs.size()); + RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + c(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; + vector d; d.push_back(c); d.push_back(c); - EXPECT_THROW(F::template apply >(d), + EXPECT_THROW(F::template apply >(d), std::domain_error); } -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_errors() { - expect_scalar_error(); - expect_std_vectors_error(); - expect_matrix_error(); - expect_vector_error(); - expect_row_vector_error(); -} - #endif diff --git a/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp b/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp new file mode 100644 index 00000000000..5e1df9d5eb9 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp @@ -0,0 +1,25 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_ERRORS_HPP + +#include +#include +#include +#include +#include +#include + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + expect_int_scalar_error(); + expect_int_std_vectors_error(); + + expect_scalar_error(); + expect_std_vectors_error(); + expect_matrix_error(); + expect_vector_error(); + expect_row_vector_error(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index b586e8e1f8a..5dd17e84ba6 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include #include diff --git a/test/unit/math/rev/mat/vectorize/expect_errors.hpp b/test/unit/math/rev/mat/vectorize/expect_errors.hpp deleted file mode 100644 index c82091f87b9..00000000000 --- a/test/unit/math/rev/mat/vectorize/expect_errors.hpp +++ /dev/null @@ -1,113 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP - -#include -#include -#include -#include -#include -#include -#include - -template -void expect_std_vectors_error() { - using std::vector; - using stan::math::var; - - vector illegal_inputs = F::illegal_inputs(); - vector y; - - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::template apply(illegal_inputs[i]), - std::domain_error); - y.push_back(illegal_inputs[i]); - } - - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); -} - -template -void expect_matrix_error() { - using std::vector; - using stan::math::var; - typedef Eigen::Matrix MatrixXvar; - - vector illegal_inputs = F::illegal_inputs(); - size_t num_rows = 3; - MatrixXvar a(num_rows, illegal_inputs.size()); - - for (size_t i = 0; i < num_rows; ++i) { - for (size_t j = 0; j < illegal_inputs.size(); ++j) - a(i, j) = illegal_inputs[j]; - } - - EXPECT_THROW(F::template apply(a), std::domain_error); - - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_vector_error() { - using std::vector; - using stan::math::var; - typedef Eigen::Matrix VectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - VectorXvar b = VectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - b(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_row_vector_error() { - using std::vector; - using stan::math::var; - typedef Eigen::Matrix RowVectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - c(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_errors() { - expect_std_vectors_error(); - expect_matrix_error(); - expect_vector_error(); - expect_row_vector_error(); -} - -#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp deleted file mode 100644 index e67db37936a..00000000000 --- a/test/unit/math/rev/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,241 +0,0 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include -#include - -template -static inline Eigen::Matrix -build_valid_var_matrix(const T& x) { - Eigen::Matrix var_matrix(x.rows(), x.cols()); - std::vector inputs = F::valid_inputs(); - for (int i = 0; i < x.size(); ++i) { - var_matrix(i) = inputs[(i % inputs.size())]; - } - return var_matrix; -} - -template -static inline std::vector -build_valid_var_vector() { - - std::vector inputs = F::valid_inputs(); - std::vector var_vector; - - for (int i = 0; i < inputs.size(); ++i) { - var_vector.push_back(inputs[i]); - } - - return var_vector; -} - -template -static inline void test_autodiff(double test_val, double test_adj) { - using stan::math::var; - - var x = test_val; - F::apply_base(x).grad(); - EXPECT_FLOAT_EQ(x.adj(), test_adj); -} - -template -void expect_scalar_value() { - using stan::math::var; - - std::vector y = build_valid_var_vector(); - for (size_t i = 0; i < y.size(); ++i) { - var fy = F::template apply(y[i]); - - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); - - fy.grad(); - test_autodiff(y[i].val(), y[i].adj()); - } -} - -template -void expect_std_vectors_value() { - using std::vector; - using stan::math::var; - - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - - vector y = build_valid_var_vector(); - vector fy = F::template apply >(y); - - EXPECT_EQ(y.size(), fy.size()); - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); - - fy[i].grad(); - test_autodiff(y[i].val(), y[i].adj()); - } -} - -template -void expect_std_vector_vectors_value() { - using std::vector; - using stan::math::var; - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - - for (size_t j = 0; j < F::valid_inputs().size(); ++j) { - - vector > z; - for (size_t i = 0; i < vector_vector_size; ++i) { - z.push_back(build_valid_var_vector()); - } - vector > fz = - F::template apply > >(z); - - EXPECT_EQ(z.size(), fz.size()); - EXPECT_EQ(z[i].size(), fz[i].size()); - EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); - - fz[i][j].grad(); - test_autodiff(z[i][j].val(), z[i][j].adj()); - } - } -} - -template -void expect_matrix_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix MatrixXvar; - - size_t num_cols = 3; - size_t num_inputs = F::valid_inputs().size(); - MatrixXvar template_matrix(num_inputs, num_cols); - - for (size_t i = 0; i < template_matrix.size(); ++i) { - MatrixXvar a = build_valid_var_matrix(template_matrix); - MatrixXvar fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); - - fa(i).grad(); - test_autodiff(a(i).val(), a(i).adj()); - } - - size_t vector_matrix_size = 2; - for (size_t i = 0; i < vector_matrix_size; ++i) { - for (size_t j = 0; j < template_matrix.size(); ++j) { - - vector b; - for (size_t k = 0; k < vector_matrix_size; ++k) - b.push_back(build_valid_var_matrix(template_matrix)); - vector fb = F::template apply >(b); - - EXPECT_EQ(b[i].size(), fb[i].size()); - EXPECT_EQ(b[i].rows(), fb[i].rows()); - EXPECT_EQ(b[i].cols(), fb[i].cols()); - EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); - - fb[i](j).grad(); - test_autodiff(b[i](j).val(), b[i](j).adj()); - } - } - - MatrixXvar a = build_valid_var_matrix(template_matrix); - MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); - fab(0,0).grad(); - test_autodiff(a(1,1).val(), a(1,1).adj()); -} - -template -void expect_vector_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar b = build_valid_var_matrix(template_vector); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - test_autodiff(b(i).val(), b(i).adj()); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector vb; - for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_valid_var_matrix(template_vector)); - vector fvb = F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - test_autodiff(vb[i](j).val(), vb[i](j).adj()); - } - } -} - -template -void expect_row_vector_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar b = build_valid_var_matrix(template_vector); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - test_autodiff(b(i).val(), b(i).adj()); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector vb; - for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_valid_var_matrix(template_vector)); - vector fvb = - F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - test_autodiff(vb[i](j).val(), vb[i](j).adj()); - } - } -} - -template -void expect_values() { - expect_scalar_value(); - expect_std_vectors_value(); - expect_std_vector_vectors_value(); - expect_matrix_value(); - expect_vector_value(); - expect_row_vector_value(); -} - -#endif diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp new file mode 100644 index 00000000000..b4a4a1b7618 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp @@ -0,0 +1,20 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_ERRORS_HPP + +#include +#include + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_errors() { + using stan::math::var; + + expect_scalar_error(); + expect_std_vectors_error(); + expect_matrix_error(); + expect_vector_error(); + expect_row_vector_error(); +} + +#endif From 5ed244da4307d476be0d2212af4b41deae71890f Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Fri, 12 Feb 2016 14:23:26 -0500 Subject: [PATCH 16/36] Cleaned up to use only one expect_match_return_t.hpp --- .../mat/vectorize/expect_match_return_t.hpp | 31 ------------------- .../fwd/mat/vectorize/fwd_expect_values.hpp | 5 +-- .../mat/vectorize/expect_match_return_t.hpp | 31 ------------------- .../mix/mat/vectorize/mix_expect_values.hpp | 5 +-- .../mat/vectorize/expect_match_return_t.hpp | 2 ++ .../mat/vectorize/expect_match_return_t.hpp | 30 ------------------ .../rev/mat/vectorize/rev_expect_values.hpp | 5 +-- 7 files changed, 5 insertions(+), 104 deletions(-) delete mode 100644 test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp delete mode 100644 test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp diff --git a/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp deleted file mode 100644 index 8613848419d..00000000000 --- a/test/unit/math/fwd/mat/vectorize/expect_match_return_t.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP - -#include -#include -#include -#include - - -namespace stan { - namespace test { - template - void expect_match_return_t() { - using stan::math::foo_fun; - using stan::math::apply_scalar_unary; - using boost::is_convertible; - - typedef typename apply_scalar_unary::return_t result_t; - - bool T1_to_T2 - = is_convertible::value; - EXPECT_TRUE(T1_to_T2); - - bool T1_from_T2 - = is_convertible::value; - EXPECT_TRUE(T1_from_T2); - } - - } -} -#endif diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp index 7a57edea059..cd2216381c8 100644 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp @@ -2,11 +2,8 @@ #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP #include -#include -#include +#include #include -#include -#include template static inline std::vector diff --git a/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp deleted file mode 100644 index 8613848419d..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_match_return_t.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP - -#include -#include -#include -#include - - -namespace stan { - namespace test { - template - void expect_match_return_t() { - using stan::math::foo_fun; - using stan::math::apply_scalar_unary; - using boost::is_convertible; - - typedef typename apply_scalar_unary::return_t result_t; - - bool T1_to_T2 - = is_convertible::value; - EXPECT_TRUE(T1_to_T2); - - bool T1_from_T2 - = is_convertible::value; - EXPECT_TRUE(T1_from_T2); - } - - } -} -#endif diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp index b1e2e0e0c0a..2c8f4385792 100644 --- a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp +++ b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp @@ -5,11 +5,8 @@ #include #include #include -#include -#include +#include #include -#include -#include #include template diff --git a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp index 16a38ea9d66..f91ddb3fb70 100644 --- a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp @@ -2,6 +2,8 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP #include +#include +#include #include #include #include diff --git a/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp deleted file mode 100644 index 268072275f0..00000000000 --- a/test/unit/math/rev/mat/vectorize/expect_match_return_t.hpp +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP - -#include -#include -#include -#include - -namespace stan { - namespace test { - template - void expect_match_return_t() { - using stan::math::foo_fun; - using stan::math::apply_scalar_unary; - using boost::is_convertible; - - typedef typename apply_scalar_unary::return_t result_t; - - bool T1_to_T2 - = is_convertible::value; - EXPECT_TRUE(T1_to_T2); - - bool T1_from_T2 - = is_convertible::value; - EXPECT_TRUE(T1_from_T2); - } - - } -} -#endif diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp index 67277e95e8b..79f4b200738 100644 --- a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp +++ b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp @@ -1,12 +1,9 @@ #ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP -#include +#include #include -#include #include -#include -#include template static inline Eigen::Matrix Date: Wed, 17 Feb 2016 18:33:26 -0500 Subject: [PATCH 17/36] refactored vectorization tests --- .../mat/vectorize/apply_scalar_unary_test.cpp | 24 +-- .../fwd/mat/vectorize/fwd_expect_errors.hpp | 18 +- .../fwd/mat/vectorize/fwd_expect_values.hpp | 55 +++--- .../mat/vectorize/apply_scalar_unary_test.cpp | 27 +-- .../mix/mat/vectorize/mix_expect_errors.hpp | 11 +- .../mix/mat/vectorize/mix_expect_values.hpp | 37 ++-- test/unit/math/prim/mat/vectorize/README.txt | 29 +++ .../mat/vectorize/apply_scalar_unary_test.cpp | 24 +-- .../math/prim/mat/vectorize/expect_errors.hpp | 144 --------------- .../mat/vectorize/expect_int_scalar_error.hpp | 18 ++ .../vectorize/expect_int_std_vector_error.hpp | 22 +++ .../mat/vectorize/expect_match_return_t.hpp | 33 ++-- .../mat/vectorize/expect_matrix_error.hpp | 29 +++ .../mat/vectorize/expect_row_vector_error.hpp | 27 +++ .../mat/vectorize/expect_scalar_error.hpp | 18 ++ .../mat/vectorize/expect_std_vector_error.hpp | 22 +++ .../math/prim/mat/vectorize/expect_values.hpp | 15 +- .../mat/vectorize/expect_vector_error.hpp | 27 +++ .../math/prim/mat/vectorize/foo_base_test.hpp | 97 ++++++---- test/unit/math/prim/mat/vectorize/foo_fun.hpp | 13 +- .../prim/mat/vectorize/prim_expect_errors.hpp | 34 ++-- .../prim/mat/vectorize/prim_expect_values.hpp | 169 ------------------ .../prim/mat/vectorize/vector_builder.hpp | 47 +++++ .../mat/vectorize/vector_builder_test.cpp | 17 ++ .../mat/vectorize/apply_scalar_unary_test.cpp | 27 +-- .../rev/mat/vectorize/rev_expect_errors.hpp | 10 +- .../rev/mat/vectorize/rev_expect_values.hpp | 8 +- 27 files changed, 445 insertions(+), 557 deletions(-) create mode 100644 test/unit/math/prim/mat/vectorize/README.txt delete mode 100644 test/unit/math/prim/mat/vectorize/expect_errors.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_vector_error.hpp delete mode 100644 test/unit/math/prim/mat/vectorize/prim_expect_values.hpp create mode 100644 test/unit/math/prim/mat/vectorize/vector_builder.hpp create mode 100644 test/unit/math/prim/mat/vectorize/vector_builder_test.cpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index c44df4d003e..8c653ec9bd3 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,29 +1,13 @@ +#include +#include +#include #include #include #include #include -#include -#include -#include // this tests that the expect_values test works on a mock function TEST(MathFwdMatVectorize, applyScalarUnaryMock) { expect_values(); - if (foo_base_test::illegal_inputs().size() > 0) - expect_errors(); -} - -// this tests that the return types work -template -void expect_scalar_unary_return_type() { - using stan::math::fvar; - using stan::math::foo; - using stan::test::expect_match_return_t; - fvar three_var = 3; - fvar exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); - - expect_match_return_t, fvar >(); - expect_match_return_t >, - std::vector > >(); + expect_errors(); } diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp index 4ce64353422..b5decfe7a8c 100644 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp @@ -1,24 +1,20 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP -#include -#include -#include -#include -#include #include -#include +#include +#include +#include +#include +#include -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example template void expect_errors() { using stan::math::fvar; - expect_scalar_error >(); expect_scalar_error > >(); - expect_std_vectors_error >(); - expect_std_vectors_error > >(); + expect_std_vector_error >(); + expect_std_vector_error > >(); expect_matrix_error >(); expect_matrix_error > >(); expect_vector_error >(); diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp index cd2216381c8..3e14863db88 100644 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp @@ -15,17 +15,16 @@ build_valid_fvar_vector(std::vector double_vector, template static inline std::vector > build_valid_fvar_vector(std::vector > fvar_vector, - int seed_index = -1) { + int seed_index = -1) { using std::vector; using stan::math::fvar; - vector template_vector = - build_valid_fvar_vector(vector(), seed_index); + vector template_vector + = build_valid_fvar_vector(vector(), seed_index); - for (int i = 0; i < template_vector.size(); ++i) { - if (seed_index == i) - fvar_vector.push_back( - fvar(template_vector[i], template_vector[i])); + for (size_t i = 0; i < template_vector.size(); ++i) { + if (seed_index == static_cast(i)) + fvar_vector.push_back(fvar(template_vector[i], template_vector[i])); else fvar_vector.push_back(fvar(template_vector[i])); } @@ -66,10 +65,8 @@ static inline void test_fvar(V exp_var, V test_var) { template void expect_scalar_value() { - using stan::math::fvar; using stan::test::expect_match_return_t; using std::vector; - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { vector y = build_valid_fvar_vector(vector(), i); @@ -77,25 +74,17 @@ void expect_scalar_value() { V exp_y = F::apply_base(y[i]); test_fvar(exp_y, fy); } - - expect_match_return_t(); - expect_match_return_t, - std::vector >(); + expect_match_return_t(); + expect_match_return_t, std::vector >(); } template void expect_std_vector_values() { - using stan::math::foo; using std::vector; - using stan::math::fvar; - using stan::math::foo; - size_t num_inputs = F::valid_inputs().size(); - for (size_t i = 0; i < num_inputs; ++i) { - vector y = - build_valid_fvar_vector(vector(), i); + vector y = build_valid_fvar_vector(vector(), i); vector fy = F::template apply >(y); EXPECT_EQ(y.size(), fy.size()); test_fvar(F::apply_base(y[i]), fy[i]); @@ -127,30 +116,29 @@ template void expect_matrix_values() { using stan::math::fvar; using std::vector; - typedef - Eigen::Matrix MatrixXvar; + typedef Eigen::Matrix matrix_t; - size_t num_inputs = F::valid_inputs().size(); - size_t num_cols = 3; - MatrixXvar template_matrix(num_inputs, num_cols); + int num_inputs = F::valid_inputs().size(); + int num_cols = 3; + matrix_t template_matrix(num_inputs, num_cols); for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar a = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar fa = F::template apply(a); + matrix_t a = build_valid_fvar_matrix(template_matrix, i); + matrix_t fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); test_fvar(F::apply_base(a(i)), fa(i)); } size_t vector_matrix_size = 2; - for (int i = 0; i < vector_matrix_size; ++i) { + for (size_t i = 0; i < vector_matrix_size; ++i) { for (int j = 0; j < template_matrix.size(); ++j) { - vector b; - for (int k = 0; k < vector_matrix_size; ++k) + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) if (k == i) b.push_back(build_valid_fvar_matrix(template_matrix, j)); else b.push_back(build_valid_fvar_matrix(template_matrix)); - vector fb = F::template apply >(b); + vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); test_fvar(F::apply_base(b[i](j)), fb[i](j)); @@ -160,8 +148,8 @@ void expect_matrix_values() { int block_i = 1; int block_j = 1; int seed_i = block_j * num_inputs + block_i; - MatrixXvar a = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar fab = foo(a.block(block_i, block_j, 1, 1)); + matrix_t a = build_valid_fvar_matrix(template_matrix, seed_i); + matrix_t fab = foo(a.block(block_i, block_j, 1, 1)); test_fvar(F::apply_base(a(1,1)), fab(0,0)); } @@ -237,7 +225,6 @@ void expect_row_vector_values() { template void expect_values() { using stan::math::fvar; - using std::vector; expect_scalar_value >(); expect_scalar_value > >(); diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp index 8df68809bdd..f050decae32 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,29 +1,16 @@ +#include +#include +#include +#include +#include +#include #include #include #include #include -#include -#include -#include -// this tests that the expect_values test works on a mock function TEST(MathFwdMatVectorize, applyScalarUnaryMock) { expect_values(); - if (foo_base_test::illegal_inputs().size() > 0) - expect_errors(); + expect_errors(); } -// this tests that the return types work -template -void expect_scalar_unary_return_type() { - using stan::math::fvar; - using stan::math::foo; - using stan::test::expect_match_return_t; - fvar three_var = 3; - fvar exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); - - expect_match_return_t, fvar >(); - expect_match_return_t >, - std::vector > >(); -} diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp index fb319be0e87..4dea05b4872 100644 --- a/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp +++ b/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp @@ -1,19 +1,22 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP -#include #include #include +#include +#include +#include +#include +#include template void expect_errors() { using stan::math::fvar; using stan::math::var; - expect_scalar_error >(); expect_scalar_error > >(); - expect_std_vectors_error >(); - expect_std_vectors_error > >(); + expect_std_vector_error >(); + expect_std_vector_error > >(); expect_matrix_error >(); expect_matrix_error > >(); expect_vector_error >(); diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp index 2c8f4385792..6cc26d088d3 100644 --- a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp +++ b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp @@ -14,7 +14,7 @@ static inline std::vector build_valid_fvar_vector(std::vector var_vector, int seed_index = -1) { std::vector inputs = F::valid_inputs(); - for (int i = 0; i < inputs.size(); ++i) { + for (size_t i = 0; i < inputs.size(); ++i) { var_vector.push_back(inputs[i]); } return var_vector; @@ -33,10 +33,9 @@ build_valid_fvar_vector(std::vector > fvar_vector, if (seed_index != -1) d_vector = build_valid_fvar_vector(vector(), seed_index); - for (int i = 0; i < val_vector.size(); ++i) { - if (seed_index == i) - fvar_vector.push_back( - fvar(val_vector[i], d_vector[i])); + for (size_t i = 0; i < val_vector.size(); ++i) { + if (seed_index == static_cast(i)) + fvar_vector.push_back(fvar(val_vector[i], d_vector[i])); else fvar_vector.push_back(fvar(val_vector[i])); } @@ -93,18 +92,14 @@ void expect_scalar_value() { using stan::math::fvar; using stan::test::expect_match_return_t; using std::vector; - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - vector z = - build_valid_fvar_vector(vector(), i); + vector y = build_valid_fvar_vector(vector(), i); + vector z = build_valid_fvar_vector(vector(), i); V fz = F::template apply(z[i]); test_fvar(F::apply_base(y[i]), y[i], fz, z[i]); } - expect_match_return_t(); - expect_match_return_t, - std::vector >(); + expect_match_return_t(); + expect_match_return_t, std::vector >(); } template @@ -115,20 +110,16 @@ void expect_std_vector_values() { size_t num_inputs = F::valid_inputs().size(); for (size_t i = 0; i < num_inputs; ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - vector z = - build_valid_fvar_vector(vector(), i); + vector y = build_valid_fvar_vector(vector(), i); + vector z = build_valid_fvar_vector(vector(), i); vector fz = F::template apply >(z); EXPECT_EQ(z.size(), fz.size()); test_fvar(F::apply_base(y[i]), y[i], fz[i], z[i]); } size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { - vector > a; vector > b; for (size_t k = 0; k < num_inputs; ++k) { @@ -171,19 +162,19 @@ void expect_matrix_values() { } size_t vector_matrix_size = 2; - for (int i = 0; i < vector_matrix_size; ++i) { + for (size_t i = 0; i < vector_matrix_size; ++i) { for (int j = 0; j < template_matrix.size(); ++j) { vector a; vector b; - for (int k = 0; k < vector_matrix_size; ++k) + for (size_t k = 0; k < vector_matrix_size; ++k) { if (k == i) { a.push_back(build_valid_fvar_matrix(template_matrix, j)); b.push_back(build_valid_fvar_matrix(template_matrix, j)); - } - else { + } else { a.push_back(build_valid_fvar_matrix(template_matrix)); b.push_back(build_valid_fvar_matrix(template_matrix)); } + } vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); diff --git a/test/unit/math/prim/mat/vectorize/README.txt b/test/unit/math/prim/mat/vectorize/README.txt new file mode 100644 index 00000000000..4edf27ae2f2 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/README.txt @@ -0,0 +1,29 @@ +Instructions for implementing and testing a vectorized function +---------------------------------------------------------------- + +* Implement the function itself with vectorization in a single file. + + For an example, see: test/unit/math/prim/mat/vectorize/foo_fun.hpp + + * Define a struct definining the function for scalars which works + for all of the scalar types (prim, rev, fwd, and mix); + see struct foo_fun in foo_fun.hpp + + * Define the actual function in terms of the struct. + see definition of foo() in foo_fun.hpp + +* For testing, we need to define a test class that implements + the function independently and supplies sequences of legal and + illegal double and int inputs. + + For an example, see the definition of struct foo_base_test in + test/unit/math/prim/mat/vectorize/foo_base_test.hpp + + * Need all of the functions to be defined + + * T appply(const T&); + + * double apply_base(int) + +*** WARNING *** +The tests presuppose that all of the scalar possibilities ***have already been tested***. These tests only test that nothing gets messed up by the vectorization. diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index bdaae517703..72753c35eee 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,27 +1,11 @@ -#include +#include #include #include #include -#include -#include -// this tests that the expect_values test works on a mock function +// test that returned values are correct and that errors are thrown +// for illegal inputs for foo TEST(MathPrimMatVectorize, applyScalarUnaryMock) { expect_values(); - if (foo_base_test::illegal_inputs().size() > 0) - expect_errors(); -} - -// this tests that the return types work -template -void expect_scalar_unary_return_type() { - using stan::math::var; - using stan::math::foo; - using stan::test::expect_match_return_t; - var three_var = 3; - var exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); - - expect_match_return_t(); - expect_match_return_t, std::vector >(); + expect_errors(); } diff --git a/test/unit/math/prim/mat/vectorize/expect_errors.hpp b/test/unit/math/prim/mat/vectorize/expect_errors.hpp deleted file mode 100644 index 25b0c92a9ad..00000000000 --- a/test/unit/math/prim/mat/vectorize/expect_errors.hpp +++ /dev/null @@ -1,144 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP - -#include -#include -#include - -template -void expect_int_scalar_error() { - using std::vector; - - vector int_illegal_inputs = F::int_illegal_inputs(); - - for (size_t i = 0; i < int_illegal_inputs.size(); ++i) { - int input = int_illegal_inputs[i]; - EXPECT_THROW(F::template apply(input), std::domain_error); - EXPECT_THROW(F::apply_base(input), std::domain_error); - } -} - - -template -void expect_scalar_error() { - using std::vector; - - vector illegal_inputs = F::illegal_inputs(); - - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - V input = illegal_inputs[i]; - EXPECT_THROW(F::apply_base(input), std::domain_error); - EXPECT_THROW(F::template apply(input), std::domain_error); - } -} - -template -void expect_int_std_vectors_error() { - using std::vector; - using stan::math::fvar; - - vector illegal_inputs = F::int_illegal_inputs(); - - EXPECT_THROW(F::template apply >(illegal_inputs), - std::domain_error); - - vector > z; - z.push_back(illegal_inputs); - z.push_back(illegal_inputs); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); -} - -template -void expect_std_vectors_error() { - using std::vector; - using stan::math::fvar; - - vector illegal_inputs = F::illegal_inputs(); - vector y; - - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - EXPECT_THROW(F::template apply(illegal_inputs[i]), - std::domain_error); - y.push_back(illegal_inputs[i]); - } - - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); -} - -template -void expect_matrix_error() { - using std::vector; - using stan::math::fvar; - typedef - Eigen::Matrix MatrixXvar; - - vector illegal_inputs = F::illegal_inputs(); - size_t num_rows = 3; - MatrixXvar a(num_rows, illegal_inputs.size()); - - for (size_t i = 0; i < num_rows; ++i) { - for (size_t j = 0; j < illegal_inputs.size(); ++j) - a(i, j) = illegal_inputs[j]; - } - - EXPECT_THROW(F::template apply(a), std::domain_error); - - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix VectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - VectorXvar b = VectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - b(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -template -void expect_row_vector_error() { - using std::vector; - using stan::math::fvar; - typedef Eigen::Matrix RowVectorXvar; - - std::vector illegal_inputs = F::illegal_inputs(); - - RowVectorXvar c = RowVectorXvar(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - c(i) = illegal_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); -} - -#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp new file mode 100644 index 00000000000..f6fb754e22b --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp @@ -0,0 +1,18 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_INT_SCALAR_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_INT_SCALAR_ERROR_HPP + +#include +#include +#include + +template +void expect_int_scalar_error() { + using std::vector; + vector int_illegal_inputs = F::int_illegal_inputs(); + for (size_t i = 0; i < int_illegal_inputs.size(); ++i) { + int input = int_illegal_inputs[i]; + EXPECT_THROW(F::template apply(input), std::domain_error); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp new file mode 100644 index 00000000000..5adfbdc3e1f --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp @@ -0,0 +1,22 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_INT_STD_VECTOR_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_INT_STD_VECTOR_ERROR_HPP + +#include +#include +#include + +template +void expect_int_std_vector_error() { + using std::vector; + vector illegal_inputs = F::int_illegal_inputs(); + EXPECT_THROW(F::template apply >(illegal_inputs), + std::domain_error); + + vector > z; + z.push_back(illegal_inputs); + z.push_back(illegal_inputs); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp index f91ddb3fb70..24a960f1e55 100644 --- a/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_match_return_t.hpp @@ -2,31 +2,30 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATCH_RETURN_T_HPP #include -#include -#include -#include -#include +#include #include namespace stan { + namespace test { - template + + /** + * Tests that the return type of a vectorized function with a + * specified argument type is a specified type. + * + * @tparam F Functor defining function to test. + * @tparam T_result_expected Expected result type. + * @tparam T_arg Argument type. + */ + template void expect_match_return_t() { - using stan::math::foo_fun; using stan::math::apply_scalar_unary; - using boost::is_convertible; - - typedef typename apply_scalar_unary::return_t result_t; - - bool T1_to_T2 - = is_convertible::value; - EXPECT_TRUE(T1_to_T2); - - bool T1_from_T2 - = is_convertible::value; - EXPECT_TRUE(T1_from_T2); + typedef typename apply_scalar_unary::return_t result_t; + EXPECT_TRUE((boost::is_same::value)); } } } #endif + + diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp new file mode 100644 index 00000000000..8c045bd1d9a --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp @@ -0,0 +1,29 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_ERROR_HPP + +#include +#include +#include +#include + +template +void expect_matrix_error() { + using std::vector; + typedef Eigen::Matrix matrix_t; + vector illegal_inputs = F::illegal_inputs(); + matrix_t a(3, illegal_inputs.size()); + for (int i = 0; i < a.rows(); ++i) + for (int j = 0; j < a.cols(); ++j) + a(i, j) = illegal_inputs[j]; + EXPECT_THROW(F::template apply(a), std::domain_error); + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp new file mode 100644 index 00000000000..9cbfa221459 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp @@ -0,0 +1,27 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_ERROR_HPP + +#include +#include +#include + +template +void expect_row_vector_error() { + using std::vector; + typedef Eigen::Matrix row_vector_t; + + std::vector illegal_inputs = F::illegal_inputs(); + + row_vector_t c = row_vector_t(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + c(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp b/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp new file mode 100644 index 00000000000..2b636308c6e --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp @@ -0,0 +1,18 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_ERROR_HPP + +#include +#include +#include + +template +void expect_scalar_error() { + using std::vector; + vector illegal_inputs = F::illegal_inputs(); + for (size_t i = 0; i < illegal_inputs.size(); ++i) { + T input = illegal_inputs[i]; + EXPECT_THROW(F::template apply(input), std::domain_error); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp new file mode 100644 index 00000000000..127293c8fb3 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp @@ -0,0 +1,22 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_ERROR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_ERROR_HPP + +#include +#include +#include + +template +void expect_std_vector_error() { + using std::vector; + vector illegal_inputs = F::illegal_inputs(); + vector y(illegal_inputs.begin(), illegal_inputs.end()); + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp index 8f8cc8fbdef..73d5340d0f9 100644 --- a/test/unit/math/prim/mat/vectorize/expect_values.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_values.hpp @@ -7,28 +7,19 @@ #include #include -// TODO(carpenter): figure out some way to deal with constraints (transforms) -// including testing out of support values -// ?? wrap this all up in a general transform framework - - - template void expect_scalar_value() { using std::vector; - vector valid_inputs = F::valid_inputs(); vector int_valid_inputs = F::int_valid_inputs(); - for (size_t i = 0; i < int_valid_inputs.size(); ++i) { int input = int_valid_inputs[i]; double v = F::template apply(input); EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); } - for (size_t i = 0; i < valid_inputs.size(); ++i) { EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), - F::template apply(valid_inputs[i])); + F::template apply(valid_inputs[i])); } } @@ -47,8 +38,8 @@ void expect_std_vectors_value() { vector > z; z.push_back(valid_inputs); z.push_back(valid_inputs); - vector > fz = - F::template apply > >(z); + vector > fz + = F::template apply > >(z); EXPECT_EQ(z.size(), fz.size()); for (size_t i = 0; i < fz.size(); ++i) { EXPECT_EQ(z[i].size(), fz[i].size()); diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp new file mode 100644 index 00000000000..5a77b2fe568 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp @@ -0,0 +1,27 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_HPP + +#include +#include +#include +#include + +template +void expect_vector_error() { + using std::vector; + typedef Eigen::Matrix vector_t; + vector illegal_inputs = F::illegal_inputs(); + + vector_t b = vector_t(illegal_inputs.size()); + for (size_t i = 0; i < illegal_inputs.size(); ++i) + b(i) = illegal_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp index 5faca17ba2f..b3b4d471a69 100644 --- a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp @@ -1,73 +1,92 @@ #ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP -#include -#include -#include #include +#include +/** + * This is the structure for testing mock function foo (defined in the + * testing framework). See README.txt for more instructions. + */ struct foo_base_test { + /** + * Redefinition of function brought in from stan::math. The reason + * to do this is that it wraps it up in this static template class. + * + * This is the version that's being tested. + * + * WARNING: assumes that the scalar values for all instantiations + * (prim, rev, fwd, mix) ***have already been tested***. + * + * @tparam R Return type. + * @tparam T Argument type. + */ template static R apply(const T& x) { using stan::math::foo; return foo(x); } - static double apply_base(const int x) { + /** + * This defines the truth against which we're testing. + * + * Because this is *not an independent test*, this function just + * delegates to the actual function defined in stan::math. + * + * Redundant definition of function from stan::math to apply to an + * integer and return a double. + * + * This function delegates to apply(), defined above, directly. + * + * WARNING: this is *not an independent test*. + */ + static double apply_base(int x) { return apply(x); } + /** + * This is the generic version of the integer version defined + * above. For every other type, the return type is the same as the + * reference type. + * + * WARNING: this is *not an independent test of the underlying function*. + */ template static T apply_base(const T& x) { return apply(x); } + /** + * Return sequence of valid double-valued inputs. + */ static std::vector valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1.3); - valid_inputs.push_back(-2.6); - valid_inputs.push_back(0); - valid_inputs.push_back(-0.2); - - return valid_inputs; + return stan::math::vector_builder() + .add(1.3).add(-2.6).add(0).add(-0.2).build(); } + /** + * Return sequence of invalid double-valued inputs. + */ static std::vector illegal_inputs() { - using std::vector; - - vector illegal_inputs(2, 10.6); - illegal_inputs.push_back(25.7); - illegal_inputs.push_back(100.25); - - return illegal_inputs; + return stan::math::vector_builder() + .add(10.6).add(10.6).add(25.7).add(100.25).build(); } + /** + * Return sequence of valid integer inputs. + */ static std::vector int_valid_inputs() { - using std::vector; - - vector valid_inputs; - valid_inputs.push_back(1); - valid_inputs.push_back(-2); - valid_inputs.push_back(0); - valid_inputs.push_back(3); - - return valid_inputs; + return stan::math::vector_builder() + .add(1).add(-2).add(0).add(3).build(); } + /** + * Return sequence of invalid integer inputs. + */ static std::vector int_illegal_inputs() { - using std::vector; - - vector illegal_inputs; - illegal_inputs.push_back(10); - illegal_inputs.push_back(25); - illegal_inputs.push_back(100); - illegal_inputs.push_back(50); - - return illegal_inputs; + return stan::math::vector_builder() + .add(10).add(25).add(100).add(50).build(); } - }; #endif diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp index 42ee4b81147..24e1e13671b 100644 --- a/test/unit/math/prim/mat/vectorize/foo_fun.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -1,21 +1,22 @@ #ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP -#include #include -#include -#include +#include namespace stan { namespace math { - - // mock class static function definition for exp() + + /** + * Example of how to define a functor for a vectorized function. + * The example includes a constrained version of exp(). + */ struct foo_fun { template static inline T fun(const T& x) { using std::exp; - stan::math::check_less_or_equal("vectorize", "x", x, 5); + stan::math::check_less_or_equal("foo_fun vectorize", "x", x, 5); return exp(x); } }; diff --git a/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp b/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp index 5e1df9d5eb9..362d13fbc13 100644 --- a/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp +++ b/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp @@ -1,22 +1,32 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#include -#include +#include +#include +#include +#include +#include +#include +#include #include -#include -#include -#include -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example +/** + * Tests that the function defined statically in the template class + * throws errors when supplied with illegal values. The illegal + * values are themselves defined statically in the template class. + * + * Tests integer, scalar, standard vector, Eigen matrix, Eigen vector, + * and Eigen row vecor cases, and nested standard vectors of Eigen + * types. + * + * @tparam F Test class used to define test case. + */ template void expect_errors() { expect_int_scalar_error(); - expect_int_std_vectors_error(); - + expect_int_std_vector_error(); expect_scalar_error(); - expect_std_vectors_error(); + expect_std_vector_error(); expect_matrix_error(); expect_vector_error(); expect_row_vector_error(); diff --git a/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp b/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp deleted file mode 100644 index 17b5702581e..00000000000 --- a/test/unit/math/prim/mat/vectorize/prim_expect_values.hpp +++ /dev/null @@ -1,169 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include - -// TODO(carpenter): figure out some way to deal with constraints (transforms) -// including testing out of support values -// ?? wrap this all up in a general transform framework - - - -template -void expect_scalar_value() { - using std::vector; - - vector valid_inputs = F::valid_inputs(); - vector int_valid_inputs = F::int_valid_inputs(); - - for (size_t i = 0; i < int_valid_inputs.size(); ++i) { - int input = int_valid_inputs[i]; - double v = F::template apply(input); - EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); - } - - for (size_t i = 0; i < valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), - F::template apply(valid_inputs[i])); - } -} - -template -void expect_std_vectors_value() { - using std::vector; - - vector valid_inputs = F::valid_inputs(); - - vector fy = F::template apply >(valid_inputs); - EXPECT_EQ(valid_inputs.size(), fy.size()); - for (size_t i = 0; i < valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), fy[i]); - } - - vector > z; - z.push_back(valid_inputs); - z.push_back(valid_inputs); - vector > fz = - F::template apply > >(z); - EXPECT_EQ(z.size(), fz.size()); - for (size_t i = 0; i < fz.size(); ++i) { - EXPECT_EQ(z[i].size(), fz[i].size()); - for (size_t j = 0; j < z[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); - } - - vector int_valid_inputs = F::int_valid_inputs(); - vector u2 = F::template apply >(int_valid_inputs); - EXPECT_EQ(int_valid_inputs.size(), u2.size()); - for (size_t i = 0; i < int_valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(int_valid_inputs[i]), u2[i]); - } -} - -template -void expect_matrix_value() { - using Eigen::MatrixXd; - using Eigen::RowVectorXd; - using std::vector; - - vector valid_inputs = F::valid_inputs(); - RowVectorXd eigen_valid_inputs = - RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); - size_t num_rows = 3; - MatrixXd a(num_rows, valid_inputs.size()); - - for (size_t i = 0; i < num_rows; i++) { - a.row(i) << eigen_valid_inputs; - } - - MatrixXd fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - for (int i = 0; i < fa.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); - - MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(F::apply_base(a(1,1)), fab(0,0)); - - vector d; - d.push_back(a); - d.push_back(a); - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].rows(), fd[i].rows()); - EXPECT_EQ(d[i].cols(), fd[i].cols()); - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} - -template -void expect_vector_value() { - using Eigen::VectorXd; - using std::vector; - - std::vector valid_inputs = F::valid_inputs(); - - VectorXd b = VectorXd::Map(valid_inputs.data(), valid_inputs.size()); - VectorXd fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); - - vector d; - d.push_back(b); - d.push_back(b); - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} - -template -void expect_row_vector_value() { - using Eigen::RowVectorXd; - using std::vector; - - std::vector valid_inputs = F::valid_inputs(); - - RowVectorXd c = - RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); - - RowVectorXd fc = F::template apply(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); - - vector d; - d.push_back(c); - d.push_back(c); - - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_values() { - expect_scalar_value(); - expect_std_vectors_value(); - expect_matrix_value(); - expect_vector_value(); - expect_row_vector_value(); -} - -#endif diff --git a/test/unit/math/prim/mat/vectorize/vector_builder.hpp b/test/unit/math/prim/mat/vectorize/vector_builder.hpp new file mode 100644 index 00000000000..e58693ff1ce --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/vector_builder.hpp @@ -0,0 +1,47 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTOR_BUILD_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTOR_BUILD_HPP + +#include + +namespace stan { + namespace math { + + /** + * Builder pattern for standard vectors. + * + * @tparam T Type of values held by builder. + */ + template + struct vector_builder { + std::vector vec_; + + /** + * Construct an empty vector builder. + */ + vector_builder() { } + + /** + * Add the specified element to the underlying vector + * and return the builder for argument chaining. + * + * @param x Value to add. + * @return Reference to this builder. + */ + vector_builder& add(const T& x) { + vec_.push_back(x); + return *this; + } + + /** + * Return the underlying vector. + * + * @return Underlying vector. + */ + std::vector build() const { + return vec_; + } + }; + + } +} +#endif diff --git a/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp new file mode 100644 index 00000000000..caca03eec9c --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp @@ -0,0 +1,17 @@ +#include +#include +#include + + +TEST(mathVectorBuilder, test1) { + using stan::math::vector_builder; + vector_builder x; + + std::vector z = x.build(); + EXPECT_EQ(0, z.size()); + + std::vector v = x.add(1).add(2).build(); + EXPECT_EQ(2, v.size()); + EXPECT_FLOAT_EQ(1, v[0]); + EXPECT_FLOAT_EQ(2, v[1]); +} diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 5dd17e84ba6..0c62c160322 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,28 +1,17 @@ +#include +#include +#include +#include #include #include #include #include -#include -#include -#include -// this tests that the expect_values test works on a mock function +/** + * this tests that the expect_values test works on a mock function + */ TEST(MathRevMatVectorize, applyScalarUnaryMock) { expect_values(); - if (foo_base_test::illegal_inputs().size() > 0) - expect_errors(); + expect_errors(); } -// this tests that the return types work -template -void expect_scalar_unary_return_type() { - using stan::math::var; - using stan::math::foo; - using stan::test::expect_match_return_t; - var three_var = 3; - var exp_3_v = foo(three_var); - EXPECT_FLOAT_EQ(std::exp(3.0), exp_3_v.val()); - - expect_match_return_t(); - expect_match_return_t, std::vector >(); -} diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp index b4a4a1b7618..8704ace76dc 100644 --- a/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp +++ b/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp @@ -2,16 +2,20 @@ #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_ERRORS_HPP #include -#include +#include +#include +#include +#include +#include + // CALL THIS TO TEST EVERYTHING // see: apply_scalar_unary_test.cpp for an example template void expect_errors() { using stan::math::var; - expect_scalar_error(); - expect_std_vectors_error(); + expect_std_vector_error(); expect_matrix_error(); expect_vector_error(); expect_row_vector_error(); diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp index 79f4b200738..d9524d6e40f 100644 --- a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp +++ b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp @@ -1,8 +1,8 @@ #ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP -#include #include +#include #include template @@ -25,7 +25,7 @@ build_valid_var_vector() { std::vector inputs = F::valid_inputs(); std::vector var_vector; - for (int i = 0; i < inputs.size(); ++i) { + for (size_t i = 0; i < inputs.size(); ++i) { var_vector.push_back(inputs[i]); } @@ -111,7 +111,7 @@ void expect_matrix_value() { size_t num_inputs = F::valid_inputs().size(); MatrixXvar template_matrix(num_inputs, num_cols); - for (size_t i = 0; i < template_matrix.size(); ++i) { + for (int i = 0; i < template_matrix.size(); ++i) { MatrixXvar a = build_valid_var_matrix(template_matrix); MatrixXvar fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); @@ -123,7 +123,7 @@ void expect_matrix_value() { size_t vector_matrix_size = 2; for (size_t i = 0; i < vector_matrix_size; ++i) { - for (size_t j = 0; j < template_matrix.size(); ++j) { + for (int j = 0; j < template_matrix.size(); ++j) { vector b; for (size_t k = 0; k < vector_matrix_size; ++k) From 52b5cfd875b1ca5c6ab3b79085bad072320c35ca Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Thu, 18 Feb 2016 16:37:10 -0500 Subject: [PATCH 18/36] Refactored expect_values by splitting it into separate files --- .../mat/vectorize/apply_scalar_unary_test.cpp | 5 +- .../math/fwd/mat/vectorize/build_matrix.hpp | 30 ++ .../math/fwd/mat/vectorize/build_vector.hpp | 34 ++ ...wd_expect_errors.hpp => expect_errors.hpp} | 4 +- .../math/fwd/mat/vectorize/expect_fvar_eq.hpp | 17 + .../fwd/mat/vectorize/expect_matrix_value.hpp | 51 +++ .../mat/vectorize/expect_row_vector_value.hpp | 45 +++ .../fwd/mat/vectorize/expect_scalar_value.hpp | 26 ++ .../mat/vectorize/expect_std_vector_value.hpp | 42 +++ .../math/fwd/mat/vectorize/expect_values.hpp | 27 ++ .../fwd/mat/vectorize/expect_vector_value.hpp | 44 +++ .../fwd/mat/vectorize/fwd_expect_values.hpp | 241 -------------- .../mat/vectorize/apply_scalar_unary_test.cpp | 5 +- .../math/mix/mat/vectorize/build_matrix.hpp | 31 ++ .../math/mix/mat/vectorize/build_vector.hpp | 41 +++ ...ix_expect_errors.hpp => expect_errors.hpp} | 4 +- .../mix/mat/vectorize/expect_fvar_var_eq.hpp | 32 ++ .../mix/mat/vectorize/expect_matrix_value.hpp | 60 ++++ .../mat/vectorize/expect_row_vector_value.hpp | 50 +++ .../mix/mat/vectorize/expect_scalar_value.hpp | 23 ++ .../mat/vectorize/expect_std_vector_value.hpp | 48 +++ .../math/mix/mat/vectorize/expect_values.hpp | 30 ++ .../mix/mat/vectorize/expect_vector_value.hpp | 49 +++ .../mix/mat/vectorize/mix_expect_values.hpp | 294 ------------------ .../mat/vectorize/apply_scalar_unary_test.cpp | 2 +- ...im_expect_errors.hpp => expect_errors.hpp} | 0 .../mat/vectorize/expect_matrix_value.hpp | 42 +++ .../mat/vectorize/expect_row_vector_value.hpp | 36 +++ .../mat/vectorize/expect_scalar_value.hpp | 23 ++ .../mat/vectorize/expect_std_vector_value.hpp | 39 +++ .../math/prim/mat/vectorize/expect_values.hpp | 150 +-------- .../mat/vectorize/expect_vector_value.hpp | 32 ++ test/unit/math/prim/mat/vectorize/foo_fun.hpp | 1 + .../mat/vectorize/apply_scalar_unary_test.cpp | 6 +- .../math/rev/mat/vectorize/build_matrix.hpp | 21 ++ .../math/rev/mat/vectorize/build_vector.hpp | 16 + .../rev/mat/vectorize/expect_autodiff.hpp | 16 + ...ev_expect_errors.hpp => expect_errors.hpp} | 4 +- .../rev/mat/vectorize/expect_matrix_value.hpp | 57 ++++ .../mat/vectorize/expect_row_vector_value.hpp | 50 +++ .../rev/mat/vectorize/expect_scalar_value.hpp | 26 ++ .../mat/vectorize/expect_std_vector_value.hpp | 48 +++ .../math/rev/mat/vectorize/expect_values.hpp | 20 ++ .../rev/mat/vectorize/expect_vector_value.hpp | 49 +++ .../rev/mat/vectorize/rev_expect_values.hpp | 237 -------------- 45 files changed, 1175 insertions(+), 933 deletions(-) create mode 100644 test/unit/math/fwd/mat/vectorize/build_matrix.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/build_vector.hpp rename test/unit/math/fwd/mat/vectorize/{fwd_expect_errors.hpp => expect_errors.hpp} (88%) create mode 100644 test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp delete mode 100644 test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp create mode 100644 test/unit/math/mix/mat/vectorize/build_matrix.hpp create mode 100644 test/unit/math/mix/mat/vectorize/build_vector.hpp rename test/unit/math/mix/mat/vectorize/{mix_expect_errors.hpp => expect_errors.hpp} (88%) create mode 100644 test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_vector_value.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/mix_expect_values.hpp rename test/unit/math/prim/mat/vectorize/{prim_expect_errors.hpp => expect_errors.hpp} (100%) create mode 100644 test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp create mode 100644 test/unit/math/prim/mat/vectorize/expect_vector_value.hpp create mode 100644 test/unit/math/rev/mat/vectorize/build_matrix.hpp create mode 100644 test/unit/math/rev/mat/vectorize/build_vector.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_autodiff.hpp rename test/unit/math/rev/mat/vectorize/{rev_expect_errors.hpp => expect_errors.hpp} (85%) create mode 100644 test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_values.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_vector_value.hpp delete mode 100644 test/unit/math/rev/mat/vectorize/rev_expect_values.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index 8c653ec9bd3..ddcac6a8ce6 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,8 +1,7 @@ -#include #include #include -#include -#include +#include +#include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp new file mode 100644 index 00000000000..f4647d0799b --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp @@ -0,0 +1,30 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_MATRIX_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_MATRIX_HPP + +#include +#include +#include + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/build_vector.hpp b/test/unit/math/fwd/mat/vectorize/build_vector.hpp new file mode 100644 index 00000000000..442ca00644b --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/build_vector.hpp @@ -0,0 +1,34 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_TECTORIZE_BUILD_TECTOR_HPP +#define TEST_UNIT_MATH_FWD_MAT_TECTORIZE_BUILD_TECTOR_HPP + +#include +#include + +template +static inline std::vector +build_vector(std::vector double_vector, + int seed_index = -1) { + return F::valid_inputs(); +} + +template +static inline std::vector > +build_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector template_vector + = build_vector(vector(), seed_index); + + for (size_t i = 0; i < template_vector.size(); ++i) { + if (seed_index == static_cast(i)) + fvar_vector + .push_back(fvar(template_vector[i], template_vector[i])); + else + fvar_vector.push_back(fvar(template_vector[i])); + } + return fvar_vector; +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp similarity index 88% rename from test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp rename to test/unit/math/fwd/mat/vectorize/expect_errors.hpp index b5decfe7a8c..ba08c519744 100644 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_errors.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ERRORS_HPP #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp b/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp new file mode 100644 index 00000000000..5dec8295d82 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp @@ -0,0 +1,17 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FVAR_EQ_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FVAR_EQ_HPP + +#include +#include + +static inline void expect_fvar_eq(double exp_var, double test_var) { + EXPECT_FLOAT_EQ(exp_var, test_var); +} + +template +static inline void expect_fvar_eq(T exp_var, T test_var) { + expect_fvar_eq(exp_var.val(), test_var.val()); + expect_fvar_eq(exp_var.d_, test_var.d_); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp new file mode 100644 index 00000000000..54a66cf9298 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp @@ -0,0 +1,51 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP + +#include +#include +#include +#include +#include + +template +void expect_matrix_value() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix matrix_t; + + int num_inputs = F::valid_inputs().size(); + int num_cols = 3; + matrix_t template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + matrix_t a = build_matrix(template_matrix, i); + matrix_t fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + expect_fvar_eq(F::apply_base(a(i)), fa(i)); + } + + size_t vector_matrix_size = 2; + for (size_t i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) + if (k == i) + b.push_back(build_matrix(template_matrix, j)); + else + b.push_back(build_matrix(template_matrix)); + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + expect_fvar_eq(F::apply_base(b[i](j)), fb[i](j)); + } + } + + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + matrix_t a = build_matrix(template_matrix, seed_i); + matrix_t fab = foo(a.block(block_i, block_j, 1, 1)); + expect_fvar_eq(F::apply_base(a(1,1)), fab(0,0)); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp new file mode 100644 index 00000000000..865fea16217 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp @@ -0,0 +1,45 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP + +#include +#include +#include +#include +#include + +template +void expect_row_vector_value() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix row_vector_t; + + size_t num_inputs = F::valid_inputs().size(); + row_vector_t template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + row_vector_t b = build_matrix(template_vector, i); + row_vector_t fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + expect_fvar_eq(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_matrix(template_vector, j)); + else + c.push_back(build_matrix(template_vector)); + vector fc = + F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + expect_fvar_eq(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp new file mode 100644 index 00000000000..4c4e9868789 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp @@ -0,0 +1,26 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP + +#include +#include +#include +#include +#include + +template +void expect_scalar_value() { + using stan::test::expect_match_return_t; + using std::vector; + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = + build_vector(vector(), i); + T fy = F::template apply(y[i]); + T exp_y = F::apply_base(y[i]); + expect_fvar_eq(exp_y, fy); + } + expect_match_return_t(); + expect_match_return_t, std::vector >(); + +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp new file mode 100644 index 00000000000..c8f27b5fb4c --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp @@ -0,0 +1,42 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP + +#include +#include +#include +#include + +template +void expect_std_vector_value() { + using std::vector; + size_t num_inputs = F::valid_inputs().size(); + for (size_t i = 0; i < num_inputs; ++i) { + vector y = build_vector(vector(), i); + vector fy = F::template apply >(y); + EXPECT_EQ(y.size(), fy.size()); + expect_fvar_eq(F::apply_base(y[i]), fy[i]); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + + vector > z; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) + z.push_back(build_vector(vector(), j)); + else + z.push_back(build_vector(vector())); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + expect_fvar_eq(F::apply_base(z[i][j]), fz[i][j]); + } + } +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..57a44aeb1a9 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_values.hpp @@ -0,0 +1,27 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include + +template +void expect_values() { + using stan::math::fvar; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_value >(); + expect_std_vector_value > >(); + expect_matrix_value >(); + expect_matrix_value > >(); + expect_vector_value >(); + expect_vector_value > >(); + expect_row_vector_value >(); + expect_row_vector_value > >(); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp new file mode 100644 index 00000000000..90ff56ed7df --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp @@ -0,0 +1,44 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP + +#include +#include +#include +#include +#include + +template +void expect_vector_value() { + using stan::math::fvar; + using std::vector; + typedef Eigen::Matrix vector_t; + + size_t num_inputs = F::valid_inputs().size(); + vector_t template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + vector_t b = build_matrix(template_vector, i); + vector_t fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + expect_fvar_eq(F::apply_base(b(i)), fb(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) + c.push_back(build_matrix(template_vector, j)); + else + c.push_back(build_matrix(template_vector)); + vector fc = F::template apply >(c); + + EXPECT_EQ(c.size(), fc.size()); + EXPECT_EQ(c[i].size(), fc[i].size()); + expect_fvar_eq(F::apply_base(c[i](j)), fc[i](j)); + } + } +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp b/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp deleted file mode 100644 index 3e14863db88..00000000000 --- a/test/unit/math/fwd/mat/vectorize/fwd_expect_values.hpp +++ /dev/null @@ -1,241 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_EXPECT_VALUES_HPP - -#include -#include -#include - -template -static inline std::vector -build_valid_fvar_vector(std::vector double_vector, - int seed_index = -1) { - return F::valid_inputs(); -} - -template -static inline std::vector > -build_valid_fvar_vector(std::vector > fvar_vector, - int seed_index = -1) { - using std::vector; - using stan::math::fvar; - - vector template_vector - = build_valid_fvar_vector(vector(), seed_index); - - for (size_t i = 0; i < template_vector.size(); ++i) { - if (seed_index == static_cast(i)) - fvar_vector.push_back(fvar(template_vector[i], template_vector[i])); - else - fvar_vector.push_back(fvar(template_vector[i])); - } - return fvar_vector; -} - -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_valid_fvar_matrix(const T& x, int seed_index = -1) { - - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); - size_t num_inputs = F::valid_inputs().size(); - for (int i = 0; i < x.size(); ++i) { - std::vector inputs; - if (seed_index == i) - inputs = build_valid_fvar_vector(std::vector(), - (seed_index % num_inputs)); - else - inputs = build_valid_fvar_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; - } - return fvar_matrix; -} - -static inline void test_fvar(double exp_var, double test_var) { - EXPECT_FLOAT_EQ(exp_var, test_var); -} - -template -static inline void test_fvar(V exp_var, V test_var) { - test_fvar(exp_var.val(), test_var.val()); - test_fvar(exp_var.d_, test_var.d_); -} - -template -void expect_scalar_value() { - using stan::test::expect_match_return_t; - using std::vector; - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = - build_valid_fvar_vector(vector(), i); - V fy = F::template apply(y[i]); - V exp_y = F::apply_base(y[i]); - test_fvar(exp_y, fy); - } - expect_match_return_t(); - expect_match_return_t, std::vector >(); - -} - -template -void expect_std_vector_values() { - using std::vector; - size_t num_inputs = F::valid_inputs().size(); - for (size_t i = 0; i < num_inputs; ++i) { - vector y = build_valid_fvar_vector(vector(), i); - vector fy = F::template apply >(y); - EXPECT_EQ(y.size(), fy.size()); - test_fvar(F::apply_base(y[i]), fy[i]); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - - vector > z; - for (size_t k = 0; k < num_inputs; ++k) { - if (i == k) - z.push_back(build_valid_fvar_vector(vector(), j)); - else - z.push_back(build_valid_fvar_vector(vector())); - } - vector > fz = - F::template apply > >(z); - - EXPECT_EQ(z.size(), fz.size()); - EXPECT_EQ(z[i].size(), fz[i].size()); - test_fvar(F::apply_base(z[i][j]), fz[i][j]); - } - } -} - -template -void expect_matrix_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix matrix_t; - - int num_inputs = F::valid_inputs().size(); - int num_cols = 3; - matrix_t template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - matrix_t a = build_valid_fvar_matrix(template_matrix, i); - matrix_t fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - test_fvar(F::apply_base(a(i)), fa(i)); - } - - size_t vector_matrix_size = 2; - for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - vector b; - for (size_t k = 0; k < vector_matrix_size; ++k) - if (k == i) - b.push_back(build_valid_fvar_matrix(template_matrix, j)); - else - b.push_back(build_valid_fvar_matrix(template_matrix)); - vector fb = F::template apply >(b); - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(b[i](j)), fb[i](j)); - } - } - - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - matrix_t a = build_valid_fvar_matrix(template_matrix, seed_i); - matrix_t fab = foo(a.block(block_i, block_j, 1, 1)); - test_fvar(F::apply_base(a(1,1)), fab(0,0)); -} - -template -void expect_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar b = build_valid_fvar_matrix(template_vector, i); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(b(i)), fb(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) - c.push_back(build_valid_fvar_matrix(template_vector, j)); - else - c.push_back(build_valid_fvar_matrix(template_vector)); - vector fc = F::template apply >(c); - - EXPECT_EQ(c.size(), fc.size()); - EXPECT_EQ(c[i].size(), fc[i].size()); - test_fvar(F::apply_base(c[i](j)), fc[i](j)); - } - } -} - -template -void expect_row_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(b(i)), fb(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) - c.push_back(build_valid_fvar_matrix(template_vector, j)); - else - c.push_back(build_valid_fvar_matrix(template_vector)); - vector fc = - F::template apply >(c); - - EXPECT_EQ(c.size(), fc.size()); - EXPECT_EQ(c[i].size(), fc[i].size()); - test_fvar(F::apply_base(c[i](j)), fc[i](j)); - } - } -} - -template -void expect_values() { - using stan::math::fvar; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_values >(); - expect_std_vector_values > >(); - expect_matrix_values >(); - expect_matrix_values > >(); - expect_vector_values >(); - expect_vector_values > >(); - expect_row_vector_values >(); - expect_row_vector_values > >(); -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp index f050decae32..7a7693bed8b 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -4,10 +4,9 @@ #include #include #include -#include -#include +#include +#include #include -#include TEST(MathFwdMatVectorize, applyScalarUnaryMock) { expect_values(); diff --git a/test/unit/math/mix/mat/vectorize/build_matrix.hpp b/test/unit/math/mix/mat/vectorize/build_matrix.hpp new file mode 100644 index 00000000000..00d7f2bd041 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/build_matrix.hpp @@ -0,0 +1,31 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MATRIX_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MATRIX_HPP + +#include +#include +#include +#include + +template +static inline Eigen::Matrix::Scalar, + T::RowsAtCompileTime, + T::ColsAtCompileTime> +build_matrix(const T& x, int seed_index = -1) { + + typedef typename Eigen::internal::traits::Scalar fvar_type; + Eigen::Matrix + fvar_matrix(x.rows(), x.cols()); + size_t num_inputs = F::valid_inputs().size(); + for (int i = 0; i < x.size(); ++i) { + std::vector inputs; + if (seed_index == i) + inputs = build_vector(std::vector(), + (seed_index % num_inputs)); + else + inputs = build_vector(std::vector()); + fvar_matrix(i) = inputs[(i % num_inputs)]; + } + return fvar_matrix; +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/build_vector.hpp b/test/unit/math/mix/mat/vectorize/build_vector.hpp new file mode 100644 index 00000000000..bc9e8eeff32 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/build_vector.hpp @@ -0,0 +1,41 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_VECTOR_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_VECTOR_HPP + +#include +#include +#include + +template +static inline std::vector +build_vector(std::vector fvar_vector, int seed_index) { + using std::vector; + using stan::math::var; + vector inputs = F::valid_inputs(); + for (size_t i = 0; i < inputs.size(); ++i) + fvar_vector.push_back(inputs[i]); + return fvar_vector; +} + +template +static inline std::vector > +build_vector(std::vector > fvar_vector, + int seed_index = -1) { + using std::vector; + using stan::math::fvar; + + vector val_vector = + build_vector(vector(), seed_index); + vector d_vector; + if (seed_index != -1) + d_vector = build_vector(vector(), seed_index); + + for (size_t i = 0; i < val_vector.size(); ++i) { + if (seed_index == static_cast(i)) + fvar_vector.push_back(fvar(val_vector[i], d_vector[i])); + else + fvar_vector.push_back(fvar(val_vector[i])); + } + return fvar_vector; +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp b/test/unit/math/mix/mat/vectorize/expect_errors.hpp similarity index 88% rename from test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp rename to test/unit/math/mix/mat/vectorize/expect_errors.hpp index 4dea05b4872..bced5eb757d 100644 --- a/test/unit/math/mix/mat/vectorize/mix_expect_errors.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ERRORS_HPP #include #include diff --git a/test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp b/test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp new file mode 100644 index 00000000000..f3ed9751336 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp @@ -0,0 +1,32 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_FVAR_VAR_EQ_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_FVAR_VAR_EQ_HPP + +#include +#include +#include +#include + +static inline void expect_fvar_var_eq(stan::math::var exp_var, + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { + EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); + AVEC exp_y = createAVEC(base_exp_var); + VEC exp_g; + exp_var.grad(exp_y, exp_g); + AVEC test_y = createAVEC(base_test_var); + VEC test_g; + test_var.grad(test_y, test_g); + EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); +} + +template +static inline void expect_fvar_var_eq(V exp_var, V base_exp_var, + V test_var, V base_test_var) { + expect_fvar_var_eq(exp_var.val(), base_exp_var.val(), + test_var.val(), base_test_var.val()); + expect_fvar_var_eq(exp_var.d_, base_exp_var.d_, + test_var.d_, base_test_var.d_); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp new file mode 100644 index 00000000000..5877d8bd40f --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp @@ -0,0 +1,60 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP + +#include +#include +#include +#include + +template +void expect_matrix_value() { + using std::vector; + typedef + Eigen::Matrix matrix_t; + + size_t num_inputs = F::valid_inputs().size(); + size_t num_cols = 3; + matrix_t template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + matrix_t y = build_matrix(template_matrix, i); + matrix_t z = build_matrix(template_matrix, i); + matrix_t fz = F::template apply(z); + EXPECT_EQ(z.size(), fz.size()); + expect_fvar_var_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); + } + + size_t vector_matrix_size = 2; + for (size_t i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + vector a; + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) { + if (k == i) { + a.push_back(build_matrix(template_matrix, j)); + b.push_back(build_matrix(template_matrix, j)); + } else { + a.push_back(build_matrix(template_matrix)); + b.push_back(build_matrix(template_matrix)); + } + } + vector fb = F::template apply >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + expect_fvar_var_eq( + F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); + } + } + + int block_i = 1; + int block_j = 1; + int seed_i = block_j * num_inputs + block_i; + matrix_t c = build_matrix(template_matrix, seed_i); + matrix_t d = build_matrix(template_matrix, seed_i); + matrix_t fab = foo(d.block(block_i, block_j, 1, 1)); + expect_fvar_var_eq( + F::apply_base(c(block_i, block_j)), c(block_i, block_j), + fab(0,0), d(block_i, block_j)); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp new file mode 100644 index 00000000000..d72777e01e2 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp @@ -0,0 +1,50 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP + +#include +#include +#include +#include + +template +void expect_row_vector_value() { + using std::vector; + typedef Eigen::Matrix row_vector_t; + + size_t num_inputs = F::valid_inputs().size(); + row_vector_t template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + row_vector_t a = build_matrix(template_vector, i); + row_vector_t b = build_matrix(template_vector, i); + row_vector_t fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + expect_fvar_var_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_matrix(template_vector, j)); + d.push_back(build_matrix(template_vector, j)); + } + else { + c.push_back(build_matrix(template_vector)); + d.push_back(build_matrix(template_vector)); + } + vector fd = + F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + expect_fvar_var_eq( + F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp new file mode 100644 index 00000000000..84e1927fae2 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp @@ -0,0 +1,23 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_TECTORIZE_EXPECT_SCALAR_TALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_TECTORIZE_EXPECT_SCALAR_TALUE_HPP + +#include +#include +#include +#include + +template +void expect_scalar_value() { + using stan::test::expect_match_return_t; + using std::vector; + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = build_vector(vector(), i); + vector z = build_vector(vector(), i); + T fz = F::template apply(z[i]); + expect_fvar_var_eq(F::apply_base(y[i]), y[i], fz, z[i]); + } + expect_match_return_t(); + expect_match_return_t, std::vector >(); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp new file mode 100644 index 00000000000..12121d75c36 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp @@ -0,0 +1,48 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_TECTORIZE_STD_TECTOR_TALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_TECTORIZE_STD_TECTOR_TALUE_HPP + +#include +#include +#include + +template +void expect_std_vector_value() { + using std::vector; + + size_t num_inputs = F::valid_inputs().size(); + + for (size_t i = 0; i < num_inputs; ++i) { + vector y = build_vector(vector(), i); + vector z = build_vector(vector(), i); + vector fz = F::template apply >(z); + EXPECT_EQ(z.size(), fz.size()); + expect_fvar_var_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector > a; + vector > b; + for (size_t k = 0; k < num_inputs; ++k) { + if (i == k) { + a.push_back(build_vector(vector(), j)); + b.push_back(build_vector(vector(), j)); + } + else { + a.push_back(build_vector(vector())); + b.push_back(build_vector(vector())); + } + } + vector > fb + = F::template apply > >(b); + + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + expect_fvar_var_eq( + F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); + } + } +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_values.hpp b/test/unit/math/mix/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..8cd8dbbecf0 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_values.hpp @@ -0,0 +1,30 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +void expect_values() { + using stan::math::fvar; + using stan::math::var; + using std::vector; + + expect_scalar_value >(); + expect_scalar_value > >(); + expect_std_vector_value >(); + expect_std_vector_value > >(); + expect_matrix_value >(); + expect_matrix_value > >(); + expect_vector_value >(); + expect_vector_value > >(); + expect_row_vector_value >(); + expect_row_vector_value > >(); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp new file mode 100644 index 00000000000..90ae1d62fa9 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp @@ -0,0 +1,49 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP + +#include +#include +#include +#include + +template +void expect_vector_value() { + using std::vector; + typedef Eigen::Matrix vector_t; + + size_t num_inputs = F::valid_inputs().size(); + vector_t template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + vector_t a = build_matrix(template_vector, i); + vector_t b = build_matrix(template_vector, i); + vector_t fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + expect_fvar_var_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector c; + vector d; + for (size_t k = 0; k < vector_vector_size; ++k) + if (k == i) { + c.push_back(build_matrix(template_vector, j)); + d.push_back(build_matrix(template_vector, j)); + } + else { + c.push_back(build_matrix(template_vector)); + d.push_back(build_matrix(template_vector)); + } + vector fd = F::template apply >(d); + + EXPECT_EQ(d.size(), fd.size()); + EXPECT_EQ(d[i].size(), fd[i].size()); + expect_fvar_var_eq( + F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + } + } +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp b/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp deleted file mode 100644 index 6cc26d088d3..00000000000 --- a/test/unit/math/mix/mat/vectorize/mix_expect_values.hpp +++ /dev/null @@ -1,294 +0,0 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include -#include - -template -static inline std::vector -build_valid_fvar_vector(std::vector var_vector, - int seed_index = -1) { - std::vector inputs = F::valid_inputs(); - for (size_t i = 0; i < inputs.size(); ++i) { - var_vector.push_back(inputs[i]); - } - return var_vector; -} - -template -static inline std::vector > -build_valid_fvar_vector(std::vector > fvar_vector, - int seed_index = -1) { - using std::vector; - using stan::math::fvar; - - vector val_vector = - build_valid_fvar_vector(vector(), seed_index); - vector d_vector; - if (seed_index != -1) - d_vector = build_valid_fvar_vector(vector(), seed_index); - - for (size_t i = 0; i < val_vector.size(); ++i) { - if (seed_index == static_cast(i)) - fvar_vector.push_back(fvar(val_vector[i], d_vector[i])); - else - fvar_vector.push_back(fvar(val_vector[i])); - } - return fvar_vector; -} - -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_valid_fvar_matrix(const T& x, int seed_index = -1) { - - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); - size_t num_inputs = F::valid_inputs().size(); - for (int i = 0; i < x.size(); ++i) { - std::vector inputs; - if (seed_index == i) - inputs = build_valid_fvar_vector(std::vector(), - (seed_index % num_inputs)); - else - inputs = build_valid_fvar_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; - } - return fvar_matrix; -} - -static inline void test_fvar(stan::math::var exp_var, - stan::math::var base_exp_var, - stan::math::var test_var, - stan::math::var base_test_var) { - EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); - AVEC exp_y = createAVEC(base_exp_var); - VEC exp_g; - exp_var.grad(exp_y, exp_g); - AVEC test_y = createAVEC(base_test_var); - VEC test_g; - test_var.grad(test_y, test_g); - EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); -} - -template -static inline void test_fvar(V exp_var, V base_exp_var, - V test_var, V base_test_var) { - test_fvar(exp_var.val(), base_exp_var.val(), - test_var.val(), base_test_var.val()); - test_fvar(exp_var.d_, base_exp_var.d_, - test_var.d_, base_test_var.d_); -} - -template -void expect_scalar_value() { - using stan::math::fvar; - using stan::test::expect_match_return_t; - using std::vector; - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = build_valid_fvar_vector(vector(), i); - vector z = build_valid_fvar_vector(vector(), i); - V fz = F::template apply(z[i]); - test_fvar(F::apply_base(y[i]), y[i], fz, z[i]); - } - expect_match_return_t(); - expect_match_return_t, std::vector >(); -} - -template -void expect_std_vector_values() { - using std::vector; - using stan::math::fvar; - - size_t num_inputs = F::valid_inputs().size(); - - for (size_t i = 0; i < num_inputs; ++i) { - vector y = build_valid_fvar_vector(vector(), i); - vector z = build_valid_fvar_vector(vector(), i); - vector fz = F::template apply >(z); - EXPECT_EQ(z.size(), fz.size()); - test_fvar(F::apply_base(y[i]), y[i], fz[i], z[i]); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector > a; - vector > b; - for (size_t k = 0; k < num_inputs; ++k) { - if (i == k) { - a.push_back(build_valid_fvar_vector(vector(), j)); - b.push_back(build_valid_fvar_vector(vector(), j)); - } - else { - a.push_back(build_valid_fvar_vector(vector())); - b.push_back(build_valid_fvar_vector(vector())); - } - } - vector > fb = - F::template apply > >(b); - - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); - } - } -} - -template -void expect_matrix_values() { - using stan::math::fvar; - using std::vector; - typedef - Eigen::Matrix MatrixXvar; - - size_t num_inputs = F::valid_inputs().size(); - size_t num_cols = 3; - MatrixXvar template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar y = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar z = build_valid_fvar_matrix(template_matrix, i); - MatrixXvar fz = F::template apply(z); - EXPECT_EQ(z.size(), fz.size()); - test_fvar(F::apply_base(y(i)), y(i), fz(i), z(i)); - } - - size_t vector_matrix_size = 2; - for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - vector a; - vector b; - for (size_t k = 0; k < vector_matrix_size; ++k) { - if (k == i) { - a.push_back(build_valid_fvar_matrix(template_matrix, j)); - b.push_back(build_valid_fvar_matrix(template_matrix, j)); - } else { - a.push_back(build_valid_fvar_matrix(template_matrix)); - b.push_back(build_valid_fvar_matrix(template_matrix)); - } - } - vector fb = F::template apply >(b); - EXPECT_EQ(b.size(), fb.size()); - EXPECT_EQ(b[i].size(), fb[i].size()); - test_fvar(F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); - } - } - - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - MatrixXvar c = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar d = build_valid_fvar_matrix(template_matrix, seed_i); - MatrixXvar fab = foo(d.block(block_i, block_j, 1, 1)); - test_fvar(F::apply_base(c(block_i, block_j)), c(block_i, block_j), - fab(0,0), d(block_i, block_j)); -} - -template -void expect_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar a = build_valid_fvar_matrix(template_vector, i); - VectorXvar b = build_valid_fvar_matrix(template_vector, i); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - vector d; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) { - c.push_back(build_valid_fvar_matrix(template_vector, j)); - d.push_back(build_valid_fvar_matrix(template_vector, j)); - } - else { - c.push_back(build_valid_fvar_matrix(template_vector)); - d.push_back(build_valid_fvar_matrix(template_vector)); - } - vector fd = F::template apply >(d); - - EXPECT_EQ(d.size(), fd.size()); - EXPECT_EQ(d[i].size(), fd[i].size()); - test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); - } - } -} - -template -void expect_row_vector_values() { - using stan::math::fvar; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar a = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar b = build_valid_fvar_matrix(template_vector, i); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - test_fvar(F::apply_base(a(i)), a(i), fb(i), b(i)); - } - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector c; - vector d; - for (size_t k = 0; k < vector_vector_size; ++k) - if (k == i) { - c.push_back(build_valid_fvar_matrix(template_vector, j)); - d.push_back(build_valid_fvar_matrix(template_vector, j)); - } - else { - c.push_back(build_valid_fvar_matrix(template_vector)); - d.push_back(build_valid_fvar_matrix(template_vector)); - } - vector fd = - F::template apply >(d); - - EXPECT_EQ(d.size(), fd.size()); - EXPECT_EQ(d[i].size(), fd[i].size()); - test_fvar(F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); - } - } -} - -template -void expect_values() { - using stan::math::fvar; - using stan::math::var; - using std::vector; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_values >(); - expect_std_vector_values > >(); - expect_matrix_values >(); - expect_matrix_values > >(); - expect_vector_values >(); - expect_vector_values > >(); - expect_row_vector_values >(); - expect_row_vector_values > >(); -} - -#endif diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 72753c35eee..3b64f1ce176 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include diff --git a/test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp b/test/unit/math/prim/mat/vectorize/expect_errors.hpp similarity index 100% rename from test/unit/math/prim/mat/vectorize/prim_expect_errors.hpp rename to test/unit/math/prim/mat/vectorize/expect_errors.hpp diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp new file mode 100644 index 00000000000..a36c5ebfa6a --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp @@ -0,0 +1,42 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP + +#include +#include +#include + +template +void expect_matrix_value() { + using Eigen::MatrixXd; + using std::vector; + + vector valid_inputs = F::valid_inputs(); + MatrixXd a(valid_inputs.size(), 3); + + for (int i = 0; i < a.size(); i++) { + a(i) = valid_inputs[(i % valid_inputs.size())]; + } + + MatrixXd fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + for (int i = 0; i < fa.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); + + MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)), fab(0,0)); + + vector d; + d.push_back(a); + d.push_back(a); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].rows(), fd[i].rows()); + EXPECT_EQ(d[i].cols(), fd[i].cols()); + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp new file mode 100644 index 00000000000..a084ae23e16 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp @@ -0,0 +1,36 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP + +#include +#include +#include + +template +void expect_row_vector_value() { + using Eigen::RowVectorXd; + using std::vector; + + std::vector valid_inputs = F::valid_inputs(); + + RowVectorXd c = + RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); + + RowVectorXd fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + for (int i = 0; i < fc.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); + + vector d; + d.push_back(c); + d.push_back(c); + + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp new file mode 100644 index 00000000000..cd7cd8319d7 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp @@ -0,0 +1,23 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP + +#include +#include + +template +void expect_scalar_value() { + using std::vector; + vector valid_inputs = F::valid_inputs(); + vector int_valid_inputs = F::int_valid_inputs(); + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + int input = int_valid_inputs[i]; + double v = F::template apply(input); + EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); + } + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), + F::template apply(valid_inputs[i])); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp new file mode 100644 index 00000000000..983cf9d9226 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp @@ -0,0 +1,39 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP + +#include +#include + +template +void expect_std_vector_value() { + using std::vector; + + vector valid_inputs = F::valid_inputs(); + + vector fy = F::template apply >(valid_inputs); + EXPECT_EQ(valid_inputs.size(), fy.size()); + for (size_t i = 0; i < valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), fy[i]); + } + + vector > z; + z.push_back(valid_inputs); + z.push_back(valid_inputs); + vector > fz + = F::template apply > >(z); + EXPECT_EQ(z.size(), fz.size()); + for (size_t i = 0; i < fz.size(); ++i) { + EXPECT_EQ(z[i].size(), fz[i].size()); + for (size_t j = 0; j < z[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); + } + + vector int_valid_inputs = F::int_valid_inputs(); + vector u2 = F::template apply >(int_valid_inputs); + EXPECT_EQ(int_valid_inputs.size(), u2.size()); + for (size_t i = 0; i < int_valid_inputs.size(); ++i) { + EXPECT_FLOAT_EQ(F::apply_base(int_valid_inputs[i]), u2[i]); + } +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp index 73d5340d0f9..13f6e03eec3 100644 --- a/test/unit/math/prim/mat/vectorize/expect_values.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_values.hpp @@ -1,157 +1,19 @@ #ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include -template -void expect_scalar_value() { - using std::vector; - vector valid_inputs = F::valid_inputs(); - vector int_valid_inputs = F::int_valid_inputs(); - for (size_t i = 0; i < int_valid_inputs.size(); ++i) { - int input = int_valid_inputs[i]; - double v = F::template apply(input); - EXPECT_FLOAT_EQ(F::apply_base(double(input)), v); - } - for (size_t i = 0; i < valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), - F::template apply(valid_inputs[i])); - } -} - -template -void expect_std_vectors_value() { - using std::vector; - - vector valid_inputs = F::valid_inputs(); - - vector fy = F::template apply >(valid_inputs); - EXPECT_EQ(valid_inputs.size(), fy.size()); - for (size_t i = 0; i < valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(valid_inputs[i]), fy[i]); - } - - vector > z; - z.push_back(valid_inputs); - z.push_back(valid_inputs); - vector > fz - = F::template apply > >(z); - EXPECT_EQ(z.size(), fz.size()); - for (size_t i = 0; i < fz.size(); ++i) { - EXPECT_EQ(z[i].size(), fz[i].size()); - for (size_t j = 0; j < z[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(z[i][j]), fz[i][j]); - } - - vector int_valid_inputs = F::int_valid_inputs(); - vector u2 = F::template apply >(int_valid_inputs); - EXPECT_EQ(int_valid_inputs.size(), u2.size()); - for (size_t i = 0; i < int_valid_inputs.size(); ++i) { - EXPECT_FLOAT_EQ(F::apply_base(int_valid_inputs[i]), u2[i]); - } -} - -template -void expect_matrix_value() { - using Eigen::MatrixXd; - using Eigen::RowVectorXd; - using std::vector; - - vector valid_inputs = F::valid_inputs(); - RowVectorXd eigen_valid_inputs = - RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); - size_t num_rows = 3; - MatrixXd a(num_rows, valid_inputs.size()); - - for (size_t i = 0; i < num_rows; i++) { - a.row(i) << eigen_valid_inputs; - } - - MatrixXd fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - for (int i = 0; i < fa.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(a(i)), fa(i)); - - MatrixXd fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(F::apply_base(a(1,1)), fab(0,0)); - - vector d; - d.push_back(a); - d.push_back(a); - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].rows(), fd[i].rows()); - EXPECT_EQ(d[i].cols(), fd[i].cols()); - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} - -template -void expect_vector_value() { - using Eigen::VectorXd; - using std::vector; - - std::vector valid_inputs = F::valid_inputs(); - - VectorXd b = VectorXd::Map(valid_inputs.data(), valid_inputs.size()); - VectorXd fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - for (int i = 0; i < fb.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); - - vector d; - d.push_back(b); - d.push_back(b); - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} - -template -void expect_row_vector_value() { - using Eigen::RowVectorXd; - using std::vector; - - std::vector valid_inputs = F::valid_inputs(); - - RowVectorXd c = - RowVectorXd::Map(valid_inputs.data(), valid_inputs.size()); - - RowVectorXd fc = F::template apply(c); - EXPECT_EQ(c.size(), fc.size()); - for (int i = 0; i < fc.size(); ++i) - EXPECT_FLOAT_EQ(F::apply_base(c(i)), fc(i)); - - vector d; - d.push_back(c); - d.push_back(c); - - vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); - for (size_t i = 0; i < fd.size(); ++i) { - EXPECT_EQ(d[i].size(), fd[i].size()); - for (int j = 0; j < fd[i].size(); ++j) - EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); - } -} // CALL THIS TO TEST EVERYTHING // see: apply_scalar_unary_test.cpp for an example template void expect_values() { expect_scalar_value(); - expect_std_vectors_value(); + expect_std_vector_value(); expect_matrix_value(); expect_vector_value(); expect_row_vector_value(); diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_vector_value.hpp new file mode 100644 index 00000000000..778ace365c0 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_vector_value.hpp @@ -0,0 +1,32 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP + +#include +#include +#include + +template +void expect_vector_value() { + using Eigen::VectorXd; + using std::vector; + + std::vector valid_inputs = F::valid_inputs(); + + VectorXd b = VectorXd::Map(valid_inputs.data(), valid_inputs.size()); + VectorXd fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + for (int i = 0; i < fb.size(); ++i) + EXPECT_FLOAT_EQ(F::apply_base(b(i)), fb(i)); + + vector d; + d.push_back(b); + d.push_back(b); + vector fd = F::template apply >(d); + EXPECT_EQ(d.size(), fd.size()); + for (size_t i = 0; i < fd.size(); ++i) { + EXPECT_EQ(d[i].size(), fd[i].size()); + for (int j = 0; j < fd[i].size(); ++j) + EXPECT_FLOAT_EQ(F::apply_base(d[i](j)), fd[i](j)); + } +} +#endif diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp index 24e1e13671b..0aaaac076af 100644 --- a/test/unit/math/prim/mat/vectorize/foo_fun.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -2,6 +2,7 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP #include +#include #include namespace stan { diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 0c62c160322..03544439665 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,10 +1,8 @@ -#include -#include #include #include #include -#include -#include +#include +#include #include /** diff --git a/test/unit/math/rev/mat/vectorize/build_matrix.hpp b/test/unit/math/rev/mat/vectorize/build_matrix.hpp new file mode 100644 index 00000000000..8bc77de590d --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/build_matrix.hpp @@ -0,0 +1,21 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_MATRIX_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_MATRIX_HPP + +#include +#include +#include + +template +static inline Eigen::Matrix +build_matrix(const T& x) { + Eigen::Matrix var_matrix(x.rows(), x.cols()); + std::vector inputs = F::valid_inputs(); + for (int i = 0; i < x.size(); ++i) { + var_matrix(i) = inputs[(i % inputs.size())]; + } + return var_matrix; +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/build_vector.hpp b/test/unit/math/rev/mat/vectorize/build_vector.hpp new file mode 100644 index 00000000000..1dad615f3e5 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/build_vector.hpp @@ -0,0 +1,16 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_VECTOR_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_VECTOR_HPP + +#include +#include + +template +static inline std::vector +build_vector() { + using std::vector; + using stan::math::var; + vector inputs = F::valid_inputs(); + return vector(inputs.begin(), inputs.end()); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp b/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp new file mode 100644 index 00000000000..9cd4ec14174 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp @@ -0,0 +1,16 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_AUTODIFF_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_AUTODIFF_HPP + +#include +#include + +template +static inline void expect_autodiff(double test_val, double test_adj) { + using stan::math::var; + + var x = test_val; + F::apply_base(x).grad(); + EXPECT_FLOAT_EQ(x.adj(), test_adj); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp b/test/unit/math/rev/mat/vectorize/expect_errors.hpp similarity index 85% rename from test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp rename to test/unit/math/rev/mat/vectorize/expect_errors.hpp index 8704ace76dc..501f90352d5 100644 --- a/test/unit/math/rev/mat/vectorize/rev_expect_errors.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ERRORS_HPP #include #include diff --git a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp new file mode 100644 index 00000000000..f03cff61330 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp @@ -0,0 +1,57 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP + +#include +#include +#include +#include +#include +#include + +template +void expect_matrix_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix MatrixXvar; + + size_t num_cols = 3; + size_t num_inputs = F::valid_inputs().size(); + MatrixXvar template_matrix(num_inputs, num_cols); + + for (int i = 0; i < template_matrix.size(); ++i) { + MatrixXvar a = build_matrix(template_matrix); + MatrixXvar fa = F::template apply(a); + EXPECT_EQ(a.size(), fa.size()); + EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); + + fa(i).grad(); + expect_autodiff(a(i).val(), a(i).adj()); + } + + size_t vector_matrix_size = 2; + for (size_t i = 0; i < vector_matrix_size; ++i) { + for (int j = 0; j < template_matrix.size(); ++j) { + + vector b; + for (size_t k = 0; k < vector_matrix_size; ++k) + b.push_back(build_matrix(template_matrix)); + vector fb = F::template apply >(b); + + EXPECT_EQ(b[i].size(), fb[i].size()); + EXPECT_EQ(b[i].rows(), fb[i].rows()); + EXPECT_EQ(b[i].cols(), fb[i].cols()); + EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); + + fb[i](j).grad(); + expect_autodiff(b[i](j).val(), b[i](j).adj()); + } + } + + MatrixXvar a = build_matrix(template_matrix); + MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); + EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); + fab(0,0).grad(); + expect_autodiff(a(1,1).val(), a(1,1).adj()); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp new file mode 100644 index 00000000000..6b7b699e6c1 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp @@ -0,0 +1,50 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP + +#include +#include +#include +#include +#include +#include + +template +void expect_row_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix RowVectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + RowVectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + RowVectorXvar b = build_matrix(template_vector); + RowVectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + expect_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_matrix(template_vector)); + vector fvb = + F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + expect_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp new file mode 100644 index 00000000000..3e213774bae --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp @@ -0,0 +1,26 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP + +#include +#include +#include +#include +#include + +template +void expect_scalar_value() { + using stan::math::var; + using std::vector; + + vector y = build_vector(); + for (size_t i = 0; i < y.size(); ++i) { + var fy = F::template apply(y[i]); + + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); + + fy.grad(); + expect_autodiff(y[i].val(), y[i].adj()); + } +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp new file mode 100644 index 00000000000..8fdd3f3056d --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp @@ -0,0 +1,48 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP + +#include +#include +#include +#include + +template +void expect_std_vector_value() { + using std::vector; + using stan::math::var; + + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + + vector y = build_vector(); + vector fy = F::template apply >(y); + + EXPECT_EQ(y.size(), fy.size()); + EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); + + fy[i].grad(); + expect_autodiff(y[i].val(), y[i].adj()); + } + + size_t vector_vector_size = 2; + for (size_t i = 0; i < vector_vector_size; ++i) { + + for (size_t j = 0; j < F::valid_inputs().size(); ++j) { + + vector > z; + for (size_t i = 0; i < vector_vector_size; ++i) { + z.push_back(build_vector()); + } + vector > fz = + F::template apply > >(z); + + EXPECT_EQ(z.size(), fz.size()); + EXPECT_EQ(z[i].size(), fz[i].size()); + EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); + + fz[i][j].grad(); + expect_autodiff(z[i][j].val(), z[i][j].adj()); + } + } +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp new file mode 100644 index 00000000000..a4a0df63cca --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_values.hpp @@ -0,0 +1,20 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP + +#include +#include +#include +#include +#include +#include + +template +void expect_values() { + expect_scalar_value(); + expect_std_vector_value(); + expect_matrix_value(); + expect_vector_value(); + expect_row_vector_value(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp new file mode 100644 index 00000000000..342596b7dd7 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp @@ -0,0 +1,49 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP + +#include +#include +#include +#include +#include +#include + +template +void expect_vector_value() { + using stan::math::var; + using std::vector; + typedef Eigen::Matrix VectorXvar; + + size_t num_inputs = F::valid_inputs().size(); + VectorXvar template_vector(num_inputs); + + for (size_t i = 0; i < num_inputs; ++i) { + VectorXvar b = build_matrix(template_vector); + VectorXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + + EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); + + fb(i).grad(); + expect_autodiff(b(i).val(), b(i).adj()); + } + + size_t vector_vector_size = 2; + + for (size_t i = 0; i < vector_vector_size; ++i) { + for (size_t j = 0; j < num_inputs; ++j) { + vector vb; + for (size_t k = 0; k < num_inputs; ++k) + vb.push_back(build_matrix(template_vector)); + vector fvb = F::template apply >(vb); + EXPECT_EQ(vb[i].size(), fvb[i].size()); + EXPECT_EQ(vb.size(), fvb.size()); + EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); + + fvb[i](j).grad(); + expect_autodiff(vb[i](j).val(), vb[i](j).adj()); + } + } +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp b/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp deleted file mode 100644 index d9524d6e40f..00000000000 --- a/test/unit/math/rev/mat/vectorize/rev_expect_values.hpp +++ /dev/null @@ -1,237 +0,0 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_EXPECT_VALUES_HPP - -#include -#include -#include - -template -static inline Eigen::Matrix -build_valid_var_matrix(const T& x) { - Eigen::Matrix var_matrix(x.rows(), x.cols()); - std::vector inputs = F::valid_inputs(); - for (int i = 0; i < x.size(); ++i) { - var_matrix(i) = inputs[(i % inputs.size())]; - } - return var_matrix; -} - -template -static inline std::vector -build_valid_var_vector() { - - std::vector inputs = F::valid_inputs(); - std::vector var_vector; - - for (size_t i = 0; i < inputs.size(); ++i) { - var_vector.push_back(inputs[i]); - } - - return var_vector; -} - -template -static inline void test_autodiff(double test_val, double test_adj) { - using stan::math::var; - - var x = test_val; - F::apply_base(x).grad(); - EXPECT_FLOAT_EQ(x.adj(), test_adj); -} - -template -void expect_scalar_value() { - using stan::math::var; - - std::vector y = build_valid_var_vector(); - for (size_t i = 0; i < y.size(); ++i) { - var fy = F::template apply(y[i]); - - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); - - fy.grad(); - test_autodiff(y[i].val(), y[i].adj()); - } -} - -template -void expect_std_vectors_value() { - using std::vector; - using stan::math::var; - - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - - vector y = build_valid_var_vector(); - vector fy = F::template apply >(y); - - EXPECT_EQ(y.size(), fy.size()); - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); - - fy[i].grad(); - test_autodiff(y[i].val(), y[i].adj()); - } -} - -template -void expect_std_vector_vectors_value() { - using std::vector; - using stan::math::var; - - size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { - - for (size_t j = 0; j < F::valid_inputs().size(); ++j) { - - vector > z; - for (size_t i = 0; i < vector_vector_size; ++i) { - z.push_back(build_valid_var_vector()); - } - vector > fz = - F::template apply > >(z); - - EXPECT_EQ(z.size(), fz.size()); - EXPECT_EQ(z[i].size(), fz[i].size()); - EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); - - fz[i][j].grad(); - test_autodiff(z[i][j].val(), z[i][j].adj()); - } - } -} - -template -void expect_matrix_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix MatrixXvar; - - size_t num_cols = 3; - size_t num_inputs = F::valid_inputs().size(); - MatrixXvar template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar a = build_valid_var_matrix(template_matrix); - MatrixXvar fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); - - fa(i).grad(); - test_autodiff(a(i).val(), a(i).adj()); - } - - size_t vector_matrix_size = 2; - for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - - vector b; - for (size_t k = 0; k < vector_matrix_size; ++k) - b.push_back(build_valid_var_matrix(template_matrix)); - vector fb = F::template apply >(b); - - EXPECT_EQ(b[i].size(), fb[i].size()); - EXPECT_EQ(b[i].rows(), fb[i].rows()); - EXPECT_EQ(b[i].cols(), fb[i].cols()); - EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); - - fb[i](j).grad(); - test_autodiff(b[i](j).val(), b[i](j).adj()); - } - } - - MatrixXvar a = build_valid_var_matrix(template_matrix); - MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); - fab(0,0).grad(); - test_autodiff(a(1,1).val(), a(1,1).adj()); -} - -template -void expect_vector_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix VectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - VectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar b = build_valid_var_matrix(template_vector); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - test_autodiff(b(i).val(), b(i).adj()); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector vb; - for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_valid_var_matrix(template_vector)); - vector fvb = F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - test_autodiff(vb[i](j).val(), vb[i](j).adj()); - } - } -} - -template -void expect_row_vector_value() { - using stan::math::var; - using std::vector; - typedef Eigen::Matrix RowVectorXvar; - - size_t num_inputs = F::valid_inputs().size(); - RowVectorXvar template_vector(num_inputs); - - for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar b = build_valid_var_matrix(template_vector); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - test_autodiff(b(i).val(), b(i).adj()); - } - - size_t vector_vector_size = 2; - - for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < num_inputs; ++j) { - vector vb; - for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_valid_var_matrix(template_vector)); - vector fvb = - F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - test_autodiff(vb[i](j).val(), vb[i](j).adj()); - } - } -} - -template -void expect_values() { - expect_scalar_value(); - expect_std_vectors_value(); - expect_std_vector_vectors_value(); - expect_matrix_value(); - expect_vector_value(); - expect_row_vector_value(); -} - -#endif From 9577e2e1cca0b6bb7702507f9163b38696ab1d2c Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 23 Feb 2016 11:02:36 -0500 Subject: [PATCH 19/36] Changed illegal_inputs to invalid_inputs and updated the corresponding expect error files --- .../math/prim/mat/vectorize/expect_int_scalar_error.hpp | 6 +++--- .../prim/mat/vectorize/expect_int_std_vector_error.hpp | 8 ++++---- test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp | 6 +++--- .../math/prim/mat/vectorize/expect_row_vector_error.hpp | 8 ++++---- test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp | 6 +++--- .../math/prim/mat/vectorize/expect_std_vector_error.hpp | 4 ++-- test/unit/math/prim/mat/vectorize/expect_vector_error.hpp | 8 ++++---- test/unit/math/prim/mat/vectorize/foo_base_test.hpp | 4 ++-- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp index f6fb754e22b..d373554e045 100644 --- a/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_int_scalar_error.hpp @@ -8,9 +8,9 @@ template void expect_int_scalar_error() { using std::vector; - vector int_illegal_inputs = F::int_illegal_inputs(); - for (size_t i = 0; i < int_illegal_inputs.size(); ++i) { - int input = int_illegal_inputs[i]; + vector int_invalid_inputs = F::int_invalid_inputs(); + for (size_t i = 0; i < int_invalid_inputs.size(); ++i) { + int input = int_invalid_inputs[i]; EXPECT_THROW(F::template apply(input), std::domain_error); } } diff --git a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp index 5adfbdc3e1f..144c13f443d 100644 --- a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp @@ -8,13 +8,13 @@ template void expect_int_std_vector_error() { using std::vector; - vector illegal_inputs = F::int_illegal_inputs(); - EXPECT_THROW(F::template apply >(illegal_inputs), + vector invalid_inputs = F::int_invalid_inputs(); + EXPECT_THROW(F::template apply >(invalid_inputs), std::domain_error); vector > z; - z.push_back(illegal_inputs); - z.push_back(illegal_inputs); + z.push_back(invalid_inputs); + z.push_back(invalid_inputs); EXPECT_THROW(F::template apply > >(z), std::domain_error); } diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp index 8c045bd1d9a..f1a269ffdaa 100644 --- a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp @@ -10,11 +10,11 @@ template void expect_matrix_error() { using std::vector; typedef Eigen::Matrix matrix_t; - vector illegal_inputs = F::illegal_inputs(); - matrix_t a(3, illegal_inputs.size()); + vector invalid_inputs = F::invalid_inputs(); + matrix_t a(3, invalid_inputs.size()); for (int i = 0; i < a.rows(); ++i) for (int j = 0; j < a.cols(); ++j) - a(i, j) = illegal_inputs[j]; + a(i, j) = invalid_inputs[j]; EXPECT_THROW(F::template apply(a), std::domain_error); EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), std::domain_error); diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp index 9cbfa221459..5052b5400e6 100644 --- a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp @@ -10,11 +10,11 @@ void expect_row_vector_error() { using std::vector; typedef Eigen::Matrix row_vector_t; - std::vector illegal_inputs = F::illegal_inputs(); + std::vector invalid_inputs = F::invalid_inputs(); - row_vector_t c = row_vector_t(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - c(i) = illegal_inputs[i]; + row_vector_t c = row_vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + c(i) = invalid_inputs[i]; EXPECT_THROW(F::template apply(c), std::domain_error); vector d; diff --git a/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp b/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp index 2b636308c6e..d8e0d0f32aa 100644 --- a/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_scalar_error.hpp @@ -8,9 +8,9 @@ template void expect_scalar_error() { using std::vector; - vector illegal_inputs = F::illegal_inputs(); - for (size_t i = 0; i < illegal_inputs.size(); ++i) { - T input = illegal_inputs[i]; + vector invalid_inputs = F::invalid_inputs(); + for (size_t i = 0; i < invalid_inputs.size(); ++i) { + T input = invalid_inputs[i]; EXPECT_THROW(F::template apply(input), std::domain_error); } } diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp index 127293c8fb3..2e59421ac19 100644 --- a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp @@ -8,8 +8,8 @@ template void expect_std_vector_error() { using std::vector; - vector illegal_inputs = F::illegal_inputs(); - vector y(illegal_inputs.begin(), illegal_inputs.end()); + vector invalid_inputs = F::invalid_inputs(); + vector y(invalid_inputs.begin(), invalid_inputs.end()); EXPECT_THROW(F::template apply >(y), std::domain_error); vector > z; diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp index 5a77b2fe568..410eb089bef 100644 --- a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp @@ -10,11 +10,11 @@ template void expect_vector_error() { using std::vector; typedef Eigen::Matrix vector_t; - vector illegal_inputs = F::illegal_inputs(); + vector invalid_inputs = F::invalid_inputs(); - vector_t b = vector_t(illegal_inputs.size()); - for (size_t i = 0; i < illegal_inputs.size(); ++i) - b(i) = illegal_inputs[i]; + vector_t b = vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + b(i) = invalid_inputs[i]; EXPECT_THROW(F::template apply(b), std::domain_error); vector d; diff --git a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp index b3b4d471a69..cfc475817a8 100644 --- a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_base_test.hpp @@ -68,7 +68,7 @@ struct foo_base_test { /** * Return sequence of invalid double-valued inputs. */ - static std::vector illegal_inputs() { + static std::vector invalid_inputs() { return stan::math::vector_builder() .add(10.6).add(10.6).add(25.7).add(100.25).build(); } @@ -84,7 +84,7 @@ struct foo_base_test { /** * Return sequence of invalid integer inputs. */ - static std::vector int_illegal_inputs() { + static std::vector int_invalid_inputs() { return stan::math::vector_builder() .add(10).add(25).add(100).add(50).build(); } From e3abd1836279cf4060c2a6d32dbff5179d03651c Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 23 Feb 2016 12:38:36 -0500 Subject: [PATCH 20/36] Added type tests --- .../mat/vectorize/apply_scalar_unary_test.cpp | 5 +++ .../mat/vectorize/apply_scalar_unary_test.cpp | 6 +++ .../mat/vectorize/apply_scalar_unary_test.cpp | 3 ++ .../math/prim/mat/vectorize/expect_types.hpp | 39 +++++++++++++++++++ .../mat/vectorize/apply_scalar_unary_test.cpp | 3 ++ 5 files changed, 56 insertions(+) create mode 100644 test/unit/math/prim/mat/vectorize/expect_types.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp index ddcac6a8ce6..ba8b722e76f 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,5 +1,7 @@ +#include #include #include +#include #include #include #include @@ -7,6 +9,9 @@ // this tests that the expect_values test works on a mock function TEST(MathFwdMatVectorize, applyScalarUnaryMock) { + using stan::math::fvar; + expect_types >(); + expect_types > >(); expect_values(); expect_errors(); } diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp index 7a7693bed8b..dd87e15617d 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp @@ -4,11 +4,17 @@ #include #include #include +#include #include #include #include TEST(MathFwdMatVectorize, applyScalarUnaryMock) { + using stan::math::fvar; + using stan::math::var; + + expect_types >(); + expect_types > >(); expect_values(); expect_errors(); } diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp index 3b64f1ce176..859173c9d66 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -6,6 +7,8 @@ // test that returned values are correct and that errors are thrown // for illegal inputs for foo TEST(MathPrimMatVectorize, applyScalarUnaryMock) { + expect_int_types(); + expect_types(); expect_values(); expect_errors(); } diff --git a/test/unit/math/prim/mat/vectorize/expect_types.hpp b/test/unit/math/prim/mat/vectorize/expect_types.hpp new file mode 100644 index 00000000000..de4fc0f95c2 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_types.hpp @@ -0,0 +1,39 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_TYPES_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_TYPES_HPP + +#include +#include +#include + +template +void expect_int_types() { + using std::vector; + using stan::test::expect_match_return_t; + + expect_match_return_t(); + expect_match_return_t, vector >(); + expect_match_return_t >, + vector > >(); +} + +template +void expect_types() { + using stan::test::expect_match_return_t; + using std::vector; + using Eigen::Matrix; + typedef Eigen::Matrix matrix_t; + typedef Eigen::Matrix vector_t; + typedef Eigen::Matrix row_vector_t; + + expect_match_return_t(); + expect_match_return_t, vector >(); + expect_match_return_t >, vector > >(); + expect_match_return_t(); + expect_match_return_t, vector >(); + expect_match_return_t(); + expect_match_return_t, vector >(); + expect_match_return_t(); + expect_match_return_t, vector >(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp index 03544439665..83895d1c20b 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp @@ -1,6 +1,8 @@ +#include #include #include #include +#include #include #include #include @@ -9,6 +11,7 @@ * this tests that the expect_values test works on a mock function */ TEST(MathRevMatVectorize, applyScalarUnaryMock) { + expect_types(); expect_values(); expect_errors(); } From 418a9935d57170922d5a9348e094260bb9b3b38e Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 23 Feb 2016 12:40:11 -0500 Subject: [PATCH 21/36] Removed unnecessary include for rev expect_values.hpp file --- test/unit/math/rev/mat/vectorize/expect_values.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp index a4a0df63cca..73a3c18421d 100644 --- a/test/unit/math/rev/mat/vectorize/expect_values.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_values.hpp @@ -1,7 +1,6 @@ #ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP -#include #include #include #include From 0621d6cc5b182778c4ab583aaa497c4598059815 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 23 Feb 2016 12:52:18 -0500 Subject: [PATCH 22/36] Renamed to fwd and mix expect_*_eq.hpp to expect_eq.hpp for greater consistency and updated files accordingly. Also removed expect_match_return_t.hpp tests from fwd and mix expect_scalar_value.hpp --- test/unit/math/fwd/mat/vectorize/expect_eq.hpp | 17 +++++++++++++++++ .../math/fwd/mat/vectorize/expect_fvar_eq.hpp | 17 ----------------- .../fwd/mat/vectorize/expect_matrix_value.hpp | 8 ++++---- .../mat/vectorize/expect_row_vector_value.hpp | 6 +++--- .../fwd/mat/vectorize/expect_scalar_value.hpp | 9 ++------- .../mat/vectorize/expect_std_vector_value.hpp | 6 +++--- .../fwd/mat/vectorize/expect_vector_value.hpp | 6 +++--- .../{expect_fvar_var_eq.hpp => expect_eq.hpp} | 12 ++++++------ .../mix/mat/vectorize/expect_matrix_value.hpp | 8 ++++---- .../mat/vectorize/expect_row_vector_value.hpp | 6 +++--- .../mix/mat/vectorize/expect_scalar_value.hpp | 8 ++------ .../mat/vectorize/expect_std_vector_value.hpp | 6 +++--- .../mix/mat/vectorize/expect_vector_value.hpp | 6 +++--- 13 files changed, 53 insertions(+), 62 deletions(-) create mode 100644 test/unit/math/fwd/mat/vectorize/expect_eq.hpp delete mode 100644 test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp rename test/unit/math/mix/mat/vectorize/{expect_fvar_var_eq.hpp => expect_eq.hpp} (68%) diff --git a/test/unit/math/fwd/mat/vectorize/expect_eq.hpp b/test/unit/math/fwd/mat/vectorize/expect_eq.hpp new file mode 100644 index 00000000000..a61a95232d9 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_eq.hpp @@ -0,0 +1,17 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_EQ_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_EQ_HPP + +#include +#include + +static inline void expect_eq(double exp_var, double test_var) { + EXPECT_FLOAT_EQ(exp_var, test_var); +} + +template +static inline void expect_eq(T exp_var, T test_var) { + expect_eq(exp_var.val(), test_var.val()); + expect_eq(exp_var.d_, test_var.d_); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp b/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp deleted file mode 100644 index 5dec8295d82..00000000000 --- a/test/unit/math/fwd/mat/vectorize/expect_fvar_eq.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FVAR_EQ_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FVAR_EQ_HPP - -#include -#include - -static inline void expect_fvar_eq(double exp_var, double test_var) { - EXPECT_FLOAT_EQ(exp_var, test_var); -} - -template -static inline void expect_fvar_eq(T exp_var, T test_var) { - expect_fvar_eq(exp_var.val(), test_var.val()); - expect_fvar_eq(exp_var.d_, test_var.d_); -} - -#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp index 54a66cf9298..7c5465e87a8 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_matrix_value() { @@ -21,7 +21,7 @@ void expect_matrix_value() { matrix_t a = build_matrix(template_matrix, i); matrix_t fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); - expect_fvar_eq(F::apply_base(a(i)), fa(i)); + expect_eq(F::apply_base(a(i)), fa(i)); } size_t vector_matrix_size = 2; @@ -36,7 +36,7 @@ void expect_matrix_value() { vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_fvar_eq(F::apply_base(b[i](j)), fb[i](j)); + expect_eq(F::apply_base(b[i](j)), fb[i](j)); } } @@ -45,7 +45,7 @@ void expect_matrix_value() { int seed_i = block_j * num_inputs + block_i; matrix_t a = build_matrix(template_matrix, seed_i); matrix_t fab = foo(a.block(block_i, block_j, 1, 1)); - expect_fvar_eq(F::apply_base(a(1,1)), fab(0,0)); + expect_eq(F::apply_base(a(1,1)), fab(0,0)); } #endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp index 865fea16217..952e1b9ccf9 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -20,7 +20,7 @@ void expect_row_vector_value() { row_vector_t b = build_matrix(template_vector, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_fvar_eq(F::apply_base(b(i)), fb(i)); + expect_eq(F::apply_base(b(i)), fb(i)); } size_t vector_vector_size = 2; @@ -37,7 +37,7 @@ void expect_row_vector_value() { EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); - expect_fvar_eq(F::apply_base(c[i](j)), fc[i](j)); + expect_eq(F::apply_base(c[i](j)), fc[i](j)); } } } diff --git a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp index 4c4e9868789..1b6c75e9c73 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp @@ -3,24 +3,19 @@ #include #include -#include #include -#include +#include template void expect_scalar_value() { - using stan::test::expect_match_return_t; using std::vector; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { vector y = build_vector(vector(), i); T fy = F::template apply(y[i]); T exp_y = F::apply_base(y[i]); - expect_fvar_eq(exp_y, fy); + expect_eq(exp_y, fy); } - expect_match_return_t(); - expect_match_return_t, std::vector >(); - } #endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp index c8f27b5fb4c..303ffa46157 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_std_vector_value() { @@ -14,7 +14,7 @@ void expect_std_vector_value() { vector y = build_vector(vector(), i); vector fy = F::template apply >(y); EXPECT_EQ(y.size(), fy.size()); - expect_fvar_eq(F::apply_base(y[i]), fy[i]); + expect_eq(F::apply_base(y[i]), fy[i]); } size_t vector_vector_size = 2; @@ -34,7 +34,7 @@ void expect_std_vector_value() { EXPECT_EQ(z.size(), fz.size()); EXPECT_EQ(z[i].size(), fz[i].size()); - expect_fvar_eq(F::apply_base(z[i][j]), fz[i][j]); + expect_eq(F::apply_base(z[i][j]), fz[i][j]); } } } diff --git a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp index 90ff56ed7df..89bb8eb9f4e 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -20,7 +20,7 @@ void expect_vector_value() { vector_t b = build_matrix(template_vector, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_fvar_eq(F::apply_base(b(i)), fb(i)); + expect_eq(F::apply_base(b(i)), fb(i)); } size_t vector_vector_size = 2; @@ -36,7 +36,7 @@ void expect_vector_value() { EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); - expect_fvar_eq(F::apply_base(c[i](j)), fc[i](j)); + expect_eq(F::apply_base(c[i](j)), fc[i](j)); } } } diff --git a/test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp b/test/unit/math/mix/mat/vectorize/expect_eq.hpp similarity index 68% rename from test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp rename to test/unit/math/mix/mat/vectorize/expect_eq.hpp index f3ed9751336..3662a2085d5 100644 --- a/test/unit/math/mix/mat/vectorize/expect_fvar_var_eq.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_eq.hpp @@ -1,12 +1,12 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_FVAR_VAR_EQ_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_FVAR_VAR_EQ_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_EQ_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_EQ_HPP #include #include #include #include -static inline void expect_fvar_var_eq(stan::math::var exp_var, +static inline void expect_eq(stan::math::var exp_var, stan::math::var base_exp_var, stan::math::var test_var, stan::math::var base_test_var) { @@ -21,11 +21,11 @@ static inline void expect_fvar_var_eq(stan::math::var exp_var, } template -static inline void expect_fvar_var_eq(V exp_var, V base_exp_var, +static inline void expect_eq(V exp_var, V base_exp_var, V test_var, V base_test_var) { - expect_fvar_var_eq(exp_var.val(), base_exp_var.val(), + expect_eq(exp_var.val(), base_exp_var.val(), test_var.val(), base_test_var.val()); - expect_fvar_var_eq(exp_var.d_, base_exp_var.d_, + expect_eq(exp_var.d_, base_exp_var.d_, test_var.d_, base_test_var.d_); } diff --git a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp index 5877d8bd40f..e83c6fe891f 100644 --- a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_matrix_value() { @@ -21,7 +21,7 @@ void expect_matrix_value() { matrix_t z = build_matrix(template_matrix, i); matrix_t fz = F::template apply(z); EXPECT_EQ(z.size(), fz.size()); - expect_fvar_var_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); + expect_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); } size_t vector_matrix_size = 2; @@ -41,7 +41,7 @@ void expect_matrix_value() { vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_fvar_var_eq( + expect_eq( F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); } } @@ -52,7 +52,7 @@ void expect_matrix_value() { matrix_t c = build_matrix(template_matrix, seed_i); matrix_t d = build_matrix(template_matrix, seed_i); matrix_t fab = foo(d.block(block_i, block_j, 1, 1)); - expect_fvar_var_eq( + expect_eq( F::apply_base(c(block_i, block_j)), c(block_i, block_j), fab(0,0), d(block_i, block_j)); } diff --git a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp index d72777e01e2..ac345668c2e 100644 --- a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -19,7 +19,7 @@ void expect_row_vector_value() { row_vector_t b = build_matrix(template_vector, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_fvar_var_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + expect_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); } size_t vector_vector_size = 2; @@ -41,7 +41,7 @@ void expect_row_vector_value() { EXPECT_EQ(d.size(), fd.size()); EXPECT_EQ(d[i].size(), fd[i].size()); - expect_fvar_var_eq( + expect_eq( F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); } } diff --git a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp index 84e1927fae2..b0bd4baee4f 100644 --- a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp @@ -2,22 +2,18 @@ #define TEST_UNIT_MATH_MIX_MAT_TECTORIZE_EXPECT_SCALAR_TALUE_HPP #include -#include #include -#include +#include template void expect_scalar_value() { - using stan::test::expect_match_return_t; using std::vector; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { vector y = build_vector(vector(), i); vector z = build_vector(vector(), i); T fz = F::template apply(z[i]); - expect_fvar_var_eq(F::apply_base(y[i]), y[i], fz, z[i]); + expect_eq(F::apply_base(y[i]), y[i], fz, z[i]); } - expect_match_return_t(); - expect_match_return_t, std::vector >(); } #endif diff --git a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp index 12121d75c36..c907ff4eb2e 100644 --- a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp @@ -3,7 +3,7 @@ #include #include -#include +#include template void expect_std_vector_value() { @@ -16,7 +16,7 @@ void expect_std_vector_value() { vector z = build_vector(vector(), i); vector fz = F::template apply >(z); EXPECT_EQ(z.size(), fz.size()); - expect_fvar_var_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); + expect_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); } size_t vector_vector_size = 2; @@ -39,7 +39,7 @@ void expect_std_vector_value() { EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_fvar_var_eq( + expect_eq( F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); } } diff --git a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp index 90ae1d62fa9..9746a769634 100644 --- a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -19,7 +19,7 @@ void expect_vector_value() { vector_t b = build_matrix(template_vector, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_fvar_var_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + expect_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); } size_t vector_vector_size = 2; @@ -40,7 +40,7 @@ void expect_vector_value() { EXPECT_EQ(d.size(), fd.size()); EXPECT_EQ(d[i].size(), fd[i].size()); - expect_fvar_var_eq( + expect_eq( F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); } } From d172f1cfacdb39fe191ae6566e559e3b455a0a46 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 23 Feb 2016 13:49:16 -0500 Subject: [PATCH 23/36] Updated rev autodiff tests and mix autodiff tests to use a rev expect_eq.hpp --- .../unit/math/mix/mat/vectorize/expect_eq.hpp | 17 +--------- .../rev/mat/vectorize/expect_autodiff.hpp | 16 --------- .../unit/math/rev/mat/vectorize/expect_eq.hpp | 22 ++++++++++++ .../rev/mat/vectorize/expect_matrix_value.hpp | 34 +++++++++---------- .../mat/vectorize/expect_row_vector_value.hpp | 29 ++++++++-------- .../rev/mat/vectorize/expect_scalar_value.hpp | 12 +++---- .../mat/vectorize/expect_std_vector_value.hpp | 27 +++++++-------- .../rev/mat/vectorize/expect_vector_value.hpp | 27 +++++++-------- 8 files changed, 86 insertions(+), 98 deletions(-) delete mode 100644 test/unit/math/rev/mat/vectorize/expect_autodiff.hpp create mode 100644 test/unit/math/rev/mat/vectorize/expect_eq.hpp diff --git a/test/unit/math/mix/mat/vectorize/expect_eq.hpp b/test/unit/math/mix/mat/vectorize/expect_eq.hpp index 3662a2085d5..7d70861a505 100644 --- a/test/unit/math/mix/mat/vectorize/expect_eq.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_eq.hpp @@ -2,24 +2,9 @@ #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_EQ_HPP #include -#include -#include +#include #include -static inline void expect_eq(stan::math::var exp_var, - stan::math::var base_exp_var, - stan::math::var test_var, - stan::math::var base_test_var) { - EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); - AVEC exp_y = createAVEC(base_exp_var); - VEC exp_g; - exp_var.grad(exp_y, exp_g); - AVEC test_y = createAVEC(base_test_var); - VEC test_g; - test_var.grad(test_y, test_g); - EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); -} - template static inline void expect_eq(V exp_var, V base_exp_var, V test_var, V base_test_var) { diff --git a/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp b/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp deleted file mode 100644 index 9cd4ec14174..00000000000 --- a/test/unit/math/rev/mat/vectorize/expect_autodiff.hpp +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_AUTODIFF_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_AUTODIFF_HPP - -#include -#include - -template -static inline void expect_autodiff(double test_val, double test_adj) { - using stan::math::var; - - var x = test_val; - F::apply_base(x).grad(); - EXPECT_FLOAT_EQ(x.adj(), test_adj); -} - -#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_eq.hpp b/test/unit/math/rev/mat/vectorize/expect_eq.hpp new file mode 100644 index 00000000000..3031f3a9094 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_eq.hpp @@ -0,0 +1,22 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_EQ_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_EQ_HPP + +#include +#include +#include + +static inline void expect_eq(stan::math::var exp_var, + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { + EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); + AVEC exp_y = createAVEC(base_exp_var); + VEC exp_g; + exp_var.grad(exp_y, exp_g); + AVEC test_y = createAVEC(base_test_var); + VEC test_g; + test_var.grad(test_y, test_g); + EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp index f03cff61330..6bc8d69f709 100644 --- a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_matrix_value() { @@ -24,34 +24,34 @@ void expect_matrix_value() { EXPECT_EQ(a.size(), fa.size()); EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); - fa(i).grad(); - expect_autodiff(a(i).val(), a(i).adj()); + MatrixXvar b = build_matrix(template_matrix); + expect_eq(F::apply_base(b(i)), b(i), fa(i), a(i)); } size_t vector_matrix_size = 2; for (size_t i = 0; i < vector_matrix_size; ++i) { for (int j = 0; j < template_matrix.size(); ++j) { - vector b; + vector c; for (size_t k = 0; k < vector_matrix_size; ++k) - b.push_back(build_matrix(template_matrix)); - vector fb = F::template apply >(b); + c.push_back(build_matrix(template_matrix)); + vector fc = F::template apply >(c); - EXPECT_EQ(b[i].size(), fb[i].size()); - EXPECT_EQ(b[i].rows(), fb[i].rows()); - EXPECT_EQ(b[i].cols(), fb[i].cols()); - EXPECT_FLOAT_EQ(F::apply_base(b[i](j)).val(), fb[i](j).val()); + EXPECT_EQ(c[i].size(), fc[i].size()); + EXPECT_EQ(c[i].rows(), fc[i].rows()); + EXPECT_EQ(c[i].cols(), fc[i].cols()); - fb[i](j).grad(); - expect_autodiff(b[i](j).val(), b[i](j).adj()); + vector d; + for (size_t k = 0; k < vector_matrix_size; ++k) + d.push_back(build_matrix(template_matrix)); + expect_eq(F::apply_base(d[i](j)), d[i](j), fc[i](j), c[i](j)); } } - MatrixXvar a = build_matrix(template_matrix); - MatrixXvar fab = F::template apply(a.block(1, 1, 1, 1)); - EXPECT_FLOAT_EQ(F::apply_base(a(1,1)).val(), fab(0,0).val()); - fab(0,0).grad(); - expect_autodiff(a(1,1).val(), a(1,1).adj()); + MatrixXvar e = build_matrix(template_matrix); + MatrixXvar feb = F::template apply(e.block(1, 1, 1, 1)); + MatrixXvar f = build_matrix(template_matrix).block(1, 1, 1, 1); + expect_eq(F::apply_base(f(0,0)), f(0,0), feb(0,0), e(1,1)); } #endif diff --git a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp index 6b7b699e6c1..d718c28077d 100644 --- a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -22,27 +22,26 @@ void expect_row_vector_value() { RowVectorXvar fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - expect_autodiff(b(i).val(), b(i).adj()); + RowVectorXvar c = build_matrix(template_vector); + expect_eq(F::apply_base(c(i)), c(i), fb(i), b(i)); } size_t vector_vector_size = 2; for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { - vector vb; + vector d; + for (size_t k = 0; k < num_inputs; ++k) + d.push_back(build_matrix(template_vector)); + vector fd = + F::template apply >(d); + EXPECT_EQ(d[i].size(), fd[i].size()); + EXPECT_EQ(d.size(), fd.size()); + + vector e; for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_matrix(template_vector)); - vector fvb = - F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - expect_autodiff(vb[i](j).val(), vb[i](j).adj()); + e.push_back(build_matrix(template_vector)); + expect_eq(F::apply_base(e[i](j)), e[i](j), fd[i](j), d[i](j)); } } } diff --git a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp index 3e213774bae..c5d913d23d0 100644 --- a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp @@ -5,21 +5,21 @@ #include #include #include -#include +#include template void expect_scalar_value() { using stan::math::var; using std::vector; - vector y = build_vector(); - for (size_t i = 0; i < y.size(); ++i) { + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = build_vector(); var fy = F::template apply(y[i]); - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy.val()); + vector z = build_vector(); + var fz = F::apply_base(z[i]); - fy.grad(); - expect_autodiff(y[i].val(), y[i].adj()); + expect_eq(fz, z[i], fy, y[i]); } } diff --git a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp index 8fdd3f3056d..90930805c90 100644 --- a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_std_vector_value() { @@ -15,12 +15,10 @@ void expect_std_vector_value() { vector y = build_vector(); vector fy = F::template apply >(y); + vector z = build_vector(); EXPECT_EQ(y.size(), fy.size()); - EXPECT_FLOAT_EQ(F::apply_base(y[i]).val(), fy[i].val()); - - fy[i].grad(); - expect_autodiff(y[i].val(), y[i].adj()); + expect_eq(F::apply_base(z[i]), z[i], fy[i], y[i]); } size_t vector_vector_size = 2; @@ -28,19 +26,20 @@ void expect_std_vector_value() { for (size_t j = 0; j < F::valid_inputs().size(); ++j) { - vector > z; + vector > a; for (size_t i = 0; i < vector_vector_size; ++i) { - z.push_back(build_vector()); + a.push_back(build_vector()); } - vector > fz = - F::template apply > >(z); + vector > fa = + F::template apply > >(a); + EXPECT_EQ(a.size(), fa.size()); + EXPECT_EQ(a[i].size(), fa[i].size()); - EXPECT_EQ(z.size(), fz.size()); - EXPECT_EQ(z[i].size(), fz[i].size()); - EXPECT_FLOAT_EQ(F::apply_base(z[i][j]).val(), fz[i][j].val()); + vector > b; + for (size_t i = 0; i < vector_vector_size; ++i) + b.push_back(build_vector()); - fz[i][j].grad(); - expect_autodiff(z[i][j].val(), z[i][j].adj()); + expect_eq(F::apply_base(b[i][j]), b[i][j], fa[i][j], a[i][j]); } } } diff --git a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp index 342596b7dd7..71344e80589 100644 --- a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -22,26 +22,25 @@ void expect_vector_value() { VectorXvar fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - EXPECT_FLOAT_EQ(F::apply_base(b(i)).val(), fb(i).val()); - - fb(i).grad(); - expect_autodiff(b(i).val(), b(i).adj()); + VectorXvar c = build_matrix(template_vector); + expect_eq(F::apply_base(c(i)), c(i), fb(i), b(i)); } size_t vector_vector_size = 2; for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { - vector vb; + vector d; + for (size_t k = 0; k < num_inputs; ++k) + d.push_back(build_matrix(template_vector)); + vector fd = F::template apply >(d); + EXPECT_EQ(d[i].size(), fd[i].size()); + EXPECT_EQ(d.size(), fd.size()); + + vector e; for (size_t k = 0; k < num_inputs; ++k) - vb.push_back(build_matrix(template_vector)); - vector fvb = F::template apply >(vb); - EXPECT_EQ(vb[i].size(), fvb[i].size()); - EXPECT_EQ(vb.size(), fvb.size()); - EXPECT_FLOAT_EQ(F::apply_base(vb[i](j)).val(), fvb[i](j).val()); - - fvb[i](j).grad(); - expect_autodiff(vb[i](j).val(), vb[i](j).adj()); + e.push_back(build_matrix(template_vector)); + expect_eq(F::apply_base(e[i](j)), e[i](j), fd[i](j), d[i](j)); } } } From 5999fd9d20b2a6420b130c718b99c3eb713df44f Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Fri, 26 Feb 2016 14:59:36 -0500 Subject: [PATCH 24/36] Doc build_vector.hpp and reset adjoint to zero for rev tests --- test/unit/math/fwd/mat/vectorize/build_vector.hpp | 3 +++ test/unit/math/mix/mat/vectorize/build_vector.hpp | 2 ++ test/unit/math/rev/mat/vectorize/expect_eq.hpp | 10 +++++++--- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/test/unit/math/fwd/mat/vectorize/build_vector.hpp b/test/unit/math/fwd/mat/vectorize/build_vector.hpp index 442ca00644b..f3725993543 100644 --- a/test/unit/math/fwd/mat/vectorize/build_vector.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_vector.hpp @@ -22,6 +22,9 @@ build_vector(std::vector > fvar_vector, = build_vector(vector(), seed_index); for (size_t i = 0; i < template_vector.size(); ++i) { + + // For fvar >, this will fill in + // all four components if (seed_index == static_cast(i)) fvar_vector .push_back(fvar(template_vector[i], template_vector[i])); diff --git a/test/unit/math/mix/mat/vectorize/build_vector.hpp b/test/unit/math/mix/mat/vectorize/build_vector.hpp index bc9e8eeff32..0932b0ba8c4 100644 --- a/test/unit/math/mix/mat/vectorize/build_vector.hpp +++ b/test/unit/math/mix/mat/vectorize/build_vector.hpp @@ -30,6 +30,8 @@ build_vector(std::vector > fvar_vector, d_vector = build_vector(vector(), seed_index); for (size_t i = 0; i < val_vector.size(); ++i) { + // For fvar >, this ensures that when + // we test the var, we don't autodiff the same var twice if (seed_index == static_cast(i)) fvar_vector.push_back(fvar(val_vector[i], d_vector[i])); else diff --git a/test/unit/math/rev/mat/vectorize/expect_eq.hpp b/test/unit/math/rev/mat/vectorize/expect_eq.hpp index 3031f3a9094..abf6dfa3ada 100644 --- a/test/unit/math/rev/mat/vectorize/expect_eq.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_eq.hpp @@ -2,19 +2,23 @@ #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_EQ_HPP #include +#include #include #include static inline void expect_eq(stan::math::var exp_var, - stan::math::var base_exp_var, - stan::math::var test_var, - stan::math::var base_test_var) { + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { + using stan::math::set_zero_all_adjoints; EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); AVEC exp_y = createAVEC(base_exp_var); VEC exp_g; + set_zero_all_adjoints(); exp_var.grad(exp_y, exp_g); AVEC test_y = createAVEC(base_test_var); VEC test_g; + set_zero_all_adjoints(); test_var.grad(test_y, test_g); EXPECT_FLOAT_EQ(exp_g[0], test_g[0]); } From 2c770fbbcbf2503d06737046c79a1c0b90839674 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Fri, 26 Feb 2016 16:38:55 -0500 Subject: [PATCH 25/36] Cleaned up style issues --- .../math/fwd/mat/vectorize/build_matrix.hpp | 22 ++++---- .../math/fwd/mat/vectorize/build_vector.hpp | 10 ++-- .../fwd/mat/vectorize/expect_matrix_value.hpp | 28 +++++----- .../mat/vectorize/expect_row_vector_value.hpp | 18 +++---- .../fwd/mat/vectorize/expect_scalar_value.hpp | 4 +- .../mat/vectorize/expect_std_vector_value.hpp | 10 ++-- ...{expect_eq.hpp => expect_val_deriv_eq.hpp} | 8 +-- .../math/fwd/mat/vectorize/expect_values.hpp | 1 + .../fwd/mat/vectorize/expect_vector_value.hpp | 15 +++--- .../math/mix/mat/vectorize/build_matrix.hpp | 23 ++++---- .../unit/math/mix/mat/vectorize/expect_eq.hpp | 17 ------ .../mix/mat/vectorize/expect_matrix_value.hpp | 43 +++++++-------- .../mat/vectorize/expect_row_vector_value.hpp | 23 ++++---- .../mix/mat/vectorize/expect_scalar_value.hpp | 8 +-- .../mat/vectorize/expect_std_vector_value.hpp | 17 +++--- .../mix/mat/vectorize/expect_val_deriv_eq.hpp | 17 ++++++ .../math/mix/mat/vectorize/expect_values.hpp | 1 + .../mix/mat/vectorize/expect_vector_value.hpp | 23 ++++---- .../math/rev/mat/vectorize/build_matrix.hpp | 18 ++++--- .../math/rev/mat/vectorize/expect_errors.hpp | 3 -- .../rev/mat/vectorize/expect_matrix_value.hpp | 52 +++++++++---------- .../mat/vectorize/expect_row_vector_value.hpp | 28 +++++----- .../rev/mat/vectorize/expect_scalar_value.hpp | 9 ++-- .../mat/vectorize/expect_std_vector_value.hpp | 29 ++++------- ...{expect_eq.hpp => expect_val_deriv_eq.hpp} | 12 ++--- .../math/rev/mat/vectorize/expect_values.hpp | 1 + .../rev/mat/vectorize/expect_vector_value.hpp | 26 +++++----- 27 files changed, 213 insertions(+), 253 deletions(-) rename test/unit/math/fwd/mat/vectorize/{expect_eq.hpp => expect_val_deriv_eq.hpp} (50%) delete mode 100644 test/unit/math/mix/mat/vectorize/expect_eq.hpp create mode 100644 test/unit/math/mix/mat/vectorize/expect_val_deriv_eq.hpp rename test/unit/math/rev/mat/vectorize/{expect_eq.hpp => expect_val_deriv_eq.hpp} (57%) diff --git a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp index f4647d0799b..59c6b2727a3 100644 --- a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp @@ -3,25 +3,23 @@ #include #include +#include #include -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_matrix(const T& x, int seed_index = -1) { +template +static inline Eigen::Matrix +build_matrix(const Eigen::Matrix& x, int seed_index = -1) { + using Eigen::Matrix; + using std::vector; - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); + Eigen::Matrix fvar_matrix(x.rows(), x.cols()); size_t num_inputs = F::valid_inputs().size(); for (int i = 0; i < x.size(); ++i) { - std::vector inputs; + std::vector inputs; if (seed_index == i) - inputs = build_vector(std::vector(), - (seed_index % num_inputs)); + inputs = build_vector(std::vector(), (seed_index % num_inputs)); else - inputs = build_vector(std::vector()); + inputs = build_vector(std::vector()); fvar_matrix(i) = inputs[(i % num_inputs)]; } return fvar_matrix; diff --git a/test/unit/math/fwd/mat/vectorize/build_vector.hpp b/test/unit/math/fwd/mat/vectorize/build_vector.hpp index f3725993543..4ba6d2c9590 100644 --- a/test/unit/math/fwd/mat/vectorize/build_vector.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_vector.hpp @@ -18,18 +18,16 @@ build_vector(std::vector > fvar_vector, using std::vector; using stan::math::fvar; - vector template_vector - = build_vector(vector(), seed_index); + vector template_v = build_vector(vector(), seed_index); - for (size_t i = 0; i < template_vector.size(); ++i) { + for (size_t i = 0; i < template_v.size(); ++i) { // For fvar >, this will fill in // all four components if (seed_index == static_cast(i)) - fvar_vector - .push_back(fvar(template_vector[i], template_vector[i])); + fvar_vector.push_back(fvar(template_v[i], template_v[i])); else - fvar_vector.push_back(fvar(template_vector[i])); + fvar_vector.push_back(fvar(template_v[i])); } return fvar_vector; } diff --git a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp index 7c5465e87a8..db5cf81c79b 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_matrix_value() { @@ -15,37 +15,35 @@ void expect_matrix_value() { int num_inputs = F::valid_inputs().size(); int num_cols = 3; - matrix_t template_matrix(num_inputs, num_cols); + matrix_t template_m(num_inputs, num_cols); - for (int i = 0; i < template_matrix.size(); ++i) { - matrix_t a = build_matrix(template_matrix, i); + for (int i = 0; i < template_m.size(); ++i) { + matrix_t a = build_matrix(template_m, i); matrix_t fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); - expect_eq(F::apply_base(a(i)), fa(i)); + expect_val_deriv_eq(F::apply_base(a(i)), fa(i)); } size_t vector_matrix_size = 2; for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { + for (int j = 0; j < template_m.size(); ++j) { vector b; for (size_t k = 0; k < vector_matrix_size; ++k) if (k == i) - b.push_back(build_matrix(template_matrix, j)); + b.push_back(build_matrix(template_m, j)); else - b.push_back(build_matrix(template_matrix)); + b.push_back(build_matrix(template_m)); vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_eq(F::apply_base(b[i](j)), fb[i](j)); + expect_val_deriv_eq(F::apply_base(b[i](j)), fb[i](j)); } } - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - matrix_t a = build_matrix(template_matrix, seed_i); - matrix_t fab = foo(a.block(block_i, block_j, 1, 1)); - expect_eq(F::apply_base(a(1,1)), fab(0,0)); + int seed_i = num_inputs + 1; + matrix_t a = build_matrix(template_m, seed_i); + matrix_t fab = foo(a.block(1, 1, 1, 1)); + expect_val_deriv_eq(F::apply_base(a(1,1)), fab(0,0)); } #endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp index 952e1b9ccf9..dabaef51517 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -14,13 +14,13 @@ void expect_row_vector_value() { typedef Eigen::Matrix row_vector_t; size_t num_inputs = F::valid_inputs().size(); - row_vector_t template_vector(num_inputs); + row_vector_t template_rv(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - row_vector_t b = build_matrix(template_vector, i); + row_vector_t b = build_matrix(template_rv, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_eq(F::apply_base(b(i)), fb(i)); + expect_val_deriv_eq(F::apply_base(b(i)), fb(i)); } size_t vector_vector_size = 2; @@ -29,15 +29,13 @@ void expect_row_vector_value() { vector c; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) - c.push_back(build_matrix(template_vector, j)); + c.push_back(build_matrix(template_rv, j)); else - c.push_back(build_matrix(template_vector)); - vector fc = - F::template apply >(c); - + c.push_back(build_matrix(template_rv)); + vector fc = F::template apply >(c); EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); - expect_eq(F::apply_base(c[i](j)), fc[i](j)); + expect_val_deriv_eq(F::apply_base(c[i](j)), fc[i](j)); } } } diff --git a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp index 1b6c75e9c73..8595f69b16c 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_scalar_value() { @@ -14,7 +14,7 @@ void expect_scalar_value() { build_vector(vector(), i); T fy = F::template apply(y[i]); T exp_y = F::apply_base(y[i]); - expect_eq(exp_y, fy); + expect_val_deriv_eq(exp_y, fy); } } diff --git a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp index 303ffa46157..d9a8f8fe79e 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp @@ -4,24 +4,23 @@ #include #include #include -#include +#include template void expect_std_vector_value() { using std::vector; + size_t num_inputs = F::valid_inputs().size(); for (size_t i = 0; i < num_inputs; ++i) { vector y = build_vector(vector(), i); vector fy = F::template apply >(y); EXPECT_EQ(y.size(), fy.size()); - expect_eq(F::apply_base(y[i]), fy[i]); + expect_val_deriv_eq(F::apply_base(y[i]), fy[i]); } size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { - vector > z; for (size_t k = 0; k < num_inputs; ++k) { if (i == k) @@ -31,10 +30,9 @@ void expect_std_vector_value() { } vector > fz = F::template apply > >(z); - EXPECT_EQ(z.size(), fz.size()); EXPECT_EQ(z[i].size(), fz[i].size()); - expect_eq(F::apply_base(z[i][j]), fz[i][j]); + expect_val_deriv_eq(F::apply_base(z[i][j]), fz[i][j]); } } } diff --git a/test/unit/math/fwd/mat/vectorize/expect_eq.hpp b/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp similarity index 50% rename from test/unit/math/fwd/mat/vectorize/expect_eq.hpp rename to test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp index a61a95232d9..db1521a3233 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_eq.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp @@ -4,14 +4,14 @@ #include #include -static inline void expect_eq(double exp_var, double test_var) { +static inline void expect_val_deriv_eq(double exp_var, double test_var) { EXPECT_FLOAT_EQ(exp_var, test_var); } template -static inline void expect_eq(T exp_var, T test_var) { - expect_eq(exp_var.val(), test_var.val()); - expect_eq(exp_var.d_, test_var.d_); +static inline void expect_val_deriv_eq(T exp_var, T test_var) { + expect_val_deriv_eq(exp_var.val(), test_var.val()); + expect_val_deriv_eq(exp_var.d_, test_var.d_); } #endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp index 57a44aeb1a9..9cd2cb8f9f9 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_values.hpp @@ -8,6 +8,7 @@ #include #include +//Also tests derivatives template void expect_values() { using stan::math::fvar; diff --git a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp index 89bb8eb9f4e..0eb07b09ee7 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -14,13 +14,13 @@ void expect_vector_value() { typedef Eigen::Matrix vector_t; size_t num_inputs = F::valid_inputs().size(); - vector_t template_vector(num_inputs); + vector_t template_v(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - vector_t b = build_matrix(template_vector, i); + vector_t b = build_matrix(template_v, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_eq(F::apply_base(b(i)), fb(i)); + expect_val_deriv_eq(F::apply_base(b(i)), fb(i)); } size_t vector_vector_size = 2; @@ -29,14 +29,13 @@ void expect_vector_value() { vector c; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) - c.push_back(build_matrix(template_vector, j)); + c.push_back(build_matrix(template_v, j)); else - c.push_back(build_matrix(template_vector)); + c.push_back(build_matrix(template_v)); vector fc = F::template apply >(c); - EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); - expect_eq(F::apply_base(c[i](j)), fc[i](j)); + expect_val_deriv_eq(F::apply_base(c[i](j)), fc[i](j)); } } } diff --git a/test/unit/math/mix/mat/vectorize/build_matrix.hpp b/test/unit/math/mix/mat/vectorize/build_matrix.hpp index 00d7f2bd041..90b309687af 100644 --- a/test/unit/math/mix/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/mix/mat/vectorize/build_matrix.hpp @@ -6,24 +6,21 @@ #include #include -template -static inline Eigen::Matrix::Scalar, - T::RowsAtCompileTime, - T::ColsAtCompileTime> -build_matrix(const T& x, int seed_index = -1) { +template +static inline Eigen::Matrix +build_matrix(const Eigen::Matrix& x, int seed_index = -1) { + using Eigen::Matrix; + using std::vector; - typedef typename Eigen::internal::traits::Scalar fvar_type; - Eigen::Matrix - fvar_matrix(x.rows(), x.cols()); + Matrix fvar_matrix(x.rows(), x.cols()); size_t num_inputs = F::valid_inputs().size(); for (int i = 0; i < x.size(); ++i) { - std::vector inputs; + vector inputs; if (seed_index == i) - inputs = build_vector(std::vector(), - (seed_index % num_inputs)); + inputs = build_vector(vector(), seed_index % num_inputs); else - inputs = build_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; + inputs = build_vector(vector()); + fvar_matrix(i) = inputs[i % num_inputs]; } return fvar_matrix; } diff --git a/test/unit/math/mix/mat/vectorize/expect_eq.hpp b/test/unit/math/mix/mat/vectorize/expect_eq.hpp deleted file mode 100644 index 7d70861a505..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_eq.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_EQ_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_EQ_HPP - -#include -#include -#include - -template -static inline void expect_eq(V exp_var, V base_exp_var, - V test_var, V base_test_var) { - expect_eq(exp_var.val(), base_exp_var.val(), - test_var.val(), base_test_var.val()); - expect_eq(exp_var.d_, base_exp_var.d_, - test_var.d_, base_test_var.d_); -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp index e83c6fe891f..89cfd538429 100644 --- a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp @@ -4,57 +4,52 @@ #include #include #include -#include +#include template void expect_matrix_value() { using std::vector; - typedef - Eigen::Matrix matrix_t; + typedef Eigen::Matrix matrix_t; size_t num_inputs = F::valid_inputs().size(); size_t num_cols = 3; - matrix_t template_matrix(num_inputs, num_cols); + matrix_t template_m(num_inputs, num_cols); - for (int i = 0; i < template_matrix.size(); ++i) { - matrix_t y = build_matrix(template_matrix, i); - matrix_t z = build_matrix(template_matrix, i); + for (int i = 0; i < template_m.size(); ++i) { + matrix_t y = build_matrix(template_m, i); + matrix_t z = build_matrix(template_m, i); matrix_t fz = F::template apply(z); EXPECT_EQ(z.size(), fz.size()); - expect_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); + expect_val_deriv_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); } size_t vector_matrix_size = 2; for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { + for (int j = 0; j < template_m.size(); ++j) { vector a; vector b; for (size_t k = 0; k < vector_matrix_size; ++k) { if (k == i) { - a.push_back(build_matrix(template_matrix, j)); - b.push_back(build_matrix(template_matrix, j)); + a.push_back(build_matrix(template_m, j)); + b.push_back(build_matrix(template_m, j)); } else { - a.push_back(build_matrix(template_matrix)); - b.push_back(build_matrix(template_matrix)); + a.push_back(build_matrix(template_m)); + b.push_back(build_matrix(template_m)); } } vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_eq( - F::apply_base(a[i](j)), a[i](j), fb[i](j), b[i](j)); + expect_val_deriv_eq(F::apply_base(a[i](j)), a[i](j), + fb[i](j), b[i](j)); } } - int block_i = 1; - int block_j = 1; - int seed_i = block_j * num_inputs + block_i; - matrix_t c = build_matrix(template_matrix, seed_i); - matrix_t d = build_matrix(template_matrix, seed_i); - matrix_t fab = foo(d.block(block_i, block_j, 1, 1)); - expect_eq( - F::apply_base(c(block_i, block_j)), c(block_i, block_j), - fab(0,0), d(block_i, block_j)); + int seed_i = num_inputs + 1; + matrix_t c = build_matrix(template_m, seed_i); + matrix_t d = build_matrix(template_m, seed_i); + matrix_t fab = foo(d.block(1, 1, 1, 1)); + expect_val_deriv_eq(F::apply_base(c(1, 1)), c(1, 1), fab(0, 0), d(1, 1)); } #endif diff --git a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp index ac345668c2e..eab58100306 100644 --- a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -12,14 +12,14 @@ void expect_row_vector_value() { typedef Eigen::Matrix row_vector_t; size_t num_inputs = F::valid_inputs().size(); - row_vector_t template_vector(num_inputs); + row_vector_t template_rv(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - row_vector_t a = build_matrix(template_vector, i); - row_vector_t b = build_matrix(template_vector, i); + row_vector_t a = build_matrix(template_rv, i); + row_vector_t b = build_matrix(template_rv, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); } size_t vector_vector_size = 2; @@ -29,20 +29,19 @@ void expect_row_vector_value() { vector d; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) { - c.push_back(build_matrix(template_vector, j)); - d.push_back(build_matrix(template_vector, j)); + c.push_back(build_matrix(template_rv, j)); + d.push_back(build_matrix(template_rv, j)); } else { - c.push_back(build_matrix(template_vector)); - d.push_back(build_matrix(template_vector)); + c.push_back(build_matrix(template_rv)); + d.push_back(build_matrix(template_rv)); } vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); EXPECT_EQ(d[i].size(), fd[i].size()); - expect_eq( - F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + expect_val_deriv_eq(F::apply_base(c[i](j)), c[i](j), + fd[i](j), d[i](j)); } } } diff --git a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp index b0bd4baee4f..370123bcbb0 100644 --- a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp @@ -1,9 +1,9 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_TECTORIZE_EXPECT_SCALAR_TALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_TECTORIZE_EXPECT_SCALAR_TALUE_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP #include #include -#include +#include template void expect_scalar_value() { @@ -12,7 +12,7 @@ void expect_scalar_value() { vector y = build_vector(vector(), i); vector z = build_vector(vector(), i); T fz = F::template apply(z[i]); - expect_eq(F::apply_base(y[i]), y[i], fz, z[i]); + expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz, z[i]); } } diff --git a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp index c907ff4eb2e..0af283809bd 100644 --- a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp @@ -1,22 +1,21 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_TECTORIZE_STD_TECTOR_TALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_TECTORIZE_STD_TECTOR_TALUE_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_STD_VECTOR_VALUE_HPP #include #include -#include +#include template void expect_std_vector_value() { using std::vector; size_t num_inputs = F::valid_inputs().size(); - for (size_t i = 0; i < num_inputs; ++i) { vector y = build_vector(vector(), i); vector z = build_vector(vector(), i); vector fz = F::template apply >(z); EXPECT_EQ(z.size(), fz.size()); - expect_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); + expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); } size_t vector_vector_size = 2; @@ -34,13 +33,11 @@ void expect_std_vector_value() { b.push_back(build_vector(vector())); } } - vector > fb - = F::template apply > >(b); - + vector > fb = F::template apply > >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); - expect_eq( - F::apply_base(a[i][j]), a[i][j], fb[i][j], b[i][j]); + expect_val_deriv_eq(F::apply_base(a[i][j]), a[i][j], + fb[i][j], b[i][j]); } } } diff --git a/test/unit/math/mix/mat/vectorize/expect_val_deriv_eq.hpp b/test/unit/math/mix/mat/vectorize/expect_val_deriv_eq.hpp new file mode 100644 index 00000000000..30c0bba06cc --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_val_deriv_eq.hpp @@ -0,0 +1,17 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VAL_DERIV_EQ_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VAL_DERIV_EQ_HPP + +#include +#include +#include + +template +static inline void expect_val_deriv_eq(V exp_var, V base_exp_var, + V test_var, V base_test_var) { + expect_val_deriv_eq(exp_var.val(), base_exp_var.val(), + test_var.val(), base_test_var.val()); + expect_val_deriv_eq(exp_var.d_, base_exp_var.d_, + test_var.d_, base_test_var.d_); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_values.hpp b/test/unit/math/mix/mat/vectorize/expect_values.hpp index 8cd8dbbecf0..0dbe5a0d31b 100644 --- a/test/unit/math/mix/mat/vectorize/expect_values.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_values.hpp @@ -9,6 +9,7 @@ #include #include +//Also will test derivatives template void expect_values() { using stan::math::fvar; diff --git a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp index 9746a769634..713f413f523 100644 --- a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -12,14 +12,14 @@ void expect_vector_value() { typedef Eigen::Matrix vector_t; size_t num_inputs = F::valid_inputs().size(); - vector_t template_vector(num_inputs); + vector_t template_v(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - vector_t a = build_matrix(template_vector, i); - vector_t b = build_matrix(template_vector, i); + vector_t a = build_matrix(template_v, i); + vector_t b = build_matrix(template_v, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); - expect_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); + expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); } size_t vector_vector_size = 2; @@ -29,19 +29,18 @@ void expect_vector_value() { vector d; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) { - c.push_back(build_matrix(template_vector, j)); - d.push_back(build_matrix(template_vector, j)); + c.push_back(build_matrix(template_v, j)); + d.push_back(build_matrix(template_v, j)); } else { - c.push_back(build_matrix(template_vector)); - d.push_back(build_matrix(template_vector)); + c.push_back(build_matrix(template_v)); + d.push_back(build_matrix(template_v)); } vector fd = F::template apply >(d); - EXPECT_EQ(d.size(), fd.size()); EXPECT_EQ(d[i].size(), fd[i].size()); - expect_eq( - F::apply_base(c[i](j)), c[i](j), fd[i](j), d[i](j)); + expect_val_deriv_eq(F::apply_base(c[i](j)), c[i](j), + fd[i](j), d[i](j)); } } } diff --git a/test/unit/math/rev/mat/vectorize/build_matrix.hpp b/test/unit/math/rev/mat/vectorize/build_matrix.hpp index 8bc77de590d..7759ee929aa 100644 --- a/test/unit/math/rev/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/rev/mat/vectorize/build_matrix.hpp @@ -5,15 +5,17 @@ #include #include -template -static inline Eigen::Matrix -build_matrix(const T& x) { - Eigen::Matrix var_matrix(x.rows(), x.cols()); - std::vector inputs = F::valid_inputs(); +template +static inline Eigen::Matrix +build_matrix(const Eigen::Matrix& x) { + using Eigen::Matrix; + using std::vector; + using stan::math::var; + + Matrix var_matrix(x.rows(), x.cols()); + vector inputs = F::valid_inputs(); for (int i = 0; i < x.size(); ++i) { - var_matrix(i) = inputs[(i % inputs.size())]; + var_matrix(i) = inputs[i % inputs.size()]; } return var_matrix; } diff --git a/test/unit/math/rev/mat/vectorize/expect_errors.hpp b/test/unit/math/rev/mat/vectorize/expect_errors.hpp index 501f90352d5..0e24bb6f3f5 100644 --- a/test/unit/math/rev/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_errors.hpp @@ -8,9 +8,6 @@ #include #include - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example template void expect_errors() { using stan::math::var; diff --git a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp index 6bc8d69f709..64250021c9b 100644 --- a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_matrix_value() { @@ -16,42 +16,38 @@ void expect_matrix_value() { size_t num_cols = 3; size_t num_inputs = F::valid_inputs().size(); - MatrixXvar template_matrix(num_inputs, num_cols); - - for (int i = 0; i < template_matrix.size(); ++i) { - MatrixXvar a = build_matrix(template_matrix); - MatrixXvar fa = F::template apply(a); - EXPECT_EQ(a.size(), fa.size()); - EXPECT_FLOAT_EQ(F::apply_base(a(i)).val(), fa(i).val()); - - MatrixXvar b = build_matrix(template_matrix); - expect_eq(F::apply_base(b(i)), b(i), fa(i), a(i)); + MatrixXvar template_m(num_inputs, num_cols); + + for (int i = 0; i < template_m.size(); ++i) { + MatrixXvar a = build_matrix(template_m); + MatrixXvar b = build_matrix(template_m); + MatrixXvar fb = F::template apply(b); + EXPECT_EQ(b.size(), fb.size()); + expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); } size_t vector_matrix_size = 2; for (size_t i = 0; i < vector_matrix_size; ++i) { - for (int j = 0; j < template_matrix.size(); ++j) { - + for (int j = 0; j < template_m.size(); ++j) { vector c; - for (size_t k = 0; k < vector_matrix_size; ++k) - c.push_back(build_matrix(template_matrix)); - vector fc = F::template apply >(c); - - EXPECT_EQ(c[i].size(), fc[i].size()); - EXPECT_EQ(c[i].rows(), fc[i].rows()); - EXPECT_EQ(c[i].cols(), fc[i].cols()); - vector d; - for (size_t k = 0; k < vector_matrix_size; ++k) - d.push_back(build_matrix(template_matrix)); - expect_eq(F::apply_base(d[i](j)), d[i](j), fc[i](j), c[i](j)); + for (size_t k = 0; k < vector_matrix_size; ++k) { + c.push_back(build_matrix(template_m)); + d.push_back(build_matrix(template_m)); + } + vector fd = F::template apply >(d); + EXPECT_EQ(d[i].size(), fd[i].size()); + EXPECT_EQ(d[i].rows(), fd[i].rows()); + EXPECT_EQ(d[i].cols(), fd[i].cols()); + expect_val_deriv_eq(F::apply_base(c[i](j)), c[i](j), + fd[i](j), d[i](j)); } } - MatrixXvar e = build_matrix(template_matrix); - MatrixXvar feb = F::template apply(e.block(1, 1, 1, 1)); - MatrixXvar f = build_matrix(template_matrix).block(1, 1, 1, 1); - expect_eq(F::apply_base(f(0,0)), f(0,0), feb(0,0), e(1,1)); + MatrixXvar e = build_matrix(template_m).block(1, 1, 1, 1); + MatrixXvar f = build_matrix(template_m); + MatrixXvar ffb = F::template apply(f.block(1, 1, 1, 1)); + expect_val_deriv_eq(F::apply_base(e(0,0)), e(0,0), ffb(0,0), f(1,1)); } #endif diff --git a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp index d718c28077d..128a4a07bc7 100644 --- a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_row_vector_value() { @@ -19,29 +19,27 @@ void expect_row_vector_value() { for (size_t i = 0; i < num_inputs; ++i) { RowVectorXvar b = build_matrix(template_vector); - RowVectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - RowVectorXvar c = build_matrix(template_vector); - expect_eq(F::apply_base(c(i)), c(i), fb(i), b(i)); + RowVectorXvar fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + expect_val_deriv_eq(F::apply_base(b(i)), b(i), fc(i), c(i)); } size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { vector d; - for (size_t k = 0; k < num_inputs; ++k) - d.push_back(build_matrix(template_vector)); - vector fd = - F::template apply >(d); - EXPECT_EQ(d[i].size(), fd[i].size()); - EXPECT_EQ(d.size(), fd.size()); - vector e; - for (size_t k = 0; k < num_inputs; ++k) + for (size_t k = 0; k < num_inputs; ++k) { + d.push_back(build_matrix(template_vector)); e.push_back(build_matrix(template_vector)); - expect_eq(F::apply_base(e[i](j)), e[i](j), fd[i](j), d[i](j)); + } + vector fe = + F::template apply >(e); + EXPECT_EQ(e[i].size(), fe[i].size()); + EXPECT_EQ(e.size(), fe.size()); + expect_val_deriv_eq(F::apply_base(d[i](j)), d[i](j), + fe[i](j), e[i](j)); } } } diff --git a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp index c5d913d23d0..c36c81a077e 100644 --- a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp @@ -5,7 +5,7 @@ #include #include #include -#include +#include template void expect_scalar_value() { @@ -14,12 +14,9 @@ void expect_scalar_value() { for (size_t i = 0; i < F::valid_inputs().size(); ++i) { vector y = build_vector(); - var fy = F::template apply(y[i]); - vector z = build_vector(); - var fz = F::apply_base(z[i]); - - expect_eq(fz, z[i], fy, y[i]); + var fz = F::template apply(z[i]); + expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz, z[i]); } } diff --git a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp index 90930805c90..81c2b158898 100644 --- a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include template void expect_std_vector_value() { @@ -12,34 +12,27 @@ void expect_std_vector_value() { using stan::math::var; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = build_vector(); - vector fy = F::template apply >(y); vector z = build_vector(); - - EXPECT_EQ(y.size(), fy.size()); - expect_eq(F::apply_base(z[i]), z[i], fy[i], y[i]); + vector fz = F::template apply >(z); + EXPECT_EQ(z.size(), fz.size()); + expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); } size_t vector_vector_size = 2; for (size_t i = 0; i < vector_vector_size; ++i) { - for (size_t j = 0; j < F::valid_inputs().size(); ++j) { - vector > a; + vector > b; for (size_t i = 0; i < vector_vector_size; ++i) { a.push_back(build_vector()); - } - vector > fa = - F::template apply > >(a); - EXPECT_EQ(a.size(), fa.size()); - EXPECT_EQ(a[i].size(), fa[i].size()); - - vector > b; - for (size_t i = 0; i < vector_vector_size; ++i) b.push_back(build_vector()); - - expect_eq(F::apply_base(b[i][j]), b[i][j], fa[i][j], a[i][j]); + } + vector > fb = F::template apply > >(b); + EXPECT_EQ(b.size(), fb.size()); + EXPECT_EQ(b[i].size(), fb[i].size()); + expect_val_deriv_eq(F::apply_base(a[i][j]), a[i][j], + fb[i][j], b[i][j]); } } } diff --git a/test/unit/math/rev/mat/vectorize/expect_eq.hpp b/test/unit/math/rev/mat/vectorize/expect_val_deriv_eq.hpp similarity index 57% rename from test/unit/math/rev/mat/vectorize/expect_eq.hpp rename to test/unit/math/rev/mat/vectorize/expect_val_deriv_eq.hpp index abf6dfa3ada..6f995bab066 100644 --- a/test/unit/math/rev/mat/vectorize/expect_eq.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_val_deriv_eq.hpp @@ -1,15 +1,15 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_EQ_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_EQ_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VAL_DERIV_EQ_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VAL_DERIV_EQ_HPP #include #include #include #include -static inline void expect_eq(stan::math::var exp_var, - stan::math::var base_exp_var, - stan::math::var test_var, - stan::math::var base_test_var) { +static inline void expect_val_deriv_eq(stan::math::var exp_var, + stan::math::var base_exp_var, + stan::math::var test_var, + stan::math::var base_test_var) { using stan::math::set_zero_all_adjoints; EXPECT_FLOAT_EQ(exp_var.val(), test_var.val()); AVEC exp_y = createAVEC(base_exp_var); diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp index 73a3c18421d..451a6b72db0 100644 --- a/test/unit/math/rev/mat/vectorize/expect_values.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_values.hpp @@ -7,6 +7,7 @@ #include #include +//Also will test derivatives template void expect_values() { expect_scalar_value(); diff --git a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp index 71344e80589..8f7cc701fea 100644 --- a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include template void expect_vector_value() { @@ -19,28 +19,26 @@ void expect_vector_value() { for (size_t i = 0; i < num_inputs; ++i) { VectorXvar b = build_matrix(template_vector); - VectorXvar fb = F::template apply(b); - EXPECT_EQ(b.size(), fb.size()); - VectorXvar c = build_matrix(template_vector); - expect_eq(F::apply_base(c(i)), c(i), fb(i), b(i)); + VectorXvar fc = F::template apply(c); + EXPECT_EQ(c.size(), fc.size()); + expect_val_deriv_eq(F::apply_base(b(i)), b(i), fc(i), c(i)); } size_t vector_vector_size = 2; - for (size_t i = 0; i < vector_vector_size; ++i) { for (size_t j = 0; j < num_inputs; ++j) { vector d; - for (size_t k = 0; k < num_inputs; ++k) - d.push_back(build_matrix(template_vector)); - vector fd = F::template apply >(d); - EXPECT_EQ(d[i].size(), fd[i].size()); - EXPECT_EQ(d.size(), fd.size()); - vector e; - for (size_t k = 0; k < num_inputs; ++k) + for (size_t k = 0; k < num_inputs; ++k) { + d.push_back(build_matrix(template_vector)); e.push_back(build_matrix(template_vector)); - expect_eq(F::apply_base(e[i](j)), e[i](j), fd[i](j), d[i](j)); + } + vector fe = F::template apply >(e); + EXPECT_EQ(e[i].size(), fe[i].size()); + EXPECT_EQ(e.size(), fe.size()); + expect_val_deriv_eq(F::apply_base(d[i](j)), d[i](j), + fe[i](j), e[i](j)); } } } From 9ff8a3bf472dcb331d1a11b51725683043ff6eff Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Fri, 26 Feb 2016 22:31:05 -0500 Subject: [PATCH 26/36] Updated doc --- test/unit/math/fwd/mat/vectorize/build_matrix.hpp | 5 +++-- test/unit/math/mix/mat/vectorize/build_matrix.hpp | 1 + test/unit/math/rev/mat/vectorize/build_matrix.hpp | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp index 59c6b2727a3..12ab06cb3c2 100644 --- a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_matrix.hpp @@ -14,13 +14,14 @@ build_matrix(const Eigen::Matrix& x, int seed_index = -1) { Eigen::Matrix fvar_matrix(x.rows(), x.cols()); size_t num_inputs = F::valid_inputs().size(); + //Fills matrix with copies of valid_input values for (int i = 0; i < x.size(); ++i) { std::vector inputs; if (seed_index == i) - inputs = build_vector(std::vector(), (seed_index % num_inputs)); + inputs = build_vector(std::vector(), seed_index % num_inputs); else inputs = build_vector(std::vector()); - fvar_matrix(i) = inputs[(i % num_inputs)]; + fvar_matrix(i) = inputs[i % num_inputs]; } return fvar_matrix; } diff --git a/test/unit/math/mix/mat/vectorize/build_matrix.hpp b/test/unit/math/mix/mat/vectorize/build_matrix.hpp index 90b309687af..4709cdc767b 100644 --- a/test/unit/math/mix/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/mix/mat/vectorize/build_matrix.hpp @@ -14,6 +14,7 @@ build_matrix(const Eigen::Matrix& x, int seed_index = -1) { Matrix fvar_matrix(x.rows(), x.cols()); size_t num_inputs = F::valid_inputs().size(); + //Fills matrix with copies of valid_input values for (int i = 0; i < x.size(); ++i) { vector inputs; if (seed_index == i) diff --git a/test/unit/math/rev/mat/vectorize/build_matrix.hpp b/test/unit/math/rev/mat/vectorize/build_matrix.hpp index 7759ee929aa..97e5668b776 100644 --- a/test/unit/math/rev/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/rev/mat/vectorize/build_matrix.hpp @@ -14,9 +14,9 @@ build_matrix(const Eigen::Matrix& x) { Matrix var_matrix(x.rows(), x.cols()); vector inputs = F::valid_inputs(); - for (int i = 0; i < x.size(); ++i) { - var_matrix(i) = inputs[i % inputs.size()]; - } + //Fills matrix with copies of valid_input values + for (int i = 0; i < x.size(); ++i) + var_matrix(i) = inputs[i % inputs.size()]; return var_matrix; } From b599a4a0a5989a7e2a053082e1eb84a3481c3c78 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Sat, 27 Feb 2016 19:01:12 -0500 Subject: [PATCH 27/36] Removed inv files --- stan/math/prim/scal/fun/inv.hpp | 13 +++++-------- test/unit/math/prim/mat/fun/inv_test.cpp | 15 --------------- 2 files changed, 5 insertions(+), 23 deletions(-) delete mode 100644 test/unit/math/prim/mat/fun/inv_test.cpp diff --git a/stan/math/prim/scal/fun/inv.hpp b/stan/math/prim/scal/fun/inv.hpp index e6642619f1a..eb3f0f17acc 100644 --- a/stan/math/prim/scal/fun/inv.hpp +++ b/stan/math/prim/scal/fun/inv.hpp @@ -6,16 +6,13 @@ namespace stan { namespace math { - /** - * Return the inverse of the specified value. The inverse of x is - * defined as 1/x. - * - * @param x Value to invert. - * @return Inverse of value. - */ - inline double inv(double x) { + template + inline + typename boost::math::tools::promote_args::type + inv(const T x) { return 1.0 / x; } + } } diff --git a/test/unit/math/prim/mat/fun/inv_test.cpp b/test/unit/math/prim/mat/fun/inv_test.cpp deleted file mode 100644 index dc682e852d0..00000000000 --- a/test/unit/math/prim/mat/fun/inv_test.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include -#include - -TEST(primMatFun, inv) { - using stan::math::inv; - std::vector x; - x.push_back(2); - x.push_back(4); - std::vector y = inv(x); - EXPECT_EQ(x.size(), y.size()); - EXPECT_FLOAT_EQ(0.5, y[0]); - EXPECT_FLOAT_EQ(0.25, y[1]); - -} From c3aeccd99448c5800c4e9e4b8c38f507a20e4755 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Sun, 28 Feb 2016 11:55:12 -0500 Subject: [PATCH 28/36] Skips error tests if no errors --- .../vectorize/expect_int_std_vector_error.hpp | 18 ++++++------ .../mat/vectorize/expect_matrix_error.hpp | 28 ++++++++++--------- .../mat/vectorize/expect_row_vector_error.hpp | 26 ++++++++--------- .../mat/vectorize/expect_std_vector_error.hpp | 18 ++++++------ .../mat/vectorize/expect_vector_error.hpp | 23 +++++++-------- 5 files changed, 60 insertions(+), 53 deletions(-) diff --git a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp index 144c13f443d..9a589456052 100644 --- a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp @@ -9,14 +9,16 @@ template void expect_int_std_vector_error() { using std::vector; vector invalid_inputs = F::int_invalid_inputs(); - EXPECT_THROW(F::template apply >(invalid_inputs), - std::domain_error); - - vector > z; - z.push_back(invalid_inputs); - z.push_back(invalid_inputs); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); + if (invalid_inputs.size() > 0) { + EXPECT_THROW(F::template apply >(invalid_inputs), + std::domain_error); + + vector > z; + z.push_back(invalid_inputs); + z.push_back(invalid_inputs); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); + } } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp index f1a269ffdaa..c74a3d132a4 100644 --- a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp @@ -11,19 +11,21 @@ void expect_matrix_error() { using std::vector; typedef Eigen::Matrix matrix_t; vector invalid_inputs = F::invalid_inputs(); - matrix_t a(3, invalid_inputs.size()); - for (int i = 0; i < a.rows(); ++i) - for (int j = 0; j < a.cols(); ++j) - a(i, j) = invalid_inputs[j]; - EXPECT_THROW(F::template apply(a), std::domain_error); - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); + if (invalid_inputs.size() > 0) { + matrix_t a(3, invalid_inputs.size()); + for (int i = 0; i < a.rows(); ++i) + for (int j = 0; j < a.cols(); ++j) + a(i, j) = invalid_inputs[j]; + EXPECT_THROW(F::template apply(a), std::domain_error); + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); + } } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp index 5052b5400e6..1e0d0e18dc6 100644 --- a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp @@ -9,19 +9,19 @@ template void expect_row_vector_error() { using std::vector; typedef Eigen::Matrix row_vector_t; - std::vector invalid_inputs = F::invalid_inputs(); - - row_vector_t c = row_vector_t(invalid_inputs.size()); - for (size_t i = 0; i < invalid_inputs.size(); ++i) - c(i) = invalid_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); + if (invalid_inputs.size() > 0) { + row_vector_t c = row_vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + c(i) = invalid_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); + } } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp index 2e59421ac19..ae71830b355 100644 --- a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp @@ -9,14 +9,16 @@ template void expect_std_vector_error() { using std::vector; vector invalid_inputs = F::invalid_inputs(); - vector y(invalid_inputs.begin(), invalid_inputs.end()); - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); + if (invalid_inputs.size() > 0) { + vector y(invalid_inputs.begin(), invalid_inputs.end()); + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); + } } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp index 410eb089bef..93b4c24e6cd 100644 --- a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp @@ -11,17 +11,18 @@ void expect_vector_error() { using std::vector; typedef Eigen::Matrix vector_t; vector invalid_inputs = F::invalid_inputs(); - - vector_t b = vector_t(invalid_inputs.size()); - for (size_t i = 0; i < invalid_inputs.size(); ++i) - b(i) = invalid_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); + if (invalid_inputs.size() > 0) { + vector_t b = vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + b(i) = invalid_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); + } } #endif From 0cb1052d6e137f54fc15b3c64eb2f719a3ddf86e Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Tue, 1 Mar 2016 13:54:26 -0500 Subject: [PATCH 29/36] Removed foo from tests --- .../fwd/mat/vectorize/expect_matrix_value.hpp | 2 +- .../mix/mat/vectorize/expect_matrix_value.hpp | 2 +- .../vectorize/vectorize_test_framework.hpp | 39 ------------------- 3 files changed, 2 insertions(+), 41 deletions(-) delete mode 100644 test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp diff --git a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp index db5cf81c79b..b31e7b4971a 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp @@ -42,7 +42,7 @@ void expect_matrix_value() { int seed_i = num_inputs + 1; matrix_t a = build_matrix(template_m, seed_i); - matrix_t fab = foo(a.block(1, 1, 1, 1)); + matrix_t fab = F::template apply(a.block(1, 1, 1, 1)); expect_val_deriv_eq(F::apply_base(a(1,1)), fab(0,0)); } diff --git a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp index 89cfd538429..6259afaa167 100644 --- a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp @@ -48,7 +48,7 @@ void expect_matrix_value() { int seed_i = num_inputs + 1; matrix_t c = build_matrix(template_m, seed_i); matrix_t d = build_matrix(template_m, seed_i); - matrix_t fab = foo(d.block(1, 1, 1, 1)); + matrix_t fab = F::template apply(d.block(1, 1, 1, 1)); expect_val_deriv_eq(F::apply_base(c(1, 1)), c(1, 1), fab(0, 0), d(1, 1)); } diff --git a/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp b/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp deleted file mode 100644 index 6d5fe8ea940..00000000000 --- a/test/unit/math/prim/mat/vectorize/vectorize_test_framework.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTORIZE_TEST_FRAMEWORK_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_VECTORIZE_TEST_FRAMEWORK_HPP - -#include -#include -#include -#include - - -namespace stan { - namespace test { - - template - struct test_vectorize { - - static inline void test_function { - - using stan::math::foo; - - double exp_val = foo(val); - EXPECT_FLOAT_EQ(std::exp(val), exp_val); - - EXPECT_FLOAT_EQ(std::exp(val), foo(val)); - } - }; - - template - struct test_vectorize { - - static inline void test_function { - - using stan::math::foo; - - - } - }; - } -} -#endif From 56006397aac4b90d2197425e15811cda0caf590d Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Thu, 3 Mar 2016 16:03:57 -0500 Subject: [PATCH 30/36] Prototyping typed test cases --- .../mat/vectorize/apply_scalar_unary_test.cpp | 14 -------- .../mat/vectorize/apply_scalar_unary_test.hpp | 36 +++++++++++++++++++ .../{foo_base_test.hpp => foo_base_test.cpp} | 7 ++++ 3 files changed, 43 insertions(+), 14 deletions(-) delete mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp create mode 100644 test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp rename test/unit/math/prim/mat/vectorize/{foo_base_test.hpp => foo_base_test.cpp} (87%) diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp deleted file mode 100644 index 859173c9d66..00000000000 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.cpp +++ /dev/null @@ -1,14 +0,0 @@ -#include -#include -#include -#include -#include - -// test that returned values are correct and that errors are thrown -// for illegal inputs for foo -TEST(MathPrimMatVectorize, applyScalarUnaryMock) { - expect_int_types(); - expect_types(); - expect_values(); - expect_errors(); -} diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp new file mode 100644 index 00000000000..5a7d171c2c7 --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp @@ -0,0 +1,36 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP + +#include +#include +#include +#include + +template +class prim_scalar_unary_test : public ::testing::Test { +}; + +TYPED_TEST_CASE_P(prim_scalar_unary_test); + +TYPED_TEST_P(prim_scalar_unary_test, expect_int_types) { + expect_int_types(); +} + +TYPED_TEST_P(prim_scalar_unary_test, expect_scalar_types) { + expect_types(); +} + +TYPED_TEST_P(prim_scalar_unary_test, expect_values) { + expect_values(); +} + +TYPED_TEST_P(prim_scalar_unary_test, expect_errors) { + expect_errors(); +} + +REGISTER_TYPED_TEST_CASE_P(prim_scalar_unary_test, + expect_int_types, + expect_scalar_types, + expect_values, + expect_errors); +#endif diff --git a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp b/test/unit/math/prim/mat/vectorize/foo_base_test.cpp similarity index 87% rename from test/unit/math/prim/mat/vectorize/foo_base_test.hpp rename to test/unit/math/prim/mat/vectorize/foo_base_test.cpp index cfc475817a8..1053858d27b 100644 --- a/test/unit/math/prim/mat/vectorize/foo_base_test.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_base_test.cpp @@ -1,6 +1,7 @@ #ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP +#include #include #include @@ -89,4 +90,10 @@ struct foo_base_test { .add(10).add(25).add(100).add(50).build(); } }; + +INSTANTIATE_TYPED_TEST_CASE_P(, prim_scalar_unary_test, foo_base_test); +// INSTANTIATE_TYPED_TEST_CASE_P(, rev_scalar_unary_test, foo_base_test); +// INSTANTIATE_TYPED_TEST_CASE_P(, fwd_scalar_unary_test, foo_base_test); +// INSTANTIATE_TYPED_TEST_CASE_P(, mix_scalar_unary_test, foo_base_test); + #endif From 7d7823878a1be35e5dfb12b71886338507eaf542 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Mon, 7 Mar 2016 17:32:01 -0500 Subject: [PATCH 31/36] Created mix test to call all tests --- .../mat/vectorize/apply_scalar_unary_test.cpp | 17 ---------- ...{build_matrix.hpp => build_fwd_matrix.hpp} | 13 ++++---- ...{build_vector.hpp => build_fwd_vector.hpp} | 10 +++--- ...xpect_errors.hpp => expect_fwd_errors.hpp} | 6 ++-- ..._value.hpp => expect_fwd_matrix_value.hpp} | 16 +++++----- ...ue.hpp => expect_fwd_row_vector_value.hpp} | 14 ++++----- ..._value.hpp => expect_fwd_scalar_value.hpp} | 10 +++--- ...ue.hpp => expect_fwd_std_vector_value.hpp} | 14 ++++----- .../fwd/mat/vectorize/expect_fwd_values.hpp | 28 +++++++++++++++++ ..._value.hpp => expect_fwd_vector_value.hpp} | 14 ++++----- .../math/fwd/mat/vectorize/expect_values.hpp | 28 ----------------- .../mat/vectorize/apply_scalar_unary_test.cpp | 21 ------------- ...{build_matrix.hpp => build_mix_matrix.hpp} | 12 +++---- ...{build_vector.hpp => build_mix_vector.hpp} | 12 +++---- ...xpect_errors.hpp => expect_mix_errors.hpp} | 6 ++-- ..._value.hpp => expect_mix_matrix_value.hpp} | 24 +++++++------- ...ue.hpp => expect_mix_row_vector_value.hpp} | 20 ++++++------ .../mat/vectorize/expect_mix_scalar_value.hpp | 19 ++++++++++++ ...ue.hpp => expect_mix_std_vector_value.hpp} | 16 +++++----- .../mix/mat/vectorize/expect_mix_values.hpp | 31 +++++++++++++++++++ ..._value.hpp => expect_mix_vector_value.hpp} | 20 ++++++------ .../mix/mat/vectorize/expect_scalar_value.hpp | 19 ------------ .../math/mix/mat/vectorize/expect_values.hpp | 31 ------------------- .../mat/vectorize/foo_base_test.cpp | 13 +++++--- .../mat/vectorize/apply_scalar_unary_test.hpp | 8 ++--- ...pect_errors.hpp => expect_prim_errors.hpp} | 6 ++-- ...value.hpp => expect_prim_matrix_value.hpp} | 6 ++-- ...e.hpp => expect_prim_row_vector_value.hpp} | 6 ++-- ...value.hpp => expect_prim_scalar_value.hpp} | 6 ++-- ...e.hpp => expect_prim_std_vector_value.hpp} | 6 ++-- .../prim/mat/vectorize/expect_prim_values.hpp | 22 +++++++++++++ ...value.hpp => expect_prim_vector_value.hpp} | 6 ++-- .../math/prim/mat/vectorize/expect_values.hpp | 22 ------------- .../mat/vectorize/apply_scalar_unary_test.cpp | 18 ----------- ...{build_matrix.hpp => build_rev_matrix.hpp} | 6 ++-- ...{build_vector.hpp => build_rev_vector.hpp} | 6 ++-- ...xpect_errors.hpp => expect_rev_errors.hpp} | 6 ++-- ..._value.hpp => expect_rev_matrix_value.hpp} | 20 ++++++------ ...ue.hpp => expect_rev_row_vector_value.hpp} | 16 +++++----- ..._value.hpp => expect_rev_scalar_value.hpp} | 12 +++---- ...ue.hpp => expect_rev_std_vector_value.hpp} | 16 +++++----- .../rev/mat/vectorize/expect_rev_values.hpp | 20 ++++++++++++ ..._value.hpp => expect_rev_vector_value.hpp} | 16 +++++----- .../math/rev/mat/vectorize/expect_values.hpp | 20 ------------ 44 files changed, 303 insertions(+), 355 deletions(-) delete mode 100644 test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp rename test/unit/math/fwd/mat/vectorize/{build_matrix.hpp => build_fwd_matrix.hpp} (57%) rename test/unit/math/fwd/mat/vectorize/{build_vector.hpp => build_fwd_vector.hpp} (69%) rename test/unit/math/fwd/mat/vectorize/{expect_errors.hpp => expect_fwd_errors.hpp} (85%) rename test/unit/math/fwd/mat/vectorize/{expect_matrix_value.hpp => expect_fwd_matrix_value.hpp} (72%) rename test/unit/math/fwd/mat/vectorize/{expect_row_vector_value.hpp => expect_fwd_row_vector_value.hpp} (71%) rename test/unit/math/fwd/mat/vectorize/{expect_scalar_value.hpp => expect_fwd_scalar_value.hpp} (58%) rename test/unit/math/fwd/mat/vectorize/{expect_std_vector_value.hpp => expect_fwd_std_vector_value.hpp} (68%) create mode 100644 test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp rename test/unit/math/fwd/mat/vectorize/{expect_vector_value.hpp => expect_fwd_vector_value.hpp} (72%) delete mode 100644 test/unit/math/fwd/mat/vectorize/expect_values.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp rename test/unit/math/mix/mat/vectorize/{build_matrix.hpp => build_mix_matrix.hpp} (58%) rename test/unit/math/mix/mat/vectorize/{build_vector.hpp => build_mix_vector.hpp} (72%) rename test/unit/math/mix/mat/vectorize/{expect_errors.hpp => expect_mix_errors.hpp} (86%) rename test/unit/math/mix/mat/vectorize/{expect_matrix_value.hpp => expect_mix_matrix_value.hpp} (65%) rename test/unit/math/mix/mat/vectorize/{expect_row_vector_value.hpp => expect_mix_row_vector_value.hpp} (65%) create mode 100644 test/unit/math/mix/mat/vectorize/expect_mix_scalar_value.hpp rename test/unit/math/mix/mat/vectorize/{expect_std_vector_value.hpp => expect_mix_std_vector_value.hpp} (71%) create mode 100644 test/unit/math/mix/mat/vectorize/expect_mix_values.hpp rename test/unit/math/mix/mat/vectorize/{expect_vector_value.hpp => expect_mix_vector_value.hpp} (65%) delete mode 100644 test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp delete mode 100644 test/unit/math/mix/mat/vectorize/expect_values.hpp rename test/unit/math/{prim => mix}/mat/vectorize/foo_base_test.cpp (82%) rename test/unit/math/prim/mat/vectorize/{expect_errors.hpp => expect_prim_errors.hpp} (88%) rename test/unit/math/prim/mat/vectorize/{expect_matrix_value.hpp => expect_prim_matrix_value.hpp} (85%) rename test/unit/math/prim/mat/vectorize/{expect_row_vector_value.hpp => expect_prim_row_vector_value.hpp} (81%) rename test/unit/math/prim/mat/vectorize/{expect_scalar_value.hpp => expect_prim_scalar_value.hpp} (76%) rename test/unit/math/prim/mat/vectorize/{expect_std_vector_value.hpp => expect_prim_std_vector_value.hpp} (85%) create mode 100644 test/unit/math/prim/mat/vectorize/expect_prim_values.hpp rename test/unit/math/prim/mat/vectorize/{expect_vector_value.hpp => expect_prim_vector_value.hpp} (81%) delete mode 100644 test/unit/math/prim/mat/vectorize/expect_values.hpp delete mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp rename test/unit/math/rev/mat/vectorize/{build_matrix.hpp => build_rev_matrix.hpp} (72%) rename test/unit/math/rev/mat/vectorize/{build_vector.hpp => build_rev_vector.hpp} (65%) rename test/unit/math/rev/mat/vectorize/{expect_errors.hpp => expect_rev_errors.hpp} (80%) rename test/unit/math/rev/mat/vectorize/{expect_matrix_value.hpp => expect_rev_matrix_value.hpp} (70%) rename test/unit/math/rev/mat/vectorize/{expect_row_vector_value.hpp => expect_rev_row_vector_value.hpp} (70%) rename test/unit/math/rev/mat/vectorize/{expect_scalar_value.hpp => expect_rev_scalar_value.hpp} (55%) rename test/unit/math/rev/mat/vectorize/{expect_std_vector_value.hpp => expect_rev_std_vector_value.hpp} (69%) create mode 100644 test/unit/math/rev/mat/vectorize/expect_rev_values.hpp rename test/unit/math/rev/mat/vectorize/{expect_vector_value.hpp => expect_rev_vector_value.hpp} (70%) delete mode 100644 test/unit/math/rev/mat/vectorize/expect_values.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp deleted file mode 100644 index ba8b722e76f..00000000000 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -// this tests that the expect_values test works on a mock function -TEST(MathFwdMatVectorize, applyScalarUnaryMock) { - using stan::math::fvar; - expect_types >(); - expect_types > >(); - expect_values(); - expect_errors(); -} diff --git a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp b/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp similarity index 57% rename from test/unit/math/fwd/mat/vectorize/build_matrix.hpp rename to test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp index 12ab06cb3c2..fb43d568af4 100644 --- a/test/unit/math/fwd/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp @@ -1,14 +1,14 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_MATRIX_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_MATRIX_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_MATRIX_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_MATRIX_HPP #include #include #include -#include +#include template static inline Eigen::Matrix -build_matrix(const Eigen::Matrix& x, int seed_index = -1) { +build_fwd_matrix(const Eigen::Matrix& x, int seed_index = -1) { using Eigen::Matrix; using std::vector; @@ -18,9 +18,10 @@ build_matrix(const Eigen::Matrix& x, int seed_index = -1) { for (int i = 0; i < x.size(); ++i) { std::vector inputs; if (seed_index == i) - inputs = build_vector(std::vector(), seed_index % num_inputs); + inputs + = build_fwd_vector(std::vector(), seed_index % num_inputs); else - inputs = build_vector(std::vector()); + inputs = build_fwd_vector(std::vector()); fvar_matrix(i) = inputs[i % num_inputs]; } return fvar_matrix; diff --git a/test/unit/math/fwd/mat/vectorize/build_vector.hpp b/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp similarity index 69% rename from test/unit/math/fwd/mat/vectorize/build_vector.hpp rename to test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp index 4ba6d2c9590..36a8391f477 100644 --- a/test/unit/math/fwd/mat/vectorize/build_vector.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp @@ -1,24 +1,24 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_TECTORIZE_BUILD_TECTOR_HPP -#define TEST_UNIT_MATH_FWD_MAT_TECTORIZE_BUILD_TECTOR_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_VECTOR_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_VECTOR_HPP #include #include template static inline std::vector -build_vector(std::vector double_vector, +build_fwd_vector(std::vector double_vector, int seed_index = -1) { return F::valid_inputs(); } template static inline std::vector > -build_vector(std::vector > fvar_vector, +build_fwd_vector(std::vector > fvar_vector, int seed_index = -1) { using std::vector; using stan::math::fvar; - vector template_v = build_vector(vector(), seed_index); + vector template_v = build_fwd_vector(vector(), seed_index); for (size_t i = 0; i < template_v.size(); ++i) { diff --git a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp similarity index 85% rename from test/unit/math/fwd/mat/vectorize/expect_errors.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp index ba08c519744..e5bfe2cfca2 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ERRORS_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ERRORS_HPP #include #include @@ -9,7 +9,7 @@ #include template -void expect_errors() { +void expect_fwd_errors() { using stan::math::fvar; expect_scalar_error >(); expect_scalar_error > >(); diff --git a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp similarity index 72% rename from test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp index b31e7b4971a..91829249733 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp @@ -1,14 +1,14 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_MATRIX_VALUE_HPP #include #include #include -#include +#include #include template -void expect_matrix_value() { +void expect_fwd_matrix_value() { using stan::math::fvar; using std::vector; typedef Eigen::Matrix matrix_t; @@ -18,7 +18,7 @@ void expect_matrix_value() { matrix_t template_m(num_inputs, num_cols); for (int i = 0; i < template_m.size(); ++i) { - matrix_t a = build_matrix(template_m, i); + matrix_t a = build_fwd_matrix(template_m, i); matrix_t fa = F::template apply(a); EXPECT_EQ(a.size(), fa.size()); expect_val_deriv_eq(F::apply_base(a(i)), fa(i)); @@ -30,9 +30,9 @@ void expect_matrix_value() { vector b; for (size_t k = 0; k < vector_matrix_size; ++k) if (k == i) - b.push_back(build_matrix(template_m, j)); + b.push_back(build_fwd_matrix(template_m, j)); else - b.push_back(build_matrix(template_m)); + b.push_back(build_fwd_matrix(template_m)); vector fb = F::template apply >(b); EXPECT_EQ(b.size(), fb.size()); EXPECT_EQ(b[i].size(), fb[i].size()); @@ -41,7 +41,7 @@ void expect_matrix_value() { } int seed_i = num_inputs + 1; - matrix_t a = build_matrix(template_m, seed_i); + matrix_t a = build_fwd_matrix(template_m, seed_i); matrix_t fab = F::template apply(a.block(1, 1, 1, 1)); expect_val_deriv_eq(F::apply_base(a(1,1)), fab(0,0)); } diff --git a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp similarity index 71% rename from test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp index dabaef51517..e7decff3cff 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp @@ -1,14 +1,14 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ROW_VECTOR_VALUE_HPP #include #include #include -#include +#include #include template -void expect_row_vector_value() { +void expect_fwd_row_vector_value() { using stan::math::fvar; using std::vector; typedef Eigen::Matrix row_vector_t; @@ -17,7 +17,7 @@ void expect_row_vector_value() { row_vector_t template_rv(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - row_vector_t b = build_matrix(template_rv, i); + row_vector_t b = build_fwd_matrix(template_rv, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); expect_val_deriv_eq(F::apply_base(b(i)), fb(i)); @@ -29,9 +29,9 @@ void expect_row_vector_value() { vector c; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) - c.push_back(build_matrix(template_rv, j)); + c.push_back(build_fwd_matrix(template_rv, j)); else - c.push_back(build_matrix(template_rv)); + c.push_back(build_fwd_matrix(template_rv)); vector fc = F::template apply >(c); EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); diff --git a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp similarity index 58% rename from test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp index 8595f69b16c..005faccece9 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp @@ -1,17 +1,17 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_SCALAR_VALUE_HPP #include #include -#include +#include #include template -void expect_scalar_value() { +void expect_fwd_scalar_value() { using std::vector; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { vector y = - build_vector(vector(), i); + build_fwd_vector(vector(), i); T fy = F::template apply(y[i]); T exp_y = F::apply_base(y[i]); expect_val_deriv_eq(exp_y, fy); diff --git a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp similarity index 68% rename from test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp index d9a8f8fe79e..cdd30677d2a 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp @@ -1,18 +1,18 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_STD_VECTOR_VALUE_HPP #include #include -#include +#include #include template -void expect_std_vector_value() { +void expect_fwd_std_vector_value() { using std::vector; size_t num_inputs = F::valid_inputs().size(); for (size_t i = 0; i < num_inputs; ++i) { - vector y = build_vector(vector(), i); + vector y = build_fwd_vector(vector(), i); vector fy = F::template apply >(y); EXPECT_EQ(y.size(), fy.size()); expect_val_deriv_eq(F::apply_base(y[i]), fy[i]); @@ -24,9 +24,9 @@ void expect_std_vector_value() { vector > z; for (size_t k = 0; k < num_inputs; ++k) { if (i == k) - z.push_back(build_vector(vector(), j)); + z.push_back(build_fwd_vector(vector(), j)); else - z.push_back(build_vector(vector())); + z.push_back(build_fwd_vector(vector())); } vector > fz = F::template apply > >(z); diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp new file mode 100644 index 00000000000..2138e25056c --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp @@ -0,0 +1,28 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VALUES_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VALUES_HPP + +#include +#include +#include +#include +#include +#include + +//Also tests derivatives +template +void expect_fwd_values() { + using stan::math::fvar; + + expect_fwd_scalar_value >(); + expect_fwd_scalar_value > >(); + expect_fwd_std_vector_value >(); + expect_fwd_std_vector_value > >(); + expect_fwd_matrix_value >(); + expect_fwd_matrix_value > >(); + expect_fwd_vector_value >(); + expect_fwd_vector_value > >(); + expect_fwd_row_vector_value >(); + expect_fwd_row_vector_value > >(); +} + +#endif diff --git a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp similarity index 72% rename from test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp rename to test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp index 0eb07b09ee7..94b4a203124 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp @@ -1,14 +1,14 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VECTOR_VALUE_HPP #include #include #include -#include +#include #include template -void expect_vector_value() { +void expect_fwd_vector_value() { using stan::math::fvar; using std::vector; typedef Eigen::Matrix vector_t; @@ -17,7 +17,7 @@ void expect_vector_value() { vector_t template_v(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - vector_t b = build_matrix(template_v, i); + vector_t b = build_fwd_matrix(template_v, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); expect_val_deriv_eq(F::apply_base(b(i)), fb(i)); @@ -29,9 +29,9 @@ void expect_vector_value() { vector c; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) - c.push_back(build_matrix(template_v, j)); + c.push_back(build_fwd_matrix(template_v, j)); else - c.push_back(build_matrix(template_v)); + c.push_back(build_fwd_matrix(template_v)); vector fc = F::template apply >(c); EXPECT_EQ(c.size(), fc.size()); EXPECT_EQ(c[i].size(), fc[i].size()); diff --git a/test/unit/math/fwd/mat/vectorize/expect_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_values.hpp deleted file mode 100644 index 9cd2cb8f9f9..00000000000 --- a/test/unit/math/fwd/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include - -//Also tests derivatives -template -void expect_values() { - using stan::math::fvar; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_value >(); - expect_std_vector_value > >(); - expect_matrix_value >(); - expect_matrix_value > >(); - expect_vector_value >(); - expect_vector_value > >(); - expect_row_vector_value >(); - expect_row_vector_value > >(); -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp deleted file mode 100644 index dd87e15617d..00000000000 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -TEST(MathFwdMatVectorize, applyScalarUnaryMock) { - using stan::math::fvar; - using stan::math::var; - - expect_types >(); - expect_types > >(); - expect_values(); - expect_errors(); -} - diff --git a/test/unit/math/mix/mat/vectorize/build_matrix.hpp b/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp similarity index 58% rename from test/unit/math/mix/mat/vectorize/build_matrix.hpp rename to test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp index 4709cdc767b..fc08d4bb9cc 100644 --- a/test/unit/math/mix/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp @@ -1,14 +1,14 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MATRIX_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MATRIX_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_MATRIX_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_MATRIX_HPP #include #include #include -#include +#include template static inline Eigen::Matrix -build_matrix(const Eigen::Matrix& x, int seed_index = -1) { +build_mix_matrix(const Eigen::Matrix& x, int seed_index = -1) { using Eigen::Matrix; using std::vector; @@ -18,9 +18,9 @@ build_matrix(const Eigen::Matrix& x, int seed_index = -1) { for (int i = 0; i < x.size(); ++i) { vector inputs; if (seed_index == i) - inputs = build_vector(vector(), seed_index % num_inputs); + inputs = build_mix_vector(vector(), seed_index % num_inputs); else - inputs = build_vector(vector()); + inputs = build_mix_vector(vector()); fvar_matrix(i) = inputs[i % num_inputs]; } return fvar_matrix; diff --git a/test/unit/math/mix/mat/vectorize/build_vector.hpp b/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp similarity index 72% rename from test/unit/math/mix/mat/vectorize/build_vector.hpp rename to test/unit/math/mix/mat/vectorize/build_mix_vector.hpp index 0932b0ba8c4..8120cb3016e 100644 --- a/test/unit/math/mix/mat/vectorize/build_vector.hpp +++ b/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_VECTOR_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_VECTOR_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_VECTOR_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_VECTOR_HPP #include #include @@ -7,7 +7,7 @@ template static inline std::vector -build_vector(std::vector fvar_vector, int seed_index) { +build_mix_vector(std::vector fvar_vector, int seed_index) { using std::vector; using stan::math::var; vector inputs = F::valid_inputs(); @@ -18,16 +18,16 @@ build_vector(std::vector fvar_vector, int seed_index) { template static inline std::vector > -build_vector(std::vector > fvar_vector, +build_mix_vector(std::vector > fvar_vector, int seed_index = -1) { using std::vector; using stan::math::fvar; vector val_vector = - build_vector(vector(), seed_index); + build_mix_vector(vector(), seed_index); vector d_vector; if (seed_index != -1) - d_vector = build_vector(vector(), seed_index); + d_vector = build_mix_vector(vector(), seed_index); for (size_t i = 0; i < val_vector.size(); ++i) { // For fvar >, this ensures that when diff --git a/test/unit/math/mix/mat/vectorize/expect_errors.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp similarity index 86% rename from test/unit/math/mix/mat/vectorize/expect_errors.hpp rename to test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp index bced5eb757d..764dea8d689 100644 --- a/test/unit/math/mix/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ERRORS_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ERRORS_HPP #include #include @@ -10,7 +10,7 @@ #include template -void expect_errors() { +void expect_mix_errors() { using stan::math::fvar; using stan::math::var; expect_scalar_error >(); diff --git a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_matrix_value.hpp similarity index 65% rename from test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp rename to test/unit/math/mix/mat/vectorize/expect_mix_matrix_value.hpp index 6259afaa167..1f34430c009 100644 --- a/test/unit/math/mix/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_matrix_value.hpp @@ -1,13 +1,13 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_MATRIX_VALUE_HPP #include #include -#include +#include #include template -void expect_matrix_value() { +void expect_mix_matrix_value() { using std::vector; typedef Eigen::Matrix matrix_t; @@ -16,8 +16,8 @@ void expect_matrix_value() { matrix_t template_m(num_inputs, num_cols); for (int i = 0; i < template_m.size(); ++i) { - matrix_t y = build_matrix(template_m, i); - matrix_t z = build_matrix(template_m, i); + matrix_t y = build_mix_matrix(template_m, i); + matrix_t z = build_mix_matrix(template_m, i); matrix_t fz = F::template apply(z); EXPECT_EQ(z.size(), fz.size()); expect_val_deriv_eq(F::apply_base(y(i)), y(i), fz(i), z(i)); @@ -30,11 +30,11 @@ void expect_matrix_value() { vector b; for (size_t k = 0; k < vector_matrix_size; ++k) { if (k == i) { - a.push_back(build_matrix(template_m, j)); - b.push_back(build_matrix(template_m, j)); + a.push_back(build_mix_matrix(template_m, j)); + b.push_back(build_mix_matrix(template_m, j)); } else { - a.push_back(build_matrix(template_m)); - b.push_back(build_matrix(template_m)); + a.push_back(build_mix_matrix(template_m)); + b.push_back(build_mix_matrix(template_m)); } } vector fb = F::template apply >(b); @@ -46,8 +46,8 @@ void expect_matrix_value() { } int seed_i = num_inputs + 1; - matrix_t c = build_matrix(template_m, seed_i); - matrix_t d = build_matrix(template_m, seed_i); + matrix_t c = build_mix_matrix(template_m, seed_i); + matrix_t d = build_mix_matrix(template_m, seed_i); matrix_t fab = F::template apply(d.block(1, 1, 1, 1)); expect_val_deriv_eq(F::apply_base(c(1, 1)), c(1, 1), fab(0, 0), d(1, 1)); } diff --git a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_row_vector_value.hpp similarity index 65% rename from test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp rename to test/unit/math/mix/mat/vectorize/expect_mix_row_vector_value.hpp index eab58100306..327ea19bba7 100644 --- a/test/unit/math/mix/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_row_vector_value.hpp @@ -1,13 +1,13 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ROW_VECTOR_VALUE_HPP #include #include -#include +#include #include template -void expect_row_vector_value() { +void expect_mix_row_vector_value() { using std::vector; typedef Eigen::Matrix row_vector_t; @@ -15,8 +15,8 @@ void expect_row_vector_value() { row_vector_t template_rv(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - row_vector_t a = build_matrix(template_rv, i); - row_vector_t b = build_matrix(template_rv, i); + row_vector_t a = build_mix_matrix(template_rv, i); + row_vector_t b = build_mix_matrix(template_rv, i); row_vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); @@ -29,12 +29,12 @@ void expect_row_vector_value() { vector d; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) { - c.push_back(build_matrix(template_rv, j)); - d.push_back(build_matrix(template_rv, j)); + c.push_back(build_mix_matrix(template_rv, j)); + d.push_back(build_mix_matrix(template_rv, j)); } else { - c.push_back(build_matrix(template_rv)); - d.push_back(build_matrix(template_rv)); + c.push_back(build_mix_matrix(template_rv)); + d.push_back(build_mix_matrix(template_rv)); } vector fd = F::template apply >(d); diff --git a/test/unit/math/mix/mat/vectorize/expect_mix_scalar_value.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_scalar_value.hpp new file mode 100644 index 00000000000..4e22402dcd6 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_mix_scalar_value.hpp @@ -0,0 +1,19 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_SCALAR_VALUE_HPP + +#include +#include +#include + +template +void expect_mix_scalar_value() { + using std::vector; + for (size_t i = 0; i < F::valid_inputs().size(); ++i) { + vector y = build_mix_vector(vector(), i); + vector z = build_mix_vector(vector(), i); + T fz = F::template apply(z[i]); + expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz, z[i]); + } +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_std_vector_value.hpp similarity index 71% rename from test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp rename to test/unit/math/mix/mat/vectorize/expect_mix_std_vector_value.hpp index 0af283809bd..35da14bf25f 100644 --- a/test/unit/math/mix/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_std_vector_value.hpp @@ -2,17 +2,17 @@ #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_STD_VECTOR_VALUE_HPP #include -#include +#include #include template -void expect_std_vector_value() { +void expect_mix_std_vector_value() { using std::vector; size_t num_inputs = F::valid_inputs().size(); for (size_t i = 0; i < num_inputs; ++i) { - vector y = build_vector(vector(), i); - vector z = build_vector(vector(), i); + vector y = build_mix_vector(vector(), i); + vector z = build_mix_vector(vector(), i); vector fz = F::template apply >(z); EXPECT_EQ(z.size(), fz.size()); expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); @@ -25,12 +25,12 @@ void expect_std_vector_value() { vector > b; for (size_t k = 0; k < num_inputs; ++k) { if (i == k) { - a.push_back(build_vector(vector(), j)); - b.push_back(build_vector(vector(), j)); + a.push_back(build_mix_vector(vector(), j)); + b.push_back(build_mix_vector(vector(), j)); } else { - a.push_back(build_vector(vector())); - b.push_back(build_vector(vector())); + a.push_back(build_mix_vector(vector())); + b.push_back(build_mix_vector(vector())); } } vector > fb = F::template apply > >(b); diff --git a/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp new file mode 100644 index 00000000000..42070d6ff29 --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp @@ -0,0 +1,31 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VALUES_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VALUES_HPP + +#include +#include +#include +#include +#include +#include +#include + +//Also will test derivatives +template +void expect_mix_values() { + using stan::math::fvar; + using stan::math::var; + using std::vector; + + expect_mix_scalar_value >(); + expect_mix_scalar_value > >(); + expect_mix_std_vector_value >(); + expect_mix_std_vector_value > >(); + expect_mix_matrix_value >(); + expect_mix_matrix_value > >(); + expect_mix_vector_value >(); + expect_mix_vector_value > >(); + expect_mix_row_vector_value >(); + expect_mix_row_vector_value > >(); +} + +#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_vector_value.hpp similarity index 65% rename from test/unit/math/mix/mat/vectorize/expect_vector_value.hpp rename to test/unit/math/mix/mat/vectorize/expect_mix_vector_value.hpp index 713f413f523..927f5c3c6c0 100644 --- a/test/unit/math/mix/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_vector_value.hpp @@ -1,13 +1,13 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VECTOR_VALUE_HPP #include #include -#include +#include #include template -void expect_vector_value() { +void expect_mix_vector_value() { using std::vector; typedef Eigen::Matrix vector_t; @@ -15,8 +15,8 @@ void expect_vector_value() { vector_t template_v(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - vector_t a = build_matrix(template_v, i); - vector_t b = build_matrix(template_v, i); + vector_t a = build_mix_matrix(template_v, i); + vector_t b = build_mix_matrix(template_v, i); vector_t fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); @@ -29,12 +29,12 @@ void expect_vector_value() { vector d; for (size_t k = 0; k < vector_vector_size; ++k) if (k == i) { - c.push_back(build_matrix(template_v, j)); - d.push_back(build_matrix(template_v, j)); + c.push_back(build_mix_matrix(template_v, j)); + d.push_back(build_mix_matrix(template_v, j)); } else { - c.push_back(build_matrix(template_v)); - d.push_back(build_matrix(template_v)); + c.push_back(build_mix_matrix(template_v)); + d.push_back(build_mix_matrix(template_v)); } vector fd = F::template apply >(d); EXPECT_EQ(d.size(), fd.size()); diff --git a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp deleted file mode 100644 index 370123bcbb0..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_scalar_value.hpp +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP - -#include -#include -#include - -template -void expect_scalar_value() { - using std::vector; - for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = build_vector(vector(), i); - vector z = build_vector(vector(), i); - T fz = F::template apply(z[i]); - expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz, z[i]); - } -} - -#endif diff --git a/test/unit/math/mix/mat/vectorize/expect_values.hpp b/test/unit/math/mix/mat/vectorize/expect_values.hpp deleted file mode 100644 index 0dbe5a0d31b..00000000000 --- a/test/unit/math/mix/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include -#include -#include - -//Also will test derivatives -template -void expect_values() { - using stan::math::fvar; - using stan::math::var; - using std::vector; - - expect_scalar_value >(); - expect_scalar_value > >(); - expect_std_vector_value >(); - expect_std_vector_value > >(); - expect_matrix_value >(); - expect_matrix_value > >(); - expect_vector_value >(); - expect_vector_value > >(); - expect_row_vector_value >(); - expect_row_vector_value > >(); -} - -#endif diff --git a/test/unit/math/prim/mat/vectorize/foo_base_test.cpp b/test/unit/math/mix/mat/vectorize/foo_base_test.cpp similarity index 82% rename from test/unit/math/prim/mat/vectorize/foo_base_test.cpp rename to test/unit/math/mix/mat/vectorize/foo_base_test.cpp index 1053858d27b..63a0fb377f5 100644 --- a/test/unit/math/prim/mat/vectorize/foo_base_test.cpp +++ b/test/unit/math/mix/mat/vectorize/foo_base_test.cpp @@ -1,7 +1,10 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_CPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_CPP #include +#include +#include +#include #include #include @@ -92,8 +95,8 @@ struct foo_base_test { }; INSTANTIATE_TYPED_TEST_CASE_P(, prim_scalar_unary_test, foo_base_test); -// INSTANTIATE_TYPED_TEST_CASE_P(, rev_scalar_unary_test, foo_base_test); -// INSTANTIATE_TYPED_TEST_CASE_P(, fwd_scalar_unary_test, foo_base_test); -// INSTANTIATE_TYPED_TEST_CASE_P(, mix_scalar_unary_test, foo_base_test); +INSTANTIATE_TYPED_TEST_CASE_P(, rev_scalar_unary_test, foo_base_test); +INSTANTIATE_TYPED_TEST_CASE_P(, fwd_scalar_unary_test, foo_base_test); +INSTANTIATE_TYPED_TEST_CASE_P(, mix_scalar_unary_test, foo_base_test); #endif diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp index 5a7d171c2c7..903b321e6cc 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp @@ -2,8 +2,8 @@ #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP #include -#include -#include +#include +#include #include template @@ -21,11 +21,11 @@ TYPED_TEST_P(prim_scalar_unary_test, expect_scalar_types) { } TYPED_TEST_P(prim_scalar_unary_test, expect_values) { - expect_values(); + expect_prim_values(); } TYPED_TEST_P(prim_scalar_unary_test, expect_errors) { - expect_errors(); + expect_prim_errors(); } REGISTER_TYPED_TEST_CASE_P(prim_scalar_unary_test, diff --git a/test/unit/math/prim/mat/vectorize/expect_errors.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_errors.hpp similarity index 88% rename from test/unit/math/prim/mat/vectorize/expect_errors.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_errors.hpp index 362d13fbc13..3998fc6a3f9 100644 --- a/test/unit/math/prim/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_ERRORS_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_ERRORS_HPP #include #include @@ -22,7 +22,7 @@ * @tparam F Test class used to define test case. */ template -void expect_errors() { +void expect_prim_errors() { expect_int_scalar_error(); expect_int_std_vector_error(); expect_scalar_error(); diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_matrix_value.hpp similarity index 85% rename from test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_matrix_value.hpp index a36c5ebfa6a..a444eab48b5 100644 --- a/test/unit/math/prim/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_matrix_value.hpp @@ -1,12 +1,12 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_MATRIX_VALUE_HPP #include #include #include template -void expect_matrix_value() { +void expect_prim_matrix_value() { using Eigen::MatrixXd; using std::vector; diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_row_vector_value.hpp similarity index 81% rename from test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_row_vector_value.hpp index a084ae23e16..46ea12224cb 100644 --- a/test/unit/math/prim/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_row_vector_value.hpp @@ -1,12 +1,12 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_ROW_VECTOR_VALUE_HPP #include #include #include template -void expect_row_vector_value() { +void expect_prim_row_vector_value() { using Eigen::RowVectorXd; using std::vector; diff --git a/test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_scalar_value.hpp similarity index 76% rename from test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_scalar_value.hpp index cd7cd8319d7..76e5cf0cd73 100644 --- a/test/unit/math/prim/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_scalar_value.hpp @@ -1,11 +1,11 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_SCALAR_VALUE_HPP #include #include template -void expect_scalar_value() { +void expect_prim_scalar_value() { using std::vector; vector valid_inputs = F::valid_inputs(); vector int_valid_inputs = F::int_valid_inputs(); diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_std_vector_value.hpp similarity index 85% rename from test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_std_vector_value.hpp index 983cf9d9226..43f8440909a 100644 --- a/test/unit/math/prim/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_std_vector_value.hpp @@ -1,11 +1,11 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_STD_VECTOR_VALUE_HPP #include #include template -void expect_std_vector_value() { +void expect_prim_std_vector_value() { using std::vector; vector valid_inputs = F::valid_inputs(); diff --git a/test/unit/math/prim/mat/vectorize/expect_prim_values.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_values.hpp new file mode 100644 index 00000000000..370cd543d7f --- /dev/null +++ b/test/unit/math/prim/mat/vectorize/expect_prim_values.hpp @@ -0,0 +1,22 @@ +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_VALUES_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_VALUES_HPP + +#include +#include +#include +#include +#include + + +// CALL THIS TO TEST EVERYTHING +// see: apply_scalar_unary_test.cpp for an example +template +void expect_prim_values() { + expect_prim_scalar_value(); + expect_prim_std_vector_value(); + expect_prim_matrix_value(); + expect_prim_vector_value(); + expect_prim_row_vector_value(); +} + +#endif diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_value.hpp b/test/unit/math/prim/mat/vectorize/expect_prim_vector_value.hpp similarity index 81% rename from test/unit/math/prim/mat/vectorize/expect_vector_value.hpp rename to test/unit/math/prim/mat/vectorize/expect_prim_vector_value.hpp index 778ace365c0..4fa77605461 100644 --- a/test/unit/math/prim/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_prim_vector_value.hpp @@ -1,12 +1,12 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_PRIM_VECTOR_VALUE_HPP #include #include #include template -void expect_vector_value() { +void expect_prim_vector_value() { using Eigen::VectorXd; using std::vector; diff --git a/test/unit/math/prim/mat/vectorize/expect_values.hpp b/test/unit/math/prim/mat/vectorize/expect_values.hpp deleted file mode 100644 index 13f6e03eec3..00000000000 --- a/test/unit/math/prim/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include - - -// CALL THIS TO TEST EVERYTHING -// see: apply_scalar_unary_test.cpp for an example -template -void expect_values() { - expect_scalar_value(); - expect_std_vector_value(); - expect_matrix_value(); - expect_vector_value(); - expect_row_vector_value(); -} - -#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp deleted file mode 100644 index 83895d1c20b..00000000000 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -/** - * this tests that the expect_values test works on a mock function - */ -TEST(MathRevMatVectorize, applyScalarUnaryMock) { - expect_types(); - expect_values(); - expect_errors(); -} - diff --git a/test/unit/math/rev/mat/vectorize/build_matrix.hpp b/test/unit/math/rev/mat/vectorize/build_rev_matrix.hpp similarity index 72% rename from test/unit/math/rev/mat/vectorize/build_matrix.hpp rename to test/unit/math/rev/mat/vectorize/build_rev_matrix.hpp index 97e5668b776..0f37ad770b3 100644 --- a/test/unit/math/rev/mat/vectorize/build_matrix.hpp +++ b/test/unit/math/rev/mat/vectorize/build_rev_matrix.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_MATRIX_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_MATRIX_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_REV_MATRIX_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_REV_MATRIX_HPP #include #include @@ -7,7 +7,7 @@ template static inline Eigen::Matrix -build_matrix(const Eigen::Matrix& x) { +build_rev_matrix(const Eigen::Matrix& x) { using Eigen::Matrix; using std::vector; using stan::math::var; diff --git a/test/unit/math/rev/mat/vectorize/build_vector.hpp b/test/unit/math/rev/mat/vectorize/build_rev_vector.hpp similarity index 65% rename from test/unit/math/rev/mat/vectorize/build_vector.hpp rename to test/unit/math/rev/mat/vectorize/build_rev_vector.hpp index 1dad615f3e5..58305105dbc 100644 --- a/test/unit/math/rev/mat/vectorize/build_vector.hpp +++ b/test/unit/math/rev/mat/vectorize/build_rev_vector.hpp @@ -1,12 +1,12 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_VECTOR_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_VECTOR_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_REV_VECTOR_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_BUILD_REV_VECTOR_HPP #include #include template static inline std::vector -build_vector() { +build_rev_vector() { using std::vector; using stan::math::var; vector inputs = F::valid_inputs(); diff --git a/test/unit/math/rev/mat/vectorize/expect_errors.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_errors.hpp similarity index 80% rename from test/unit/math/rev/mat/vectorize/expect_errors.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_errors.hpp index 0e24bb6f3f5..7659bf76aa8 100644 --- a/test/unit/math/rev/mat/vectorize/expect_errors.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_errors.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ERRORS_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ERRORS_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_ERRORS_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_ERRORS_HPP #include #include @@ -9,7 +9,7 @@ #include template -void expect_errors() { +void expect_rev_errors() { using stan::math::var; expect_scalar_error(); expect_std_vector_error(); diff --git a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_matrix_value.hpp similarity index 70% rename from test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_matrix_value.hpp index 64250021c9b..856a45649ba 100644 --- a/test/unit/math/rev/mat/vectorize/expect_matrix_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_matrix_value.hpp @@ -1,15 +1,15 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_MATRIX_VALUE_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_MATRIX_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_MATRIX_VALUE_HPP #include #include #include #include -#include +#include #include template -void expect_matrix_value() { +void expect_rev_matrix_value() { using stan::math::var; using std::vector; typedef Eigen::Matrix MatrixXvar; @@ -19,8 +19,8 @@ void expect_matrix_value() { MatrixXvar template_m(num_inputs, num_cols); for (int i = 0; i < template_m.size(); ++i) { - MatrixXvar a = build_matrix(template_m); - MatrixXvar b = build_matrix(template_m); + MatrixXvar a = build_rev_matrix(template_m); + MatrixXvar b = build_rev_matrix(template_m); MatrixXvar fb = F::template apply(b); EXPECT_EQ(b.size(), fb.size()); expect_val_deriv_eq(F::apply_base(a(i)), a(i), fb(i), b(i)); @@ -32,8 +32,8 @@ void expect_matrix_value() { vector c; vector d; for (size_t k = 0; k < vector_matrix_size; ++k) { - c.push_back(build_matrix(template_m)); - d.push_back(build_matrix(template_m)); + c.push_back(build_rev_matrix(template_m)); + d.push_back(build_rev_matrix(template_m)); } vector fd = F::template apply >(d); EXPECT_EQ(d[i].size(), fd[i].size()); @@ -44,8 +44,8 @@ void expect_matrix_value() { } } - MatrixXvar e = build_matrix(template_m).block(1, 1, 1, 1); - MatrixXvar f = build_matrix(template_m); + MatrixXvar e = build_rev_matrix(template_m).block(1, 1, 1, 1); + MatrixXvar f = build_rev_matrix(template_m); MatrixXvar ffb = F::template apply(f.block(1, 1, 1, 1)); expect_val_deriv_eq(F::apply_base(e(0,0)), e(0,0), ffb(0,0), f(1,1)); } diff --git a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_row_vector_value.hpp similarity index 70% rename from test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_row_vector_value.hpp index 128a4a07bc7..80b202abc33 100644 --- a/test/unit/math/rev/mat/vectorize/expect_row_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_row_vector_value.hpp @@ -1,15 +1,15 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_ROW_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_ROW_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_ROW_VECTOR_VALUE_HPP #include #include #include #include -#include +#include #include template -void expect_row_vector_value() { +void expect_rev_row_vector_value() { using stan::math::var; using std::vector; typedef Eigen::Matrix RowVectorXvar; @@ -18,8 +18,8 @@ void expect_row_vector_value() { RowVectorXvar template_vector(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - RowVectorXvar b = build_matrix(template_vector); - RowVectorXvar c = build_matrix(template_vector); + RowVectorXvar b = build_rev_matrix(template_vector); + RowVectorXvar c = build_rev_matrix(template_vector); RowVectorXvar fc = F::template apply(c); EXPECT_EQ(c.size(), fc.size()); expect_val_deriv_eq(F::apply_base(b(i)), b(i), fc(i), c(i)); @@ -31,8 +31,8 @@ void expect_row_vector_value() { vector d; vector e; for (size_t k = 0; k < num_inputs; ++k) { - d.push_back(build_matrix(template_vector)); - e.push_back(build_matrix(template_vector)); + d.push_back(build_rev_matrix(template_vector)); + e.push_back(build_rev_matrix(template_vector)); } vector fe = F::template apply >(e); diff --git a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_scalar_value.hpp similarity index 55% rename from test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_scalar_value.hpp index c36c81a077e..345b1212470 100644 --- a/test/unit/math/rev/mat/vectorize/expect_scalar_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_scalar_value.hpp @@ -1,20 +1,20 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_SCALAR_VALUE_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_SCALAR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_SCALAR_VALUE_HPP #include #include #include -#include +#include #include template -void expect_scalar_value() { +void expect_rev_scalar_value() { using stan::math::var; using std::vector; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = build_vector(); - vector z = build_vector(); + vector y = build_rev_vector(); + vector z = build_rev_vector(); var fz = F::template apply(z[i]); expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz, z[i]); } diff --git a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_std_vector_value.hpp similarity index 69% rename from test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_std_vector_value.hpp index 81c2b158898..a2a92dd7a34 100644 --- a/test/unit/math/rev/mat/vectorize/expect_std_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_std_vector_value.hpp @@ -1,19 +1,19 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_STD_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_STD_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_STD_VECTOR_VALUE_HPP #include #include -#include +#include #include template -void expect_std_vector_value() { +void expect_rev_std_vector_value() { using std::vector; using stan::math::var; for (size_t i = 0; i < F::valid_inputs().size(); ++i) { - vector y = build_vector(); - vector z = build_vector(); + vector y = build_rev_vector(); + vector z = build_rev_vector(); vector fz = F::template apply >(z); EXPECT_EQ(z.size(), fz.size()); expect_val_deriv_eq(F::apply_base(y[i]), y[i], fz[i], z[i]); @@ -25,8 +25,8 @@ void expect_std_vector_value() { vector > a; vector > b; for (size_t i = 0; i < vector_vector_size; ++i) { - a.push_back(build_vector()); - b.push_back(build_vector()); + a.push_back(build_rev_vector()); + b.push_back(build_rev_vector()); } vector > fb = F::template apply > >(b); EXPECT_EQ(b.size(), fb.size()); diff --git a/test/unit/math/rev/mat/vectorize/expect_rev_values.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_values.hpp new file mode 100644 index 00000000000..0bd49a96f50 --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/expect_rev_values.hpp @@ -0,0 +1,20 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_VALUES_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_VALUES_HPP + +#include +#include +#include +#include +#include + +//Also will test derivatives +template +void expect_rev_values() { + expect_rev_scalar_value(); + expect_rev_std_vector_value(); + expect_rev_matrix_value(); + expect_rev_vector_value(); + expect_rev_row_vector_value(); +} + +#endif diff --git a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp b/test/unit/math/rev/mat/vectorize/expect_rev_vector_value.hpp similarity index 70% rename from test/unit/math/rev/mat/vectorize/expect_vector_value.hpp rename to test/unit/math/rev/mat/vectorize/expect_rev_vector_value.hpp index 8f7cc701fea..3eb9c88067e 100644 --- a/test/unit/math/rev/mat/vectorize/expect_vector_value.hpp +++ b/test/unit/math/rev/mat/vectorize/expect_rev_vector_value.hpp @@ -1,15 +1,15 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VECTOR_VALUE_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_VECTOR_VALUE_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_REV_VECTOR_VALUE_HPP #include #include #include #include -#include +#include #include template -void expect_vector_value() { +void expect_rev_vector_value() { using stan::math::var; using std::vector; typedef Eigen::Matrix VectorXvar; @@ -18,8 +18,8 @@ void expect_vector_value() { VectorXvar template_vector(num_inputs); for (size_t i = 0; i < num_inputs; ++i) { - VectorXvar b = build_matrix(template_vector); - VectorXvar c = build_matrix(template_vector); + VectorXvar b = build_rev_matrix(template_vector); + VectorXvar c = build_rev_matrix(template_vector); VectorXvar fc = F::template apply(c); EXPECT_EQ(c.size(), fc.size()); expect_val_deriv_eq(F::apply_base(b(i)), b(i), fc(i), c(i)); @@ -31,8 +31,8 @@ void expect_vector_value() { vector d; vector e; for (size_t k = 0; k < num_inputs; ++k) { - d.push_back(build_matrix(template_vector)); - e.push_back(build_matrix(template_vector)); + d.push_back(build_rev_matrix(template_vector)); + e.push_back(build_rev_matrix(template_vector)); } vector fe = F::template apply >(e); EXPECT_EQ(e[i].size(), fe[i].size()); diff --git a/test/unit/math/rev/mat/vectorize/expect_values.hpp b/test/unit/math/rev/mat/vectorize/expect_values.hpp deleted file mode 100644 index 451a6b72db0..00000000000 --- a/test/unit/math/rev/mat/vectorize/expect_values.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_EXPECT_VALUES_HPP - -#include -#include -#include -#include -#include - -//Also will test derivatives -template -void expect_values() { - expect_scalar_value(); - expect_std_vector_value(); - expect_matrix_value(); - expect_vector_value(); - expect_row_vector_value(); -} - -#endif From d37e4734a421d364ceafa9b1b68f1e2762e94660 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Mon, 7 Mar 2016 17:44:10 -0500 Subject: [PATCH 32/36] Refactored expect error files --- .../vectorize/expect_int_std_vector_error.hpp | 19 ++++++------ .../mat/vectorize/expect_matrix_error.hpp | 29 +++++++++---------- .../mat/vectorize/expect_row_vector_error.hpp | 25 ++++++++-------- .../mat/vectorize/expect_std_vector_error.hpp | 19 ++++++------ .../mat/vectorize/expect_vector_error.hpp | 23 +++++++-------- 5 files changed, 55 insertions(+), 60 deletions(-) diff --git a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp index 9a589456052..44ddc7dbcfa 100644 --- a/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_int_std_vector_error.hpp @@ -9,16 +9,15 @@ template void expect_int_std_vector_error() { using std::vector; vector invalid_inputs = F::int_invalid_inputs(); - if (invalid_inputs.size() > 0) { - EXPECT_THROW(F::template apply >(invalid_inputs), - std::domain_error); - - vector > z; - z.push_back(invalid_inputs); - z.push_back(invalid_inputs); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); - } + if (invalid_inputs.size() == 0) return; + EXPECT_THROW(F::template apply >(invalid_inputs), + std::domain_error); + + vector > z; + z.push_back(invalid_inputs); + z.push_back(invalid_inputs); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp index c74a3d132a4..880f09dfedf 100644 --- a/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_matrix_error.hpp @@ -11,21 +11,20 @@ void expect_matrix_error() { using std::vector; typedef Eigen::Matrix matrix_t; vector invalid_inputs = F::invalid_inputs(); - if (invalid_inputs.size() > 0) { - matrix_t a(3, invalid_inputs.size()); - for (int i = 0; i < a.rows(); ++i) - for (int j = 0; j < a.cols(); ++j) - a(i, j) = invalid_inputs[j]; - EXPECT_THROW(F::template apply(a), std::domain_error); - EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), - std::domain_error); - - vector d; - d.push_back(a); - d.push_back(a); - EXPECT_THROW(F::template apply >(d), - std::domain_error); - } + if (invalid_inputs.size() == 0) return; + matrix_t a(3, invalid_inputs.size()); + for (int i = 0; i < a.rows(); ++i) + for (int j = 0; j < a.cols(); ++j) + a(i, j) = invalid_inputs[j]; + EXPECT_THROW(F::template apply(a), std::domain_error); + EXPECT_THROW(F::template apply(a.block(1, 1, 1, 1)), + std::domain_error); + + vector d; + d.push_back(a); + d.push_back(a); + EXPECT_THROW(F::template apply >(d), + std::domain_error); } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp index 1e0d0e18dc6..24f689de330 100644 --- a/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_row_vector_error.hpp @@ -10,18 +10,17 @@ void expect_row_vector_error() { using std::vector; typedef Eigen::Matrix row_vector_t; std::vector invalid_inputs = F::invalid_inputs(); - if (invalid_inputs.size() > 0) { - row_vector_t c = row_vector_t(invalid_inputs.size()); - for (size_t i = 0; i < invalid_inputs.size(); ++i) - c(i) = invalid_inputs[i]; - EXPECT_THROW(F::template apply(c), std::domain_error); - - vector d; - d.push_back(c); - d.push_back(c); - - EXPECT_THROW(F::template apply >(d), - std::domain_error); - } + if (invalid_inputs.size() == 0) return; + row_vector_t c = row_vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + c(i) = invalid_inputs[i]; + EXPECT_THROW(F::template apply(c), std::domain_error); + + vector d; + d.push_back(c); + d.push_back(c); + + EXPECT_THROW(F::template apply >(d), + std::domain_error); } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp index ae71830b355..d609bf38dec 100644 --- a/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_std_vector_error.hpp @@ -9,16 +9,15 @@ template void expect_std_vector_error() { using std::vector; vector invalid_inputs = F::invalid_inputs(); - if (invalid_inputs.size() > 0) { - vector y(invalid_inputs.begin(), invalid_inputs.end()); - EXPECT_THROW(F::template apply >(y), std::domain_error); - - vector > z; - z.push_back(y); - z.push_back(y); - EXPECT_THROW(F::template apply > >(z), - std::domain_error); - } + if (invalid_inputs.size() == 0) return; + vector y(invalid_inputs.begin(), invalid_inputs.end()); + EXPECT_THROW(F::template apply >(y), std::domain_error); + + vector > z; + z.push_back(y); + z.push_back(y); + EXPECT_THROW(F::template apply > >(z), + std::domain_error); } #endif diff --git a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp index 93b4c24e6cd..cafc9e2ce06 100644 --- a/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp +++ b/test/unit/math/prim/mat/vectorize/expect_vector_error.hpp @@ -11,18 +11,17 @@ void expect_vector_error() { using std::vector; typedef Eigen::Matrix vector_t; vector invalid_inputs = F::invalid_inputs(); - if (invalid_inputs.size() > 0) { - vector_t b = vector_t(invalid_inputs.size()); - for (size_t i = 0; i < invalid_inputs.size(); ++i) - b(i) = invalid_inputs[i]; - EXPECT_THROW(F::template apply(b), std::domain_error); - - vector d; - d.push_back(b); - d.push_back(b); - EXPECT_THROW(F::template apply >(d), - std::domain_error); - } + if (invalid_inputs.size() == 0) return; + vector_t b = vector_t(invalid_inputs.size()); + for (size_t i = 0; i < invalid_inputs.size(); ++i) + b(i) = invalid_inputs[i]; + EXPECT_THROW(F::template apply(b), std::domain_error); + + vector d; + d.push_back(b); + d.push_back(b); + EXPECT_THROW(F::template apply >(d), + std::domain_error); } #endif From 463ca1294dc2cb7aa431ff1e2a79f9ffa27fc198 Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Mon, 7 Mar 2016 17:46:11 -0500 Subject: [PATCH 33/36] Include all test files --- .../mat/vectorize/apply_scalar_unary_test.hpp | 37 +++++++++++++++++ .../mat/vectorize/apply_scalar_unary_test.hpp | 40 +++++++++++++++++++ .../mat/vectorize/apply_scalar_unary_test.hpp | 34 ++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp create mode 100644 test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp create mode 100644 test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp new file mode 100644 index 00000000000..cadaeb89333 --- /dev/null +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp @@ -0,0 +1,37 @@ +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +class fwd_scalar_unary_test : public ::testing::Test { +}; + +TYPED_TEST_CASE_P(fwd_scalar_unary_test); + +TYPED_TEST_P(fwd_scalar_unary_test, expect_scalar_types) { + using stan::math::fvar; + + expect_types >(); + expect_types > >(); +} + +TYPED_TEST_P(fwd_scalar_unary_test, expect_values) { + expect_fwd_values(); +} + +TYPED_TEST_P(fwd_scalar_unary_test, expect_errors) { + expect_fwd_errors(); +} + +REGISTER_TYPED_TEST_CASE_P(fwd_scalar_unary_test, + expect_scalar_types, + expect_values, + expect_errors); +#endif diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp new file mode 100644 index 00000000000..71b56b16bdb --- /dev/null +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp @@ -0,0 +1,40 @@ +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +template +class mix_scalar_unary_test : public ::testing::Test { +}; + +TYPED_TEST_CASE_P(mix_scalar_unary_test); + +TYPED_TEST_P(mix_scalar_unary_test, expect_scalar_types) { + using stan::math::fvar; + using stan::math::var; + + expect_types >(); + expect_types > >(); +} + +TYPED_TEST_P(mix_scalar_unary_test, expect_values) { + expect_mix_values(); +} + +TYPED_TEST_P(mix_scalar_unary_test, expect_errors) { + expect_mix_errors(); +} + +REGISTER_TYPED_TEST_CASE_P(mix_scalar_unary_test, + expect_scalar_types, + expect_values, + expect_errors); +#endif diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp new file mode 100644 index 00000000000..b75d07ebd8d --- /dev/null +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp @@ -0,0 +1,34 @@ +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP + +#include +#include +#include +#include +#include +#include +#include + +template +class rev_scalar_unary_test : public ::testing::Test { +}; + +TYPED_TEST_CASE_P(rev_scalar_unary_test); + +TYPED_TEST_P(rev_scalar_unary_test, expect_scalar_types) { + expect_types(); +} + +TYPED_TEST_P(rev_scalar_unary_test, expect_values) { + expect_rev_values(); +} + +TYPED_TEST_P(rev_scalar_unary_test, expect_errors) { + expect_rev_errors(); +} + +REGISTER_TYPED_TEST_CASE_P(rev_scalar_unary_test, + expect_scalar_types, + expect_values, + expect_errors); +#endif From eeca2ef1dfe21e83b130bded5eac121214c5ed9b Mon Sep 17 00:00:00 2001 From: Rayleigh L Date: Mon, 7 Mar 2016 17:54:43 -0500 Subject: [PATCH 34/36] Updated includes --- test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp | 3 +-- test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp | 4 +--- test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp | 1 - 3 files changed, 2 insertions(+), 6 deletions(-) diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp index cadaeb89333..e1c85a8c2b0 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp @@ -1,8 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#include -#include +#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp index 71b56b16bdb..8afcca8de8f 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp @@ -1,10 +1,8 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#include +#include #include -#include -#include #include #include #include diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp index b75d07ebd8d..a697edcee51 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp @@ -2,7 +2,6 @@ #define TEST_UNIT_MATH_REV_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP #include -#include #include #include #include From 6d7c6fab8521baf47315d538188d5fd1523b2aea Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 14 Mar 2016 12:28:34 -0400 Subject: [PATCH 35/36] Moving files, changing some includes --- .../fwd/mat/vectorize/build_fwd_matrix.hpp | 2 +- .../fwd/mat/vectorize/build_fwd_vector.hpp | 2 +- .../fwd/mat/vectorize/expect_fwd_errors.hpp | 2 +- .../mat/vectorize/expect_fwd_matrix_value.hpp | 2 +- .../vectorize/expect_fwd_row_vector_value.hpp | 2 +- .../mat/vectorize/expect_fwd_scalar_value.hpp | 2 +- .../vectorize/expect_fwd_std_vector_value.hpp | 2 +- .../fwd/mat/vectorize/expect_fwd_values.hpp | 2 +- .../mat/vectorize/expect_fwd_vector_value.hpp | 2 +- .../fwd/mat/vectorize/expect_val_deriv_eq.hpp | 2 +- ...ary_test.hpp => fwd_scalar_unary_test.hpp} | 4 +-- .../mix/mat/vectorize/build_mix_matrix.hpp | 2 +- .../mix/mat/vectorize/build_mix_vector.hpp | 3 +- .../mix/mat/vectorize/expect_mix_errors.hpp | 3 +- .../mix/mat/vectorize/expect_mix_values.hpp | 3 +- .../{foo_base_test.cpp => foo_test.cpp} | 33 +++++++++---------- ...ary_test.hpp => mix_scalar_unary_test.hpp} | 9 ++--- test/unit/math/prim/mat/vectorize/foo_fun.hpp | 3 +- ...ry_test.hpp => prim_scalar_unary_test.hpp} | 4 +-- .../prim/mat/vectorize/vector_builder.hpp | 2 +- .../mat/vectorize/vector_builder_test.cpp | 7 ++-- ...ary_test.hpp => rev_scalar_unary_test.hpp} | 4 +-- 22 files changed, 44 insertions(+), 53 deletions(-) rename test/unit/math/fwd/mat/vectorize/{apply_scalar_unary_test.hpp => fwd_scalar_unary_test.hpp} (87%) rename test/unit/math/mix/mat/vectorize/{foo_base_test.cpp => foo_test.cpp} (71%) rename test/unit/math/mix/mat/vectorize/{apply_scalar_unary_test.hpp => mix_scalar_unary_test.hpp} (73%) rename test/unit/math/prim/mat/vectorize/{apply_scalar_unary_test.hpp => prim_scalar_unary_test.hpp} (87%) rename test/unit/math/rev/mat/vectorize/{apply_scalar_unary_test.hpp => rev_scalar_unary_test.hpp} (86%) diff --git a/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp b/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp index fb43d568af4..bfd15170938 100644 --- a/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_fwd_matrix.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_MATRIX_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_MATRIX_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp b/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp index 36a8391f477..c9c268fdd02 100644 --- a/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp +++ b/test/unit/math/fwd/mat/vectorize/build_fwd_vector.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_VECTOR_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_BUILD_FWD_VECTOR_HPP -#include +#include #include template diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp index e5bfe2cfca2..da98b4af492 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_errors.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ERRORS_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ERRORS_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp index 91829249733..bd601a13b94 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_matrix_value.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_MATRIX_VALUE_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_MATRIX_VALUE_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp index e7decff3cff..d1b6fd07a32 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_row_vector_value.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ROW_VECTOR_VALUE_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_ROW_VECTOR_VALUE_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp index 005faccece9..d736efb0649 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_scalar_value.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_SCALAR_VALUE_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_SCALAR_VALUE_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp index cdd30677d2a..fb6cddf9ebe 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_std_vector_value.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_STD_VECTOR_VALUE_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_STD_VECTOR_VALUE_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp index 2138e25056c..578d8532797 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_values.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VALUES_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VALUES_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp b/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp index 94b4a203124..939893486b1 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_fwd_vector_value.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VECTOR_VALUE_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_FWD_VECTOR_VALUE_HPP -#include +#include #include #include #include diff --git a/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp b/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp index db1521a3233..648b5457b6f 100644 --- a/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp +++ b/test/unit/math/fwd/mat/vectorize/expect_val_deriv_eq.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_EQ_HPP #define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_EXPECT_EQ_HPP -#include +#include #include static inline void expect_val_deriv_eq(double exp_var, double test_var) { diff --git a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/fwd/mat/vectorize/fwd_scalar_unary_test.hpp similarity index 87% rename from test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp rename to test/unit/math/fwd/mat/vectorize/fwd_scalar_unary_test.hpp index e1c85a8c2b0..dd890f25498 100644 --- a/test/unit/math/fwd/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/fwd/mat/vectorize/fwd_scalar_unary_test.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#ifndef TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_FWD_MAT_VECTORIZE_FWD_SCALAR_UNARY_TEST_HPP #include #include diff --git a/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp b/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp index fc08d4bb9cc..64ae94c548f 100644 --- a/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp +++ b/test/unit/math/mix/mat/vectorize/build_mix_matrix.hpp @@ -1,7 +1,7 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_MATRIX_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_MATRIX_HPP -#include +#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp b/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp index 8120cb3016e..09284999777 100644 --- a/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp +++ b/test/unit/math/mix/mat/vectorize/build_mix_vector.hpp @@ -1,8 +1,7 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_VECTOR_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_BUILD_MIX_VECTOR_HPP -#include -#include +#include #include template diff --git a/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp index 764dea8d689..8bf7fde57e9 100644 --- a/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_errors.hpp @@ -1,8 +1,7 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ERRORS_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_ERRORS_HPP -#include -#include +#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp b/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp index 42070d6ff29..80232d1720a 100644 --- a/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp +++ b/test/unit/math/mix/mat/vectorize/expect_mix_values.hpp @@ -1,8 +1,7 @@ #ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VALUES_HPP #define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_EXPECT_MIX_VALUES_HPP -#include -#include +#include #include #include #include diff --git a/test/unit/math/mix/mat/vectorize/foo_base_test.cpp b/test/unit/math/mix/mat/vectorize/foo_test.cpp similarity index 71% rename from test/unit/math/mix/mat/vectorize/foo_base_test.cpp rename to test/unit/math/mix/mat/vectorize/foo_test.cpp index 63a0fb377f5..8109892c4ff 100644 --- a/test/unit/math/mix/mat/vectorize/foo_base_test.cpp +++ b/test/unit/math/mix/mat/vectorize/foo_test.cpp @@ -1,10 +1,9 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_CPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_BASE_TEST_CPP - -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include #include @@ -12,7 +11,7 @@ * This is the structure for testing mock function foo (defined in the * testing framework). See README.txt for more instructions. */ -struct foo_base_test { +struct foo_test { /** * Redefinition of function brought in from stan::math. The reason @@ -65,7 +64,7 @@ struct foo_base_test { * Return sequence of valid double-valued inputs. */ static std::vector valid_inputs() { - return stan::math::vector_builder() + return test::math::vector_builder() .add(1.3).add(-2.6).add(0).add(-0.2).build(); } @@ -73,7 +72,7 @@ struct foo_base_test { * Return sequence of invalid double-valued inputs. */ static std::vector invalid_inputs() { - return stan::math::vector_builder() + return test::math::vector_builder() .add(10.6).add(10.6).add(25.7).add(100.25).build(); } @@ -81,7 +80,7 @@ struct foo_base_test { * Return sequence of valid integer inputs. */ static std::vector int_valid_inputs() { - return stan::math::vector_builder() + return test::math::vector_builder() .add(1).add(-2).add(0).add(3).build(); } @@ -89,14 +88,12 @@ struct foo_base_test { * Return sequence of invalid integer inputs. */ static std::vector int_invalid_inputs() { - return stan::math::vector_builder() + return test::math::vector_builder() .add(10).add(25).add(100).add(50).build(); } }; -INSTANTIATE_TYPED_TEST_CASE_P(, prim_scalar_unary_test, foo_base_test); -INSTANTIATE_TYPED_TEST_CASE_P(, rev_scalar_unary_test, foo_base_test); -INSTANTIATE_TYPED_TEST_CASE_P(, fwd_scalar_unary_test, foo_base_test); -INSTANTIATE_TYPED_TEST_CASE_P(, mix_scalar_unary_test, foo_base_test); - -#endif +INSTANTIATE_TYPED_TEST_CASE_P(, prim_scalar_unary_test, foo_test); +INSTANTIATE_TYPED_TEST_CASE_P(, rev_scalar_unary_test, foo_test); +INSTANTIATE_TYPED_TEST_CASE_P(, fwd_scalar_unary_test, foo_test); +INSTANTIATE_TYPED_TEST_CASE_P(, mix_scalar_unary_test, foo_test); diff --git a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/mix/mat/vectorize/mix_scalar_unary_test.hpp similarity index 73% rename from test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp rename to test/unit/math/mix/mat/vectorize/mix_scalar_unary_test.hpp index 8afcca8de8f..aa31ab2703b 100644 --- a/test/unit/math/mix/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/mix/mat/vectorize/mix_scalar_unary_test.hpp @@ -1,10 +1,7 @@ -#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#ifndef TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_MIX_MAT_VECTORIZE_MIX_SCALAR_UNARY_TEST_HPP -#include -#include -#include -#include +#include #include #include #include diff --git a/test/unit/math/prim/mat/vectorize/foo_fun.hpp b/test/unit/math/prim/mat/vectorize/foo_fun.hpp index 0aaaac076af..1bdcd371cd1 100644 --- a/test/unit/math/prim/mat/vectorize/foo_fun.hpp +++ b/test/unit/math/prim/mat/vectorize/foo_fun.hpp @@ -1,12 +1,11 @@ #ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP #define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_FOO_FUN_HPP -#include #include +#include #include namespace stan { - namespace math { /** diff --git a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/prim/mat/vectorize/prim_scalar_unary_test.hpp similarity index 87% rename from test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp rename to test/unit/math/prim/mat/vectorize/prim_scalar_unary_test.hpp index 903b321e6cc..e72e322caf7 100644 --- a/test/unit/math/prim/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/prim/mat/vectorize/prim_scalar_unary_test.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#ifndef TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_PRIM_MAT_VECTORIZE_PRIM_SCALAR_UNARY_TEST_HPP #include #include diff --git a/test/unit/math/prim/mat/vectorize/vector_builder.hpp b/test/unit/math/prim/mat/vectorize/vector_builder.hpp index e58693ff1ce..0019954ca55 100644 --- a/test/unit/math/prim/mat/vectorize/vector_builder.hpp +++ b/test/unit/math/prim/mat/vectorize/vector_builder.hpp @@ -3,7 +3,7 @@ #include -namespace stan { +namespace test { namespace math { /** diff --git a/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp index caca03eec9c..13f12872836 100644 --- a/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp +++ b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp @@ -1,10 +1,11 @@ -#include -#include +#include #include +#include +#include TEST(mathVectorBuilder, test1) { - using stan::math::vector_builder; + using test::math::vector_builder; vector_builder x; std::vector z = x.build(); diff --git a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp b/test/unit/math/rev/mat/vectorize/rev_scalar_unary_test.hpp similarity index 86% rename from test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp rename to test/unit/math/rev/mat/vectorize/rev_scalar_unary_test.hpp index a697edcee51..b8b5f90d9aa 100644 --- a/test/unit/math/rev/mat/vectorize/apply_scalar_unary_test.hpp +++ b/test/unit/math/rev/mat/vectorize/rev_scalar_unary_test.hpp @@ -1,5 +1,5 @@ -#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP -#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_APPLY_SCALAR_UNARY_TEST_HPP +#ifndef TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_SCALAR_UNARY_TEST_HPP +#define TEST_UNIT_MATH_REV_MAT_VECTORIZE_REV_SCALAR_UNARY_TEST_HPP #include #include From 1cf908d7ac74bc0bbd6a6fc42550c1c4826d4fff Mon Sep 17 00:00:00 2001 From: Daniel Lee Date: Mon, 14 Mar 2016 14:34:25 -0400 Subject: [PATCH 36/36] Fixed include --- test/unit/math/prim/mat/vectorize/vector_builder_test.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp index 13f12872836..da574459d68 100644 --- a/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp +++ b/test/unit/math/prim/mat/vectorize/vector_builder_test.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include