Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
66 changes: 66 additions & 0 deletions stan/math/fwd/mat/fun/quad_form_sym.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#ifndef STAN_MATH_FWD_MAT_FUN_QUAD_FORM_SYM_HPP
#define STAN_MATH_FWD_MAT_FUN_QUAD_FORM_SYM_HPP

#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/mat/fun/multiply.hpp>
#include <stan/math/fwd/mat/fun/dot_product.hpp>
#include <stan/math/prim/mat/fun/quad_form_sym.hpp>

namespace stan {
namespace math {

template<int RA, int CA, int RB, int CB, typename T>
inline Eigen::Matrix<fvar<T>, CB, CB>
quad_form_sym(const Eigen::Matrix<fvar<T>, RA, CA>& A,
const Eigen::Matrix<double, RB, CB>& B) {
check_square("quad_form_sym", "A", A);
check_multiplicable("quad_form_sym",
"A", A,
"B", B);
check_symmetric("quad_form_sym", "A", A);
Eigen::Matrix<fvar<T>, CB, CB>
ret(multiply(transpose(B), multiply(A, B)));
return T(0.5) * (ret + transpose(ret));
}

template<int RA, int CA, int RB, typename T>
inline fvar<T>
quad_form_sym(const Eigen::Matrix<fvar<T>, RA, CA>& A,
const Eigen::Matrix<double, RB, 1>& B) {
check_square("quad_form_sym", "A", A);
check_multiplicable("quad_form_sym",
"A", A,
"B", B);
check_symmetric("quad_form_sym", "A", A);
return dot_product(B, multiply(A, B));
}
template<int RA, int CA, int RB, int CB, typename T>
inline Eigen::Matrix<fvar<T>, CB, CB>
quad_form_sym(const Eigen::Matrix<double, RA, CA>& A,
const Eigen::Matrix<fvar<T>, RB, CB>& B) {
check_square("quad_form_sym", "A", A);
check_multiplicable("quad_form_sym",
"A", A,
"B", B);
check_symmetric("quad_form_sym", "A", A);
Eigen::Matrix<fvar<T>, CB, CB>
ret(multiply(transpose(B), multiply(A, B)));
return T(0.5) * (ret + transpose(ret));
}

template<int RA, int CA, int RB, typename T>
inline fvar<T>
quad_form_sym(const Eigen::Matrix<double, RA, CA>& A,
const Eigen::Matrix<fvar<T>, RB, 1>& B) {
check_square("quad_form_sym", "A", A);
check_multiplicable("quad_form_sym",
"A", A,
"B", B);
check_symmetric("quad_form_sym", "A", A);
return dot_product(B, multiply(A, B));
}
}
}

#endif

170 changes: 170 additions & 0 deletions test/unit/math/fwd/mat/fun/quad_form_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <stan/math/prim/mat/fun/quad_form.hpp>
#include <stan/math/prim/mat/fun/quad_form_sym.hpp>
#include <stan/math/fwd/mat/fun/quad_form_sym.hpp>
#include <stan/math/fwd/mat/fun/typedefs.hpp>
#include <stan/math/fwd/mat/fun/multiply.hpp>
#include <stan/math/fwd/mat/fun/dot_product.hpp>
Expand All @@ -9,8 +10,13 @@
#include <stan/math/fwd/core.hpp>
#include <stan/math/fwd/scal/fun/fabs.hpp>
#include <stan/math/fwd/scal/fun/value_of.hpp>
#include <stan/math/fwd/scal/fun/value_of_rec.hpp>
#include <stan/math/prim/mat/fun/value_of_rec.hpp>
#include <stan/math/rev/scal/fun/value_of_rec.hpp>
#include <stan/math/prim/scal/err/check_not_nan.hpp>

using stan::math::fvar;
using stan::math::check_not_nan;

