mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-18 16:27:34 +01:00
[poincare] MatrixIdentity
This commit is contained in:
@@ -86,6 +86,7 @@ src += $(addprefix poincare/src/,\
|
||||
matrix.cpp \
|
||||
matrix_complex.cpp \
|
||||
matrix_dimension.cpp \
|
||||
matrix_identity.cpp \
|
||||
matrix_inverse.cpp \
|
||||
matrix_trace.cpp \
|
||||
matrix_transpose.cpp \
|
||||
|
||||
@@ -56,6 +56,7 @@ class Expression : public TreeHandle {
|
||||
friend class Logarithm;
|
||||
friend class Matrix;
|
||||
friend class MatrixDimension;
|
||||
friend class MatrixIdentity;
|
||||
friend class MatrixInverse;
|
||||
friend class MatrixTrace;
|
||||
friend class MatrixTranspose;
|
||||
|
||||
@@ -93,6 +93,7 @@ public:
|
||||
Matrix,
|
||||
ConfidenceInterval,
|
||||
MatrixDimension,
|
||||
MatrixIdentity,
|
||||
MatrixInverse,
|
||||
MatrixTranspose,
|
||||
PredictionInterval,
|
||||
|
||||
45
poincare/include/poincare/matrix_identity.h
Normal file
45
poincare/include/poincare/matrix_identity.h
Normal file
@@ -0,0 +1,45 @@
|
||||
#ifndef POINCARE_MATRIX_IDENTITY_H
|
||||
#define POINCARE_MATRIX_IDENTITY_H
|
||||
|
||||
#include <poincare/expression.h>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
class MatrixIdentityNode final : public ExpressionNode {
|
||||
public:
|
||||
// TreeNode
|
||||
size_t size() const override { return sizeof(MatrixIdentityNode); }
|
||||
int numberOfChildren() const override;
|
||||
#if POINCARE_TREE_LOG
|
||||
virtual void logNodeName(std::ostream & stream) const override {
|
||||
stream << "MatrixIdentity";
|
||||
}
|
||||
#endif
|
||||
|
||||
// Properties
|
||||
Type type() const override { return Type::MatrixIdentity; }
|
||||
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(Context & context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ReductionTarget target) override;
|
||||
// Evaluation
|
||||
Evaluation<float> approximate(SinglePrecision p, Context& context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<float>(context, complexFormat, angleUnit); }
|
||||
Evaluation<double> approximate(DoublePrecision p, Context& context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const override { return templatedApproximate<double>(context, complexFormat, angleUnit); }
|
||||
template<typename T> Evaluation<T> templatedApproximate(Context& context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const;
|
||||
};
|
||||
|
||||
class MatrixIdentity final : public Expression {
|
||||
public:
|
||||
MatrixIdentity(const MatrixIdentityNode * n) : Expression(n) {}
|
||||
static MatrixIdentity Builder(Expression child) { return TreeHandle::FixedArityBuilder<MatrixIdentity, MatrixIdentityNode>(&child, 1); }
|
||||
|
||||
static constexpr Expression::FunctionHelper s_functionHelper = Expression::FunctionHelper("identity", 1, &UntypedBuilderOneChild<MatrixIdentity>);
|
||||
|
||||
Expression shallowReduce(Context & context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ExpressionNode::ReductionTarget target);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -47,6 +47,7 @@
|
||||
#include <poincare/matrix.h>
|
||||
#include <poincare/matrix_complex.h>
|
||||
#include <poincare/matrix_dimension.h>
|
||||
#include <poincare/matrix_identity.h>
|
||||
#include <poincare/matrix_inverse.h>
|
||||
#include <poincare/matrix_trace.h>
|
||||
#include <poincare/matrix_transpose.h>
|
||||
|
||||
56
poincare/src/matrix_identity.cpp
Normal file
56
poincare/src/matrix_identity.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <poincare/matrix_identity.h>
|
||||
#include <poincare/matrix_complex.h>
|
||||
#include <poincare/layout_helper.h>
|
||||
#include <poincare/serialization_helper.h>
|
||||
#include <poincare/undefined.h>
|
||||
#include <cmath>
|
||||
|
||||
namespace Poincare {
|
||||
|
||||
constexpr Expression::FunctionHelper MatrixIdentity::s_functionHelper;
|
||||
|
||||
int MatrixIdentityNode::numberOfChildren() const { return MatrixIdentity::s_functionHelper.numberOfChildren(); }
|
||||
|
||||
Expression MatrixIdentityNode::shallowReduce(Context & context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ReductionTarget target) {
|
||||
return MatrixIdentity(this).shallowReduce(context, complexFormat, angleUnit, target);
|
||||
}
|
||||
|
||||
Layout MatrixIdentityNode::createLayout(Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
|
||||
return LayoutHelper::Prefix(MatrixIdentity(this), floatDisplayMode, numberOfSignificantDigits, MatrixIdentity::s_functionHelper.name());
|
||||
}
|
||||
|
||||
int MatrixIdentityNode::serialize(char * buffer, int bufferSize, Preferences::PrintFloatMode floatDisplayMode, int numberOfSignificantDigits) const {
|
||||
return SerializationHelper::Prefix(this, buffer, bufferSize, floatDisplayMode, numberOfSignificantDigits, MatrixIdentity::s_functionHelper.name());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Evaluation<T> MatrixIdentityNode::templatedApproximate(Context& context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) const {
|
||||
Evaluation<T> input = childAtIndex(0)->approximate(T(), context, complexFormat, angleUnit);
|
||||
// Check if the child is an integer
|
||||
if (input.type() == EvaluationNode<T>::Type::Complex) {
|
||||
// The child is a complex
|
||||
std::complex<T> c = static_cast<Complex<T>&>(input).stdComplex();
|
||||
T im = c.imag();
|
||||
T re = c.real();
|
||||
if ((im == 0.0 || std::isnan(im)) // The child is a real
|
||||
&& (!std::isnan(re) && re > 0) // The child is positive
|
||||
&& (std::ceil(re) == std::floor(re))) // The child is an integer
|
||||
{
|
||||
return MatrixComplex<T>::CreateIdentity((int)re);
|
||||
}
|
||||
}
|
||||
return Complex<T>::Undefined();
|
||||
}
|
||||
|
||||
|
||||
Expression MatrixIdentity::shallowReduce(Context & context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit, ExpressionNode::ReductionTarget target) {
|
||||
{
|
||||
Expression e = Expression::defaultShallowReduce();
|
||||
if (e.isUndefined()) {
|
||||
return e;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -109,6 +109,7 @@ private:
|
||||
&Floor::s_functionHelper,
|
||||
&FracPart::s_functionHelper,
|
||||
&GreatCommonDivisor::s_functionHelper,
|
||||
&MatrixIdentity::s_functionHelper,
|
||||
&ImaginaryPart::s_functionHelper,
|
||||
&Integral::s_functionHelper,
|
||||
&MatrixInverse::s_functionHelper,
|
||||
|
||||
@@ -310,6 +310,7 @@ template Matrix TreeHandle::NAryBuilder<Matrix, MatrixNode>(TreeHandle*, size_t)
|
||||
template MatrixComplex<double> TreeHandle::NAryBuilder<MatrixComplex<double>, MatrixComplexNode<double> >(TreeHandle*, size_t);
|
||||
template MatrixComplex<float> TreeHandle::NAryBuilder<MatrixComplex<float>, MatrixComplexNode<float> >(TreeHandle*, size_t);
|
||||
template MatrixDimension TreeHandle::FixedArityBuilder<MatrixDimension, MatrixDimensionNode>(TreeHandle*, size_t);
|
||||
template MatrixIdentity TreeHandle::FixedArityBuilder<MatrixIdentity, MatrixIdentityNode>(TreeHandle*, size_t);
|
||||
template MatrixInverse TreeHandle::FixedArityBuilder<MatrixInverse, MatrixInverseNode>(TreeHandle*, size_t);
|
||||
template MatrixTrace TreeHandle::FixedArityBuilder<MatrixTrace, MatrixTraceNode>(TreeHandle*, size_t);
|
||||
template MatrixTranspose TreeHandle::FixedArityBuilder<MatrixTranspose, MatrixTransposeNode>(TreeHandle*, size_t);
|
||||
|
||||
Reference in New Issue
Block a user