diff --git a/omnn/math/Exponentiation.cpp b/omnn/math/Exponentiation.cpp index d36573955..b0701c160 100644 --- a/omnn/math/Exponentiation.cpp +++ b/omnn/math/Exponentiation.cpp @@ -49,7 +49,7 @@ using namespace omnn::math; max_exp_t Exponentiation::getMaxVaExp() const { - return getMaxVaExp(getBase(), getExponentiation()); + return getMaxVaExp(ebase(), eexp()); } Valuable Exponentiation::varless() const @@ -203,7 +203,7 @@ using namespace omnn::math; if (_.IsExponentiation()) { auto& e = _.as(); if (!(e.ebase()==ebase() && eexp()==e.eexp())) { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::operator== not implemented for different base/exponent") } } else { Become(std::move(_)); @@ -301,14 +301,14 @@ using namespace omnn::math; Become(std::move(ebase())); return; } else - IMPLEMENT; + LOG_AND_IMPLEMENT("Exponentiation::optimize not implemented for this case"); } else if (ebase() == -1 && eexp().IsInt() && eexp() > 0 && eexp() != 1) { eexp() = eexp().bit(0); } else if (eexp()==-1) { Become(Fraction{1,ebase()}); return; } else if (eexp().IsInfinity()) { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::optimize not implemented for infinity exponent"); } else if (eexp().IsFraction()) { auto& f = eexp().as(); auto& n = f.getNumerator(); @@ -433,7 +433,7 @@ using namespace omnn::math; { bool isInt = n.IsInt(); if (!isInt) - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::optimize not implemented for non-integer n") if (isInt && n.bit().IsZero()) { x.sq(); @@ -489,11 +489,11 @@ using namespace omnn::math; if(IsExponentiation() && ebase().IsExponentiation()) { auto& e = ebase().as(); - auto& eeexp = e.getExponentiation(); + auto& eeexp = e.eexp(); if ((eeexp.FindVa() == nullptr) == (eexp().FindVa() == nullptr)) { eexp() *= eeexp; // todo : copy if it shared - ebase() = std::move(const_cast((e.getBase()))); + ebase() = std::move(const_cast((e.ebase()))); } if (eexp() == constants::one || IsEquation()) { @@ -525,7 +525,7 @@ using namespace omnn::math; for(auto& it : *p){ if (it.IsExponentiation()) { auto& e = it.as(); - if (ebase() == e.getBase()) { + if (ebase() == e.ebase()) { return &e; } } @@ -534,12 +534,12 @@ using namespace omnn::math; }; auto& b = ebase(); if (v.IsExponentiation() - && b == (e = &v.as())->getBase() + && b == (e = &v.as())->ebase() && (eexp().IsInt() || eexp().IsSimpleFraction()) && (e->eexp().IsInt() || e->eexp().IsSimpleFraction()) ) { - updateExponentiation(eexp() + e->getExponentiation()); + updateExponentiation(eexp() + e->eexp()); } else if(v.IsFraction() && (f = &v.as())->getDenominator() == ebase()) @@ -561,7 +561,7 @@ using namespace omnn::math; else if(fdn && (fdne = isProdHasExpWithSameBase(fdn))) { - eexp() -= fdne->getExponentiation(); + eexp() -= fdne->eexp(); optimized={}; optimize(); return *this *= f->getNumerator() / (*fdn / *fdne); @@ -609,7 +609,7 @@ using namespace omnn::math; bool Exponentiation::MultiplyIfSimplifiable(const Valuable& v) { - auto is = v == getBase(); + auto is = v == ebase(); if (is) { ++eexp(); optimized = {}; @@ -631,17 +631,17 @@ using namespace omnn::math; Become(Valuable(std::move(values))); } else if (v.IsExponentiation()) { auto& vexpo = v.as(); - is = vexpo.getBase() == getBase(); + is = vexpo.ebase() == ebase(); if (is) { eexp() += vexpo.eexp(); optimized = {}; optimize(); } // TODO : else if ? (base^2 == v.base) else { - is = vexpo.getExponentiation() == getExponentiation(); + is = vexpo.eexp() == eexp(); if (is) { auto wasBaseHash = ebase().Hash(); - is = ebase().MultiplyIfSimplifiable(vexpo.getBase()); + is = ebase().MultiplyIfSimplifiable(vexpo.ebase()); if (is) { Valuable::hash ^= wasBaseHash ^ ebase().Hash(); optimized = {}; @@ -658,23 +658,23 @@ using namespace omnn::math; std::pair Exponentiation::IsMultiplicationSimplifiable(const Valuable& v) const { std::pair is, expSumSimplifiable = {}; - is.first = v == getBase() + is.first = v == ebase() && (expSumSimplifiable = eexp().IsSummationSimplifiable(constants::one)).first; if (is.first) { - is.second = getBase() ^ expSumSimplifiable.second; + is.second = ebase() ^ expSumSimplifiable.second; } else if (v.IsExponentiation()) { auto& vexpo = v.as(); - is.first = vexpo.getBase() == getBase(); + is.first = vexpo.ebase() == ebase(); if (is.first) { is.second = ebase() ^ (eexp() + vexpo.eexp()); } // TODO : else if ? (base^2 == v.base) } else if (v.IsSimple()) { - auto& ee = getExponentiation(); + auto& ee = eexp(); //is = v.IsExponentiationSimplifiable(ee); // TODO: Implement IsExponentiationSimplifiable // FIXME: Until IsExponentiationSimplifiable ready: //if (ee.IsSimpleFraction()) { // is.second = v ^ ee.as().Reciprocal(); // v.IsExponentiationSimplifiable(ee) - // is.first = is.second.MultiplyIfSimplifiable(getBase()); + // is.first = is.second.MultiplyIfSimplifiable(ebase()); // if(is.first){ // auto copy = *this; // copy.updateBase(std::move(is.second)); @@ -689,7 +689,7 @@ using namespace omnn::math; is.second = (v/gcd)/(ebase()/gcd); } else { is.second = v ^ constants::minus_1; // v.IsExponentiationSimplifiable(ee) - is.first = is.second.MultiplyIfSimplifiable(getBase()); + is.first = is.second.MultiplyIfSimplifiable(ebase()); if (is.first) { auto copy = *this; copy.updateBase(std::move(is.second)); @@ -700,18 +700,18 @@ using namespace omnn::math; else if (ee.IsInt() && (ee > constants::zero)) // TODO: ee < 0 too { // is.second = v ^ (ee ^ constants::minus_1); // v.IsExponentiationSimplifiable(ee) -// is.first = is.second.MultiplyIfSimplifiable(getBase()); +// is.first = is.second.MultiplyIfSimplifiable(ebase()); // if(is.first){ // auto copy = *this; // copy.updateBase(std::move(is.second)); // is.second = copy; // } } -// if (getBase().IsVa()) { -// } else if (getExponentiation().IsSimpleFraction()) { +// if (ebase().IsVa()) { +// } else if (eexp().IsSimpleFraction()) { // auto // is.first = IsMultiplicationSimplifiable() -// } else if (getExponentiation().IsSimple()) { +// } else if (eexp().IsSimple()) { // is = base::IsMultiplicationSimplifiable(v); // } else { // IMPLEMENT @@ -722,7 +722,7 @@ using namespace omnn::math; is=v.IsMultiplicationSimplifiable(*this); } else if (v.IsSum()) { auto& sum=v.as(); - is.first=sum==getBase()||-sum==getBase(); + is.first=sum==ebase()||-sum==ebase(); if(is.first){ is.second=*this*sum; }else{ @@ -732,11 +732,11 @@ using namespace omnn::math; is = {true, v}; } else if (v.IsPrincipalSurd()) { auto& surd = v.as(); - if (surd.Radicand() == getBase() && surd.Index().IsEven() == YesNoMaybe::No) { - is = surd.Index().Reciprocal().IsSummationSimplifiable(getExponentiation()); + if (surd.Radicand() == ebase() && surd.Index().IsEven() == YesNoMaybe::No) { + is = surd.Index().Reciprocal().IsSummationSimplifiable(eexp()); if (is.first) { is.second = std::static_pointer_cast( - std::make_shared(getBase(), std::move(is.second))); + std::make_shared(ebase(), std::move(is.second))); } } } else { @@ -914,7 +914,7 @@ using namespace omnn::math; Become(std::move(p.integral(x, C))); } } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::integral not implemented for non-x base") } return *this; @@ -925,10 +925,10 @@ using namespace omnn::math; if (v.IsExponentiation()) { auto& e = v.as(); - if (e.getBase() == getBase()) - return getExponentiation() < e.getExponentiation(); - if (e.getExponentiation() == getExponentiation()) - return getBase() < e.getBase(); + if (e.ebase() == ebase()) + return eexp() < e.eexp(); + if (e.eexp() == eexp()) + return ebase() < e.ebase(); } return base::operator <(v); @@ -988,7 +988,7 @@ using namespace omnn::math; if(value.IsMultival()==YesNoMaybe::No) vals.insert(value); else { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::Values not implemented for multival case") } } } @@ -1003,46 +1003,46 @@ using namespace omnn::math; std::ostream& Exponentiation::code(std::ostream& out) const { - if (getExponentiation().IsInt()) { + if (eexp().IsInt()) { out << "(1"; - for (auto i = getExponentiation(); i-- > 0;) { + for (auto i = eexp(); i-- > 0;) { out << '*'; - getBase().code(out); + ebase().code(out); } out << ')'; } else { out << "pow("; - getBase().code(out) << ','; - getExponentiation().code(out) << ')'; + ebase().code(out) << ','; + eexp().code(out) << ')'; } return out; } bool Exponentiation::IsComesBefore(const Exponentiation& e) const { bool is = {}; - bool baseIsVa = getBase().IsVa(); - bool vbaseIsVa = e.getBase().IsVa(); + bool baseIsVa = ebase().IsVa(); + bool vbaseIsVa = e.ebase().IsVa(); if (baseIsVa && vbaseIsVa) - is = getExponentiation() == e.getExponentiation() ? getBase().IsComesBefore(e.getBase()) - : getExponentiation() > e.getExponentiation(); + is = eexp() == e.eexp() ? ebase().IsComesBefore(e.ebase()) + : eexp() > e.eexp(); else if (baseIsVa) is = false; else if (vbaseIsVa) is = true; - else if (getBase() == e.ebase()) - is = getExponentiation().IsComesBefore(e.getExponentiation()); - else if (getExponentiation() == e.getExponentiation()) - is = getBase().IsComesBefore(e.getBase()); + else if (ebase() == e.ebase()) + is = eexp().IsComesBefore(e.eexp()); + else if (eexp() == e.eexp()) + is = ebase().IsComesBefore(e.ebase()); else { auto c = Complexity(); auto ec = e.Complexity(); if (c != ec) is = c > ec; else { - is = getBase().IsComesBefore(e.getBase()) || + is = ebase().IsComesBefore(e.ebase()) || (!e.ebase().IsComesBefore(ebase()) && - getExponentiation().IsComesBefore( - e.getExponentiation())); // || str().length() > e->str().length(); + eexp().IsComesBefore( + e.eexp())); // || str().length() > e->str().length(); // auto expComesBefore = eexp().IsComesBefore(e->eexp()); // auto ebase()ComesBefore = ebase().IsComesBefore(e->ebase()); // is = expComesBefore==ebase()ComesBefore || str().length() > e->str().length(); @@ -1079,7 +1079,7 @@ using namespace omnn::math; else if(value.IsSum()) is = IsComesBefore(*value.as().begin()); else - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::IsComesBefore not implemented for non-sum value") return is; } @@ -1087,12 +1087,12 @@ using namespace omnn::math; Valuable Exponentiation::calcFreeMember() const { Valuable c; - if(getBase().IsSum() && getExponentiation().IsInt()){ - c = getBase().calcFreeMember() ^ getExponentiation(); - } else if(getBase().IsVa()) { + if(ebase().IsSum() && eexp().IsInt()){ + c = ebase().calcFreeMember() ^ eexp(); + } else if(ebase().IsVa()) { c = 0_v; } else - IMPLEMENT; + LOG_AND_IMPLEMENT("Exponentiation::calcFreeMember not implemented for this case"); return c; } @@ -1142,8 +1142,8 @@ using namespace omnn::math; Valuable::vars_cont_t Exponentiation::GetVaExps() const { - auto vaExps = getBase().GetVaExps(); - auto& e = getExponentiation(); + auto vaExps = ebase().GetVaExps(); + auto& e = eexp(); for (auto& ve : vaExps) { ve.second *= e; } @@ -1162,63 +1162,63 @@ using namespace omnn::math; } } else if (v.IsExponentiation()) { auto& e = v.as(); - if (e.getBase() == getBase()) { - if (e.getExponentiation() == getExponentiation()) { + if (e.ebase() == ebase()) { + if (e.eexp() == eexp()) { c = e; - } else if (getExponentiation().IsSimple() && e.getExponentiation().IsSimple()) { - if (getExponentiation() > 0 || e.getExponentiation() > 0) { - if (e.getExponentiation() >= getExponentiation()) { + } else if (eexp().IsSimple() && e.eexp().IsSimple()) { + if (eexp() > 0 || e.eexp() > 0) { + if (e.eexp() >= eexp()) { c = *this; } else c = e; - } else if (getExponentiation() < 0 || e.getExponentiation() < 0) { - if (e.getExponentiation() >= getExponentiation()) { + } else if (eexp() < 0 || e.eexp() < 0) { + if (e.eexp() >= eexp()) { c = e; } else c = *this; } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::InCommonWith not implemented for this case"); } - } else if (getExponentiation().IsSimpleFraction() && e.getExponentiation().IsSimpleFraction()) { - if (getExponentiation()<0 == e.getExponentiation()<0) { - c = getBase() ^ getExponentiation().InCommonWith(e.getExponentiation()); + } else if (eexp().IsSimpleFraction() && e.eexp().IsSimpleFraction()) { + if (eexp()<0 == e.eexp()<0) { + c = ebase() ^ eexp().InCommonWith(e.eexp()); } - } else if (getExponentiation().IsSum()) { - auto sz = getExponentiation().as().size(); - auto diff = getExponentiation() - e.getExponentiation(); + } else if (eexp().IsSum()) { + auto sz = eexp().as().size(); + auto diff = eexp() - e.eexp(); if (!diff.IsSum() || diff.as().size() < sz) c = v; - } else if (e.getExponentiation().IsSum()) { + } else if (e.eexp().IsSum()) { c = e.InCommonWith(*this); - } else if (e.getExponentiation().IsProduct()) { + } else if (e.eexp().IsProduct()) { c = ebase() ^ e.eexp().InCommonWith(eexp()); - } else if (getExponentiation().IsPrincipalSurd()) { - auto& surd = getExponentiation().as(); - auto isSumSimpl = e.getExponentiation().IsSummationSimplifiable(-surd); + } else if (eexp().IsPrincipalSurd()) { + auto& surd = eexp().as(); + auto isSumSimpl = e.eexp().IsSummationSimplifiable(-surd); if (isSumSimpl.first) { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::InCommonWith not implemented for sum simplifiable case"); } else { - c = surd.InCommonWith(e.getExponentiation()); + c = surd.InCommonWith(e.eexp()); if (c != constants::one) { - c = Exponentiation{getBase(), std::move(c)}; // NOTE: this is not strictly common form (not GCD) + c = Exponentiation{ebase(), std::move(c)}; // NOTE: this is not strictly common form (not GCD) } } - } else if (e.getExponentiation().IsPrincipalSurd()) { + } else if (e.eexp().IsPrincipalSurd()) { c = e.InCommonWith(*this); } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::InCommonWith not implemented for this exponentiation case"); } } - } else if (getExponentiation().IsInt()) { - if(getExponentiation() > 0) - c = getBase().InCommonWith(v); - } else if (getExponentiation().IsFraction()) { + } else if (eexp().IsInt()) { + if(eexp() > 0) + c = ebase().InCommonWith(v); + } else if (eexp().IsFraction()) { } else if (v.IsVa()) { c = v.InCommonWith(*this); } else if (v.IsInt() || v.IsSimpleFraction()) { } else if (v.IsModulo()) { - } else if (getExponentiation().IsVa()) { - } else if (getExponentiation().IsModulo()) { + } else if (eexp().IsVa()) { + } else if (eexp().IsModulo()) { } else if (v.IsPrincipalSurd()) { auto commonWithBase = v.InCommonWith(ebase()); if (commonWithBase != constants::one) { @@ -1237,13 +1237,13 @@ using namespace omnn::math; Valuable Exponentiation::operator()(const Variable& v, const Valuable& augmentation) const { - if (!getExponentiation().FindVa() && getExponentiation()!=0 && augmentation==0) { - return getBase()(v,augmentation); - } else if (getExponentiation().IsSimpleFraction()) { - auto& f = getExponentiation().as(); - return (getBase()^f.getNumerator())(v,augmentation^f.getDenominator()); + if (!eexp().FindVa() && eexp()!=0 && augmentation==0) { + return ebase()(v,augmentation); + } else if (eexp().IsSimpleFraction()) { + auto& f = eexp().as(); + return (ebase()^f.getNumerator())(v,augmentation^f.getDenominator()); } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation::operator() not implemented for this case"); } } diff --git a/omnn/math/Formula.cpp b/omnn/math/Formula.cpp index 56d5e57d7..f919a6098 100644 --- a/omnn/math/Formula.cpp +++ b/omnn/math/Formula.cpp @@ -20,7 +20,7 @@ namespace math { for(auto& v : vars) s.push_back(v); } - + Formula::Formula(const Variable& va, const Valuable& ex, const std::list* sequence) : v(va), e(ex) { @@ -30,13 +30,13 @@ namespace math { CollectVarSequence(); } } - + Formula Formula::DeduceFormula(const Valuable& e, const Variable& v) { //todo : once iterator ready throw "Implement!"; } - + Formula Formula::DeclareFormula(const Variable& v, const Valuable& e) { return Formula(v,e); @@ -49,21 +49,27 @@ namespace math { bool is = {}; if (_.IsExponentiation()) { auto& e = _.as(); - bool isSum = e.getBase().IsSum(); - Valuable sum = Sum{e.getBase(), constants::zero}; - auto& s = (isSum ? e.getBase() : sum).as(); + bool isSum = e.ebase().IsSum(); + Valuable sum = Sum{e.ebase(), constants::zero}; + auto& s = (isSum ? e.ebase() : sum).as(); if (s) { if (s.size() > 2) { - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for sum size > 2"); } - auto va = s.template GetFirstOccurence(); + auto va = s.GetFirstOccurence(); if (va != s.end()) { - auto it = vaVals.find(va->as()); + // Check if we have a Variable in the sum + if (!va->IsVa()) { + LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for non-variable value"); + return false; + } + auto& var = va->as(); + auto it = vaVals.find(var); auto isValue = it == vaVals.end(); is = isValue; auto isCoord = !isValue; - + if (va == s.begin()) ++va; else --va; @@ -77,16 +83,16 @@ namespace math { } } else - IMPLEMENT - } + LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for non-variable sum"); + } else - IMPLEMENT - } + LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for non-sum"); + } else - IMPLEMENT; + LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for non-exponentiation"); return is; } - + bool Formula::InCoordFactorization(const VaValMap& vaVals) const { bool is = {}; @@ -99,7 +105,7 @@ namespace math { is = std::all_of(std::begin(vaVals), std::end(vaVals), [](auto& _){ return *_.second >= 0; }); - + if(is) { // check right gauge @@ -108,7 +114,7 @@ namespace math { ++it; auto& l = *it; if (!l.IsSum()) { - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::InCoordFactorization not implemented for non-sum"); } auto& s = l.as(); is = std::all_of(std::begin(s), std::end(s), @@ -122,7 +128,7 @@ namespace math { } return is; } - + Valuable Formula::GetProductRootByCoordinates(const VaValMap& vaVals) const { Valuable root; @@ -147,7 +153,7 @@ namespace math { ); if(is) { if (!value) - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::GetProductRootByCoordinates value not found"); root = -*value; return root; // found } @@ -155,22 +161,22 @@ namespace math { else if (m==1) continue; else - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::GetProductRootByCoordinates not implemented for non-sum/non-one"); } - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::GetProductRootByCoordinates root not found"); } - + std::ostream& Formula::print(std::ostream& out) const { return out << "f(" << v << ")=" << e; } - + Valuable Formula::Solve(Valuable& v) const { - IMPLEMENT + LOG_AND_IMPLEMENT("Formula::Solve not implemented"); v.optimize(); return v; } - + }} diff --git a/omnn/math/Fraction.cpp b/omnn/math/Fraction.cpp index eb96d72cb..3156e5873 100644 --- a/omnn/math/Fraction.cpp +++ b/omnn/math/Fraction.cpp @@ -206,7 +206,7 @@ using namespace omnn::math; } while (denominator().IsFraction()) { - auto& fdn = denominator().as(); + auto& fdn = denominator().template as(); numerator() *= fdn.denominator(); denominator() = std::move(fdn.numerator()); } @@ -244,14 +244,14 @@ using namespace omnn::math; } if (numerator().IsExponentiation()) { - auto& e = numerator().as(); - auto& exp = e.getExponentiation(); + auto& e = numerator().template as(); + auto& exp = e.eexp(); if (exp.IsInt() && exp < 0) { - denominator() *= e.getBase() ^ (-exp); + denominator() *= e.ebase() ^ (-exp); numerator() = constants::one; } else if (exp.IsFraction()) { - auto& f = exp.as(); - auto in = e.getBase() / (denominator() ^ f.Reciprocal()); + auto& f = exp.template as(); + auto in = e.ebase() / (denominator() ^ f.Reciprocal()); if (in.IsInt()) { e.setBase(std::move(in)); Become(std::move(e)); @@ -298,7 +298,7 @@ using namespace omnn::math; } } else if (denom.IsSimple()) { if (denom.IsPrincipalSurd()) { - auto& ps = denom.as(); + auto& ps = denom.template as(); auto& e = ps.Index(); numerator() *= ps ^ (e + constants::minus_1); setDenominator(std::move(ps.Radicand())); @@ -312,7 +312,7 @@ using namespace omnn::math; } if (denominator().IsProduct()) { - auto& dn = denominator().as(); + auto& dn = denominator().template as(); if (dn.Has(numerator())) { denominator() /= numerator(); numerator() = constants::one; @@ -334,8 +334,8 @@ using namespace omnn::math; if (m.IsVa()) { numerator() *= m ^ -1; } else if (m.IsExponentiation()) { - auto& e = m.as(); - numerator() *= e.getBase() ^ -e.getExponentiation(); + auto& e = m.template as(); + numerator() *= e.ebase() ^ -e.eexp(); } else numerator() /= m; } @@ -344,7 +344,7 @@ using namespace omnn::math; } } } else if (denominator().IsSum()) { - auto& s = denominator().as(); + auto& s = denominator().template as(); auto lcm = s.LCMofMemberFractionDenominators(); if (lcm != constants::one) { numerator() *= lcm; @@ -352,7 +352,7 @@ using namespace omnn::math; reoptimize_the_fraction = true; continue; } else if (denominator().IsSum()) { - auto& s = denominator().as(); + auto& s = denominator().template as(); auto lcm = s.LCMofMemberFractionDenominators(); if (lcm != constants::one) { numerator() *= lcm; @@ -367,7 +367,7 @@ using namespace omnn::math; } else // no products { // TODO : - // IMPLEMENT // uncomment to cover scenarios + // sum // auto s = Sum::cast(numerator()); diff --git a/omnn/math/Logarithm.cpp b/omnn/math/Logarithm.cpp index 3f8362153..5d847322d 100644 --- a/omnn/math/Logarithm.cpp +++ b/omnn/math/Logarithm.cpp @@ -64,24 +64,24 @@ void Logarithm::optimize() { // Simplify logarithm if base and target are the same if (_1 == _2) { Become(1); - } else if (_2.IsExponentiation() && _2.as().getBase() == _1) { // Simplify logarithm if the target is a power of the base + } else if (_2.IsExponentiation() && _2.as().ebase() == _1) { // Simplify logarithm if the target is a power of the base Become(std::move(_2.as().eexp())); - } else if (_1.IsInt() && _2.IsInt() && getTarget() > constants::zero && getBase() > constants::one) { - auto base = getBase().ca(); + } else if (_1.IsInt() && _2.IsInt() && getTarget() > constants::zero && lbase() > constants::one) { + auto base = lbase().ca(); auto target = getTarget().ca(); // Use binary search for initial approximation auto high = target; decltype(high) low = 0; while (low < high) { decltype(high) mid = (low + high) / 2; - if ((getBase() ^ mid) <= getTarget()) { + if ((lbase() ^ mid) <= getTarget()) { low = mid + 1; } else { high = mid; } } decltype(high) x = low - 1; - if ((getBase() ^ x) == getTarget()) { + if ((lbase() ^ x) == getTarget()) { Become(std::move(x)); } } else if (lbase().IsPrincipalSurd()) { @@ -106,7 +106,7 @@ void Logarithm::optimize() { Valuable& Logarithm::operator+=(const Valuable& v) { // Implement addition of logarithms - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, add the targets _2 += v.as().getTarget(); optimize(); @@ -119,7 +119,7 @@ Valuable& Logarithm::operator+=(const Valuable& v) { Valuable& Logarithm::operator*=(const Valuable& v) { // Implement multiplication of logarithms - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, multiply the targets _2 *= v.as().getTarget(); optimize(); @@ -134,7 +134,7 @@ Valuable& Logarithm::operator*=(const Valuable& v) { bool Logarithm::MultiplyIfSimplifiable(const Valuable& v) { // Check if multiplication can be simplified - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, multiply the targets and simplify _2 *= v.as().getTarget(); optimize(); @@ -145,7 +145,7 @@ bool Logarithm::MultiplyIfSimplifiable(const Valuable& v) { std::pair Logarithm::IsMultiplicationSimplifiable(const Valuable& v) const { // Check if multiplication can be simplified and return the result - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, return true and the multiplied targets Valuable newTarget = _2 * v.as().getTarget(); return {true, Logarithm(_1, newTarget)}; @@ -155,7 +155,7 @@ std::pair Logarithm::IsMultiplicationSimplifiable(const Valuable bool Logarithm::SumIfSimplifiable(const Valuable& v) { // Check if summation can be simplified - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, add the targets and simplify _2 += v.as().getTarget(); optimize(); @@ -166,7 +166,7 @@ bool Logarithm::SumIfSimplifiable(const Valuable& v) { std::pair Logarithm::IsSummationSimplifiable(const Valuable& v) const { // Check if summation can be simplified and return the result - if (v.IsLogarithm() && v.as().getBase() == _1) { + if (v.IsLogarithm() && v.as().lbase() == _1) { // If bases are the same, return true and the added targets Valuable newTarget = _2 + v.as().getTarget(); return {true, Logarithm(_1, newTarget)}; @@ -176,7 +176,9 @@ std::pair Logarithm::IsSummationSimplifiable(const Valuable& v) Valuable& Logarithm::operator/=(const Valuable& value) { return Become(Fraction{*this, value}); } -Valuable& Logarithm::operator^=(const Valuable& value) { return Become(Exponentiation{*this, value}); } + Valuable& Logarithm::operator^=(const Valuable& value) { + return Become(Exponentiation{*this, value}); + } Logarithm::operator double() const { return std::log(static_cast(_2)) / std::log(static_cast(_1)); @@ -212,7 +214,7 @@ Valuable& Logarithm::integral(const Variable& x, const Variable& C) { Valuable::vars_cont_t Logarithm::GetVaExps() const { - Valuable::vars_cont_t exponentiations; + Valuable::vars_cont_t exponentiations; for (auto& variable : Vars()) { exponentiations.emplace(std::move(variable), NaN()); } diff --git a/omnn/math/Logarithm.h b/omnn/math/Logarithm.h index 72e705b44..a2590ee6b 100644 --- a/omnn/math/Logarithm.h +++ b/omnn/math/Logarithm.h @@ -45,7 +45,6 @@ class Logarithm constexpr bool IsLogarithm() const override { return true; } void optimize() override; - const Valuable& getBase() const { return _1; } const Valuable& lbase() const { return _1; } template void setBase(T&& b) { diff --git a/omnn/math/Product.cpp b/omnn/math/Product.cpp index 817875dae..ee5b15fc4 100644 --- a/omnn/math/Product.cpp +++ b/omnn/math/Product.cpp @@ -213,12 +213,12 @@ using namespace omnn::math; else if (it->IsExponentiation()) { auto& e = it->as(); - auto& ebase = e.getBase(); + auto& ebase = e.ebase(); if (ebase.IsVa()) { - addToVars = std::bind(&Product::AddToVars, this, ebase.as(), -e.getExponentiation()); + addToVars = std::bind(&Product::AddToVars, this, ebase.as(), -e.eexp()); } } - + base::Delete(it); if(addToVars) @@ -447,7 +447,7 @@ using namespace omnn::math; Delete(it); fo *= dn; break; - } else if (it->IsExponentiation() && it->as().getBase() == dn) { + } else if (it->IsExponentiation() && it->as().ebase() == dn) { Update(it, *it/dn); fo *= dn; break; @@ -822,7 +822,7 @@ using namespace omnn::math; if (!is.first) { if (m.IsExponentiation()) { auto& e = m.as(); - if (e.getBase().IsSum()) { + if (e.ebase().IsSum()) { is = m.IsMultiplicationSimplifiable(v); if (is.first) { p.Add(is.second); @@ -1007,8 +1007,8 @@ using namespace omnn::math; else if (it->IsExponentiation()) { auto& e = it->as(); - if (e.getBase() == va) { - Update(it, va ^ (e.getExponentiation()+1)); + if (e.ebase() == va) { + Update(it, va ^ (e.eexp()+1)); optimize(); return *this; } @@ -1018,11 +1018,11 @@ using namespace omnn::math; else if (v.IsExponentiation()) { auto& exponentiation = v.as(); - auto& vExpBase = exponentiation.getBase(); + auto& vExpBase = exponentiation.ebase(); for (auto it = members.begin(); it != members.end();) { if (it->IsExponentiation() && - it->as().getBase() == vExpBase) + it->as().ebase() == vExpBase) { Update(it, *it*v); optimize(); @@ -1030,7 +1030,7 @@ using namespace omnn::math; } else if (vExpBase == *it) { - Update(it, vExpBase ^ (exponentiation.getExponentiation() + 1)); + Update(it, vExpBase ^ (exponentiation.eexp() + 1)); optimize(); return *this; } @@ -1061,10 +1061,10 @@ using namespace omnn::math; ++it; } } - + // add new member Add(v); - + optimize(); return *this; } @@ -1140,11 +1140,11 @@ using namespace omnn::math; return *this; } } - else if (vIsExp && *it == e->getBase()) + else if (vIsExp && *it == e->ebase()) { - return *this *= e->getBase() ^ (-e->getExponentiation()); + return *this *= e->ebase() ^ (-e->eexp()); } - else if (it->IsExponentiation() && it->as().getBase() == v) + else if (it->IsExponentiation() && it->as().ebase() == v) { Update(it, *it / v); optimize(); @@ -1352,7 +1352,7 @@ using namespace omnn::math; auto& e = v.as(); same = e.IsMultiSign() && sz1 == 2 && Has(constants::minus_1); if (!same && Has(constants::i)) { - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee.IsFraction()) { auto& eef = ee.as(); auto& eefdn = eef.getDenominator(); @@ -1365,10 +1365,10 @@ using namespace omnn::math; } else { auto& e = v.as(); - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee.IsSimple() && ee < constants::zero) { OptimizeOn on; - auto potentiallyAlternativeForm = e.getBase().Reciprocal() ^ -ee; + auto potentiallyAlternativeForm = e.ebase().Reciprocal() ^ -ee; if (!potentiallyAlternativeForm.Same(v)) { same = operator==(potentiallyAlternativeForm); } @@ -1416,7 +1416,7 @@ using namespace omnn::math; Valuable Product::operator()(const Variable& va, const Valuable& augmentation) const { Valuable s; s.SetView(Valuable::View::Flat); - + if(augmentation.HasVa(va)) { IMPLEMENT; } else { @@ -1441,12 +1441,12 @@ using namespace omnn::math; else aug *= m; if (a==1) { - IMPLEMENT + LOG_AND_IMPLEMENT("Unhandled case in Product::operator()") } s = a(va,aug); } } - + // if(augmentation.HasVa(va)) { // IMPLEMENT; // } else { @@ -1460,12 +1460,12 @@ using namespace omnn::math; // _ /= m; // } // } -// +// // left.optimize(); // if (left.IsProduct()) { // IMPLEMENT // } -// +// // return left(va, _); // } // auto cova = getCommonVars(); @@ -1485,7 +1485,7 @@ using namespace omnn::math; // } return s; } - + void Product::solve(const Variable& va, solutions_t& solutions) const { auto it = std::find(members.begin(), members.end(), va); @@ -1497,13 +1497,13 @@ using namespace omnn::math; const Exponentiation* e = {}; while (it != end() && !(it->IsExponentiation() - && (found = it->as().getBase() == va) + && (found = it->as().ebase() == va) ) ) { ++it; } if (found) { - if (e->getExponentiation().IsZero()) { - IMPLEMENT + if (e->eexp().IsZero()) { + LOG_AND_IMPLEMENT("Zero exponent case in Product::solve") } solutions.insert(0_v); } @@ -1554,14 +1554,14 @@ using namespace omnn::math; out << cstr; return out; } - + Valuable::vars_cont_t Product::GetVaExps() const { vars_cont_t vaExps; for (auto& m : members) { auto mVaExps = m.GetVaExps(); for (auto& mve : mVaExps) { vaExps[mve.first] += mve.second; - } + } } return vaExps; } diff --git a/omnn/math/Sum.cpp b/omnn/math/Sum.cpp index 79dd8db80..b8af72bec 100644 --- a/omnn/math/Sum.cpp +++ b/omnn/math/Sum.cpp @@ -174,9 +174,9 @@ namespace } } else if (m.IsExponentiation()) { auto& e = m.as(); - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee == constants::minus_1) { - lcm.lcm(e.getBase()); + lcm.lcm(e.ebase()); } } else if (m.IsProduct()) { auto& p = m.as(); @@ -190,9 +190,9 @@ namespace } else if (m.IsExponentiation()) { auto& e = m.as(); - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee == constants::minus_1) { - lcm.lcm(e.getBase()); + lcm.lcm(e.ebase()); } } } @@ -495,14 +495,14 @@ namespace auto copy = it->Optimized(); CHECK_OPTIMIZATION_CACHE - + if (!it->Same(copy)) { Update(it, copy); } else ++it; } - + #if !defined(NDEBUG) && !defined(NOOMDEBUG) // if (w!=*this) { // std::cout << "Sum optimized from \n\t" << w << "\n \t to " << *this << std::endl; @@ -558,9 +558,9 @@ namespace } else if (m.IsExponentiation()) { auto& e = m.as(); - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee.IsInt() && ee.ca() < 0) { - operator*=(e.getBase() ^ (-ee)); // FIXME: (-1*((-1*percentWaterDehydrated + 100)^(-1))*potatoKgDehydrated + (percentWaterDehydrated^(-1))*weightWaterDehydrated) * (-1*percentWaterDehydrated + 100) + operator*=(e.ebase() ^ (-ee)); // FIXME: (-1*((-1*percentWaterDehydrated + 100)^(-1))*potatoKgDehydrated + (percentWaterDehydrated^(-1))*weightWaterDehydrated) * (-1*percentWaterDehydrated + 100) scan = true; break; @@ -576,7 +576,7 @@ namespace nop.Delete(p); auto balanced = - ((e.getBase() ^ f.getNumerator()) * (wo ^ d)) - ((-nop) ^ d); + ((e.ebase() ^ f.getNumerator()) * (wo ^ d)) - ((-nop) ^ d); Become(std::move(balanced)); scan = true; break; @@ -591,15 +591,15 @@ namespace break; } else if (member.IsExponentiation()) { auto& e = member.as(); - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee.IsInt() && ee < 0) { - operator*=(e.getBase() ^ (-ee)); + operator*=(e.ebase() ^ (-ee)); scan = true; break; } else if (ee.IsFraction()) { auto& f = ee.as(); - Become((e.getBase() ^ f.getNumerator()) - ((-(*this - member)) ^ f.getDenominator())); + Become((e.ebase() ^ f.getNumerator()) - ((-(*this - member)) ^ f.getDenominator())); scan = true; break; } @@ -642,7 +642,7 @@ namespace auto areAllPrincipalSurdFactors = std::all_of(begin(), end(), [](auto& m) { return m.PrincipalSurdFactor() != nullptr; }); if (areAllPrincipalSurdFactors) { - IMPLEMENT + LOG_AND_IMPLEMENT("All members have PrincipalSurdFactors but no implementation for handling this case") } } return isTherePrincipalSurdFactor; @@ -1431,7 +1431,7 @@ namespace if(IsMultival() == YesNoMaybe::Yes){ auto univariate = Univariate(); if (!univariate.IsSum()) { - IMPLEMENT + LOG_AND_IMPLEMENT("Univariate is not a sum") } else { grade = univariate.as().FillPolynomialCoefficients(coefficients, v); return grade; @@ -1460,7 +1460,7 @@ namespace //#pragma omp parallel default(none) shared(grade,coefficients) { OptimizeOff off; - //#pragma omp for + //#pragma omp for for (auto& m : members) { if(!m.HasVa(v)) @@ -1487,14 +1487,14 @@ namespace coefficients.clear(); return s.as().FillPolynomialCoefficients(coefficients, v); } else { - IMPLEMENT - } + LOG_AND_IMPLEMENT("Normalized polynomial required") + } } } auto vcnt = noVa ? constants::zero : it->second; // exponentation of va if (!vcnt.IsInt()) { - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation is not an integer") } int ie = static_cast(vcnt); if (ie < 0) @@ -1504,7 +1504,7 @@ namespace coefficients.clear(); return normalized.as().FillPolynomialCoefficients(coefficients, v); } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Normalization failed") } } else if (ie > grade) { @@ -1527,9 +1527,9 @@ namespace else if(m.IsExponentiation()) { auto& e = m.as(); - if (e.getBase() == v) + if (e.ebase() == v) { - auto& ee = e.getExponentiation(); + auto& ee = e.eexp(); if (ee.IsInt()) { auto i = static_cast(ee.ca()); if (i > grade) { @@ -1541,13 +1541,13 @@ namespace add(i, 1); } else - IMPLEMENT + LOG_AND_IMPLEMENT("Exponentiation is not an integer") } } else { std::cout << m << std::endl; - IMPLEMENT + LOG_AND_IMPLEMENT("Unhandled case in FillPolyCoeff") } } } diff --git a/omnn/math/Valuable.cpp b/omnn/math/Valuable.cpp index a090e77be..6ecd7f854 100644 --- a/omnn/math/Valuable.cpp +++ b/omnn/math/Valuable.cpp @@ -1114,7 +1114,7 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const { id = str.substr(i, to - i); if (str[to] == '(') { // functions if (to == 0) { - IMPLEMENT + LOG_AND_IMPLEMENT("Function call at position 0 in " << str) } auto cb = bracketsmap[to]; if (id == "sqrt"sv) { @@ -1872,7 +1872,7 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const { else if (val == 0) { some.insert(e); } else { - IMPLEMENT + LOG_AND_IMPLEMENT("Unexpected case in get_zeros_zones for " << *this) } prev = to; valPrevious = val; @@ -2419,7 +2419,7 @@ bool Valuable::SerializedStrEqual(const std::string_view& s) const { return exp->Same(value); if (Is()) return {}; - + return Hash() == value.Hash() && OfSameType(value) && operator==(value); diff --git a/omnn/math/Valuable.h b/omnn/math/Valuable.h index ad13b933d..9136a1aff 100644 --- a/omnn/math/Valuable.h +++ b/omnn/math/Valuable.h @@ -58,16 +58,15 @@ std::ostream& operator<<(std::ostream& out, const vars_cont_t& vars); } // namespace omnn namespace std { -omnn::math::Valuable abs(const omnn::math::Valuable&); -omnn::math::Valuable log(const omnn::math::Valuable&); -omnn::math::Valuable pow(const omnn::math::Valuable&, const omnn::math::Valuable&); +omnn::math::Valuable abs(const omnn::math::Valuable& valuable); +omnn::math::Valuable log(const omnn::math::Valuable& valuable); +omnn::math::Valuable pow(const omnn::math::Valuable& valuable1, const omnn::math::Valuable& valuable2); omnn::math::Valuable&& move(omnn::math::Valuable&&); -omnn::math::Valuable sqrt(const omnn::math::Valuable&); -omnn::math::Valuable tanh(const omnn::math::Valuable&); +omnn::math::Valuable sqrt(const omnn::math::Valuable& valuable); +omnn::math::Valuable tanh(const omnn::math::Valuable& valuable); template <> struct hash { - [[nodiscard]] size_t operator()(const omnn::math::Valuable& valuable) const { return hash_value(valuable); } }; @@ -339,20 +338,20 @@ class Valuable virtual ~Valuable()//{} ; virtual Valuable operator -() const; - virtual Valuable& operator +=(const Valuable&); - virtual Valuable& operator +=(int); - virtual Valuable& operator *=(const Valuable&); - virtual bool MultiplyIfSimplifiable(const Valuable&); + virtual Valuable& operator +=(const Valuable& v); + virtual Valuable& operator +=(int i); + virtual Valuable& operator *=(const Valuable& v); + virtual bool MultiplyIfSimplifiable(const Valuable& v); virtual bool MultiplyIfSimplifiable(const Integer& i); - virtual std::pair IsMultiplicationSimplifiable(const Valuable&) const; - virtual bool SumIfSimplifiable(const Valuable&); - virtual std::pair IsSummationSimplifiable(const Valuable&) const; - virtual std::pair IsModSimplifiable(const Valuable&) const; - virtual Valuable& operator /=(const Valuable&); - virtual Valuable& operator %=(const Valuable&); + virtual std::pair IsMultiplicationSimplifiable(const Valuable& v) const; + virtual bool SumIfSimplifiable(const Valuable& v); + virtual std::pair IsSummationSimplifiable(const Valuable& v) const; + virtual std::pair IsModSimplifiable(const Valuable& v) const; + virtual Valuable& operator /=(const Valuable& v); + virtual Valuable& operator %=(const Valuable& v); virtual Valuable& operator--(); virtual Valuable& operator++(); - virtual Valuable& operator^=(const Valuable&); + virtual Valuable& operator^=(const Valuable& v); // returns the greatest common divisor (GCD) with the given object virtual Valuable GCD(const Valuable&) const; @@ -597,8 +596,8 @@ class Valuable // SetView(View::Equation); // FIXME: see ifz test return operator -=(std::forward(valuable)); } - Valuable Equals(const Valuable&) const; - Valuable NotEquals(const Valuable&) const; + Valuable Equals(const Valuable& valuable) const; + Valuable NotEquals(const Valuable& valuable) const; // Valuable NE(const Valuable& to, const Valuable& abet) const; // not equals // Valuable NE(const Variable& x, const Valuable& to, std::initializer_list abet) const; // not equals Valuable LogicAnd(const Valuable& valuable) const; @@ -862,7 +861,6 @@ namespace std { template requires std::derived_from struct hash { - [[nodiscard]] constexpr size_t operator()(const T& valuable) const { return static_cast(valuable).Hash(); } }; diff --git a/omnn/math/Variable.cpp b/omnn/math/Variable.cpp index fbd46c811..8b687b1ab 100644 --- a/omnn/math/Variable.cpp +++ b/omnn/math/Variable.cpp @@ -321,9 +321,9 @@ namespace math { c = v.InCommonWith(*this); } else if (v.IsExponentiation()) { auto& e = v.as(); - if (e.getBase() == *this) { - if (e.getExponentiation().IsInt()) { - if (e.getExponentiation() > 0) { + if (e.ebase() == *this) { + if (e.eexp().IsInt()) { + if (e.eexp() > 0) { c = *this; } } else if (e.IsMultival()==YesNoMaybe::Yes) { diff --git a/omnn/math/test/Integer_test.cpp b/omnn/math/test/Integer_test.cpp index 1f191c0c7..7c7012787 100644 --- a/omnn/math/test/Integer_test.cpp +++ b/omnn/math/test/Integer_test.cpp @@ -469,14 +469,14 @@ BOOST_AUTO_TEST_CASE(Integer_IsModSimplifiable_test) // Test with zero divisor (by Knuth's definition, 10 % 0 = 10) result = i.IsModSimplifiable(0); BOOST_TEST(result.first); - BOOST_TEST(result.second == i); // 10 % 0 = 10 + BOOST_TEST(result.second == i); // 10 % 0 = 10 - // Test with variable (should defer to variable's implementation) + // Test with variable (should not be simplifiable) Variable va; result = i.IsModSimplifiable(va); BOOST_TEST(!result.first); - // Test with fraction (should not be simplifiable directly) + // Test with fraction (should not be simplifiable) result = i.IsModSimplifiable(Fraction(1, 2)); BOOST_TEST(!result.first);