Skip to content

Commit

Permalink
[core] updated basic types Interval[Vector,Matrix]
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Feb 12, 2024
1 parent 946bfd0 commit e32242c
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 113 deletions.
37 changes: 22 additions & 15 deletions src/core/2/domains/codac2_Domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,42 @@
#ifndef __CODAC2_DOMAIN_H__
#define __CODAC2_DOMAIN_H__

#include <cstdio>
#include <vector>
#include <string>

namespace codac2
{
class DomainVolume;

{
class Domain
{
public:

virtual ~Domain() = default;
virtual DomainVolume dom_volume() const = 0;
};
Domain& operator=(const Domain& x)
{
_name = x._name;
return *this;
}

class DomainVolume : public std::vector<double>
{
public:
virtual ~Domain() = default;

DomainVolume(size_t n = 0) : std::vector<double>(n)
std::string name() const
{

try {
return _name.empty() ? "?" : _name;
}
catch(const std::exception& e) {
return "!";
};
}

bool update(const Domain& x)
void set_name(const std::string& name) const
{
DomainVolume _v = *this;
*this = x.dom_volume();
return _v != *this;
_name = name;
}

protected:

mutable std::string _name;
};

} // namespace codac
Expand Down
19 changes: 17 additions & 2 deletions src/core/2/domains/interval/codac2_Interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@ namespace codac2

}

size_t Interval::size() const
{
return 1;
}

double Interval::volume() const
{
return ibex::Interval::diam();
}

Interval operator""_i(long double x)
{
return Interval(x);
}

Interval sqr(const Interval& x)
{
return ibex::sqr(x);
Expand All @@ -78,7 +93,7 @@ namespace codac2
return ibex::pow(x,d);
}

Interval pow(const Interval &x, const Interval &y)
Interval pow(const Interval& x, const Interval& y)
{
return ibex::pow(x,y);
}
Expand Down Expand Up @@ -163,7 +178,7 @@ namespace codac2
return ibex::atanh(x);
}

Interval abs(const Interval &x)
Interval abs(const Interval& x)
{
return static_cast<Interval>(ibex::abs(static_cast<ibex::Interval>(x)));
}
Expand Down
16 changes: 13 additions & 3 deletions src/core/2/domains/interval/codac2_Interval.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
/**
* \file
*
* This class reuses many of the functions developed for ibex::Interval.
* The original IBEX code is encapsulated in Codac for allowing inheritance
* to Codac classes and also for documentation and independency purposes.
* See ibex::Interval (IBEX lib, author: G. Chabert)
*
* ----------------------------------------------------------------------------
* \date 2023
* \author Simon Rohou
Expand All @@ -13,13 +18,14 @@
#define __CODAC2_INTERVAL_H__

#include <array>
#include <codac2_Domain.h>
#include <ibex_Interval.h>

namespace codac2
{
const double oo = POS_INFINITY;

class Interval : public ibex::Interval
class Interval : public ibex::Interval, public Domain
{
public:

Expand All @@ -30,8 +36,12 @@ namespace codac2
Interval(std::array<double,2> array);
Interval(const Interval& x);
Interval(const ibex::Interval& x);
double volume() const;
size_t size() const;
};

Interval operator""_i(long double x);

/** \brief [x]^2 */
Interval sqr(const Interval& x);

Expand All @@ -45,7 +55,7 @@ namespace codac2
Interval pow(const Interval& x, double d);

/** \brief [x]^[y]. */
Interval pow(const Interval &x, const Interval &y);
Interval pow(const Interval& x, const Interval& y);

/** \brief n^{th} root of [x]. */
Interval root(const Interval& x, int n);
Expand Down Expand Up @@ -96,7 +106,7 @@ namespace codac2
Interval atanh(const Interval& x);

/** \brief \f$abs([x]) = \{|x|, x\in[x]\}.\f$. */
Interval abs(const Interval &x);
Interval abs(const Interval& x);

