Skip to content
Merged
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
11 changes: 9 additions & 2 deletions doc/manual/manual/contractors/geometric/src.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,25 @@ def tests_CtcDist_manual(test):
# [ctcdist-1-beg]
x = IntervalVector([[2,5],[2,6],[0,0],[0,0],[1,3]])
c = CtcDist()
c.contract(x)
x = c.contract(x)
# x = [ [2, 2.23607] ; [2, 2.23607] ; <0, 0> ; <0, 0> ; [2.82842, 3] ]
# [ctcdist-1-end]

test.assertTrue(Approx(x,1e-5) == IntervalVector([[2,2.23607],[2,2.23607],[0],[0],[2.82842,3]]))


# [ctcdist-2-beg]
a1,a2,b1,b2,d = Interval(2,5),Interval(2,6),Interval(0),Interval(0),Interval(1,3)
c = CtcDist()
c.contract(a1,a2,b1,b2,d)
a1,a2,b1,b2,d = c.contract(a1,a2,b1,b2,d)
# a1 = [2, 2.23607] ; a2 = [2, 2.23607] ; b1 = <0, 0> ; b2 = <0, 0> ; d = [2.82842, 3]
# [ctcdist-2-end]

test.assertTrue(Approx(a1,1e-5) == Interval([2,2.23607]))
test.assertTrue(Approx(a2,1e-5) == Interval([2,2.23607]))
test.assertTrue(Approx(b1,1e-5) == Interval([0]))
test.assertTrue(Approx(b2,1e-5) == Interval([0]))
test.assertTrue(Approx(d,1e-5) == Interval([2.82842,3]))

# [ctcdist-3-beg]
b1, b2 = Vector([1,2]), Vector([3.6,2.4])
Expand Down
4 changes: 2 additions & 2 deletions python/src/core/contractors/codac2_py_Ctc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector> export_CtcIntervalVector
std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c1.copy()),
std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c2.copy()));
},
CTCINTER_TYPENAME_C1_CONTRACTEDTYPE_OPERATORAND_CONST_C1_REF_CONST_C2_REF)
CTCINTERTYPE_TYPENAMEC1_CONTRACTEDTYPES_CTC_OPERATORAND_CONST_C1_REF_CONST_C2_REF)

.def("__and__", [](const CtcBase<IntervalVector>& c1, const IntervalVector& x2)
{
Expand Down Expand Up @@ -79,7 +79,7 @@ py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector> export_CtcIntervalVector
std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c1.copy()),
std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c2.copy()));
},
CTCUNION_TYPENAME_C1_CONTRACTEDTYPE_OPERATOROR_CONST_C1_REF_CONST_C2_REF)
CTCUNIONTYPE_TYPENAMEC1_CONTRACTEDTYPES_CTC_OPERATOROR_CONST_C1_REF_CONST_C2_REF)

.def("__or__", [](const CtcBase<IntervalVector>& c1, const IntervalVector& x2)
{
Expand Down
7 changes: 6 additions & 1 deletion python/src/core/contractors/codac2_py_CtcDist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ void export_CtcDist(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcInter
.def(CONTRACT_BOX_METHOD(CtcDist,
VOID_CTCDIST_CONTRACT_INTERVALVECTOR_REF_CONST))

.def("contract", (void (CtcDist::*)(Interval&,Interval&,Interval&,Interval&,Interval&) const)&CtcDist::contract,
.def("contract",
[](const CtcDist& c, Interval& a1, Interval& a2, Interval& b1, Interval& b2, Interval& d)
{
c.contract(a1,a2,b1,b2,d);
return std::make_tuple(a1,a2,b1,b2,d);
},
VOID_CTCDIST_CONTRACT_INTERVAL_REF_INTERVAL_REF_INTERVAL_REF_INTERVAL_REF_INTERVAL_REF_CONST,
"a1"_a, "a2"_a, "b1"_a, "b2"_a, "d"_a)
;
Expand Down
4 changes: 2 additions & 2 deletions python/src/core/contractors/codac2_py_CtcInter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ void export_CtcInter(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcInte
"c1"_a, "c2"_a)

.def(CONTRACT_BOX_METHOD(CtcInter<IntervalVector>,
VOID_CTCINTER_X_CONTRACT_X_REF_CONST))
VOID_CTCINTER_X_CONTRACT_IMPL_X__REF_CONST))

.def("__iand__", [](CtcInter<IntervalVector>& c1, const CtcBase<IntervalVector>& c2)
{
c1 &= std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c2.copy());
return c1;
},
CTCINTER_X_REF_CTCINTER_X_OPERATORANDEQ_CONST_C_REF)
CTCINTER_X_VARIADIC_REF_CTCINTER_X_OPERATORANDEQ_CONST_SHARED_PTR_CTCBASE_X_VARIADIC_REF)
;
}
16 changes: 9 additions & 7 deletions python/src/core/contractors/codac2_py_CtcInverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ template<typename T>
void export_CtcInverse(py::module& m, const std::string& export_name, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& pyctc)
{
using D = typename T::Domain;
py::class_<CtcInverse<D>> exported(m, export_name.c_str(), pyctc, CTCINVERSE_MAIN);
py::class_<CtcInverse<D,IntervalVector>> exported(m, export_name.c_str(), pyctc, CTCINVERSE_MAIN);

exported
.def(py::init<const AnalyticFunction<T>&, const D&, bool>(),
"f"_a, "y"_a, "with_centered_form"_a = true,
CTCINVERSE_YX_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_EXPRTYPE_Y_TYPE_REF_CONST_TYPENAME_WRAPPER_Y_DOMAIN_REF_BOOL_BOOL);
CTCINVERSE_YX_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_EXPRTYPE_Y_TYPE_REF_CONST_Y_REF_BOOL_BOOL);

