tryingtosolve
tryingtosolve

Reputation: 803

C++ Eigen: Passing a column of a matrix into a function by reference

I have a matrix in C++ created using Eigen and I would like to pass a column of that matrix into a function by reference, which I hope will modify that column without creating anything new.

This is my sample code

int changeTwo(Eigen::Ref<Eigen::ArrayXd> f) {
    for (int i = 0; i < 10; i++) {
      f(i) = 2;
    }
}

In the code snippet above, f would be a column of a matrix, and I hope to change the first 10 elements of that column to 2. For example, I hope to execute the function like this:

int main(int argc, const char * argv[]) {

  Eigen::MatrixXd randomMat = Eigen::MatrixXd::Random(1000,2);
  changeTwo(randomMat.col(0));
}

However I get this error:

note: this candidate was rejected because at least one template argument could not be deduced

I've tried passing the column like this: changeTwo(randomMat.col(0).array());, but that yielded the same error.

Can I get a hint as to what went wrong?

Upvotes: 2

Views: 1406

Answers (1)

Francis Cugler
Francis Cugler

Reputation: 7925

I just downloaded and setup Eigen for my IDE: MS Visual Studio 2017 CE on an Intel Quad Core Extreme running Win7 64bit. I compiled this as an x86 version. I was not able to reproduce the same error: I'm in agreement with Henri Menke. I was able to compile and run this without error and here is my code:

#include <iostream>
#include <fstream>

#include <Eigen/Eigen> // force to include full lib

using Eigen::MatrixXd;

void changeTwo( Eigen::Ref<Eigen::ArrayXd> f ) {
    for ( int i = 0; i < 10; i++ ) {
        f( i ) = 2;
    }
}

int main() {
    std::ofstream out;

    Eigen::MatrixXd randomMat = Eigen::MatrixXd::Random( 20, 2 );
    std::cout << randomMat << std::endl;
    std::cout << std::endl;

    out.open( "EigenResults.txt" );
    out << randomMat << std::endl;
    out << std::endl;

    changeTwo( randomMat.col( 0 ) );
    std::cout << randomMat << std::endl;
    out << randomMat << std::endl;

    out.close();

    std::cout << "\nPress any key and enter to quit." << std::endl;
    char q;
    std::cin >> q;

    return 0; 
}

Output: - From console & EigenResults.txt

 -0.997497    0.97705
  0.127171  -0.108615
 -0.613392  -0.761834
  0.617481  -0.990661
  0.170019  -0.982177
-0.0402539   -0.24424
 -0.299417  0.0633259
  0.791925   0.142369
   0.64568   0.203528
   0.49321   0.214331
 -0.651784  -0.667531
  0.717887    0.32609
  0.421003 -0.0984222
 0.0270699  -0.295755
  -0.39201  -0.885922
 -0.970031   0.215369
 -0.817194   0.566637
 -0.271096   0.605213
 -0.705374  0.0397656
 -0.668203    -0.3961

         2    0.97705
         2  -0.108615
         2  -0.761834
         2  -0.990661
         2  -0.982177
         2   -0.24424
         2  0.0633259
         2   0.142369
         2   0.203528
         2   0.214331
 -0.651784  -0.667531
  0.717887    0.32609
  0.421003 -0.0984222
 0.0270699  -0.295755
  -0.39201  -0.885922
 -0.970031   0.215369
 -0.817194   0.566637
 -0.271096   0.605213
 -0.705374  0.0397656
 -0.668203    -0.3961 

Upvotes: 2

Related Questions