Skip to content

Commit

Permalink
Implement ComplexInf
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jul 28, 2023
1 parent 55b7674 commit 5fca39f
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 28 deletions.
1 change: 1 addition & 0 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ enum class MathObjectType : MathObjectTypeId {
False,
Inf,
NegInf,
ComplexInf,
Undefined,

IFunction = 11000,
Expand Down
22 changes: 22 additions & 0 deletions include/fintamath/literals/constants/ComplexInf.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/numbers/INumber.hpp"

namespace fintamath {

class ComplexInf : public IConstantCRTP<INumber, ComplexInf> {
public:
std::string toString() const override {
return "ComplexInf";
}

static MathObjectTypeId getTypeIdStatic() {
return MathObjectTypeId(MathObjectType::ComplexInf);
}

protected:
std::unique_ptr<IMathObject> call() const override;
};

}
2 changes: 2 additions & 0 deletions src/fintamath/config/ParserConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
#include "fintamath/literals/Boolean.hpp"
#include "fintamath/literals/ILiteral.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/E.hpp"
#include "fintamath/literals/constants/False.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
Expand Down Expand Up @@ -170,6 +171,7 @@ struct ParserConfig {
IConstant::registerType<False>();
IConstant::registerType<Inf>();
IConstant::registerType<NegInf>();
IConstant::registerType<ComplexInf>();
IConstant::registerType<Undefined>();

IFunction::registerType<Abs>();
Expand Down
34 changes: 34 additions & 0 deletions src/fintamath/expressions/binary/CompExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
#include "fintamath/functions/comparison/MoreEqv.hpp"
#include "fintamath/functions/comparison/Neqv.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
#include "fintamath/numbers/Integer.hpp"

namespace fintamath {
Expand Down Expand Up @@ -58,8 +62,16 @@ ArgumentPtr CompExpression::preSimplify() const {
return simpl;
}

CompExpression::SimplifyFunctionsVector CompExpression::getFunctionsForPreSimplify() const {
static const CompExpression::SimplifyFunctionsVector simplifyFunctions = {
&CompExpression::constSimplify, //
};
return simplifyFunctions;
}

CompExpression::SimplifyFunctionsVector CompExpression::getFunctionsForPostSimplify() const {
static const CompExpression::SimplifyFunctionsVector simplifyFunctions = {
&CompExpression::constSimplify, //
&CompExpression::divSimplify, //
&CompExpression::coeffSimplify, //
};
Expand All @@ -82,6 +94,28 @@ std::shared_ptr<IFunction> CompExpression::getOppositeFunction(const IFunction &
return oppositeFunctions.at(function.toString());
}

ArgumentPtr CompExpression::constSimplify(const IFunction & /*func*/, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
if (is<ComplexInf>(lhs) && (is<Inf>(rhs) || is<NegInf>(rhs) || is<ComplexInf>(rhs))) {
return Undefined().clone();
}

if ((is<Inf>(lhs) || is<NegInf>(lhs) || is<ComplexInf>(lhs)) && is<ComplexInf>(rhs)) {
return Undefined().clone();
}

if ((is<Inf>(lhs) && is<Inf>(rhs)) || (is<NegInf>(lhs) && is<NegInf>(rhs))) {
return Boolean(true).clone();
}

if (is<Inf>(lhs) || is<NegInf>(lhs) || is<ComplexInf>(lhs) || //
is<Inf>(rhs) || is<NegInf>(rhs) || is<ComplexInf>(rhs)) {

return Boolean(false).clone();
}

return {};
}

ArgumentPtr CompExpression::divSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
if (const auto lhsExpr = cast<IExpression>(lhs); lhsExpr && is<Div>(lhsExpr->getFunction())) {
return makeExpr(func, lhsExpr->getChildren().front(), rhs);
Expand Down
4 changes: 4 additions & 0 deletions src/fintamath/expressions/binary/CompExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class CompExpression : public IBinaryExpressionCRTP<CompExpression, true> {
protected:
ArgumentPtr preSimplify() const override;

SimplifyFunctionsVector getFunctionsForPreSimplify() const override;

SimplifyFunctionsVector getFunctionsForPostSimplify() const override;

static std::map<std::string, std::shared_ptr<IFunction>, std::less<>> &getOppositeFunctionsMap();
Expand All @@ -30,6 +32,8 @@ class CompExpression : public IBinaryExpressionCRTP<CompExpression, true> {
static std::shared_ptr<IFunction> getLogicOppositeFunction(const IFunction &function);

private:
static ArgumentPtr constSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs);

static ArgumentPtr divSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs);

static ArgumentPtr coeffSimplify(const IFunction &func, const ArgumentPtr &lhs, const ArgumentPtr &rhs);
Expand Down
9 changes: 5 additions & 4 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/powers/Pow.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
Expand Down Expand Up @@ -53,8 +54,8 @@ DivExpression::SimplifyFunctionsVector DivExpression::getFunctionsForPostSimplif
}

ArgumentPtr DivExpression::constSimplify(const IFunction & /*func*/, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
if ((*lhs == Integer(0) || is<Inf>(lhs) || is<NegInf>(lhs)) &&
(*rhs == Integer(0) || is<Inf>(rhs) || is<NegInf>(rhs))) {
if ((*lhs == Integer(0) || is<Inf>(lhs) || is<NegInf>(lhs) || is<ComplexInf>(lhs)) &&
(*rhs == Integer(0) || is<Inf>(rhs) || is<NegInf>(rhs) || is<ComplexInf>(rhs))) {

return Undefined().clone();
}
Expand All @@ -64,10 +65,10 @@ ArgumentPtr DivExpression::constSimplify(const IFunction & /*func*/, const Argum
}

if (*rhs == Integer(0)) {
return Inf().clone();
return ComplexInf().clone();
}

if (is<Inf>(rhs) || is<NegInf>(rhs)) {
if (is<Inf>(rhs) || is<NegInf>(rhs) || is<ComplexInf>(rhs)) {
return Integer(0).clone();
}

Expand Down
22 changes: 18 additions & 4 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/expressions/ExpressionUtils.hpp"
#include "fintamath/functions/arithmetic/Abs.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/powers/Pow.hpp"
#include "fintamath/functions/powers/Root.hpp"
#include "fintamath/functions/powers/Sqrt.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
Expand Down Expand Up @@ -226,7 +228,7 @@ ArgumentPtr PowExpression::constSimplify(const IFunction & /*func*/, const Argum
return makeExpr(Mul(), mulLhs, mulRhs);
}

if (is<Inf>(lhs)) {
if (is<Inf>(lhs) || is<ComplexInf>(lhs)) {
if (*rhs == Integer(0)) {
return Undefined().clone();
}
Expand All @@ -238,11 +240,23 @@ ArgumentPtr PowExpression::constSimplify(const IFunction & /*func*/, const Argum
return lhs;
}

if (*lhs == Integer(1)) {
if (is<Inf>(rhs)) {
return Undefined().clone();
if (is<Inf>(rhs) || is<ComplexInf>(rhs)) {
if (const auto lhsNum = cast<INumber>(lhs)) {
auto lhsNumAbs = cast<INumber>(Abs()(*lhsNum));

if (*lhsNumAbs == Integer(1)) {
return Undefined().clone();
}

if (*lhsNumAbs > Integer(1)) {
return ComplexInf().clone();
}

return Integer(0).clone();
}
}

if (*lhs == Integer(1)) {
return lhs;
}

Expand Down
7 changes: 6 additions & 1 deletion src/fintamath/expressions/polynomial/AddExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "fintamath/functions/logarithms/Log.hpp"
#include "fintamath/functions/powers/Pow.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
Expand Down Expand Up @@ -104,14 +105,18 @@ ArgumentPtr AddExpression::simplifyConst(const IFunction & /*func*/, const Argum
return lhsChild;
}

if (is<NegInf>(lhsChild) && is<Inf>(rhsChild)) {
if ((is<NegInf>(lhsChild) || is<ComplexInf>(lhsChild)) && (is<Inf>(rhsChild) || is<ComplexInf>(rhsChild))) {
return Undefined().clone();
}

if (is<Inf>(lhsChild) || is<NegInf>(lhsChild)) {
return lhsChild;
}

if (is<Inf>(rhsChild) || is<NegInf>(rhsChild)) {
return rhsChild;
}

return {};
}

Expand Down
9 changes: 7 additions & 2 deletions src/fintamath/expressions/polynomial/MulExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/powers/Pow.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"
Expand Down Expand Up @@ -92,15 +93,19 @@ std::pair<ArgumentPtr, ArgumentPtr> MulExpression::getRateValuePair(const Argume
ArgumentPtr MulExpression::simplifyConst(const IFunction & /*func*/, const ArgumentPtr &lhsChild,
const ArgumentPtr &rhsChild) {

if (*lhsChild == Integer(0) && (is<Inf>(rhsChild) || is<NegInf>(rhsChild))) {
if (*lhsChild == Integer(0) && (is<Inf>(rhsChild) || is<NegInf>(rhsChild) || is<ComplexInf>(rhsChild))) {
return Undefined().clone();
}

if (is<ComplexInf>(lhsChild) || is<ComplexInf>(rhsChild)) {
return ComplexInf().clone();
}

if (is<NegInf>(lhsChild) && is<Inf>(rhsChild)) {
return lhsChild;
}

if (is<Inf>(rhsChild) || is<NegInf>(rhsChild)) {
if (is<Inf>(rhsChild) || is<NegInf>(rhsChild) || is<ComplexInf>(rhsChild)) {
if (const auto lhsNum = cast<INumber>(lhsChild)) {
return *lhsNum < Integer(0) ? makeExpr(Neg(), rhsChild) : rhsChild;
}
Expand Down
5 changes: 5 additions & 0 deletions src/fintamath/expressions/unary/NegExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/logarithms/Log.hpp"
#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/literals/constants/NegInf.hpp"

Expand Down Expand Up @@ -92,6 +93,10 @@ ArgumentPtr NegExpression::simplifyConst(const IFunction & /*func*/, const Argum
return Inf().clone();
}

if (is<ComplexInf>(rhs)) {
return rhs;
}

return {};
}

Expand Down
8 changes: 4 additions & 4 deletions src/fintamath/functions/arithmetic/Div.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "fintamath/literals/constants/Inf.hpp"
#include "fintamath/numbers/INumber.hpp"

#include "fintamath/functions/arithmetic/Div.hpp"

#include "fintamath/literals/constants/ComplexInf.hpp"
#include "fintamath/literals/constants/Undefined.hpp"
#include "fintamath/numbers/INumber.hpp"
#include "fintamath/numbers/Integer.hpp"
#include "fintamath/numbers/Rational.hpp"

Expand All @@ -27,7 +27,7 @@ std::unique_ptr<IMathObject> Div::call(const ArgumentsRefVector &argsVect) const
}

if (rhs == Integer(0)) {
return Inf().clone();
return ComplexInf().clone();
}

if (auto res = multiPow(lhs, rhs)) {
Expand Down
11 changes: 11 additions & 0 deletions src/fintamath/literals/constants/ComplexInf.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include "fintamath/literals/constants/ComplexInf.hpp"

#include "fintamath/numbers/RealFunctions.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> ComplexInf::call() const {
return clone();
}

}
Loading

0 comments on commit 5fca39f

Please sign in to comment.