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

lpNorm<> Question

Tags: None
(comma "," separated)
ddmarsha
Registered Member
Posts
3
Karma
0

lpNorm<> Question

Wed May 25, 2011 12:35 am
I'm just getting started with Eigen3, and I really like it.

Here's a newby question: why does the following code not work? If I replace the static sized matrix with a dynamic sized via Eigen::MatrixXd m(2,2) it does work. I get a compiler error on the m.lpNorm<1>() call. I am using gcc 4.5 and the latest download of Eigen3 from the web page.

Code: Select all
  int main(int argc, char *argv[])
  {
      Eigen::Matrix<double, 2, 2> m;

      m << 1,-2,
           -3,4;

      std::cout << "m.lpNorm<1>() = " << m.lpNorm<1>() << std::endl; // copiler error here!!!
      return 0;
  }


Thanks for a great library.
jitseniesen
Registered Member
Posts
204
Karma
2

Re: lpNorm<> Question

Wed May 25, 2011 12:38 pm
Your program works fine here, also with gcc 4.5. What error do you get?

I don't know whether you noticed, but mat.lpNorm<1>() computes the L1 norm of the matrix considered as a vector, i.e. the sum of the absolute value of all the coefficients. This may not be what you want.
ddmarsha
Registered Member
Posts
3
Karma
0

Re: lpNorm<> Question

Wed May 25, 2011 6:24 pm
You are correct. I was trying to excerpt a code snippet, and I didn't test it. The actual error was buried deep in some code, but I was able to extract out a simple case that showed the error. Here's the code that errors out:
Code: Select all
#include <iostream>
#include "Eigen/Eigen"

template<typename __data>
__data calc_norm(Eigen::Matrix<__data, 2, 2> &m)
{
  return m.lpNorm<1>();
}

int main(int argc, char *argv[])
{
    Eigen::Matrix<double, 2, 2> m;

    m << 1,-2,
         -3,4;

    std::cout << "m.lpNorm<1>() = " << calc_norm<double>(m) << std::endl; // compiler error here!!!
    return 0;
}


The is the error message that g++ emits:
$ /usr/local/bin/g++ -I /usr/local/eigen3/ -o eigen_test eigen_test.cpp
eigen_test.cpp: In function '__data calc_norm(Eigen::Matrix<Scalar, 2, 2>&)':
eigen_test.cpp:7:22: error: expected primary-expression before ')' token
eigen_test.cpp: In function '__data calc_norm(Eigen::Matrix<Scalar, 2, 2>&) [with __data = double]':
eigen_test.cpp:17:61: instantiated from here
eigen_test.cpp:7:22: error: invalid operands of types '<unresolved overloaded function type>' and 'int' to binary 'operator<'


If you don't make the function a template, then the error goes away. Also, you can change the call to lpNorm<> to norm(), and it will compile. FYI, the error in my code is not in a template function, it is in a member function of a template class, so I don't think it is a template function problem.

Thanks for the help.
jitseniesen
Registered Member
Posts
204
Karma
2

Re: lpNorm<> Question

Wed May 25, 2011 9:54 pm
I don't have a compiler handy at the moment, but I think you're missing the 'template' keyword for calling template member functions of a dependent type.
If you add the word 'template' as shown below it should work (consult your favourite C++ reference to learn why). If not let me know, and I (or somebody else) will look into it further.

Code: Select all
template<typename __data>
__data calc_norm(Eigen::Matrix<__data, 2, 2> &m)
{
  return m.template lpNorm<1>();
}
ddmarsha
Registered Member
Posts
3
Karma
0

Re: lpNorm<> Question

Wed May 25, 2011 10:26 pm
You are right. I have to say that I didn't know about that usage of the template keyword. I'll be looking that up tonight. Thank you.


Bookmarks



Who is online

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