Skip to content

Commit 85feefd

Browse files
authored
Merge pull request #372 from SimonRohou/codac2_dev
[py] added ** and ^ operators for Interval and SlicedTube<Interval>
2 parents ca1f6f4 + af99ee2 commit 85feefd

6 files changed

Lines changed: 82 additions & 28 deletions

File tree

python/src/codac2_py_deprecated.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* \file
3+
* Codac binding (core)
4+
* ----------------------------------------------------------------------------
5+
* \date 2026
6+
* \author Simon Rohou
7+
* \copyright Copyright 2026 Codac Team
8+
* \license GNU Lesser General Public License (LGPL)
9+
*/
10+
11+
#pragma once
12+
13+
#include <iostream>
14+
15+
inline void deprecated_xor()
16+
{
17+
std::cout
18+
<< "Operator '^' is intentionally disabled in Codac. Use pow(x,y), sqr(x), or '**' instead."
19+
<< std::endl;
20+
}

python/src/core/domains/interval/codac2_py_Interval.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@
1313
#include <pybind11/operators.h>
1414
#include <pybind11/stl.h>
1515
#include <codac2_Interval.h>
16+
#include <codac2_Interval_operations.h>
1617
#include "codac2_py_Interval_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
1718
#include "codac2_py_Interval_impl_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
19+
#include "codac2_py_Interval_operations_impl_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
1820
#include "codac2_py_matlab.h"
21+
#include "codac2_py_deprecated.h"
1922

2023
using namespace std;
2124
using namespace codac2;
@@ -262,6 +265,30 @@ py::class_<Interval> export_Interval(py::module& m)
262265
INTERVAL_REF_INTERVAL_OPERATORDIVEQ_CONST_INTERVAL_REF,
263266
"x"_a)
264267

268+
.def("__xor__", [](const Interval& x1, int x2) { deprecated_xor(); return codac2::pow(x1,x2); },
269+
INTERVAL_POW_CONST_INTERVAL_REF_INT,
270+
"n"_a)
271+
272+
.def("__xor__", [](const Interval& x1, double x2) { deprecated_xor(); return codac2::pow(x1,x2); },
273+
INTERVAL_POW_CONST_INTERVAL_REF_DOUBLE,
274+
"n"_a)
275+
276+
.def("__xor__", [](const Interval& x1, const Interval& x2) { deprecated_xor(); return codac2::pow(x1,x2); },
277+
INTERVAL_POW_CONST_INTERVAL_REF_CONST_INTERVAL_REF,
278+
"n"_a)
279+
280+
.def("__pow__", (Interval(*)(const Interval&,int)) &codac2::pow,
281+
INTERVAL_POW_CONST_INTERVAL_REF_INT,
282+
"n"_a)
283+
284+
.def("__pow__", (Interval(*)(const Interval&,double)) &codac2::pow,
285+
INTERVAL_POW_CONST_INTERVAL_REF_DOUBLE,
286+
"n"_a)
287+
288+
.def("__pow__", (Interval(*)(const Interval&,const Interval&)) &codac2::pow,
289+
INTERVAL_POW_CONST_INTERVAL_REF_CONST_INTERVAL_REF,
290+
"n"_a)
291+
265292
.def_static("empty", &Interval::empty,
266293
STATIC_INTERVAL_INTERVAL_EMPTY)
267294

python/src/core/domains/interval/codac2_py_IntervalMatrixBase.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,56 +72,49 @@ void export_IntervalMatrixBase(py::module& m, py::class_<S>& pyclass)
7272

7373
.def("min_rad", [](const S& x, const std::vector<Index_type>& among_indices)
7474
{
75-
return x.min_rad(
76-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices));
75+
return x.min_rad(matlab::convert_indices(among_indices));
7776
},
7877
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_DOUBLE_MIN_RAD_CONST_VECTOR_INDEX_REF_CONST,
7978
"among_indices"_a=std::vector<Index_type>())
8079

8180
.def("max_rad", [](const S& x, const std::vector<Index_type>& among_indices)
8281
{
83-
return x.max_rad(
84-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices));
82+
return x.max_rad(matlab::convert_indices(among_indices));
8583
},
8684
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_DOUBLE_MAX_RAD_CONST_VECTOR_INDEX_REF_CONST,
8785
"among_indices"_a=std::vector<Index_type>())
8886

8987
.def("min_diam", [](const S& x, const std::vector<Index_type>& among_indices)
9088
{
91-
return x.min_diam(
92-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices));
89+
return x.min_diam(matlab::convert_indices(among_indices));
9390
},
9491
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_DOUBLE_MIN_DIAM_CONST_VECTOR_INDEX_REF_CONST,
9592
"among_indices"_a=std::vector<Index_type>())
9693

9794
.def("max_diam", [](const S& x, const std::vector<Index_type>& among_indices)
9895
{
99-
return x.max_diam(
100-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices));
96+
return x.max_diam(matlab::convert_indices(among_indices));
10197
},
10298
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_DOUBLE_MAX_DIAM_CONST_VECTOR_INDEX_REF_CONST,
10399
"among_indices"_a=std::vector<Index_type>())
104100

105101
.def("min_diam_index", [](const S& x, const std::vector<Index_type>& among_indices)
106102
{
107-
return matlab::output_index(x.min_diam_index(
108-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices)));
103+
return matlab::output_index(x.min_diam_index(matlab::convert_indices(among_indices)));
109104
},
110105
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_INDEX_MIN_DIAM_INDEX_CONST_VECTOR_INDEX_REF_CONST,
111106
"among_indices"_a=std::vector<Index_type>())
112107

