Skip to content

Commit

Permalink
[Poincare] Added kmat()
Browse files Browse the repository at this point in the history
  • Loading branch information
RedGl0w committed Oct 30, 2020
1 parent e0dcdce commit 270921f
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions poincare/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ poincare_src += $(addprefix poincare/src/,\
grid_layout.cpp \
horizontal_layout.cpp \
integral_layout.cpp \
kmat.cpp \
layout_cursor.cpp \
layout.cpp \
layout_node.cpp \
Expand Down
1 change: 1 addition & 0 deletions poincare/include/poincare/expression_node.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ExpressionNode : public TreeNode {
Integral,
InvBinom,
InvNorm,
KMat,
LeastCommonMultiple,
Logarithm,
MatrixTrace,
Expand Down
53 changes: 53 additions & 0 deletions poincare/include/poincare/kmat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#ifndef POINCARE_KMAT_H
#define POINCARE_KMAT_H

#include <poincare/approximation_helper.h>
#include <poincare/expression.h>

namespace Poincare {

class KMatNode final : public ExpressionNode {
public:

// TreeNode
size_t size() const override { return sizeof(KMatNode); }
int numberOfChildren() const override;
#if POINCARE_TREE_LOG
void logNodeName(std::ostream & stream) const override {
stream << "KMat";
}
#endif

// Properties
Type type() const override { return Type::KMat; }

// Layout
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;

// Simplification
Expression shallowReduce(ReductionContext reductionContext) override;
LayoutShape leftLayoutShape() const override { return LayoutShape::MoreLetters; };
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }

//Evaluation
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<float>(); }
Evaluation<double> approximate(DoublePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<double>(); }
template<typename T> Evaluation<T> templatedApproximate() const;
};

class KMat final : public Expression {
public:
KMat(const KMatNode * n) : Expression(n) {}
static KMat Builder(Expression child0, Expression child1, Expression child2) { return TreeHandle::FixedArityBuilder<KMat, KMatNode>({child0, child1, child2}); }


static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("kmat", 3, &UntypedBuilderThreeChildren<KMat>);

Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);

};

}

#endif
1 change: 1 addition & 0 deletions poincare/include/poincare_nodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <poincare/integral.h>
#include <poincare/inv_binom.h>
#include <poincare/inv_norm.h>
#include <poincare/kmat.h>
#include <poincare/least_common_multiple.h>
#include <poincare/logarithm.h>
#include <poincare/matrix.h>
Expand Down
72 changes: 72 additions & 0 deletions poincare/src/kmat.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <poincare/kmat.h>
#include <poincare/constant.h>
#include <poincare/serialization_helper.h>
#include <poincare/layout_helper.h>
#include <poincare/expression.h>
#include "parsing/token.h"
#include <poincare/integer.h>
#include <poincare/expression.h>
#include <poincare/rational.h>
#include <poincare/matrix.h>
#include <poincare/multiplication.h>
#include <poincare/symbol.h>
#include <utility>

namespace Poincare {

constexpr Expression::FunctionHelper KMat::s_functionHelper;

int KMatNode::numberOfChildren() const { return KMat::s_functionHelper.numberOfChildren(); }

Layout KMatNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return LayoutHelper::Prefix(KMat(this), floatDisplayMode, numberOfSignificantDigits, KMat::s_functionHelper.name());
}

int KMatNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, KMat::s_functionHelper.name());
}

template<typename T> Evaluation<T> KMatNode::templatedApproximate() const {
return Complex<T>::RealUndefined();
}

Expression KMatNode::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
return KMat(this).shallowReduce(reductionContext);
}

Expression KMat::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
Expression c0 = childAtIndex(0);
Expression c1 = childAtIndex(1);
if (c0.type() == ExpressionNode::Type::Rational) {
Rational r0 = static_cast<Rational &>(c0);
if (!r0.isInteger() || r0.signedIntegerNumerator().isNegative()) {
return replaceWithUndefinedInPlace();
}
}
if (c1.type() == ExpressionNode::Type::Rational) {
Rational r1 = static_cast<Rational&>(c1);
if (!r1.isInteger() || r1.signedIntegerNumerator().isNegative()) {
return replaceWithUndefinedInPlace();
}
}
if (c0.type() != ExpressionNode::Type::Rational || c1.type() != ExpressionNode::Type::Rational) {
return *this;
}

Rational r0 = static_cast<Rational&>(c0);
Rational r1 = static_cast<Rational&>(c1);

Integer w = r0.signedIntegerNumerator();
Integer h = r1.signedIntegerNumerator();
uint32_t size = *Integer::Multiplication(w,h).digits();
Matrix matrix = Matrix::Builder();
matrix.addChildAtIndexInPlace(childAtIndex(2).clone(), 0, 0);
for (uint32_t i = 1; i < size; i++) {
matrix.addChildAtIndexInPlace(childAtIndex(2).clone(), matrix.numberOfChildren(), matrix.numberOfChildren());
}
matrix.setDimensions(*w.digits(), *h.digits());
replaceWithInPlace(matrix);
return std::move(matrix);
}

}
1 change: 1 addition & 0 deletions poincare/src/parsing/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ class Parser {
&InvBinom::s_functionHelper,
&MatrixInverse::s_functionHelper,
&InvNorm::s_functionHelper,
&KMat::s_functionHelper,
&LeastCommonMultiple::s_functionHelper,
&NaperianLogarithm::s_functionHelper,
&CommonLogarithm::s_functionHelper,
Expand Down
1 change: 1 addition & 0 deletions poincare/src/tree_handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ template Integral TreeHandle::FixedArityBuilder<Integral, IntegralNode>(const Tu
template IntegralLayout TreeHandle::FixedArityBuilder<IntegralLayout, IntegralLayoutNode>(const Tuple &);
template InvBinom TreeHandle::FixedArityBuilder<InvBinom, InvBinomNode>(const Tuple &);
template InvNorm TreeHandle::FixedArityBuilder<InvNorm, InvNormNode>(const Tuple &);
template KMat TreeHandle::FixedArityBuilder<KMat, KMatNode>(const Tuple &);
template LeastCommonMultiple TreeHandle::FixedArityBuilder<LeastCommonMultiple, LeastCommonMultipleNode>(const Tuple &);
template LeftParenthesisLayout TreeHandle::FixedArityBuilder<LeftParenthesisLayout, LeftParenthesisLayoutNode>(const Tuple &);
template LeftSquareBracketLayout TreeHandle::FixedArityBuilder<LeftSquareBracketLayout, LeftSquareBracketLayoutNode>(const Tuple &);
Expand Down

0 comments on commit 270921f

Please sign in to comment.