Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Python slice computation #594

Merged
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
38 changes: 24 additions & 14 deletions jormungandr/cpp/autodiff/BindVariableBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ void BindVariableBlock(py::module_& autodiff,
int blockRows = self.Rows();
int blockCols = self.Cols();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;

// Row slice
const auto& rowElem = slices[0];
if (py::isinstance<py::slice>(rowElem)) {
const auto& rowSlice = rowElem.cast<py::slice>();
auto rowSlice = rowElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!rowSlice.compute(self.Rows(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand All @@ -92,7 +92,12 @@ void BindVariableBlock(py::module_& autodiff,
// Column slice
const auto& colElem = slices[1];
if (py::isinstance<py::slice>(colElem)) {
const auto& colSlice = colElem.cast<py::slice>();
auto colSlice = colElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!colSlice.compute(self.Cols(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand Down Expand Up @@ -172,15 +177,15 @@ void BindVariableBlock(py::module_& autodiff,
int blockRows = self.Rows();
int blockCols = self.Cols();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;

// Row slice
const auto& rowElem = slices[0];
if (py::isinstance<py::slice>(rowElem)) {
const auto& rowSlice = rowElem.cast<py::slice>();
auto rowSlice = rowElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!rowSlice.compute(self.Rows(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand All @@ -198,7 +203,12 @@ void BindVariableBlock(py::module_& autodiff,
// Column slice
const auto& colElem = slices[1];
if (py::isinstance<py::slice>(colElem)) {
const auto& colSlice = colElem.cast<py::slice>();
auto colSlice = colElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!colSlice.compute(self.Cols(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand Down
38 changes: 24 additions & 14 deletions jormungandr/cpp/autodiff/BindVariableMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ void BindVariableMatrix(py::module_& autodiff,
int blockRows = self.Rows();
int blockCols = self.Cols();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;

// Row slice
const auto& rowElem = slices[0];
if (py::isinstance<py::slice>(rowElem)) {
const auto& rowSlice = rowElem.cast<py::slice>();
auto rowSlice = rowElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!rowSlice.compute(self.Rows(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand All @@ -83,7 +83,12 @@ void BindVariableMatrix(py::module_& autodiff,
// Column slice
const auto& colElem = slices[1];
if (py::isinstance<py::slice>(colElem)) {
const auto& colSlice = colElem.cast<py::slice>();
auto colSlice = colElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!colSlice.compute(self.Cols(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand Down Expand Up @@ -163,15 +168,15 @@ void BindVariableMatrix(py::module_& autodiff,
int blockRows = self.Rows();
int blockCols = self.Cols();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;

// Row slice
const auto& rowElem = slices[0];
if (py::isinstance<py::slice>(rowElem)) {
const auto& rowSlice = rowElem.cast<py::slice>();
auto rowSlice = rowElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!rowSlice.compute(self.Rows(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand All @@ -189,7 +194,12 @@ void BindVariableMatrix(py::module_& autodiff,
// Column slice
const auto& colElem = slices[1];
if (py::isinstance<py::slice>(colElem)) {
const auto& colSlice = colElem.cast<py::slice>();
auto colSlice = colElem.cast<py::slice>();

size_t start;
size_t stop;
size_t step;
size_t sliceLength;
if (!colSlice.compute(self.Cols(), &start, &stop, &step,
&sliceLength)) {
throw py::error_already_set();
Expand Down
Loading