113108
.def("max_diam_index", [](const S& x, const std::vector<Index_type>& among_indices)
114109
{
115-
return matlab::output_index(x.max_diam_index(
116-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices)));
110+
return matlab::output_index(x.max_diam_index(matlab::convert_indices(among_indices)));
117111
},
118112
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_INDEX_MAX_DIAM_INDEX_CONST_VECTOR_INDEX_REF_CONST,
119113
"among_indices"_a=std::vector<Index_type>())
120114

121115
.def("extr_diam_index", [](const S& x, bool min, const std::vector<Index_type>& among_indices)
122116
{
123-
return matlab::output_index(x.extr_diam_index(min,
124-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices)));
117+
return matlab::output_index(x.extr_diam_index(min,matlab::convert_indices(among_indices)));
125118
},
126119
MATRIXBASE_ADDONS_INTERVALMATRIXBASE_INDEX_EXTR_DIAM_INDEX_BOOL_CONST_VECTOR_INDEX_REF_CONST,
127120
"min"_a, "among_indices"_a=std::vector<Index_type>())
@@ -196,8 +189,7 @@ void export_IntervalMatrixBase(py::module& m, py::class_<S>& pyclass)
196189
.def("bisect_largest",
197190
[](const S& x, double ratio, const std::vector<Index_type>& among_indices)
198191
{
199-
return x.bisect_largest(ratio,
200-
among_indices.empty() ? among_indices : matlab::convert_indices(among_indices));
192+
return x.bisect_largest(ratio,matlab::convert_indices(among_indices));
201193
},
202194
MATRIX_ADDONS_INTERVALMATRIXBASE_AUTO_BISECT_LARGEST_DOUBLE_CONST_VECTOR_INDEX_REF_CONST,
203195
"ratio"_a = 0.49, "among_indices"_a=std::vector<Index_type>())

python/src/core/domains/tube/codac2_py_SlicedTube_operations.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <codac2_TubeBase.h>
1616
#include "codac2_py_matlab.h"
1717
#include "codac2_py_SlicedTube_operations_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py)
18+
#include "codac2_py_deprecated.h"
1819

1920
using namespace codac2;
2021
namespace py = pybind11;
@@ -401,4 +402,23 @@ void export_SlicedTube_operations(
401402
py::is_operator());
402403

403404
bind_scalar_slicedtube_functions(m);
405+
406+
py_SlicedTube_Interval
407+
408+
.def("__xor__", [](const SlicedTube<Interval>& x1, int x2) { deprecated_xor(); return codac2::pow(x1,x2); },
409+
SLICEDTUBE_INTERVAL_POW_CONST_SLICEDTUBE_INTERVAL_REF_INT,
410+
"n"_a)
411+
412+
.def("__xor__", [](const SlicedTube<Interval>& x1, const Interval& x2) { deprecated_xor(); return codac2::pow(x1,x2); },
413+
SLICEDTUBE_INTERVAL_POW_CONST_SLICEDTUBE_INTERVAL_REF_CONST_INTERVAL_REF,
414+
"n"_a)
415+
416+
.def("__pow__", (SlicedTube<Interval> (*)(const SlicedTube<Interval>&,int)) &codac2::pow,
417+
SLICEDTUBE_INTERVAL_POW_CONST_SLICEDTUBE_INTERVAL_REF_INT,
418+
"n"_a)
419+
420+
.def("__pow__", (SlicedTube<Interval> (*)(const SlicedTube<Interval>&,const Interval&)) &codac2::pow,
421+
SLICEDTUBE_INTERVAL_POW_CONST_SLICEDTUBE_INTERVAL_REF_CONST_INTERVAL_REF,
422+
"n"_a)
423+
;
404424
}

python/src/core/functions/analytic/codac2_py_AnalyticExprWrapper.h

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,18 @@
1616
#include <pybind11/stl.h>
1717
#include "codac2_AnalyticExprWrapper.h"
1818
#include "codac2_py_matlab.h"
19-
#include "codac2_arith_add.h"
20-
#include "codac2_arith_sub.h"
21-
#include "codac2_arith_mul.h"
22-
#include "codac2_arith_div.h"
19+
#include <codac2_arith_add.h>
20+
#include <codac2_arith_sub.h>
21+
#include <codac2_arith_mul.h>
22+
#include <codac2_arith_div.h>
23+
#include <codac2_pow.h>
24+
#include "codac2_py_deprecated.h"
2325
#include "codac2_py_AnalyticExprWrapper_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py)
24-
#include "codac2_pow.h"
2526

2627
using namespace codac2;
2728
namespace py = pybind11;
2829
using namespace pybind11::literals;
2930

30-
inline void deprecated_xor()
31-
{
32-
std::cout
33-
<< "Operator '^' is intentionally disabled in Codac. Use pow(x,y), sqr(x), or '**' instead."
34-
<< std::endl;
35-
}
36-
3731
inline void export_ScalarExpr(py::module& m)
3832
{
3933
py::class_<ScalarExpr>

python/src/core/functions/analytic/codac2_py_analytic_variables.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "codac2_py_analytic_variables_docs.h" // Generated file from Doxygen XML (doxygen2docstring.py):
1717
#include "codac2_py_AnalyticExprWrapper.h"
1818
#include "codac2_py_matlab.h"
19+
#include "codac2_py_deprecated.h"
1920

2021
using namespace codac2;
2122
namespace py = pybind11;

0 commit comments

Comments
 (0)