This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen
View | Details | Raw Unified | Return to bug 565 | Differences between
and this patch

Collapse All | Expand All

(-)a/unsupported/Eigen/src/KroneckerProduct/KroneckerTensorProduct.h (-2 / +2 lines)
Lines 43-60 class KroneckerProduct : public ReturnBy Link Here
43
    /*! \brief Evaluate the Kronecker tensor product. */
43
    /*! \brief Evaluate the Kronecker tensor product. */
44
    template<typename Dest> void evalTo(Dest& dst) const;
44
    template<typename Dest> void evalTo(Dest& dst) const;
45
    
45
    
46
    inline Index rows() const { return m_A.rows() * m_B.rows(); }
46
    inline Index rows() const { return m_A.rows() * m_B.rows(); }
47
    inline Index cols() const { return m_A.cols() * m_B.cols(); }
47
    inline Index cols() const { return m_A.cols() * m_B.cols(); }
48
48
49
    Scalar coeff(Index row, Index col) const
49
    Scalar coeff(Index row, Index col) const
50
    {
50
    {
51
      return m_A.coeff(row / m_A.cols(), col / m_A.rows()) *
51
      return m_A.coeff(row / m_B.rows(), col / m_B.cols()) *
52
             m_B.coeff(row % m_A.cols(), col % m_A.rows());
52
             m_B.coeff(row % m_B.rows(), col % m_B.cols());
53
    }
53
    }
54
54
55
    Scalar coeff(Index i) const
55
    Scalar coeff(Index i) const
56
    {
56
    {
57
      EIGEN_STATIC_ASSERT_VECTOR_ONLY(KroneckerProduct);
57
      EIGEN_STATIC_ASSERT_VECTOR_ONLY(KroneckerProduct);
58
      return m_A.coeff(i / m_A.size()) * m_B.coeff(i % m_A.size());
58
      return m_A.coeff(i / m_A.size()) * m_B.coeff(i % m_A.size());
59
    }
59
    }
