Correcting the ReturnType in traits<KroneckerProduct<>> to include the correct Index type.

Fixed mixup of types Rhs::Index and Lhs:Index in various loop variables.
Added explicit type conversion for arithmetic expressions which may return a wider type.
This commit is contained in:
Georg Drenkhahn 2014-09-21 23:19:29 +02:00
parent b2755edcdd
commit 8a502233d8

View File

@ -48,8 +48,8 @@ class KroneckerProductBase : public ReturnByValue<Derived>
*/
Scalar coeff(Index row, Index col) const
{
return m_A.coeff(row / m_B.rows(), col / m_B.cols()) *
m_B.coeff(row % m_B.rows(), col % m_B.cols());
return m_A.coeff(typename Lhs::Index(row / m_B.rows()), typename Lhs::Index(col / m_B.cols())) *
m_B.coeff(typename Rhs::Index(row % m_B.rows()), typename Rhs::Index(col % m_B.cols()));
}
/*!
@ -59,7 +59,7 @@ class KroneckerProductBase : public ReturnByValue<Derived>
Scalar coeff(Index i) const
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
return m_A.coeff(i / m_A.size()) * m_B.coeff(i % m_A.size());
return m_A.coeff(typename Lhs::Index(i / m_A.size())) * m_B.coeff(typename Rhs::Index(i % m_A.size()));
}
protected:
@ -148,10 +148,12 @@ template<typename Lhs, typename Rhs>
template<typename Dest>
void KroneckerProductSparse<Lhs,Rhs>::evalTo(Dest& dst) const
{
typedef typename Base::Index Index;
const Index Br = m_B.rows(),
Bc = m_B.cols();
dst.resize(this->rows(), this->cols());
typedef typename Dest::Index DestIndex;
const typename Rhs::Index Br = m_B.rows(),
Bc = m_B.cols();
eigen_assert(this->rows() <= NumTraits<DestIndex>::highest());
eigen_assert(this->cols() <= NumTraits<DestIndex>::highest());
dst.resize(DestIndex(this->rows()), DestIndex(this->cols()));
dst.resizeNonZeros(0);
// 1 - evaluate the operands if needed:
@ -182,12 +184,12 @@ void KroneckerProductSparse<Lhs,Rhs>::evalTo(Dest& dst) const
// compute number of non-zeros per innervectors of dst
{
VectorXi nnzA = VectorXi::Zero(Dest::IsRowMajor ? m_A.rows() : m_A.cols());
for (Index kA=0; kA < m_A.outerSize(); ++kA)
for (typename Lhs::Index kA=0; kA < m_A.outerSize(); ++kA)
for (LhsInnerIterator itA(lhsEval,kA); itA; ++itA)
nnzA(Dest::IsRowMajor ? itA.row() : itA.col())++;
VectorXi nnzB = VectorXi::Zero(Dest::IsRowMajor ? m_B.rows() : m_B.cols());
for (Index kB=0; kB < m_B.outerSize(); ++kB)
for (typename Rhs::Index kB=0; kB < m_B.outerSize(); ++kB)
for (RhsInnerIterator itB(rhsEval,kB); itB; ++itB)
nnzB(Dest::IsRowMajor ? itB.row() : itB.col())++;
@ -195,16 +197,17 @@ void KroneckerProductSparse<Lhs,Rhs>::evalTo(Dest& dst) const
dst.reserve(VectorXi::Map(nnzAB.data(), nnzAB.size()));
}
for (Index kA=0; kA < m_A.outerSize(); ++kA)
for (typename Lhs::Index kA=0; kA < m_A.outerSize(); ++kA)
{
for (Index kB=0; kB < m_B.outerSize(); ++kB)
for (typename Rhs::Index kB=0; kB < m_B.outerSize(); ++kB)
{
for (LhsInnerIterator itA(lhsEval,kA); itA; ++itA)
{
for (RhsInnerIterator itB(rhsEval,kB); itB; ++itB)
{
const Index i = itA.row() * Br + itB.row(),
j = itA.col() * Bc + itB.col();
const DestIndex
i = DestIndex(itA.row() * Br + itB.row()),
j = DestIndex(itA.col() * Bc + itB.col());
dst.insert(i,j) = itA.value() * itB.value();
}
}
@ -259,7 +262,7 @@ struct traits<KroneckerProductSparse<_Lhs,_Rhs> >
CoeffReadCost = Dynamic
};
typedef SparseMatrix<Scalar> ReturnType;
typedef SparseMatrix<Scalar, 0, Index> ReturnType;
};
} // end namespace internal