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

Commits on Aug 25, 2020

  1. Copy the full SHA
    318e11d View commit details
  2. Copy the full SHA
    c49e79e View commit details
  3. Undo some Poisson changes.

    pleroy committed Aug 25, 2020
    Copy the full SHA
    a72be29 View commit details
  4. Copy the full SHA
    dc10043 View commit details
  5. Format.

    pleroy committed Aug 25, 2020
    Copy the full SHA
    9e7cc4e View commit details
  6. Merge pull request #2694 from pleroy/Affine

    Support for affine-valued polynomials
    pleroy authored Aug 25, 2020
    Copy the full SHA
    1138f40 View commit details
Showing with 57 additions and 13 deletions.
  1. +2 −0 numerics/poisson_series.hpp
  2. +11 −4 numerics/polynomial.hpp
  3. +10 −7 numerics/polynomial_body.hpp
  4. +30 −0 numerics/polynomial_test.cpp
  5. +3 −2 quantities/named_quantities.hpp
  6. +1 −0 serialization/numerics.proto
2 changes: 2 additions & 0 deletions numerics/poisson_series.hpp
Original file line number Diff line number Diff line change
@@ -59,6 +59,8 @@ class PoissonSeries {

// TODO(phl): Use designated initializers for this struct once this project
// can be compiled using c++latest.
// TODO(phl): If we wanted to have Poisson series returning affine values,
// these polynomials should be changed to return Difference<Value>.
struct Polynomials {
Polynomial sin;
Polynomial cos;
15 changes: 11 additions & 4 deletions numerics/polynomial.hpp
Original file line number Diff line number Diff line change
@@ -5,9 +5,11 @@
#include <optional>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>

#include "base/not_null.hpp"
#include "base/traits.hpp"
#include "geometry/point.hpp"
#include "quantities/named_quantities.hpp"
#include "quantities/tuples.hpp"
@@ -38,6 +40,7 @@ FORWARD_DECLARE_FUNCTION_FROM(
namespace numerics {
namespace internal_polynomial {

using base::is_instance_of_v;
using base::not_constructible;
using base::not_null;
using geometry::Point;
@@ -118,8 +121,10 @@ class PolynomialInMonomialBasis : public Polynomial<Value, Argument> {
Derivative() const;

// The constant term of the result is zero.
PolynomialInMonomialBasis<
Primitive<Value, Argument>, Argument, degree_ + 1, Evaluator>
template<typename V = Value,
typename = std::enable_if_t<!base::is_instance_of_v<Point, V>>>
PolynomialInMonomialBasis<Primitive<Value, Argument>, Argument,
degree_ + 1, Evaluator>
Primitive() const;

PolynomialInMonomialBasis& operator+=(PolynomialInMonomialBasis const& right);
@@ -228,8 +233,10 @@ class PolynomialInMonomialBasis<Value, Point<Argument>, degree_, Evaluator>
Derivative() const;

// The constant term of the result is zero.
PolynomialInMonomialBasis<
Primitive<Value, Argument>, Point<Argument>, degree_ + 1, Evaluator>
template<typename V = Value,
typename = std::enable_if_t<!base::is_instance_of_v<Point, V>>>
PolynomialInMonomialBasis<Primitive<Value, Argument>, Point<Argument>,
degree_ + 1, Evaluator>
Primitive() const;

PolynomialInMonomialBasis& operator+=(const PolynomialInMonomialBasis& right);
17 changes: 10 additions & 7 deletions numerics/polynomial_body.hpp
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ namespace internal_polynomial {
using base::is_instance_of_v;
using base::make_not_null_unique;
using base::not_constructible;
using geometry::DoubleOrQuantityOrMultivectorSerializer;
using geometry::DoubleOrQuantityOrPointOrMultivectorSerializer;
using geometry::cartesian_product::operator+;
using geometry::cartesian_product::operator-;
using geometry::cartesian_product::operator*;
@@ -175,7 +175,8 @@ template<typename Argument, typename Tuple, std::size_t... indices>
constexpr auto
TupleIntegration<Argument, Tuple, std::index_sequence<indices...>>::Integrate(
Tuple const& tuple) {
constexpr auto zero = std::tuple_element_t<0, Tuple>{} * Argument{};
constexpr auto zero =
quantities::Primitive<std::tuple_element_t<0, Tuple>, Argument>{};
return std::make_tuple(
zero, std::get<indices>(tuple) / static_cast<double>(indices + 1)...);
}
@@ -208,7 +209,7 @@ template<typename Tuple, int k, int size>
void TupleSerializer<Tuple, k, size>::WriteToMessage(
Tuple const& tuple,
not_null<serialization::PolynomialInMonomialBasis*> message) {
DoubleOrQuantityOrMultivectorSerializer<
DoubleOrQuantityOrPointOrMultivectorSerializer<
std::tuple_element_t<k, Tuple>,
serialization::PolynomialInMonomialBasis::Coefficient>::
WriteToMessage(std::get<k>(tuple), message->add_coefficient());
@@ -220,7 +221,7 @@ void TupleSerializer<Tuple, k, size>::FillFromMessage(
serialization::PolynomialInMonomialBasis const& message,
Tuple& tuple) {
std::get<k>(tuple) =
DoubleOrQuantityOrMultivectorSerializer<
DoubleOrQuantityOrPointOrMultivectorSerializer<
std::tuple_element_t<k, Tuple>,
serialization::PolynomialInMonomialBasis::Coefficient>::
ReadFromMessage(message.coefficient(k));
@@ -386,6 +387,7 @@ Derivative() const {

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
template<typename, typename>
PolynomialInMonomialBasis<
Primitive<Value, Argument>, Argument, degree_ + 1, Evaluator>
PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>::
@@ -538,15 +540,16 @@ Derivative() const {

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

30 changes: 30 additions & 0 deletions numerics/polynomial_test.cpp
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@ using geometry::Displacement;
using geometry::Handedness;
using geometry::Inertial;
using geometry::Instant;
using geometry::Position;
using geometry::Vector;
using geometry::Velocity;
using quantities::Acceleration;
@@ -65,6 +66,8 @@ class PolynomialTest : public ::testing::Test {
HornerEvaluator>;
using P2A = PolynomialInMonomialBasis<Displacement<World>, Instant, 2,
HornerEvaluator>;
using P2P = PolynomialInMonomialBasis<Position<World>, Instant, 2,
HornerEvaluator>;
using P17 = PolynomialInMonomialBasis<Displacement<World>, Time, 17,
EstrinEvaluator>;

@@ -139,6 +142,33 @@ TEST_F(PolynomialTest, Evaluate2A) {
EXPECT_THAT(v, AlmostEquals(Velocity<World>({1 * Metre / Second,
1 * Metre / Second,
0 * Metre / Second}), 0));

// This compiles.
p.Primitive();
}

// Check that a polynomial can return an affine value.
TEST_F(PolynomialTest, Evaluate2P) {
Instant const t0 = Instant() + 0.3 * Second;
P2P const p({World::origin + std::get<0>(coefficients_),
std::get<1>(coefficients_),
std::get<2>(coefficients_)},
t0);
EXPECT_EQ(2, p.degree());
Position<World> const d = p.Evaluate(t0 + 0.5 * Second);
Velocity<World> const v = p.EvaluateDerivative(t0 + 0.5 * Second);
EXPECT_THAT(d, AlmostEquals(World::origin + Displacement<World>({0.25 * Metre,
0.5 * Metre,
1 * Metre}),
0));
EXPECT_THAT(v, AlmostEquals(Velocity<World>({1 * Metre / Second,
1 * Metre / Second,
0 * Metre / Second}), 0));

// This doesn't compile (and rightly so).
#if 0
p.Primitive();
#endif
}

// Check that a polynomial of high order may be declared.
5 changes: 3 additions & 2 deletions quantities/named_quantities.hpp
Original file line number Diff line number Diff line change
@@ -44,9 +44,10 @@ using Derivative = typename std::conditional_t<
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.
// its |Argument|-valued argument. The primitive of an affine-valued function
// does not make much sense, but it must compile, hence the Difference.
template<typename Value, typename Argument>
using Primitive = Product<Value, Argument>;
using Primitive = Product<Difference<Value>, Argument>;

// |Variation<T>| is the type of the time derivative of a |T|-valued function.
template<typename T>
1 change: 1 addition & 0 deletions serialization/numerics.proto
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ message PolynomialInMonomialBasis {
double double = 1;
Quantity quantity = 2;
Multivector multivector = 3;
Point point = 4;
}
}
repeated Coefficient coefficient = 1;