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

std::views, projections #148

Merged
merged 3 commits into from
Sep 23, 2023
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
23 changes: 10 additions & 13 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,10 @@ struct hash<fintamath::MathObjectType> {
namespace fintamath {

class MathObjectBoundTypes {
static constexpr auto getTypeHash = [](const MathObjectType &type) { return size_t(type); };

using TypeMap = std::unordered_map<MathObjectType, MathObjectType>;

using enum MathObjectType::Id;

public:
static const TypeMap &get() {
return getMutable();
}

static void reg(const MathObjectType &type, const MathObjectType &boundType) {
getMutable().emplace(type, boundType);
}
using TypeMap = std::unordered_map<MathObjectType, MathObjectType>;

private:
static TypeMap &getMutable() {
static TypeMap ids{
{IMathObject, None},
Expand All @@ -223,6 +211,15 @@ class MathObjectBoundTypes {

return ids;
}

public:
static const TypeMap &get() {
return getMutable();
}

static void reg(const MathObjectType &type, const MathObjectType &boundType) {
getMutable().emplace(type, boundType);
}
};

inline bool isBaseOf(const MathObjectType &toType, const MathObjectType &fromType) {
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/expressions/IExpressionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class IExpressionCRTP_ : public IExpressionBaseCRTP<Derived, isMultiFunction> {
return false;
}

for (size_t i = 0; i < lhsChildren.size(); i++) {
for (auto i : std::views::iota(0U, lhsChildren.size())) {
if (lhsChildren[i] != rhsChildren[i] && *lhsChildren[i] != *rhsChildren[i]) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion include/fintamath/functions/IFunctionCRTP.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class IFunctionCRTP_ : public IFunction {
void throwInvalidInputFunctionException(const ArgumentRefVector &argsVect) const {
std::vector<std::string> argNamesVect(argsVect.size());

for (size_t i = 0; i < argNamesVect.size(); i++) {
for (auto i : std::views::iota(0U, argNamesVect.size())) {
argNamesVect[i] = argsVect[i].get().toString();
}

Expand Down
9 changes: 5 additions & 4 deletions include/fintamath/parser/Parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <functional>
#include <map>
#include <memory>
#include <ranges>
#include <string>
#include <vector>

Expand Down Expand Up @@ -71,7 +72,7 @@ class Parser {

const auto &valuePairs = parserMap.equal_range(parsedStr);

for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) {
for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) {
if (Return value = pair->second(args...)) {
return value;
}
Expand All @@ -88,7 +89,7 @@ class Parser {

const auto &valuePairs = parserMap.equal_range(parsedStr);

for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) {
for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) {
if (Return value = pair->second(move(args)...)) {
return value;
}
Expand All @@ -105,7 +106,7 @@ class Parser {

const auto &valuePairs = parserMap.equal_range(parsedStr);

for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) {
for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) {
if (Return value = pair->second(args...); value && comp(value)) {
return value;
}
Expand All @@ -123,7 +124,7 @@ class Parser {

const auto &valuePairs = parserMap.equal_range(parsedStr);

for (auto pair = valuePairs.first; pair != valuePairs.second; pair++) {
for (auto pair : std::views::iota(valuePairs.first, valuePairs.second)) {
if (Return value = pair->second(move(args)...); value && comp(value)) {
return value;
}
Expand Down
15 changes: 8 additions & 7 deletions src/fintamath/expressions/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ TermVector Expression::tokensToTerms(const TokenVector &tokens) {

TermVector terms(tokens.size());

for (size_t i = 0; i < tokens.size(); i++) {
for (auto i : std::views::iota(0U, terms.size())) {
if (auto term = Parser::parse(getTermMakers(), (tokens[i]))) {
terms[i] = std::move(term);
}
Expand Down Expand Up @@ -334,7 +334,7 @@ void Expression::fixOperatorTypes(TermVector &terms) {
return;
}

for (size_t i = 1; i < terms.size() - 1; i++) {
for (auto i : std::views::iota(1U, terms.size() - 1)) {
const auto &term = terms[i];
const auto &termPrev = terms[i - 1];

Expand All @@ -347,7 +347,8 @@ void Expression::fixOperatorTypes(TermVector &terms) {
}
}

for (size_t i = terms.size() - 2; i > 1; i--) {
// TODO: use reverse(iota(1, terms.size() - 1)) when it is work
for (size_t i = terms.size() - 2; i > 0; i--) {
const auto &term = terms[i];
const auto &termNext = terms[i + 1];

Expand Down Expand Up @@ -406,7 +407,7 @@ bool Expression::skipBrackets(const TermVector &terms, size_t &openBracketIndex)

int64_t brackets = 0;

for (size_t i = openBracketIndex; i < terms.size(); i++) {
for (auto i : std::views::iota(openBracketIndex, terms.size())) {
const auto &term = terms[i];

if (term->name == "(") {
Expand Down Expand Up @@ -479,8 +480,8 @@ void Expression::validateChild(const ArgumentPtr &inChild) {
validateFunctionArgs(func, children);
}
else {
for (size_t i = 0; i + 1 < children.size(); i++) {
for (size_t j = i + 1; j < children.size(); j++) {
for (auto i : std::views::iota(0U, children.size() - 1)) {
for (auto j : std::views::iota(i + 1, children.size())) {
validateFunctionArgs(func, {children[i], children[j]});
}
}
Expand All @@ -502,7 +503,7 @@ void Expression::validateFunctionArgs(const std::shared_ptr<IFunction> &func, co
childrenTypes = ArgumentTypeVector(args.size(), childrenTypes.front());
}

for (size_t i = 0; i < args.size(); i++) {
for (auto i : std::views::iota(0U, args.size())) {
const ArgumentPtr &arg = args[i];
const MathObjectType Type = childrenTypes[i];

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/ExpressionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ bool isNegated(const ArgumentPtr &arg) {
std::vector<std::string> argumentVectorToStringVector(const ArgumentPtrVector &args) {
std::vector<std::string> argStrings(args.size());

for (size_t i = 0; i < argStrings.size(); i++) {
for (auto i : std::views::iota(0U, argStrings.size())) {
argStrings[i] = args[i].get()->toString();
}

Expand Down
4 changes: 1 addition & 3 deletions src/fintamath/expressions/IExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ std::vector<Variable> IExpression::getVariables() const {
}
}

std::ranges::sort(vars, [](const Variable &lhs, const Variable &rhs) {
return lhs.toString() < rhs.toString();
});
std::ranges::sort(vars, std::less(), &Variable::toString);
auto unique = std::ranges::unique(vars);
vars.erase(unique.begin(), unique.end());

Expand Down
16 changes: 10 additions & 6 deletions src/fintamath/expressions/binary/DivExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,20 @@ ArgumentPtr DivExpression::mulSimplify(const IFunction &func, const ArgumentPtr
size_t lhsChildrenSizeInitial = lhsChildren.size();
size_t rhsChildrenSizeInitial = rhsChildren.size();

for (auto &lhsChild : lhsChildren) {
for (size_t j = 0; j < rhsChildren.size(); j++) {
ArgumentPtr res = constSimplify(func, lhsChild, rhsChildren[j]);
// TODO: use more efficient algorithm
for (auto i : std::views::iota(0U, lhsChildren.size())) {
auto &lhsChild = lhsChildren[i];

for (auto j : std::views::iota(0U, rhsChildren.size())) {
const auto &rhsChild = rhsChildren[j];
ArgumentPtr res = constSimplify(func, lhsChild, rhsChild);

if (!res) {
res = callFunction(Div(), {lhsChild, rhsChildren[j]});
res = callFunction(Div(), {lhsChild, rhsChild});
}

if (!res) {
res = powSimplify(lhsChild, rhsChildren[j]);
res = powSimplify(lhsChild, rhsChild);
}

if (res && !is<Rational>(res)) {
Expand Down Expand Up @@ -360,7 +364,7 @@ std::pair<ArgumentPtr, ArgumentPtr> DivExpression::mulSumSimplify(const Argument

ArgumentPtrVector multiplicator;

for (size_t i = 1; i < rhsChildren.size(); i++) {
for (auto i : std::views::iota(1U, rhsChildren.size())) {
multiplicator.emplace_back(mulExpr(rhsChildren[i], result));
}

Expand Down
20 changes: 11 additions & 9 deletions src/fintamath/expressions/binary/PowExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ PowExpression::SimplifyFunctionVector PowExpression::getFunctionsForPostSimplify
Integer PowExpression::generateFirstNum(const Integer &countOfOne) {
Integer n = 0;

for (int i = 0; i < countOfOne; i++) {
n = n << 1 | 1;
for ([[maybe_unused]] auto _ : std::views::iota(0U, countOfOne)) {
n <<= 1;
n |= 1;
}

return n;
Expand Down Expand Up @@ -147,20 +148,21 @@ ArgumentPtr PowExpression::sumPolynomSimplify(const ArgumentPtr &expr, const Int
return {};
}

ArgumentPtrVector newPolynom;
Integer variableCount = int64_t(polynom.size());

size_t variableCount = polynom.size();
Integer bitNumber = generateFirstNum(powValue);
Integer combins = combinations(powValue + variableCount - 1, powValue);

ArgumentPtrVector newPolynom;

for (int i = 0; i < combinations(powValue + variableCount - 1, powValue); i++) {
std::vector<Integer> vectOfPows = getPartition(bitNumber, variableCount);
for ([[maybe_unused]] auto _ : std::views::iota(0U, combins)) {
std::vector<Integer> vectOfPows = getPartition(bitNumber, Integer(variableCount));
bitNumber = generateNextNumber(bitNumber);

ArgumentPtrVector mulExprPolynom;
mulExprPolynom.emplace_back(multinomialCoefficient(powValue, vectOfPows).clone());

for (size_t j = 0; j < size_t(variableCount); j++) {
ArgumentPtr powExprChild = powExpr(polynom[j], vectOfPows[j].clone());
for (auto i : std::views::iota(0U, variableCount)) {
ArgumentPtr powExprChild = powExpr(polynom[i], vectOfPows[i].clone());
mulExprPolynom.emplace_back(powExprChild);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ ArgumentPtrVector getVariableIntPowerRates(const ArgumentPtr &elem, const Variab
std::shared_ptr<const INumber> power = getElementPower(polynomChild, var);

if (auto intPow = cast<Integer>(power)) {
if (int64_t(powerRates.size()) < *intPow + 1) {
while (int64_t(powerRates.size()) != *intPow + 1) {
if (powerRates.size() < *intPow + 1) {
while (powerRates.size() != *intPow + 1) {
powerRates.emplace_back(Integer(0).clone());
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/fintamath/expressions/interfaces/IPolynomExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ std::string IPolynomExpression::toString() const {

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

for (size_t i = 1; i < children.size(); i++) {
for (auto i : std::views::iota(1U, children.size())) {
const std::string childStr = childToString(*oper, children[i], children[i - 1]);

if (childStr.length() > 2 && childStr[0] == ' ' && std::isdigit(childStr[1]) && std::isdigit(result.back())) {
Expand Down Expand Up @@ -91,6 +91,7 @@ void IPolynomExpression::simplifyRec(bool isPostSimplify) {

bool isSimplified = true;

// TODO: refactor this loop
for (size_t i = 1; i < children.size(); i++) {
const ArgumentPtr &lhs = children[i - 1];
const ArgumentPtr &rhs = children[i];
Expand Down Expand Up @@ -447,7 +448,9 @@ IPolynomExpression::comparatorChildren(const ArgumentPtrVector &lhsChildren,
result.postfixUnary = 0;
}

for (size_t i = 0; i < std::min(std::max(lhsStart, rhsStart), std::min(lhsChildren.size(), rhsChildren.size())); i++) {
for (auto end = std::min(std::max(lhsStart, rhsStart), std::min(lhsChildren.size(), rhsChildren.size()));
auto i : std::views::iota(0U, end)) {

int childrenComp = comparator(lhsChildren[i], rhsChildren[i]);

if (childrenComp != 0) {
Expand Down Expand Up @@ -538,6 +541,7 @@ std::shared_ptr<const Variable> IPolynomExpression::getNextVariable(ExpressionTr
while (!stack.empty()) {
ArgumentPtrVector children = stack.top().first->getChildren();

// TODO: looks weird
size_t &exprIndex = stack.top().second;
exprIndex++;

Expand Down Expand Up @@ -566,11 +570,12 @@ std::shared_ptr<const Variable> IPolynomExpression::getNextVariable(ExpressionTr
}

size_t IPolynomExpression::getPositionOfFirstChildWithVariable(const ArgumentPtrVector &children) {
for (size_t i = 0; i < children.size(); i++) {
for (auto i : std::views::iota(0U, children.size())) {
auto lhsChildExpr = cast<IExpression>(children[i]);

if (is<Variable>(children[i]) ||
(lhsChildExpr && hasVariable(lhsChildExpr))) {

return i;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/expressions/polynomial/AddExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ ArgumentPtr AddExpression::addRatesToValue(const ArgumentPtrVector &rates, const
std::vector<size_t> AddExpression::findLogarithms(const ArgumentPtrVector &children) {
std::vector<size_t> indexes;

for (size_t i = 0; i < children.size(); i++) {
for (auto i : std::views::iota(0U, children.size())) {
if (const auto childExpr = cast<const IExpression>(children[i]);
childExpr && is<Log>(childExpr->getFunction())) {

Expand Down
20 changes: 14 additions & 6 deletions src/fintamath/expressions/polynomial/OrExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ ArgumentPtr OrExpression::postSimplify() const {
auto simplChildren = simpl->children;
auto simplChildrenSizeInitial = simplChildren.size();

// TODO: use more efficient algorithm
for (size_t i = 0; i + 1 < simplChildren.size(); i++) {
for (size_t j = i + 1; j < simplChildren.size(); j++) {
if (auto res = absorptionSimplify(simplChildren[i], simplChildren[j])) {
simplChildren[i] = res;
simplChildren.erase(simplChildren.begin() + ptrdiff_t(j));
break;
j--;
}
}
}
Expand Down Expand Up @@ -131,7 +132,7 @@ ArgumentPtr OrExpression::andSimplify(const IFunction & /*func*/, const Argument

size_t resolutionIndex = lhsChildren.size();

for (size_t i = 0; i < lhsChildren.size(); i++) {
for (auto i : std::views::iota(0U, lhsChildren.size())) {
ArgumentPtr lhsSubChild = lhsChildren[i];
ArgumentPtr rhsSubChild = rhsChildren[i];

Expand Down Expand Up @@ -213,10 +214,17 @@ ArgumentPtr OrExpression::absorptionSimplify(const ArgumentPtr &lhsChild, const
ArgumentPtrVector minChildren = lhsChildren.size() < rhsChildren.size() ? lhsChildren : rhsChildren;
size_t matchCount = 0;

for (size_t i = 0, j = 0; i < maxChildren.size() && j < minChildren.size(); i++) {
if (*maxChildren[i] == *minChildren[j]) {
matchCount++;
j++;
{
size_t i = 0;
size_t j = 0;

while (i < maxChildren.size() && j < minChildren.size()) {
if (*maxChildren[i] == *minChildren[j]) {
matchCount++;
j++;
}

i++;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/calculus/Max.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace fintamath {
std::unique_ptr<IMathObject> Max::call(const ArgumentRefVector &argsVect) const {
std::reference_wrapper<const IComparable> res = cast<IComparable>(argsVect.front().get());

for (size_t i = 1; i < argsVect.size(); i++) {
for (auto i : std::views::iota(1U, argsVect.size())) {
std::reference_wrapper<const IComparable> arg = cast<IComparable>(argsVect[i].get());

if (is<Complex>(arg)) {
Expand Down
2 changes: 1 addition & 1 deletion src/fintamath/functions/calculus/Min.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace fintamath {
std::unique_ptr<IMathObject> Min::call(const ArgumentRefVector &argsVect) const {
std::reference_wrapper<const IComparable> res = cast<IComparable>(argsVect.front().get());

for (size_t i = 1; i < argsVect.size(); i++) {
for (auto i : std::views::iota(1U, argsVect.size())) {
std::reference_wrapper<const IComparable> arg = cast<IComparable>(argsVect[i].get());

if (is<Complex>(arg)) {
Expand Down
Loading