Skip to content

Commit

Permalink
serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
devin-ai-integration[bot] authored and ohhmm committed Sep 30, 2024
1 parent b76acd2 commit c5466c0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 44 deletions.
6 changes: 3 additions & 3 deletions omnn/math/Exponentiation.cpp
Original file line number Diff line number Diff line change
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
32 changes: 16 additions & 16 deletions omnn/math/Polyfit.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,38 @@
Finds the coefficients of a polynomial p(x) of degree n that fits the data,
p(x(i)) to y(i), in a least squares sense. The result p is a row vector of
length n+1 containing the polynomial coefficients in incremental powers.
param:
oX x axis values
oY y axis values
nDegree polynomial degree including the constant
return:
coefficients of a polynomial starting at the constant coefficient and
ending with the coefficient of power to nDegree.
*/
template<typename T>
auto polyfit( const std::vector<T>& oX, const std::vector<T>& oY, size_t nDegree )
{
using namespace boost::numeric::ublas;

if ( oX.size() != oY.size() )
throw std::invalid_argument( "X and Y vector sizes do not match" );

// more intuative this way
nDegree++;

auto nCount = oX.size();
matrix<T> oXMatrix( nCount, nDegree );
matrix<T> oYMatrix( nCount, 1 );

// copy y matrix
for ( size_t i = 0; i < nCount; i++ )
{
new (&oYMatrix(i, 0)) T(oY[i]);
}

// create the X matrix
for ( size_t nRow = 0; nRow < nCount; nRow++ )
{
Expand All @@ -64,23 +64,23 @@ auto polyfit( const std::vector<T>& oX, const std::vector<T>& oY, size_t nDegree
nVal *= oX[nRow];
}
}

// transpose X matrix
matrix<T> oXtMatrix( trans(oXMatrix) );
// multiply transposed X matrix with X matrix
matrix<T> oXtXMatrix( prec_prod(oXtMatrix, oXMatrix) );
// multiply transposed X matrix with Y matrix
matrix<T> oXtYMatrix( prec_prod(oXtMatrix, oYMatrix) );

// lu decomposition
permutation_matrix<int> pert(oXtXMatrix.size1());
auto singular = lu_factorize(oXtXMatrix, pert);
// must be singular
BOOST_ASSERT( singular == 0 );

// backsubstitution
lu_substitute(oXtXMatrix, pert, oXtYMatrix);

// copy the result to coeff
return std::vector<T>( oXtYMatrix.data().begin(), oXtYMatrix.data().end() );
}
Expand All @@ -89,11 +89,11 @@ auto polyfit( const std::vector<T>& oX, const std::vector<T>& oY, size_t nDegree
Calculates the value of a polynomial of degree n evaluated at x. The input
argument pCoeff is a vector of length n+1 whose elements are the coefficients
in incremental powers of the polynomial to be evaluated.
param:
oCoeff polynomial coefficients generated by polyfit() function
oX x axis values
return:
Fitted Y values.
*/
Expand All @@ -103,7 +103,7 @@ auto polyval( const std::vector<T>& oCoeff, const std::vector<T>& oX )
auto nCount = oX.size();
auto nDegree = oCoeff.size();
std::vector<T> oY( nCount );

for ( size_t i = 0; i < nCount; i++ )
{
T nY = 0;
Expand All @@ -118,7 +118,7 @@ auto polyval( const std::vector<T>& oCoeff, const std::vector<T>& oX )
}
oY[i] = std::move(nY);
}

return oY;
}

Expand Down
33 changes: 26 additions & 7 deletions omnn/math/Valuable.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/preprocessor.hpp>
#include <boost/rational.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/split_member.hpp>
#include <boost/serialization/serialization.hpp>

