-merge patch from Gael Guennebaud adding NumTraits for long long

and long double.
-define scalar-multiple operators only for the current Scalar type;
 thanks to Gael for expaining how to make the compiler understand
 when automatic casting is needed.
-take ScalarMultiple take only 1 template param, again.
 We lose some flexibility especially when dealing with complex numbers,
 but we gain a lot of extensibility to new scalar types.
This commit is contained in:
Benoit Jacob 2008-02-29 13:20:44 +00:00
parent aa8e2bcbde
commit b3268a6e2f
6 changed files with 89 additions and 91 deletions

View File

@ -121,7 +121,7 @@ typename NumTraits<Scalar>::Real MatrixBase<Scalar, Derived>::norm() const
* \sa norm()
*/
template<typename Scalar, typename Derived>
const ScalarMultiple<typename NumTraits<Scalar>::Real, Derived>
const ScalarMultiple<Derived>
MatrixBase<Scalar, Derived>::normalized() const
{
return (*this) / norm();

View File

@ -39,7 +39,7 @@ template<typename MatrixType> class Opposite;
template<typename Lhs, typename Rhs> class Sum;
template<typename Lhs, typename Rhs> class Difference;
template<typename Lhs, typename Rhs> class Product;
template<typename FactorType, typename MatrixType> class ScalarMultiple;
template<typename MatrixType> class ScalarMultiple;
template<typename MatrixType> class Random;
template<typename MatrixType> class Zero;
template<typename MatrixType> class Ones;

View File

@ -215,7 +215,7 @@ template<typename Scalar, typename Derived> class MatrixBase
Scalar dot(const OtherDerived& other) const;
RealScalar norm2() const;
RealScalar norm() const;
const ScalarMultiple<RealScalar, Derived> normalized() const;
const ScalarMultiple<Derived> normalized() const;
template<typename OtherDerived>
bool isOrtho(const OtherDerived& other, RealScalar prec = precision<Scalar>()) const;
bool isOrtho(RealScalar prec = precision<Scalar>()) const;
@ -269,17 +269,16 @@ template<typename Scalar, typename Derived> class MatrixBase
template<typename OtherDerived>
Derived& operator*=(const MatrixBase<Scalar, OtherDerived>& other);
Derived& operator*=(const int& other);
Derived& operator*=(const float& other);
Derived& operator*=(const double& other);
Derived& operator*=(const std::complex<float>& other);
Derived& operator*=(const std::complex<double>& other);
Derived& operator/=(const int& other);
Derived& operator/=(const float& other);
Derived& operator/=(const double& other);
Derived& operator/=(const std::complex<float>& other);
Derived& operator/=(const std::complex<double>& other);
Derived& operator*=(const Scalar& other);
Derived& operator/=(const Scalar& other);
const ScalarMultiple<Derived> operator*(const Scalar& scalar) const;
const ScalarMultiple<Derived> operator/(const Scalar& scalar) const;
friend
const ScalarMultiple<Derived> operator*(const Scalar& scalar,
const MatrixBase& matrix)
{ return matrix*scalar; }
Scalar coeff(int row, int col) const;
Scalar operator()(int row, int col) const;

View File

@ -61,14 +61,14 @@ template<typename T, int Size> class MatrixStorage<T, Size, Dynamic, Dynamic>
int m_rows;
int m_cols;
public:
MatrixStorage(int, int nbRows, int nbCols) : m_rows(nbRows), m_cols(nbCols) {}
MatrixStorage(int, int rows, int cols) : m_rows(rows), m_cols(cols) {}
~MatrixStorage() {}
int rows(void) const {return m_rows;}
int cols(void) const {return m_cols;}
void resize(int, int nbRows, int nbCols)
void resize(int, int rows, int cols)
{
m_rows = nbRows;
m_cols = nbCols;
m_rows = rows;
m_cols = cols;
}
const T *data() const { return m_data; }
T *data() { return m_data; }
@ -80,13 +80,13 @@ template<typename T, int Size, int _Cols> class MatrixStorage<T, Size, Dynamic,
T m_data[Size];
int m_rows;
public:
MatrixStorage(int, int nbRows, int) : m_rows(nbRows) {}
MatrixStorage(int, int rows, int) : m_rows(rows) {}
~MatrixStorage() {}
int rows(void) const {return m_rows;}
int cols(void) const {return _Cols;}
void resize(int size, int nbRows, int)
void resize(int size, int rows, int)
{
m_rows = nbRows;
m_rows = rows;
}
const T *data() const { return m_data; }
T *data() { return m_data; }
@ -98,13 +98,13 @@ template<typename T, int Size, int _Rows> class MatrixStorage<T, Size, _Rows, Dy
T m_data[Size];
int m_cols;
public:
MatrixStorage(int, int nbRows, int nbCols) : m_cols(nbCols) {}
MatrixStorage(int, int, int cols) : m_cols(cols) {}
~MatrixStorage() {}
int rows(void) const {return _Rows;}
int cols(void) const {return m_cols;}
void resize(int size, int, int nbCols)
void resize(int size, int, int cols)
{
m_cols = nbCols;
m_cols = cols;
}
const T *data() const { return m_data; }
T *data() { return m_data; }
@ -117,19 +117,20 @@ template<typename T> class MatrixStorage<T, Dynamic, Dynamic, Dynamic>
int m_rows;
int m_cols;
public:
MatrixStorage(int size, int nbRows, int nbCols) : m_data(new T[size]), m_rows(nbRows), m_cols(nbCols) {}
MatrixStorage(int size, int rows, int cols)
: m_data(new T[size]), m_rows(rows), m_cols(cols) {}
~MatrixStorage() { delete[] m_data; }
int rows(void) const {return m_rows;}
int cols(void) const {return m_cols;}
void resize(int size, int nbRows, int nbCols)
void resize(int size, int rows, int cols)
{
if(size != m_rows*m_cols)
{
delete[] m_data;
m_data = new T[size];
}
m_rows = nbRows;
m_cols = nbCols;
m_rows = rows;
m_cols = cols;
}
const T *data() const { return m_data; }
T *data() { return m_data; }
@ -141,18 +142,18 @@ template<typename T, int _Rows> class MatrixStorage<T, Dynamic, _Rows, Dynamic>
T *m_data;
int m_cols;
public:
MatrixStorage(int size, int, int nbCols) : m_data(new T[size]), m_cols(nbCols) {}
MatrixStorage(int size, int, int cols) : m_data(new T[size]), m_cols(cols) {}
~MatrixStorage() { delete[] m_data; }
static int rows(void) {return _Rows;}
int cols(void) const {return m_cols;}
void resize(int size, int, int nbCols)
void resize(int size, int, int cols)
{
if(size != _Rows*m_cols)
{
delete[] m_data;
m_data = new T[size];
}
m_cols = nbCols;
m_cols = cols;
}
const T *data() const { return m_data; }
T *data() { return m_data; }
@ -164,18 +165,18 @@ template<typename T, int _Cols> class MatrixStorage<T, Dynamic, Dynamic, _Cols>
T *m_data;
int m_rows;
public:
MatrixStorage(int size, int nbRows, int) : m_data(new T[size]), m_rows(nbRows) {}
MatrixStorage(int size, int rows, int) : m_data(new T[size]), m_rows(rows) {}
~MatrixStorage() { delete[] m_data; }
int rows(void) const {return m_rows;}
static int cols(void) {return _Cols;}
void resize(int size, int nbRows, int)
void resize(int size, int rows, int)
{
if(size != m_rows*_Cols)
{
delete[] m_data;
m_data = new T[size];
}
m_rows = nbRows;
m_rows = rows;
}
const T *data() const { return m_data; }
T *data() { return m_data; }

View File

@ -87,4 +87,24 @@ template<typename _Real> struct NumTraits<std::complex<_Real> >
};
};
template<> struct NumTraits<long long int>
{
typedef long long int Real;
typedef long double FloatingPoint;
enum {
IsComplex = 0,
HasFloatingPoint = 0
};
};
template<> struct NumTraits<long double>
{
typedef long double Real;
typedef long double FloatingPoint;
enum {
IsComplex = 0,
HasFloatingPoint = 1
};
};
#endif // EIGEN_NUMTRAITS_H

