add cast<newtype>() function to casts the scalars to another type.

This commit is contained in:
Benoit Jacob 2007-10-19 14:46:08 +00:00
parent d6f26dc8eb
commit 49c78643da
6 changed files with 77 additions and 3 deletions

View File

@ -11,6 +11,7 @@ namespace Eigen {
#include "Core/MatrixRef.h"
#include "Core/MatrixStorage.h"
#include "Core/Matrix.h"
#include "Core/Cast.h"
#include "Core/Eval.h"
#include "Core/ScalarMultiple.h"
#include "Core/Sum.h"

70
src/Core/Cast.h Normal file
View File

@ -0,0 +1,70 @@
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2006-2007 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 or (at your option) any later version.
//
// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the GNU General Public License along
// with Eigen; if not, write to the Free Software Foundation, Inc., 51
// Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
//
// As a special exception, if other files instantiate templates or use macros
// or functions from this file, or you compile this file and link it
// with other works to produce a work based on this file, this file does not
// by itself cause the resulting work to be covered by the GNU General Public
// License. This exception does not invalidate any other reasons why a work
// based on this file might be covered by the GNU General Public License.
#ifndef EI_CAST_H
#define EI_CAST_H
template<typename NewScalar, typename MatrixType> class Cast
: public Object<NewScalar, Cast<NewScalar, MatrixType> >
{
public:
typedef NewScalar Scalar;
typedef typename MatrixType::Ref MatRef;
friend class Object<Scalar, Cast<Scalar, MatrixType> >;
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
ColsAtCompileTime = MatrixType::ColsAtCompileTime;
Cast(const MatRef& matrix) : m_matrix(matrix) {}
Cast(const Cast& other)
: m_matrix(other.m_matrix) {}
// assignments are illegal but we still want to intercept them and get clean compile errors
EI_INHERIT_ASSIGNMENT_OPERATORS(Cast)
private:
const Cast& _ref() const { return *this; }
int _rows() const { return m_matrix.rows(); }
int _cols() const { return m_matrix.cols(); }
Scalar _read(int row, int col) const
{
return static_cast<Scalar>(m_matrix.read(row, col));
}
protected:
MatRef m_matrix;
};
template<typename Scalar, typename Derived>
template<typename NewScalar>
Cast<NewScalar, Derived>
Object<Scalar, Derived>::cast() const
{
return Cast<NewScalar, Derived>(static_cast<const Derived*>(this)->ref());
}
#endif // EI_CAST_H

View File

@ -42,7 +42,8 @@ template<typename MatrixType> class Conjugate
Conjugate(const Conjugate& other)
: m_matrix(other.m_matrix) {}
EI_INHERIT_ASSIGNMENT_OPERATORS(Conjugate)
// assignments are illegal but we still want to intercept them and get clean compile errors
EI_INHERIT_ASSIGNMENT_OPERATORS(Conjugate)
private:
const Conjugate& _ref() const { return *this; }

View File

@ -28,7 +28,7 @@
template<typename _Scalar, int _Rows, int _Cols>
class Matrix : public Object<_Scalar, Matrix<_Scalar, _Rows, _Cols> >,
public MatrixStorage<_Scalar, _Rows, _Cols>
public MatrixStorage<_Scalar, _Rows, _Cols>
{
public:
friend class Object<_Scalar, Matrix>;

View File

@ -91,6 +91,8 @@ template<typename Scalar, typename Derived> class Object
return *static_cast<Derived*>(this);
}
template<typename NewScalar> Cast<NewScalar, Derived> cast() const;
Row<Derived> row(int i) const;
Column<Derived> col(int i) const;
Minor<Derived> minor(int row, int col) const;
@ -102,7 +104,6 @@ template<typename Scalar, typename Derived> class Object
template<typename OtherDerived>
Scalar dot(const OtherDerived& other) const;
RealScalar norm2() const;
RealScalar norm() const;
ScalarMultiple<Derived> normalized() const;

View File

@ -43,6 +43,7 @@ using Eigen::Matrix;
//forward declarations
template<typename _Scalar, int _Rows, int _Cols> class Matrix;
template<typename MatrixType> class MatrixRef;
template<typename NewScalar, typename MatrixType> class Cast;
template<typename MatrixType> class Row;
template<typename MatrixType> class Column;
template<typename MatrixType> class Minor;