#define _NUM2STR(x) #x
#define NUM2STR(x) _NUM2STR(x)
Expand Down Expand Up @@ -59,7 +63,7 @@ struct hash<omnn::math::Valuable> {

namespace omnn{
namespace math {

namespace constants {
extern const Valuable& e;
extern const Valuable& i;
Expand Down Expand Up @@ -135,6 +139,10 @@ class Valuable
static constexpr max_exp_t const& max_exp_z = max_exp_cz;
max_exp_t maxVaExp = 0;//max_exp_z; // ordering weight: vars max exponentiation in this valuable

bool optimized = false;

void MarkAsOptimized();

public:

/// <summary>
Expand Down Expand Up @@ -253,7 +261,7 @@ class Valuable
explicit Valuable(Valuable* v);
explicit Valuable(const encapsulated_instance& e);
virtual std::type_index Type() const;
const Valuable Link() const; // TODO : handle simulteneous values changes
const Valuable Link() const; // TODO : handle simulteneous values changes

Valuable& operator =(const Valuable& v);
Valuable& operator =(Valuable&& v);
Expand Down Expand Up @@ -555,7 +563,7 @@ class Valuable
Valuable IfEq(const Valuable& v, const Valuable& Then,
const Valuable& Else) const; /// returns an expression which equals to @Then when this expression
/// equals to @v param and @Else otherwise

/// <summary>
/// bool is 0 or 1
/// </summary>
Expand Down Expand Up @@ -658,14 +666,25 @@ class Valuable

[[nodiscard]] virtual bool is_optimized() const;

protected:
private:
friend class boost::serialization::access;

View view = View::Flat;
bool optimized = false;
void MarkAsOptimized();

// TODO : std::shared_ptr<std::vector<Valuable>> cachedValues;
template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & exp;
ar & hash;
ar & sz;
ar & maxVaExp;
ar & view;
ar & optimized;
}

};

BOOST_SERIALIZATION_ASSUME_ABSTRACT(Valuable)

template<class T>
const T& Valuable::as() const {
auto& the = get();
Expand Down
45 changes: 27 additions & 18 deletions omnn/math/ValuableCollectionDescendantContract.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace math {
class ValuableCollectionDescendantContract : public ValuableDescendantContract<ChildT>
{
using base = ValuableDescendantContract<ChildT>;

protected:
using cont = ContT;
virtual cont& GetCont() = 0;
Expand All @@ -31,7 +31,7 @@ namespace math {
}
return is;
}

public:
using iterator = typename cont::iterator;
using const_reference = typename ContT::const_reference;
Expand All @@ -43,7 +43,7 @@ namespace math {
ValuableCollectionDescendantContract& operator=(const ValuableCollectionDescendantContract&)=default;

virtual const cont& GetConstCont() const = 0;

constexpr auto begin() noexcept { return GetCont().begin(); }
constexpr auto end() noexcept { return GetCont().end(); }
constexpr auto begin() const noexcept { return GetConstCont().begin(); }
Expand All @@ -59,7 +59,7 @@ namespace math {
{
return GetConstCont().size();
}

static iterator getit(iterator it){
return it;
}
Expand All @@ -74,15 +74,15 @@ namespace math {
{
IMPLEMENT
}

bool VarSurdFactor(const iterator it) const {
return VarSurdFactor(*it);
}

bool HasSurdFactor() const {
return std::any_of(begin(), end(), ChildT::VarSurdFactor);
}

virtual const iterator Add(const Valuable& item, const iterator hint)
{
Valuable::hash ^= item.Hash();
Expand Down Expand Up @@ -132,22 +132,22 @@ namespace math {
break;
return i;
}

a_int Complexity() const override {
a_int c = 0;
for(auto& m : GetConstCont())
c += m.Complexity();
return c;
}

bool HasValueType(const std::type_info& type) const
{
for(const auto& a : GetConstCont())
if(typeid(a.get()) == type)
return true;
return false;
}

template<class T>
bool HasValueType() const
{
Expand All @@ -158,7 +158,7 @@ namespace math {
return m.template Is<T>();
});
}

bool Has(const Valuable& v) const
{
auto& c = GetConstCont();
Expand All @@ -173,7 +173,7 @@ namespace math {
#endif
return has;
}

const Variable* FindVa() const override
{
for (auto& i : GetConstCont())
Expand All @@ -184,15 +184,15 @@ namespace math {
}
return nullptr;
}

bool HasVa(const Variable& va) const override
{
for (auto& i : GetConstCont())
if(i.HasVa(va))
return true;
return false;
}

void CollectVa(std::set<Variable>& s) const override {
for (auto& i : GetConstCont())
i.CollectVa(s);
Expand All @@ -201,7 +201,7 @@ namespace math {
for (auto& i : GetConstCont())
i.CollectVaNames(s);
}

Valuable Each(const std::function<Valuable(const Valuable&)>& m) const {
ChildT c;
for(auto& i:GetConstCont())
Expand Down Expand Up @@ -230,7 +230,7 @@ namespace math {
}
return is;
}

void Values(const std::function<bool(const Valuable&)>& fun) const override {
auto sharedValuesProjection = std::vector<std::vector<Valuable>>();
for (auto& m : GetConstCont()) {
Expand All @@ -246,7 +246,7 @@ namespace math {
return true;
});
} else {
m.Values([&](const Valuable& value) {
m.Values([&](const Valuable& value) {
sharedValuesProjection.emplace_back().emplace_back(value.Link());
return true;
});
Expand Down Expand Up @@ -303,7 +303,7 @@ namespace math {
}
return evaluated;
}

void Eval(const Variable& va, const Valuable& v) override
{
Valuable::SetView(Valuable::View::Calc);
Expand All @@ -326,7 +326,7 @@ namespace math {
}
}
} while (updated);

// if(!FindVa())
// this->optimize();
}
Expand Down Expand Up @@ -379,5 +379,14 @@ namespace math {
auto e = this->Extract(it++);
it = Add(e, it);
}

private:
friend class boost::serialization::access;

template<class Archive>
void serialize(Archive & ar, const unsigned int version) {
ar & boost::serialization::base_object<ValuableDescendantContract<ChildT>>(*this);
ar & GetCont();
}
};
}}

0 comments on commit c5466c0

Please sign in to comment.