TEST(AgradFwdMatrixQuadForm, quad_form_mat_fd) {
using stan::math::quad_form;
Expand Down Expand Up @@ -118,6 +124,50 @@ TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_fd) {
EXPECT_FLOAT_EQ(810, resd(1,1).d_);
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_dfd) {
using stan::math::quad_form_sym;
using stan::math::matrix_fd;
using stan::math::check_not_nan;

Eigen::Matrix<double, -1, -1> ad(4,4);
matrix_fd bd(4,2);

bd << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
matrix_fd resd = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",resd));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to add a test here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these are all just examples that I can instantiate something that couldn't be instantiated; these tests won't compile in current develop. I'll add EXPECT_NO_THROW(check_not_nan(resd)) tests in all of these


TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_fdd) {
using stan::math::quad_form_sym;
using stan::math::matrix_fd;

matrix_fd ad(4,4);
Eigen::Matrix<double, -1, -1> bd(4,2);

bd << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
matrix_fd resd = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",resd));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add an EXPECT_* here?



TEST(AgradFwdMatrixQuadForm, quad_form_vec_fd) {
using stan::math::quad_form;
using stan::math::matrix_fd;
Expand Down Expand Up @@ -203,6 +253,44 @@ TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_fd) {
EXPECT_FLOAT_EQ(14320, res.d_);
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_dfd) {
using stan::math::quad_form_sym;
using stan::math::vector_fd;

Eigen::Matrix<double, -1, -1> ad(4,4);
vector_fd bd(4);
fvar<double> res;

bd << 100, 0, -3, 5;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
res = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",res));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test


TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_fdd) {
using stan::math::quad_form_sym;
using stan::math::matrix_fd;

matrix_fd ad(4,4);
Eigen::Matrix<double, -1, 1> bd(4);
fvar<double> res;

bd << 100, 0, -3, 5;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
res = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",res));
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add test


TEST(AgradFwdMatrixQuadForm, quad_form_sym_symmetry_fd) {
using stan::math::quad_form_sym;
using stan::math::matrix_fd;
Expand Down Expand Up @@ -428,6 +516,50 @@ TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_ffd) {
EXPECT_FLOAT_EQ(810, resd(1,1).d_.val_);
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_dffd) {
using stan::math::quad_form_sym;
using stan::math::matrix_ffd;

Eigen::Matrix<double, -1, -1> ad(4,4);
matrix_ffd bd(4,2);

bd << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;


// fvar<fvar<double> > - fvar<fvar<double> >
matrix_ffd resd = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",resd));
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_mat_ffdd) {
using stan::math::quad_form_sym;
using stan::math::matrix_ffd;

matrix_ffd ad(4,4);
Eigen::Matrix<double, -1, -1> bd(4,2);

bd << 100, 10,
0, 1,
-3, -3,
5, 2;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;


// fvar<fvar<double> > - fvar<fvar<double> >
matrix_ffd resd = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",resd));
}

TEST(AgradFwdMatrixQuadForm, quad_form_vec_ffd) {
using stan::math::quad_form;
using stan::math::matrix_ffd;
Expand Down Expand Up @@ -471,6 +603,44 @@ TEST(AgradFwdMatrixQuadForm, quad_form_vec_ffd) {
EXPECT_FLOAT_EQ(15226, res.d_.val_);
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_dffd) {
using stan::math::quad_form_sym;
using stan::math::vector_ffd;

Eigen::Matrix<double, -1, -1> ad(4,4);
vector_ffd bd(4);
fvar<fvar<double> > res;

bd << 100, 0, -3, 5;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
res = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",res));
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_ffdd) {
using stan::math::quad_form_sym;
using stan::math::matrix_ffd;

matrix_ffd ad(4,4);
Eigen::Matrix<double, -1, 1> bd(4);
fvar<fvar<double> > res;

bd << 100, 0, -3, 5;
ad << 2.0, 3.0, 4.0, 5.0,
3.0, 10.0, 2.0, 2.0,
4.0, 2.0, 7.0, 1.0,
5.0, 2.0, 1.0, 112.0;

// fvar<double> - fvar<double>
res = quad_form_sym(ad,bd);
EXPECT_NO_THROW(check_not_nan("quad_form_sym","resd",res));
}

TEST(AgradFwdMatrixQuadForm, quad_form_sym_vec_ffd) {
using stan::math::quad_form_sym;
using stan::math::matrix_ffd;
Expand Down
Loading