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

Commits on Aug 9, 2020

  1. Tracing for polynomials.

    # Conflicts:
    #	numerics/frequency_analysis_test.cpp
    pleroy committed Aug 9, 2020
    Copy the full SHA
    26fd8ab View commit details
  2. Nicer formatting.

    pleroy committed Aug 9, 2020
    Copy the full SHA
    7970786 View commit details
  3. Poisson series output.

    pleroy committed Aug 9, 2020
    Copy the full SHA
    72a4740 View commit details
  4. Poisson series output.

    pleroy committed Aug 9, 2020
    Copy the full SHA
    0cec62a View commit details
  5. A minimal test.

    pleroy committed Aug 9, 2020
    Copy the full SHA
    f67b7f8 View commit details
  6. Lint.

    pleroy committed Aug 9, 2020
    Copy the full SHA
    9fa0ae7 View commit details
  7. Merge pull request #2667 from pleroy/Output

    Output operators for Polynomial and PoissonSeries
    pleroy authored Aug 9, 2020
    Copy the full SHA
    af0daf0 View commit details
Showing with 155 additions and 1 deletion.
  1. +1 −1 geometry/rotation.hpp
  2. +11 −0 numerics/poisson_series.hpp
  3. +31 −0 numerics/poisson_series_body.hpp
  4. +4 −0 numerics/poisson_series_test.cpp
  5. +25 −0 numerics/polynomial.hpp
  6. +83 −0 numerics/polynomial_body.hpp
2 changes: 1 addition & 1 deletion geometry/rotation.hpp
Original file line number Diff line number Diff line change
@@ -292,7 +292,7 @@ template<typename From, typename To>
bool operator!=(Rotation<From, To> const& left,
Rotation<From, To> const& right);

template<typename FromFrame, typename ToFrame>
template<typename FromFrame, typename ToFrame>
std::ostream& operator<<(std::ostream& out,
Rotation<FromFrame, ToFrame> const& rotation);

11 changes: 11 additions & 0 deletions numerics/poisson_series.hpp
Original file line number Diff line number Diff line change
@@ -96,6 +96,9 @@ class PoissonSeries {
PoissonSeries<Product<L, R>, l + r, E>
friend operator*(PoissonSeries<L, l, E> const& left,
PoissonSeries<R, r, E> const& right);
template<typename V, int d, template<typename, typename, int> class E>
friend std::ostream& operator<<(std::ostream& out,
PoissonSeries<V, d, E> const& series);
};

// Vector space of Poisson series.
@@ -152,6 +155,14 @@ PoissonSeries<Product<LValue, RValue>, ldegree_ + rdegree_, Evaluator>
operator*(PoissonSeries<LValue, ldegree_, Evaluator> const& left,
PoissonSeries<RValue, rdegree_, Evaluator> const& right);

// Output.

template<typename Value, int degree_,
template<typename, typename, int> class Evaluator>
std::ostream& operator<<(
std::ostream& out,
PoissonSeries<Value, degree_, Evaluator> const& series);

// Inner product space of Poisson series.

// Technically the weight function must be nonnegative for this to be an inner
31 changes: 31 additions & 0 deletions numerics/poisson_series_body.hpp
Original file line number Diff line number Diff line change
@@ -362,6 +362,37 @@ operator*(PoissonSeries<LValue, ldegree_, Evaluator> const& left,
return {aperiodic, std::move(periodic)};
}

template<typename Value, int degree_,
template<typename, typename, int> class Evaluator>
std::ostream& operator<<(
std::ostream& out,
PoissonSeries<Value, degree_, Evaluator> const& series) {
bool is_start_of_output = true;
if (!series.aperiodic_.is_zero()) {
out << series.aperiodic_;
is_start_of_output = false;
}
for (auto const& [ω, polynomials] : series.periodic_) {
if (!polynomials.sin.is_zero()) {
if (!is_start_of_output) {
out << " + ";
}
out << polynomials.sin << " * Sin(" << quantities::DebugString(ω)
<< " * (T - " << series.origin_ << "))";
is_start_of_output = false;
}
if (!polynomials.cos.is_zero()) {
if (!is_start_of_output) {
out << " + ";
}
out << polynomials.cos << " * Cos(" << quantities::DebugString(ω)
<< " * (T - " << series.origin_ << "))";
is_start_of_output = false;
}
}
return out;
}

template<typename LValue, typename RValue,
int ldegree_, int rdegree_, int wdegree_,
template<typename, typename, int> class Evaluator>
4 changes: 4 additions & 0 deletions numerics/poisson_series_test.cpp
Original file line number Diff line number Diff line change
@@ -186,6 +186,10 @@ TEST_F(PoissonSeriesTest, Dot) {
AlmostEquals(-381.25522770148542400, 71));
}

TEST_F(PoissonSeriesTest, Output) {
LOG(ERROR) << *pa_;
}

