Skip to content

Commit

Permalink
PrincipalSurd addition test
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 a6d85e8
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 163 deletions.
1 change: 0 additions & 1 deletion cmake/deps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -377,4 +377,3 @@ macro(deps)
endif()

endmacro()

11 changes: 6 additions & 5 deletions omnn/extrapolator/Extrapolator.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define BOOST_NO_CXX11_ALLOCATOR
//
// Created by Сергей Кривонос on 10.08.17.
//
Expand All @@ -14,20 +15,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 Expand Up @@ -164,7 +165,7 @@ bool Extrapolator::Consistent(const ublas::vector<T>& augment)
Valuable Extrapolator::Complete(const Valuable& e)
{
// search for the row
IMPLEMENT
std::cout << "Method Complete not implemented." << std::endl;
}

Extrapolator Extrapolator::ViewMatrix() const
Expand Down
8 changes: 4 additions & 4 deletions omnn/extrapolator/Extrapolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <limits.h>
#include <type_traits>

#include <omnn/math/FormulaOfVaWithSingleIntegerRoot.h>
#include "omnn/math/FormulaOfVaWithSingleIntegerRoot.h"

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/lu.hpp>
Expand All @@ -20,14 +20,14 @@ namespace omnn::math {
//using extrapolator_value_type = a_rational;
using extrapolator_value_type = Valuable;
using extrapolator_base_matrix = boost::numeric::ublas::matrix<extrapolator_value_type>;


class Extrapolator
: public extrapolator_base_matrix
{
using base = extrapolator_base_matrix;
using T = extrapolator_base_matrix::value_type;

using solution_t = typename ublas::matrix_vector_solve_traits<base, ublas::vector<T>>::result_type;

mutable solution_t solution;
Expand Down Expand Up @@ -89,7 +89,7 @@ class Extrapolator
operator Valuable() const;

Valuable Factors(const Variable& row, const Variable& col, const Variable& val) const;

/**
* Build formula of its ViewMatrix
* @returns formula from two params
Expand Down
116 changes: 76 additions & 40 deletions omnn/math/Cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@
//
#include "Cache.h"

#include <chrono>
#include <iostream>
#include <thread>
#include <any>
#include <sstream>
#include <utility>
#include <future>
#include <type_traits>
#include <new>

#include <boost/tokenizer.hpp>

#include <rt/tasq.h>
#include <storage/LevelDbCache.h>

#include <boost/tokenizer.hpp>
#include "PrincipalSurd.h"
#include "Valuable.h"
#include "Variable.h"

void TestStandardLibraryTypes() {
// Test for std::aligned_storage
std::aligned_storage<sizeof(int), alignof(int)> storage;

using namespace omnn::math;
using namespace omnn::rt;
// Test for std::future_error
try {
throw std::future_error(std::future_errc::no_state);
} catch (const std::future_error& e) {
// If caught, std::future_error is recognized
}
}

namespace {


#ifdef OPENMIND_MATH_USE_LEVELDB_CACHE
void DeleteDB(const Cache::path_str_t& path) {
if(fs::exists(path))
Expand All @@ -39,8 +57,7 @@ void Cache::DbOpen() {
std::cerr << err << status.ToString() << std::endl;
if(status.IsIOError()){
std::cout << "DB is busy. Waiting. Retrying in 5 sec." << std::endl;
using namespace std::chrono_literals;
std::this_thread::sleep_for(5s);
std::this_thread::sleep_for(std::chrono::seconds(5));
} else {
#ifndef NDEBUG
DeleteDB(path);
Expand All @@ -65,11 +82,10 @@ omnn::math::Cache::~Cache() {
}

Cache::Cached Cache::AsyncFetch(const Valuable &v, bool itIsOptimized) {
using self_t = std::remove_reference<decltype(*this)>::type;
auto&& task = std::async(std::launch::async, &self_t::GetOne,
this, v.str(), v.VaNames(), itIsOptimized);
// Cache::Cached cached(task);
return std::move(task);
using self_t = std::remove_reference<decltype(*this)>::type;
auto task = std::async(std::launch::async, &self_t::GetOne,
this, v.str(), v.VaNames(), itIsOptimized);
return task;
}

Cache::CheckCacheResult Cache::GetOneUsingVarHost(std::string&& key, VarHost::ptr host, bool itIsOptimized) {
Expand Down Expand Up @@ -169,40 +185,60 @@ void Cache::AsyncSet(std::string &&key, std::string &&v) {
CacheStoringTasks.AddTask(
[ This=this, // TODO: This=shared_from_this(),
k=std::move(key), v=std::move(v)
](){
return This->Set(k,v);
}
);
](){
return This->Set(k, v);
}
);
#endif
}

void Cache::AsyncSetSet(const Valuable& key, const val_set_t& v) {
std::stringstream ss;
ss << v;
auto s = ss.str();
auto l = s.length();
if (s[l-1] == ' ')
s[l-1] = 0;
AsyncSet(key.str(), std::move(s));
std::stringstream ss; // Declare the stringstream object
for (const auto& item : v)
ss << item.str() << ' '; // Serialize each Valuable in the set
auto s = ss.str();
if (!s.empty() && s.back() == ' ')
s.pop_back(); // Remove the trailing space
AsyncSet(key.str(), std::move(s)); // Use std::move from the std namespace
}

std::pair<bool, Valuable> Cache::Cached::Get() {
if (!future.valid())
throw std::future_error(std::future_errc::no_state);
try {
auto result = future.get();
return {true, std::move(result)}; // Changed to uniform initialization
} catch (const std::future_error& e) {
// Handle the case where the future is not ready
throw;
}
}

Cache::Cached::operator Valuable() {
assert(operator bool());
auto& got = Get();
#ifndef NDEBUG
assert(got.first);
// std::cout << "Used from cache: " << got.second << std::endl;
#endif
assert(got.second.is_optimized()); // if cached value is not optimized then just remove this assert
return got.second;
std::pair<bool, val_set_t> Cache::CachedSet::Get() {
if (!future.valid())
throw std::future_error(std::future_errc::no_state);
try {
auto result = future.get();
return {true, std::move(result)}; // Changed to uniform initialization
} catch (const std::future_error& e) {
// Handle the case where the future is not ready
throw;
}
}

Cache::CachedSet::operator Cache::val_set_t() {
assert(operator bool());
auto got = Get();
assert(got.first);
#ifndef NDEBUG
// std::cout << "Used from cache: [ " << got.second << "]" << std::endl;
#endif
return got.second;
}
Cache::Cached::operator Valuable() {
if (!*this) // Check if the future is ready and valid
throw std::future_error(std::future_errc::future_not_ready);
auto& got = Get(); // Extract the result from the future
assert(got.first); // Assert that the cached value is valid
return got.second; // Return the Valuable object
}

Cache::CachedSet::operator val_set_t() {
if (!*this) // Check if the future is ready and valid
throw std::future_error(std::future_errc::future_not_ready);
auto got = Get(); // Extract the result from the future
assert(got.first); // Assert that the cached set is valid
return got.second; // Return the val_set_t object
} // Missing closing brace added
} // Closes the namespace or class
33 changes: 16 additions & 17 deletions omnn/math/DuoValDescendant.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Author: sergejkrivonos
*/
#pragma once
#include <omnn/math/ValuableDescendantContract.h>
#include "ValuableDescendantContract.h"
#include <utility>

namespace omnn::math {
Expand Down Expand Up @@ -78,7 +78,7 @@ namespace omnn::math {
Valuable::hash ^= _1.Hash();
Valuable::optimized = {};
}

const Valuable& get2() const { return _2; }
template<class T>
void set2(T&& p) {
Expand All @@ -99,13 +99,13 @@ namespace omnn::math {
Valuable::hash ^= _2.Hash();
Valuable::optimized = {};
}

using base::base;

template <class T1, class T2>
constexpr DuoValDescendant(T1&& v1, T2&& v2)
: _1(std::forward<T1>(v1))
, _2(std::forward<T2>(v2))
template <class T1, class T2>
constexpr DuoValDescendant(T1&& v1, T2&& v2)
: _1(std::forward<T1>(v1))
, _2(std::forward<T2>(v2))
{
Valuable::hash = _1.Hash() ^ _2.Hash();
Valuable::maxVaExp = Chld::getMaxVaExp(_1, _2);
Expand Down Expand Up @@ -165,16 +165,16 @@ namespace omnn::math {
max_exp_t getMaxVaExp() const override {
return Chld::getMaxVaExp(_1, _2);
}

bool IsSimple() const override {
return IsMultival() == Valuable::YesNoMaybe::No
&& _1.IsSimple() && _2.IsSimple();
}

Valuable::YesNoMaybe IsMultival() const override {
return _1.IsMultival() || _2.IsMultival();
}

void Values(const std::function<bool(const Valuable&)>& f) const override
{
if (f) {
Expand All @@ -184,14 +184,14 @@ namespace omnn::math {
}

// TODO: multival caching (inspect all optimized and optimization transisions)


Valuable::solutions_t vals, thisValues;
_1.Values([&](auto& thisVal){
thisValues.insert(thisVal);
return true;
});

_2.Values([&](auto&vVal){
for(auto& tv:thisValues){
Valuable v = Chld{tv,vVal};
Expand All @@ -200,17 +200,17 @@ namespace omnn::math {
}
return true;
});

for(auto& v:vals)
f(v);
}
}

a_int Complexity() const override {
return _1.Complexity() + _2.Complexity();
}


Valuable::solutions_t Distinct() const override {
Valuable::solutions_t branches;
for (auto&& f : _1.Distinct()) {
Expand All @@ -222,4 +222,3 @@ namespace omnn::math {
}
};
}

16 changes: 8 additions & 8 deletions omnn/math/Exponentiation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by Сергей Кривонос on 01.09.17.
//
#pragma once
#include <omnn/math/Variable.h>
#include <omnn/math/DuoValDescendant.h>
#include "Variable.h"
#include "DuoValDescendant.h"

namespace omnn::math {

Expand Down Expand Up @@ -51,14 +51,14 @@ class Exponentiation
set1(::std::forward<T>(b));
InitVars();
optimized = {};
}
}
template<class T>
void updateBase(T&& b)
{
update1(std::forward<T>(b));
InitVars();
optimized = {};
}
}

const Valuable& eexp() const { return _2; }
const Valuable& getExponentiation() const { return _2; }
Expand All @@ -68,15 +68,15 @@ class Exponentiation
set2(std::forward<T>(exponentiation));
InitVars();
optimized = {};
}
}
template<class T>
void updateExponentiation(T&& exponentiation)
{
update2(std::forward<T>(exponentiation));
InitVars();
optimized = {};
}

static max_exp_t getMaxVaExp(const Valuable& b, const Valuable& e);
max_exp_t getMaxVaExp() const override;

Expand All @@ -93,10 +93,10 @@ class Exponentiation
explicit operator double() const override;
Valuable& d(const Variable& x) override;
Valuable& integral(const Variable& x, const Variable& C) override;

bool operator <(const Valuable& v) const override;
void optimize() override;

Valuable varless() const override;
const vars_cont_t& getCommonVars() const override;
vars_cont_t GetVaExps() const override;
Expand Down
Loading

0 comments on commit a6d85e8

Please sign in to comment.