Skip to content

Commit

Permalink
Use makeExpr without validation in simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Apr 28, 2024
1 parent 3a26a01 commit bba34bb
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 92 deletions.
2 changes: 2 additions & 0 deletions include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ class Expression : public IExpressionCRTP<Expression> {

friend std::unique_ptr<IMathObject> detail::makeExpr(const IFunction &func, ArgumentPtrVector args);

friend std::unique_ptr<IMathObject> detail::makeExprWithValidation(const IFunction &func, ArgumentPtrVector args);

friend std::unique_ptr<IMathObject> parseRawExpr(const std::string &str);

friend Expression approximate(const Expression &rhs, unsigned precision);
Expand Down
2 changes: 0 additions & 2 deletions include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ ArgumentPtr invert(const ArgumentPtr &arg);

ArgumentPtrVector getPolynomChildren(const IFunction &func, const ArgumentPtr &arg);

ArgumentPtrVector argumentRefVectorToArgumentPtrVector(const ArgumentRefVector &args);

std::string putInBrackets(const std::string &str);

std::string putInSpaces(const std::string &str);
Expand Down
17 changes: 16 additions & 1 deletion include/fintamath/functions/FunctionUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@
}

namespace fintamath {

class IFunction;

namespace detail {

extern std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args);

extern std::unique_ptr<IMathObject> makeExpr(const IFunction &func, const ArgumentRefVector &args);
std::unique_ptr<IMathObject> makeExpr(const IFunction &func, const ArgumentRefVector &args);

std::unique_ptr<IMathObject> makeExpr(const IFunction &func, const std::derived_from<IMathObject> auto &...args) {
return makeExpr(func, ArgumentPtrVector{args.clone()...});
Expand All @@ -29,6 +30,20 @@ std::unique_ptr<IMathObject> makeExpr(const IFunction &func, std::convertible_to
return makeExpr(func, ArgumentPtrVector{ArgumentPtr(std::forward<decltype(args)>(args))...});
}

extern std::unique_ptr<IMathObject> makeExprWithValidation(const IFunction &func, ArgumentPtrVector args);

std::unique_ptr<IMathObject> makeExprWithValidation(const IFunction &func, const ArgumentRefVector &args);

std::unique_ptr<IMathObject> makeExprWithValidation(const IFunction &func, const std::derived_from<IMathObject> auto &...args) {
return makeExprWithValidation(func, ArgumentPtrVector{args.clone()...});
}

std::unique_ptr<IMathObject> makeExprWithValidation(const IFunction &func, std::convertible_to<ArgumentPtr> auto &&...args) {
return makeExprWithValidation(func, ArgumentPtrVector{ArgumentPtr(std::forward<decltype(args)>(args))...});
}

ArgumentPtrVector argumentRefVectorToArgumentPtrVector(const ArgumentRefVector &args);

}

}
2 changes: 1 addition & 1 deletion include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ class IFunctionCRTP_ : public IFunction {
}
}

return detail::makeExpr(*this, argVect)->toMinimalObject();
return detail::makeExprWithValidation(*this, argVect)->toMinimalObject();
}

private:
Expand Down
13 changes: 6 additions & 7 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ std::unique_ptr<IMathObject> Expression::objectsToExpr(ObjectStack &objects) {

if (isBinaryOperator(func.get())) {
const ArgumentPtr lhsChild = objectsToExpr(objects);
return makeExpr(*func, {lhsChild, rhsChild});
return makeExprWithValidation(*func, {lhsChild, rhsChild});
}

ArgumentPtrVector children = unwrapComma(rhsChild);
Expand All @@ -255,7 +255,7 @@ std::unique_ptr<IMathObject> Expression::objectsToExpr(ObjectStack &objects) {
}
}

return makeExpr(*func, std::move(children));
return makeExprWithValidation(*func, std::move(children));
}

