Search code examples
c++eigen

computing lpNorm column wise in Eigen


When I try to call lpNorm<1> with colwise() in Eigen I get the error:

error: 'Eigen::DenseBase > >::ColwiseReturnType' has no member named 'lpNorm'

Instead norm() and squaredNorm() work fine calling them colwise.

example

  #include <Eigen/Dense>
  #include <iostream>
  using namespace std;
  using namespace Eigen;
  int main()
  {
    MatrixXf m(2,2), n(2,2);
    m << 1,-2,
      -3,4;
    cout << "m.colwise().squaredNorm() = " << m.colwise().squaredNorm() << endl;
    cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << endl;
 //  cout << "m.colwise().lpNorm<1>() = " << m.colwise().lpNorm<1>() << endl;
}

works fine giving

m.colwise().squaredNorm() = 10 20
m.lpNorm<1>() = 10

If I uncomment the last line I get the error.

Can someone help?


Solution

  • It is not implemented for colwise in Eigen <=3.2.9. You have two options:

    1. Upgrade to Eigen 3.3 (beta)
    2. Loop over all columns and calculate the lp norms one by one.