Skip to content
Open
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
188 changes: 94 additions & 94 deletions omnn/math/Exponentiation.cpp

Large diffs are not rendered by default.

58 changes: 32 additions & 26 deletions omnn/math/Formula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Variable>* sequence)
: v(va), e(ex)
{
Expand All @@ -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);
Expand All @@ -49,21 +49,27 @@ namespace math {
bool is = {};
if (_.IsExponentiation()) {
auto& e = _.as<Exponentiation>();
bool isSum = e.getBase().IsSum();
Valuable sum = Sum{e.getBase(), constants::zero};
auto& s = (isSum ? e.getBase() : sum).as<Sum>();
bool isSum = e.ebase().IsSum();
Valuable sum = Sum{e.ebase(), constants::zero};
auto& s = (isSum ? e.ebase() : sum).as<Sum>();
if (s) {
if (s.size() > 2) {
IMPLEMENT
LOG_AND_IMPLEMENT("Formula::coordMatch not implemented for sum size > 2");
}
auto va = s.template GetFirstOccurence<Variable>();
auto va = s.GetFirstOccurence<Variable>();
if (va != s.end())
{
auto it = vaVals.find(va->as<Variable>());
// 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<Variable>();
auto it = vaVals.find(var);
auto isValue = it == vaVals.end();
is = isValue;
auto isCoord = !isValue;

if (va == s.begin())
++va;
else --va;
Expand All @@ -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 = {};
Expand All @@ -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
Expand All @@ -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<Sum>();
is = std::all_of(std::begin(s), std::end(s),
Expand All @@ -122,7 +128,7 @@ namespace math {
}
return is;
}

Valuable Formula::GetProductRootByCoordinates(const VaValMap& vaVals) const
{
Valuable root;
Expand All @@ -147,30 +153,30 @@ namespace math {
);
if(is) {
if (!value)
IMPLEMENT
LOG_AND_IMPLEMENT("Formula::GetProductRootByCoordinates value not found");
root = -*value;
return root; // found
}
}
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;
}


}}
26 changes: 13 additions & 13 deletions omnn/math/Fraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@
}

while (denominator().IsFraction()) {
auto& fdn = denominator().as<Fraction>();
auto& fdn = denominator().template as<Fraction>();
numerator() *= fdn.denominator();
denominator() = std::move(fdn.numerator());
}
Expand Down Expand Up @@ -244,14 +244,14 @@
}

