This forum has been archived. All content is frozen. Please use KDE Discuss instead.

taking array() of SparseMatrix::diagonal()

Tags: None
(comma "," separated)
junsaito
Registered Member
Posts
7
Karma
0
Hi,

Is there a good reason why taking array() of SparseMatrix::diagonal() does not work?

Code: Select all
  Matrix3f M = Matrix3f(Vector3f::Ones().asDiagonal());
  cout << M.diagonal() << endl;
  // OK
  cout << M.diagonal().array() << endl;

  SparseMatrix<float> spM = SparseMatrix<float>(Vector3f::Ones().asDiagonal());
  cout << spM.diagonal() << endl;
  // NOT OK
  cout << spM.diagonal().array() << endl;


At the moment the compilation of this code gives the warning "returning reference to temporary [enabled by default]".
The executed result from last cout also is corrupt.

I can get around it by copying the diagonal to a vector first, but just wondering why there is a limitation here.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Works for me with 3.3 or 3.2, and gcc or clang:

Code: Select all
#include <iostream>
#include <Eigen/Sparse>
using namespace Eigen;
using namespace std;

int main()
{
  Matrix3f M = Matrix3f(Vector3f::Ones().asDiagonal());
  cout << M.diagonal() << endl;
  cout << M.diagonal().array() << endl;

  SparseMatrix<float> spM = M.sparseView();
  cout << spM.diagonal() << endl;
  cout << spM.diagonal().array() << endl;
}
junsaito
Registered Member
Posts
7
Karma
0
OK, perhaps this is a compiler-dependent problem.
I am on Ubuntu 12.04 using gcc 4.6.3 and Eigen 3.3.2. I had the same problem in Eigen 3.3.1.
I will check on OSX as well.
junsaito
Registered Member
Posts
7
Karma
0
Just in case, here is the warning message I get when compiling. I hope this might give you a hint.

In file included from ../Eigen/Eigen/Core:414:0,
from ../Eigen/Eigen/SparseCore:11,
from ../Eigen/Eigen/Sparse:26,
from main.cpp:2:
../Eigen/Eigen/src/Core/CoreEvaluators.h: In member function 'Eigen::internal::evaluator_wrapper_base<XprType>::CoeffReturnType Eigen::internal::evaluator_wrapper_base<XprType>::coeff(Eigen::Index) const [with XprType = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Eigen::internal::evaluator_wrapper_base<XprType>::CoeffReturnType = const float&, Eigen::Index = long int]':
../Eigen/Eigen/src/Core/AssignEvaluator.h:637:5: instantiated from 'void Eigen::internal::generic_dense_assignment_kernel<DstEvaluatorTypeT, SrcEvaluatorTypeT, Functor, Version>::assignCoeff(Eigen::Index) [with DstEvaluatorTypeT = Eigen::internal::evaluator<Eigen::Array<float, -0x00000000000000001, 1> >, SrcEvaluatorTypeT = Eigen::internal::evaluator<Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> > >, Functor = Eigen::internal::assign_op<float, float>, int Version = 0, Eigen::Index = long int]'
../Eigen/Eigen/src/Core/AssignEvaluator.h:497:7: instantiated from 'static void Eigen::internal::dense_assignment_loop<Kernel, 1, 0>::run(Kernel&) [with Kernel = Eigen::internal::generic_dense_assignment_kernel<Eigen::internal::evaluator<Eigen::Array<float, -0x00000000000000001, 1> >, Eigen::internal::evaluator<Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> > >, Eigen::internal::assign_op<float, float>, 0>]'
../Eigen/Eigen/src/Core/AssignEvaluator.h:724:3: instantiated from 'void Eigen::internal::call_dense_assignment_loop(DstXprType&, const SrcXprType&, const Functor&) [with DstXprType = Eigen::Array<float, -0x00000000000000001, 1>, SrcXprType = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Functor = Eigen::internal::assign_op<float, float>]'
../Eigen/Eigen/src/Core/AssignEvaluator.h:862:5: instantiated from 'static void Eigen::internal::Assignment<DstXprType, SrcXprType, Functor, Eigen::internal::Dense2Dense, Weak>::run(DstXprType&, const SrcXprType&, const Functor&) [with DstXprType = Eigen::Array<float, -0x00000000000000001, 1>, SrcXprType = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Functor = Eigen::internal::assign_op<float, float>, Weak = void]'
../Eigen/Eigen/src/Core/AssignEvaluator.h:819:3: instantiated from 'void Eigen::internal::call_assignment_no_alias(Dst&, const Src&, const Func&) [with Dst = Eigen::Array<float, -0x00000000000000001, 1>, Src = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Func = Eigen::internal::assign_op<float, float>]'
../Eigen/Eigen/src/Core/PlainObjectBase.h:728:7: instantiated from 'Derived& Eigen::PlainObjectBase<Derived>::_set_noalias(const Eigen::DenseBase<OtherDerived>&) [with OtherDerived = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Derived = Eigen::Array<float, -0x00000000000000001, 1>]'
../Eigen/Eigen/src/Core/PlainObjectBase.h:812:7: instantiated from 'void Eigen::PlainObjectBase<Derived>::_init1(const Eigen::DenseBase<ElseDerived>&) [with T = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, OtherDerived = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Derived = Eigen::Array<float, -0x00000000000000001, 1>]'
../Eigen/Eigen/src/Core/Array.h:173:7: instantiated from 'Eigen::Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Array(const T&) [with T = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, _Scalar = float, int _Rows = -0x00000000000000001, int _Cols = 1, int _Options = 0, int _MaxRows = -0x00000000000000001, int _MaxCols = 1]'
../Eigen/Eigen/src/Core/DenseBase.h:406:62: instantiated from 'Eigen::DenseBase<Derived>::EvalReturnType Eigen::DenseBase<Derived>::eval() const [with Derived = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, Eigen::DenseBase<Derived>::EvalReturnType = const Eigen::Array<float, -0x00000000000000001, 1>]'
../Eigen/Eigen/src/Core/IO.h:234:69: instantiated from 'std::ostream& Eigen::operator<<(std::ostream&, const Eigen::DenseBase<Derived>&) [with Derived = Eigen::ArrayWrapper<Eigen::Diagonal<Eigen::SparseMatrix<float>, 0> >, std::ostream = std::basic_ostream<char>]'
main.cpp:14:32: instantiated from here
../Eigen/Eigen/src/Core/CoreEvaluators.h:1340:33: warning: returning reference to temporary [enabled by default]
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
ah, that warning. Fixed there: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1381 will be part of 3.3.3
junsaito
Registered Member
Posts
7
Karma
0
OK, good to know it is already taken care of. Thanks!


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]