Eigen: convert Matrix3d rotation to Quaternion - c++

I'm trying to convert a Matrix3d rotation to a Quaternion<double>, but I got only weird compiler errors so far. The code I'm using is:
Quaternion<double> getQuaternionFromRotationMatrix(const Matrix3d& mat)
{
AngleAxisd aa;
aa = mat;
Quaternion<double> q = aa;// conversion error
return q;
}
And the compiler errors:
path/src/Utils.cpp: In function ‘Eigen::Quaternion<double> Utils::getQuaternionFromRotationMatrix(const Matrix3d&)’:
path/src/Utils.cpp:55:26: error: conversion from ‘Eigen::AngleAxisd {aka Eigen::AngleAxis<double>}’ to non-scalar type ‘Eigen::Quaternion<double>’ requested
In file included from /usr/local/include/eigen3/Eigen/Core:283:0,
from /usr/local/include/eigen3/Eigen/Dense:1,
from path/include/Utils.h:4,
from path/src/Utils.cpp:1:
/usr/local/include/eigen3/Eigen/src/Core/Assign.h: In member function ‘Derived& Eigen::DenseBase<Derived>::lazyAssign(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::Matrix<double, 3, 1>, Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>]’:
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:534:123: instantiated from ‘static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::run(Derived&, const OtherDerived&) [with Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>, OtherDerived = Eigen::Matrix<double, 3, 1>]’
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:574:89: instantiated from ‘Derived& Eigen::MatrixBase<Derived>::operator=(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::Matrix<double, 3, 1>, Derived = Eigen::Block<Eigen::Matrix<double, 4, 4>, 4, -0x00000000000000001, true, true>]’
path/src/Utils.cpp:34:20: instantiated from here
/usr/local/include/eigen3/Eigen/src/Core/Assign.h:504:3: error: static assertion failed: "YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES"
Does someone know how to convert between both representations?

The constructors from an AngleAxis or Matrix are explicit meaning you have to write the conversion as follow:
Matrix3f mat;
Quaternionf q(mat);
or
Quaternionf q;
q = mat;
Same for AngleAxis.

Related

Automatically convert sycl swizzle operation to vector

