Skip to content

Commit

Permalink
OptimizationLoopDetect<Product> for Product::optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
ohhmm committed Sep 15, 2024
1 parent 1989e76 commit 6c2da86
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 62 deletions.
1 change: 0 additions & 1 deletion omnn/math/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//
#include "Cache.h"

#include <chrono>
#include <iostream>
#include <thread>

Expand Down
3 changes: 1 addition & 2 deletions omnn/math/Cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ class DB;

#include <storage/CacheBase.h>

#include "VarHost.h"
#include "Variable.h"
#include <omnn/math/VarHost.h>


namespace omnn::math {
Expand Down
2 changes: 1 addition & 1 deletion omnn/math/Integer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
//

#pragma once
#include <omnn/math/ValuableDescendantContract.h>
#include <type_traits>
#include "ValuableDescendantContract.h"

namespace omnn{
namespace math {
Expand Down
94 changes: 54 additions & 40 deletions omnn/math/Product.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,13 @@ namespace math {
if (!optimizations || optimized)
return;
MarkAsOptimized();
OptimizationLoopDetect<Product> antilooper(*this);
if (antilooper.isLoopDetected()) {
#if !defined(NDEBUG) && !defined(NOOMDEBUG)
LOG_AND_IMPLEMENT("Loop of optimizating detected in " << *this);
#endif
return;
}

auto isPlusMinus = YesNoMaybe::Maybe;
auto IsPlusMinus = [&] {
Expand Down Expand Up @@ -1121,6 +1128,49 @@ namespace math {
return isLess;
}

bool Product::operator ==(const Product& value) const
{
bool same = {};
auto& c1 = GetConstCont();
auto& c2 = value.GetConstCont();
auto sz1 = c1.size();
auto sz2 = c2.size();
auto sameSizes = sz1 == sz2;
if(sameSizes) {
same = c1 == c2;
} else if (sz1-sz2==1 || sz2-sz1==1) {
auto it1 = c1.begin(), it2 = c2.begin();
auto e1 = c1.end(), e2 = c2.end();
for(same = true;
same && it1 != e1 && it2 != e2;
++it1, ++it2)
{
if(it1->Same(1_v)){
++it1;
}
if(it2->Same(1_v)){
++it2;
}

if(it1 == e1 || it2 == e2)
continue;

same = same && it1->Same(*it2);
}

same = same && it1 == e1 && it2 == e2;
} else {
same = {};
}
if (!same // TODO: Check if it has same multival exponentiation and different sign or i in coefficient
&& IsMultival() == YesNoMaybe::Yes
&& value.IsMultival() == YesNoMaybe::Yes)
{
//LOG_AND_IMPLEMENT("Check if it has same multival exponentiation and different sign or i in coefficient: " << *this << " == " << v);
}
return same;
}

namespace {
constexpr std::hash<a_int> Hasher;
const size_t Hash1 = Hasher(1);
Expand All @@ -1130,42 +1180,12 @@ namespace math {
auto sameHash = (Valuable::hash & ~Hash1) == (v.Hash() & ~Hash1); // ignore multiplication by 1
auto same = v.Is<Product>() && sameHash;
auto& c1 = GetConstCont();
auto sz1 = c1.size();
if (same) {
auto& vp = v.as<Product>();
auto& c2 = vp.GetConstCont();
auto sz2 = c2.size();
auto sameSizes = sz1 == sz2;
if(sameSizes) {
same = c1 == c2;
} else if (sz1-sz2==1 || sz2-sz1==1) {
auto it1 = c1.begin(), it2 = c2.begin();
auto e1 = c1.end(), e2 = c2.end();
for(same = true;
same && it1 != e1 && it2 != e2;
++it1, ++it2)
{
if(it1->Same(1_v)){
++it1;
}
if(it2->Same(1_v)){
++it2;
}

if(it1 == e1 || it2 == e2)
continue;

same = same && it1->Same(*it2);
}

same = same && it1 == e1 && it2 == e2;
} else {
same = {};
}
same = operator ==(v.as<Product>());
}
else if (sameHash
&& (sz1 == 1
|| (sz1 == 2 && c1.begin()->Same(1_v) )))
else if (size_t sz1; sameHash
&& ((sz1 = c1.size()) == 1
|| (sz1 == 2 && c1.begin()->Same(1_v) )))
{
same = c1.rbegin()->operator==(v);
} else if (members.empty()) {
Expand All @@ -1187,12 +1207,6 @@ namespace math {
}
}
}
} else if (v.IsProduct()
&& IsMultival() == YesNoMaybe::Yes
&& v.IsMultival() == YesNoMaybe::Yes)
{
// TODO: Check if it has same multival exponentiation and different sign or i in coefficient
//LOG_AND_IMPLEMENT("Check if it has same multival exponentiation and different sign or i in coefficient: " << *this << " == " << v);
}
return same;
}
Expand Down
1 change: 1 addition & 0 deletions omnn/math/Product.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Product
Valuable& operator /=(const Valuable& v) override;
Valuable& operator %=(const Valuable& v) override;
Valuable& operator ^=(const Valuable& v) override;
bool operator ==(const Product& v) const;
bool operator ==(const Valuable& v) const override;
bool operator<(const Valuable& v) const override;
Valuable Sign() const override;
Expand Down
6 changes: 3 additions & 3 deletions omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ extern const Variable& integration_result_constant;
class VarHost;
struct ValuableDescendantMarker {};

Valuable implement(const char* str = "");


class Valuable
: public OpenOps<Valuable>
{
Expand Down Expand Up @@ -660,9 +663,6 @@ class Valuable
// TODO : std::shared_ptr<std::vector<Valuable>> cachedValues;
};


Valuable implement(const char* str = "");

template<class T>
const T& Valuable::as() const {
auto& the = get();
Expand Down
7 changes: 6 additions & 1 deletion omnn/math/ValuableDescendantContract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,9 @@ Valuable ValuableDescendantBase::Univariate() const {
return uni;
}

}}
Valuable ValuableDescendantBase::Sqrt() const {
LOG_AND_IMPLEMENT("Implement Sqrt method for " << *this);
}

} // namespace math
} // namespace omnn
4 changes: 2 additions & 2 deletions omnn/math/ValuableDescendantContract.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ namespace math {

void optimize() override { }
//void expand() override { }
Valuable Sqrt() const override { IMPLEMENT }
Valuable Sqrt() const override;

bool IsComesBefore(const Valuable& v) const override
{
return *this > v;
Expand Down
24 changes: 12 additions & 12 deletions omnn/math/VarHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

#include <boost/any.hpp>

#include "Integer.h"
#include "Variable.h"
#include <omnn/math/Integer.h>
#include <omnn/math/Variable.h>

namespace omnn {
namespace math {
Expand All @@ -35,34 +35,34 @@ namespace math {

public:
using ptr = std::shared_ptr<VarHost>;
using cptr = std::shared_ptr<const VarHost>;
using cptr = std::shared_ptr<const VarHost>;
using cref = const VarHost&;
using hosted_storage_t = std::pair<Variable, std::string>;

virtual ~VarHost() = default;

template<class T>
Variable New(const T& id) {
#ifndef NDEBUG
#ifndef NDEBUG
auto host = dynamic_cast<TypedVarHost<T>*>(this);
if (host != this) {
LOG_AND_IMPLEMENT("wrong id type");
}
#endif
if (host != this) {
LOG_AND_IMPLEMENT("wrong id type");
}
#endif
Variable v(sh());
v.SetId(id);
return v;
}

Variable New();
Variable New();

protected:
VarHost() = default;

const any::any& GetId(const Variable&) const;

Variable New(const any::any& id);

Variable New(const any::any& id);

virtual void AddNewId(const void* id) {
IMPLEMENT;
}
Expand Down Expand Up @@ -223,7 +223,7 @@ namespace math {
const T& idT = *idTp;
it = hosted.find(idT);
if (it == hosted.end()) {
it = hosted.emplace(idT, hosted_storage_t{New(id), ""s}).first;
it = hosted.emplace(idT, hosted_storage_t{New(id), ""s}).first;
}
}

Expand Down

0 comments on commit 6c2da86

Please sign in to comment.