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

Commits on May 29, 2020

  1. More Mathematica improvements.

    pleroy committed May 29, 2020
    Copy the full SHA
    bacc0cf View commit details
  2. Remove useless code.

    pleroy committed May 29, 2020
    Copy the full SHA
    9df78b9 View commit details
  3. Hide Apply.

    pleroy committed May 29, 2020
    Copy the full SHA
    86d8ba6 View commit details

Commits on May 30, 2020

  1. Fix clients.

    pleroy committed May 30, 2020
    Copy the full SHA
    2352bb5 View commit details
  2. Merge pull request #2595 from pleroy/2519b

    More Mathematica improvements
    pleroy authored May 30, 2020
    Copy the full SHA
    5e20176 View commit details
2 changes: 1 addition & 1 deletion astronomy/ksp_system_test.cpp
Original file line number Diff line number Diff line change
@@ -456,7 +456,7 @@ TEST_P(KSPSystemConvergenceTest, DISABLED_Convergence) {
}
mathematica_entries.push_back({steps[i + 1],
position_errors[i],
mathematica::Escape(worst_body[i]),
worst_body[i],
durations[i + 1].count() * Second});
}

2 changes: 1 addition & 1 deletion astronomy/solar_system_dynamics_test.cpp
Original file line number Diff line number Diff line change
@@ -697,7 +697,7 @@ TEST_P(SolarSystemDynamicsConvergenceTest, DISABLED_Convergence) {
}
mathematica_entries.push_back({steps[i + 1],
position_errors[i],
mathematica::Escape(worst_body[i]),
worst_body[i],
durations[i + 1].count() * Second});
}

2 changes: 1 addition & 1 deletion ksp_plugin_adapter/ksp_plugin_adapter.cs
Original file line number Diff line number Diff line change
@@ -1494,7 +1494,7 @@ private void FashionablyLate() {
part.flightID,
(from force in part.forces
select PartCentredForceHolder.FromPartForceHolder(
part, force)).ToArray());
part, force)).ToArray());
}
}
}
2 changes: 1 addition & 1 deletion mathematica/integrator_plots.cpp
Original file line number Diff line number Diff line change
@@ -253,7 +253,7 @@ class WorkErrorGraphGenerator {
PlottableDataset(evaluations_[i], v_errors_[i]));
e_error_data.emplace_back(
PlottableDataset(evaluations_[i], e_errors_[i]));
names.emplace_back(Escape(methods_[i].name));
names.emplace_back(ToMathematica(methods_[i].name));
}
std::string result;
result += Assign("qErrorData", q_error_data);
11 changes: 1 addition & 10 deletions mathematica/mathematica.hpp
Original file line number Diff line number Diff line change
@@ -88,9 +88,6 @@ class ExpressIn {
std::tuple<Qs...> units_;
};

std::string Apply(std::string const& function,
std::vector<std::string> const& arguments);

template<typename T, typename OptionalExpressIn = std::nullopt_t>
std::string Option(std::string const& name,
T const& right,
@@ -200,25 +197,21 @@ template<typename T, typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(std::optional<T> const& opt,
OptionalExpressIn express_in = std::nullopt);

// Returns its argument.
template<typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(char const* str,
OptionalExpressIn express_in = std::nullopt);
template<typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(std::string const& str,
OptionalExpressIn express_in = std::nullopt);

// 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.
// If make_unique is true, a unique id is inserted before the file extension
// to identify different loggers.
Logger(std::filesystem::path const& path,
bool make_unique = false);
bool make_unique = true);
~Logger();

// Appends an element to the list of values for the variable |name|. The
@@ -237,9 +230,7 @@ class Logger final {

} // 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;
76 changes: 39 additions & 37 deletions mathematica/mathematica_body.hpp
Original file line number Diff line number Diff line change
@@ -26,6 +26,41 @@ using quantities::si::Radian;
using quantities::si::Second;
namespace si = quantities::si;

// Wraps the string in quotes and escapes things properly.
inline std::string Escape(std::string_view const str) {
std::string result = "\"";
for (const char c : str) {
switch (c) {
case '"':
result += "\\\"";
break;
case '\\':
result += "\\\\";
break;
default:
result += c;
break;
}
}
result += "\"";
return result;
}

// Does not wrap its arguments in ToMathematica.
inline std::string Apply(
std::string const& function,
std::vector<std::string> const& arguments) {
std::string result;
result += function;
result += "[";
for (int i = 0; i < arguments.size(); ++i) {
result += arguments[i];
result += (i + 1 < arguments.size() ? "," : "");
}
result += "]";
return result;
}

// A helper struct to scan the elements of a tuple and stringify them.
template<int index, typename Tuple, typename OptionalExpressIn>
struct TupleHelper : not_constructible {
@@ -46,20 +81,6 @@ struct TupleHelper<0, Tuple, OptionalExpressIn> : not_constructible {
OptionalExpressIn express_in) {}
};

inline std::string Apply(
std::string const& function,
std::vector<std::string> const& arguments) {
std::string result;
result += function;
result += "[";
for (int i = 0; i < arguments.size(); ++i) {
result += arguments[i];
result += (i + 1 < arguments.size() ? "," : "");
}
result += "]";
return result;
}

