Skip to content

Commit

Permalink
Merge branch 'codac2_dev' into codac2_ellipsoids
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonRohou committed Dec 16, 2024
2 parents 833559c + 4963fc0 commit b7444b5
Show file tree
Hide file tree
Showing 38 changed files with 1,118 additions and 122 deletions.
2 changes: 1 addition & 1 deletion examples/02_centered_form/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ int main()
2*x[2]*cos(x[2]*x[0])-sin(x[2]*x[1])
));

CtcInverse_<IntervalVector> ctc(f, {0.,0.});
CtcInverse_ ctc(f, IntervalVector::zero(2));
draw_while_paving({{0,2},{2,4},{0,10}}, ctc, 0.004);
}
104 changes: 96 additions & 8 deletions python/codac/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ def copy(self):
class Approx:

def __init__(self, x, eps = float_info.epsilon*10):
if isinstance(x, (float,Interval)):
if isinstance(x, (int,float)):
self.a = Approx_double(x,eps)
elif isinstance(x, (Interval)):
self.a = Approx_Interval(x,eps)
elif isinstance(x, (Vector)):
self.a = Approx_Vector(x,eps)
Expand All @@ -151,7 +153,8 @@ def __init__(self, x, eps = float_info.epsilon*10):
elif isinstance(x, (IntervalMatrix)):
self.a = Approx_IntervalMatrix(x,eps)
else:
codac_error("Approx: can only build Approx for: Interval, Vector, IntervalVector, Matrix, IntervalMatrix")
codac_error("Approx: can only build Approx for: \
double, Interval, Vector, IntervalVector, Matrix, IntervalMatrix")

def __eq__(self, x):
return self.a == x
Expand Down Expand Up @@ -227,10 +230,95 @@ def sivia(x,f,y,eps):
codac_error("sivia: can only compute sivia from scalar or vector functions")


def nonlinear_mapping(e,f):
class AnalyticTrajectory:

if isinstance(f.f, AnalyticFunction_Vector):
return nonlinear_mapping_(e, f.f)

else:
codac_error("nonlinear_mapping: can only compute from vector functions")
def __init__(self, f, t):
if isinstance(f, AnalyticFunction):
self.__init__(f.f,t)
elif isinstance(f, AnalyticFunction_Scalar):
self.traj = AnalyticTrajectory_Scalar(f,t)
elif isinstance(f, AnalyticFunction_Vector):
self.traj = AnalyticTrajectory_Vector(f,t)
else:
codac_error("AnalyticTrajectory: can only build this trajectory from an AnalyticFunction_[Scalar/Vector]")

# Methods from TrajectoryBase:

def size(self):
return self.traj.size()

def is_empty(self):
return self.traj.is_empty()

def tdomain(self):
return self.traj.tdomain()

def truncate_tdomain(self, new_tdomain):
return self.traj.truncate_tdomain(new_tdomain)

def codomain(self):
return self.traj.codomain()

def __call__(self, t):
return self.traj(t)

def nan_value(self):
return self.traj.nan_value()

def sampled(self, dt):
return SampledTrajectory(self.traj.sampled(dt))

def primitive(self, y0, t):
return SampledTrajectory(self.traj.primitive(y0, t))

# Methods from AnalyticTrajectory:
# none


class SampledTrajectory:

def __init__(self, m):
if isinstance(m, (SampledTrajectory_double,SampledTrajectory_Vector)):
self.traj = m
elif not isinstance(m,dict):
codac_error("SampledTrajectory: can only build this trajectory from a dictionary")
elif isinstance(next(iter(m.values())), (int,float)):
self.traj = SampledTrajectory_double(m)
elif isinstance(next(iter(m.values())), (Vector,list)):
self.traj = SampledTrajectory_Vector(m)
else:
codac_error("SampledTrajectory: can only build this trajectory from maps of scalar or vector values")

# Methods from TrajectoryBase:

def size(self):
return self.traj.size()

def is_empty(self):
return self.traj.is_empty()

def tdomain(self):
return self.traj.tdomain()

def truncate_tdomain(self, new_tdomain):
return self.traj.truncate_tdomain(new_tdomain)

def codomain(self):
return self.traj.codomain()

def __call__(self, t):
return self.traj(t)

def nan_value(self):
return self.traj.nan_value()

def sampled(self, *args):
return SampledTrajectory(self.traj.sampled(*args))

def primitive(self, y0, t):
return SampledTrajectory(self.traj.primitive(y0, t))

# Methods from SampledTrajectory:

def nb_samples(self):
return self.traj.nb_samples()
7 changes: 6 additions & 1 deletion python/src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
matrices/codac2_py_arithmetic_sub.cpp
matrices/codac2_py_arithmetic_mul.cpp
matrices/codac2_py_arithmetic_div.cpp
matrices/codac2_py_Inversion.cpp
matrices/codac2_py_inversion.cpp
matrices/codac2_py_Matrix.cpp
matrices/codac2_py_MatrixBase.h
matrices/codac2_py_MatrixBlock.h
Expand All @@ -84,6 +84,10 @@
separators/codac2_py_SepWrapper.cpp

