Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operands_and_partials refactor #547

Merged
merged 44 commits into from
May 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
74464c8
Refactor scalar_type and value_type
seantalts Mar 27, 2017
d09a3a2
Don't need consts here.
seantalts Mar 28, 2017
0f63e72
Temp - multivariate working, needs fvar
seantalts Mar 30, 2017
c927e44
Temp2 - show Rob. Started getting everything working for fwd mode, ne…
seantalts Apr 11, 2017
a9cc0b3
Get fwd/mat test passing.
seantalts Apr 17, 2017
c341d7a
Get fvar/scal test working
seantalts Apr 17, 2017
cc99757
Fix mixed mode
seantalts Apr 18, 2017
9361833
more fixes for mixed mode
seantalts Apr 19, 2017
7116fb2
Adding a temporary test to hunt down seg fault
Apr 20, 2017
7e343b9
Fix segfault by initializing partials for fvar<var> etc
seantalts Apr 20, 2017
6d94a24
Merge branch 'develop' of github.com:stan-dev/math into ops_partials
seantalts Apr 26, 2017
2eeb9e6
Get all the unit tests passing with the new API
seantalts May 3, 2017
2366e49
Replace OperandsAndPartials uses; remove it and VectorView
seantalts May 3, 2017
52761b9
cpplint
seantalts May 3, 2017
61dae11
Merge branch 'develop' of github.com:stan-dev/math into cleanup/546-o…
seantalts May 3, 2017
739c3fd
Fix includes.
seantalts May 4, 2017
a7bfeb1
Fix mixed mode tests. Remove unused tests.
seantalts May 5, 2017
d249fa0
Add test for return_type
seantalts May 5, 2017
8c196ed
Merge branch 'develop' of github.com:stan-dev/math into cleanup/546-o…
seantalts May 6, 2017
56ebec5
Add doc
seantalts May 6, 2017
f4bdc5e
Merge branch 'develop' of https://github.com/stan-dev/math into clean…
seantalts May 8, 2017
2c4d302
Fix broadcast_array test to use new fvar ctor
seantalts May 8, 2017
848b3c3
Address Bob's comments.
seantalts May 9, 2017
27907f4
Merge branch 'cleanup/546-operands-partials' of github.com:stan-dev/m…
seantalts May 9, 2017
c2b5b2f
Fix whitespace errors introduced by _
seantalts May 9, 2017
f18697e
Fix last missing _
seantalts May 9, 2017
3ef7cdc
Address Dan's pull request comments
seantalts May 10, 2017
fcbcbb8
Add mixed mode multivariate test
seantalts May 10, 2017
2dece4d
Fix mixed multivariate operands_and_partials
seantalts May 10, 2017
1afac1b
Fix cpplint error.
seantalts May 10, 2017
65f894d
Don't need container view anymore -
seantalts May 10, 2017
b0b7329
Fix multivariate mixed tests
seantalts May 11, 2017
eaa147b
New test for creating zero matrices and vectors
seantalts May 11, 2017
e1d06bd
Use friendship to hide edge internals from stan::math namespace.
seantalts May 16, 2017
3b0e04c
Switch from a 'detail' namespace to 'internal'
seantalts May 16, 2017
a0e7d1f
Deal with empty operands_ appropriately.
seantalts May 16, 2017
03aecb0
Fix cpplint
seantalts May 16, 2017
9044360
Unlikely.
seantalts May 16, 2017
e71f0d5
Fix doc up a bit, more doxygen
seantalts May 18, 2017
4131e23
[ci-skip] Start addressing Bob's comments; sync
seantalts May 19, 2017
f904dc0
Refactor to avoid using inheritance for clarity's sake
seantalts May 19, 2017
be58571
Get rid of ViewElt
seantalts May 20, 2017
f9863b8
Need ViewElt still for arithmetic primitive edges
seantalts May 20, 2017
ae81a2e
cpplint
seantalts May 20, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Refactor scalar_type and value_type
We weren't getting expected behavior from some of these tests (e.g.
return_type<std::vector<matrix_v>> should be var, not matrix_v). I think
I made them simpler and they give me expected behavior for all the
use-cases I'm aware of.
  • Loading branch information
