Merge branch 'kmat' of https://github.com/RedGl0w/epsilon into RedGl0w-kmat

This commit is contained in:
M4x1m3
2021-03-15 20:48:43 +01:00
7 changed files with 135 additions and 0 deletions

View File

@@ -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 \

View File

@@ -79,6 +79,7 @@ public:
Integral,
InvBinom,
InvNorm,
KMat,
LeastCommonMultiple,
Logarithm,
MatrixTrace,

View File

@@ -0,0 +1,54 @@
#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; }
private:
// 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, ApproximationContext approximationContext) const override { return templatedApproximate<float>(approximationContext); }
Evaluation<double> approximate(DoublePrecision p, ApproximationContext approximationContext) const override { return templatedApproximate<double>(approximationContext); }
template<typename T> Evaluation<T> templatedApproximate(ApproximationContext approximationContext) 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

View File

@@ -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>

75
poincare/src/kmat.cpp Normal file
View File

@@ -0,0 +1,75 @@
#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());
}
Expression KMatNode::shallowReduce(ExpressionNode::ReductionContext reductionContext) {
return KMat(this).shallowReduce(reductionContext);
}
template<typename T>
Evaluation<T> KMatNode::templatedApproximate(ApproximationContext approximationContext) const {
return Complex<T>::Undefined();
}
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);
}
}

View File

@@ -126,6 +126,7 @@ private:
&InvBinom::s_functionHelper,
&MatrixInverse::s_functionHelper,
&InvNorm::s_functionHelper,
&KMat::s_functionHelper,
&LeastCommonMultiple::s_functionHelper,
&NaperianLogarithm::s_functionHelper,
&CommonLogarithm::s_functionHelper,

View File

@@ -325,6 +325,8 @@ template IntegralLayout TreeHandle::FixedArityBuilder<IntegralLayout, IntegralLa
template InvBinom TreeHandle::FixedArityBuilder<InvBinom, InvBinomNode>(const Tuple &);
template InvNorm TreeHandle::FixedArityBuilder<InvNorm, InvNormNode>(const Tuple &);
template LeastCommonMultiple TreeHandle::NAryBuilder<LeastCommonMultiple, LeastCommonMultipleNode>(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 &);
template Logarithm TreeHandle::FixedArityBuilder<Logarithm, LogarithmNode<2> >(const Tuple &);