more changes in ScalarMultiple, reintroduce FloatingPoint in NumTraits, improve

examples
This commit is contained in:
Benoit Jacob 2007-12-26 09:25:00 +00:00
parent 05a49547e1
commit 6b9370e0f0
7 changed files with 57 additions and 25 deletions

View File

@ -31,6 +31,7 @@ template<typename T> struct NumTraits;
template<> struct NumTraits<int>
{
typedef int Real;
typedef double FloatingPoint;
static const bool IsComplex = false;
static const bool HasFloatingPoint = false;
};
@ -38,6 +39,7 @@ template<> struct NumTraits<int>
template<> struct NumTraits<float>
{
typedef float Real;
typedef float FloatingPoint;
static const bool IsComplex = false;
static const bool HasFloatingPoint = true;
};
@ -45,6 +47,7 @@ template<> struct NumTraits<float>
template<> struct NumTraits<double>
{
typedef double Real;
typedef double FloatingPoint;
static const bool IsComplex = false;
static const bool HasFloatingPoint = true;
};
@ -52,6 +55,7 @@ template<> struct NumTraits<double>
template<typename _Real> struct NumTraits<std::complex<_Real> >
{
typedef _Real Real;
typedef std::complex<_Real> FloatingPoint;
static const bool IsComplex = true;
static const bool HasFloatingPoint = NumTraits<Real>::HasFloatingPoint;
};

View File

@ -76,12 +76,14 @@ operator*(FactorType scalar, \
} \
\
template<typename Scalar, typename Derived> \
const ScalarMultiple<FactorType, Derived> \
const ScalarMultiple<typename NumTraits<FactorType>::FloatingPoint, Derived> \
operator/(const MatrixBase<Scalar, Derived>& matrix, \
FactorType scalar) \
{ \
assert(NumTraits<Scalar>::HasFloatingPoint); \
return matrix * (static_cast<Scalar>(1) / scalar); \
return matrix * (static_cast< \
typename NumTraits<FactorType>::FloatingPoint \
>(1) / scalar); \
} \
\
template<typename Scalar, typename Derived> \

View File

@ -7,17 +7,20 @@ Eigen::Block<Derived, 2, 2>
topLeft2x2Corner(MatrixBase<Scalar, Derived>& m)
{
return Eigen::Block<Derived, 2, 2>(m.ref(), 0, 0);
// note: tempting as it is, writing "m.block<2,2>(0,0)" here
// causes a compile error with g++ 4.2, apparently due to
// g++ getting confused by the many template types and
// template arguments involved.
}
template<typename Scalar, typename Derived>
const Eigen::Block<Derived, 2, 2>
topLeft2x2Corner(const MatrixBase<Scalar, Derived>& m)
{
return Eigen::Block<Derived, 2, 2>(m.ref(), 0, 0);
}
int main(int, char**)
{
Matrix3d m = Matrix3d::identity();
cout << topLeft2x2Corner(m) << endl;
topLeft2x2Corner(m) *= 2;
cout << topLeft2x2Corner(4*m) << endl; // calls the const version
topLeft2x2Corner(m) *= 2; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl;
return 0;
}

View File

@ -3,14 +3,16 @@ USING_PART_OF_NAMESPACE_EIGEN
using namespace std;
template<typename Scalar, typename Derived>
Eigen::Cast<double, Derived>
castToDouble(const MatrixBase<Scalar, Derived>& m)
const Eigen::Cast<
typename Eigen::NumTraits<Scalar>::FloatingPoint,
Derived
>
castToFloatingPoint(const MatrixBase<Scalar, Derived>& m)
{
return Eigen::Cast<double, Derived>(m.ref());
// note: tempting as it is, writing "m.cast<double>()" here
// causes a compile error with g++ 4.2, apparently due to
// g++ getting confused by the many template types and
// template arguments involved.
return Eigen::Cast<
typename Eigen::NumTraits<Scalar>::FloatingPoint,
Derived
>(m.ref());
}
int main(int, char**)
@ -18,6 +20,6 @@ int main(int, char**)
Matrix2i m = Matrix2i::random();
cout << "Here's the matrix m. It has coefficients of type int."
<< endl << m << endl;
cout << "Here's 0.05*m:" << endl << 0.05 * castToDouble(m) << endl;
cout << "Here's m/20:" << endl << castToFloatingPoint(m)/20 << endl;
return 0;
}

View File

@ -6,14 +6,21 @@ template<typename Scalar, typename Derived>
Eigen::Column<Derived>
firstColumn(MatrixBase<Scalar, Derived>& m)
{
return m.col(0);
return Eigen::Column<Derived>(m.ref(), 0);
}
template<typename Scalar, typename Derived>
const Eigen::Column<Derived>
firstColumn(const MatrixBase<Scalar, Derived>& m)
{
return Eigen::Column<Derived>(m.ref(), 0);
}
int main(int, char**)
{
Matrix4d m = Matrix4d::identity();
cout << firstColumn(m) << endl;
firstColumn(m) *= 5;
cout << firstColumn(2*m) << endl; // calls the const version
firstColumn(m) *= 5; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl;
return 0;
}

View File

@ -6,14 +6,21 @@ template<typename Scalar, typename Derived>
Eigen::DynBlock<Derived>
topLeftCorner(MatrixBase<Scalar, Derived>& m, int rows, int cols)
{
return m.dynBlock(0, 0, rows, cols);
return Eigen::DynBlock<Derived>(m.ref(), 0, 0, rows, cols);
}
template<typename Scalar, typename Derived>
const Eigen::DynBlock<Derived>
topLeftCorner(const MatrixBase<Scalar, Derived>& m, int rows, int cols)
{
return Eigen::DynBlock<Derived>(m.ref(), 0, 0, rows, cols);
}
int main(int, char**)
{
Matrix4d m = Matrix4d::identity();
cout << topLeftCorner(m, 2, 3) << endl;
topLeftCorner(m, 2, 3) *= 5;
cout << topLeftCorner(4*m, 2, 3) << endl; // calls the const version
topLeftCorner(m, 2, 3) *= 5; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl;
return 0;
}

View File

@ -6,14 +6,21 @@ template<typename Scalar, typename Derived>
Eigen::Row<Derived>
firstRow(MatrixBase<Scalar, Derived>& m)
{
return m.row(0);
return Eigen::Row<Derived>(m.ref(), 0);
}
template<typename Scalar, typename Derived>
const Eigen::Row<Derived>
firstRow(const MatrixBase<Scalar, Derived>& m)
{
return Eigen::Row<Derived>(m.ref(), 0);
}
int main(int, char**)
{
Matrix4d m = Matrix4d::identity();
cout << firstRow(m) << endl;
firstRow(m) *= 5;
cout << firstRow(2*m) << endl; // calls the const version
firstRow(m) *= 5; // calls the non-const version
cout << "Now the matrix m is:" << endl << m << endl;
return 0;
}