Skip to content

Commit 8e4187f

Browse files
committed
[Poincare] Added kmat()
1 parent e0dcdce commit 8e4187f

File tree

7 files changed

+125
-0
lines changed

7 files changed

+125
-0
lines changed

poincare/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ poincare_src += $(addprefix poincare/src/,\
1212
grid_layout.cpp \
1313
horizontal_layout.cpp \
1414
integral_layout.cpp \
15+
kmat.cpp \
1516
layout_cursor.cpp \
1617
layout.cpp \
1718
layout_node.cpp \

poincare/include/poincare/expression_node.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class ExpressionNode : public TreeNode {
7474
Integral,
7575
InvBinom,
7676
InvNorm,
77+
KMat,
7778
LeastCommonMultiple,
7879
Logarithm,
7980
MatrixTrace,

poincare/include/poincare/kmat.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#ifndef POINCARE_KMAT_H
2+
#define POINCARE_KMAT_H
3+
4+
#include <poincare/approximation_helper.h>
5+
#include <poincare/expression.h>
6+
7+
namespace Poincare {
8+
9+
class KMatNode final : public ExpressionNode {
10+
public:
11+
12+
// TreeNode
13+
size_t size() const override { return sizeof(KMatNode); }
14+
int numberOfChildren() const override;
15+
#if POINCARE_TREE_LOG
16+
void logNodeName(std::ostream & stream) const override {
17+
stream << "KMat";
18+
}
19+
#endif
20+
21+
// Properties
22+
Type type() const override { return Type::KMat; }
23+
24+
// Layout
25+
Layout createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
26+
int serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const override;
27+
28+
// Simplification
29+
Expression shallowReduce(ReductionContext reductionContext) override;
30+
LayoutShape leftLayoutShape() const override { return LayoutShape::MoreLetters; };
31+
LayoutShape rightLayoutShape() const override { return LayoutShape::BoundaryPunctuation; }
32+
33+
//Evaluation
34+
Evaluation<float> approximate(SinglePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return Complex<float>::RealUndefined(); }
35+
Evaluation<double> approximate(DoublePrecision p, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return Complex<double>::RealUndefined(); }
36+
};
37+
38+
class KMat final : public Expression {
39+
public:
40+
KMat(const KMatNode * n) : Expression(n) {}
41+
static KMat Builder(Expression child0, Expression child1, Expression child2) { return TreeHandle::FixedArityBuilder<KMat, KMatNode>({child0, child1, child2}); }
42+
43+
44+
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("kmat", 3, &UntypedBuilderThreeChildren<KMat>);
45+
46+
Expression shallowReduce(ExpressionNode::ReductionContext reductionContext);
47+
48+
};
49+
50+
}
51+
52+
#endif

poincare/include/poincare_nodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include <poincare/integral.h>
4646
#include <poincare/inv_binom.h>
4747
#include <poincare/inv_norm.h>
48+
#include <poincare/kmat.h>
4849
#include <poincare/least_common_multiple.h>
4950
#include <poincare/logarithm.h>
5051
#include <poincare/matrix.h>

poincare/src/kmat.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
}

poincare/src/parsing/parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ class Parser {
123123
&InvBinom::s_functionHelper,
124124
&MatrixInverse::s_functionHelper,
125125
&InvNorm::s_functionHelper,
126+
&KMat::s_functionHelper,
126127
&LeastCommonMultiple::s_functionHelper,
127128
&NaperianLogarithm::s_functionHelper,
128129
&CommonLogarithm::s_functionHelper,

poincare/src/tree_handle.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ template Integral TreeHandle::FixedArityBuilder<Integral, IntegralNode>(const Tu
324324
template IntegralLayout TreeHandle::FixedArityBuilder<IntegralLayout, IntegralLayoutNode>(const Tuple &);
325325
template InvBinom TreeHandle::FixedArityBuilder<InvBinom, InvBinomNode>(const Tuple &);
326326
template InvNorm TreeHandle::FixedArityBuilder<InvNorm, InvNormNode>(const Tuple &);
327+
template KMat TreeHandle::FixedArityBuilder<KMat, KMatNode>(const Tuple &);
327328
template LeastCommonMultiple TreeHandle::FixedArityBuilder<LeastCommonMultiple, LeastCommonMultipleNode>(const Tuple &);
328329
template LeftParenthesisLayout TreeHandle::FixedArityBuilder<LeftParenthesisLayout, LeftParenthesisLayoutNode>(const Tuple &);
329330
template LeftSquareBracketLayout TreeHandle::FixedArityBuilder<LeftSquareBracketLayout, LeftSquareBracketLayoutNode>(const Tuple &);

0 commit comments

Comments
 (0)