mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-02-11 18:00:51 +08:00
bug #1457: add setUnit() methods for consistency.
This commit is contained in:
parent
bc4dae9aeb
commit
9deee79922
@ -861,6 +861,42 @@ template<typename Derived>
|
|||||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const typename MatrixBase<Derived>::BasisReturnType MatrixBase<Derived>::UnitW()
|
||||||
{ return Derived::Unit(3); }
|
{ return Derived::Unit(3); }
|
||||||
|
|
||||||
|
/** \brief Set the coefficients of \c *this to the i-th unit (basis) vector
|
||||||
|
*
|
||||||
|
* \param i index of the unique coefficient to be set to 1
|
||||||
|
*
|
||||||
|
* \only_for_vectors
|
||||||
|
*
|
||||||
|
* \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Unit(Index,Index)
|
||||||
|
*/
|
||||||
|
template<typename Derived>
|
||||||
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index i)
|
||||||
|
{
|
||||||
|
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
|
||||||
|
eigen_assert(i<size());
|
||||||
|
derived().setZero();
|
||||||
|
derived().coeffRef(i) = Scalar(1);
|
||||||
|
return derived();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** \brief Resizes to the given \a newSize, and writes the i-th unit (basis) vector into *this.
|
||||||
|
*
|
||||||
|
* \param newSize the new size of the vector
|
||||||
|
* \param i index of the unique coefficient to be set to 1
|
||||||
|
*
|
||||||
|
* \only_for_vectors
|
||||||
|
*
|
||||||
|
* \sa MatrixBase::setIdentity(), class CwiseNullaryOp, MatrixBase::Unit(Index,Index)
|
||||||
|
*/
|
||||||
|
template<typename Derived>
|
||||||
|
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived& MatrixBase<Derived>::setUnit(Index newSize, Index i)
|
||||||
|
{
|
||||||
|
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
|
||||||
|
eigen_assert(i<newSize);
|
||||||
|
derived().resize(newSize);
|
||||||
|
return setUnit(i);
|
||||||
|
}
|
||||||
|
|
||||||
} // end namespace Eigen
|
} // end namespace Eigen
|
||||||
|
|
||||||
#endif // EIGEN_CWISE_NULLARY_OP_H
|
#endif // EIGEN_CWISE_NULLARY_OP_H
|
||||||
|
@ -268,6 +268,8 @@ template<typename Derived> class MatrixBase
|
|||||||
Derived& setIdentity();
|
Derived& setIdentity();
|
||||||
EIGEN_DEVICE_FUNC
|
EIGEN_DEVICE_FUNC
|
||||||
Derived& setIdentity(Index rows, Index cols);
|
Derived& setIdentity(Index rows, Index cols);
|
||||||
|
EIGEN_DEVICE_FUNC Derived& setUnit(Index i);
|
||||||
|
EIGEN_DEVICE_FUNC Derived& setUnit(Index newSize, Index i);
|
||||||
|
|
||||||
bool isIdentity(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
bool isIdentity(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||||
bool isDiagonal(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
bool isDiagonal(const RealScalar& prec = NumTraits<Scalar>::dummy_precision()) const;
|
||||||
|
@ -261,6 +261,8 @@ x.setIdentity();
|
|||||||
Vector3f::UnitX() // 1 0 0
|
Vector3f::UnitX() // 1 0 0
|
||||||
Vector3f::UnitY() // 0 1 0
|
Vector3f::UnitY() // 0 1 0
|
||||||
Vector3f::UnitZ() // 0 0 1
|
Vector3f::UnitZ() // 0 0 1
|
||||||
|
Vector4f::Unit(i)
|
||||||
|
x.setUnit(i);
|
||||||
\endcode
|
\endcode
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
@ -278,6 +280,7 @@ N/A
|
|||||||
|
|
||||||
|
|
||||||
VectorXf::Unit(size,i)
|
VectorXf::Unit(size,i)
|
||||||
|
x.setUnit(size,i);
|
||||||
VectorXf::Unit(4,1) == Vector4f(0,1,0,0)
|
VectorXf::Unit(4,1) == Vector4f(0,1,0,0)
|
||||||
== Vector4f::UnitY()
|
== Vector4f::UnitY()
|
||||||
\endcode
|
\endcode
|
||||||
@ -285,7 +288,12 @@ VectorXf::Unit(4,1) == Vector4f(0,1,0,0)
|
|||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
Note that it is allowed to call any of the \c set* functions to a dynamic-sized vector or matrix without passing new sizes.
|
||||||
|
For instance:
|
||||||
|
\code
|
||||||
|
MatrixXi M(3,3);
|
||||||
|
M.setIdentity();
|
||||||
|
\endcode
|
||||||
|
|
||||||
\subsection QuickRef_Map Mapping external arrays
|
\subsection QuickRef_Map Mapping external arrays
|
||||||
|
|
||||||
|
@ -191,6 +191,24 @@ void testVectorType(const VectorType& base)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// test setUnit()
|
||||||
|
if(m.size()>0)
|
||||||
|
{
|
||||||
|
for(Index k=0; k<10; ++k)
|
||||||
|
{
|
||||||
|
Index i = internal::random<Index>(0,m.size()-1);
|
||||||
|
m.setUnit(i);
|
||||||
|
VERIFY_IS_APPROX( m, VectorType::Unit(m.size(), i) );
|
||||||
|
}
|
||||||
|
if(VectorType::SizeAtCompileTime==Dynamic)
|
||||||
|
{
|
||||||
|
Index i = internal::random<Index>(0,2*m.size()-1);
|
||||||
|
m.setUnit(2*m.size(),i);
|
||||||
|
VERIFY_IS_APPROX( m, VectorType::Unit(m.size(),i) );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename MatrixType>
|
template<typename MatrixType>
|
||||||
|
Loading…
Reference in New Issue
Block a user