Skip to content

Commit

Permalink
Fix const correctness for sqrt() methods in Valuable and Exponentiati…
Browse files Browse the repository at this point in the history
…on classes
  • Loading branch information
devin-ai-integration[bot] authored and ohhmm committed Sep 17, 2024
1 parent de55ef2 commit 3d3d4f7
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 74 deletions.
21 changes: 17 additions & 4 deletions omnn/extrapolator/Extrapolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@
#include "math/Integer.h"
#include "math/Sum.h"

namespace omnn{
namespace math{
Valuable abs(const Valuable& v) {
return v.abs();
}

Valuable sqrt(const Valuable& v) {
// Assuming Valuable class has a sqrt method
return v.sqrt();
}
}
}

using namespace omnn;
using namespace math;

Expand All @@ -14,20 +27,20 @@ auto det_fast(extrapolator_base_matrix matrix)
{
using T = extrapolator_base_matrix::value_type;
ublas::permutation_matrix<std::size_t> pivots(matrix.size1());

auto isSingular = ublas::lu_factorize(matrix, pivots);
if (isSingular)
return T(0);

T det = 1;
for (std::size_t i = 0; i < pivots.size(); ++i)
{
if (pivots(i) != i)
det *= static_cast<double>(-1);

det *= matrix(i, i);
}

return det;
}

