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

Exception when trying to access a SparseMatrix<> row

Tags: None
(comma "," separated)
rmoraes
Registered Member
Posts
28
Karma
0
Hello everyone,

I am implementing an algorithm that requires vectors and row-vectors matrix multiplications. The vectors are extracted from a given matrix.

If try the following

Code: Select all
Eigen::SparseMatrix<double> matrix(2, 2);
matrix.col(0);


everything works fine. On the other hand, the following

Code: Select all
Eigen::SparseMatrix<double> matrix(2, 2);
matrix.row(0);


gives me an exception saying that

Program: ...
File: <path>/XprHelper.h
Line: 53

Expression: v == T(Value)


I wonder if it is somehow related to the sparse matrix internal storage order.

Shouldn't I be trying to access rows/columns of a sparse matrix using the the col() and row() methods? If that is the case, how should I access the matrix rows/columns to be later used in a matvec operation?

Thanks in advance!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
This should be very slow (1), but this should work... Which Eigen version are you using???

(1) the complexity to access to the row elements of a column major sparse matrix is O(n) .... In case you need to access to several rows, better to copy it to a row-major matrix first, or rethink your problem to avoid the need to access to the rows....
rmoraes
Registered Member
Posts
28
Karma
0
I am using version 3.2.4.

I might consider to rethink my problem. However, because I am solving a transposed system with the unknown vector left-multiplied instead of the more standard presentation which uses right-multiplication, and because Eigen has this nice feature of not transposing the matrix in place by only changing the way it is accessed, I should be able to use the pre-shipped Eigen solvers to solve my system of equations. But I really want to construct my linear system without having to change the matvec operations required to build it (because of encapsulation), hence some row-vector times matrix operations will be necessary and the functionality to access a row of a matrix is very appealing.

Thanks for warning me about the element-accessing issue. I will definitely consider that after the problem with the row() method is sorted out.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I cannot reproduce your issue. Do you have a self-contained example that trigger the assertion?
rmoraes
Registered Member
Posts
28
Karma
0
If I simply try the code snippet I provided in my first post I already have the exception.

Here is the "unit test" I run in order to observe the exception:

Code: Select all
using namespace System;
using namespace System::Text;
using namespace System::Collections::Generic;
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;

#include <vector>

// Base include
#include "Eigen/Sparse"

namespace Tests
{
   [TestClass]
   public ref class MatrixTests
   {
   #pragma region Test context
   private:
      TestContext^ testContextInstance;

   public:
      /// <summary>
      ///Gets or sets the test context which provides
      ///information about and functionality for the current test run.
      ///</summary>
      property Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ TestContext
      {
         Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ get()
         {
            return testContextInstance;
         }
         System::Void set(Microsoft::VisualStudio::TestTools::UnitTesting::TestContext^ value)
         {
            testContextInstance = value;
         }
      };
   #pragma endregion

        [TestMethod]
        void TestEigenRowAccess()
        {
            Eigen::SparseMatrix<double> matrix(2, 2);
            matrix.col(0); // Works fine
            matrix.row(0); // Assertion failure: Expression: v == T(value) in XprHelper.h, line 53
        }
   };
}


Perhaps something related with my development environment? The OS is Windows 7. I am developing my application using VS2010. The unit test above runs using the VS Unit Testing framework. I am using Eigen by basically including the header files (no CMAKE).
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
And what about the latest 3.2 branch which contains a few MSVC fixes: https://bitbucket.org/eigen/eigen/get/3.2.zip ???
rmoraes
Registered Member
Posts
28
Karma
0
Ok, it works now! Thanks a lot!

Out of curiosity, what type of fixes are needed to compile Eigen using msvc?


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], q.ignora, watchstar