-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
14 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 12 additions & 9 deletions
21
...ude/lbr_fri_ros2/damped_least_squares.hpp → lbr_fri_ros2/include/lbr_fri_ros2/lstsq.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
#ifndef LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ | ||
#define LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ | ||
#ifndef LBR_FRI_ROS2__LSTSQ_HPP_ | ||
#define LBR_FRI_ROS2__LSTSQ_HPP_ | ||
|
||
#include <algorithm> | ||
|
||
#include "eigen3/Eigen/Core" | ||
#include "eigen3/Eigen/SVD" | ||
|
||
namespace lbr_fri_ros2 { | ||
template <class MatT> | ||
Eigen::Matrix<typename MatT::Scalar, MatT::ColsAtCompileTime, MatT::RowsAtCompileTime> | ||
damped_least_squares(const MatT &mat, typename MatT::Scalar lambda = | ||
typename MatT::Scalar{2e-1}) // choose appropriately | ||
lstsq(const MatT &mat, | ||
typename MatT::Scalar lambda = typename MatT::Scalar{2e-1}) // choose appropriately | ||
{ | ||
typedef typename MatT::Scalar Scalar; | ||
auto svd = mat.jacobiSvd(Eigen::ComputeFullU | Eigen::ComputeFullV); | ||
const auto &singularValues = svd.singularValues(); | ||
Eigen::Matrix<Scalar, MatT::ColsAtCompileTime, MatT::RowsAtCompileTime> dampedSingularValuesInv( | ||
mat.cols(), mat.rows()); | ||
dampedSingularValuesInv.setZero(); | ||
for (unsigned int i = 0; i < singularValues.size(); ++i) { | ||
dampedSingularValuesInv(i, i) = | ||
singularValues(i) / (singularValues(i) * singularValues(i) + lambda * lambda); | ||
} | ||
std::for_each(singularValues.data(), singularValues.data() + singularValues.size(), | ||
[&, i = 0](Scalar &s)[mutable] { | ||
dampedSingularValuesInv(i, i) = s / (s * s + lambda * lambda); | ||
++i; | ||
}); | ||
return svd.matrixV() * dampedSingularValuesInv * svd.matrixU().adjoint(); | ||
} | ||
} // end of namespace lbr_fri_ros2 | ||
#endif // LBR_FRI_ROS2__DAMPED_LEAST_SQUARES_HPP_ | ||
#endif // LBR_FRI_ROS2__LSTSQ_HPP_ |