[apps/solver] Fix polar tests

This commit is contained in:
Léa Saviot
2020-04-15 11:28:39 +02:00
committed by Ecco
parent 9988447b9a
commit c2bd19d9a1
2 changed files with 11 additions and 33 deletions

View File

@@ -117,8 +117,8 @@ QUIZ_CASE(equation_solve_complex_polar) {
set_complex_format(Polar);
assert_solves_to("x+𝐢=0", "x=^(-(π/2)𝐢)");
assert_solves_to("x+√(-1)=0", "x=^(-(π/2)𝐢)");
assert_solves_to("x^2+x+1=0", {"x=^(-(2π/3)𝐢)", "x=^((2π/3)𝐢)", "delta=3^(𝐢π)"});
assert_solves_to("x^2-√(-1)=0", {"x=^(-(3×π/4)𝐢)", "x=^((π/4)𝐢)", "delta=4^(𝐢π/2)"});
assert_solves_to("x^2+x+1=0", {"x=^(-(2π/3)𝐢)", "x=^((2π/3)𝐢)", "delta=3^(π𝐢)"});
assert_solves_to("x^2-√(-1)=0", {"x=^(-(3π/4)𝐢)", "x=^((π/4)𝐢)", "delta=4^((π/2)𝐢)"});
assert_solves_to("root(-8,3)*x+3=0", "x=3/2×^((2π/3)𝐢)");
reset_complex_format();
}

View File

@@ -84,43 +84,21 @@ void assert_solves_to(std::initializer_list<const char *> equations, std::initia
const char * expectedValue = equal + 1;
Preferences::ComplexFormat complexFormat = Preferences::sharedPreferences()->complexFormat();
/* We want to give complex results to equations that explicitely use 𝐢
* As a result, we need to enforce a non-real complex format here. */
if (complexFormat == Preferences::ComplexFormat::Real) {
complexFormat = Preferences::ComplexFormat::Cartesian;
}
/* We're pretty much reinventing ParseAndSimplify here.
* But for some reason, we really need to call simplifyAndApproximate,
* otherwise simplification of Polar numbers don't work. For instance,
* ParseAndSimplify("𝐢") will yield "𝐢", even in Polar mode!
* We're using the same weird trick as in assert_parsed_expression_simplify_to
* TODO: Fix ParseAndSimplify */
/* We compare Expressions, by parsing the expected Expression and
* serializing and parsing the obtained layout. We need to ignore the
* parentheses during the comparison, because to create an expression from
* a const char * we need to add parentheses that are not necessary when
* creating an expression from a layout. */
Expression expectedExpression = Expression::Parse(expectedValue, &globalContext, false);
quiz_assert(!expectedExpression.isUninitialized());
expectedExpression.simplifyAndApproximate(
&expectedExpression,
nullptr,
&globalContext,
complexFormat,
Preferences::sharedPreferences()->angleUnit(),
ExpressionNode::SymbolicComputation::ReplaceAllDefinedSymbolsWithDefinition
);
Layout expectedLayout = expectedExpression.createLayout(Preferences::PrintFloatMode::Decimal, 5);
Layout obtainedLayout = store->exactSolutionLayoutAtIndex(i, true);
#if 0
// Uncomment this if you need to see why a test fails using a debugger
constexpr int bufferSize = 200;
char debugExpectedLayout[bufferSize];
char debugObtainedLayout[bufferSize];
expectedLayout.serializeForParsing(debugExpectedLayout, bufferSize);
obtainedLayout.serializeForParsing(debugObtainedLayout, bufferSize);
#endif
quiz_assert(obtainedLayout.isIdenticalTo(expectedLayout));
char obtainedLayoutBuffer[bufferSize];
obtainedLayout.serializeForParsing(obtainedLayoutBuffer, bufferSize);
Expression obtainedExpression = Expression::Parse(obtainedLayoutBuffer, &globalContext, false);
quiz_assert(expectedExpression.isIdenticalToWithoutParentheses(obtainedExpression));
i++;
}