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: c5a5a7a946e2
Choose a base ref
...
head repository: mockingbirdnest/Principia
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: e42f7cfd8b4f
Choose a head ref
  • 8 commits
  • 3 files changed
  • 1 contributor

Commits on May 25, 2021

  1. Mixed affine operators.

    pleroy committed May 25, 2021
    Copy the full SHA
    2d8643a View commit details
  2. Copy the full SHA
    274aefe View commit details
  3. Copy the full SHA
    953d236 View commit details
  4. After egg's review.

    pleroy committed May 25, 2021
    Copy the full SHA
    37b01c1 View commit details
  5. Fix a test.

    pleroy committed May 25, 2021
    Copy the full SHA
    32d550f View commit details
  6. Link is dumb.

    pleroy committed May 25, 2021
    Copy the full SHA
    65ec84a View commit details
  7. Encoding.

    pleroy committed May 25, 2021
    Copy the full SHA
    5ea69ae View commit details

Commits on May 26, 2021

  1. Merge pull request #2996 from pleroy/AffinePolynomials

    Additive operators for converting vector-valued and affine valued polynomials
    pleroy authored May 26, 2021

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e42f7cf View commit details
Showing with 183 additions and 2 deletions.
  1. +65 −1 numerics/polynomial.hpp
  2. +92 −1 numerics/polynomial_body.hpp
  3. +26 −0 numerics/polynomial_test.cpp
66 changes: 65 additions & 1 deletion numerics/polynomial.hpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


#pragma once

#include <algorithm>
@@ -18,6 +18,11 @@
#include "quantities/tuples.hpp"
#include "serialization/numerics.pb.h"

// The presence of an operator+ below causes a bizarre compilation error in
// seemingly unrelated code in PolynomialTest.VectorSpace.
#define PRINCIPIA_COMPILER_MSVC_HANDLES_POLYNOMIAL_OPERATORS \
!PRINCIPIA_COMPILER_MSVC || !(_MSC_FULL_VER == 192'930'036)

