eigen3_interface.hh 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //=====================================================
  2. // Copyright (C) 2008 Gael Guennebaud <gael.guennebaud@inria.fr>
  3. //=====================================================
  4. //
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU General Public License
  7. // as published by the Free Software Foundation; either version 2
  8. // of the License, or (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. //
  18. #ifndef EIGEN3_INTERFACE_HH
  19. #define EIGEN3_INTERFACE_HH
  20. #include <Eigen/Eigen>
  21. #include <vector>
  22. #include "btl.hh"
  23. using namespace Eigen;
  24. template<class real, int SIZE=Dynamic>
  25. class eigen3_interface
  26. {
  27. public :
  28. enum {IsFixedSize = (SIZE!=Dynamic)};
  29. typedef real real_type;
  30. typedef std::vector<real> stl_vector;
  31. typedef std::vector<stl_vector> stl_matrix;
  32. typedef Eigen::Matrix<real,SIZE,SIZE> gene_matrix;
  33. typedef Eigen::Matrix<real,SIZE,1> gene_vector;
  34. static inline std::string name( void )
  35. {
  36. return EIGEN_MAKESTRING(BTL_PREFIX);
  37. }
  38. static void free_matrix(gene_matrix & /*A*/, int /*N*/) {}
  39. static void free_vector(gene_vector & /*B*/) {}
  40. static BTL_DONT_INLINE void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
  41. A.resize(A_stl[0].size(), A_stl.size());
  42. for (unsigned int j=0; j<A_stl.size() ; j++){
  43. for (unsigned int i=0; i<A_stl[j].size() ; i++){
  44. A.coeffRef(i,j) = A_stl[j][i];
  45. }
  46. }
  47. }
  48. static BTL_DONT_INLINE void vector_from_stl(gene_vector & B, stl_vector & B_stl){
  49. B.resize(B_stl.size(),1);
  50. for (unsigned int i=0; i<B_stl.size() ; i++){
  51. B.coeffRef(i) = B_stl[i];
  52. }
  53. }
  54. static BTL_DONT_INLINE void vector_to_stl(gene_vector & B, stl_vector & B_stl){
  55. for (unsigned int i=0; i<B_stl.size() ; i++){
  56. B_stl[i] = B.coeff(i);
  57. }
  58. }
  59. static BTL_DONT_INLINE void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
  60. int N=A_stl.size();
  61. for (int j=0;j<N;j++){
  62. A_stl[j].resize(N);
  63. for (int i=0;i<N;i++){
  64. A_stl[j][i] = A.coeff(i,j);
  65. }
  66. }
  67. }
  68. static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int /*N*/){
  69. X.noalias() = A*B;
  70. }
  71. static inline void transposed_matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int /*N*/){
  72. X.noalias() = A.transpose()*B.transpose();
  73. }
  74. static inline void ata_product(const gene_matrix & A, gene_matrix & X, int /*N*/){
  75. //X.noalias() = A.transpose()*A;
  76. X.template triangularView<Lower>().setZero();
  77. X.template selfadjointView<Lower>().rankUpdate(A.transpose());
  78. }
  79. static inline void aat_product(const gene_matrix & A, gene_matrix & X, int /*N*/){
  80. X.template triangularView<Lower>().setZero();
  81. X.template selfadjointView<Lower>().rankUpdate(A);
  82. }
  83. static inline void matrix_vector_product(const gene_matrix & A, const gene_vector & B, gene_vector & X, int /*N*/){
  84. X.noalias() = A*B;
  85. }
  86. static inline void symv(const gene_matrix & A, const gene_vector & B, gene_vector & X, int /*N*/){
  87. X.noalias() = (A.template selfadjointView<Lower>() * B);
  88. // internal::product_selfadjoint_vector<real,0,LowerTriangularBit,false,false>(N,A.data(),N, B.data(), 1, X.data(), 1);
  89. }
  90. template<typename Dest, typename Src> static void triassign(Dest& dst, const Src& src)
  91. {
  92. typedef typename Dest::Scalar Scalar;
  93. typedef typename internal::packet_traits<Scalar>::type Packet;
  94. const int PacketSize = sizeof(Packet)/sizeof(Scalar);
  95. int size = dst.cols();
  96. for(int j=0; j<size; j+=1)
  97. {
  98. // const int alignedEnd = alignedStart + ((innerSize-alignedStart) & ~packetAlignedMask);
  99. Scalar* A0 = dst.data() + j*dst.stride();
  100. int starti = j;
  101. int alignedEnd = starti;
  102. int alignedStart = (starti) + internal::first_aligned(&A0[starti], size-starti);
  103. alignedEnd = alignedStart + ((size-alignedStart)/(2*PacketSize))*(PacketSize*2);
  104. // do the non-vectorizable part of the assignment
  105. for (int index = starti; index<alignedStart ; ++index)
  106. {
  107. if(Dest::Flags&RowMajorBit)
  108. dst.copyCoeff(j, index, src);
  109. else
  110. dst.copyCoeff(index, j, src);
  111. }
  112. // do the vectorizable part of the assignment
  113. for (int index = alignedStart; index<alignedEnd; index+=PacketSize)
  114. {
  115. if(Dest::Flags&RowMajorBit)
  116. dst.template copyPacket<Src, Aligned, Unaligned>(j, index, src);
  117. else
  118. dst.template copyPacket<Src, Aligned, Unaligned>(index, j, src);
  119. }
  120. // do the non-vectorizable part of the assignment
  121. for (int index = alignedEnd; index<size; ++index)
  122. {
  123. if(Dest::Flags&RowMajorBit)
  124. dst.copyCoeff(j, index, src);
  125. else
  126. dst.copyCoeff(index, j, src);
  127. }
  128. //dst.col(j).tail(N-j) = src.col(j).tail(N-j);
  129. }
  130. }
  131. static EIGEN_DONT_INLINE void syr2(gene_matrix & A, gene_vector & X, gene_vector & Y, int N){
  132. // internal::product_selfadjoint_rank2_update<real,0,LowerTriangularBit>(N,A.data(),N, X.data(), 1, Y.data(), 1, -1);
  133. for(int j=0; j<N; ++j)
  134. A.col(j).tail(N-j) += X[j] * Y.tail(N-j) + Y[j] * X.tail(N-j);
  135. }
  136. static EIGEN_DONT_INLINE void ger(gene_matrix & A, gene_vector & X, gene_vector & Y, int N){
  137. for(int j=0; j<N; ++j)
  138. A.col(j) += X * Y[j];
  139. }
  140. static EIGEN_DONT_INLINE void rot(gene_vector & A, gene_vector & B, real c, real s, int /*N*/){
  141. internal::apply_rotation_in_the_plane(A, B, JacobiRotation<real>(c,s));
  142. }
  143. static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int /*N*/){
  144. X.noalias() = (A.transpose()*B);
  145. }
  146. static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int /*N*/){
  147. Y += coef * X;
  148. }
  149. static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int /*N*/){
  150. Y = a*X + b*Y;
  151. }
  152. static EIGEN_DONT_INLINE void copy_matrix(const gene_matrix & source, gene_matrix & cible, int /*N*/){
  153. cible = source;
  154. }
  155. static EIGEN_DONT_INLINE void copy_vector(const gene_vector & source, gene_vector & cible, int /*N*/){
  156. cible = source;
  157. }
  158. static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector& X, int /*N*/){
  159. X = L.template triangularView<Lower>().solve(B);
  160. }
  161. static inline void trisolve_lower_matrix(const gene_matrix & L, const gene_matrix& B, gene_matrix& X, int /*N*/){
  162. X = L.template triangularView<Upper>().solve(B);
  163. }
  164. static inline void trmm(const gene_matrix & L, const gene_matrix& B, gene_matrix& X, int /*N*/){
  165. X.noalias() = L.template triangularView<Lower>() * B;
  166. }
  167. static inline void cholesky(const gene_matrix & X, gene_matrix & C, int /*N*/){
  168. C = X;
  169. internal::llt_inplace<real,Lower>::blocked(C);
  170. //C = X.llt().matrixL();
  171. // C = X;
  172. // Cholesky<gene_matrix>::computeInPlace(C);
  173. // Cholesky<gene_matrix>::computeInPlaceBlock(C);
  174. }
  175. static inline void lu_decomp(const gene_matrix & X, gene_matrix & C, int /*N*/){
  176. C = X.fullPivLu().matrixLU();
  177. }
  178. static inline void partial_lu_decomp(const gene_matrix & X, gene_matrix & C, int N){
  179. Matrix<DenseIndex,1,Dynamic> piv(N);
  180. DenseIndex nb;
  181. C = X;
  182. internal::partial_lu_inplace(C,piv,nb);
  183. // C = X.partialPivLu().matrixLU();
  184. }
  185. static inline void tridiagonalization(const gene_matrix & X, gene_matrix & C, int N){
  186. typename Tridiagonalization<gene_matrix>::CoeffVectorType aux(N-1);
  187. C = X;
  188. internal::tridiagonalization_inplace(C, aux);
  189. }
  190. static inline void hessenberg(const gene_matrix & X, gene_matrix & C, int /*N*/){
  191. C = HessenbergDecomposition<gene_matrix>(X).packedMatrix();
  192. }
  193. };
  194. #endif