|
| 1 | +#include <poincare/kmat.h> |
| 2 | +#include <poincare/constant.h> |
| 3 | +#include <poincare/serialization_helper.h> |
| 4 | +#include <poincare/layout_helper.h> |
| 5 | +#include <poincare/expression.h> |
| 6 | +#include "parsing/token.h" |
| 7 | +#include <poincare/integer.h> |
| 8 | +#include <poincare/expression.h> |
| 9 | +#include <poincare/rational.h> |
| 10 | +#include <poincare/matrix.h> |
| 11 | +#include <poincare/multiplication.h> |
| 12 | +#include <poincare/symbol.h> |
| 13 | +#include <utility> |
| 14 | + |
| 15 | +namespace Poincare { |
| 16 | + |
| 17 | +constexpr Expression::FunctionHelper KMat::s_functionHelper; |
| 18 | + |
| 19 | +int KMatNode::numberOfChildren() const { return KMat::s_functionHelper.numberOfChildren(); } |
| 20 | + |
| 21 | +Layout KMatNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { |
| 22 | + return LayoutHelper::Prefix(KMat(this), floatDisplayMode, numberOfSignificantDigits, KMat::s_functionHelper.name()); |
| 23 | +} |
| 24 | + |
| 25 | +int KMatNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const { |
| 26 | + return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, KMat::s_functionHelper.name()); |
| 27 | +} |
| 28 | + |
| 29 | +Expression KMatNode::shallowReduce(ExpressionNode::ReductionContext reductionContext) { |
| 30 | + return KMat(this).shallowReduce(reductionContext); |
| 31 | +} |
| 32 | + |
| 33 | +Expression KMat::shallowReduce(ExpressionNode::ReductionContext reductionContext) { |
| 34 | + Expression c0 = childAtIndex(0); |
| 35 | + Expression c1 = childAtIndex(1); |
| 36 | + if (c0.type() == ExpressionNode::Type::Rational) { |
| 37 | + Rational r0 = static_cast<Rational &>(c0); |
| 38 | + if (!r0.isInteger() || r0.signedIntegerNumerator().isNegative()) { |
| 39 | + return replaceWithUndefinedInPlace(); |
| 40 | + } |
| 41 | + } |
| 42 | + if (c1.type() == ExpressionNode::Type::Rational) { |
| 43 | + Rational r1 = static_cast<Rational&>(c1); |
| 44 | + if (!r1.isInteger() || r1.signedIntegerNumerator().isNegative()) { |
| 45 | + return replaceWithUndefinedInPlace(); |
| 46 | + } |
| 47 | + } |
| 48 | + if (c0.type() != ExpressionNode::Type::Rational || c1.type() != ExpressionNode::Type::Rational) { |
| 49 | + return *this; |
| 50 | + } |
| 51 | + |
| 52 | + Rational r0 = static_cast<Rational&>(c0); |
| 53 | + Rational r1 = static_cast<Rational&>(c1); |
| 54 | + |
| 55 | + Integer w = r0.signedIntegerNumerator(); |
| 56 | + Integer h = r1.signedIntegerNumerator(); |
| 57 | + uint32_t size = *Integer::Multiplication(w,h).digits(); |
| 58 | + Matrix matrix = Matrix::Builder(); |
| 59 | + matrix.addChildAtIndexInPlace(childAtIndex(2).clone(), 0, 0); |
| 60 | + for (uint32_t i = 1; i < size; i++) { |
| 61 | + matrix.addChildAtIndexInPlace(childAtIndex(2).clone(), matrix.numberOfChildren(), matrix.numberOfChildren()); |
| 62 | + } |
| 63 | + matrix.setDimensions(*w.digits(), *h.digits()); |
| 64 | + replaceWithInPlace(matrix); |
| 65 | + return std::move(matrix); |
| 66 | +} |
| 67 | + |
| 68 | +} |
0 commit comments