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

Is there a way to create a "dynamic fixed size" matrix

Tags: None
(comma "," separated)
woodbird
Registered Member
Posts
4
Karma
0
OS
I know the subject sounds confusing and wired. The problem is that, I have a matrix whose size are only known at run-time. Therefore, I can only use MatrixXd, etc. However, it is known that, once it is created, its size will not change until it is destroyed. That is the matrix shall be allocated dynamically, but its size is fixed during its lifetime.

Is there a way to construct such a matrix? That is, I would like to have a sort of lock mechanism. After the creation of the matrix, I lock its size. And subsequent calls to resize() or assignment that may change the size of matrix shall raise an exception condition or fail an assertion in debug mode, unless it is unlock explicitly.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
I guess this is for debugging purpose. Then you may try to compile with -DEIGEN_NO_AUTOMATIC_RESIZING, see this page:
http://eigen.tuxfamily.org/dox-devel/To ... tives.html
woodbird
Registered Member
Posts
4
Karma
0
OS
Thanks for the hint. I actually would like the explicit resize method be disabled. Like a true fixed size matrix that the interface is there but has no effect
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Resizing a fixed size object with wrong trigger and assertion, just like this macro does for dynamic objects.
woodbird
Registered Member
Posts
4
Karma
0
OS
I think I have not express things, clear. Here is an example

Code: Select all
#include <iostream>
#include <Eigen/Dense>
int main ()
{
    Eigen::VectorXd A(100);
    A.resize(101); // This is what I want to disable
    A[100] = 100;
    std::cout << A[100] << std::endl;
}

Compile it with EIGEN_NO_AUTOMATIC_RESIZING does not make it an error or failed assertion.

I think it must sounds odd why I want to disable this explicit resize(Integer). So here is a little more context.

I am writing a library, which has a class that abstract a certain mathematical object. I use an Eigen::Matrix to store some elements. Let's say
Code: Select all
template <unsigned Dim>
class ParticleCollection
{
    public :

    ParticleCollection (std::size_t N) : elements_(N, Dim) {}

    Eigen::MatrixXd &states_mat () {return states_;}

    void resize (std::size_t N)
    {
        states_.resize(N, Dim);
        // Some other side effects
    }

    private :

    Eigen::MatrixXd states_;
};

I would like the user only be able to resize the matrix through MY resize() method. It will have some other side effects, so merely resize the states_ matrix will not do. One solution is clearly I don't provide user the write access to the whole MatrixXd object at all. Only allows them to manipulate it through wrapper elements accessors. But there is no restriction on what user can do with the values of states_ at all. So for example if the user want to assign to the a column of the matrix with another Eigen::VectorXd, it has either loop through my element accessors, or I provide some wrapper to make the things similar to the following happen
Code: Select all
particle.states_mat().col(0) = some_vector;

with

The only thing that I don't want them to do is resize it, because there are other things in the class must change when the states are resized.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
OK, at least EIGEN_NO_AUTOMATIC_RESIZING prevents from implicit resizing like:

particle.states_mat() = MatrixXf::Zero(1000,200);

Now want you need is to disable resize() for users, but you still need to be able to call it from ParticleCollection. The only way to do it is to add a new class, let's call it StaticMatrix, inheriting Eigen::Matrix<>. In this class you only have to declare and implement the constructors you need for you, import the assignment operators, and declare a private resize function.

Then in ParticleCollection, you will still be able to resize it by first casting it into a MatrixXf:

static_cast<MatrixXf&>(state_).resize(....);

Of course the users will still be able to do it, but that cannot be done by accident.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]