mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-04-12 19:20:36 +08:00
add matrix conjugation and adjunction.
compilation fixes in the numeric traits.
This commit is contained in:
parent
ae2072406c
commit
9eff685428
@ -31,5 +31,6 @@
|
||||
#include "internal/Block.h"
|
||||
#include "internal/Minor.h"
|
||||
#include "internal/Transpose.h"
|
||||
#include "internal/Conjugate.h"
|
||||
|
||||
#endif // EI_MANIP_H
|
||||
|
69
src/internal/Conjugate.h
Normal file
69
src/internal/Conjugate.h
Normal file
@ -0,0 +1,69 @@
|
||||
// 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_CONJUGATE_H
|
||||
#define EI_CONJUGATE_H
|
||||
|
||||
template<typename MatrixType> class EiConjugate
|
||||
: public EiObject<typename MatrixType::Scalar, EiConjugate<MatrixType> >
|
||||
{
|
||||
public:
|
||||
typedef typename MatrixType::Scalar Scalar;
|
||||
typedef typename MatrixType::Ref MatRef;
|
||||
friend class EiObject<Scalar, EiConjugate<MatrixType> >;
|
||||
|
||||
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||
ColsAtCompileTime = MatrixType::ColsAtCompileTime;
|
||||
|
||||
EiConjugate(const MatRef& matrix) : m_matrix(matrix) {}
|
||||
|
||||
EiConjugate(const EiConjugate& other)
|
||||
: m_matrix(other.m_matrix) {}
|
||||
|
||||
EI_INHERIT_ASSIGNMENT_OPERATORS(EiConjugate)
|
||||
|
||||
private:
|
||||
EiConjugate& _ref() { return *this; }
|
||||
const EiConjugate& _constRef() 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 EiConj(m_matrix.read(row, col));
|
||||
}
|
||||
|
||||
protected:
|
||||
MatRef m_matrix;
|
||||
};
|
||||
|
||||
template<typename Scalar, typename Derived>
|
||||
EiConjugate<Derived>
|
||||
EiObject<Scalar, Derived>::conjugate()
|
||||
{
|
||||
return EiConjugate<Derived>(static_cast<Derived*>(this)->ref());
|
||||
}
|
||||
|
||||
#endif // EI_CONJUGATE_H
|
@ -127,4 +127,5 @@ EI_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
|
||||
#include "MatrixOps.h"
|
||||
#include "ScalarOps.h"
|
||||
|
||||
|
||||
#endif // EI_MATRIX_H
|
||||
|
@ -24,8 +24,8 @@
|
||||
// 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_TRAITS_H
|
||||
#define EI_TRAITS_H
|
||||
#ifndef EI_NUMERIC_H
|
||||
#define EI_NUMERIC_H
|
||||
|
||||
template<typename T> struct EiTraits;
|
||||
|
||||
@ -35,10 +35,10 @@ template<> struct EiTraits<int>
|
||||
typedef double FloatingPoint;
|
||||
typedef double RealFloatingPoint;
|
||||
|
||||
static const int Epsilon = 0;
|
||||
static const bool IsComplex = false;
|
||||
static const bool HasFloatingPoint = false;
|
||||
|
||||
static int epsilon() { return 0; }
|
||||
static int real(const int& x) { return x; }
|
||||
static int imag(const int& x) { EI_UNUSED(x); return 0; }
|
||||
static int conj(const int& x) { return x; }
|
||||
@ -50,7 +50,7 @@ template<> struct EiTraits<int>
|
||||
// "rand()%21" would be bad. always use the high-order bits, not the low-order bits.
|
||||
// note: here (gcc 4.1) static_cast<int> seems to round the nearest int.
|
||||
// I don't know if that's part of the standard.
|
||||
return -10 + static_cast<int>(20.0 * (rand() / (RAND_MAX + 1.0)));
|
||||
return -10 + static_cast<int>(rand() / ((RAND_MAX + 1.0)/20.0));
|
||||
}
|
||||
};
|
||||
|
||||
@ -60,10 +60,10 @@ template<> struct EiTraits<float>
|
||||
typedef float FloatingPoint;
|
||||
typedef float RealFloatingPoint;
|
||||
|
||||
static const float Epsilon;
|
||||
static const bool IsComplex = false;
|
||||
static const bool HasFloatingPoint = true;
|
||||
|
||||
static float epsilon() { return 1e-5f; }
|
||||
static float real(const float& x) { return x; }
|
||||
static float imag(const float& x) { EI_UNUSED(x); return 0; }
|
||||
static float conj(const float& x) { return x; }
|
||||
@ -72,22 +72,20 @@ template<> struct EiTraits<float>
|
||||
static float abs2(const float& x) { return x*x; }
|
||||
static float random()
|
||||
{
|
||||
return 20.0f * rand() / RAND_MAX - 10.0f;
|
||||
return rand() / (RAND_MAX/20.0f) - 10.0f;
|
||||
}
|
||||
};
|
||||
|
||||
const float EiTraits<float>::Epsilon = 1e-5f;
|
||||
|
||||
template<> struct EiTraits<double>
|
||||
{
|
||||
typedef double Real;
|
||||
typedef double FloatingPoint;
|
||||
typedef double RealFloatingPoint;
|
||||
|
||||
static const double Epsilon;
|
||||
static const bool IsComplex = false;
|
||||
static const bool HasFloatingPoint = true;
|
||||
|
||||
static double epsilon() { return 1e-11; }
|
||||
static double real(const double& x) { return x; }
|
||||
static double imag(const double& x) { EI_UNUSED(x); return 0; }
|
||||
static double conj(const double& x) { return x; }
|
||||
@ -96,12 +94,10 @@ template<> struct EiTraits<double>
|
||||
static double abs2(const double& x) { return x*x; }
|
||||
static double random()
|
||||
{
|
||||
return 20.0 * rand() / RAND_MAX - 10.0;
|
||||
return rand() / (RAND_MAX/20.0) - 10.0;
|
||||
}
|
||||
};
|
||||
|
||||
const double EiTraits<double>::Epsilon = 1e-11;
|
||||
|
||||
template<typename _Real> struct EiTraits<std::complex<_Real> >
|
||||
{
|
||||
typedef _Real Real;
|
||||
@ -109,10 +105,10 @@ template<typename _Real> struct EiTraits<std::complex<_Real> >
|
||||
typedef std::complex<double> FloatingPoint;
|
||||
typedef typename EiTraits<Real>::FloatingPoint RealFloatingPoint;
|
||||
|
||||
static const Real Epsilon;
|
||||
static const bool IsComplex = true;
|
||||
static const bool HasFloatingPoint = EiTraits<Real>::HasFloatingPoint;
|
||||
|
||||
static Real epsilon() { return EiTraits<Real>::epsilon(); }
|
||||
static Real real(const Complex& x) { return std::real(x); }
|
||||
static Real imag(const Complex& x) { return std::imag(x); }
|
||||
static Complex conj(const Complex& x) { return std::conj(x); }
|
||||
@ -128,10 +124,6 @@ template<typename _Real> struct EiTraits<std::complex<_Real> >
|
||||
}
|
||||
};
|
||||
|
||||
template<typename _Real>
|
||||
const _Real EiTraits<std::complex<_Real> >::Epsilon
|
||||
= EiTraits<_Real>::Epsilon;
|
||||
|
||||
template<typename T> typename EiTraits<T>::Real EiReal(const T& x)
|
||||
{ return EiTraits<T>::real(x); }
|
||||
|
||||
@ -155,13 +147,13 @@ template<typename T> T EiRandom()
|
||||
|
||||
template<typename T> bool EiNegligible(const T& a, const T& b)
|
||||
{
|
||||
return(EiAbs(a) <= EiAbs(b) * EiTraits<T>::Epsilon);
|
||||
return(EiAbs(a) <= EiAbs(b) * EiTraits<T>::epsilon());
|
||||
}
|
||||
|
||||
template<typename T> bool EiApprox(const T& a, const T& b)
|
||||
{
|
||||
if(EiTraits<T>::IsFloat)
|
||||
return(EiAbs(a - b) <= std::min(EiAbs(a), EiAbs(b)) * EiTraits<T>::Epsilon);
|
||||
return(EiAbs(a - b) <= std::min(EiAbs(a), EiAbs(b)) * EiTraits<T>::epsilon());
|
||||
else
|
||||
return(a == b);
|
||||
}
|
||||
@ -174,4 +166,4 @@ template<typename T> bool EiLessThanOrApprox(const T& a, const T& b)
|
||||
return(a <= b);
|
||||
}
|
||||
|
||||
#endif // EI_TRAITS_H
|
||||
#endif // EI_NUMERIC_H
|
||||
|
@ -95,6 +95,8 @@ template<typename Scalar, typename Derived> class EiObject
|
||||
EiMinor<Derived> minor(int row, int col);
|
||||
EiBlock<Derived> block(int startRow, int endRow, int startCol, int endCol);
|
||||
EiTranspose<Derived> transpose();
|
||||
EiConjugate<Derived> conjugate();
|
||||
EiTranspose<EiConjugate<Derived> > adjoint() { return conjugate().transpose(); }
|
||||
|
||||
template<typename OtherDerived>
|
||||
EiMatrixProduct<Derived, OtherDerived>
|
||||
|
@ -49,6 +49,7 @@ template<typename MatrixType> class EiColumn;
|
||||
template<typename MatrixType> class EiMinor;
|
||||
template<typename MatrixType> class EiBlock;
|
||||
template<typename MatrixType> class EiTranspose;
|
||||
template<typename MatrixType> class EiConjugate;
|
||||
template<typename Lhs, typename Rhs> class EiSum;
|
||||
template<typename Lhs, typename Rhs> class EiDifference;
|
||||
template<typename Lhs, typename Rhs> class EiMatrixProduct;
|
||||
|
12
test/main.h
12
test/main.h
@ -47,11 +47,13 @@ class EigenTest : public QObject
|
||||
void testMatrixManip();
|
||||
};
|
||||
|
||||
template<typename T> T TestEpsilon();
|
||||
template<> int TestEpsilon<int>() { return 0; }
|
||||
template<> float TestEpsilon<float>() { return 1e-2f; }
|
||||
template<> double TestEpsilon<double>() { return 1e-4; }
|
||||
template<typename T> T TestEpsilon<std::complex<T> >() { return TestEpsilon<T>(); }
|
||||
template<typename T> inline typename EiTraits<T>::Real TestEpsilon();
|
||||
template<> inline int TestEpsilon<int>() { return 0; }
|
||||
template<> inline float TestEpsilon<float>() { return 1e-2f; }
|
||||
template<> inline double TestEpsilon<double>() { return 1e-4; }
|
||||
template<> inline int TestEpsilon<std::complex<int> >() { return TestEpsilon<int>(); }
|
||||
template<> inline float TestEpsilon<std::complex<float> >() { return TestEpsilon<float>(); }
|
||||
template<> inline double TestEpsilon<std::complex<double> >() { return TestEpsilon<double>(); }
|
||||
|
||||
template<typename T> bool TestNegligible(const T& a, const T& b)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user