Skip to content

Commit

Permalink
polynomial: fix printing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Chrétien committed Jan 26, 2015
1 parent f701a7a commit bbf615b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
17 changes: 11 additions & 6 deletions include/roboptim/trajectory/polynomial.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,22 @@ namespace trajectory
> std::numeric_limits<value_type>::epsilon ())
{
if (printed_before)
o << " + ";
o << coefs_[idx] << " * x**" << idx;
o << ((coefs_[idx] >= 0.) ? " + " : " - ")
<< std::abs (coefs_[idx]);
else
o << coefs_[idx];
o << " * x**" << idx;
printed_before = true;
}
}
if (std::abs (coefs_[0])
> std::numeric_limits<value_type>::epsilon ())
{
if (printed_before)
o << " + ";
o << coefs_[0];
o << ((coefs_[0] >= 0.) ? " + " : " - ")
<< std::abs (coefs_[0]);
else
o << coefs_[0];
}
else if (!printed_before)
{
Expand All @@ -115,9 +120,9 @@ namespace trajectory
{
if (std::abs (t0_)
> std::numeric_limits<value_type>::epsilon ())
o << " (x = t - " << t0_ << ")";
o << " (x = t - " << t0_ << ")";
else
o << " (x = t)";
o << " (x = t)";
}

return o;
Expand Down
16 changes: 16 additions & 0 deletions tests/polynomial.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@ void test_print ()
poly3_t null (1., params);
(*output) << null << std::endl;

params[0] = -42;
poly3_t negative_constant (2., params);
(*output) << negative_constant << std::endl;

params[0] = 42;
poly3_t constant (2., params);
(*output) << constant << std::endl;
Expand All @@ -527,6 +531,18 @@ void test_print ()
poly3_t linear2 (3., params);
(*output) << linear2 << std::endl;

params[0] = -42;
poly3_t linear3 (0., params);
(*output) << linear3 << std::endl;

params[1] = -1;
poly3_t linear4 (3., params);
(*output) << linear4 << std::endl;

params[0] = 42;
poly3_t linear5 (3., params);
(*output) << linear5 << std::endl;

std::cout << output->str () << std::endl;

BOOST_CHECK (output->match_pattern ());
Expand Down
4 changes: 4 additions & 0 deletions tests/polynomial.stdout
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
0
-42
42
1 * x**1 + 42 (x = t)
1 * x**1 + 42 (x = t - 3)
1 * x**1 - 42 (x = t)
-1 * x**1 - 42 (x = t - 3)
-1 * x**1 + 42 (x = t - 3)

0 comments on commit bbf615b

Please sign in to comment.