-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters