Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom allocator construct and destroy methods #278

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ubuntu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
build-in-ubuntu:

runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- name: Cache dependencies
Expand Down
16 changes: 16 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ CMakeLists.txt.user.*
/*.cmake
/*.ninja
/compile_commands.json

# Add build directories and common build artifacts
/CMakeFiles/
/CMakeCache.txt
/CTestTestfile.cmake
/Makefile
/cmake_install.cmake
*.a
*.o
*.so
/bin/
/lib/
/*/CMakeFiles/
/*/CTestTestfile.cmake
/*/Makefile
/*/cmake_install.cmake
79 changes: 42 additions & 37 deletions omnn/extrapolator/Extrapolator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,49 @@
#include "Extrapolator.h"
#include "math/Integer.h"
#include "math/Sum.h"
#include "math/custom_allocator.h"
#include <boost/numeric/ublas/storage.hpp>

using namespace omnn;
using namespace math;
using namespace boost::numeric::ublas;

// Correct usage of Boost Ublas vector and matrix with custom_allocator
// Replace incorrect allocator functions with correct Boost Ublas container functions

auto det_fast(extrapolator_base_matrix matrix)
{
using T = extrapolator_base_matrix::value_type;
ublas::permutation_matrix<std::size_t> pivots(matrix.size1());
// Ensure that matrix_t, vector_t, and permutation_matrix_t are using unbounded_array_wrapper with custom_allocator
using matrix_t = boost::numeric::ublas::matrix<Valuable, boost::numeric::ublas::basic_row_major<>, unbounded_array_wrapper<Valuable, custom_allocator<Valuable>>>;
using vector_t = boost::numeric::ublas::vector<Valuable, unbounded_array_wrapper<Valuable, custom_allocator<Valuable>>>;
using permutation_matrix_t = boost::numeric::ublas::permutation_matrix<std::size_t, unbounded_array_wrapper<std::size_t, custom_allocator<std::size_t>>>;

// This function calculates the determinant of a matrix using LU factorization
auto det_fast(matrix_t matrix) {
// Use the matrix's size1 directly to avoid calling size on the allocator
auto matrix_size = matrix.size1();
permutation_matrix_t pivots(matrix_size);

auto isSingular = ublas::lu_factorize(matrix, pivots);
// Perform LU factorization on a copy of the matrix to preserve the original matrix
matrix_t matrix_copy(matrix);
auto isSingular = ublas::lu_factorize(matrix_copy, pivots);
if (isSingular)
return T(0);
return Valuable(0);

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

det *= matrix(i, i);
det *= matrix_copy(i, i);
}

return det;
}

bool Extrapolator::Consistent(const extrapolator_base_matrix& augment)
{
bool Extrapolator::Consistent(const matrix_t& augment) {
return Determinant() == det_fast(augment);
}

Valuable Extrapolator::Factors(const Variable& row, const Variable& col, const Variable& val) const
{
Valuable Extrapolator::Factors(const Variable& row, const Variable& col, const Variable& val) const {
Product e;
auto szy = size1();
auto szx = size2();
Expand All @@ -54,8 +64,7 @@ Valuable Extrapolator::Factors(const Variable& row, const Variable& col, const V
return Valuable(std::move(e));
}

Extrapolator::operator Formula() const
{
Extrapolator::operator Formula() const {
bool integers = true;
auto vm = ViewMatrix();
Valuable e = 1_v;
Expand All @@ -81,9 +90,8 @@ Extrapolator::operator Formula() const
return s.FormulaOfVa(vv);
}

Extrapolator::Extrapolator(std::initializer_list<std::vector<T>> dependancy_matrix)
: base(dependancy_matrix.size(), dependancy_matrix.begin()->size())
{
Extrapolator::Extrapolator(std::initializer_list<std::vector<Valuable>> dependancy_matrix)
: base(dependancy_matrix.size(), dependancy_matrix.begin()->size()) {
auto rows = dependancy_matrix.size();
auto r = dependancy_matrix.begin();
auto columns = r->size();
Expand All @@ -96,45 +104,44 @@ Extrapolator::Extrapolator(std::initializer_list<std::vector<T>> dependancy_matr
++r;
}
}
Extrapolator::solution_t Extrapolator::Solve(const ublas::vector<T>& augment) const
Extrapolator::solution_t Extrapolator::Solve(const vector_t& augment) const
{
auto e = *this;
auto e = static_cast<const matrix_t&>(*this);
auto sz1 = size1();
auto sz2 = size2();

ublas::vector<T> a(sz2);
const ublas::vector<T>* au = &augment;
// Initialize vector with size and default value
vector_t a(sz2, Valuable(0));
const vector_t* au = &augment;
if (sz1 > sz2 + 1 /*augment*/) {
// make square matrix to make it solvable by boost ublas
e = Extrapolator(sz2, sz2);
// sum first equations
a[0] = 0;
for (auto i = sz2; i--;) {
e(0, i) = 0;
e.insert_element(0, i, Valuable(0));
}
auto d = sz1 - sz2;
for (auto i = d; i--;) {
for (auto j = sz2; j--;) {
e(0, j) += operator()(i, j);
}
a[0] += augment[i];
a(i - d) = (*au)(i); // Use vector's operator() provided by Boost Ublas for element access
}

for (auto i = d; i < sz1; ++i) {
for (auto j = sz2; j--;) {
e(i - d, j) = operator()(i, j);
}
a[i - d] = augment[i];
a(i - d) = (*au)(i); // Use vector's operator() provided by Boost Ublas for element access
}
au = &a;
}
solution = ublas::solve(e, *au, ublas::upper_tag());
return solution;
}