class PiecewisePoissonSeriesTest : public ::testing::Test {
protected:
using Degree0 = PiecewisePoissonSeries<double, 0, HornerEvaluator>;
25 changes: 25 additions & 0 deletions numerics/polynomial.hpp
Original file line number Diff line number Diff line change
@@ -47,6 +47,9 @@ class Polynomial {
// code.
virtual int degree() const = 0;

// Only useful for logging. Do not use in real code.
virtual bool is_zero() const = 0;

virtual void WriteToMessage(
not_null<serialization::Polynomial*> message) const = 0;

@@ -84,6 +87,7 @@ class PolynomialInMonomialBasis : public Polynomial<Value, Argument> {
EvaluateDerivative(Argument const& argument) const override;

constexpr int degree() const override;
bool is_zero() const override;

template<int order = 1>
PolynomialInMonomialBasis<
@@ -145,6 +149,11 @@ class PolynomialInMonomialBasis : public Polynomial<Value, Argument> {
friend operator*(
PolynomialInMonomialBasis<L, A, l, E> const& left,
PolynomialInMonomialBasis<R, A, r, E> const& right);
template<typename V, typename A, int d,
template<typename, typename, int> class E>
friend std::ostream& operator<<(
std::ostream& out,
PolynomialInMonomialBasis<V, A, d, E> const& polynomial);
};

template<typename Value, typename Argument, int degree_,
@@ -176,6 +185,8 @@ class PolynomialInMonomialBasis<Value, Point<Argument>, degree_, Evaluator>
EvaluateDerivative(Point<Argument> const& argument) const override;

constexpr int degree() const override;
bool is_zero() const override;

Point<Argument> const& origin() const;

template<int order = 1>
@@ -240,6 +251,11 @@ class PolynomialInMonomialBasis<Value, Point<Argument>, degree_, Evaluator>
friend operator*(
PolynomialInMonomialBasis<L, A, l, E> const& left,
PolynomialInMonomialBasis<R, A, r, E> const& right);
template<typename V, typename A, int d,
template<typename, typename, int> class E>
friend std::ostream& operator<<(
std::ostream& out,
PolynomialInMonomialBasis<V, A, d, E> const& polynomial);
};

// Vector space of polynomials.
@@ -314,6 +330,15 @@ operator*(
PolynomialInMonomialBasis<RValue, Argument, rdegree_, Evaluator> const&
right);

// Output.

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
std::ostream& operator<<(
std::ostream& out,
PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator> const&
polynomial);

} // namespace internal_polynomial

using internal_polynomial::Polynomial;
83 changes: 83 additions & 0 deletions numerics/polynomial_body.hpp
Original file line number Diff line number Diff line change
@@ -3,9 +3,13 @@
#include "numerics/polynomial.hpp"

#include <algorithm>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

#include "absl/strings/str_join.h"
#include "absl/strings/str_cat.h"
#include "base/not_constructible.hpp"
#include "base/traits.hpp"
#include "geometry/cartesian_product.hpp"
@@ -90,6 +94,8 @@ struct TupleSerializer : not_constructible {
static void FillFromMessage(
serialization::PolynomialInMonomialBasis const& message,
Tuple& tuple);
static std::vector<std::string> DebugString(Tuple const& tuple,
std::string const& argument);
};

template<typename Tuple, int size>
@@ -100,6 +106,8 @@ struct TupleSerializer<Tuple, size, size> : not_constructible {
static void FillFromMessage(
serialization::PolynomialInMonomialBasis const& message,
Tuple& tuple);
static std::vector<std::string> DebugString(Tuple const& tuple,
std::string const& argument);
};

template<typename Tuple, int k, int size>
@@ -125,6 +133,34 @@ void TupleSerializer<Tuple, k, size>::FillFromMessage(
TupleSerializer<Tuple, k + 1, size>::FillFromMessage(message, tuple);
}

template<typename Tuple, int k, int size>
std::vector<std::string> TupleSerializer<Tuple, k, size>::DebugString(
Tuple const& tuple,
std::string const& argument) {
auto tail =
TupleSerializer<Tuple, k + 1, size>::DebugString(tuple, argument);
auto const coefficient = std::get<k>(tuple);
if (coefficient == std::tuple_element_t<k, Tuple>{}) {
return tail;
}
std::string head;
switch (k) {
case 0:
head = quantities::DebugString(coefficient);
break;
case 1:
head = absl::StrCat(
quantities::DebugString(coefficient), " * ", argument);
break;
default:
head = absl::StrCat(
quantities::DebugString(coefficient), " * ", argument, "^", k);
break;
}
tail.insert(tail.begin(), head);
return tail;
}

template<typename Tuple, int size>
void TupleSerializer<Tuple, size, size>::WriteToMessage(
Tuple const& tuple,
@@ -135,6 +171,13 @@ void TupleSerializer<Tuple, size, size>::FillFromMessage(
serialization::PolynomialInMonomialBasis const& message,
Tuple& tuple) {}

template<typename Tuple, int size>
std::vector<std::string> TupleSerializer<Tuple, size, size>::DebugString(
Tuple const& tuple,
std::string const& argument) {
return {};
}


#define PRINCIPIA_POLYNOMIAL_DEGREE_VALUE_CASE(value) \
case value: \
@@ -227,6 +270,13 @@ PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>::degree() const {
return degree_;
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
bool PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>::is_zero()
const {
return coefficients_ == Coefficients{};
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
template<int order>
@@ -351,6 +401,13 @@ degree() const {
return degree_;
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
bool PolynomialInMonomialBasis<Value, Point<Argument>, degree_, Evaluator>::
is_zero() const {
return coefficients_ == Coefficients{};
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
Point<Argument> const&
@@ -584,6 +641,32 @@ operator*(
}
}

template<typename Value, typename Argument, int degree_,
template<typename, typename, int> class Evaluator>
std::ostream& operator<<(
std::ostream& out,
PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator> const&
polynomial) {
using Coefficients =
typename PolynomialInMonomialBasis<Value, Argument, degree_, Evaluator>::
Coefficients;
std::vector<std::string> debug_string;
if constexpr (is_instance_of_v<Point, Argument>) {
debug_string = TupleSerializer<Coefficients, 0>::DebugString(
polynomial.coefficients_,
"(T - " + DebugString(polynomial.origin_) + ")");
} else {
debug_string = TupleSerializer<Coefficients, 0>::DebugString(
polynomial.coefficients_, "T");
}
if (debug_string.empty()) {
out << quantities::DebugString(Value{});
} else {
out << absl::StrJoin(debug_string, " + ");
}
return out;
}

} // namespace internal_polynomial
} // namespace numerics
} // namespace principia