View File

@ -36,8 +36,8 @@
* It is the return type of the operator* between a matrix or vector and a scalar, and most
* of the time this is the only way it is used.
*/
template<typename FactorType, typename MatrixType> class ScalarMultiple : NoOperatorEquals,
public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<FactorType, MatrixType> >
template<typename MatrixType> class ScalarMultiple : NoOperatorEquals,
public MatrixBase<typename MatrixType::Scalar, ScalarMultiple<MatrixType> >
{
public:
typedef typename MatrixType::Scalar Scalar;
@ -45,7 +45,7 @@ template<typename FactorType, typename MatrixType> class ScalarMultiple : NoOper
friend class MatrixBase<Scalar, ScalarMultiple>;
typedef MatrixBase<Scalar, ScalarMultiple> Base;
ScalarMultiple(const MatRef& matrix, FactorType factor)
ScalarMultiple(const MatRef& matrix, Scalar factor)
: m_matrix(matrix), m_factor(factor) {}
private:
@ -67,62 +67,40 @@ template<typename FactorType, typename MatrixType> class ScalarMultiple : NoOper
protected:
const MatRef m_matrix;
const FactorType m_factor;
const Scalar m_factor;
};
#define EIGEN_MAKE_SCALAR_OPS(FactorType) \
/** \relates MatrixBase \sa class ScalarMultiple */ \
template<typename Scalar, typename Derived> \
const ScalarMultiple<FactorType, Derived> \
operator*(const MatrixBase<Scalar, Derived>& matrix, \
FactorType scalar) \
{ \
return ScalarMultiple<FactorType, Derived>(matrix.ref(), scalar); \
} \
\
/** \relates MatrixBase \sa class ScalarMultiple */ \
template<typename Scalar, typename Derived> \
const ScalarMultiple<FactorType, Derived> \
operator*(FactorType scalar, \
const MatrixBase<Scalar, Derived>& matrix) \
{ \
return ScalarMultiple<FactorType, Derived>(matrix.ref(), scalar); \
} \
\
/** \relates MatrixBase \sa class ScalarMultiple */ \
template<typename Scalar, typename Derived> \
const ScalarMultiple<typename NumTraits<FactorType>::FloatingPoint, Derived> \
operator/(const MatrixBase<Scalar, Derived>& matrix, \
FactorType scalar) \
{ \
assert(NumTraits<Scalar>::HasFloatingPoint); \
return matrix * (static_cast< \
typename NumTraits<FactorType>::FloatingPoint \
>(1) / scalar); \
} \
\
/** \sa class ScalarMultiple */ \
template<typename Scalar, typename Derived> \
Derived & \
MatrixBase<Scalar, Derived>::operator*=(const FactorType &other) \
{ \
return *this = *this * other; \
} \
\
/** \sa class ScalarMultiple */ \
template<typename Scalar, typename Derived> \
Derived & \
MatrixBase<Scalar, Derived>::operator/=(const FactorType &other) \
{ \
return *this = *this / other; \
/** relates MatrixBase sa class ScalarMultiple */
template<typename Scalar, typename Derived>
const ScalarMultiple<Derived>
MatrixBase<Scalar, Derived>::operator*(const Scalar& scalar) const
{
return ScalarMultiple<Derived>(ref(), scalar);
}
/** \relates MatrixBase \sa class ScalarMultiple */
template<typename Scalar, typename Derived>
const ScalarMultiple<Derived>
MatrixBase<Scalar, Derived>::operator/(const Scalar& scalar) const
{
assert(NumTraits<Scalar>::HasFloatingPoint);
return ScalarMultiple<Derived>(ref(), static_cast<Scalar>(1) / scalar);
}
/** \sa ScalarMultiple */
template<typename Scalar, typename Derived>
Derived&
MatrixBase<Scalar, Derived>::operator*=(const Scalar& other)
{
return *this = *this * other;
}
/** \sa ScalarMultiple */
template<typename Scalar, typename Derived>
Derived&
MatrixBase<Scalar, Derived>::operator/=(const Scalar& other)
{
return *this = *this / other;
}
EIGEN_MAKE_SCALAR_OPS(int)
EIGEN_MAKE_SCALAR_OPS(float)
EIGEN_MAKE_SCALAR_OPS(double)
EIGEN_MAKE_SCALAR_OPS(std::complex<float>)
EIGEN_MAKE_SCALAR_OPS(std::complex<double>)
#undef EIGEN_MAKE_SCALAR_OPS
#endif // EIGEN_SCALARMULTIPLE_H