Extrapolator::T Extrapolator::Determinant() const
{
ublas::permutation_matrix<std::size_t> pivots(this->size1());
Extrapolator::T Extrapolator::Determinant() const {
permutation_matrix_t pivots(this->size1());
auto mLu = *this;
auto isSingular = ublas::lu_factorize(mLu, pivots);
if (isSingular)
Expand All @@ -151,12 +158,11 @@ Extrapolator::T Extrapolator::Determinant() const
return det;
}

bool Extrapolator::Consistent(const ublas::vector<T>& augment)
{
bool Extrapolator::Consistent(const vector_t& augment) {
auto sz = augment.size();
extrapolator_base_matrix augmentedMatrix(sz, 1);
matrix_t augmentedMatrix(sz, 1);
for (auto i = sz; i-- > 0;) {
augmentedMatrix(i, 0) = augment[i];
augmentedMatrix(i, 0) = augment(i);
}
return Consistent(augmentedMatrix);
}
Expand Down Expand Up @@ -185,8 +191,7 @@ Extrapolator Extrapolator::ViewMatrix() const
return e;
}

Extrapolator::operator Valuable() const
{
Extrapolator::operator Valuable() const {
Valuable v = 0_v;
auto szy = size1();
auto szx = size2();
Expand Down
31 changes: 14 additions & 17 deletions omnn/extrapolator/Extrapolator.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <type_traits>

#include <omnn/math/FormulaOfVaWithSingleIntegerRoot.h>
#include <omnn/math/custom_allocator.h> // Include custom_allocator

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/lu.hpp>
Expand All @@ -28,8 +29,13 @@ class Extrapolator
using base = extrapolator_base_matrix;
using T = extrapolator_base_matrix::value_type;

public:
using solution_t = typename ublas::matrix_vector_solve_traits<base, ublas::vector<T>>::result_type;
// Define vector_t and matrix_t as aliases for ublas types with custom_allocator
using vector_t = ublas::vector<T, custom_allocator<T>>;
using matrix_t = ublas::matrix<T, ublas::row_major, custom_allocator<T>>;

private:
mutable solution_t solution;

public:
Expand All @@ -39,15 +45,15 @@ class Extrapolator

Extrapolator(std::initializer_list<std::vector<T>> dependancy_matrix);

solution_t Solve(const ublas::vector<T>& augment) const;
solution_t Solve(const vector_t& augment) const;

T Determinant() const;

/**
* If possible, make the matrix consistent
* @return true if it is
* **/
bool Consistent(const ublas::vector<T>& augment);
bool Consistent(const matrix_t& augment);

/**
* If possible, make the matrix consistent
Expand All @@ -56,27 +62,17 @@ class Extrapolator
bool Consistent(const extrapolator_base_matrix& augment);

/**
* this is to complete it with other deducible variables
* @param e expression with known varable values INCLUDING ZEROS
* @return expression with additional deducted variables
*/
Valuable Complete(const Valuable& e);
* If possible, make the matrix consistent
* @return true if it is
* **/
bool Consistent(const vector_t& augment);

/**
* this is to complete it with other deducible variables
* @param e expression with known varable values INCLUDING ZEROS
* @return expression with additional deducted variables
*/
// template<template <class> class RawContainerT>
// Expression Complete(const RawContainerT<T>& data) {
// // search for the row
// auto it1 = this->begin1();
// for (; it1 != this->end1(); ++it1) {
// if (std::equal(data.begin(), data.end(), it1.begin())) {
// break; // found it1
// }
// }
// }
Valuable Complete(const Valuable& e);

/**
* Build matrix as column,raw,value
Expand All @@ -88,6 +84,7 @@ class Extrapolator
*/
operator Valuable() const;

// Corrected the signature to match the definition in Extrapolator.cpp
Valuable Factors(const Variable& row, const Variable& col, const Variable& val) const;

/**
Expand Down
Loading
Loading