Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 9fc4ea64c0b1
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1c052d1ff271
Choose a head ref
  • 5 commits
  • 5 files changed
  • 2 contributors

Commits on Jun 30, 2020

  1. Polynomial primitive.

    pleroy committed Jun 30, 2020
    Copy the full SHA
    f19fb59 View commit details
  2. Test polynomial primitive.

    pleroy committed Jun 30, 2020
    Copy the full SHA
    f913552 View commit details
  3. Typenames.

    pleroy committed Jun 30, 2020
    Copy the full SHA
    e4df6d3 View commit details
  4. After egg's review.

    pleroy committed Jun 30, 2020
    Copy the full SHA
    e14b590 View commit details

Commits on Jul 1, 2020

  1. Merge pull request #2620 from pleroy/Primitive

    Polynomial primitive and a few templates
    pleroy authored Jul 1, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    1c052d1 View commit details
Showing with 68 additions and 12 deletions.
  1. +12 −12 numerics/poisson_series_body.hpp
  2. +6 −0 numerics/polynomial.hpp
  3. +31 −0 numerics/polynomial_body.hpp
  4. +14 −0 numerics/polynomial_test.cpp
  5. +5 −0 quantities/named_quantities.hpp
24 changes: 12 additions & 12 deletions numerics/poisson_series_body.hpp
Original file line number Diff line number Diff line change
@@ -235,32 +235,32 @@ operator*(PoissonSeries<LValue, ldegree_, Evaluator> const& left,
// Compute all the individual terms using elementary trigonometric identities
// and put them in a multimap, because the same frequency may appear multiple
// times.
std::multimap<AngularFrequency, Result::Polynomials> terms;
std::multimap<AngularFrequency, typename Result::Polynomials> terms;
auto const aperiodic = left.aperiodic_ * right.aperiodic_;
for (auto const& [ω, polynomials] : left.periodic_) {
terms.emplace(
ω,
Result::Polynomials{/*sin=*/polynomials.sin * right.aperiodic_,
/*cos=*/polynomials.cos * right.aperiodic_});
terms.emplace(ω,
typename Result::Polynomials{
/*sin=*/polynomials.sin * right.aperiodic_,
/*cos=*/polynomials.cos * right.aperiodic_});
}
for (auto const& [ω, polynomials] : right.periodic_) {
terms.emplace(
ω,
Result::Polynomials{/*sin=*/left.aperiodic_ * polynomials.sin,
/*cos=*/left.aperiodic_ * polynomials.cos});
terms.emplace(ω,
typename Result::Polynomials{
/*sin=*/left.aperiodic_ * polynomials.sin,
/*cos=*/left.aperiodic_ * polynomials.cos});
}
for (auto const& [ωl, polynomials_left] : left.periodic_) {
for (auto const& [ωr, polynomials_right] : right.periodic_) {
terms.emplace(ωl - ωr,
Result::Polynomials{
typename Result::Polynomials{
/*sin=*/(-polynomials_left.cos * polynomials_right.sin +
polynomials_left.sin * polynomials_right.cos) /
2,
/*cos=*/(polynomials_left.sin * polynomials_right.sin +
polynomials_left.cos * polynomials_right.cos) /
2});
terms.emplace(ωl + ωr,
Result::Polynomials{
typename Result::Polynomials{
/*sin=*/(polynomials_left.cos * polynomials_right.sin +
polynomials_left.sin * polynomials_right.cos) /
2,
@@ -271,7 +271,7 @@ operator*(PoissonSeries<LValue, ldegree_, Evaluator> const& left,
}

// Now group the terms together by frequency.
Result::PolynomialsByAngularFrequency periodic;
typename Result::PolynomialsByAngularFrequency periodic;
std::optional<AngularFrequency> previous_ω;
for (auto it = terms.cbegin(); it != terms.cend(); ++it) {
auto const& ω = it->first;
6 changes: 6 additions & 0 deletions numerics/polynomial.hpp
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ using base::not_null;
using geometry::Point;
using quantities::Derivative;
using quantities::Derivatives;
using quantities::Primitive;
using quantities::Product;
using quantities::Quotient;

@@ -82,6 +83,11 @@ class PolynomialInMonomialBasis : public Polynomial<Value, Argument> {
Derivative<Value, Argument, order>, Argument, degree_ - order, Evaluator>
Derivative() const;

// The constant term of the result is zero.
PolynomialInMonomialBasis<
Primitive<Value, Argument>, Argument, degree_ + 1, Evaluator>
Primitive() const;

PolynomialInMonomialBasis& operator+=(const PolynomialInMonomialBasis& right);
PolynomialInMonomialBasis& operator-=(const PolynomialInMonomialBasis& right);

31 changes: 31 additions & 0 deletions numerics/polynomial_body.hpp
Original file line number Diff line number Diff line change
@@ -42,6 +42,24 @@ TupleDerivation<Tuple, order, std::index_sequence<indices...>>::Derive(
std::get<order + indices>(tuple)...);
}

template<typename Argument, typename Tuple,
typename = std::make_index_sequence<std::tuple_size_v<Tuple>>>
struct TupleIntegration;

template<typename Argument, typename Tuple, std::size_t... indices>
struct TupleIntegration<Argument, Tuple, std::index_sequence<indices...>> {
static constexpr auto Integrate(Tuple const& tuple);
};

template<typename Argument, typename Tuple, std::size_t... indices>
constexpr auto
TupleIntegration<Argument, Tuple, std::index_sequence<indices...>>::Integrate(
Tuple const& tuple) {
static constexpr auto zero = std::tuple_element_t<0, Tuple>{} * Argument{};
return std::make_tuple(
zero, std::get<indices>(tuple) / static_cast<double>(indices + 1)...);
}

template<typename Tuple, int k, int size = std::tuple_size_v<Tuple>>
struct TupleSerializer : not_constructible {
static void WriteToMessage(
@@ -184,6 +202,19 @@ Derivative() const {
TupleDerivation<Coefficients, order>::Derive(coefficients_));
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
PolynomialInMonomialBasis<
Primitive<Value, Argument>, Argument, degree_ + 1, Evaluator>
PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>::
Primitive() const {
return PolynomialInMonomialBasis<
quantities::Primitive<Value, Argument>, Argument,
degree_ + 1, Evaluator>(
TupleIntegration<Argument, Coefficients>::
Integrate(coefficients_));
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>&
14 changes: 14 additions & 0 deletions numerics/polynomial_test.cpp
Original file line number Diff line number Diff line change
@@ -244,6 +244,20 @@ TEST_F(PolynomialTest, Derivative) {
p3.Derivative<3>().Evaluate(0 * Second));
}

TEST_F(PolynomialTest, Primitive) {
using P2 = PolynomialInMonomialBasis<Temperature, Time, 2, HornerEvaluator>;
P2 const p2({1 * Kelvin, 3 * Kelvin / Second, -8 * Kelvin / Second / Second});

EXPECT_THAT(p2.Primitive().Evaluate(0 * Second),
AlmostEquals(0 * Kelvin * Second, 0));
EXPECT_THAT(p2.Primitive().Evaluate(1 * Second),
AlmostEquals(-1.0 / 6.0 * Kelvin * Second, 5));
EXPECT_THAT(p2.Primitive().Evaluate(-1 * Second),
AlmostEquals(19.0 / 6.0 * Kelvin * Second, 1));
EXPECT_THAT(p2.Primitive().Evaluate(2 * Second),
AlmostEquals(-40.0 / 3.0 * Kelvin * Second, 1));
}

TEST_F(PolynomialTest, EvaluateConstant) {
PolynomialInMonomialBasis<Entropy, Time, 0, HornerEvaluator> const
horner_boltzmann(std::make_tuple(BoltzmannConstant));
5 changes: 5 additions & 0 deletions quantities/named_quantities.hpp
Original file line number Diff line number Diff line change
@@ -43,6 +43,11 @@ using Derivative = typename std::conditional_t<
Value,
Quotient<Difference<Value>, Exponentiation<Difference<Argument>, order>>>;

// The result type of the primitive of a |Value|-valued function with respect to
// its |Argument|-valued argument.
template<typename Value, typename Argument>
using Primitive = Product<Value, Argument>;

// |Variation<T>| is the type of the time derivative of a |T|-valued function.
template<typename T>
using Variation = Derivative<T, Time>;