namespace principia {
namespace numerics {
FORWARD_DECLARE_FROM(
@@ -51,6 +56,7 @@ using geometry::Hilbert;
using geometry::Point;
using quantities::Derivative;
using quantities::Derivatives;
using quantities::Difference;
using quantities::Primitive;
using quantities::Product;
using quantities::Quotient;
@@ -201,6 +207,30 @@ class PolynomialInMonomialBasis : public Polynomial<Value_, Argument_> {
friend operator*(
PolynomialInMonomialBasis<L, A, l, E> const& left,
PolynomialInMonomialBasis<R, A, r, E> const& right);
#if PRINCIPIA_COMPILER_MSVC_HANDLES_POLYNOMIAL_OPERATORS
template<typename V, typename A, int l,
template<typename, typename, int> typename E>
constexpr PolynomialInMonomialBasis<V, A, l, E>
friend operator+(
PolynomialInMonomialBasis<Difference<V>, A, l, E> const& left,
V const& right);
#endif
template<typename V, typename A, int r,
template<typename, typename, int> typename E>
constexpr PolynomialInMonomialBasis<V, A, r, E>
friend operator+(
V const& left,
PolynomialInMonomialBasis<Difference<V>, A, r, E> const& right);
template<typename V, typename A, int l,
template<typename, typename, int> typename E>
constexpr PolynomialInMonomialBasis<Difference<V>, A, l, E>
friend operator-(PolynomialInMonomialBasis<V, A, l, E> const& left,
V const& right);
template<typename V, typename A, int r,
template<typename, typename, int> typename E>
constexpr PolynomialInMonomialBasis<Difference<V>, A, r, E>
friend operator-(V const& left,
PolynomialInMonomialBasis<V, A, r, E> const& right);
template<typename L, typename R, typename A,
int l, int r,
template<typename, typename, int> typename E>
@@ -302,6 +332,40 @@ operator*(
PolynomialInMonomialBasis<RValue, Argument, rdegree_, Evaluator> const&
right);

// Additive operators polynomial ± constant.

#if PRINCIPIA_COMPILER_MSVC_HANDLES_POLYNOMIAL_OPERATORS
template<typename Value, typename Argument, int ldegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Value, Argument, ldegree_, Evaluator>
operator+(PolynomialInMonomialBasis<Difference<Value>, Argument,
ldegree_, Evaluator> const& left,
Value const& right);
#endif

template<typename Value, typename Argument, int rdegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Value, Argument, rdegree_, Evaluator>
operator+(Value const& left,
PolynomialInMonomialBasis<Difference<Value>, Argument,
rdegree_, Evaluator> const& right);

template<typename Value, typename Argument, int ldegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Difference<Value>, Argument,
ldegree_, Evaluator>
operator-(PolynomialInMonomialBasis<Value, Argument,
ldegree_, Evaluator> const& left,
Value const& right);

template<typename Value, typename Argument, int rdegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Difference<Value>, Argument,
rdegree_, Evaluator>
operator-(Value const& left,
PolynomialInMonomialBasis<Value, Argument,
rdegree_, Evaluator> const& right);

// Application monoid.

template<typename LValue, typename RValue,
93 changes: 92 additions & 1 deletion numerics/polynomial_body.hpp
Original file line number Diff line number Diff line change
@@ -190,7 +190,6 @@ TupleComposition<LTuple, RTuple, std::index_sequence<left_indices...>>::Compose(
}



template<typename Tuple, int order,
typename = std::make_index_sequence<std::tuple_size_v<Tuple> - order>>
struct TupleDerivation;
@@ -208,6 +207,25 @@ TupleDerivation<Tuple, order, std::index_sequence<indices...>>::Derive(
std::get<order + indices>(tuple)...);
}


template<typename Tuple, int count,
typename = std::make_index_sequence<std::tuple_size_v<Tuple> - count>>
struct TupleDropper;

template<typename Tuple, int count, std::size_t... indices>
struct TupleDropper<Tuple, count, std::index_sequence<indices...>> {
// Drops the first |count| elements of |tuple|.
static constexpr auto Drop(Tuple const& tuple);
};

template<typename Tuple, int count, std::size_t... indices>
constexpr auto
TupleDropper<Tuple, count, std::index_sequence<indices...>>::Drop(
Tuple const& tuple) {
return std::make_tuple(std::get<count + indices>(tuple)...);
}


template<typename Argument, typename Tuple,
typename = std::make_index_sequence<std::tuple_size_v<Tuple>>>
struct TupleIntegration;
@@ -227,6 +245,7 @@ TupleIntegration<Argument, Tuple, std::index_sequence<indices...>>::Integrate(
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(
@@ -668,6 +687,78 @@ operator*(
left.origin_);
}

#if PRINCIPIA_COMPILER_MSVC_HANDLES_POLYNOMIAL_OPERATORS
template<typename Value, typename Argument, int ldegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Value, Argument, ldegree_, Evaluator>
operator+(PolynomialInMonomialBasis<Difference<Value>, Argument,
ldegree_, Evaluator> const& left,
Argument const& right) {
auto const dropped_left_coefficients =
TupleDropper<typename PolynomialInMonomialBasis<
Difference<Value>, Argument, ldegree_, Evaluator>::
Coefficients,
/*count=*/1>::Drop(left.coefficients_);
return PolynomialInMonomialBasis<Value, Argument, ldegree_, Evaluator>(
std::tuple_cat(std::tuple(std::get<0>(left.coefficients_) + right),
dropped_left_coefficients),
left.origin_);
}
#endif

template<typename Value, typename Argument, int rdegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Value, Argument, rdegree_, Evaluator>
operator+(Value const& left,
PolynomialInMonomialBasis<Difference<Value>, Argument,
rdegree_, Evaluator> const& right) {
auto const dropped_right_coefficients =
TupleDropper<typename PolynomialInMonomialBasis<
Difference<Value>, Argument, rdegree_, Evaluator>::
Coefficients,
/*count=*/1>::Drop(right.coefficients_);
return PolynomialInMonomialBasis<Value, Argument, rdegree_, Evaluator>(
std::tuple_cat(std::tuple(left + std::get<0>(right.coefficients_)),
dropped_right_coefficients),
right.origin_);
}

template<typename Value, typename Argument, int ldegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Difference<Value>, Argument,
ldegree_, Evaluator>
operator-(PolynomialInMonomialBasis<Value, Argument,
ldegree_, Evaluator> const& left,
Value const& right) {
auto const dropped_left_coefficients =
TupleDropper<typename PolynomialInMonomialBasis<
Value, Argument, ldegree_, Evaluator>::Coefficients,
/*count=*/1>::Drop(left.coefficients_);
return PolynomialInMonomialBasis<Difference<Value>, Argument,
ldegree_, Evaluator>(
std::tuple_cat(std::tuple(std::get<0>(left.coefficients_) - right),
dropped_left_coefficients),
left.origin_);
}

template<typename Value, typename Argument, int rdegree_,
template<typename, typename, int> typename Evaluator>
constexpr PolynomialInMonomialBasis<Difference<Value>, Argument,
rdegree_, Evaluator>
operator-(Value const& left,
PolynomialInMonomialBasis<Value, Argument,
rdegree_, Evaluator> const& right) {
auto const dropped_right_coefficients =
TupleDropper<typename PolynomialInMonomialBasis<
Value, Argument, rdegree_, Evaluator>::Coefficients,
/*count=*/1>::Drop(right.coefficients_);
return PolynomialInMonomialBasis<Difference<Value>, Argument,
rdegree_, Evaluator>(
std::tuple_cat(std::tuple(left - std::get<0>(right.coefficients_)),
dropped_right_coefficients),
right.origin_);
}

template<typename LValue, typename RValue,
typename Argument, int ldegree_, int rdegree_,
template<typename, typename, int> typename Evaluator>
26 changes: 26 additions & 0 deletions numerics/polynomial_test.cpp
Original file line number Diff line number Diff line change
@@ -267,6 +267,32 @@ TEST_F(PolynomialTest, Ring) {
}
}

TEST_F(PolynomialTest, Affine) {
using P0A = PolynomialInMonomialBasis<Instant, Time, 0, HornerEvaluator>;
using P0V = PolynomialInMonomialBasis<Time, Time, 0, HornerEvaluator>;

P0A const p0a(std::tuple{Instant() + 1 * Second});
P0V const p0v(std::tuple{2 * Second});
#if PRINCIPIA_COMPILER_MSVC_HANDLES_POLYNOMIAL_OPERATORS
{
P0A const p = p0v + Instant();
EXPECT_THAT(p(3 * Second), AlmostEquals(Instant() + 2 * Second, 0));
}
#endif
{
P0A const p = Instant() + p0v;
EXPECT_THAT(p(3 * Second), AlmostEquals(Instant() + 2 * Second, 0));
}
{
P0V const p = p0a - Instant();
EXPECT_THAT(p(3 * Second), AlmostEquals(1 * Second, 0));
}
{
P0V const p = Instant() - p0a;
EXPECT_THAT(p(3 * Second), AlmostEquals(-1 * Second, 0));
}
}

// Compose contains a fold expression which fails to compile in Clang because of
// https://bugs.llvm.org/show_bug.cgi?id=30590. That bug will be fixed post-
// 11.0.0. Since we don't use Compose as of this writing, and working around