Expand Down
73 changes: 35 additions & 38 deletions omnn/math/Exponentiation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace omnn::math {
{
return getMaxVaExp(getBase(), getExponentiation());
}

Valuable Exponentiation::varless() const
{
if (FindVa())
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace omnn::math {
Become(std::move(ebase()));
return;
}

if (eexp().IsSum())
{
auto& s = eexp().as<Sum>();
Expand Down Expand Up @@ -203,7 +203,7 @@ namespace omnn::math {
// }
// }
// }

if (ebase().IsFraction() && eexp().IsMultival()==YesNoMaybe::No) {
auto& f = ebase().as<Fraction>();
auto _ = (f.getNumerator() ^ eexp()) / (f.getDenominator() ^ eexp());
Expand Down Expand Up @@ -232,7 +232,7 @@ namespace omnn::math {
if (p.Has(constants::pi) &&
p.Has(constants::i)) { // TODO : sequence does matter :
// e^(i*pi) =?= e^(pi*i)
// https://en.wikipedia.org/wiki/Commutative_property#Division,_subtraction,_and_exponentiation
// https://en.wikipedia.org/wiki/Commutative_property#Division,_subtraction,_and_exponentiation
// what about Commutativity on irrationals product?
// lets assume yes for this particular expression, but there are some doubts, need a prove

Expand Down Expand Up @@ -496,7 +496,7 @@ namespace omnn::math {
InitVars();
}
}

Valuable& Exponentiation::operator +=(const Valuable& v)
{
return Become(Sum {*this, v});
Expand Down Expand Up @@ -660,7 +660,7 @@ namespace omnn::math {
// copy.updateBase(std::move(is.second));
// is.second = copy;
// }
//} else
//} else
if (ee == constants::minus_1)
{
auto gcd = ebase().GCD(v);
Expand Down Expand Up @@ -760,14 +760,14 @@ namespace omnn::math {
thisValues.insert(thisVal);
return true;
});

v.Values([&](auto& vVal) {
for (auto& tv : thisValues) {
vals.insert(tv / vVal);
}
return true;
});

return Become(Valuable(std::move(vals)));
}
else if (v.IsExponentiation())
Expand Down Expand Up @@ -810,7 +810,7 @@ namespace omnn::math {
optimize();
return *this;
}

bool Exponentiation::operator ==(const Valuable& v) const
{
auto eq = v.IsExponentiation() && Hash()==v.Hash();
Expand All @@ -829,7 +829,7 @@ namespace omnn::math {
}
return eq;
}

Exponentiation::operator double() const
{
return std::pow(static_cast<double>(ebase()), static_cast<double>(eexp()));
Expand All @@ -842,9 +842,9 @@ namespace omnn::math {
if(ehx) {
IMPLEMENT
if(bhx){

}else{

}
} else if (bhx) {
if(ebase() == x)
Expand All @@ -856,8 +856,8 @@ namespace omnn::math {
optimize();
return *this;
}
Valuable& Exponentiation::integral(const Variable& x, const Variable& C)

Valuable& Exponentiation::integral(const Variable& x, Variable& C)
{
if (ebase()==x) {
auto& ee = eexp();
Expand All @@ -875,7 +875,7 @@ namespace omnn::math {
} else {
IMPLEMENT
}

return *this;
}

Expand All @@ -889,7 +889,7 @@ namespace omnn::math {
if (e.getExponentiation() == getExponentiation())
return getBase() < e.getBase();
}

return base::operator <(v);
}

Expand All @@ -909,27 +909,27 @@ namespace omnn::math {
is = _2.as<Fraction>().getDenominator().IsEven() || is;
return is;
}

void Exponentiation::Values(const std::function<bool(const Valuable&)>& fun) const
{
if (fun) {
auto cache = optimized; // TODO: multival caching (inspect all optimized and optimization transisions) auto isCached =

solutions_t vals;
{
std::deque<Valuable> d1;
_1.Values([&](auto& v){
d1.push_back(v);
return true;
});

_2.Values([&](auto& v){
auto vIsFrac = v.IsFraction();
const Fraction* f;
if(vIsFrac)
f = &v.template as<Fraction>();
auto vMakesMultival = vIsFrac && f->getDenominator().IsEven()==YesNoMaybe::Yes;

for(auto& item1:d1){
if(vMakesMultival){
static const Variable x;
Expand All @@ -949,7 +949,7 @@ namespace omnn::math {
return true;
});
}

for(auto& v:vals)
fun(v);
}
Expand All @@ -968,10 +968,10 @@ namespace omnn::math {
out << "pow(";
getBase().code(out) << ',';
getExponentiation().code(out) << ')';
}
}
return out;
}

bool Exponentiation::IsComesBefore(const Valuable& v) const
{
auto mve = getMaxVaExp();
Expand Down Expand Up @@ -1001,7 +1001,7 @@ namespace omnn::math {
if (c != ec)
is = c > ec;
else {
is = getBase().IsComesBefore(e.getBase()) ||
is = getBase().IsComesBefore(e.getBase()) ||
(!e.ebase().IsComesBefore(ebase()) && getExponentiation().IsComesBefore(e.getExponentiation())); // || str().length() > e->str().length();
// auto expComesBefore = eexp().IsComesBefore(e->eexp());
// auto ebase()ComesBefore = ebase().IsComesBefore(e->ebase());
Expand All @@ -1026,7 +1026,7 @@ namespace omnn::math {

return is;
}

Valuable Exponentiation::calcFreeMember() const
{
Valuable c;
Expand Down Expand Up @@ -1060,17 +1060,14 @@ namespace omnn::math {
return std::move(copy);
}

Valuable& Exponentiation::sqrt() {
hash ^= _2.Hash();
_2 /= 2;
hash ^= _2.Hash();
optimized = {};
optimize();
return *this;
Valuable Exponentiation::sqrt() const {
auto result = *this;
result._2 /= 2;
result.optimize();
return result;
}

const Valuable::vars_cont_t& Exponentiation::getCommonVars() const
{
const Valuable::vars_cont_t& Exponentiation::getCommonVars() const {
#ifndef NDEBUG
auto& b = ebase();
if (b.IsVa()) { // TODO: FindVa too
Expand All @@ -1083,13 +1080,13 @@ namespace omnn::math {

//<< (v.begin()->first == b)
LOG_AND_IMPLEMENT(*this << " Exponentiation::getCommonVars not ready, no " << b << " in v "
);
);
}
}
#endif
return v;
}

Valuable::vars_cont_t Exponentiation::GetVaExps() const
{
auto vaExps = getBase().GetVaExps();
Expand All @@ -1099,7 +1096,7 @@ namespace omnn::math {
}
return vaExps;
}

Valuable Exponentiation::InCommonWith(const Valuable& v) const
{
auto c = 1_v;
Expand Down Expand Up @@ -1161,7 +1158,7 @@ namespace omnn::math {
}
return c;
}

Valuable Exponentiation::operator()(const Variable& va) const
{
return operator()(va, constants::zero);
Expand Down
Loading

0 comments on commit 3d3d4f7

Please sign in to comment.