return arg;
Expand Down Expand Up @@ -554,9 +554,6 @@ std::pair<MathObjectClass, bool> Expression::doesArgMatch(const MathObjectClass
namespace detail {

std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector args) {
std::ranges::transform(args, args.begin(), &Expression::compress);
Expression::validateFunctionArgs(func, args);

if (func.isVariadic() && args.size() == 1) {
return std::move(args.front())->clone();
}
Expand All @@ -572,8 +569,10 @@ std::unique_ptr<IMathObject> makeExpr(const IFunction &func, ArgumentPtrVector a
return FunctionExpression(func, std::move(args)).clone();
}

std::unique_ptr<IMathObject> makeExpr(const IFunction &func, const ArgumentRefVector &args) {
return makeExpr(func, argumentRefVectorToArgumentPtrVector(args));
std::unique_ptr<IMathObject> makeExprWithValidation(const IFunction &func, ArgumentPtrVector args) {
std::ranges::transform(args, args.begin(), &Expression::compress);
Expression::validateFunctionArgs(func, args);
return makeExpr(func, std::move(args));
}

}
Expand Down
88 changes: 45 additions & 43 deletions src/fintamath/expressions/ExpressionFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,180 +52,182 @@

namespace fintamath {

using namespace detail;

Expression operator+(const Expression &lhs, const Expression &rhs) {
return Expression(addExpr(lhs, rhs));
return Expression(makeExprWithValidation(Add{}, lhs, rhs));
}

Expression operator+(const Expression &rhs) {
return rhs;
}

Expression operator-(const Expression &lhs, const Expression &rhs) {
return Expression(subExpr(lhs, rhs));
return Expression(makeExprWithValidation(Sub{}, lhs, rhs));
}

Expression operator-(const Expression &rhs) {
return Expression(negExpr(rhs));
return Expression(makeExprWithValidation(Neg{}, rhs));
}

Expression operator*(const Expression &lhs, const Expression &rhs) {
return Expression(mulExpr(lhs, rhs));
return Expression(makeExprWithValidation(Mul{}, lhs, rhs));
}

Expression operator/(const Expression &lhs, const Expression &rhs) {
return Expression(divExpr(lhs, rhs));
return Expression(makeExprWithValidation(Div{}, lhs, rhs));
}

Expression mod(const Expression &lhs, const Expression &rhs) {
return Expression(modExpr(lhs, rhs));
return Expression(makeExprWithValidation(Mod{}, lhs, rhs));
}

Expression eqv(const Expression &lhs, const Expression &rhs) {
return Expression(eqvExpr(lhs, rhs));
return Expression(makeExprWithValidation(Eqv{}, lhs, rhs));
}

Expression neqv(const Expression &lhs, const Expression &rhs) {
return Expression(neqvExpr(lhs, rhs));
return Expression(makeExprWithValidation(Neqv{}, lhs, rhs));
}

Expression less(const Expression &lhs, const Expression &rhs) {
return Expression(lessExpr(lhs, rhs));
return Expression(makeExprWithValidation(Less{}, lhs, rhs));
}

Expression more(const Expression &lhs, const Expression &rhs) {
return Expression(moreExpr(lhs, rhs));
return Expression(makeExprWithValidation(More{}, lhs, rhs));
}

Expression lessEqv(const Expression &lhs, const Expression &rhs) {
return Expression(lessEqvExpr(lhs, rhs));
return Expression(makeExprWithValidation(LessEqv{}, lhs, rhs));
}

Expression moreEqv(const Expression &lhs, const Expression &rhs) {
return Expression(moreEqvExpr(lhs, rhs));
return Expression(makeExprWithValidation(MoreEqv{}, lhs, rhs));
}

Expression floor(const Expression &rhs) {
return Expression(floorExpr(rhs));
return Expression(makeExprWithValidation(Floor{}, rhs));
}

Expression ceil(const Expression &rhs) {
return Expression(ceilExpr(rhs));
return Expression(makeExprWithValidation(Ceil{}, rhs));
}

Expression abs(const Expression &rhs) {
return Expression(absExpr(rhs));
return Expression(makeExprWithValidation(Abs{}, rhs));
}

Expression factorial(const Expression &rhs) {
return Expression(factorialExpr(rhs));
return Expression(makeExprWithValidation(Factorial{}, rhs));
}

Expression sqrt(const Expression &rhs) {
return Expression(sqrtExpr(rhs));
return Expression(makeExprWithValidation(Sqrt{}, rhs));
}

Expression pow(const Expression &lhs, const Expression &rhs) {
return Expression(powExpr(lhs, rhs));
return Expression(makeExprWithValidation(Pow{}, lhs, rhs));
}

Expression exp(const Expression &rhs) {
return Expression(expExpr(rhs));
return Expression(makeExprWithValidation(Exp{}, rhs));
}

Expression log(const Expression &lhs, const Expression &rhs) {
return Expression(logExpr(lhs, rhs));
return Expression(makeExprWithValidation(Log{}, lhs, rhs));
}

Expression ln(const Expression &rhs) {
return Expression(lnExpr(rhs));
return Expression(makeExprWithValidation(Ln{}, rhs));
}

Expression lb(const Expression &rhs) {
return Expression(lbExpr(rhs));
return Expression(makeExprWithValidation(Lb{}, rhs));
}

Expression lg(const Expression &rhs) {
return Expression(lgExpr(rhs));
return Expression(makeExprWithValidation(Lg{}, rhs));
}

Expression sin(const Expression &rhs) {
return Expression(sinExpr(rhs));
return Expression(makeExprWithValidation(Sin{}, rhs));
}

Expression cos(const Expression &rhs) {
return Expression(cosExpr(rhs));
return Expression(makeExprWithValidation(Cos{}, rhs));
}

Expression tan(const Expression &rhs) {
return Expression(tanExpr(rhs));
return Expression(makeExprWithValidation(Tan{}, rhs));
}

Expression cot(const Expression &rhs) {
return Expression(cotExpr(rhs));
return Expression(makeExprWithValidation(Cot{}, rhs));
}

Expression asin(const Expression &rhs) {
return Expression(asinExpr(rhs));
return Expression(makeExprWithValidation(Asin{}, rhs));
}

Expression acos(const Expression &rhs) {
return Expression(acosExpr(rhs));
return Expression(makeExprWithValidation(Acos{}, rhs));
}

Expression atan(const Expression &rhs) {
return Expression(atanExpr(rhs));
return Expression(makeExprWithValidation(Atan{}, rhs));
}

Expression acot(const Expression &rhs) {
return Expression(acotExpr(rhs));
return Expression(makeExprWithValidation(Acot{}, rhs));
}

Expression sinh(const Expression &rhs) {
return Expression(sinhExpr(rhs));
return Expression(makeExprWithValidation(Sinh{}, rhs));
}

Expression cosh(const Expression &rhs) {
return Expression(coshExpr(rhs));
return Expression(makeExprWithValidation(Cosh{}, rhs));
}

Expression tanh(const Expression &rhs) {
return Expression(tanhExpr(rhs));
return Expression(makeExprWithValidation(Tanh{}, rhs));
}

Expression coth(const Expression &rhs) {
return Expression(cothExpr(rhs));
return Expression(makeExprWithValidation(Coth{}, rhs));
}

Expression asinh(const Expression &rhs) {
return Expression(asinhExpr(rhs));
return Expression(makeExprWithValidation(Asinh{}, rhs));
}

Expression acosh(const Expression &rhs) {
return Expression(acoshExpr(rhs));
return Expression(makeExprWithValidation(Acosh{}, rhs));
}

Expression atanh(const Expression &rhs) {
return Expression(atanhExpr(rhs));
return Expression(makeExprWithValidation(Atanh{}, rhs));
}

Expression acoth(const Expression &rhs) {
return Expression(acothExpr(rhs));
return Expression(makeExprWithValidation(Acoth{}, rhs));
}

Expression derivative(const Expression &lhs, const Expression &rhs) {
return Expression(derivativeExpr(lhs, rhs));
return Expression(makeExprWithValidation(Derivative{}, lhs, rhs));
}

Expression notL(const Expression &rhs) {
return Expression(notExpr(rhs));
return Expression(makeExprWithValidation(Not{}, rhs));
}

Expression andL(const Expression &lhs, const Expression &rhs) {
return Expression(andExpr(lhs, rhs));
return Expression(makeExprWithValidation(And{}, lhs, rhs));
}

Expression orL(const Expression &lhs, const Expression &rhs) {
return Expression(orExpr(lhs, rhs));
return Expression(makeExprWithValidation(Or{}, lhs, rhs));
}

Expression e() {
Expand Down
10 changes: 0 additions & 10 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,16 +270,6 @@ ArgumentPtrVector getPolynomChildren(const IFunction &func, const ArgumentPtr &a
return {arg};
}

ArgumentPtrVector argumentRefVectorToArgumentPtrVector(const ArgumentRefVector &args) {
ArgumentPtrVector argsPtrVect;

for (const auto &arg : args) {
argsPtrVect.emplace_back(arg.get().clone());
}

return argsPtrVect;
}

std::string putInBrackets(const std::string &str) {
return '(' + str + ')';
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/FunctionExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ ArgumentPtr FunctionExpression::simplifyRec(bool isPostSimplify) const {
}

void FunctionExpression::setChildren(const ArgumentPtrVector &childVect) {
(void)makeExpr(*func, childVect);
(void)makeExprWithValidation(*func, childVect);

children = childVect;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/interfaces/IBinaryExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ IBinaryExpression::SimplifyFunctionVector IBinaryExpression::getFunctionsForPost
}

void IBinaryExpression::setChildren(const ArgumentPtrVector &childVect) {
(void)makeExpr(*func, childVect);
(void)makeExprWithValidation(*func, childVect);

lhsChild = childVect[0];
rhsChild = childVect[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const ArgumentPtrVector &IPolynomExpression::getChildren() const {
}

void IPolynomExpression::setChildren(const ArgumentPtrVector &childVect) {
(void)makeExpr(*func, childVect);
(void)makeExprWithValidation(*func, childVect);

children = childVect;
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/interfaces/IUnaryExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ArgumentPtr IUnaryExpression::simplifyRec(const bool isPostSimplify) const {
}

void IUnaryExpression::setChildren(const ArgumentPtrVector &childVect) {
(void)makeExpr(*func, childVect);
(void)makeExprWithValidation(*func, childVect);

child = childVect.front();
}
Expand Down
Loading

0 comments on commit bba34bb

Please sign in to comment.