-
Notifications
You must be signed in to change notification settings - Fork 21
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
DM-14740: Stop using ndarray::EigenView indirectly in C++ code #360
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -122,8 +122,8 @@ class LeastSquares { | |
// the weird C++ syntax required for calling a templated member function | ||
// called "cast" in this context; see | ||
// http://eigen.tuxfamily.org/dox-devel/TopicTemplateKeyword.html | ||
_getDesignMatrix() = design.asEigen().template cast<double>(); | ||
_getDataVector() = data.asEigen().template cast<double>(); | ||
_getDesignMatrix() = ndarray::asEigenMatrix(design).template cast<double>(); | ||
_getDataVector() = ndarray::asEigenMatrix(data).template cast<double>(); | ||
_factor(false); | ||
} | ||
|
||
|
@@ -138,7 +138,7 @@ class LeastSquares { | |
/// Reset the design matrix given as an ndarray; dimension and data are not changed. | ||
template <typename T1, int C1> | ||
void setDesignMatrix(ndarray::Array<T1, 2, C1> const& design) { | ||
_getDesignMatrix() = design.asEigen().template cast<double>(); | ||
_getDesignMatrix() = ndarray::asEigenMatrix(design).template cast<double>(); | ||
_factor(false); | ||
} | ||
|
||
|
@@ -173,11 +173,11 @@ class LeastSquares { | |
template <typename T1, typename T2, int C1, int C2> | ||
void setNormalEquations(ndarray::Array<T1, 2, C1> const& fisher, ndarray::Array<T2, 1, C2> const& rhs) { | ||
if ((C1 > 0) == bool(Eigen::MatrixXd::IsRowMajor)) { | ||
_getFisherMatrix() = fisher.asEigen().template cast<double>(); | ||
_getFisherMatrix() = ndarray::asEigenMatrix(fisher).template cast<double>(); | ||
} else { | ||
_getFisherMatrix() = fisher.asEigen().transpose().template cast<double>(); | ||
_getFisherMatrix() = ndarray::asEigenMatrix(fisher).transpose().template cast<double>(); | ||
} | ||
_getRhsVector() = rhs.asEigen().template cast<double>(); | ||
_getRhsVector() = ndarray::asEigenMatrix(rhs).template cast<double>(); | ||
_factor(true); | ||
} | ||
|
||
|
@@ -223,7 +223,7 @@ class LeastSquares { | |
* by future calls to LeastSquares member functions, so it's best to promptly copy the | ||
* result elsewhere. | ||
* | ||
* If you want an Eigen object instead, just use getSolution().asEigen(). | ||
* If you want an Eigen object instead, just use ndarray::asEigenMatrix(getSolution()). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 for updating the documentation. |
||
* | ||
* The solution is cached the first time this member function is called, and will be | ||
* reused unless the matrices are reset or the threshold is changed. | ||
|
@@ -237,7 +237,7 @@ class LeastSquares { | |
* by future calls to LeastSquares member functions, so it's best to promptly copy the | ||
* result elsewhere. | ||
* | ||
* If you want an Eigen object instead, just use getCovariance().asEigen(). | ||
* If you want an Eigen object instead, just use ndarray::asEigenMatrix(getCovariance()). | ||
* | ||
* The covariance is cached the first time this member function is called, and will be | ||
* reused unless the matrices are reset or the threshold is changed. | ||
|
@@ -254,7 +254,7 @@ class LeastSquares { | |
* by future calls to LeastSquares member functions, so it's best to promptly copy the | ||
* result elsewhere. | ||
* | ||
* If you want an Eigen object instead, just use getFisherMatrix().asEigen(). | ||
* If you want an Eigen object instead, just use ndarray::asEigenMatrix(getFisherMatrix()). | ||
*/ | ||
ndarray::Array<double const, 2, 2> getFisherMatrix(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -140,7 +140,7 @@ std::shared_ptr<GaussianPsf::Image> GaussianPsf::doComputeKernelImage(lsst::geom | |
sum += row[xIndex] = std::exp(-0.5 * (x * x + y * y) / (_sigma * _sigma)); | ||
} | ||
} | ||
array.asEigen() /= sum; | ||
ndarray::asEigenMatrix(array) /= sum; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks a bit weird. How is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, @TallJimbo may be able to explain it better, but I believe the fact that it is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. |
||
return r; | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if
design
anddata
are temporaries? Or iscast<double>
guaranteed to make a defensive copy (which would also explain how the parameters can beconst
)? I looked up the documentation and it didn't say anything about object independence.Same question about other methods that use this implementation pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope @TallJimbo will say more, but my understanding is that this makes a copy because the target is a (reference to) an
Eigen::MatrixXd
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The assignment operation is where the (only) copy is made. The call to
cast
is safe only because temporaries returned byasEigenMatrix
are not destroyed until the end of their statements (see e.g. https://stackoverflow.com/questions/3041249/when-are-temporaries-created-as-part-of-a-function-call-destroyed).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, ok, I didn't realize the assignment copied the data. Maybe I've spent too long staring at
ndarray
andafw::image
... 🙃