Skip to content

Commit

Permalink
minimize copies in eigen to numpy Conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Sep 10, 2024
1 parent 05fd602 commit 229cd05
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions rednose/helpers/ekf_sym_pyx.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,13 @@ cdef extern from "rednose/helpers/ekf_sym.h" namespace "EKFS":

@cython.wraparound(False)
@cython.boundscheck(False)
cdef np.ndarray[np.float64_t, ndim=2, mode="c"] matrix_to_numpy(MatrixXdr arr):
cdef np.ndarray[np.float64_t, ndim=2, mode="c"] matrix_to_numpy(MatrixXdr &arr):
cdef double[:,:] mem_view = <double[:arr.rows(),:arr.cols()]>arr.data()
return np.copy(np.asarray(mem_view, dtype=np.double, order="C"))

@cython.wraparound(False)
@cython.boundscheck(False)
cdef np.ndarray[np.float64_t, ndim=1, mode="c"] vector_to_numpy(VectorXd arr):
cdef np.ndarray[np.float64_t, ndim=1, mode="c"] vector_to_numpy(VectorXd &arr):
cdef double[:] mem_view = <double[:arr.rows()]>arr.data()
return np.copy(np.asarray(mem_view, dtype=np.double, order="C"))

Expand Down Expand Up @@ -120,11 +120,13 @@ cdef class EKF_sym_pyx:
)

def state(self):
cdef np.ndarray res = vector_to_numpy(self.ekf.state())
cdef VectorXd state = self.ekf.state()
cdef np.ndarray res = vector_to_numpy(state)
return res

def covs(self):
return matrix_to_numpy(self.ekf.covs())
cdef MatrixXdr covs = self.ekf.covs()
return matrix_to_numpy(covs)

def set_filter_time(self, double t):
self.ekf.set_filter_time(t)
Expand Down Expand Up @@ -167,14 +169,15 @@ cdef class EKF_sym_pyx:
return None

cdef VectorXd tmpvec
cdef Estimate* est = &res.value()
return (
vector_to_numpy(res.value().xk1),
vector_to_numpy(res.value().xk),
matrix_to_numpy(res.value().Pk1),
matrix_to_numpy(res.value().Pk),
res.value().t,
res.value().kind,
[vector_to_numpy(tmpvec) for tmpvec in res.value().y],
vector_to_numpy(est.xk1),
vector_to_numpy(est.xk),
matrix_to_numpy(est.Pk1),
matrix_to_numpy(est.Pk),
est.t,
est.kind,
[vector_to_numpy(tmpvec) for tmpvec in est.y],
z, # TODO: take return values?
extra_args,
)
Expand Down

0 comments on commit 229cd05

Please sign in to comment.