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

Commits on Apr 26, 2020

  1. Copy the full SHA
    a467857 View commit details
  2. After egg's review.

    pleroy committed Apr 26, 2020
    Copy the full SHA
    7420a9b View commit details
  3. Merge pull request #2546 from pleroy/MathematicaImprovements

    Fix a number of TODOs in ToMathematica and friends
    pleroy authored Apr 26, 2020
    Copy the full SHA
    c65fa7b View commit details
Showing with 46 additions and 68 deletions.
  1. +1 −2 mathematica/mathematica.hpp
  2. +21 −10 mathematica/mathematica_body.hpp
  3. +24 −56 mathematica/mathematica_test.cpp
3 changes: 1 addition & 2 deletions mathematica/mathematica.hpp
Original file line number Diff line number Diff line change
@@ -169,8 +169,7 @@ template<typename OptionalExpressIn = std::nullopt_t>
std::string ToMathematica(std::string const& str,
OptionalExpressIn express_in = std::nullopt);

// Wraps the string in quotes.
// TODO(egg): escape things properly.
// Wraps the string in quotes and escapes things properly.
std::string Escape(std::string const& str);

} // namespace internal_mathematica
31 changes: 21 additions & 10 deletions mathematica/mathematica_body.hpp
Original file line number Diff line number Diff line change
@@ -152,10 +152,12 @@ std::string ToMathematica(double const real,

template<typename OptionalExpressIn>
std::string ToMathematica(Quaternion const& quaternion,
OptionalExpressIn express_in) {
return ToMathematica(
std::tuple{quaternion.real_part(), quaternion.imaginary_part()},
express_in);
OptionalExpressIn /*express_in*/) {
return Apply("Quaternion",
{ToMathematica(quaternion.real_part()),
ToMathematica(quaternion.imaginary_part().x),
ToMathematica(quaternion.imaginary_part().y),
ToMathematica(quaternion.imaginary_part().z)});
}

template<typename T, int size, typename OptionalExpressIn>
@@ -191,8 +193,7 @@ std::string ToMathematica(Quantity<D> const& quantity,
std::string const number = ToMathematica(quantity / si::Unit<Quantity<D>>);
std::size_t const split = s.find(" ");
std::string const units = Escape(s.substr(split, s.size()));
return Apply("SetPrecision",
{ Apply("Quantity", {number, units}), "$MachinePrecision" });
return Apply("Quantity", {number, units});
}

template<typename S, typename F, typename OptionalExpressIn>
@@ -263,11 +264,21 @@ std::string ToMathematica(std::string const& str,
return str;
}

// Wraps the string in quotes.
// TODO(egg): escape things properly.
inline std::string Escape(std::string const& str) {
std::string result = {"\""};
result += str;
std::string result = "\"";
for (const char c : str) {
switch (c) {
case '"':
result += "\\\"";
break;
case '\\':
result += "\\\\";
break;
default:
result += c;
break;
}
}
result += "\"";
return result;
}
80 changes: 24 additions & 56 deletions mathematica/mathematica_test.cpp
Original file line number Diff line number Diff line change
@@ -94,24 +94,19 @@ TEST_F(MathematicaTest, ToMathematica) {
ToMathematica(R3Element<double>(2.0, 3.0, -4.0)));
}
{
// TODO(phl): Use Quaternion[].
EXPECT_EQ(
"List["
"Quaternion["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"List["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[-4.00000000000000000*^+00,$MachinePrecision]]]",
"SetPrecision[-4.00000000000000000*^+00,$MachinePrecision]]",
ToMathematica(Quaternion(1.0, R3Element<double>(2.0, 3.0, -4.0))));
}
{
// TODO(phl): Why two SetPrecision[]?
EXPECT_EQ(
"SetPrecision["
"Quantity["
"SetPrecision[+1.50000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision]",
"\" m s^-1\"]",
ToMathematica(1.5 * Metre / Second));
}
{
@@ -140,41 +135,28 @@ TEST_F(MathematicaTest, ToMathematica) {
Vector<double, F>({2.0, 3.0, -4.0})));
}
{
// TODO(phl): Why no units?
EXPECT_EQ(
"List["
"List["
"SetPrecision["
"Quantity["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[-4.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision]],"
"\" m\"]],"
"List["
"SetPrecision["
"Quantity["
"SetPrecision[-1.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[-5.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[+8.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision]]]",
"\" m s^-1\"]]]",
ToMathematica(DegreesOfFreedom<F>(
F::origin +
Displacement<F>({2.0 * Metre, 3.0 * Metre, -4.0 * Metre}),
@@ -185,15 +167,15 @@ TEST_F(MathematicaTest, ToMathematica) {
{
EXPECT_EQ(
"List["
"SetPrecision["
"Quantity[SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],$MachinePrecision],"
"SetPrecision["
"Quantity[SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"\" s\"],$MachinePrecision],"
"SetPrecision["
"Quantity[SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],$MachinePrecision]]",
"Quantity["
"SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"Quantity["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"\" s\"],"
"Quantity["
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"]]",
ToMathematica(std::tuple{1 * Metre, 2 * Second, 3 * Metre / Second}));
}
{
@@ -208,66 +190,46 @@ TEST_F(MathematicaTest, ToMathematica) {
8.0 * Metre / Second})));
EXPECT_EQ(
"List["
"SetPrecision["
"Quantity["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" s\"],"
"$MachinePrecision],"
"List["
"List["
"SetPrecision["
"Quantity["
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[-4.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision]],"
"\" m\"]],"
"List["
"SetPrecision["
"Quantity["
"SetPrecision[-1.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[-5.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity["
"SetPrecision[+8.00000000000000000*^+00,$MachinePrecision],"
"\" m s^-1\"],"
"$MachinePrecision]]]]",
"\" m s^-1\"]]]]",
ToMathematica(*trajectory.begin()));
}
{
OrbitalElements::EquinoctialElements elements{
Instant(), 1 * Metre, 2, 3, 4 * Radian, 5, 6, 7, 8};
EXPECT_EQ(
"List["
"SetPrecision["
"Quantity["
"SetPrecision[+0.00000000000000000*^+00,$MachinePrecision],"
"\" s\"],"
"$MachinePrecision],"
"SetPrecision["
"Quantity[SetPrecision[+1.00000000000000000*^+00,$MachinePrecision],"
"\" m\"],"
"$MachinePrecision],"
"SetPrecision[+2.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+3.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision["
"Quantity[SetPrecision[+4.00000000000000000*^+00,$MachinePrecision],"
"\" rad\"],"
"$MachinePrecision],"
"SetPrecision[+5.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+6.00000000000000000*^+00,$MachinePrecision],"
"SetPrecision[+7.00000000000000000*^+00,$MachinePrecision],"
@@ -313,11 +275,17 @@ TEST_F(MathematicaTest, PlottableDataset) {
}

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

TEST_F(MathematicaTest, ExpressIn) {
ExpressIn<> default; // Check that this compiles.
{
EXPECT_EQ("SetPrecision[+3.00000000000000000*^+00,$MachinePrecision]",
ToMathematica(3.0 * Metre / Second / Second,