Skip to content

Commit

Permalink
Extract function into random_polynomial_matrix.h
Browse files Browse the repository at this point in the history
This code is test-only, and uses uncontrolled randomness,
so doesn't belong in the common library.  (It also happens
to break with Eigen 3.2, so got in my way.)
  • Loading branch information
jwnimmer-tri committed Oct 13, 2016
1 parent c1d81ae commit 01eec0b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
16 changes: 0 additions & 16 deletions drake/common/polynomial.h
Original file line number Diff line number Diff line change
Expand Up @@ -423,22 +423,6 @@ class DRAKE_EXPORT Polynomial {
return os;
}

/// Obtains a matrix of random unvariate Polynomials of the specified size.
static Eigen::Matrix<Polynomial, Eigen::Dynamic, Eigen::Dynamic>
RandomPolynomialMatrix(Eigen::Index num_coefficients_per_polynomial,
Eigen::Index rows, Eigen::Index cols) {
Eigen::Matrix<Polynomial, Eigen::Dynamic, Eigen::Dynamic> mat(rows, cols);
for (Eigen::Index row = 0; row < mat.rows(); ++row) {
for (Eigen::Index col = 0; col < mat.cols(); ++col) {
auto coeffs =
(Eigen::Matrix<CoefficientType, Eigen::Dynamic, 1>::Random(
num_coefficients_per_polynomial)).eval();
mat(row, col) = Polynomial(coeffs);
}
}
return mat;
}

//@{
/** Variable name/ID conversion facility. */
static bool IsValidVariableName(const std::string name);
Expand Down
13 changes: 7 additions & 6 deletions drake/common/test/polynomial_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "gtest/gtest.h"

#include "drake/common/eigen_matrix_compare.h"
#include "drake/common/test/random_polynomial_matrix.h"

using Eigen::VectorXd;
using std::default_random_engine;
Expand Down Expand Up @@ -146,12 +147,12 @@ void testPolynomialMatrix() {
int rows_B = cols_A;
int cols_B = matrix_size_distribution(generator);

auto A = Polynomial<CoefficientType>::RandomPolynomialMatrix(num_coefficients,
rows_A, cols_A);
auto B = Polynomial<CoefficientType>::RandomPolynomialMatrix(num_coefficients,
rows_B, cols_B);
auto C = Polynomial<CoefficientType>::RandomPolynomialMatrix(num_coefficients,
rows_A, cols_A);
auto A = test::RandomPolynomialMatrix<CoefficientType>(num_coefficients,
rows_A, cols_A);
auto B = test::RandomPolynomialMatrix<CoefficientType>(num_coefficients,
rows_B, cols_B);
auto C = test::RandomPolynomialMatrix<CoefficientType>(num_coefficients,
rows_A, cols_A);
auto product = A * B;
auto sum = A + C;

Expand Down
28 changes: 28 additions & 0 deletions drake/common/test/random_polynomial_matrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "drake/common/polynomial.h"

namespace drake {
namespace test {

/// Obtains a matrix of random unvariate Polynomials of the specified size.
template <typename CoefficientType = double>
static Eigen::Matrix<Polynomial<CoefficientType>,
Eigen::Dynamic, Eigen::Dynamic>
RandomPolynomialMatrix(Eigen::Index num_coefficients_per_polynomial,
Eigen::Index rows, Eigen::Index cols) {
Eigen::Matrix<Polynomial<CoefficientType>, Eigen::Dynamic, Eigen::Dynamic>
mat(rows, cols);
for (Eigen::Index row = 0; row < mat.rows(); ++row) {
for (Eigen::Index col = 0; col < mat.cols(); ++col) {
auto coeffs =
(Eigen::Matrix<CoefficientType, Eigen::Dynamic, 1>::Random(
num_coefficients_per_polynomial)).eval();
mat(row, col) = Polynomial<CoefficientType>(coeffs);
}
}
return mat;
}

} // namespace test
} // namespace drake
6 changes: 5 additions & 1 deletion drake/systems/trajectories/PiecewisePolynomial.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "drake/systems/trajectories/PiecewisePolynomial.h"

#include "drake/common/drake_assert.h"
#include "drake/common/test/random_polynomial_matrix.h"

using namespace std;
using namespace Eigen;
Expand Down Expand Up @@ -320,6 +321,9 @@ Eigen::Index PiecewisePolynomial<CoefficientType>::cols() const {
"PiecewisePolynomial has no segments. Number of columns is undefined.");
}

// TODO(jwnimmer-tri) This method should move into legacy test-only code (in
// other words, some other class and header). Unseeded randomness leads to
// hard-to-debug failures.
template <typename CoefficientType>
PiecewisePolynomial<CoefficientType> PiecewisePolynomial<
CoefficientType>::random(Eigen::Index rows, Eigen::Index cols,
Expand All @@ -331,7 +335,7 @@ PiecewisePolynomial<CoefficientType> PiecewisePolynomial<
for (Eigen::Index segment_index = 0; segment_index < num_segments;
++segment_index) {
polynomials.push_back(
PolynomialType::RandomPolynomialMatrix(
drake::test::RandomPolynomialMatrix<CoefficientType>(
num_coefficients_per_polynomial, rows, cols));
}
return PiecewisePolynomial<CoefficientType>(polynomials, segment_times);
Expand Down
3 changes: 2 additions & 1 deletion drake/util/test/testLCMUtil.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "drake/common/eigen_matrix_compare.h"
#include "drake/common/test/random_polynomial_matrix.h"
#include "drake/math/random_rotation.h"
#include "drake/util/lcmUtil.h"

Expand All @@ -25,7 +26,7 @@ GTEST_TEST(TestLcmUtil, testPolynomial) {
}

GTEST_TEST(TestLcmUtil, testPolynomialMatrix) {
auto poly_matrix = Polynomial<double>::RandomPolynomialMatrix(6, 5, 8);
auto poly_matrix = drake::test::RandomPolynomialMatrix<double>(6, 5, 8);
drake::lcmt_polynomial_matrix msg;
encodePolynomialMatrix<Eigen::Dynamic, Eigen::Dynamic>(poly_matrix, msg);
EXPECT_EQ(static_cast<int>(msg.rows), static_cast<int>(poly_matrix.rows()));
Expand Down

0 comments on commit 01eec0b

Please sign in to comment.