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

Commits on Apr 26, 2020

  1. Copy the full SHA
    877bdf5 View commit details
  2. Copy the full SHA
    acf8be5 View commit details
  3. Lint.

    pleroy committed Apr 26, 2020
    Copy the full SHA
    b72b377 View commit details
  4. Typo.

    pleroy committed Apr 26, 2020
    Copy the full SHA
    7d518e9 View commit details
  5. After egg's review.

    pleroy committed Apr 26, 2020
    Copy the full SHA
    0dae6d0 View commit details
  6. Merge pull request #2547 from pleroy/MathematicaLogger

    An RAII object for Mathematica logging
    pleroy authored Apr 26, 2020

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    19f171f View commit details
Showing with 79 additions and 3 deletions.
  1. +27 −3 mathematica/mathematica.hpp
  2. +14 −0 mathematica/mathematica_body.hpp
  3. +38 −0 mathematica/mathematica_test.cpp
30 changes: 27 additions & 3 deletions mathematica/mathematica.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@

#pragma once

#include <filesystem>
#include <map>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

#include "astronomy/orbital_elements.hpp"
#include "base/file.hpp"
#include "base/traits.hpp"
#include "geometry/grassmann.hpp"
#include "geometry/point.hpp"
@@ -23,6 +26,7 @@ namespace mathematica {
namespace internal_mathematica {

using base::all_different_v;
using base::OFStream;
using geometry::Bivector;
using geometry::Point;
using geometry::Quaternion;
@@ -51,9 +55,9 @@ using quantities::Time;
// ToMathematica(... , ExpressIn(Metre, Second, Degree));
//
// The construction parameters must be values of distinct SI base quantities
// (or Angle). They define a system of units.. They may be in any order. If
// the other arguments of the functions contain quantities that are not spanned
// by that system of units, the call is ill-formed.
// (or Angle). They define a system of units. They may be in any order. If the
// other arguments of the functions contain quantities that are not spanned by
// that system of units, the call is ill-formed.
template<typename... Qs>
class ExpressIn {
public:
@@ -172,12 +176,32 @@ std::string ToMathematica(std::string const& str,
// Wraps the string in quotes and escapes things properly.
std::string Escape(std::string const& str);

// An RAII object to help with Mathematica logging.
class Logger final {
public:
// Creates a logger object that will, at destruction, write to the given file.
explicit Logger(std::filesystem::path const& path);
~Logger();

// Appends an element to the list of values for the variable |name|. The
// |args...| are passed verbatim to ToMathematica for stringification. When
// this object is destroyed, an assignment is generated for each of the
// variables named in a call to Append.
template<typename... Args>
void Append(std::string const& name, Args... args);

private:
OFStream file_;
std::map<std::string, std::vector<std::string>> names_and_values_;
};

} // namespace internal_mathematica

using internal_mathematica::Apply;
using internal_mathematica::Assign;
using internal_mathematica::Escape;
using internal_mathematica::ExpressIn;
using internal_mathematica::Logger;
using internal_mathematica::Option;
using internal_mathematica::PlottableDataset;
using internal_mathematica::ToMathematica;
14 changes: 14 additions & 0 deletions mathematica/mathematica_body.hpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#include "mathematica/mathematica.hpp"

#include <cmath>
#include <map>
#include <string>
#include <tuple>
#include <vector>
@@ -283,6 +284,19 @@ inline std::string Escape(std::string const& str) {
return result;
}

inline Logger::Logger(std::filesystem::path const& path) : file_(path) {}

inline Logger::~Logger() {
for (auto const& [name, values] : names_and_values_) {
file_ << Assign(name, values);
}
}

template<typename... Args>
void Logger::Append(std::string const& name, Args... args) {
names_and_values_[name].push_back(ToMathematica(args...));
}

} // namespace internal_mathematica
} // namespace mathematica
} // namespace principia
38 changes: 38 additions & 0 deletions mathematica/mathematica_test.cpp
Original file line number Diff line number Diff line change
@@ -357,5 +357,43 @@ TEST_F(MathematicaTest, ExpressIn) {
#endif
}

TEST_F(MathematicaTest, Logger) {
{
Logger logger(TEMP_DIR / "mathematica_test.wl");
logger.Append("a", std::vector{1, 2, 3});
logger.Append("b", 4 * Metre / Second);
logger.Append("a", F::origin);
}
// Go check the file.
EXPECT_EQ(
"Set["
"a,"
"List["
"List["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]],"
"List["
"Quantity["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"Quantity["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"Quantity["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" m\"]]]];\n"
"Set["
"b,"
"List["
"Quantity["
"SetPrecision[+4.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"]]];\n",
(
std::stringstream{}
<< std::ifstream(TEMP_DIR / "mathematica_test.wl").rdbuf())
.str());
}

} // namespace mathematica
} // namespace principia