tools/codac2_py_Approx.cpp

trajectory/codac2_py_AnalyticTrajectory.cpp
trajectory/codac2_py_SampledTrajectory.cpp
trajectory/codac2_py_TrajectoryBase.h
)

target_include_directories(_core
Expand All @@ -103,6 +107,7 @@
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/paver/
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/separators/
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/tools/
PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/trajectory/
)

target_link_libraries(_core
Expand Down
20 changes: 14 additions & 6 deletions python/src/core/codac2_py_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ void export_SepWrapper(py::module& m, py::class_<SepBase,pySep>& sep);
// tools
void export_Approx(py::module& m);

// trajectory
void export_AnalyticTrajectory(py::module& m);
void export_SampledTrajectory(py::module& m);


PYBIND11_MODULE(_core, m)
{
Expand All @@ -138,10 +142,10 @@ PYBIND11_MODULE(_core, m)
export_CtcIdentity(m, py_ctc_iv);
export_CtcInnerOuter(m, py_ctc_iv);
export_CtcInter(m, py_ctc_iv);
export_CtcInverse<Interval>(m,"CtcInverse_Interval",py_ctc_iv);
export_CtcInverse<IntervalVector>(m,"CtcInverse_IntervalVector",py_ctc_iv);
export_CtcInverseNotIn<Interval>(m,"CtcInverseNotIn_Interval",py_ctc_iv);
export_CtcInverseNotIn<IntervalVector>(m,"CtcInverseNotIn_IntervalVector",py_ctc_iv);
export_CtcInverse<double,Interval>(m,"CtcInverse_Interval",py_ctc_iv);
export_CtcInverse<Vector,IntervalVector>(m,"CtcInverse_IntervalVector",py_ctc_iv);
export_CtcInverseNotIn<double,Interval>(m,"CtcInverseNotIn_Interval",py_ctc_iv);
export_CtcInverseNotIn<Vector,IntervalVector>(m,"CtcInverseNotIn_IntervalVector",py_ctc_iv);
export_CtcLazy(m, py_ctc_iv);
export_CtcNot(m, py_ctc_iv);
export_CtcPolar(m, py_ctc_iv);
Expand Down Expand Up @@ -209,8 +213,8 @@ PYBIND11_MODULE(_core, m)
export_SepCtcBoundary(m,py_sep);
export_SepCtcPair(m,py_sep);
export_SepInter(m,py_sep);
export_SepInverse<Interval>(m,"SepInverse_Interval",py_sep);
export_SepInverse<IntervalVector>(m,"SepInverse_IntervalVector",py_sep);
export_SepInverse<double,Interval>(m,"SepInverse_Interval",py_sep);
export_SepInverse<Vector,IntervalVector>(m,"SepInverse_IntervalVector",py_sep);
export_SepNot(m,py_sep);
export_SepPolygon(m,py_sep);
export_SepProj(m,py_sep);
Expand All @@ -221,4 +225,8 @@ PYBIND11_MODULE(_core, m)
// tools
export_Approx(m);

// trajectory
export_AnalyticTrajectory(m);
export_SampledTrajectory(m);

}
12 changes: 6 additions & 6 deletions python/src/core/contractors/codac2_py_CtcInverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

template<typename T>
template<typename S,typename T>
void export_CtcInverse(py::module& m, const std::string& export_name, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& pyctc)
{
py::class_<CtcInverse_<T>> exported(m, export_name.c_str(), pyctc, CTCINVERSE_MAIN);

exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&, const T&, bool>(),
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&, const T&, bool>(),
"f"_a, "y"_a, "with_centered_form"_a = true,
CTCINVERSE_Y_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL_BOOL);
CTCINVERSE_Y_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL_BOOL);

if constexpr(std::is_same_v<T,IntervalVector>) // separators only associated with interval vectors
{
exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&, const pyCtcIntervalVector&, bool>(),
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&, const pyCtcIntervalVector&, bool>(),
"f"_a, "c"_a, "with_centered_form"_a = true,
CTCINVERSE_Y_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_C_REF_BOOL_BOOL);
CTCINVERSE_Y_CTCINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_C_REF_BOOL_BOOL);
}

exported
Expand All @@ -43,7 +43,7 @@ void export_CtcInverse(py::module& m, const std::string& export_name, py::class_
VOID_CTCINVERSE_Y_CONTRACT_X_REF_VARIADIC_CONST))

.def("function", &CtcInverse_<T>::function,
CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CTCINVERSE_Y_FUNCTION_CONST)
CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CTCINVERSE_Y_FUNCTION_CONST)

;
}
10 changes: 5 additions & 5 deletions python/src/core/contractors/codac2_py_CtcInverseNotIn.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

template<typename T>
template<typename S,typename T>
void export_CtcInverseNotIn(py::module& m, const std::string& export_name, py::class_<CtcBase<IntervalVector>,pyCtcIntervalVector>& pyctc)
{
py::class_<CtcInverseNotIn<T>> exported(m, export_name.c_str(), pyctc, CTCINVERSE_MAIN);

exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&, const T&, bool>(),
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&, const T&, bool>(),
"f"_a, "y"_a, "with_centered_form"_a = true,
CTCINVERSENOTIN_YX_CTCINVERSENOTIN_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL);
CTCINVERSENOTIN_YX_CTCINVERSENOTIN_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL);

