mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-08-14 04:35:57 +08:00
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:
parent
b2755edcdd
commit
8a502233d8
@ -48,8 +48,8 @@ class KroneckerProductBase : public ReturnByValue<Derived>
|
|||||||
*/
|
*/
|
||||||
Scalar coeff(Index row, Index col) const
|
Scalar coeff(Index row, Index col) const
|
||||||
{
|
{
|
||||||
return m_A.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(row % m_B.rows(), 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
|
Scalar coeff(Index i) const
|
||||||
{
|
{
|
||||||
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
|
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:
|
protected:
|
||||||
@ -148,10 +148,12 @@ template<typename Lhs, typename Rhs>
|
|||||||
template<typename Dest>
|
template<typename Dest>
|
||||||
void KroneckerProductSparse<Lhs,Rhs>::evalTo(Dest& dst) const
|
void KroneckerProductSparse<Lhs,Rhs>::evalTo(Dest& dst) const
|
||||||
{
|
{
|
||||||
typedef typename Base::Index Index;
|
typedef typename Dest::Index DestIndex;
|
||||||
const Index Br = m_B.rows(),
|
const typename Rhs::Index Br = m_B.rows(),
|
||||||
Bc = m_B.cols();
|
Bc = m_B.cols();
|
||||||
dst.resize(this->rows(), this->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);
|
dst.resizeNonZeros(0);
|
||||||
|
|
||||||
// 1 - evaluate the operands if needed:
|
// 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
|
// compute number of non-zeros per innervectors of dst
|
||||||
{
|
{
|
||||||
VectorXi nnzA = VectorXi::Zero(Dest::IsRowMajor ? m_A.rows() : m_A.cols());
|
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)
|
for (LhsInnerIterator itA(lhsEval,kA); itA; ++itA)
|
||||||
nnzA(Dest::IsRowMajor ? itA.row() : itA.col())++;
|
nnzA(Dest::IsRowMajor ? itA.row() : itA.col())++;
|
||||||
|
|
||||||
VectorXi nnzB = VectorXi::Zero(Dest::IsRowMajor ? m_B.rows() : m_B.cols());
|
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)
|
for (RhsInnerIterator itB(rhsEval,kB); itB; ++itB)
|
||||||
nnzB(Dest::IsRowMajor ? itB.row() : itB.col())++;
|
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()));
|
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 (LhsInnerIterator itA(lhsEval,kA); itA; ++itA)
|
||||||
{
|
{
|
||||||
for (RhsInnerIterator itB(rhsEval,kB); itB; ++itB)
|
for (RhsInnerIterator itB(rhsEval,kB); itB; ++itB)
|
||||||
{
|
{
|
||||||
const Index i = itA.row() * Br + itB.row(),
|
const DestIndex
|
||||||
j = itA.col() * Bc + itB.col();
|
i = DestIndex(itA.row() * Br + itB.row()),
|
||||||
|
j = DestIndex(itA.col() * Bc + itB.col());
|
||||||
dst.insert(i,j) = itA.value() * itB.value();
|
dst.insert(i,j) = itA.value() * itB.value();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -259,7 +262,7 @@ struct traits<KroneckerProductSparse<_Lhs,_Rhs> >
|
|||||||
CoeffReadCost = Dynamic
|
CoeffReadCost = Dynamic
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef SparseMatrix<Scalar> ReturnType;
|
typedef SparseMatrix<Scalar, 0, Index> ReturnType;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // end namespace internal
|
} // end namespace internal
|
||||||
|
Loading…
x
Reference in New Issue
Block a user