60
60
(-)a/unsupported/test/kronecker_product.cpp (-2 / +11 lines)
Lines 81-118 void check_sparse_kronecker_product(cons Link Here
81
  VERIFY_IS_APPROX(ab.coeff(6,8),  0.12);
81
  VERIFY_IS_APPROX(ab.coeff(6,8),  0.12);
82
  VERIFY_IS_APPROX(ab.coeff(8,9), -0.15);
82
  VERIFY_IS_APPROX(ab.coeff(8,9), -0.15);
83
}
83
}
84
84
85
85
86
void test_kronecker_product()
86
void test_kronecker_product()
87
{
87
{
88
  // DM = dense matrix; SM = sparse matrix
88
  // DM = dense matrix; SM = sparse matrix
89
89
  Matrix<double, 2, 3> DM_a;
90
  Matrix<double, 2, 3> DM_a;
90
  MatrixXd             DM_b(3,2);
91
  SparseMatrix<double> SM_a(2,3);
91
  SparseMatrix<double> SM_a(2,3);
92
  SparseMatrix<double> SM_b(3,2);
93
  SM_a.insert(0,0) = DM_a.coeffRef(0,0) = -0.4461540300782201;
92
  SM_a.insert(0,0) = DM_a.coeffRef(0,0) = -0.4461540300782201;
94
  SM_a.insert(0,1) = DM_a.coeffRef(0,1) = -0.8057364375283049;
93
  SM_a.insert(0,1) = DM_a.coeffRef(0,1) = -0.8057364375283049;
95
  SM_a.insert(0,2) = DM_a.coeffRef(0,2) =  0.3896572459516341;
94
  SM_a.insert(0,2) = DM_a.coeffRef(0,2) =  0.3896572459516341;
96
  SM_a.insert(1,0) = DM_a.coeffRef(1,0) = -0.9076572187376921;
95
  SM_a.insert(1,0) = DM_a.coeffRef(1,0) = -0.9076572187376921;
97
  SM_a.insert(1,1) = DM_a.coeffRef(1,1) =  0.6469156566545853;
96
  SM_a.insert(1,1) = DM_a.coeffRef(1,1) =  0.6469156566545853;
98
  SM_a.insert(1,2) = DM_a.coeffRef(1,2) = -0.3658010398782789;
97
  SM_a.insert(1,2) = DM_a.coeffRef(1,2) = -0.3658010398782789;
98
 
99
  MatrixXd             DM_b(3,2);
100
  SparseMatrix<double> SM_b(3,2);
99
  SM_b.insert(0,0) = DM_b.coeffRef(0,0) =  0.9004440976767099;
101
  SM_b.insert(0,0) = DM_b.coeffRef(0,0) =  0.9004440976767099;
100
  SM_b.insert(0,1) = DM_b.coeffRef(0,1) = -0.2368830858139832;
102
  SM_b.insert(0,1) = DM_b.coeffRef(0,1) = -0.2368830858139832;
101
  SM_b.insert(1,0) = DM_b.coeffRef(1,0) = -0.9311078389941825;
103
  SM_b.insert(1,0) = DM_b.coeffRef(1,0) = -0.9311078389941825;
102
  SM_b.insert(1,1) = DM_b.coeffRef(1,1) =  0.5310335762980047;
104
  SM_b.insert(1,1) = DM_b.coeffRef(1,1) =  0.5310335762980047;
103
  SM_b.insert(2,0) = DM_b.coeffRef(2,0) = -0.1225112806872035;
105
  SM_b.insert(2,0) = DM_b.coeffRef(2,0) = -0.1225112806872035;
104
  SM_b.insert(2,1) = DM_b.coeffRef(2,1) =  0.5903998022741264;
106
  SM_b.insert(2,1) = DM_b.coeffRef(2,1) =  0.5903998022741264;
107
105
  SparseMatrix<double,RowMajor> SM_row_a(SM_a), SM_row_b(SM_b);
108
  SparseMatrix<double,RowMajor> SM_row_a(SM_a), SM_row_b(SM_b);
106
109
107
  // test kroneckerProduct(DM_block,DM,DM_fixedSize)
110
  // test kroneckerProduct(DM_block,DM,DM_fixedSize)
108
  Matrix<double, 6, 6> DM_fix_ab = kroneckerProduct(DM_a.topLeftCorner<2,3>(),DM_b);
111
  Matrix<double, 6, 6> DM_fix_ab = kroneckerProduct(DM_a.topLeftCorner<2,3>(),DM_b);
112
109
  CALL_SUBTEST(check_kronecker_product(DM_fix_ab));
113
  CALL_SUBTEST(check_kronecker_product(DM_fix_ab));
110
114
115
  for(unsigned int i=0;i<DM_fix_ab.rows();++i)
116
    for(unsigned int j=0;j<DM_fix_ab.cols();++j)
117
       VERIFY_IS_APPROX(kroneckerProduct(DM_a,DM_b).coeff(i,j), DM_fix_ab(i,j));
118
111
  // test kroneckerProduct(DM,DM,DM_block)
119
  // test kroneckerProduct(DM,DM,DM_block)
112
  MatrixXd DM_block_ab(10,15);
120
  MatrixXd DM_block_ab(10,15);
113
  DM_block_ab.block<6,6>(2,5) = kroneckerProduct(DM_a,DM_b);
121
  DM_block_ab.block<6,6>(2,5) = kroneckerProduct(DM_a,DM_b);
114
  CALL_SUBTEST(check_kronecker_product(DM_block_ab.block<6,6>(2,5)));
122
  CALL_SUBTEST(check_kronecker_product(DM_block_ab.block<6,6>(2,5)));
115
123
116
  // test kroneckerProduct(DM,DM,DM)
124
  // test kroneckerProduct(DM,DM,DM)
117
  MatrixXd DM_ab = kroneckerProduct(DM_a,DM_b);
125
  MatrixXd DM_ab = kroneckerProduct(DM_a,DM_b);
118
  CALL_SUBTEST(check_kronecker_product(DM_ab));
126
  CALL_SUBTEST(check_kronecker_product(DM_ab));
Lines 147-162 void test_kronecker_product() Link Here
147
  SM_a.resize(4,5);
155
  SM_a.resize(4,5);
148
  SM_b.resize(3,2);
156
  SM_b.resize(3,2);
149
  SM_a.resizeNonZeros(0);
157
  SM_a.resizeNonZeros(0);
150
  SM_b.resizeNonZeros(0);
158
  SM_b.resizeNonZeros(0);
151
  SM_a.insert(1,0) = -0.1;
159
  SM_a.insert(1,0) = -0.1;
152
  SM_a.insert(0,3) = -0.2;
160
  SM_a.insert(0,3) = -0.2;
153
  SM_a.insert(2,4) =  0.3;
161
  SM_a.insert(2,4) =  0.3;
154
  SM_a.finalize();
162
  SM_a.finalize();
163
  
155
  SM_b.insert(0,0) =  0.4;
164
  SM_b.insert(0,0) =  0.4;
156
  SM_b.insert(2,1) = -0.5;
165
  SM_b.insert(2,1) = -0.5;
157
  SM_b.finalize();
166
  SM_b.finalize();
158
  SM_ab.resize(1,1);
167
  SM_ab.resize(1,1);
159
  SM_ab.insert(0,0)=37.0;
168
  SM_ab.insert(0,0)=37.0;
160
  SM_ab = kroneckerProduct(SM_a,SM_b);
169
  SM_ab = kroneckerProduct(SM_a,SM_b);
161
  CALL_SUBTEST(check_sparse_kronecker_product(SM_ab));
170
  CALL_SUBTEST(check_sparse_kronecker_product(SM_ab));
162
171

Return to bug 565