Reference to a Vector of Eigen Matrix #1056
Answered
by
wjakob
petrasvestartas
asked this question in
Q&A
-
How do you make a reference to a Vector of Eigen Matrix? For vectors I used this, but this does not work for vector of eigen matrices, values cannot be modified: from {{cookiecutter.project_slug}}._vectors_reference import DoubleVector
from {{cookiecutter.project_slug}}._vectors_reference import subtract_inplace
a = DoubleVector([1, 2, 3])
b = DoubleVector([4, 6, 8])
subtract_inplace(a, b)
for i in range(len(a)):
print(a[i]) #include "compas.h"
#include <nanobind/stl/bind_vector.h>
using DoubleVector = std::vector<double>;
void subtract_inplace(DoubleVector& a, const DoubleVector& b) {
for (size_t i = 0; i < a.size(); i++) {
a[i] = a[i] - b[i];
}
}
NB_MODULE(_vectors_reference, m) {
m.doc() = "Vectors by reference example.";
nb::bind_vector<DoubleVector>(m, "DoubleVector");
m.def("subtract_inplace", &subtract_inplace, "a"_a, "b"_a, "Subtract two vectors in place");
} |
Beta Was this translation helpful? Give feedback.
Answered by
wjakob
May 29, 2025
Replies: 1 comment 5 replies
-
Please look at the caveats of |
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Right, this is allowed. You can access a bound
std::vector<>
in C++ bindings and change the contents. But that same operation copies elements when done in Python. The rationale and a workaround (dangerous) is described in the reference documentation ofnb::bind_vector
.