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

Commits on Dec 28, 2020

  1. Triangular output.

    pleroy committed Dec 28, 2020
    Copy the full SHA
    f6d3104 View commit details
  2. Copy the full SHA
    95a8430 View commit details
  3. Copy the full SHA
    6b8f5cf View commit details
  4. Transposition.

    pleroy committed Dec 28, 2020
    Copy the full SHA
    6e7d03e View commit details
  5. Tests.

    pleroy committed Dec 28, 2020
    Copy the full SHA
    667b0fa View commit details
  6. Fix a compilation error.

    pleroy committed Dec 28, 2020
    Copy the full SHA
    b4c5b4e View commit details
  7. Lint.

    pleroy committed Dec 28, 2020
    Copy the full SHA
    21feb8d View commit details

Commits on Dec 29, 2020

  1. Merge pull request #2827 from pleroy/Triangular

    Output of triangular arrays
    pleroy authored Dec 29, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    fa59bf7 View commit details
Showing with 163 additions and 15 deletions.
  1. +16 −0 mathematica/mathematica.hpp
  2. +49 −0 mathematica/mathematica_body.hpp
  3. +35 −0 mathematica/mathematica_test.cpp
  4. +12 −1 numerics/unbounded_arrays.hpp
  5. +45 −14 numerics/unbounded_arrays_body.hpp
  6. +6 −0 numerics/unbounded_arrays_test.cpp
16 changes: 16 additions & 0 deletions mathematica/mathematica.hpp
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@
#include "numerics/piecewise_poisson_series.hpp"
#include "numerics/poisson_series.hpp"
#include "numerics/polynomial.hpp"
#include "numerics/unbounded_arrays.hpp"
#include "physics/degrees_of_freedom.hpp"
#include "quantities/elementary_functions.hpp"
#include "quantities/quantities.hpp"
@@ -43,6 +44,9 @@ using numerics::FixedVector;
using numerics::PiecewisePoissonSeries;
using numerics::PoissonSeries;
using numerics::PolynomialInMonomialBasis;
using numerics::UnboundedLowerTriangularMatrix;
using numerics::UnboundedUpperTriangularMatrix;
using numerics::UnboundedVector;
using physics::DegreesOfFreedom;
using quantities::Amount;
using quantities::Angle;
@@ -199,6 +203,18 @@ template<typename Tuple,
std::string ToMathematica(Tuple const& tuple,
OptionalExpressIn express_in = std::nullopt);

template<typename Scalar, typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(UnboundedLowerTriangularMatrix<Scalar> const& matrix,
OptionalExpressIn express_in = std::nullopt);

template<typename Scalar, typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(UnboundedUpperTriangularMatrix<Scalar> const& matrix,
OptionalExpressIn express_in = std::nullopt);

template<typename Scalar, typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(UnboundedVector<Scalar> const& vector,
OptionalExpressIn express_in = std::nullopt);

template<typename R,
typename = std::void_t<decltype(std::declval<R>().time)>,
typename = std::void_t<decltype(std::declval<R>().degrees_of_freedom)>,
49 changes: 49 additions & 0 deletions mathematica/mathematica_body.hpp
Original file line number Diff line number Diff line change
@@ -379,6 +379,55 @@ std::string ToMathematica(Tuple const& tuple, OptionalExpressIn express_in) {
return Apply("List", expressions);
}

template<typename Scalar, typename OptionalExpressIn>
std::string ToMathematica(UnboundedLowerTriangularMatrix<Scalar> const& matrix,
OptionalExpressIn express_in) {
std::vector<std::string> rows;
rows.reserve(matrix.rows());
for (int i = 0; i < matrix.rows(); ++i) {
std::vector<std::string> row;
row.reserve(matrix.rows());
for (int j = 0; j <= i; ++j) {
row.push_back(ToMathematica(matrix[i][j], express_in));
}
for (int j = i + 1; j < matrix.rows(); ++j) {
row.push_back(ToMathematica(Scalar{}, express_in));
}
rows.push_back(Apply("List", row));
}
return Apply("List", rows);
}

