diff --git a/omnn/math/Exponentiation.cpp b/omnn/math/Exponentiation.cpp index eee65da5d..94860cb7a 100644 --- a/omnn/math/Exponentiation.cpp +++ b/omnn/math/Exponentiation.cpp @@ -91,7 +91,7 @@ namespace omnn::math { Become(std::move(ebase())); return; } - + if (eexp().IsSum()) { auto& s = eexp().as(); @@ -203,7 +203,7 @@ namespace omnn::math { // } // } // } - + if (ebase().IsFraction() && eexp().IsMultival()==YesNoMaybe::No) { auto& f = ebase().as(); auto _ = (f.getNumerator() ^ eexp()) / (f.getDenominator() ^ eexp()); @@ -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 diff --git a/omnn/math/Polyfit.h b/omnn/math/Polyfit.h index 9d0ef3be9..9bf674dfa 100644 --- a/omnn/math/Polyfit.h +++ b/omnn/math/Polyfit.h @@ -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 auto polyfit( const std::vector& oX, const std::vector& 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 oXMatrix( nCount, nDegree ); matrix 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++ ) { @@ -64,23 +64,23 @@ auto polyfit( const std::vector& oX, const std::vector& oY, size_t nDegree nVal *= oX[nRow]; } } - + // transpose X matrix matrix oXtMatrix( trans(oXMatrix) ); // multiply transposed X matrix with X matrix matrix oXtXMatrix( prec_prod(oXtMatrix, oXMatrix) ); // multiply transposed X matrix with Y matrix matrix oXtYMatrix( prec_prod(oXtMatrix, oYMatrix) ); - + // lu decomposition permutation_matrix 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( oXtYMatrix.data().begin(), oXtYMatrix.data().end() ); } @@ -89,11 +89,11 @@ auto polyfit( const std::vector& oX, const std::vector& 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. */ @@ -103,7 +103,7 @@ auto polyval( const std::vector& oCoeff, const std::vector& oX ) auto nCount = oX.size(); auto nDegree = oCoeff.size(); std::vector oY( nCount ); - + for ( size_t i = 0; i < nCount; i++ ) { T nY = 0; @@ -118,7 +118,7 @@ auto polyval( const std::vector& oCoeff, const std::vector& oX ) } oY[i] = std::move(nY); } - + return oY; } diff --git a/omnn/math/Valuable.h b/omnn/math/Valuable.h index 3b9f1dce7..64649173e 100644 --- a/omnn/math/Valuable.h +++ b/omnn/math/Valuable.h @@ -18,6 +18,10 @@ #include #include #include +#include +#include +#include +#include #define _NUM2STR(x) #x #define NUM2STR(x) _NUM2STR(x) @@ -59,7 +63,7 @@ struct hash { namespace omnn{ namespace math { - + namespace constants { extern const Valuable& e; extern const Valuable& i; @@ -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: /// @@ -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); @@ -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 - + /// /// bool is 0 or 1 /// @@ -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> cachedValues; + template + 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 const T& Valuable::as() const { auto& the = get(); diff --git a/omnn/math/ValuableCollectionDescendantContract.h b/omnn/math/ValuableCollectionDescendantContract.h index 563dec943..8f2d73951 100644 --- a/omnn/math/ValuableCollectionDescendantContract.h +++ b/omnn/math/ValuableCollectionDescendantContract.h @@ -16,7 +16,7 @@ namespace math { class ValuableCollectionDescendantContract : public ValuableDescendantContract { using base = ValuableDescendantContract; - + protected: using cont = ContT; virtual cont& GetCont() = 0; @@ -31,7 +31,7 @@ namespace math { } return is; } - + public: using iterator = typename cont::iterator; using const_reference = typename ContT::const_reference; @@ -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(); } @@ -59,7 +59,7 @@ namespace math { { return GetConstCont().size(); } - + static iterator getit(iterator it){ return it; } @@ -74,7 +74,7 @@ namespace math { { IMPLEMENT } - + bool VarSurdFactor(const iterator it) const { return VarSurdFactor(*it); } @@ -82,7 +82,7 @@ namespace math { 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(); @@ -132,14 +132,14 @@ 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()) @@ -147,7 +147,7 @@ namespace math { return true; return false; } - + template bool HasValueType() const { @@ -158,7 +158,7 @@ namespace math { return m.template Is(); }); } - + bool Has(const Valuable& v) const { auto& c = GetConstCont(); @@ -173,7 +173,7 @@ namespace math { #endif return has; } - + const Variable* FindVa() const override { for (auto& i : GetConstCont()) @@ -184,7 +184,7 @@ namespace math { } return nullptr; } - + bool HasVa(const Variable& va) const override { for (auto& i : GetConstCont()) @@ -192,7 +192,7 @@ namespace math { return true; return false; } - + void CollectVa(std::set& s) const override { for (auto& i : GetConstCont()) i.CollectVa(s); @@ -201,7 +201,7 @@ namespace math { for (auto& i : GetConstCont()) i.CollectVaNames(s); } - + Valuable Each(const std::function& m) const { ChildT c; for(auto& i:GetConstCont()) @@ -230,7 +230,7 @@ namespace math { } return is; } - + void Values(const std::function& fun) const override { auto sharedValuesProjection = std::vector>(); for (auto& m : GetConstCont()) { @@ -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; }); @@ -303,7 +303,7 @@ namespace math { } return evaluated; } - + void Eval(const Variable& va, const Valuable& v) override { Valuable::SetView(Valuable::View::Calc); @@ -326,7 +326,7 @@ namespace math { } } } while (updated); - + // if(!FindVa()) // this->optimize(); } @@ -379,5 +379,14 @@ namespace math { auto e = this->Extract(it++); it = Add(e, it); } + + private: + friend class boost::serialization::access; + + template + void serialize(Archive & ar, const unsigned int version) { + ar & boost::serialization::base_object>(*this); + ar & GetCont(); + } }; }}