Skip to content

Instantly share code, notes, and snippets.

@mtao
Created September 22, 2017 20:06
Show Gist options
  • Save mtao/342872cc54e83103b7d01cd224e035a3 to your computer and use it in GitHub Desktop.
Save mtao/342872cc54e83103b7d01cd224e035a3 to your computer and use it in GitHub Desktop.
bad naming scheme and should probably use a more general template than Eigen::VexctorX?, but this does what i ened for now
#ifndef EIGEN_HDK_CONVERSION_HPP
#define EIGEN_HDK_CONVERSION_HPP
#include <UT/UT_SparseMatrix.h>
#include <Eigen/Sparse>
template <typename T, int Options = Eigen::ColMajor, typename StorageIndex>
UT_SparseMatrixELLT<T> eigen_to_hdk_ellt(const Eigen::SparseMatrix<T,Options,StorageIndex>& A) {
UT_SparseMatrixELLT<T> B(A.rows(), A.nonZeros());
int nz;
for (int k=0; k<A.outerSize(); ++k) {
for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it) {
std::cout << it.row() << ", " << it.col() << ": " << it.value() << std::endl;
B.appendRowElement(it.row(), it.col(), it.value(), nz);
}
}
return B;
}
template <typename T, int Options = Eigen::ColMajor, typename StorageIndex>
UT_SparseMatrixT<T,false> eigen_to_hdk(const Eigen::SparseMatrix<T,Options,StorageIndex>& A) {
UT_SparseMatrixT<T,false> B(A.rows(),A.cols());
for (int k=0; k<A.outerSize(); ++k) {
for (typename Eigen::SparseMatrix<T>::InnerIterator it(A,k); it; ++it) {
B.addToElement(it.row(), it.col(), it.value());
}
}
B.compile();
return B;
}
template <typename T>
Eigen::SparseMatrix<T> hdk_to_eigen(const UT_SparseMatrixT<T,false>& A) {
using Triplet = Eigen::Triplet<T>;
std::vector<Triplet> ts;
auto func = [&](int r, int c, T v) {
ts.emplace_back(r,c,v);
};
A.accept(func);
Eigen::SparseMatrix<T> B(A.getNumRows(),A.getNumCols());
B.setFromTriplets(ts.begin(),ts.end());
return B;
}
template <typename T>
UT_VectorT<T> eigen_to_hdk(const Eigen::Matrix<T,Eigen::Dynamic,1>& A) {
UT_VectorT<T> B(0,A.size()-1);
for(int i = 0; i < A.rows(); ++i) {
B(i) = A(i);
}
//std::copy(A.data(),A.data()+A.size(),B.getData());
return B;
}
template <typename T>
Eigen::Matrix<T,Eigen::Dynamic,1> hdk_to_eigen(const UT_VectorT<T>& A) {
Eigen::Matrix<T,Eigen::Dynamic,1> B(A.length());
for(int i = 0; i < A.length(); ++i) {
B(i) = A(i + A.getNL());
}
//std::copy(A.data(),A.data()+A.size(),B.getData());
return B;
}
#endif//EIGEN_HDK_CONVERSION_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment