Unified LinSpaced in order to be conform with other setter methods as e.g. Constant.

This commit is contained in:
Hauke Heibel 2010-07-22 14:04:00 +02:00
parent 8e21cef80a
commit 734469e43f
7 changed files with 51 additions and 20 deletions

View File

@ -240,16 +240,29 @@ DenseBase<Derived>::Constant(const Scalar& value)
* Example: \include DenseBase_LinSpaced_seq.cpp
* Output: \verbinclude DenseBase_LinSpaced_seq.out
*
* \sa setLinSpaced(const Scalar&,const Scalar&,Index), LinSpaced(Scalar,Scalar,Index), CwiseNullaryOp
* \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Index,Scalar,Scalar), CwiseNullaryOp
*/
template<typename Derived>
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high, Index size)
DenseBase<Derived>::LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
return DenseBase<Derived>::NullaryExpr(size, ei_linspaced_op<Scalar,false>(low,high,size));
}
/**
* \copydoc DenseBase<Derived>::LinSpaced(Sequential_t, Index, const Scalar&, const Scalar&)
* Special version for fixed size types which does not require the size parameter.
*/
template<typename Derived>
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::SequentialLinSpacedReturnType
DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& high)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, ei_linspaced_op<Scalar,false>(low,high,Derived::SizeAtCompileTime));
}
/**
* \brief Sets a linearly space vector.
*
@ -260,16 +273,29 @@ DenseBase<Derived>::LinSpaced(Sequential_t, const Scalar& low, const Scalar& hig
* Example: \include DenseBase_LinSpaced.cpp
* Output: \verbinclude DenseBase_LinSpaced.out
*
* \sa setLinSpaced(const Scalar&,const Scalar&,Index), LinSpaced(Sequential_t,const Scalar&,const Scalar&,Index), CwiseNullaryOp
* \sa setLinSpaced(Index,const Scalar&,const Scalar&), LinSpaced(Sequential_t,Index,const Scalar&,const Scalar&,Index), CwiseNullaryOp
*/
template<typename Derived>
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high, Index size)
DenseBase<Derived>::LinSpaced(Index size, const Scalar& low, const Scalar& high)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
return DenseBase<Derived>::NullaryExpr(size, ei_linspaced_op<Scalar,true>(low,high,size));
}
/**
* \copydoc DenseBase<Derived>::LinSpaced(Index, const Scalar&, const Scalar&)
* Special version for fixed size types which does not require the size parameter.
*/
template<typename Derived>
EIGEN_STRONG_INLINE const typename DenseBase<Derived>::RandomAccessLinSpacedReturnType
DenseBase<Derived>::LinSpaced(const Scalar& low, const Scalar& high)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
EIGEN_STATIC_ASSERT_FIXED_SIZE(Derived)
return DenseBase<Derived>::NullaryExpr(Derived::SizeAtCompileTime, ei_linspaced_op<Scalar,true>(low,high,Derived::SizeAtCompileTime));
}
/** \returns true if all coefficients in this matrix are approximately equal to \a value, to within precision \a prec */
template<typename Derived>
bool DenseBase<Derived>::isApproxToConstant
@ -360,7 +386,7 @@ DenseStorageBase<Derived>::setConstant(Index rows, Index cols, const Scalar& val
* \sa CwiseNullaryOp
*/
template<typename Derived>
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(const Scalar& low, const Scalar& high, Index size)
EIGEN_STRONG_INLINE Derived& DenseBase<Derived>::setLinSpaced(Index size, const Scalar& low, const Scalar& high)
{
EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived)
return derived() = Derived::NullaryExpr(size, ei_linspaced_op<Scalar,false>(low,high,size));

View File

@ -398,9 +398,13 @@ template<typename Derived> class DenseBase
Constant(const Scalar& value);
static const SequentialLinSpacedReturnType
LinSpaced(Sequential_t, const Scalar& low, const Scalar& high, Index size);
LinSpaced(Sequential_t, Index size, const Scalar& low, const Scalar& high);
static const RandomAccessLinSpacedReturnType
LinSpaced(const Scalar& low, const Scalar& high, Index size);
LinSpaced(Index size, const Scalar& low, const Scalar& high);
static const SequentialLinSpacedReturnType
LinSpaced(Sequential_t, const Scalar& low, const Scalar& high);
static const RandomAccessLinSpacedReturnType
LinSpaced(const Scalar& low, const Scalar& high);
template<typename CustomNullaryOp>
static const CwiseNullaryOp<CustomNullaryOp, Derived>
@ -421,7 +425,8 @@ template<typename Derived> class DenseBase
void fill(const Scalar& value);
Derived& setConstant(const Scalar& value);
Derived& setLinSpaced(const Scalar& low, const Scalar& high, Index size);
Derived& setLinSpaced(Index size, const Scalar& low, const Scalar& high);
Derived& setLinSpaced(const Scalar& low, const Scalar& high);
Derived& setZero();
Derived& setOnes();
Derived& setRandom();

View File

@ -573,7 +573,7 @@ template <typename Scalar, bool RandomAccess> struct ei_linspaced_op
template<typename Index>
EIGEN_STRONG_INLINE const PacketScalar packetOp(Index i, Index = 0) const { return impl.packetOp(i); }
// This proxy object handles the actual required temporaries, the different
// implementations (random vs. sequential access) as well as the piping
// implementations (random vs. sequential access) as well as the
// correct piping to size 2/4 packet operations.
const ei_linspaced_op_impl<Scalar,RandomAccess> impl;
};

