From 9742e019d9b1394161c880f6f9b55e259c2b71c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 8 Jul 2019 09:47:16 +0200 Subject: [PATCH] [poincare/multiplication] Handle non reduced matrices in shallowReduce --- poincare/src/multiplication.cpp | 20 ++++++++++++-------- poincare/test/multiplication.cpp | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/poincare/src/multiplication.cpp b/poincare/src/multiplication.cpp index c3c8bf636..5e420db30 100644 --- a/poincare/src/multiplication.cpp +++ b/poincare/src/multiplication.cpp @@ -261,7 +261,7 @@ Expression Multiplication::denominator(Context * context, Preferences::ComplexFo Expression Multiplication::privateShallowReduce(ExpressionNode::ReductionContext reductionContext, bool shouldExpand, bool canBeInterrupted) { { - Expression e = Expression::defaultShallowReduce();; + Expression e = Expression::defaultShallowReduce(); if (e.isUndefined()) { return e; } @@ -275,9 +275,8 @@ Expression Multiplication::privateShallowReduce(ExpressionNode::ReductionContext sortChildrenInPlace([](const ExpressionNode * e1, const ExpressionNode * e2, bool canBeInterrupted) { return ExpressionNode::SimplificationOrder(e1, e2, true, canBeInterrupted); }, reductionContext.context(), true); // Step 3: Handle matrices - /* All children have been simplified so if any child contains a matrix, it - * is at the root node of the child. Moreover, thanks to the simplification - * order, all matrix children (if any) are the last children. */ + /* Thanks to the simplification order, all matrix children (if any) are the + * last children. */ Expression lastChild = childAtIndex(numberOfChildren()-1); if (lastChild.type() == ExpressionNode::Type::Matrix) { Matrix resultMatrix = static_cast(lastChild); @@ -343,9 +342,14 @@ Expression Multiplication::privateShallowReduce(ExpressionNode::ReductionContext resultMatrix = newResult; multiplicationChildIndex--; } - // Distribute the remaining multiplication children on the matrix children - int remainingChildrenCount = numberOfChildren(); - if (remainingChildrenCount > 1) { + /* Distribute the remaining multiplication children on the matrix children, + * if there are no oether matrices (such as a non reduced confidence + * interval). */ + + if (multiplicationChildIndex >= 0) { + if (SortedIsMatrix(childAtIndex(multiplicationChildIndex), reductionContext.context())) { + return *this; + } removeChildInPlace(resultMatrix, resultMatrix.numberOfChildren()); for (int i = 0; i < n*m; i++) { Multiplication m = clone().convert(); @@ -552,7 +556,7 @@ void Multiplication::mergeMultiplicationChildrenInPlace() { while (i < numberOfChildren()) { Expression c = childAtIndex(i); if (c.type() == ExpressionNode::Type::Multiplication) { - mergeChildrenAtIndexInPlace(c, numberOfChildren()); // TODO: ensure that matrix children are not swapped to implement MATRIX_EXACT_REDUCING + mergeChildrenAtIndexInPlace(c, i); // TODO: ensure that matrix children are not swapped to implement MATRIX_EXACT_REDUCING continue; } i++; diff --git a/poincare/test/multiplication.cpp b/poincare/test/multiplication.cpp index f25b9ddde..d953d3e5f 100644 --- a/poincare/test/multiplication.cpp +++ b/poincare/test/multiplication.cpp @@ -70,4 +70,5 @@ QUIZ_CASE(poincare_multiplication_simplify) { assert_parsed_expression_simplify_to("12^(1/4)×(π/6)×(12×π)^(1/4)", "(√(3)×π^(5/4))/3"); assert_parsed_expression_simplify_to("[[1,2+𝐢][3,4][5,6]]×[[1,2+𝐢,3,4][5,6+𝐢,7,8]]", "[[11+5×𝐢,13+9×𝐢,17+7×𝐢,20+8×𝐢][23,30+7×𝐢,37,44][35,46+11×𝐢,57,68]]"); assert_parsed_expression_simplify_to("[[1,2][3,4]]×[[1,3][5,6]]×[[2,3][4,6]]", "[[82,123][178,267]]"); + assert_parsed_expression_simplify_to("π×confidence(π/5,3)[[1,2]]", "π×confidence(π/5,3)×[[1,2]]"); }