I have the following line of code in a sycl application under a parallel_for:
queue.submit(
[&map, &output](cl::sycl::handler& cgh)
{
auto d_image = output.get_access<access::mode::read_write>(cgh);
auto d_map_probabilities =
map.probabilities.get_access<access::mode::read>(cgh);
cgh.parallel_for<class CreateImage>(
map.probabilities.get_range(),
[=](id<3> work_position)
{
// ... Code!
float3 relative{};
float elevation =
atan2(relative.z(), length(relative.xy())) * 30.0f * M_2_PI_F;
}
}
);
For which i get the error:
error: no matching function for call to 'length'
atan2(relative.z(), length(relative.xy())) * 30.0f * M_2_PI_F;
^~~~~~
/opt/sycl/bin/../include/sycl/CL/sycl/builtins.hpp:1032:7: note: candidate template ignored: requirement 'detail::is_contained<sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>, sycl::detail::type_list<sycl::detail::type_list<float>, sycl::detail::type_list<sycl::vec<float, 1>, sycl::vec<float, 2>, sycl::vec<float, 3>, sycl::vec<float, 4>>>>::value' was not satisfied [with T = sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>]
float length(T p) __NOEXC {
^
/opt/sycl/bin/../include/sycl/CL/sycl/builtins.hpp:1039:8: note: candidate template ignored: requirement 'detail::is_contained<sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>, sycl::detail::type_list<sycl::detail::type_list<double>, sycl::detail::type_list<sycl::vec<double, 1>, sycl::vec<double, 2>, sycl::vec<double, 3>, sycl::vec<double, 4>>>>::value' was not satisfied [with T = sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>]
double length(T p) __NOEXC {
^
/opt/sycl/bin/../include/sycl/CL/sycl/builtins.hpp:1046:6: note: candidate template ignored: requirement 'detail::is_contained<sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>, sycl::detail::type_list<sycl::detail::type_list<sycl::detail::half_impl::half>, sycl::detail::type_list<sycl::vec<sycl::detail::half_impl::half, 1>, sycl::vec<sycl::detail::half_impl::half, 2>, sycl::vec<sycl::detail::half_impl::half, 3>, sycl::vec<sycl::detail::half_impl::half, 4>>>>::value' was not satisfied [with T = sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>]
half length(T p) __NOEXC {
^
Basically it comes down to this. The operation relative.xy() produces the type:
sycl::detail::SwizzleOp<sycl::vec<float, 3>, sycl::detail::GetOp<float>, sycl::detail::GetOp<float>, sycl::detail::GetOp, 0, 1>
which is not implicitly convertible to float2 which is required by the length function. It should be though, this was fine to do in opencl. This can be solved by the workaround:
float2 rel_part = relative.xy();
float elevation = atan2(relative.z(), length(rel_part)) * 30.0f * M_2_PI_F;
Or
float elevation = atan2(relative.z(), length(float2{relative.xy()})) * 30.0f * M_2_PI_F;
But this defeats the purpose of a nice swizzle definition. Is there a way around this?

Eigen error associated with triangularView of template

I have the following function that is throwing an error. I can isolate the problem to the line
A.template triangularView<Lower>().solveInPlace(MatrixXd::Identity(p,p)); // the problem line
But I am having trouble figuring out how what the problem is an how to fix it.
template <typename T, typename RNG>
inline void rInvWishRevCholesky_thread_inplace(Eigen::MatrixBase<T>& A,
const int v,
const Eigen::Ref<const Eigen::MatrixXd>& Psi,
RNG& rng){
int p = Psi.rows();
MatrixXd PsiInv = Psi.llt().solve(MatrixXd::Identity(p,p));
if (v <= p-1)
Rcpp::stop("v must be > Psi.rows - 1");
VectorXd z(p*(p-1)/2);
fillUnitNormal_thread(z, rng);
MatrixXd X = MatrixXd::Zero(p, p);
for (int i=0; i<p; i++){
boost::random::chi_squared_distribution<> rchisq(v-i);
X(i,i) = sqrt(rchisq(rng));
}
int pos = 0;
for (int i=1; i<p; i++){
for (int j=0; j<i; j++){
X(i,j) = z(pos);
pos++;
}
}
A.template noalias() = PsiInv.llt().matrixL()*X;
A.template triangularView<Lower>().solveInPlace(MatrixXd::Identity(p,p)); // the problem line
A.template transposeInPlace();
}
Here is the error message:
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:496:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/SolveTriangular.h: In instantiation of 'void Eigen::TriangularViewImpl<_MatrixType, _Mode, Eigen::Dense>::solveInPlace(const Eigen::MatrixBase<OtherDerived>&) const [with int Side = 1; OtherDerived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >; _MatrixType = Eigen::Matrix<double, -1, -1>; unsigned int _Mode = 1]':
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/TriangularMatrix.h:511:37: required from 'void Eigen::TriangularViewImpl<_MatrixType, _Mode, Eigen::Dense>::solveInPlace(const Eigen::MatrixBase<OtherDerived>&) const [with OtherDerived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >; _MatrixType = Eigen::Matrix<double, -1, -1>; unsigned int _Mode = 1]'
../inst/include/MatDist_thread.h:153:3: required from 'void rInvWishRevCholesky_thread_inplace(Eigen::MatrixBase<Derived>&, int, const Eigen::Ref<const Eigen::Matrix<double, -1, -1> >&, RNG&) [with T = Eigen::Matrix<double, -1, -1>; RNG = boost::random::mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>]'
MongrelCollapsed_Uncollapse.cpp:162:72: required from here
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/SolveTriangular.h:182:11: error: use of deleted function 'Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >& Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >::operator=(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >&)'
other = otherCopy;
~~~~~~^~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:463:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/CwiseNullaryOp.h:60:7: note: 'Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >& Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >::operator=(const Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >&)' is implicitly deleted because the default definition would be ill-formed:
class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
^~~~~~~~~~~~~~
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/CwiseNullaryOp.h:60:7: error: 'Eigen::internal::no_assignment_operator& Eigen::internal::no_assignment_operator::operator=(const Eigen::internal::no_assignment_operator&)' is private within this context
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:367:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/XprHelper.h:92:29: note: declared private here
no_assignment_operator& operator=(const no_assignment_operator&);
^~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:463:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/CwiseNullaryOp.h:60:7: error: passing 'const Eigen::internal::variable_if_dynamic<long int, -1>' as 'this' argument discards qualifiers [-fpermissive]
class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
^~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:367:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/XprHelper.h:115:28: note: in call to 'Eigen::internal::variable_if_dynamic<long int, -1>& Eigen::internal::variable_if_dynamic<long int, -1>::operator=(const Eigen::internal::variable_if_dynamic<long int, -1>&)'
template<typename T> class variable_if_dynamic<T, Dynamic>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:463:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/CwiseNullaryOp.h:60:7: error: passing 'const Eigen::internal::variable_if_dynamic<long int, -1>' as 'this' argument discards qualifiers [-fpermissive]
class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
^~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:367:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/util/XprHelper.h:115:28: note: in call to 'Eigen::internal::variable_if_dynamic<long int, -1>& Eigen::internal::variable_if_dynamic<long int, -1>::operator=(const Eigen::internal::variable_if_dynamic<long int, -1>&)'
template<typename T> class variable_if_dynamic<T, Dynamic>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:463:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/CwiseNullaryOp.h:60:7: error: passing 'const Eigen::internal::scalar_identity_op<double>' as 'this' argument discards qualifiers [-fpermissive]
class CwiseNullaryOp : public internal::dense_xpr_base< CwiseNullaryOp<NullaryOp, PlainObjectType> >::type, internal::no_assignment_operator
^~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:425:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/functors/NullaryFunctors.h:31:34: note: in call to 'Eigen::internal::scalar_identity_op<double>& Eigen::internal::scalar_identity_op<double>::operator=(const Eigen::internal::scalar_identity_op<double>&)'
template<typename Scalar> struct scalar_identity_op {
^~~~~~~~~~~~~~~~~~
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:496:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/SolveTriangular.h: In instantiation of 'static void Eigen::internal::triangular_solver_selector<Lhs, Rhs, Side, Mode, 0, -1>::run(const Lhs&, Rhs&) [with Lhs = Eigen::Matrix<double, -1, -1>; Rhs = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >; int Side = 1; int Mode = 1]':
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/SolveTriangular.h:179:21: required from 'void Eigen::TriangularViewImpl<_MatrixType, _Mode, Eigen::Dense>::solveInPlace(const Eigen::MatrixBase<OtherDerived>&) const [with int Side = 1; OtherDerived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >; _MatrixType = Eigen::Matrix<double, -1, -1>; unsigned int _Mode = 1]'
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/TriangularMatrix.h:511:37: required from 'void Eigen::TriangularViewImpl<_MatrixType, _Mode, Eigen::Dense>::solveInPlace(const Eigen::MatrixBase<OtherDerived>&) const [with OtherDerived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >; _MatrixType = Eigen::Matrix<double, -1, -1>; unsigned int _Mode = 1]'
../inst/include/MatDist_thread.h:153:3: required from 'void rInvWishRevCholesky_thread_inplace(Eigen::MatrixBase<Derived>&, int, const Eigen::Ref<const Eigen::Matrix<double, -1, -1> >&, RNG&) [with T = Eigen::Matrix<double, -1, -1>; RNG = boost::random::mersenne_twister_engine<unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>]'
MongrelCollapsed_Uncollapse.cpp:162:72: required from here
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/SolveTriangular.h:102:81: error: no matching function for call to 'Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >::coeffRef(int, int)'
::run(size, othersize, &actualLhs.coeffRef(0,0), actualLhs.outerStride(), &rhs.coeffRef(0,0), rhs.outerStride(), blocking);
In file included from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Core:434:0,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/Dense:1,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigenForward.h:30,
from /Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/RcppEigen.h:25,
from ../inst/include/MatrixAlgebra.h:4,
from ../inst/include/mongrel.h:17,
from MongrelCollapsed_Uncollapse.cpp:1:
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:273:10: note: candidate: void Eigen::DenseCoeffsBase<Derived, 0>::coeffRef() [with Derived = Eigen::CwiseNullaryOp<Eigen::internal::scalar_identity_op<double>, Eigen::Matrix<double, -1, -1> >]
void coeffRef();
^~~~~~~~
/Library/Frameworks/R.framework/Versions/3.5/Resources/library/RcppEigen/include/Eigen/src/Core/DenseCoeffsBase.h:273:10: note: candidate expects 0 arguments, 2 provided
make: *** [MongrelCollapsed_Uncollapse.o] Error 1
ERROR: compilation failed for package ‘mongrel’
* removing ‘/private/var/folders/x1/9_lpy_fs0kvg0p88td6_df200000gq/T/Rtmp7seVyi/devtools_install_1385330e7ebb0/mongrel’
Error: Command failed (1)
Any advice would be much appreciated!
solveInPlace requires its argument to be writable, MatrixXd::Identity(p,p) is a read-only expression. (Where would you expect to get the result from that function?)
Maybe what you want is this?
A.noalias() = (PsiInv.llt().matrixL()*X).transpose()
.template triangularView<Upper>().solve(MatrixXd::Identity(p,p));

When casting Eigen matrix type, error: expected primary-expression before ‘float’

template <typename T>
bool operator()(const T* parameters, T* residuals) const
{
Eigen::Matrix<T, 3, 1> pose(parameters[0],parameters[1],parameters[2]);
Eigen::Vector3f pose1 = pose.cast<float>();
Eigen::Affine2f transform = occ->getTransformForState(pose1); // transform: rotation->translation
Eigen::Vector3f tmp1 = occ->interpMapValueWithDerivatives( transform * currPoint);
Eigen::Matrix<T, 3, 1> transformedPointData(tmp1.cast<T>()); /// {M,dM/dx,dM/dy}
T funVal = T(1) - transformedPointData[0];
residuals[0] = funVal;
return true;
}
I have a template member function like above. During compilation, it reports
error: expected primary-expression before ‘float’
Eigen::Vector3f pose1 = pose.cast<float>();
I have to cast to type "float" to make it consistent with "getTransformForState" function's input and output.
I compared with other examples provided by Eigen Library but couldnt find anything wrong.
Any ideas are highly appreciated !
-------------------- update ------------------------
By changing as pose.template cast<float>()
The error is changed as:
/usr/include/eigen3/Eigen/src/Core/MathFunctions.h: In instantiation of ‘static NewType Eigen::internal::cast_impl<OldType, NewType>::run(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]’:
/usr/include/eigen3/Eigen/src/Core/MathFunctions.h:328:44: required from ‘NewType Eigen::internal::cast(const OldType&) [with OldType = ceres::Jet<double, 3>; NewType = float]’
/usr/include/eigen3/Eigen/src/Core/Functors.h:351:104: required from ‘const NewType Eigen::internal::scalar_cast_op<Scalar, NewType>::operator()(const Scalar&) const [with Scalar = ceres::Jet<double, 3>; NewType = float]’
/usr/include/eigen3/Eigen/src/Core/CwiseUnaryOp.h:114:75: required from ‘const Scalar Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::coeff(Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Index) const [with UnaryOp = Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>; XprType = const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1>; Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Scalar = float; Eigen::CwiseUnaryOpImpl<UnaryOp, XprType, Eigen::Dense>::Index = long int]’
/usr/include/eigen3/Eigen/src/Core/DenseCoeffsBase.h:495:33: required from ‘void Eigen::DenseCoeffsBase<Derived, 1>::copyCoeff(Eigen::DenseCoeffsBase<Derived, 1>::Index, const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; Derived = Eigen::Matrix<float, 3, 1>; Eigen::DenseCoeffsBase<Derived, 1>::Index = long int]’
/usr/include/eigen3/Eigen/src/Core/Assign.h:180:5: required from ‘static void Eigen::internal::assign_LinearTraversal_CompleteUnrolling<Derived1, Derived2, Index, Stop>::run(Derived1&, const Derived2&) [with Derived1 = Eigen::Matrix<float, 3, 1>; Derived2 = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; int Index = 0; int Stop = 3]’
/usr/include/eigen3/Eigen/src/Core/Assign.h:314:21: [ skipping 5 instantiation contexts, use -ftemplate-backtrace-limit=0 to disable ]
/usr/include/eigen3/Eigen/src/Core/Matrix.h:281:31: required from ‘Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Matrix(const Eigen::MatrixBase<OtherDerived>&) [with OtherDerived = Eigen::CwiseUnaryOp<Eigen::internal::scalar_cast_op<ceres::Jet<double, 3>, float>, const Eigen::Matrix<ceres::Jet<double, 3>, 3, 1, 0, 3, 1> >; _Scalar = float; int _Rows = 3; int _Cols = 1; int _Options = 0; int _MaxRows = 3; int _MaxCols = 1]’
The error message means the compiler doesn't know that pose.cast is a template. There are three basic options for what a class member .foo can be:
A value (e.g. a data member or an enum value)
A type name (e.g. something defined with typedef)
A template for one of the above.
In your case, pose is of type Eigen::Matrix<T, 3, 1>. The compiler doesn't yet know what Eigen::Matrix<T, 3, 1> looks like because it depends on what T is (someone could have specialized Eigen::Matrix differently for different types).
So when you access a member of an unknown class (as with pose.cast), the compiler assumes it's option #1 (a value). This leads to it parsing pose.cast < as the beginning of a less-than comparison. The next token (float) triggers an error because the compiler was expecting another value, not a type name.
The fix is to tell the compiler explicitly that .cast is a template:
Eigen::Vector3f pose1 = pose.template cast<float>();
(The fix for case #2 is to use the typename keyword to force interpretation as a type name.)

A^T*A sparse product, results Store in Dense Matrix / Eigen Lib

SparseMatrix<double> matA(rows, cols);
SparseMatrix<double> ATA(rows, cols);
...
ATA = (SparseMatrix<double>(matA.transpose()) * matA).pruned();
//.triangularView<Lower>();
If I want to store results to Dense matrix for ex. DATA = ..., it returns very strange error of pruned function.
smaller question:
If I use .triangularView<Lower>(); this store in the heap only n/2+n elements of (ATA OR DATA) in dynamic allocation?
LOG:
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h: In function 'void Eigen::internal::sparse_sparse_product_with_pruning_impl(const Lhs&, const Rhs&, ResultType&, const typename ResultType::RealScalar&) [with Lhs = Eigen::SparseMatrix<double>, Rhs = Eigen::SparseMatrix<double>, ResultType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, typename ResultType::RealScalar = double]':
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h:91:5: instantiated from 'static void Eigen::internal::sparse_sparse_product_with_pruning_selector<Lhs, Rhs, ResultType, 0, 0, 0>::run(const Lhs&, const Rhs&, ResultType&, const RealScalar&) [with Lhs = Eigen::SparseMatrix<double>, Rhs = Eigen::SparseMatrix<double>, ResultType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, Eigen::internal::sparse_sparse_product_with_pruning_selector<Lhs, Rhs, ResultType, 0, 0, 0>::RealScalar = double]'
jni/Eigen/src/SparseCore/SparseProduct.h:121:9: instantiated from 'void Eigen::SparseSparseProduct<Lhs, Rhs>::evalTo(Dest&) const [with Dest = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, LhsNested = Eigen::SparseMatrix<double>, RhsNested = const Eigen::SparseMatrix<double>&]'
jni/Eigen/src/Core/Assign.h:522:101: instantiated from 'static Derived& Eigen::internal::assign_selector<Derived, OtherDerived, false, false>::evalTo(ActualDerived&, const ActualOtherDerived&) [with ActualDerived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, ActualOtherDerived = Eigen::SparseSparseProduct<Eigen::SparseMatrix<double>, const Eigen::SparseMatrix<double>&>, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, OtherDerived = Eigen::SparseSparseProduct<Eigen::SparseMatrix<double>, const Eigen::SparseMatrix<double>&>]'
jni/Eigen/src/Core/Assign.h:571:98: instantiated from 'Derived& Eigen::MatrixBase<Derived>::operator=(const Eigen::EigenBase<OtherDerived>&) [with OtherDerived = Eigen::SparseSparseProduct<Eigen::SparseMatrix<double>, const Eigen::SparseMatrix<double>&>, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>]'
jni/Eigen/src/Core/PlainObjectBase.h:453:7: instantiated from 'Derived& Eigen::PlainObjectBase<Derived>::operator=(const Eigen::EigenBase<OtherDerived>&) [with OtherDerived = Eigen::SparseSparseProduct<Eigen::SparseMatrix<double>, const Eigen::SparseMatrix<double>&>, Derived = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>]'
jni/Eigen/src/Core/Matrix.h:184:35: instantiated from 'Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::operator=(const Eigen::EigenBase<OtherDerived>&) [with OtherDerived = Eigen::SparseSparseProduct<Eigen::SparseMatrix<double>, const Eigen::SparseMatrix<double>&>, _Scalar = double, int _Rows = -0x00000000000000001, int _Cols = -0x00000000000000001, int _Options = 0, int _MaxRows = -0x00000000000000001, int _MaxCols = -0x00000000000000001, Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>]'
jni/After.cpp:429:36: instantiated from here
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h:50:3: error: 'class Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>' has no member named 'reserve'
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h:69:5: error: 'class Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>' has no member named 'startVec'
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h:71:7: error: 'class Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>' has no member named 'insertBackByOuterInner'
jni/Eigen/src/SparseCore/SparseSparseProductWithPruning.h:73:3: error: 'class Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>' has no member named 'finalize'
make.exe: *** [obj/local/armeabi-v7a/objs/com_jp_algi_CoreC/After.o] Error 1
**** Build Finished ****
For future reference, it would be a lot easier for people to help you (and you to help yourself) if you would prepare a Minimal, Complete, and Verifiable example, which I did below. For me, it helped me understand what you tried to ask. For you, it would help you understand your problem better and maybe even solve it.
int main(int argc, char *argv[])
{
SparseMatrix<double> a(3,3), ata;
a.coeffRef(1,2) = 0.;
a.coeffRef(1,1) = 2.;
a.coeffRef(1,0) = 6.;
a.coeffRef(0,1) = 1.;
cout << a << endl;
ata = (a.transpose() * a).pruned();
cout << ata << endl;
ata = (SparseMatrix<double>(a.transpose()) * a).pruned();
cout << ata << endl;
MatrixXd dense = ata.toDense();
cout << dense << endl;
/*****************************************************/
// Everything works fine until this point
/*****************************************************/
//dense = (SparseMatrix<double>(a.transpose()) * a).pruned(); // Doesn't compile
// Would compile if written as the following line
dense = SparseMatrix<double>((SparseMatrix<double>(a.transpose()) * a).pruned()).toDense(); // Works
cout << dense << endl;
return 0;
}

map eigen::MatrixXf to an existing eigen::matrixXf

Sometime ago user ggael gave an answer to the
problem of mapping a eigen::vectorXf to an eigen::matrixXf.
Now, i need to do something similar, but to an existing matrix,
e.g i know i can:
for(int i=0;i<p;++i){
VectorXf vec=q.col(i);
/*q is a p**2 by n matrix*/
Map<MatrixXf> qi(vec.data(),p,p);
/*run function that uses qi to produce a scalare and store that scalar*/
}
but (it seems to me) it would be more efficient to
create qi once and for all outside the loop and then
use the same qi over and over again (is that right?)
Also, i wonder whether the intermediate step where
i map q.col(i) to vec is really necessary...
A recent answer proposes to do:
qi=Map<MatrixXd>(vec.data(),p,p);
but doing that yields:
In function ‘Eigen::VectorXi DepType(const MatrixXf&, const MatrixXf&, const int&)’:
DeC.cpp:279:34: error: no matching function for call to ‘Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >::Map(Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar*, int&, int&)’
DeC.cpp:279:34: note: candidates are:
/eigen/Eigen/src/Core/Map.h:179:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*, Eigen::Map<MatrixType, MapOptions, StrideType>::Index = long int]
/home/kaveh/Desktop/work/p1/geqw4/vi3/out/sp/ccode/eigen/Eigen/src/Core/Map.h:179:12: note: no known conversion for argument 1 from ‘Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar* {aka float*}’ to ‘double*’
/eigen/Eigen/src/Core/Map.h:166:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, Eigen::Map<MatrixType, MapOptions, StrideType>::Index, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*, Eigen::Map<MatrixType, MapOptions, StrideType>::Index = long int]
/eigen/Eigen/src/Core/Map.h:166:12: note: no known conversion for argument 1 from ‘Eigen::PlainObjectBase<Eigen::Matrix<float, -0x00000000000000001, 1> >::Scalar* {aka float*}’ to ‘double*’
/eigen/Eigen/src/Core/Map.h:154:12: note: Eigen::Map<MatrixType, MapOptions, StrideType>::Map(Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType, const StrideType&) [with PlainObjectType = Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001>, int MapOptions = 0, StrideType = Eigen::Stride<0, 0>, Eigen::Map<MatrixType, MapOptions, StrideType>::PointerArgType = double*]
/eigen/Eigen/src/Core/Map.h:154:12: note: candidate expects 2 arguments, 3 provided
/eigen/Eigen/src/Core/Map.h:119:79: note: Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >::Map(const Eigen::Map<Eigen::Matrix<double, -0x00000000000000001, -0x00000000000000001> >&)
/eigen/Eigen/src/Core/Map.h:119:79: note: candidate expects 1 argument, 3 provided
e.g. it seems Eigen thinks qi is a vector....:(
The following:
qi=Map<MatrixXd>(vec.data(),p,p);
means that you want to copy the coefficients of Map(vec.data(),p,p) into the matrix referenced by qi. However, what you actually want is to re-initialize the Map<> object. To do so you must call the constructor again using the construct new syntax of C++:
new (&qi) Map<MatrixXd>(vec.data(),p,p);
Moreover, I must say that a Map<> object is extremely lightweight: it is only one pointer and 2 integers that are statically allocated on the stack. So moving the declaration of Map<> qi outside the loop will have zero effect on the performance. On the other hand, note that you do not need to copy q.col(i) into a temporary buffer. If q is column major, you can directly do:
Map<MatrixXf> qi(q.col(i).data(),p,p);