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
14 changes: 7 additions & 7 deletions python/codac/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ def __init__(self, f, y, with_centered_form = True):
f_args = []
for a in f.args():
if a.size() == 1:
f_args.append(total_var[i])
f_args.append(total_var.get_item_0(i))
i = i+1
else:
f_args.append(total_var.subvector(i,i+a.size()-1))
f_args.append(total_var.subvector_0(i,i+a.size()-1))
i = i+a.size()

g = AnalyticFunction([total_var], f(*f_args))
Expand Down Expand Up @@ -156,9 +156,9 @@ def contract(self,*x):
for xi in x:
k = xi.size()
if k==1:
xi &= total[i]
xi &= total.get_item_0(i)
else:
xi &= total.subvector(i,i+k-1)
xi &= total.subvector_0(i,i+k-1)
i = i+k
return x

Expand All @@ -174,9 +174,9 @@ def contract_tube(self,*x):
for xi in x:
k = xi.size()
if k==1:
xi &= total[i]
xi &= total.get_item_0(i)
else:
xi &= total.subvector(i,i+k-1)
xi &= total.subvector_0(i,i+k-1)
i = i+k
return x

Expand Down Expand Up @@ -565,7 +565,7 @@ def fixpoint(contract, *x):
x = contract(x)

if not isinstance(x,tuple):
vol = x[0].volume()
vol = x.get_item_0(0).volume()
else:
vol = 0.0
for xi in x:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,11 @@ void export_ScalarVar(py::module& m)
py::implicitly_convertible<ScalarVar,ScalarExpr>();
}

ScalarExpr get_item(const VectorVar& v, Index_type i)
ScalarExpr get_item(const VectorVar& v, Index_type i, bool indexing_from_0)
{
matlab::test_integer(i);
i = matlab::input_index(i);
if(!indexing_from_0)
i = matlab::input_index(i);

if(i < 0 || i >= static_cast<Index>(v.size()))
throw py::index_error("index is out of range");
Expand Down Expand Up @@ -117,7 +118,12 @@ void export_VectorVar(py::module& m)
#endif
, [](const VectorVar& v, Index_type i) -> ScalarExpr
{
return get_item(v, i);
return get_item(v, i, !FOR_MATLAB);
}, ANALYTICEXPRWRAPPER_SCALARTYPE_VECTORVAR_OPERATORCOMPO_INDEX_CONST)

.def("get_item_0", [](const VectorVar& v, Index_type i) -> ScalarExpr
{
return get_item(v, i, true);
}, ANALYTICEXPRWRAPPER_SCALARTYPE_VECTORVAR_OPERATORCOMPO_INDEX_CONST)

.def("subvector", [](const VectorVar& v, Index_type i, Index_type j) -> VectorExpr
Expand All @@ -126,6 +132,12 @@ void export_VectorVar(py::module& m)
return v.subvector(matlab::input_index(i),matlab::input_index(j));
}, ANALYTICEXPRWRAPPER_VECTORTYPE_VECTORVAR_SUBVECTOR_INDEX_INDEX_CONST)

.def("subvector_0", [](const VectorVar& v, Index_type i, Index_type j) -> VectorExpr
{
matlab::test_integer(i, j);
return v.subvector(i,j);
}, ANALYTICEXPRWRAPPER_VECTORTYPE_VECTORVAR_SUBVECTOR_INDEX_INDEX_CONST)

.def("__pos__", [](const VectorVar& e1) { return e1; }, py::is_operator())
.def("__add__", [](const VectorVar& e1, const VectorVar& e2) { return e1 + e2; }, py::is_operator())
.def("__add__", [](const VectorVar& e1, const IntervalVector& e2) { return e1 + e2; }, py::is_operator())
Expand Down
35 changes: 29 additions & 6 deletions python/src/core/matrices/codac2_py_VectorBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,18 @@ void export_VectorBase([[maybe_unused]] py::module& m, py::class_<S>& pyclass)
"__getitem__"
#endif
,
[](const S& x, Index_type index) -> const T&
[](const S& x, Index_type i) -> const T&
{
matlab::test_integer(index);
return x[matlab::input_index(index)];
matlab::test_integer(i);
return x[matlab::input_index(i)];
}, py::return_value_policy::reference_internal,
MATRIX_ADDONS_VECTORBASE_CONST_SCALAR_REF_OPERATORCOMPO_INDEX_CONST)

.def("get_item_0",
[](const S& x, Index_type i) -> const T&
{
matlab::test_integer(i);
return x[i];
}, py::return_value_policy::reference_internal,
MATRIX_ADDONS_VECTORBASE_CONST_SCALAR_REF_OPERATORCOMPO_INDEX_CONST)

Expand All @@ -59,10 +67,17 @@ void export_VectorBase([[maybe_unused]] py::module& m, py::class_<S>& pyclass)
#else
"__setitem__"
#endif
, [](S& x, Index_type index, const T& a)
, [](S& x, Index_type i, const T& a)
{
matlab::test_integer(i);
x[matlab::input_index(i)] = a;
},
MATRIX_ADDONS_VECTORBASE_SCALAR_REF_OPERATORCOMPO_INDEX)

.def("set_item_0", [](S& x, Index_type i, const T& a)
{
matlab::test_integer(index);
x[matlab::input_index(index)] = a;
matlab::test_integer(i);
x[i] = a;
},
MATRIX_ADDONS_VECTORBASE_SCALAR_REF_OPERATORCOMPO_INDEX)

Expand All @@ -74,6 +89,14 @@ void export_VectorBase([[maybe_unused]] py::module& m, py::class_<S>& pyclass)
MATRIXBASE_ADDONS_VECTORBASE_AUTO_SUBVECTOR_INDEX_INDEX_CONST,
"start_id"_a, "end_id"_a)

.def("subvector_0", [](const S& x, Index_type start_id, Index_type end_id) -> S
{
matlab::test_integer(start_id, end_id);
return x.subvector(start_id, end_id);
},
MATRIXBASE_ADDONS_VECTORBASE_AUTO_SUBVECTOR_INDEX_INDEX_CONST,
"start_id"_a, "end_id"_a)

.def("resize", [](S& x, Index_type n)
{
matlab::test_integer(n);
Expand Down
Loading