Skip to content

Commit

Permalink
[py] binding of NumPy arrays -> IntervalVector + union/inter of Inter…
Browse files Browse the repository at this point in the history
…valMatrix
  • Loading branch information
SimonRohou committed Jun 6, 2022
1 parent 9000cc6 commit c0e13ef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
10 changes: 7 additions & 3 deletions python/src/core/domains/interval/codac_py_IntervalMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ibex_Matrix.h"
#include "codac_Interval.h"
#include "codac_IntervalMatrix.h"
#include "codac_matrix_arithmetic.h"
// Generated file from Doxygen XML (doxygen2docstring.py):
// todo: #include "codac_py_IntervalMatrix_docs.h"

Expand Down Expand Up @@ -85,12 +86,12 @@ void export_IntervalMatrix(py::module& m)

// Some sanity checks...
if(info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");
throw std::runtime_error("Incompatible format: expected a double array");

if(info.ndim != 2)
throw std::runtime_error("Incompatible buffer dimension");
throw std::runtime_error("Incompatible buffer dimension");

ibex::Matrix m((int)info.shape[0], (int)info.shape[1], static_cast<double *>(info.ptr));
ibex::Matrix m((int)info.shape[0], (int)info.shape[1], static_cast<double*>(info.ptr));
return IntervalMatrix(m);
}))

Expand Down Expand Up @@ -134,5 +135,8 @@ void export_IntervalMatrix(py::module& m)
.def( "__mul__", [](IntervalMatrix& m, const Interval& x) { return x*m; })
.def("shape", [] (IntervalMatrix& o) { return make_tuple(o.nb_rows(), o.nb_cols()); })
.def("__repr__", [](const IntervalMatrix& x) { ostringstream str; str << x; return str.str(); })

.def("__or__", [](const IntervalMatrix& x, const IntervalMatrix& y) { return x|y; })
.def("__and__", [](const IntervalMatrix& x, const IntervalMatrix& y) { return x&y; })
;
};
26 changes: 24 additions & 2 deletions python/src/core/domains/interval/codac_py_IntervalVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,28 @@ void export_IntervalVector(py::module& m)
.def(py::init(&create_from_vector_of_intervals), "list"_a)
//.def("__init__", &create_from_tuple, "list"_a)

.def(py::init([](py::array_t<double> b) // create for instance an IntervalVector from a NumPy array
{
// Request a buffer descriptor from Python
py::buffer_info info = b.request();

// Some sanity checks...
if(info.format != py::format_descriptor<double>::format())
throw std::runtime_error("Incompatible format: expected a double array");

if(info.ndim == 1 || // e.g.: a=np.array([1,0,0])
(info.ndim == 2 && (int)info.shape[1] == 1)) // e.g.: a=np.array([[1],[0],[0]])
{
ibex::Vector m((int)info.shape[0], static_cast<double*>(info.ptr));
return IntervalVector(m);
}

else
throw std::runtime_error("Incompatible buffer dimension");

return IntervalVector(0);
}))

// Python vector methods

.def("__len__", &IntervalVector::size)
Expand Down Expand Up @@ -310,8 +332,8 @@ void export_IntervalVector(py::module& m)
// bwd_mul overloading
//bool (*bwd_mul_1) (const IntervalVector&, Interval&, IntervalVector&) = &ibex::bwd_mul;
//bool (*bwd_mul_2) (const Interval&, IntervalVector&, IntervalVector&) = &ibex::bwd_mul;
m.def( "bwd_mul", (bool (*) (const IntervalVector&, Interval&, IntervalVector&)) &ibex::bwd_mul);
m.def( "bwd_mul", (bool (*) (const Interval&, IntervalVector&, IntervalVector&)) &ibex::bwd_mul);
m.def("bwd_mul", (bool (*) (const IntervalVector&, Interval&, IntervalVector&)) &ibex::bwd_mul);
m.def("bwd_mul", (bool (*) (const Interval&, IntervalVector&, IntervalVector&)) &ibex::bwd_mul);

m.def("max", (IntervalVector(*) (const IntervalVector&, const IntervalVector&)) &max_IntevalVector);
};
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
${CMAKE_CURRENT_SOURCE_DIR}/arithmetic/codac_traj_arithmetic.h
${CMAKE_CURRENT_SOURCE_DIR}/arithmetic/codac_traj_arithmetic_scalar.cpp
${CMAKE_CURRENT_SOURCE_DIR}/arithmetic/codac_traj_arithmetic_vector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/arithmetic/codac_matrix_arithmetic.cpp
${CMAKE_CURRENT_SOURCE_DIR}/arithmetic/codac_matrix_arithmetic.h
${CMAKE_CURRENT_SOURCE_DIR}/graphics/codac_Figure.h
${CMAKE_CURRENT_SOURCE_DIR}/graphics/codac_Figure.cpp
${CMAKE_CURRENT_SOURCE_DIR}/graphics/vibes/vibes.h
Expand Down
5 changes: 1 addition & 4 deletions src/core/arithmetic/codac_tube_arithmetic.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
* \copyright Copyright 2021 Codac Team
* \license This program is distributed under the terms of
* the GNU Lesser General Public License (LGPL).
* \param x
* \return Tube output
*/
*/

#ifndef __CODAC_TUBE_ARITHMETIC_H__
#define __CODAC_TUBE_ARITHMETIC_H__
Expand Down

0 comments on commit c0e13ef

Please sign in to comment.