SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PermutationMatrix.cpp
Go to the documentation of this file.
00001 #include "PermutationMatrix.h"
00002 
00003 #ifdef HAVE_EIGEN3
00004 
00005 #include <shogun/mathematics/Math.h>
00006 #include <shogun/mathematics/eigen3.h>
00007 
00008 using namespace shogun;
00009 using namespace Eigen;
00010 
00011 bool is_permutation_matrix(SGMatrix<float64_t> m)
00012 {
00013     Map<MatrixXd> mat(m.matrix,m.num_rows,m.num_cols);
00014 
00015     // scale
00016     for(int t = 0; t < mat.cols(); t++)
00017         mat.col(t) /= mat.col(t).cwiseAbs().maxCoeff();
00018 
00019     // round
00020     for (int i = 0; i < mat.rows(); i++)
00021     {
00022         for (int j = 0; j < mat.cols(); j++)
00023         {
00024             if (CMath::abs(CMath::round(mat(i,j))) >= 1.0)
00025                 mat(i,j) = 1.0;
00026             else
00027                 mat(i,j) = 0.0;
00028         }
00029     }
00030 
00031     // check only a single 1 per row
00032     for (int i = 0; i < mat.rows(); i++)
00033     {
00034         int num_ones = 0;
00035         for (int j = 0; j < mat.cols(); j++)
00036         {
00037             if (mat(i,j) >= 1.0)
00038                 num_ones++;
00039         }
00040 
00041         if (num_ones != 1)
00042             return false;
00043     }
00044 
00045     // check only a single 1 per col
00046     for (int j = 0; j < mat.cols(); j++)
00047     {
00048         int num_ones = 0;
00049         for (int i = 0; i < mat.rows(); i++)
00050         {
00051             if (mat(i,j) >= 1.0)
00052                 num_ones++;
00053         }
00054 
00055         if (num_ones != 1)
00056             return false;
00057     }
00058 
00059     return true;
00060 }
00061 #endif //HAVE_EIGEN3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation