Skip to content

Commit

Permalink
Improve coverage & fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Aug 26, 2023
1 parent d6c6524 commit 07bc0c2
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class IPolynomExpression : public IExpression {

virtual SimplifyFunctionsVector getFunctionsForPostSimplify() const;

virtual std::string childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const;
virtual std::string childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const;

/**
* @brief
Expand Down
10 changes: 9 additions & 1 deletion src/fintamath/expressions/binary/CompExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ ArgumentPtr CompExpression::preSimplify() const {
if (!simplExpr->isSolution &&
(!is<Integer>(rhsChild) || *rhsChild != Integer(0))) {

if (*func != Eqv() && *func != Neqv() &&
if (*func != Eqv() &&
*func != Neqv() &&
(hasComplex(lhsChild) || hasComplex(rhsChild))) {

return simpl;
Expand Down Expand Up @@ -153,6 +154,13 @@ ArgumentPtr CompExpression::coeffSimplify(const IFunction &func, const ArgumentP
return {};
}

if (func != Eqv() &&
func != Neqv() &&
(hasComplex(lhs) || hasComplex(rhs))) {

return {};
}

ArgumentsPtrVector dividendPolynom;
ArgumentPtr polynomFirstChild;

Expand Down
18 changes: 6 additions & 12 deletions src/fintamath/expressions/interfaces/IPolynomExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ ArgumentsPtrVector IPolynomExpression::getChildren() const {
}

std::string IPolynomExpression::toString() const {
if (!is<IOperator>(func)) {
const auto oper = cast<IOperator>(func);
if (!oper) {
return functionToString(*func, children);
}

std::string result;

result += childToString(children.front(), {});
result += childToString(*oper, children.front(), {});

for (size_t i = 1; i < children.size(); i++) {
result += childToString(children[i], children[i - 1]);
result += childToString(*oper, children[i], children[i - 1]);
}

return result;
Expand Down Expand Up @@ -180,15 +181,8 @@ ArgumentPtr IPolynomExpression::preciseSimplify() const {
return preciseExpr;
}

std::string IPolynomExpression::childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string childStr;
if (const auto oper = cast<IOperator>(func)) {
childStr = operatorChildToString(*oper, inChild);
}
else {
childStr = inChild->toString();
}

std::string IPolynomExpression::childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string childStr = operatorChildToString(oper, inChild);
return prevChild ? (putInSpaces(func->toString()) + childStr) : childStr;
}

Expand Down
6 changes: 3 additions & 3 deletions src/fintamath/expressions/polynomial/AddExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ AddExpression::AddExpression(const ArgumentsPtrVector &inChildren)
: IPolynomExpressionCRTP(Add(), inChildren) {
}

std::string AddExpression::childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string result = operatorChildToString(Add(), inChild);
std::string AddExpression::childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string result = operatorChildToString(oper, inChild);
bool isChildNegated = false;

if (result.size() > 1 &&
Expand All @@ -39,7 +39,7 @@ std::string AddExpression::childToString(const ArgumentPtr &inChild, const Argum
funcStr = Sub().toString();
}
else if (prevChild) {
funcStr = Add().toString();
funcStr = oper.toString();
}

if (!funcStr.empty()) {
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/AddExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class AddExpression : public IPolynomExpressionCRTP<AddExpression> {

SimplifyFunctionsVector getFunctionsForPostSimplify() const override;

std::string childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;
std::string childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;

/**
* @brief
Expand Down
6 changes: 3 additions & 3 deletions src/fintamath/expressions/polynomial/MulExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,22 @@ std::string MulExpression::toString() const {
return IPolynomExpression::toString();
}

std::string MulExpression::childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string MulExpression::childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
if (!prevChild && *inChild == Integer(-1)) {
return Neg().toString();
}

std::string operStr;
if (prevChild && *prevChild != Integer(-1)) {
if (is<INumber>(inChild) && is<INumber>(prevChild)) {
operStr = "*";
operStr = oper.toString();
}
else {
operStr = " ";
}
}

return operStr + operatorChildToString(Mul(), inChild);
return operStr + operatorChildToString(oper, inChild);
}

MulExpression::SimplifyFunctionsVector MulExpression::getFunctionsForPreSimplify() const {
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/MulExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class MulExpression : public IPolynomExpressionCRTP<MulExpression> {
}

protected:
std::string childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;
std::string childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;

SimplifyFunctionsVector getFunctionsForPreSimplify() const override;

Expand Down
4 changes: 2 additions & 2 deletions src/fintamath/expressions/polynomial/OrExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ OrExpression::OrExpression(const ArgumentsPtrVector &inChildren)
: IPolynomExpressionCRTP(Or(), inChildren) {
}

std::string OrExpression::childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string result = operatorChildToString(Or(), inChild);
std::string OrExpression::childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const {
std::string result = operatorChildToString(oper, inChild);

if (const auto &childExpr = cast<IExpression>(inChild);
childExpr && is<And>(childExpr->getFunction())) {
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/OrExpression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class OrExpression : public IPolynomExpressionCRTP<OrExpression> {
}

protected:
std::string childToString(const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;
std::string childToString(const IOperator &oper, const ArgumentPtr &inChild, const ArgumentPtr &prevChild) const override;

ArgumentPtr postSimplify() const override;

Expand Down
7 changes: 6 additions & 1 deletion tests/src/expressions/ExpressionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -978,26 +978,31 @@ TEST(ExpressionTests, stringConstructorTest) {
EXPECT_EQ(Expression("I = I").toString(), "True");
EXPECT_EQ(Expression("I = 2 I").toString(), "False");
EXPECT_EQ(Expression("I = I x").toString(), "x - 1 = 0");
EXPECT_EQ(Expression("I x = I").toString(), "x - 1 = 0");

EXPECT_EQ(Expression("I != I").toString(), "False");
EXPECT_EQ(Expression("I != 2 I").toString(), "True");
EXPECT_EQ(Expression("I != I x").toString(), "x - 1 != 0");
EXPECT_EQ(Expression("I x != I").toString(), "x - 1 != 0");

EXPECT_EQ(Expression("I < I").toString(), "I < I");
EXPECT_EQ(Expression("I < 2 I").toString(), "I < 2 I");
EXPECT_EQ(Expression("I < I x").toString(), "I < I x");
EXPECT_EQ(Expression("I x < I").toString(), "I x < I");

EXPECT_EQ(Expression("I > I").toString(), "I > I");
EXPECT_EQ(Expression("I > 2 I").toString(), "I > 2 I");
EXPECT_EQ(Expression("I > I x").toString(), "I > I x");
EXPECT_EQ(Expression("I x > I").toString(), "I x > I");

EXPECT_EQ(Expression("I <= I").toString(), "I <= I");
EXPECT_EQ(Expression("I <= 2 I").toString(), "I <= 2 I");
EXPECT_EQ(Expression("I <= I x").toString(), "I <= I x");
EXPECT_EQ(Expression("I x <= I").toString(), "I x <= I");

EXPECT_EQ(Expression("I >= I").toString(), "I >= I");
EXPECT_EQ(Expression("I >= 2 I").toString(), "I >= 2 I");
EXPECT_EQ(Expression("I >= I x").toString(), "I >= I x");
EXPECT_EQ(Expression("I x >= I").toString(), "I x >= I");

EXPECT_EQ(Expression("x (2 + I)").toString(), "(2 + I) x"); // TODO! simplify
EXPECT_EQ(Expression("sqrt(3) (2/3 + 1/2 I)").toString(), "(2/3 + 1/2 I) sqrt(3)"); // TODO! simplify
Expand Down
2 changes: 2 additions & 0 deletions tests/src/functions/hyperbolic/AcoshTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ TEST(AcoshTests, callTest) {

EXPECT_EQ(f(Complex(1, 1))->toString(), "acosh(1 + I)");

EXPECT_EQ(f(Integer(-2))->toString(), "acosh(-2)");

EXPECT_EQ(f(Variable("a"))->toString(), "acosh(a)");

EXPECT_THROW(f(), InvalidInputFunctionException);
Expand Down
2 changes: 2 additions & 0 deletions tests/src/functions/hyperbolic/AcothTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ TEST(AcothTests, callTest) {

EXPECT_EQ(f(Complex(1, 1))->toString(), "acoth(1 + I)");

EXPECT_EQ(f(Real("0.5"))->toString(), "acoth(0.5)");

EXPECT_EQ(f(Variable("a"))->toString(), "acoth(a)");

EXPECT_THROW(f(), InvalidInputFunctionException);
Expand Down
1 change: 1 addition & 0 deletions tests/src/functions/logarithms/LogTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ TEST(LogTests, callTest) {

EXPECT_EQ(f(Integer(-10), Integer(10))->toString(), "log(-10, 10)");
EXPECT_EQ(f(Integer(10), Integer(-10))->toString(), "log(10, -10)");
EXPECT_EQ(f(Real("-10.2"), Integer(-10))->toString(), "log(-10.2, -10)");

EXPECT_EQ(f(Variable("a"), Variable("b"))->toString(), "log(a, b)");

Expand Down

0 comments on commit 07bc0c2

Please sign in to comment.