mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[apps/solver] Fix polar tests
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user