if constexpr(std::is_same_v<T,VectorType>) // contractors only associated with interval vectors
{
exported
.def(py::init(
[](const py::object& f, const CtcBase<IntervalVector>& c, bool with_centered_form)
{
return std::make_unique<CtcInverse<D>>(
return std::make_unique<CtcInverse<D,IntervalVector>>(
cast<AnalyticFunction<T>>(f),
c.copy(), with_centered_form);
}
Expand All @@ -45,13 +45,15 @@ void export_CtcInverse(py::module& m, const std::string& export_name, py::class_
"f"_a, "c"_a, "with_centered_form"_a = true);
}

using C = CtcInverse<D,IntervalVector>; // comma cannot be interpreted by the following macro

exported

.def(CONTRACT_BOX_METHOD(CtcInverse<D>,
VOID_CTCINVERSE__Y_CONTRACT_X_REF_VARIADIC_CONST))
.def(CONTRACT_BOX_METHOD(C,
VOID_CTCINVERSE_YX_CONTRACT_X_REF_VARIADIC_CONST))

.def("function", &CtcInverse<D>::function,
CONST_ANALYTICFUNCTION_TYPENAME_EXPRTYPE_Y_TYPE_REF_CTCINVERSE__Y_FUNCTION_CONST)
.def("function", &CtcInverse<D,IntervalVector>::function,
CONST_ANALYTICFUNCTION_TYPENAME_EXPRTYPE_Y_TYPE_REF_CTCINVERSE_YX_FUNCTION_CONST)

;
}
4 changes: 2 additions & 2 deletions python/src/core/contractors/codac2_py_CtcUnion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ void export_CtcUnion(py::module& m, py::class_<CtcBase<IntervalVector>,pyCtcInte
"c1"_a, "c2"_a)

.def(CONTRACT_BOX_METHOD(CtcUnion<IntervalVector>,
VOID_CTCUNION_X_CONTRACT_X_REF_CONST))
VOID_CTCUNION_X_CONTRACT_IMPL_X__REF_CONST))

.def("__ior__", [](CtcUnion<IntervalVector>& c1, const CtcBase<IntervalVector>& c2)
{
c1 |= std::dynamic_pointer_cast<CtcBase<IntervalVector>>(c2.copy());
return c1;
},
CTCUNION_X_REF_CTCUNION_X_OPERATOROREQ_CONST_C_REF)
CTCUNION_X_VARIADIC_REF_CTCUNION_X_OPERATOROREQ_CONST_SHARED_PTR_CTCBASE_X_VARIADIC_REF)
;
}
49 changes: 15 additions & 34 deletions src/core/contractors/codac2_Ctc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,12 @@

namespace codac2
{
//class Ctc
//{
// public:
//
// //virtual std::shared_ptr<Ctc> copy() const = 0;
//};
//template<typename... X>
//class Ctc
//{
// public:
//
// virtual std::shared_ptr<Ctc<X...>> copy() const
// {
// return nullptr;
// }
//
// virtual void contract(X&... x) const = 0;
//};

template<typename X>
class CtcBase// : virtual public Ctc<X_>
template<typename... X>
class CtcBase
{
public:

using ContractedType = X;
using ContractedTypes = std::tuple<X...>;

CtcBase(Index n)
: _n(n)
Expand All @@ -55,36 +36,36 @@ namespace codac2
return _n;
}

virtual void contract(X& x) const = 0;
virtual void contract(X&... x) const = 0;

virtual std::shared_ptr<CtcBase<X>> copy() const = 0;
virtual std::shared_ptr<CtcBase<X...>> copy() const = 0;

protected:

const Index _n;
};

