Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overall refactoring #181

Merged
merged 1 commit into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/fintamath/core/IMathObject.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <concepts>
#include <memory>
#include <ostream>
#include <string>
Expand Down Expand Up @@ -59,6 +60,7 @@ template <typename Derived>
class IMathObjectCRTP : public IMathObject {
#define I_MATH_OBJECT_CRTP IMathObjectCRTP
#include "fintamath/core/IMathObjectCRTP.hpp"

#undef I_MATH_OBJECT_CRTP
};

Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class Expression final : public IExpressionCRTP<Expression> {

static void insertMultiplications(TermVector &terms);

static void fixOperatorTypes(TermVector &terms);
static void fixOperatorTypes(const TermVector &terms);

static void collapseFactorials(TermVector &terms);

Expand Down
2 changes: 0 additions & 2 deletions include/fintamath/expressions/ExpressionUtils.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include <concepts>
#include <cstddef>
#include <functional>
#include <string>
#include <utility>
#include <vector>

#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/IFunction.hpp"
#include "fintamath/functions/IOperator.hpp"
Expand Down
1 change: 0 additions & 1 deletion include/fintamath/functions/calculus/Max.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <memory>
#include <string>
#include <type_traits>

#include "fintamath/core/IComparable.hpp"
#include "fintamath/core/IMathObject.hpp"
Expand Down
1 change: 0 additions & 1 deletion include/fintamath/functions/calculus/Min.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <memory>
#include <string>
#include <type_traits>

#include "fintamath/core/IComparable.hpp"
#include "fintamath/core/IMathObject.hpp"
Expand Down
1 change: 0 additions & 1 deletion include/fintamath/functions/other/Comma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <memory>
#include <string>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/literals/constants/Undefined.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <memory>
#include <string>

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/literals/constants/IConstant.hpp"
#include "fintamath/numbers/INumber.hpp"

namespace fintamath {

Expand Down
1 change: 1 addition & 0 deletions include/fintamath/numbers/Real.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/core/IComparable.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/numbers/INumber.hpp"
#include "fintamath/numbers/Integer.hpp"
Expand Down
42 changes: 21 additions & 21 deletions src/fintamath/config/ExpressionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void registerFunctionExpressionMakers() {
return divExpr(std::move(args));
});

Expression::registerFunctionExpressionMaker<FracMixed>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<FracMixed>([](ArgumentPtrVector args) {
ArgumentPtr integ = std::move(args[0]);
ArgumentPtr numer = std::move(args[1]);
ArgumentPtr denom = std::move(args[2]);
Expand Down Expand Up @@ -255,9 +255,9 @@ void registerFunctionExpressionMakers() {
return orExpr(std::move(notLhsAndRhs), std::move(lhsAndNotRhs));
});

Expression::registerFunctionExpressionMaker<Neg>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Neg>([](ArgumentPtrVector args) {
static const ArgumentPtr negOne = Integer(-1).clone();
return mulExpr(negOne, args.front());
return mulExpr(negOne, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<UnaryPlus>([](ArgumentPtrVector args) {
Expand All @@ -280,29 +280,29 @@ void registerFunctionExpressionMakers() {
return LogExpression(std::move(args.front()), std::move(args.back())).clone();
});

Expression::registerFunctionExpressionMaker<Ln>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Ln>([](ArgumentPtrVector args) {
static const ArgumentPtr logBase = E{}.clone();
return logExpr(logBase, args.front());
return logExpr(logBase, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<Lb>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Lb>([](ArgumentPtrVector args) {
static const ArgumentPtr logBase = Integer(2).clone();
return logExpr(logBase, args.front());
return logExpr(logBase, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<Lg>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Lg>([](ArgumentPtrVector args) {
static const ArgumentPtr logBase = Integer(10).clone();
return logExpr(logBase, args.front());
return logExpr(logBase, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<Exp>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Exp>([](ArgumentPtrVector args) {
static const ArgumentPtr powBase = E{}.clone();
return powExpr(powBase, args.front());
return powExpr(powBase, std::move(args.front()));
});

Expression::registerFunctionExpressionMaker<Percent>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Percent>([](ArgumentPtrVector args) {
static const ArgumentPtr percentValue = Integer(100).clone();
return divExpr(args.front(), percentValue);
return divExpr(std::move(args.front()), percentValue);
});

Expression::registerFunctionExpressionMaker<Min>([](ArgumentPtrVector &&args) {
Expand All @@ -313,26 +313,26 @@ void registerFunctionExpressionMakers() {
return MinMaxExpression(Max{}, std::move(args)).clone();
});

Expression::registerFunctionExpressionMaker<Sqr>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Sqr>([](ArgumentPtrVector args) {
static const ArgumentPtr powRate = Integer(2).clone();
return powExpr(args.front(), powRate);
return powExpr(std::move(args.front()), powRate);
});

Expression::registerFunctionExpressionMaker<Sqrt>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Sqrt>([](ArgumentPtrVector args) {
static const ArgumentPtr powRate = Rational(1, 2).clone();
return powExpr(args.front(), powRate);
return powExpr(std::move(args.front()), powRate);
});

Expression::registerFunctionExpressionMaker<Root>([](ArgumentPtrVector args) {
ArgumentPtr lhs = std::move(args.front());
const ArgumentPtr rhs = std::move(args.back());
ArgumentPtr rhs = std::move(args.back());

if (const auto rhsNum = cast<INumber>(rhs); rhsNum && *rhsNum != Integer(0)) {
return powExpr(std::move(lhs), Rational(1) / (*rhsNum));
}

static const ArgumentPtr one = Integer(1).clone();
const ArgumentPtr invRhs = divExpr(one, rhs);
ArgumentPtr invRhs = divExpr(one, std::move(rhs));

return powExpr(std::move(lhs), std::move(invRhs));
});
Expand Down Expand Up @@ -433,9 +433,9 @@ void registerFunctionExpressionMakers() {
return InvHyperbExpression(Acsch{}, std::move(args.front())).clone();
});

Expression::registerFunctionExpressionMaker<Deg>([](const ArgumentPtrVector &args) {
Expression::registerFunctionExpressionMaker<Deg>([](ArgumentPtrVector args) {
static const ArgumentPtr deg1 = Deg{}(Integer(1));
return mulExpr(args.front(), deg1);
return mulExpr(std::move(args.front()), deg1);
});

Expression::registerFunctionExpressionMaker<Floor>([](ArgumentPtrVector args) {
Expand Down
19 changes: 13 additions & 6 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <ranges>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include "fintamath/core/Cache.hpp"
#include "fintamath/core/CoreUtils.hpp"
#include "fintamath/core/IMathObject.hpp"
#include "fintamath/core/MathObjectTypes.hpp"
#include "fintamath/core/Tokenizer.hpp"
#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/expressions/ExpressionParser.hpp"
#include "fintamath/expressions/ExpressionUtils.hpp"
#include "fintamath/expressions/FunctionExpression.hpp"
#include "fintamath/expressions/IExpression.hpp"
#include "fintamath/functions/FunctionArguments.hpp"
#include "fintamath/functions/FunctionUtils.hpp"
#include "fintamath/functions/IFunction.hpp"
#include "fintamath/functions/IOperator.hpp"
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
Expand Down Expand Up @@ -65,7 +73,7 @@ std::string Expression::toString() const {
}

Expression approximate(const Expression &rhs, const unsigned precision) {
Real::ScopedSetPrecision setPrecision(precision);
const Real::ScopedSetPrecision setPrecision(precision);

static Cache<unsigned, Integer> cache([](const unsigned inPrecision) {
static const Integer powBase = 10;
Expand Down Expand Up @@ -275,10 +283,10 @@ std::unique_ptr<IMathObject> Expression::operandsToObject(OperandStack &operands

if (is<IFunction>(arg)) {
auto func = cast<IFunction>(std::move(arg));
ArgumentPtr rhsChild = operandsToObject(operands);
const ArgumentPtr rhsChild = operandsToObject(operands);

if (isBinaryOperator(func.get())) {
ArgumentPtr lhsChild = operandsToObject(operands);
const ArgumentPtr lhsChild = operandsToObject(operands);
return makeExpr(*func, {lhsChild, rhsChild});
}

Expand Down Expand Up @@ -328,7 +336,7 @@ void Expression::insertMultiplications(TermVector &terms) {
}
}

void Expression::fixOperatorTypes(TermVector &terms) {
void Expression::fixOperatorTypes(const TermVector &terms) {
bool isFixed = true;

if (const auto &term = terms.front();
Expand Down Expand Up @@ -393,8 +401,7 @@ void Expression::collapseFactorials(TermVector &terms) {

if (is<Factorial>(term->value) && is<Factorial>(termNext->value)) {
const auto &oldFactorial = cast<Factorial>(*term->value);
Factorial newFactorial(oldFactorial.getOrder() + 1);
term->value = newFactorial.clone();
term->value = Factorial(oldFactorial.getOrder() + 1).clone();

terms.erase(terms.begin() + static_cast<ptrdiff_t>(i) + 1);
i--;
Expand Down
6 changes: 1 addition & 5 deletions src/fintamath/expressions/ExpressionComparator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@ Ordering reverse(Ordering ordering);
template <typename T>
size_t getPositionOfFirstChildWithTerm(const ArgumentPtrVector &children) {
for (const auto i : stdv::iota(0U, children.size())) {
bool containsTerm = containsIf(children[i], [](const ArgumentPtr &child) {
return is<T>(child);
});

if (containsTerm) {
if (containsIf(children[i], [](const ArgumentPtr &child) { return is<T>(child); })) {
return i;
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,6 @@ std::string operatorChildToString(const IOperator &oper, const ArgumentPtr &chil
}

std::string binaryOperatorToString(const IOperator &oper, const ArgumentPtr &lhs, const ArgumentPtr &rhs) {
std::string result;

std::string operStr = oper.toString();
const IOperator::Priority operPriority = oper.getPriority();

Expand Down
1 change: 0 additions & 1 deletion src/fintamath/expressions/FunctionExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "fintamath/expressions/FunctionExpression.hpp"

#include <cstddef>
#include <memory>
#include <string>
#include <utility>
Expand Down
5 changes: 3 additions & 2 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@ void IExpression::setVariables(const std::vector<std::pair<Variable, ArgumentPtr
ArgumentPtrVector newChildren;

for (const auto &child : children) {
if (std::shared_ptr exprChild = cast<IExpression>(child->clone())) {
if (is<IExpression>(child)) {
std::shared_ptr exprChild = cast<IExpression>(child->clone());
exprChild->setVariables(varsToVals);
newChildren.emplace_back(exprChild);
newChildren.emplace_back(std::move(exprChild));
continue;
}

Expand Down
3 changes: 1 addition & 2 deletions src/fintamath/expressions/binary/CompExpression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "fintamath/expressions/binary/CompExpression.hpp"

#include <functional>
#include <memory>
#include <ranges>
#include <string>
Expand Down Expand Up @@ -177,7 +176,7 @@ ArgumentPtr CompExpression::negSimplify(const IFunction &func, const ArgumentPtr

if (isNegated(lhs)) {
ArgumentPtr newLhs = negExpr(lhs);
return makeExpr(*cast<IFunction>(getOppositeFunction(func)), newLhs, rhs);
return makeExpr(*cast<IFunction>(getOppositeFunction(func)), std::move(newLhs), rhs);
}

return {};
Expand Down
Loading
Loading