View File

@ -1,2 +1,2 @@
cout << VectorXi::LinSpaced(7,10,4).transpose() << endl;
cout << VectorXd::LinSpaced(0.0,1.0,5).transpose() << endl;
cout << VectorXi::LinSpaced(4,7,10).transpose() << endl;
cout << VectorXd::LinSpaced(5,0.0,1.0).transpose() << endl;

View File

@ -1,2 +1,2 @@
cout << VectorXi::LinSpaced(Sequential,7,10,4).transpose() << endl;
cout << VectorXd::LinSpaced(Sequential,0.0,1.0,5).transpose() << endl;
cout << VectorXi::LinSpaced(Sequential,4,7,10).transpose() << endl;
cout << VectorXd::LinSpaced(Sequential,5,0.0,1.0).transpose() << endl;

View File

@ -1,3 +1,3 @@
VectorXf v;
v.setLinSpaced(0.5f,1.5f,5).transpose();
v.setLinSpaced(5,0.5f,1.5f).transpose();
cout << v << endl;

View File

@ -59,7 +59,7 @@ void testVectorType(const VectorType& base)
// check whether the result yields what we expect it to do
VectorType m(base);
m.setLinSpaced(low,high,size);
m.setLinSpaced(size,low,high);
VectorType n(size);
for (int i=0; i<size; ++i)
@ -68,7 +68,7 @@ void testVectorType(const VectorType& base)
VERIFY( (m-n).norm() < std::numeric_limits<Scalar>::epsilon()*10e3 );
// random access version
m = VectorType::LinSpaced(low,high,size);
m = VectorType::LinSpaced(size,low,high);
VERIFY( (m-n).norm() < std::numeric_limits<Scalar>::epsilon()*10e3 );
// These guys sometimes fail! This is not good. Any ideas how to fix them!?
@ -76,7 +76,7 @@ void testVectorType(const VectorType& base)
//VERIFY( m(0) == low );
// sequential access version
m = VectorType::LinSpaced(Sequential,low,high,size);
m = VectorType::LinSpaced(Sequential,size,low,high);
VERIFY( (m-n).norm() < std::numeric_limits<Scalar>::epsilon()*10e3 );
// These guys sometimes fail! This is not good. Any ideas how to fix them!?
@ -86,12 +86,12 @@ void testVectorType(const VectorType& base)
// check whether everything works with row and col major vectors
Matrix<Scalar,Dynamic,1> row_vector(size);
Matrix<Scalar,1,Dynamic> col_vector(size);
row_vector.setLinSpaced(low,high,size);
col_vector.setLinSpaced(low,high,size);
row_vector.setLinSpaced(size,low,high);
col_vector.setLinSpaced(size,low,high);
VERIFY( row_vector.isApprox(col_vector.transpose(), NumTraits<Scalar>::epsilon()));
Matrix<Scalar,Dynamic,1> size_changer(size+50);
size_changer.setLinSpaced(low,high,size);
size_changer.setLinSpaced(size,low,high);
VERIFY( size_changer.size() == size );
}