TMB Documentation  v1.9.11
kronecker.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 
8 namespace tmbutils {
9 
11 template <class scalartype, int n1, int n2, int n3, int n4>
12 Matrix<scalartype,n1*n3,n2*n4> kronecker(Matrix<scalartype,n1,n2> x, Matrix<scalartype,n3,n4> y){
13  Matrix<scalartype,n1*n3,n2*n4> ans;
14  for(int i=0;i<n1;i++)
15  for(int j=0;j<n2;j++)
16  for(int k=0;k<n3;k++)
17  for(int l=0;l<n4;l++)
18  ans(i*n3+k,j*n4+l)=x(i,j)*y(k,l);
19  return ans;
20 }
21 
23 template <class scalartype>
25  int n1 = x.rows(), n2 = x.cols(), n3 = y.rows(), n4 = y.cols();
26  matrix<scalartype> ans(n1 * n3, n2 * n4);
27  for(int i=0;i<n1;i++)
28  for(int j=0;j<n2;j++)
29  for(int k=0;k<n3;k++)
30  for(int l=0;l<n4;l++)
31  ans(i*n3+k,j*n4+l)=x(i,j)*y(k,l);
32  return ans;
33 }
34 
35 }
Matrix class used by TMB.
Definition: tmbutils.hpp:102
Matrix< scalartype, n1 *n3, n2 *n4 > kronecker(Matrix< scalartype, n1, n2 > x, Matrix< scalartype, n3, n4 > y)
Kronecker product of two matrices.
Definition: kronecker.hpp:12
Utility functions for TMB (automatically included)
Definition: concat.hpp:5
License: GPL v2