seantalts committed Mar 28, 2017
commit 74464c8a42f0f63537d7d9e371bec6ecedaf3e29
1 change: 1 addition & 0 deletions stan/math/prim/arr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <stan/math/prim/arr/meta/is_constant_struct.hpp>
#include <stan/math/prim/arr/meta/is_vector.hpp>
#include <stan/math/prim/arr/meta/length.hpp>
#include <stan/math/prim/arr/meta/scalar_type.hpp>
#include <stan/math/prim/arr/meta/value_type.hpp>
#include <stan/math/prim/arr/meta/VectorBuilderHelper.hpp>
#include <stan/math/prim/arr/meta/VectorView.hpp>
Expand Down
28 changes: 28 additions & 0 deletions stan/math/prim/arr/meta/scalar_type.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef STAN_MATH_PRIM_ARR_META_SCALAR_TYPE_HPP
#define STAN_MATH_PRIM_ARR_META_SCALAR_TYPE_HPP

#include <stan/math/prim/arr/meta/scalar_type.hpp>
#include <vector>

namespace stan {
template <typename T>
struct scalar_type<std::vector<T> > {
typedef typename scalar_type<T>::type type;
};

template <typename T>
struct scalar_type<const std::vector<T> > {
typedef typename scalar_type<T>::type type;
};

template <typename T>
struct scalar_type<std::vector<T>& > {
typedef typename scalar_type<T>::type type;
};

template <typename T>
struct scalar_type<const std::vector<T>& > {
typedef typename scalar_type<T>::type type;
};
}
#endif
2 changes: 1 addition & 1 deletion stan/math/prim/arr/meta/value_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace stan {
* Type of value stored in a standard vector with type
* <code>T</code> entries.
*/
typedef typename std::vector<T>::value_type type;
typedef T type;
};

}
Expand Down
27 changes: 22 additions & 5 deletions stan/math/prim/mat/meta/scalar_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@
#define STAN_MATH_PRIM_MAT_META_SCALAR_TYPE_HPP

#include <stan/math/prim/mat/fun/Eigen.hpp>
#include <stan/math/prim/mat/meta/is_vector.hpp>
#include <stan/math/prim/mat/meta/value_type.hpp>
#include <stan/math/prim/scal/meta/scalar_type.hpp>
#include <stan/math/prim/arr/meta/scalar_type.hpp>