/** \brief Maximum of two intervals.
*
Expand Down
57 changes: 31 additions & 26 deletions src/core/2/domains/interval/codac2_IntervalMatrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ namespace codac2
: Eigen::Matrix<Interval,R,C>(other.template cast<Interval>())
{ }

IntervalMatrix_& operator=(const IntervalMatrix_<R,C>& other)
{
this->Eigen::Matrix<Interval,R,C>::operator=(other);
return *this;
}

// This method allows you to assign Eigen expressions to IntervalMatrix_
template<typename OtherDerived>
IntervalMatrix_& operator=(const Eigen::MatrixBase<OtherDerived>& other)
Expand Down Expand Up @@ -145,7 +151,7 @@ namespace codac2
return false;
}

static IntervalMatrix_<R,C> empty_set(size_t nb_rows = R, size_t nb_cols = C)
static auto empty_set(size_t nb_rows = R, size_t nb_cols = C)
{
return IntervalMatrix_<R,C>(nb_rows, nb_cols, Interval::empty_set());
}
Expand All @@ -155,6 +161,11 @@ namespace codac2
return Eigen::Matrix<Interval,R,C>::Identity();
}

auto diagonal_matrix() const
{
return this->diagonal().asDiagonal();
}

bool is_flat() const
{
if(is_empty()) return true;
Expand Down Expand Up @@ -266,14 +277,6 @@ namespace codac2
return false;
}

DomainVolume dom_volume() const
{
DomainVolume v(size());
for(size_t i = 0 ; i < size() ; i++)
v[i] = (this->data()+i)->diam();
return v;
}

double min_diam() const
{
return (this->data()+extr_diam_index(true))->diam();
Expand Down Expand Up @@ -370,7 +373,7 @@ namespace codac2
return selected_index;
}

auto bisect(float ratio = 0.49) const
std::pair<IntervalMatrix_<R,C>,IntervalMatrix_<R,C>> bisect(float ratio = 0.49) const
{
size_t i = largest_diam_index();
assert((this->data()+i)->is_bisectable());
Expand Down Expand Up @@ -453,15 +456,15 @@ namespace codac2
init(Interval::empty_set());
}

auto& inflate(double r)
IntervalMatrix_<R,C>& inflate(double r)
{
assert(r >= 0.);
for(size_t i = 0 ; i < this->size() ; i++)
(this->data()+i)->inflate(r);
return *this;
}

auto& inflate(const Matrix_<R,C>& r)
IntervalMatrix_<R,C>& inflate(const Matrix_<R,C>& r)
{
assert(r.minCoeff() >= 0.);
for(size_t i = 0 ; i < this->size() ; i++)
Expand All @@ -486,7 +489,7 @@ namespace codac2
return !(*this == x);
}

auto& operator&=(const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C>& operator&=(const IntervalMatrix_<R,C>& x)
{
if(!this->is_empty())
{
Expand All @@ -499,7 +502,7 @@ namespace codac2
return *this;
}

auto& operator|=(const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C>& operator|=(const IntervalMatrix_<R,C>& x)
{
if(!x.is_empty())
{
Expand All @@ -512,49 +515,49 @@ namespace codac2
return *this;
}

auto operator+(const IntervalMatrix_<R,C>& x) const
IntervalMatrix_<R,C> operator+(const IntervalMatrix_<R,C>& x) const
{
auto y = *this;
return y += x;
}

auto operator-(const IntervalMatrix_<R,C>& x) const
IntervalMatrix_<R,C> operator-(const IntervalMatrix_<R,C>& x) const
{
auto y = *this;
return y -= x;
}

auto operator&(const IntervalMatrix_<R,C>& x) const
IntervalMatrix_<R,C> operator&(const IntervalMatrix_<R,C>& x) const
{
auto y = *this;
return y &= x;
}

auto operator|(const IntervalMatrix_<R,C>& x) const
IntervalMatrix_<R,C> operator|(const IntervalMatrix_<R,C>& x) const
{
auto y = *this;
return y |= x;
}

auto& operator+=(const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C>& operator+=(const IntervalMatrix_<R,C>& x)
{
(*this).noalias() += x;//.template cast<Interval>();
return *this;
}

auto& operator-=(const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C>& operator-=(const IntervalMatrix_<R,C>& x)
{
(*this).noalias() -= x;//.template cast<Interval>();
return *this;
}

auto& operator+=(const Matrix_<R,C>& x)
IntervalMatrix_<R,C>& operator+=(const Matrix_<R,C>& x)
{
(*this).noalias() += x.template cast<Interval>();
return *this;
}

auto& operator-=(const Matrix_<R,C>& x)
IntervalMatrix_<R,C>& operator-=(const Matrix_<R,C>& x)
{
(*this).noalias() -= x;//.template cast<Interval>();
return *this;
Expand All @@ -579,20 +582,22 @@ namespace codac2
}

template<int R,int C>
auto operator-(const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C> operator-(const IntervalMatrix_<R,C>& x)
{
auto y = x;
y.init(0.);
return y -= x;
}

template<int R,int C>
auto operator*(double a, const IntervalMatrix_<R,C>& x)
IntervalMatrix_<R,C> operator*(double a, const IntervalMatrix_<R,C>& x)
{
return Interval(a)*x;
}

class IntervalMatrix : public IntervalMatrix_<>
using IntervalMatrix = IntervalMatrix_<>;

/*class IntervalMatrix : public IntervalMatrix_<>
{
public:
Expand Down Expand Up @@ -626,7 +631,7 @@ namespace codac2
{
return IntervalMatrix_<>::empty_set(nb_rows,nb_cols);
}
};
};*/

} // namespace codac

Expand Down
Loading

0 comments on commit e32242c

Please sign in to comment.