template<typename... Qs>
ExpressIn<Qs...>::ExpressIn(Qs const&... qs)
: units_(std::make_tuple(qs...)) {}
@@ -109,7 +130,7 @@ std::string PlottableDataset(std::vector<T> const& x,
OptionalExpressIn express_in) {
std::vector<std::string> const xy = {ToMathematica(x, express_in),
ToMathematica(y, express_in)};
return Apply("Transpose", {ToMathematica(xy, express_in)});
return Apply("Transpose", {Apply("List", xy)});
}

template<typename T, typename OptionalExpressIn>
@@ -301,32 +322,13 @@ std::string ToMathematica(std::optional<T> const& opt,
template<typename OptionalExpressIn>
std::string ToMathematica(char const* const str,
OptionalExpressIn /*express_in*/) {
return std::string(str);
return Escape(str);
}

template<typename OptionalExpressIn>
std::string ToMathematica(std::string const& str,
OptionalExpressIn /*express_in*/) {
return str;
}

inline std::string Escape(std::string const& str) {
std::string result = "\"";
for (const char c : str) {
switch (c) {
case '"':
result += "\\\"";
break;
case '\\':
result += "\\\\";
break;
default:
result += c;
break;
}
}
result += "\"";
return result;
return Escape(str);
}

inline Logger::Logger(std::filesystem::path const& path, bool const make_unique)
@@ -343,7 +345,7 @@ inline Logger::Logger(std::filesystem::path const& path, bool const make_unique)

inline Logger::~Logger() {
for (auto const& [name, values] : names_and_values_) {
file_ << Assign(name, values);
file_ << Apply("Set", {name, Apply("List", values)}) + ";\n";
}
}

31 changes: 13 additions & 18 deletions mathematica/mathematica_test.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "mathematica/mathematica.hpp"
#include "mathematica/mathematica.hpp"

#include <list>
#include <string>
@@ -288,32 +288,27 @@ TEST_F(MathematicaTest, ToMathematica) {
std::optional<std::string> opt1;
std::optional<std::string> opt2("foo");
EXPECT_EQ("List[]", ToMathematica(opt1));
EXPECT_EQ("List[foo]", ToMathematica(opt2));
EXPECT_EQ("List[\"foo\"]", ToMathematica(opt2));
}
{
EXPECT_EQ("foo\"bar", ToMathematica("foo\"bar"));
EXPECT_EQ("\"foo\\\"bar\"", ToMathematica("foo\"bar"));
}
}

TEST_F(MathematicaTest, Apply) {
EXPECT_EQ("Fun[]", Apply("Fun", {}));
EXPECT_EQ("Fun[1,[],\"string\"]", Apply("Fun", {"1", "[]", "\"string\""}));
}

TEST_F(MathematicaTest, Option) {
EXPECT_EQ(
"Rule["
"option,"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]]",
Option("option", ToMathematica(3.0)));
Option("option", 3.0));
}

TEST_F(MathematicaTest, Assign) {
EXPECT_EQ(
"Set["
"var,"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]];\n",
Assign("var", ToMathematica(3.0)));
Assign("var", 3.0));
}

TEST_F(MathematicaTest, PlottableDataset) {
@@ -323,20 +318,20 @@ TEST_F(MathematicaTest, PlottableDataset) {
"List["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]],"
"List[2,3]]]",
"List[\"2\",\"3\"]]]",
PlottableDataset(std::vector<double>{2, 3},
std::vector<std::string>{"2", "3"}));
}

TEST_F(MathematicaTest, Escape) {
EXPECT_EQ(R"("foo")", Escape("foo"));
EXPECT_EQ(R"("foo")", ToMathematica("foo"));
{
// This string messes up the macro.
std::string expected = R"("fo\"o")";
EXPECT_EQ(expected, Escape("fo\"o"));
EXPECT_EQ(expected, ToMathematica("fo\"o"));
}
EXPECT_EQ(R"("fo\\o")", Escape("fo\\o"));
EXPECT_EQ(R"("")", Escape(""));
EXPECT_EQ(R"("fo\\o")", ToMathematica("fo\\o"));
EXPECT_EQ(R"("")", ToMathematica(""));
}

TEST_F(MathematicaTest, ExpressIn) {
@@ -415,9 +410,9 @@ TEST_F(MathematicaTest, ExpressIn) {
#if !defined(__APPLE__)
TEST_F(MathematicaTest, Logger) {
{
Logger logger(TEMP_DIR / "mathematica_test.wl", /*make_unique=*/true);
Logger logger(TEMP_DIR / "mathematica_test.wl");
logger.Append("a", std::vector{1.0, 2.0, 3.0});
logger.Append("b", 4 * Metre / Second);
logger.Append(u8"β", 4 * Metre / Second);
logger.Append("a", F::origin);
}
// Go check the file.
@@ -440,7 +435,7 @@ TEST_F(MathematicaTest, Logger) {
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" m\"]]]];\n"
"Set["
"b,"
u8"β,"
"List["
"Quantity["
"SetPrecision[+4.00000000000000000*^+00,$MachinePrecision],"