From b58cfee5a1f9f895bdbb58da0e6e3ffb4d2b1ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Wed, 31 May 2017 12:05:59 +0200 Subject: [PATCH] [poincare] Clean matrix power Change-Id: I7ff27b45f240e0f6c89fb5bf79f21126475265e4 --- poincare/include/poincare/matrix.h | 1 + poincare/src/matrix.cpp | 16 ++++++++++++++++ poincare/src/power.cpp | 18 ++---------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/poincare/include/poincare/matrix.h b/poincare/include/poincare/matrix.h index 3bd7d0b68..1b736aab4 100644 --- a/poincare/include/poincare/matrix.h +++ b/poincare/include/poincare/matrix.h @@ -33,6 +33,7 @@ public: float determinant(Context& context, AngleUnit angleUnit) const; Expression * createInverse(Context& context, AngleUnit angleUnit) const; Expression * createTranspose(Context& context, AngleUnit angleUnit) const; + static Expression * createIdentity(int dim); private: ExpressionLayout * privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const override; float privateApproximate(Context& context, AngleUnit angleUnit) const override; diff --git a/poincare/src/matrix.cpp b/poincare/src/matrix.cpp index 029797e19..1fee60037 100644 --- a/poincare/src/matrix.cpp +++ b/poincare/src/matrix.cpp @@ -318,4 +318,20 @@ Expression * Matrix::createTranspose(Context& context, AngleUnit angleUnit) cons return matrix; } +Expression * Matrix::createIdentity(int dim) { + Expression ** operands = (Expression **)malloc(dim*dim*sizeof(Expression *)); + for (int i = 0; i < dim; i++) { + for (int j = 0; j < dim; j++) { + if (i == j) { + operands[i*dim+j] = new Complex(Complex::Float(1.0f)); + } else { + operands[i*dim+j] = new Complex(Complex::Float(0.0f)); + } + } + } + Expression * matrix = new Matrix(new MatrixData(operands, dim*dim, dim, dim, false)); + free(operands); + return matrix; +} + } diff --git a/poincare/src/power.cpp b/poincare/src/power.cpp index f021a0715..d9bc42d28 100644 --- a/poincare/src/power.cpp +++ b/poincare/src/power.cpp @@ -78,28 +78,14 @@ Expression * Power::evaluateOnMatrixAndComplex(Matrix * m, Complex * c, Context& } if (power == 0.0f) { /* Build the identity matrix with the same dimensions as m */ - Expression ** operands = (Expression **)malloc(m->numberOfOperands()*sizeof(Expression *)); - for (int i = 0; i < m->numberOfRows(); i++) { - for (int j = 0; j < m->numberOfColumns(); j++) { - if (i == j) { - operands[i*m->numberOfColumns()+j] = new Complex(Complex::Float(1.0f)); - } else { - operands[i*m->numberOfColumns()+j] = new Complex(Complex::Float(0.0f)); - } - } - } - Expression * matrix = new Matrix(new MatrixData(operands, m->numberOfOperands(), m->numberOfColumns(), m->numberOfRows(), false)); - free(operands); - return matrix; + return Matrix::createIdentity(m->numberOfRows()); } if (power < 0.0f) { Expression * inverse = m->createInverse(context, angleUnit); Expression * operands[2]; operands[0] = inverse; operands[1] = new Complex(Complex::Float(-power));; - Expression * power = new Power(operands, true); - delete operands[0]; - delete operands[1]; + Expression * power = new Power(operands, false); Expression * result = power->evaluate(context, angleUnit); delete power; return result;