Menu

Calculation of matrix rank?

maikl
2007-10-01
2012-09-15
  • maikl

    maikl - 2007-10-01

    It seems that IT++ does not provide the calculation of the rank of a given mat/cmat? I would appreciate if anyone can offer some help in this matter. Many thanks.

     
    • Martin Senst

      Martin Senst - 2007-10-02

      You can perform an SVD and count the number of nonzero singular values. I took Matlab's rank function and translated it to IT++:

      template <class Num_T>
      int rank(const itpp::Mat<Num_T>& m, double tol = -1.0)
      {
      if (m.cols() == 0 || m.rows() == 0) return 0;

      itpp::vec singular_values = itpp::svd(m);
      
      if (tol &lt;= 0) // Calculate default tolerance
      {
          tol = itpp::eps * singular_values(0) * (m.rows() &gt; m.cols() ? m.rows() : m.cols());
      }
      
      // Count number of nonzero singular values
      int r;
      for (r = 0 ; r &lt; length(singular_values); r++)
      {
          if (singular_values(r) &lt; tol) break;
      }
      return r;
      

      }

      Cheers
      Martin

       
    • maikl

      maikl - 2007-10-03

      Many thanks Martin for your help - this is very much appreciated.

      Just one queston: what is the rationale for the calculation of the default tol? Why it is calculated as stated?

      tol = itpp::eps * singular_values(0) * (m.rows() > m.cols() ? m.rows() : m.cols());

      Thanks again.

       
    • Martin Senst

      Martin Senst - 2007-10-04

      Well, I have to admit that I can't really explain this choice of tol. I simply took it from the Matlab-version of rank(). The only difference is that they use eps(max(singular_values)), which is close to eps(1)*max(singular_values), but not equal. I can't tell if this difference is important.

      Sorry for not being able to help you here...

       
    • maikl

      maikl - 2007-10-04

      thank you very much for your reply. it's already very helpful. thx

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.