From 3112d4a130cafce0f253ea45d60bcdb725bb3310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Fri, 31 Aug 2018 17:39:24 +0200 Subject: [PATCH] [poincare] Update matrix transpose, dimension, inverse, trace --- poincare/include/poincare/expression.h | 4 ++++ poincare/include/poincare/matrix_transpose.h | 1 - poincare/src/matrix_dimension.cpp | 9 ++++++--- poincare/src/matrix_inverse.cpp | 12 ++++++++---- poincare/src/matrix_trace.cpp | 9 ++++++--- poincare/src/matrix_transpose.cpp | 9 ++++++--- 6 files changed, 30 insertions(+), 14 deletions(-) diff --git a/poincare/include/poincare/expression.h b/poincare/include/poincare/expression.h index d6f1b81e5..ec4544fb6 100644 --- a/poincare/include/poincare/expression.h +++ b/poincare/include/poincare/expression.h @@ -41,6 +41,10 @@ class Expression : public TreeByReference { friend class Integral; friend class LeastCommonMultiple; friend class Logarithm; + friend class MatrixDimension; + friend class MatrixInverse; + friend class MatrixTrace; + friend class MatrixTranspose; friend class Sine; friend class Store; diff --git a/poincare/include/poincare/matrix_transpose.h b/poincare/include/poincare/matrix_transpose.h index cbd9ec091..a95b96552 100644 --- a/poincare/include/poincare/matrix_transpose.h +++ b/poincare/include/poincare/matrix_transpose.h @@ -48,4 +48,3 @@ public: } #endif - diff --git a/poincare/src/matrix_dimension.cpp b/poincare/src/matrix_dimension.cpp index 68f6fbc5a..65a109354 100644 --- a/poincare/src/matrix_dimension.cpp +++ b/poincare/src/matrix_dimension.cpp @@ -40,9 +40,11 @@ Evaluation MatrixDimensionNode::templatedApproximate(Context& context, Prefer } Expression MatrixDimension::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } Expression c = childAtIndex(0); #if MATRIX_EXACT_REDUCING @@ -68,6 +70,7 @@ Expression MatrixDimension::shallowReduce(Context & context, Preferences::AngleU result.addChildAtIndexInPlace(Rational(1), 0, 0); result.addChildAtIndexInPlace(Rational(1), 1, 1); result.setDimensions(1, 2); + replaceWithInPlace(result); return result; } return *this; diff --git a/poincare/src/matrix_inverse.cpp b/poincare/src/matrix_inverse.cpp index f3a2c319d..b9370fa0d 100644 --- a/poincare/src/matrix_inverse.cpp +++ b/poincare/src/matrix_inverse.cpp @@ -45,9 +45,11 @@ Evaluation MatrixInverseNode::templatedApproximate(Context& context, Preferen } Expression MatrixInverse::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } Expression c = childAtIndex(0); #if MATRIX_EXACT_REDUCING @@ -63,7 +65,9 @@ Expression MatrixInverse::shallowReduce(Context & context, Preferences::AngleUni return *this; #else if (c.type() != ExpressionNode::Type::Matrix) { - return Power(c, Rational(-1)).shallowReduce(context, angleUnit); + Expression result = Power(c, Rational(-1)).shallowReduce(context, angleUnit); + replaceWithInPlace(result); + return result; } return *this; #endif diff --git a/poincare/src/matrix_trace.cpp b/poincare/src/matrix_trace.cpp index 5c1d29c9d..02c285956 100644 --- a/poincare/src/matrix_trace.cpp +++ b/poincare/src/matrix_trace.cpp @@ -35,9 +35,11 @@ Evaluation MatrixTraceNode::templatedApproximate(Context& context, Preference } Expression MatrixTrace::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } Expression c = childAtIndex(0); #if MATRIX_EXACT_REDUCING @@ -59,6 +61,7 @@ Expression MatrixTrace::shallowReduce(Context & context, Preferences::AngleUnit return *this; #else if (c.type() != ExpressionNode::Type::Matrix) { + replaceWithInPlace(c); return c; } return *this; diff --git a/poincare/src/matrix_transpose.cpp b/poincare/src/matrix_transpose.cpp index 7340eea01..79c4619d2 100644 --- a/poincare/src/matrix_transpose.cpp +++ b/poincare/src/matrix_transpose.cpp @@ -39,9 +39,11 @@ Evaluation MatrixTransposeNode::templatedApproximate(Context& context, Prefer } Expression MatrixTranspose::shallowReduce(Context & context, Preferences::AngleUnit angleUnit) { - Expression e = Expression::defaultShallowReduce(context, angleUnit); - if (e.isUndefinedOrAllocationFailure()) { - return e; + { + Expression e = Expression::defaultShallowReduce(context, angleUnit); + if (e.isUndefinedOrAllocationFailure()) { + return e; + } } Expression c = childAtIndex(0); #if MATRIX_EXACT_REDUCING @@ -55,6 +57,7 @@ Expression MatrixTranspose::shallowReduce(Context & context, Preferences::AngleU return *this; #else if (c.type() != ExpressionNode::Type::Matrix) { + replaceWithInPlace(c); return c; } return *this;