[poincare] Change Factor approximation to return undef on non real

values
This commit is contained in:
Émilie Feral
2020-01-17 10:34:07 +01:00
committed by Léa Saviot
parent 8deb81b8c2
commit 4e948ce533
4 changed files with 14 additions and 3 deletions

View File

@@ -33,9 +33,7 @@ private:
/* Evaluation */
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<float>(context, complexFormat, angleUnit); }
Evaluation<double> approximate(DoublePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<double>(context, complexFormat, angleUnit); }
template<typename T> Evaluation<T> templatedApproximate(Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const {
return childAtIndex(0)->approximate(T(), context, complexFormat, angleUnit);
}
template<typename T> Evaluation<T> templatedApproximate(Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const;
};
class Factor final : public Expression {

View File

@@ -35,6 +35,17 @@ Expression FactorNode::shallowBeautify(ReductionContext reductionContext) {
return Factor(this).shallowBeautify(reductionContext);
}
// Add tests :)
template<typename T>
Evaluation<T> FactorNode::templatedApproximate(Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const {
Evaluation<T> e = childAtIndex(0)->approximate(T(), context, complexFormat, angleUnit);
if (std::isnan(e.toScalar())) {
return Complex<T>::Undefined();
}
return e;
}
Multiplication Factor::createMultiplicationOfIntegerPrimeDecomposition(Integer i, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const {
assert(!i.isZero());
assert(!i.isNegative());

View File

@@ -343,6 +343,7 @@ QUIZ_CASE(poincare_approximation_function) {
assert_expression_approximates_to<float>("factor(-23/4)", "-5.75");
assert_expression_approximates_to<double>("factor(-123/24)", "-5.125");
assert_expression_approximates_to<float>("factor(𝐢)", "undef");
assert_expression_approximates_to<float>("inverse([[1,2,3][4,5,-6][7,8,9]])", "[[-1.2917,-0.083333,0.375][1.0833,0.16667,-0.25][0.041667,-0.083333,0.041667]]", Degree, Cartesian, 5); // inverse is not precise enough to display 7 significative digits
assert_expression_approximates_to<double>("inverse([[1,2,3][4,5,-6][7,8,9]])", "[[-1.2916666666667,-8.3333333333333ᴇ-2,0.375][1.0833333333333,1.6666666666667ᴇ-1,-0.25][4.1666666666667ᴇ-2,-8.3333333333333ᴇ-2,4.1666666666667ᴇ-2]]");

View File

@@ -362,6 +362,7 @@ QUIZ_CASE(poincare_simplification_function) {
assert_parsed_expression_simplify_to("factor(1008/6895)", "\u00122^4×3^2\u0013/\u00125×197\u0013");
assert_parsed_expression_simplify_to("factor(10007)", "10007");
assert_parsed_expression_simplify_to("factor(10007^2)", Undefined::Name());
assert_parsed_expression_simplify_to("factor(𝐢)", Undefined::Name());
assert_parsed_expression_simplify_to("floor(-1.3)", "-2");
assert_parsed_expression_simplify_to("floor(2π)", "6");
assert_parsed_expression_simplify_to("floor(123456789012345678901234567892/3)", "41152263004115226300411522630");