template<typename C,typename X_>
class Ctc : public CtcBase<X_>
template<typename C,typename... X>
class Ctc : public CtcBase<X...>
{
public:

Ctc(Index n)
: CtcBase<X_>(n)
: CtcBase<X...>(n)
{ }

virtual std::shared_ptr<CtcBase<X_>> copy() const
virtual std::shared_ptr<CtcBase<X...>> copy() const
{
return std::make_shared<C>(*dynamic_cast<const C*>(this));
}
};

template<class C,class X>
concept IsCtcBaseOrPtr = (std::is_base_of_v<CtcBase<X>,C>
|| std::is_same_v<std::shared_ptr<CtcBase<X>>,C>);
template<class C,class... X>
concept IsCtcBaseOrPtr = (std::is_base_of_v<CtcBase<X...>,C>
|| std::is_same_v<std::shared_ptr<CtcBase<X...>>,C>);

template<class C,class X>
concept IsCtcBase = std::is_base_of_v<CtcBase<X>,C>;
template<class C,class... X>
concept IsCtcBase = std::is_base_of_v<CtcBase<X...>,C>;


template<typename C>
Expand Down
52 changes: 34 additions & 18 deletions src/core/contractors/codac2_CtcInter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,36 @@

namespace codac2
{
template<typename X=IntervalVector>
class CtcInter : public Ctc<CtcInter<X>,X>
template<typename... X>
class CtcInter : public Ctc<CtcInter<X...>,X...>
{
public:

explicit CtcInter(Index n)
: Ctc<CtcInter<X>,X>(n)
: Ctc<CtcInter<X...>,X...>(n)
{
if constexpr(std::is_same_v<X,Interval>)
if constexpr(std::is_same_v<X...,Interval>)
{
assert(n == 1);
}
}

template<typename C>
requires (IsCtcBaseOrPtr<C,X> && !std::is_same_v<CtcInter<X>,C>)
requires (IsCtcBaseOrPtr<C,X...> && !std::is_same_v<CtcInter<X...>,C>)
CtcInter(const C& c)
: Ctc<CtcInter<X>,X>(size_of(c)), _ctcs(c)
: Ctc<CtcInter<X...>,X...>(size_of(c)), _ctcs(c)
{ }

template<typename... C>
requires (IsCtcBaseOrPtr<C,X> && ...)
requires (IsCtcBaseOrPtr<C,X...> && ...)
CtcInter(const C&... c)
: Ctc<CtcInter<X>,X>(size_first_item(c...)), _ctcs(c...)
: Ctc<CtcInter<X...>,X...>(size_first_item(c...)), _ctcs(c...)
{
assert_release(all_same_size(c...));
}

void contract(X& x) const
template<typename X_> // single type
void contract_impl(X_& x) const
{
for(const auto& ci : _ctcs)
{
Expand All @@ -54,16 +55,22 @@ namespace codac2
}
}

void contract(X&... x) const
{
// contract_impl(..) method for multiple types is not yet implemented
contract_impl(x...);
}

template<typename C>
requires std::is_base_of_v<CtcBase<X>,C>
CtcInter<X>& operator&=(const C& c)
requires std::is_base_of_v<CtcBase<X...>,C>
CtcInter<X...>& operator&=(const C& c)
{
assert_release(c.size() == this->size());
_ctcs.push_object_back(c);
return *this;
}

CtcInter<X>& operator&=(const std::shared_ptr<CtcBase<X>>& c)
CtcInter<X...>& operator&=(const std::shared_ptr<CtcBase<X...>>& c)
{
assert_release(c->size() == this->size());
_ctcs.push_back(c);
Expand All @@ -72,16 +79,25 @@ namespace codac2

protected:

Collection<CtcBase<X>> _ctcs;
Collection<CtcBase<X...>> _ctcs;
};

template <>
class CtcInter<> : public CtcInter<IntervalVector>
{ };

template<typename Tuple>
struct CtcInterType;

template<typename... T>
struct CtcInterType<std::tuple<T...>> {
using Ctc = CtcInter<T...>;
};

template<typename C1, typename C2>
requires (std::is_same_v<typename C1::ContractedType,typename C2::ContractedType>
&& std::is_base_of_v<CtcBase<typename C1::ContractedType>,C1>
&& std::is_base_of_v<CtcBase<typename C1::ContractedType>,C2>)
inline CtcInter<typename C1::ContractedType> operator&(const C1& c1, const C2& c2)
typename CtcInterType<typename C1::ContractedTypes>::Ctc operator&(const C1& c1, const C2& c2)
{
return CtcInter<typename C1::ContractedType>(c1,c2);
return { c1, c2 };
}

template<typename C2>
Expand Down
Loading