#include #include #include #include #include extern "C" { #include #include } #include #include "layout/horizontal_layout.h" #include "layout/parenthesis_layout.h" #include "layout/string_layout.h" namespace Poincare { Expression::Type Opposite::type() const { return Type::Opposite; } Expression * Opposite::clone() const { Opposite * o = new Opposite(m_operands, true); return o; } /* Layout */ bool Opposite::needParenthesisWithParent(const Expression * e) const { Type types[] = {Type::Addition, Type::Subtraction, Type::Opposite, Type::Multiplication, Type::Division, Type::Power, Type::Factorial}; return e->isOfType(types, 7); } template Complex Opposite::compute(const Complex c, AngleUnit angleUnit) { return Complex::Cartesian(-c.a(), -c.b()); } Expression * Opposite::shallowReduce(Context& context, AngleUnit angleUnit) { Expression * e = Expression::shallowReduce(context, angleUnit); if (e != this) { return e; } const Expression * op = operand(0); #if MATRIX_EXACT_REDUCING if (op->type() == Type::Matrix) { return SimplificationEngine::map(this, context, angleUnit); } #endif detachOperand(op); Multiplication * m = new Multiplication(new Rational(-1), op, false); replaceWith(m, true); return m->shallowReduce(context, angleUnit); } ExpressionLayout * Opposite::privateCreateLayout(FloatDisplayMode floatDisplayMode, ComplexFormat complexFormat) const { assert(floatDisplayMode != FloatDisplayMode::Default); assert(complexFormat != ComplexFormat::Default); ExpressionLayout * children_layouts[2]; char string[2] = {'-', '\0'}; children_layouts[0] = new StringLayout(string, 1); children_layouts[1] = operand(0)->type() == Type::Opposite ? new ParenthesisLayout(operand(0)->createLayout(floatDisplayMode, complexFormat)) : operand(0)->createLayout(floatDisplayMode, complexFormat); return new HorizontalLayout(children_layouts, 2); } int Opposite::writeTextInBuffer(char * buffer, int bufferSize, int numberOfSignificantDigits) const { if (bufferSize == 0) { return -1; } buffer[bufferSize-1] = 0; int numberOfChar = 0; if (bufferSize == 1) { return 0; } buffer[numberOfChar++] = '-'; numberOfChar += operand(0)->writeTextInBuffer(buffer+numberOfChar, bufferSize-numberOfChar, numberOfSignificantDigits); buffer[numberOfChar] = 0; return numberOfChar; } } template Poincare::Complex Poincare::Opposite::compute(Poincare::Complex, AngleUnit angleUnit); template Poincare::Complex Poincare::Opposite::compute(Poincare::Complex, AngleUnit angleUnit);