template<typename Scalar, typename OptionalExpressIn>
std::string ToMathematica(UnboundedUpperTriangularMatrix<Scalar> const& matrix,
OptionalExpressIn express_in) {
std::vector<std::string> rows;
rows.reserve(matrix.columns());
for (int i = 0; i < matrix.columns(); ++i) {
std::vector<std::string> row;
row.reserve(matrix.columns());
for (int j = 0; j < i; ++j) {
row.push_back(ToMathematica(Scalar{}, express_in));
}
for (int j = i; j < matrix.columns(); ++j) {
row.push_back(ToMathematica(matrix[i][j], express_in));
}
rows.push_back(Apply("List", row));
}
return Apply("List", rows);
}

template<typename Scalar, typename OptionalExpressIn>
std::string ToMathematica(UnboundedVector<Scalar> const& vector,
OptionalExpressIn express_in) {
std::vector<std::string> elements;
elements.reserve(vector.size());
for (int i = 0; i < vector.size(); ++i) {
elements.push_back(ToMathematica(vector[i], express_in));
}
return Apply("List", elements);
}

template<typename R, typename, typename, typename OptionalExpressIn>
std::string ToMathematica(R const ref,
OptionalExpressIn express_in) {
35 changes: 35 additions & 0 deletions mathematica/mathematica_test.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#include "numerics/poisson_series.hpp"
#include "numerics/polynomial.hpp"
#include "numerics/polynomial_evaluators.hpp"
#include "numerics/unbounded_arrays.hpp"
#include "physics/degrees_of_freedom.hpp"
#include "physics/discrete_trajectory.hpp"
#include "quantities/quantities.hpp"
@@ -45,6 +46,9 @@ using numerics::HornerEvaluator;
using numerics::PiecewisePoissonSeries;
using numerics::PoissonSeries;
using numerics::PolynomialInMonomialBasis;
using numerics::UnboundedLowerTriangularMatrix;
using numerics::UnboundedUpperTriangularMatrix;
using numerics::UnboundedVector;
using physics::DegreesOfFreedom;
using physics::DiscreteTrajectory;
using quantities::Length;
@@ -237,6 +241,37 @@ TEST_F(MathematicaTest, ToMathematica) {
"\" m s^-1\"]]",
ToMathematica(std::tuple{1 * Metre, 2 * Second, 3 * Metre / Second}));
}
{
UnboundedLowerTriangularMatrix<double> l2({1,
2, 3});
EXPECT_EQ("List["
"List["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision]],"
"List["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]]]",
ToMathematica(l2));
}
{
UnboundedUpperTriangularMatrix<double> u2({1, 2,
3});
EXPECT_EQ("List["
"List["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision]],"
"List["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]]]",
ToMathematica(u2));
}
{
UnboundedVector<double> v2({1, 2});
EXPECT_EQ("List["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision]]",
ToMathematica(v2));
}
{
DiscreteTrajectory<F> trajectory;
trajectory.Append(
13 changes: 12 additions & 1 deletion numerics/unbounded_arrays.hpp
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@

#include <initializer_list>
#include <memory>
#include <type_traits>
#include <vector>

#include "base/tags.hpp"
@@ -21,6 +22,9 @@ class uninitialized_allocator : public std::allocator<T> {
void construct(U* p, Args&&... args);
};

template<typename Scalar>
class UnboundedUpperTriangularMatrix;

// The following classes are similar to those in fixed_arrays.hpp, but they have
// an Extend method to add more entries to the arrays.

@@ -69,6 +73,8 @@ class UnboundedLowerTriangularMatrix final {

void EraseToEnd(int begin_row_index);

UnboundedUpperTriangularMatrix<Scalar> Transpose() const;

int rows() const;
int dimension() const;

@@ -107,6 +113,8 @@ class UnboundedUpperTriangularMatrix final {

void EraseToEnd(int begin_column_index);

UnboundedLowerTriangularMatrix<Scalar> Transpose() const;

int columns() const;
int dimension() const;

@@ -122,7 +130,10 @@ class UnboundedUpperTriangularMatrix final {
private:
explicit Row(Matrix& matrix, int row);

Matrix& matrix_;
// We need to remove the const because, when this class is instantiated with
// 'UnboundedUpperTriangularMatrix const', the first operator[], not the
// second, is picked by overload resolution.
std::remove_const_t<Matrix>& matrix_;
int row_;

template<typename S>
59 changes: 45 additions & 14 deletions numerics/unbounded_arrays_body.hpp
Original file line number Diff line number Diff line change
@@ -129,6 +129,18 @@ void UnboundedLowerTriangularMatrix<Scalar>::EraseToEnd(
data_.end());
}

template<typename Scalar>
UnboundedUpperTriangularMatrix<Scalar>
UnboundedLowerTriangularMatrix<Scalar>::Transpose() const {
UnboundedUpperTriangularMatrix<Scalar> u(rows_, uninitialized);
for (int i = 0; i < rows_; ++i) {
for (int j = 0; j <= i; ++j) {
u[j][i] = (*this)[i][j];
}
}
return u;
}

template<typename Scalar>
int UnboundedLowerTriangularMatrix<Scalar>::rows() const {
return rows_;
@@ -218,6 +230,18 @@ void UnboundedUpperTriangularMatrix<Scalar>::EraseToEnd(
data_.end());
}

template<typename Scalar>
UnboundedLowerTriangularMatrix<Scalar>
UnboundedUpperTriangularMatrix<Scalar>::Transpose() const {
UnboundedLowerTriangularMatrix<Scalar> l(columns_, uninitialized);
for (int i = 0; i < columns_; ++i) {
for (int j = i; j < columns_; ++j) {
l[j][i] = (*this)[i][j];
}
}
return l;
}

template<typename Scalar>
int UnboundedUpperTriangularMatrix<Scalar>::columns() const {
return columns_;
@@ -255,7 +279,7 @@ template<typename Scalar>
template<typename Matrix>
UnboundedUpperTriangularMatrix<Scalar>::Row<Matrix>::Row(Matrix& matrix,
int const row)
: matrix_(matrix),
: matrix_(const_cast<std::remove_const_t<Matrix>&>(matrix)),
row_(row) {}

template<typename Scalar>
@@ -333,27 +357,34 @@ template<typename Scalar>
std::ostream& operator<<(std::ostream& out,
UnboundedLowerTriangularMatrix<Scalar> const& matrix) {
std::stringstream s;
// TODO(phl): Triangular printout.
s << "rows: " << matrix.rows_ << " ";
for (int i = 0; i < matrix.data_.size(); ++i) {
s << (i == 0 ? "{" : "") << matrix.data_[i]
<< (i == matrix.data_.size() - 1 ? "}" : ", ");
s << "rows: " << matrix.rows() << "\n";
for (int i = 0; i < matrix.rows(); ++i) {
out << "{";
for (int j = 0; j <= i; ++j) {
if (j > i) {
out << ", ";
}
out << matrix[i][j];
}
out << "}\n";
}
out << s.str();
return out;
}

template<typename Scalar>
std::ostream& operator<<(std::ostream& out,
UnboundedUpperTriangularMatrix<Scalar> const& matrix) {
std::stringstream s;
// TODO(phl): Triangular printout.
s << "columns: " << matrix.columns_ << " ";
for (int i = 0; i < matrix.data_.size(); ++i) {
s << (i == 0 ? "{" : "") << matrix.data_[i]
<< (i == matrix.data_.size() - 1 ? "}" : ", ");
out << "columns: " << matrix.columns_ << "\n";
for (int i = 0; i < matrix.columns(); ++i) {
out << "{";
for (int j = i; j < matrix.columns(); ++j) {
if (j > i) {
out << ", ";
}
out << matrix[i][j];
}
out << "}\n";
}
out << s.str();
return out;
}

6 changes: 6 additions & 0 deletions numerics/unbounded_arrays_test.cpp
Original file line number Diff line number Diff line change
@@ -93,6 +93,9 @@ TEST_F(UnboundedArraysTest, LowerTriangularMatrixIndexing) {
EXPECT_EQ(89, l4_[3][3]);
l4_[3][1] = -666;
EXPECT_EQ(-666, l4_[3][1]);

UnboundedLowerTriangularMatrix<double> const l4 = l4_;
EXPECT_EQ(1, l4[0][0]);
}

TEST_F(UnboundedArraysTest, UpperTriangularMatrixIndexing) {
@@ -109,6 +112,9 @@ TEST_F(UnboundedArraysTest, UpperTriangularMatrixIndexing) {
EXPECT_EQ(89, u4_[3][3]);
u4_[1][3] = -666;
EXPECT_EQ(-666, u4_[1][3]);

UnboundedUpperTriangularMatrix<double> const u4 = u4_;
EXPECT_EQ(1, u4[0][0]);
}

TEST_F(UnboundedArraysTest, Extend) {