Numworks Epsilon  1.4.1
Graphing Calculator Operating System
matrix_trace.cpp
Go to the documentation of this file.
2 #include <poincare/matrix.h>
3 #include <poincare/undefined.h>
4 #include <poincare/addition.h>
5 extern "C" {
6 #include <assert.h>
7 }
8 #include <cmath>
9 
10 namespace Poincare {
11 
13  return Type::MatrixTrace;
14 }
15 
17  MatrixTrace * a = new MatrixTrace(m_operands, true);
18  return a;
19 }
20 
21 Expression * MatrixTrace::shallowReduce(Context& context, AngleUnit angleUnit) {
22  Expression * e = Expression::shallowReduce(context, angleUnit);
23  if (e != this) {
24  return e;
25  }
26  Expression * op = editableOperand(0);
27 #if MATRIX_EXACT_REDUCING
28  if (op->type() == Type::Matrix) {
29  Matrix * m = static_cast<Matrix *>(op);
30  if (m->numberOfRows() != m->numberOfColumns()) {
31  return replaceWith(new Undefined(), true);
32  }
33  int n = m->numberOfRows();
34  Addition * a = new Addition();
35  for (int i = 0; i < n; i++) {
36  Expression * diagEntry = m->editableOperand(i+n*i);
37  m->detachOperand(diagEntry);
38  a->addOperand(diagEntry);
39  }
40  return replaceWith(a, true)->shallowReduce(context, angleUnit);
41  }
42  if (!op->recursivelyMatches(Expression::IsMatrix)) {
43  return replaceWith(op, true);
44  }
45  return this;
46 #else
47  return replaceWith(op, true);
48 #endif
49 }
50 
51 template<typename T>
52 Expression * MatrixTrace::templatedApproximate(Context& context, AngleUnit angleUnit) const {
53  Expression * input = operand(0)->approximate<T>(context, angleUnit);
54  Expression * result = nullptr;
55  if (input->type() == Type::Complex) {
56  result = input->clone();
57  } else {
58  assert(input->type() == Type::Matrix);
59  result = static_cast<Matrix *>(input)->createTrace<T>();
60  }
61  delete input;
62  return result;
63 }
64 
65 }
66 
Type type() const override
friend class Addition
Definition: expression.h:22
#define assert(e)
Definition: assert.h:9
Expression * replaceWith(Expression *newOperand, bool deleteAfterReplace=true)
Definition: expression.cpp:85
Expression * approximate(Context &context, AngleUnit angleUnit=AngleUnit::Default) const
Definition: expression.cpp:338
static bool IsMatrix(const Expression *e, Context &context)
Definition: expression.cpp:198
#define T(x)
Definition: events.cpp:26
Expression * editableOperand(int i)
Definition: expression.h:176
const Expression * m_operands[T]
friend class Matrix
Definition: expression.h:73
friend class MatrixTrace
Definition: expression.h:58
Expression * clone() const override
const Expression * operand(int i) const
Definition: expression.cpp:78
friend class Undefined
Definition: expression.h:17