if (numerator().IsExponentiation()) {
auto& e = numerator().as<Exponentiation>();
auto& exp = e.getExponentiation();
auto& e = numerator().template as<Exponentiation>();
auto& exp = e.eexp();

Check failure on line 248 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Linux Build

‘constexpr omnn::math::Valuable& omnn::math::Exponentiation::eexp()’ is protected within this context

Check failure on line 248 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / macOS Build

'eexp' is a protected member of 'omnn::math::Exponentiation'

Check failure on line 248 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Windows Build

'omnn::math::Exponentiation::eexp': cannot access protected member declared in class 'omnn::math::Exponentiation'
if (exp.IsInt() && exp < 0) {
denominator() *= e.getBase() ^ (-exp);
denominator() *= e.ebase() ^ (-exp);

Check failure on line 250 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Linux Build

‘constexpr omnn::math::Valuable& omnn::math::Exponentiation::ebase()’ is protected within this context

Check failure on line 250 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / macOS Build

'ebase' is a protected member of 'omnn::math::Exponentiation'

Check failure on line 250 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Windows Build

'omnn::math::Exponentiation::ebase': cannot access protected member declared in class 'omnn::math::Exponentiation'
numerator() = constants::one;
} else if (exp.IsFraction()) {
auto& f = exp.as<Fraction>();
auto in = e.getBase() / (denominator() ^ f.Reciprocal());
auto& f = exp.template as<Fraction>();
auto in = e.ebase() / (denominator() ^ f.Reciprocal());

Check failure on line 254 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Linux Build

‘constexpr omnn::math::Valuable& omnn::math::Exponentiation::ebase()’ is protected within this context

Check failure on line 254 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / macOS Build

'ebase' is a protected member of 'omnn::math::Exponentiation'

Check failure on line 254 in omnn/math/Fraction.cpp

View workflow job for this annotation

GitHub Actions / Windows Build

'omnn::math::Exponentiation::ebase': cannot access protected member declared in class 'omnn::math::Exponentiation'
if (in.IsInt()) {
e.setBase(std::move(in));
Become(std::move(e));
Expand Down Expand Up @@ -298,7 +298,7 @@
}
} else if (denom.IsSimple()) {
if (denom.IsPrincipalSurd()) {
auto& ps = denom.as<PrincipalSurd>();
auto& ps = denom.template as<PrincipalSurd>();
auto& e = ps.Index();
numerator() *= ps ^ (e + constants::minus_1);
setDenominator(std::move(ps.Radicand()));
Expand All @@ -312,7 +312,7 @@
}

if (denominator().IsProduct()) {
auto& dn = denominator().as<Product>();
auto& dn = denominator().template as<Product>();
if (dn.Has(numerator())) {
denominator() /= numerator();
numerator() = constants::one;
Expand All @@ -334,8 +334,8 @@
if (m.IsVa()) {
numerator() *= m ^ -1;
} else if (m.IsExponentiation()) {
auto& e = m.as<Exponentiation>();
numerator() *= e.getBase() ^ -e.getExponentiation();
auto& e = m.template as<Exponentiation>();
numerator() *= e.ebase() ^ -e.eexp();
} else
numerator() /= m;
}
Expand All @@ -344,15 +344,15 @@
}
}
} else if (denominator().IsSum()) {
auto& s = denominator().as<Sum>();
auto& s = denominator().template as<Sum>();
auto lcm = s.LCMofMemberFractionDenominators();
if (lcm != constants::one) {
numerator() *= lcm;
denominator() *= lcm;
reoptimize_the_fraction = true;
continue;
} else if (denominator().IsSum()) {
auto& s = denominator().as<Sum>();
auto& s = denominator().template as<Sum>();
auto lcm = s.LCMofMemberFractionDenominators();
if (lcm != constants::one) {
numerator() *= lcm;
Expand All @@ -367,7 +367,7 @@
} else // no products
{
// TODO :
// IMPLEMENT // uncomment to cover scenarios


// sum
// auto s = Sum::cast(numerator());
Expand Down
28 changes: 15 additions & 13 deletions omnn/math/Logarithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Exponentiation>().getBase() == _1) { // Simplify logarithm if the target is a power of the base
} else if (_2.IsExponentiation() && _2.as<Exponentiation>().ebase() == _1) { // Simplify logarithm if the target is a power of the base
Become(std::move(_2.as<Exponentiation>().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()) {
Expand All @@ -106,7 +106,7 @@ void Logarithm::optimize() {

Valuable& Logarithm::operator+=(const Valuable& v) {
// Implement addition of logarithms
if (v.IsLogarithm() && v.as<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, add the targets
_2 += v.as<Logarithm>().getTarget();
optimize();
Expand All @@ -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<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, multiply the targets
_2 *= v.as<Logarithm>().getTarget();
optimize();
Expand All @@ -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<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, multiply the targets and simplify
_2 *= v.as<Logarithm>().getTarget();
optimize();
Expand All @@ -145,7 +145,7 @@ bool Logarithm::MultiplyIfSimplifiable(const Valuable& v) {

std::pair<bool, Valuable> Logarithm::IsMultiplicationSimplifiable(const Valuable& v) const {
// Check if multiplication can be simplified and return the result
if (v.IsLogarithm() && v.as<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, return true and the multiplied targets
Valuable newTarget = _2 * v.as<Logarithm>().getTarget();
return {true, Logarithm(_1, newTarget)};
Expand All @@ -155,7 +155,7 @@ std::pair<bool, Valuable> Logarithm::IsMultiplicationSimplifiable(const Valuable

bool Logarithm::SumIfSimplifiable(const Valuable& v) {
// Check if summation can be simplified
if (v.IsLogarithm() && v.as<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, add the targets and simplify
_2 += v.as<Logarithm>().getTarget();
optimize();
Expand All @@ -166,7 +166,7 @@ bool Logarithm::SumIfSimplifiable(const Valuable& v) {

std::pair<bool, Valuable> Logarithm::IsSummationSimplifiable(const Valuable& v) const {
// Check if summation can be simplified and return the result
if (v.IsLogarithm() && v.as<Logarithm>().getBase() == _1) {
if (v.IsLogarithm() && v.as<Logarithm>().lbase() == _1) {
// If bases are the same, return true and the added targets
Valuable newTarget = _2 + v.as<Logarithm>().getTarget();
return {true, Logarithm(_1, newTarget)};
Expand All @@ -176,7 +176,9 @@ std::pair<bool, Valuable> 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<double>(_2)) / std::log(static_cast<double>(_1));
Expand Down Expand Up @@ -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());
}
Expand Down
1 change: 0 additions & 1 deletion omnn/math/Logarithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T>
void setBase(T&& b) {
Expand Down
Loading
Loading