if constexpr(std::is_same_v<T,IntervalVector>) // separators only associated with interval vectors
{
exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&, const pyCtcIntervalVector&, bool>(),
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&, const pyCtcIntervalVector&, bool>(),
"f"_a, "c"_a, "with_centered_form"_a = true,
CTCINVERSENOTIN_YX_CTCINVERSENOTIN_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_C_REF_BOOL);
CTCINVERSENOTIN_YX_CTCINVERSENOTIN_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_C_REF_BOOL);
}

exported
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
#include <pybind11/stl.h>
#include <codac2_Matrix.h>
#include <codac2_IntervalMatrix.h>
#include <codac2_Inversion.h>
#include <codac2_inversion.h>

#include "codac2_py_doc.h"
#include "codac2_py_Inversion_docs.h" // Generated file from Doxygen
#include "codac2_py_inversion_docs.h" // Generated file from Doxygen


using namespace std;
Expand Down
10 changes: 5 additions & 5 deletions python/src/core/separators/codac2_py_SepInverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

template<typename T>
template<typename S,typename T>
void export_SepInverse(py::module& m, const std::string& export_name, py::class_<SepBase,pySep>& pysep)
{
py::class_<SepInverse<T>> exported(m, export_name.c_str(), pysep, SEPINVERSE_MAIN);

exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&,const T&,bool>(),
SEPINVERSE_Y_SEPINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL,
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&,const T&,bool>(),
SEPINVERSE_Y_SEPINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_Y_REF_BOOL,
"f"_a, "y"_a, "with_centered_form"_a = true);

if constexpr(std::is_same_v<T,IntervalVector>) // separators only associated with interval vectors
{
exported
.def(py::init<const AnalyticFunction<OpValue<T,IntervalMatrix>>&,const pySep&,bool>(),
SEPINVERSE_Y_SEPINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_WRAPPER_Y_DOMAIN_REF_CONST_S_REF_BOOL,
.def(py::init<const AnalyticFunction<OpValue<S,T,IntervalMatrix>>&,const pySep&,bool>(),
SEPINVERSE_Y_SEPINVERSE_CONST_ANALYTICFUNCTION_TYPENAME_ARGWRAPPER_Y_DOMAIN_REF_CONST_S_REF_BOOL,
"f"_a, "s"_a, "with_centered_form"_a = true);
}

Expand Down
3 changes: 3 additions & 0 deletions python/src/core/tools/codac2_py_Approx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ void _export_Approx(py::module& m, const string& class_name)

void export_Approx(py::module& m)
{
_export_Approx<double>(m, "Approx_double");
_export_Approx<Interval>(m, "Approx_Interval");
_export_Approx<Vector>(m, "Approx_Vector");
_export_Approx<IntervalVector>(m, "Approx_IntervalVector");
_export_Approx<Row>(m, "Approx_Row");
_export_Approx<IntervalRow>(m, "Approx_IntervalRow");
_export_Approx<Matrix>(m, "Approx_Matrix");
_export_Approx<IntervalMatrix>(m, "Approx_IntervalMatrix");
}
45 changes: 45 additions & 0 deletions python/src/core/trajectory/codac2_py_AnalyticTrajectory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* AnalyticTrajectory Python binding
* ----------------------------------------------------------------------------
* \date 2024
* \author Simon Rohou
* \copyright Copyright 2024 Codac Team
* \license GNU Lesser General Public License (LGPL)
*/

#include <limits>
#include <sstream>
#include <pybind11/pybind11.h>
#include <pybind11/operators.h>
#include <codac2_AnalyticTrajectory.h>
#include <codac2_SampledTrajectory.h>
#include "codac2_py_AnalyticTrajectory_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
#include "codac2_py_TrajectoryBase_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
#include "codac2_py_TrajectoryBase.h"
#include "codac2_py_doc.h"

using namespace std;
using namespace codac2;
namespace py = pybind11;
using namespace pybind11::literals;

template<typename O>
void _export_AnalyticTrajectory(py::module& m, const string& class_name)
{
py::class_<AnalyticTrajectory<O>> exported_class(m, class_name.c_str(), ANALYTICTRAJECTORY_MAIN);
export_TrajectoryBase<AnalyticTrajectory<O>>(exported_class);

exported_class

.def(py::init<const AnalyticFunction<O>&,const Interval&>(),
ANALYTICTRAJECTORY_OS_ANALYTICTRAJECTORY_CONST_ANALYTICFUNCTION_O_REF_CONST_INTERVAL_REF,
"f"_a, "tdomain"_a)

;
}

void export_AnalyticTrajectory(py::module& m)
{
_export_AnalyticTrajectory<ScalarOpValue>(m, "AnalyticTrajectory_Scalar");
_export_AnalyticTrajectory<VectorOpValue>(m, "AnalyticTrajectory_Vector");
}
Loading

0 comments on commit b7444b5

Please sign in to comment.