blob: 2e302d0729306515cfca1975ca6c1689e212d361 [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001//=====================================================
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
21#include <Eigen/Eigen>
22#include <vector>
23#include "btl.hh"
24
25using namespace Eigen;
26
27template<class real, int SIZE=Dynamic>
28class eigen3_interface
29{
30
31public :
32
33 enum {IsFixedSize = (SIZE!=Dynamic)};
34
35 typedef real real_type;
36
37 typedef std::vector<real> stl_vector;
38 typedef std::vector<stl_vector> stl_matrix;
39
40 typedef Eigen::Matrix<real,SIZE,SIZE> gene_matrix;
41 typedef Eigen::Matrix<real,SIZE,1> gene_vector;
42
43 static inline std::string name( void )
44 {
45 return EIGEN_MAKESTRING(BTL_PREFIX);
46 }
47
Austin Schuh189376f2018-12-20 22:11:15 +110048 static void free_matrix(gene_matrix & /*A*/, int /*N*/) {}
Brian Silverman72890c22015-09-19 14:37:37 -040049
Austin Schuh189376f2018-12-20 22:11:15 +110050 static void free_vector(gene_vector & /*B*/) {}
Brian Silverman72890c22015-09-19 14:37:37 -040051
52 static BTL_DONT_INLINE void matrix_from_stl(gene_matrix & A, stl_matrix & A_stl){
53 A.resize(A_stl[0].size(), A_stl.size());
54
Austin Schuh189376f2018-12-20 22:11:15 +110055 for (unsigned int j=0; j<A_stl.size() ; j++){
56 for (unsigned int i=0; i<A_stl[j].size() ; i++){
Brian Silverman72890c22015-09-19 14:37:37 -040057 A.coeffRef(i,j) = A_stl[j][i];
58 }
59 }
60 }
61
62 static BTL_DONT_INLINE void vector_from_stl(gene_vector & B, stl_vector & B_stl){
63 B.resize(B_stl.size(),1);
64
Austin Schuh189376f2018-12-20 22:11:15 +110065 for (unsigned int i=0; i<B_stl.size() ; i++){
Brian Silverman72890c22015-09-19 14:37:37 -040066 B.coeffRef(i) = B_stl[i];
67 }
68 }
69
70 static BTL_DONT_INLINE void vector_to_stl(gene_vector & B, stl_vector & B_stl){
Austin Schuh189376f2018-12-20 22:11:15 +110071 for (unsigned int i=0; i<B_stl.size() ; i++){
Brian Silverman72890c22015-09-19 14:37:37 -040072 B_stl[i] = B.coeff(i);
73 }
74 }
75
76 static BTL_DONT_INLINE void matrix_to_stl(gene_matrix & A, stl_matrix & A_stl){
Austin Schuh189376f2018-12-20 22:11:15 +110077 int N=A_stl.size();
Brian Silverman72890c22015-09-19 14:37:37 -040078
79 for (int j=0;j<N;j++){
80 A_stl[j].resize(N);
81 for (int i=0;i<N;i++){
82 A_stl[j][i] = A.coeff(i,j);
83 }
84 }
85 }
86
Austin Schuh189376f2018-12-20 22:11:15 +110087 static inline void matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -040088 X.noalias() = A*B;
89 }
90
Austin Schuh189376f2018-12-20 22:11:15 +110091 static inline void transposed_matrix_matrix_product(const gene_matrix & A, const gene_matrix & B, gene_matrix & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -040092 X.noalias() = A.transpose()*B.transpose();
93 }
94
Austin Schuhc55b0172022-02-20 17:52:35 -080095 static inline void ata_product(const gene_matrix & A, gene_matrix & X, int /*N*/){
96 //X.noalias() = A.transpose()*A;
97 X.template triangularView<Lower>().setZero();
98 X.template selfadjointView<Lower>().rankUpdate(A.transpose());
99 }
Brian Silverman72890c22015-09-19 14:37:37 -0400100
Austin Schuh189376f2018-12-20 22:11:15 +1100101 static inline void aat_product(const gene_matrix & A, gene_matrix & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400102 X.template triangularView<Lower>().setZero();
103 X.template selfadjointView<Lower>().rankUpdate(A);
104 }
105
Austin Schuh189376f2018-12-20 22:11:15 +1100106 static inline void matrix_vector_product(const gene_matrix & A, const gene_vector & B, gene_vector & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400107 X.noalias() = A*B;
108 }
109
Austin Schuh189376f2018-12-20 22:11:15 +1100110 static inline void symv(const gene_matrix & A, const gene_vector & B, gene_vector & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400111 X.noalias() = (A.template selfadjointView<Lower>() * B);
112// internal::product_selfadjoint_vector<real,0,LowerTriangularBit,false,false>(N,A.data(),N, B.data(), 1, X.data(), 1);
113 }
114
115 template<typename Dest, typename Src> static void triassign(Dest& dst, const Src& src)
116 {
117 typedef typename Dest::Scalar Scalar;
118 typedef typename internal::packet_traits<Scalar>::type Packet;
119 const int PacketSize = sizeof(Packet)/sizeof(Scalar);
120 int size = dst.cols();
121 for(int j=0; j<size; j+=1)
122 {
123// const int alignedEnd = alignedStart + ((innerSize-alignedStart) & ~packetAlignedMask);
124 Scalar* A0 = dst.data() + j*dst.stride();
125 int starti = j;
126 int alignedEnd = starti;
127 int alignedStart = (starti) + internal::first_aligned(&A0[starti], size-starti);
128 alignedEnd = alignedStart + ((size-alignedStart)/(2*PacketSize))*(PacketSize*2);
129
130 // do the non-vectorizable part of the assignment
131 for (int index = starti; index<alignedStart ; ++index)
132 {
133 if(Dest::Flags&RowMajorBit)
134 dst.copyCoeff(j, index, src);
135 else
136 dst.copyCoeff(index, j, src);
137 }
138
139 // do the vectorizable part of the assignment
140 for (int index = alignedStart; index<alignedEnd; index+=PacketSize)
141 {
142 if(Dest::Flags&RowMajorBit)
143 dst.template copyPacket<Src, Aligned, Unaligned>(j, index, src);
144 else
145 dst.template copyPacket<Src, Aligned, Unaligned>(index, j, src);
146 }
147
148 // do the non-vectorizable part of the assignment
149 for (int index = alignedEnd; index<size; ++index)
150 {
151 if(Dest::Flags&RowMajorBit)
152 dst.copyCoeff(j, index, src);
153 else
154 dst.copyCoeff(index, j, src);
155 }
156 //dst.col(j).tail(N-j) = src.col(j).tail(N-j);
157 }
158 }
159
Austin Schuh189376f2018-12-20 22:11:15 +1100160 static EIGEN_DONT_INLINE void syr2(gene_matrix & A, gene_vector & X, gene_vector & Y, int N){
Brian Silverman72890c22015-09-19 14:37:37 -0400161 // internal::product_selfadjoint_rank2_update<real,0,LowerTriangularBit>(N,A.data(),N, X.data(), 1, Y.data(), 1, -1);
162 for(int j=0; j<N; ++j)
163 A.col(j).tail(N-j) += X[j] * Y.tail(N-j) + Y[j] * X.tail(N-j);
164 }
165
Austin Schuh189376f2018-12-20 22:11:15 +1100166 static EIGEN_DONT_INLINE void ger(gene_matrix & A, gene_vector & X, gene_vector & Y, int N){
Brian Silverman72890c22015-09-19 14:37:37 -0400167 for(int j=0; j<N; ++j)
168 A.col(j) += X * Y[j];
169 }
170
Austin Schuh189376f2018-12-20 22:11:15 +1100171 static EIGEN_DONT_INLINE void rot(gene_vector & A, gene_vector & B, real c, real s, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400172 internal::apply_rotation_in_the_plane(A, B, JacobiRotation<real>(c,s));
173 }
174
Austin Schuh189376f2018-12-20 22:11:15 +1100175 static inline void atv_product(gene_matrix & A, gene_vector & B, gene_vector & X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400176 X.noalias() = (A.transpose()*B);
177 }
178
Austin Schuh189376f2018-12-20 22:11:15 +1100179 static inline void axpy(real coef, const gene_vector & X, gene_vector & Y, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400180 Y += coef * X;
181 }
182
Austin Schuh189376f2018-12-20 22:11:15 +1100183 static inline void axpby(real a, const gene_vector & X, real b, gene_vector & Y, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400184 Y = a*X + b*Y;
185 }
186
Austin Schuh189376f2018-12-20 22:11:15 +1100187 static EIGEN_DONT_INLINE void copy_matrix(const gene_matrix & source, gene_matrix & cible, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400188 cible = source;
189 }
190
Austin Schuh189376f2018-12-20 22:11:15 +1100191 static EIGEN_DONT_INLINE void copy_vector(const gene_vector & source, gene_vector & cible, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400192 cible = source;
193 }
194
Austin Schuh189376f2018-12-20 22:11:15 +1100195 static inline void trisolve_lower(const gene_matrix & L, const gene_vector& B, gene_vector& X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400196 X = L.template triangularView<Lower>().solve(B);
197 }
198
Austin Schuh189376f2018-12-20 22:11:15 +1100199 static inline void trisolve_lower_matrix(const gene_matrix & L, const gene_matrix& B, gene_matrix& X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400200 X = L.template triangularView<Upper>().solve(B);
201 }
202
Austin Schuh189376f2018-12-20 22:11:15 +1100203 static inline void trmm(const gene_matrix & L, const gene_matrix& B, gene_matrix& X, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400204 X.noalias() = L.template triangularView<Lower>() * B;
205 }
206
Austin Schuh189376f2018-12-20 22:11:15 +1100207 static inline void cholesky(const gene_matrix & X, gene_matrix & C, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400208 C = X;
209 internal::llt_inplace<real,Lower>::blocked(C);
210 //C = X.llt().matrixL();
211// C = X;
212// Cholesky<gene_matrix>::computeInPlace(C);
213// Cholesky<gene_matrix>::computeInPlaceBlock(C);
214 }
215
Austin Schuh189376f2018-12-20 22:11:15 +1100216 static inline void lu_decomp(const gene_matrix & X, gene_matrix & C, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400217 C = X.fullPivLu().matrixLU();
218 }
219
Austin Schuh189376f2018-12-20 22:11:15 +1100220 static inline void partial_lu_decomp(const gene_matrix & X, gene_matrix & C, int N){
Brian Silverman72890c22015-09-19 14:37:37 -0400221 Matrix<DenseIndex,1,Dynamic> piv(N);
222 DenseIndex nb;
223 C = X;
224 internal::partial_lu_inplace(C,piv,nb);
225// C = X.partialPivLu().matrixLU();
226 }
227
Austin Schuh189376f2018-12-20 22:11:15 +1100228 static inline void tridiagonalization(const gene_matrix & X, gene_matrix & C, int N){
Brian Silverman72890c22015-09-19 14:37:37 -0400229 typename Tridiagonalization<gene_matrix>::CoeffVectorType aux(N-1);
230 C = X;
231 internal::tridiagonalization_inplace(C, aux);
232 }
233
Austin Schuh189376f2018-12-20 22:11:15 +1100234 static inline void hessenberg(const gene_matrix & X, gene_matrix & C, int /*N*/){
Brian Silverman72890c22015-09-19 14:37:37 -0400235 C = HessenbergDecomposition<gene_matrix>(X).packedMatrix();
236 }
237
238
239
240};
241
242#endif