namespace stan {

template <typename T>
struct scalar_type<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> > {
template <typename T, int R, int C>
struct scalar_type<Eigen::Matrix<T, R, C> > {
typedef typename scalar_type<T>::type type;
};

template <typename T, int R, int C>
struct scalar_type<const Eigen::Matrix<T, R, C> > {
typedef typename scalar_type<T>::type type;
};

template <typename T, int R, int C>
struct scalar_type<Eigen::Matrix<T, R, C>& > {
typedef typename scalar_type<T>::type type;
};

template <typename T, int R, int C>
struct scalar_type<const Eigen::Matrix<T, R, C>& > {
typedef typename scalar_type<T>::type type;
};

template <typename T>
struct scalar_type<Eigen::Block<T> > {
typedef typename scalar_type<T>::type type;
};
}
#endif
4 changes: 2 additions & 2 deletions stan/math/prim/mat/meta/value_type.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef STAN_MATH_PRIM_MAT_META_VALUE_TYPE_HPP
#define STAN_MATH_PRIM_MAT_META_VALUE_TYPE_HPP

#include <stan/math/prim/scal/meta/value_type.hpp>
#include <stan/math/prim/arr/meta/value_type.hpp>
#include <Eigen/Core>

namespace stan {
Expand All @@ -17,7 +17,7 @@ namespace stan {
*/
template <typename T, int R, int C>
struct value_type<Eigen::Matrix<T, R, C> > {
typedef typename Eigen::Matrix<T, R, C>::Scalar type;
typedef T type;
};

}
Expand Down
18 changes: 1 addition & 17 deletions stan/math/prim/scal/meta/scalar_type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,6 @@
#include <stan/math/prim/scal/meta/value_type.hpp>

namespace stan {
namespace {
template <bool is_vec, typename T>
struct scalar_type_helper {
typedef T type;
};

template <typename T>
struct scalar_type_helper<true, T> {
typedef typename
scalar_type_helper<is_vector<typename
stan::math::value_type<T>::type>::value,
typename stan::math::value_type<T>::type>::type
type;
};
}

/**
* Metaprogram structure to determine the base scalar type
* of a template argument.
Expand All @@ -31,7 +15,7 @@ namespace stan {
*/
template <typename T>
struct scalar_type {
typedef typename scalar_type_helper<is_vector<T>::value, T>::type type;
typedef T type;
};

template <typename T>
Expand Down
18 changes: 18 additions & 0 deletions test/unit/math/prim/arr/meta/scalar_type_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ TEST(MetaTraits, ScalarTypeArray) {
test::expect_same_type<int, scalar_type<vector<int> >::type>();
test::expect_same_type<double, scalar_type<vector<vector<double> > >::type>();
}

TEST(MetaTraits, ScalarTypeArrayConst) {
using stan::scalar_type;
using std::vector;

test::expect_same_type<double, scalar_type<const vector<double> >::type>();
test::expect_same_type<int, scalar_type<const vector<int> >::type>();
test::expect_same_type<double, scalar_type<const vector<vector<double> > >::type>();
}

TEST(MetaTraits, ScalarTypeArrayConstConst) {
using stan::scalar_type;
using std::vector;

test::expect_same_type<const double, scalar_type<const vector<const double> >::type>();
test::expect_same_type<const int, scalar_type<const vector<const int> >::type>();
test::expect_same_type<const double, scalar_type<const vector<const vector<const double> > >::type>();
}
6 changes: 2 additions & 4 deletions test/unit/math/prim/mat/meta/scalar_type_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ TEST(MetaTraits, ScalarTypeMat) {
expect_same_type<double, scalar_type<MatrixXd>::type>();
expect_same_type<double, scalar_type<VectorXd>::type>();
expect_same_type<double, scalar_type<RowVectorXd>::type>();

// TODO(Sean): This behavior doesn't seem to make sense prima facie
// Likely change this so that we would expect `double` here instead.
expect_same_type<MatrixXd, scalar_type<vector<MatrixXd> >::type>();
expect_same_type<double, scalar_type<vector<double> >::type>();
expect_same_type<double, scalar_type<vector<MatrixXd> >::type>();
}
3 changes: 3 additions & 0 deletions test/unit/math/prim/mat/meta/value_type_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ TEST(MathMeta, index_type) {

expect_same_type<Matrix<double,1,Dynamic>::Scalar,
value_type<Matrix<double,1,Dynamic> >::type>();

expect_same_type<Matrix<double,1,Dynamic>,
value_type<std::vector<Matrix<double,1,Dynamic> > >::type>();
}
19 changes: 19 additions & 0 deletions test/unit/math/rev/arr/meta/return_type_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <stan/math/rev/arr.hpp>
#include <gtest/gtest.h>
#include <test/unit/util.hpp>

using stan::return_type;
using stan::math::var;
using std::vector;

TEST(MetaTraits, ReturnTypeVarArray) {
test::expect_same_type<var, return_type<vector<var> >::type>();
test::expect_same_type<var, return_type<vector<var>, double>::type>();
test::expect_same_type<var, return_type<vector<var>, double>::type>();
}

TEST(MetaTraits, ReturnTypeDoubleArray) {
test::expect_same_type<double, return_type<vector<double> >::type>();
test::expect_same_type<double, return_type<vector<double>, double>::type>();
test::expect_same_type<double, return_type<vector<double>, double>::type>();
}
34 changes: 34 additions & 0 deletions test/unit/math/rev/mat/meta/return_type_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <stan/math/rev/mat.hpp>
#include <stan/math/rev/mat/fun/typedefs.hpp>
#include <gtest/gtest.h>
#include <test/unit/util.hpp>

using stan::return_type;
using stan::math::var;
using stan::math::matrix_d;
using stan::math::matrix_v;
using stan::math::vector_d;
using stan::math::vector_v;
using std::vector;

TEST(MetaTraits, ReturnTypeVarMat) {
test::expect_same_type<var, return_type<vector_v >::type>();
test::expect_same_type<var, return_type<matrix_v >::type>();
test::expect_same_type<var, return_type<matrix_v, double>::type>();
test::expect_same_type<var, return_type<matrix_v, var>::type>();
test::expect_same_type<var, return_type<matrix_d, matrix_v>::type>();
}

TEST(MetaTraits, ReturnTypeMatMultivar) {
//test::expect_same_type<var, return_type<vector<vector_v> >::type>();
test::expect_same_type<var, return_type<vector<matrix_v> >::type>();
test::expect_same_type<var, return_type<vector<matrix_v>, double>::type>();
test::expect_same_type<var, return_type<vector<matrix_v>, var>::type>();
test::expect_same_type<var, return_type<vector<matrix_d>, matrix_v>::type>();
}

TEST(MetaTraits, ReturnTypeDoubleMat) {
test::expect_same_type<double, return_type<vector_d >::type>();
test::expect_same_type<double, return_type<matrix_d, double>::type>();
test::expect_same_type<var, return_type<matrix_d, var>::type>();
}