0

convert Eigen::Matrix to flann::Matrix

The following functions convert (note the matrix size!!)

  • a column-major NxM Eigen::Matrix to a row-major MxN flann::Matrix.
  • a row-major NxM Eigen::Matrix to a row-major NxM flann::Matrix.
template <typename ValueType>
flann::Matrix<ValueType> convertEigen2Flann(
const Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>&amp; mEigen
)
{
flann::Matrix<ValueType>mFlann(new ValueType[mEigen.size()], mEigen.rows(), mEigen.cols());

for(size_t n = 0; n < mEigen.size(); ++n)
{
*(mFlann.ptr()+n) = *(mEigen.data()+n);
}
return mFlann;
}

template <typename ValueType>
flann::Matrix<ValueType> convertEigen2Flann(
const Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>&amp; _mEigen
)
{
Eigen::Matrix<ValueType, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> mEigen = _mEigen.transpose();
return convertEigen2Flann(mEigen);
}
0

Eigen: select columns with N largest norm 2

PTAM/Build/Linux/VideoSource_Linux_OpenCV.ccThis post is to measure the computation time of the previous post, in which I posted a C++ code using Eigen to select columns with N largest norm. My code is available here.

Suppose we define a function doing the column selection. It’s natural using the function is slower than column selection without using the function, meaning write the selection code in main function. My question is how slower using function is. So, the following code compares two methods: (1) column selection implemented in main function and (2) column selection executed as a function. I tried with the following two cases.

  1. Case 1.
    • an original matrix of size 100×100000
    • the number of selected columns: 10
    • the number of trials: 100
  2. Case 2
    • an original matrix of size 100000×100
    • the number of selected columns: 10
    • the number of trials: 100

Case 1: result

Elapsed time w/o using function = 75.2322 sec. for 100 trial

Elapsed time w/ using function = 75.2383 sec. for 100 trial

Case 2: result

Elapsed time w/o using function = 75.3003 sec. for 100 trial

Elapsed time w/ using function = 89.1946 sec. for 100 trial

#include <Eigen/Core>;
#include <iostream>;
#include <vector>;
#include <ctime>;

Eigen::MatrixXd func(
	const Eigen::MatrixXd &m,
	const int numDataSub
)
{
	int dimData = m.rows();
	Eigen::MatrixXd mSub = Eigen::MatrixXd::Zero(dimData, numDataSub);
	Eigen::VectorXd mNorm = m.colwise().norm();
	std::vector<int>; indexMax(numDataSub);
	for(int n = 0; n < numDataSub; ++n)
	{
		mNorm.maxCoeff(&indexMax[n]);
		mNorm(indexMax[n]) = 0.0;
		mSub.col(n) = m.col(indexMax[n]);
	}
	return mSub;
}

int main()
{
	std::clock_t timeStart;
	std::clock_t timeEnd;
	int numTrial = 100;

	int dimData = 100;
	int numData = 100000;
	int numDataSub = 50;
	Eigen::MatrixXd m;
	Eigen::MatrixXd mSub = Eigen::MatrixXd::Zero(dimData, numDataSub);
	Eigen::VectorXd mNorm;
	std::vector<int>; indexMax(numDataSub);

	// std::cout << "All data and its norm:" << std::endl;
	// for(int n = 0; n < numData; ++n)
	// {
	// 	std::cout << "	data[" << n << "] = (" << m.col(n).transpose() << ")	norm = " << mNorm(n) << std::endl;		
	// }

	// retrieve columns whose norm  is top numDataSub

	///
	// Method 1: column selection without using a function
	timeStart = std::clock();
	for(int t = 0; t < numTrial; ++t)
	{
		m = Eigen::MatrixXd::Random(dimData, numData);
		mNorm = m.colwise().norm();
		for(int n = 0; n < numDataSub; ++n)
		{
			mNorm.maxCoeff(&indexMax[n]);
			mNorm(indexMax[n]) = 0.0;
			mSub.col(n) = m.col(indexMax[n]);
		}
		// std::cout << "Selected data with " << numDataSub << "largest norm:" << std::endl;
		// for(int n = 0; n < numDataSub; ++n)
		// {
		// 	std::cout << "	data[" << n << "] = (" << mSub.col(n).transpose() << ")	original index = " << indexMax[n] << std::endl;		
		// }
	}
	timeEnd = std::clock();
	std::cout << "Elapsed time w/o using function = " << (double)(timeEnd - timeStart)/CLOCKS_PER_SEC << " sec. for " << numTrial << " trial" << std::endl;

	///
	// Method 2: column selection using a function
	timeStart = std::clock();
	for(int t = 0; t < numTrial; ++t)
	{
		m = Eigen::MatrixXd::Random(dimData, numData);
		mSub = func(m, numDataSub);
	}
	timeEnd = std::clock();
	std::cout << "Elapsed time w/  using function = " << (double)(timeEnd - timeStart)/CLOCKS_PER_SEC << " sec. for " << numTrial << " trial" << std::endl;

	return 0;
}
1

Eigen: select columns with N largest norm

The following code selects columns with N largest norm. The output should be

All data and its norm:
data[0] = ( 68.0375 -21.1234) norm = 71.2412
data[1] = (56.6198 59.688) norm = 82.2707
data[2] = ( 82.3295 -60.4897) norm = 102.162
data[3] = (-32.9554 53.6459) norm = 62.9599
data[4] = (-44.4451 10.794) norm = 45.737
data[5] = (-4.52059 25.7742) norm = 26.1676
data[6] = (-27.0431 2.68018) norm = 27.1756
data[7] = (90.4459 83.239) norm = 122.919
data[8] = (27.1423 43.4594) norm = 51.2389
data[9] = (-71.6795 21.3938) norm = 74.804
Selected data with 3largest norm:
data[0] = (90.4459 83.239) original index = 7
data[1] = ( 82.3295 -60.4897) original index = 2
data[2] = (56.6198 59.688) original index = 1

#include <Eigen/Core>;
#include <iostream>;
#include <vector>;

int main()
{
	int dimData = 2;
	int numData = 10;
	int numDataSub = 3;
	Eigen::MatrixXd m = 100.0*Eigen::MatrixXd::Random(dimData, numData);
	Eigen::MatrixXd mSub = Eigen::MatrixXd::Zero(dimData, numDataSub);
	Eigen::VectorXd mNorm = m.colwise().norm();
	std::vector<int>; indexMax(numDataSub);

	std::cout << "All data and its norm:" << std::endl;
	for(int n = 0; n < numData; ++n)
	{
		std::cout << "	data[" << n << "] = (" << m.col(n).transpose() << ")	norm = " << mNorm(n) << std::endl;		
	}

	// retrieve columns whose norm  is top numDataSub
	for(int n = 0; n < numDataSub; ++n)
	{
		mNorm.maxCoeff(&amp;indexMax[n]);
		mNorm(indexMax[n]) = 0.0;
		mSub.col(n) = m.col(indexMax[n]);
	}
	std::cout << "Selected data with " << numDataSub << "largest norm:" << std::endl;
	for(int n = 0; n < numDataSub; ++n)
	{
		std::cout << "	data[" << n << "] = (" << mSub.col(n).transpose() << ")	original index = " << indexMax[n] << std::endl;		
	}

	return 0;
}