mirror of
https://gitlab.com/libeigen/eigen.git
synced 2025-09-25 07:43:14 +08:00
Give the axe to the aliasing system.
Improve the evaluation system instead.
This commit is contained in:
parent
55227b1f63
commit
a2dd9dd6f9
@ -12,9 +12,9 @@ int main(int, char **)
|
|||||||
m(1,1) = 4;
|
m(1,1) = 4;
|
||||||
|
|
||||||
n = m;
|
n = m;
|
||||||
n = eval(n*n);
|
n = eval(n*n+n);
|
||||||
cout << n << endl;
|
cout << n << endl;
|
||||||
#if 0
|
|
||||||
cout << "Here is a 2x2 matrix m:" << endl << m << endl;
|
cout << "Here is a 2x2 matrix m:" << endl << m << endl;
|
||||||
cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl;
|
cout << "Let us now build a 4x4 matrix m2 by assembling together four 2x2 blocks." << endl;
|
||||||
Matrix<double,4,4> m2; // dynamic matrix with initial size 4x4 and uninitialized entries
|
Matrix<double,4,4> m2; // dynamic matrix with initial size 4x4 and uninitialized entries
|
||||||
@ -50,10 +50,9 @@ int main(int, char **)
|
|||||||
<< "overwritten _while_ the matrix product m * m is being computed." << endl
|
<< "overwritten _while_ the matrix product m * m is being computed." << endl
|
||||||
<< "This is the counterpart of eliminating temporary objects!" << endl
|
<< "This is the counterpart of eliminating temporary objects!" << endl
|
||||||
<< "Anyway, if you want to store m * m into m, you can do this:" << endl
|
<< "Anyway, if you want to store m * m into m, you can do this:" << endl
|
||||||
<< " m.alias() = m * m;" << endl;
|
<< " m = eval(m * m);" << endl;
|
||||||
m = m_save;
|
m = m_save;
|
||||||
m.alias() = m * m;
|
m = eval(m * m);
|
||||||
cout << "And m is now:" << endl << m << endl << "as was expected." << endl;
|
cout << "And m is now:" << endl << m << endl << "as was expected." << endl;
|
||||||
#endif
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,9 @@ template<typename MatrixType> class MatrixBlock
|
|||||||
typedef typename MatrixType::Ref MatRef;
|
typedef typename MatrixType::Ref MatRef;
|
||||||
friend class EigenBase<Scalar, MatrixBlock<MatrixType> >;
|
friend class EigenBase<Scalar, MatrixBlock<MatrixType> >;
|
||||||
typedef MatrixBlock Ref;
|
typedef MatrixBlock Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = DynamicSize,
|
||||||
|
ColsAtCompileTime = DynamicSize;
|
||||||
|
|
||||||
MatrixBlock(const MatRef& matrix,
|
MatrixBlock(const MatRef& matrix,
|
||||||
int startRow, int endRow,
|
int startRow, int endRow,
|
||||||
|
@ -125,7 +125,6 @@ EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
|
|||||||
|
|
||||||
} // namespace Eigen
|
} // namespace Eigen
|
||||||
|
|
||||||
#include "MatrixAlias.h"
|
|
||||||
#include "MatrixOps.h"
|
#include "MatrixOps.h"
|
||||||
#include "ScalarOps.h"
|
#include "ScalarOps.h"
|
||||||
#include "RowAndCol.h"
|
#include "RowAndCol.h"
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
// 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 EIGEN_MATRIXALIAS_H
|
|
||||||
#define EIGEN_MATRIXALIAS_H
|
|
||||||
|
|
||||||
namespace Eigen
|
|
||||||
{
|
|
||||||
|
|
||||||
template<typename MatrixType> class MatrixAlias
|
|
||||||
: public EigenBase<typename MatrixType::Scalar, MatrixAlias<MatrixType> >
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
typedef typename MatrixType::Scalar Scalar;
|
|
||||||
typedef MatrixRef<MatrixAlias> Ref;
|
|
||||||
typedef EigenBase<typename MatrixType::Scalar, MatrixAlias> Base;
|
|
||||||
friend class EigenBase<typename MatrixType::Scalar, MatrixAlias>;
|
|
||||||
|
|
||||||
MatrixAlias(MatrixType& matrix) : m_aliased(matrix), m_tmp(matrix) {}
|
|
||||||
MatrixAlias(const MatrixAlias& other) : m_aliased(other.m_aliased), m_tmp(other.m_tmp) {}
|
|
||||||
|
|
||||||
~MatrixAlias()
|
|
||||||
{
|
|
||||||
m_aliased = m_tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
INHERIT_ASSIGNMENT_OPERATORS(MatrixAlias)
|
|
||||||
|
|
||||||
private:
|
|
||||||
Ref _ref() const
|
|
||||||
{
|
|
||||||
return Ref(*const_cast<MatrixAlias*>(this));
|
|
||||||
}
|
|
||||||
|
|
||||||
int _rows() const { return m_tmp.rows(); }
|
|
||||||
int _cols() const { return m_tmp.cols(); }
|
|
||||||
|
|
||||||
Scalar& _write(int row, int col)
|
|
||||||
{
|
|
||||||
return m_tmp.write(row, col);
|
|
||||||
}
|
|
||||||
|
|
||||||
Scalar _read(int row, int col) const
|
|
||||||
{
|
|
||||||
return m_aliased.read(row, col);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
MatrixRef<MatrixType> m_aliased;
|
|
||||||
MatrixType m_tmp;
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename _Scalar, int _Rows, int _Cols>
|
|
||||||
typename Matrix<_Scalar, _Rows, _Cols>::Alias
|
|
||||||
Matrix<_Scalar, _Rows, _Cols>::alias()
|
|
||||||
{
|
|
||||||
return Alias(*this);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Eigen
|
|
||||||
|
|
||||||
#endif // EIGEN_MATRIXALIAS_H
|
|
@ -37,6 +37,9 @@ template<typename Lhs, typename Rhs> class MatrixSum
|
|||||||
typedef typename Rhs::Ref RhsRef;
|
typedef typename Rhs::Ref RhsRef;
|
||||||
friend class EigenBase<Scalar, MatrixSum>;
|
friend class EigenBase<Scalar, MatrixSum>;
|
||||||
typedef MatrixSum Ref;
|
typedef MatrixSum Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
||||||
|
ColsAtCompileTime = Rhs::ColsAtCompileTime;
|
||||||
|
|
||||||
MatrixSum(const LhsRef& lhs, const RhsRef& rhs)
|
MatrixSum(const LhsRef& lhs, const RhsRef& rhs)
|
||||||
: m_lhs(lhs), m_rhs(rhs)
|
: m_lhs(lhs), m_rhs(rhs)
|
||||||
@ -75,6 +78,9 @@ template<typename Lhs, typename Rhs> class MatrixDifference
|
|||||||
friend class EigenBase<Scalar, MatrixDifference>;
|
friend class EigenBase<Scalar, MatrixDifference>;
|
||||||
typedef MatrixDifference Ref;
|
typedef MatrixDifference Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = Lhs::RowsAtCompileTime,
|
||||||
|
ColsAtCompileTime = Rhs::ColsAtCompileTime;
|
||||||
|
|
||||||
MatrixDifference(const LhsRef& lhs, const RhsRef& rhs)
|
MatrixDifference(const LhsRef& lhs, const RhsRef& rhs)
|
||||||
: m_lhs(lhs), m_rhs(rhs)
|
: m_lhs(lhs), m_rhs(rhs)
|
||||||
{
|
{
|
||||||
|
@ -36,6 +36,9 @@ template<typename MatrixType> class MatrixMinor
|
|||||||
typedef typename MatrixType::Ref MatRef;
|
typedef typename MatrixType::Ref MatRef;
|
||||||
friend class EigenBase<Scalar, MatrixMinor<MatrixType> >;
|
friend class EigenBase<Scalar, MatrixMinor<MatrixType> >;
|
||||||
typedef MatrixMinor Ref;
|
typedef MatrixMinor Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime - 1,
|
||||||
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime - 1;
|
||||||
|
|
||||||
MatrixMinor(const MatRef& matrix,
|
MatrixMinor(const MatRef& matrix,
|
||||||
int row, int col = 0)
|
int row, int col = 0)
|
||||||
|
@ -37,6 +37,9 @@ template<typename MatrixType> class MatrixRow
|
|||||||
friend class EigenBase<Scalar, MatrixRow<MatrixType> >;
|
friend class EigenBase<Scalar, MatrixRow<MatrixType> >;
|
||||||
typedef MatrixRow Ref;
|
typedef MatrixRow Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = MatrixType::ColsAtCompileTime,
|
||||||
|
ColsAtCompileTime = 1;
|
||||||
|
|
||||||
MatrixRow(const MatRef& matrix, int row)
|
MatrixRow(const MatRef& matrix, int row)
|
||||||
: m_matrix(matrix), m_row(row)
|
: m_matrix(matrix), m_row(row)
|
||||||
{
|
{
|
||||||
@ -88,6 +91,9 @@ template<typename MatrixType> class MatrixCol
|
|||||||
friend class EigenBase<Scalar, MatrixCol<MatrixType> >;
|
friend class EigenBase<Scalar, MatrixCol<MatrixType> >;
|
||||||
typedef MatrixCol Ref;
|
typedef MatrixCol Ref;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||||
|
ColsAtCompileTime = 1;
|
||||||
|
|
||||||
MatrixCol(const MatRef& matrix, int col)
|
MatrixCol(const MatRef& matrix, int col)
|
||||||
: m_matrix(matrix), m_col(col)
|
: m_matrix(matrix), m_col(col)
|
||||||
{
|
{
|
||||||
|
@ -37,6 +37,9 @@ template<typename MatrixType> class ScalarProduct
|
|||||||
typedef ScalarProduct Ref;
|
typedef ScalarProduct Ref;
|
||||||
friend class EigenBase<typename MatrixType::Scalar, ScalarProduct<MatrixType> >;
|
friend class EigenBase<typename MatrixType::Scalar, ScalarProduct<MatrixType> >;
|
||||||
|
|
||||||
|
static const int RowsAtCompileTime = MatrixType::RowsAtCompileTime,
|
||||||
|
ColsAtCompileTime = MatrixType::ColsAtCompileTime;
|
||||||
|
|
||||||
ScalarProduct(const MatRef& matrix, Scalar scalar)
|
ScalarProduct(const MatRef& matrix, Scalar scalar)
|
||||||
: m_matrix(matrix), m_scalar(scalar) {}
|
: m_matrix(matrix), m_scalar(scalar) {}
|
||||||
|
|
||||||
|
@ -38,7 +38,7 @@ template<typename MatrixType> void matrixManip(const MatrixType& m)
|
|||||||
a.row(i) = b.row(i);
|
a.row(i) = b.row(i);
|
||||||
a.row(i) += b.row(i);
|
a.row(i) += b.row(i);
|
||||||
a.minor(i, j) = b.block(1, rows-1, 1, cols-1);
|
a.minor(i, j) = b.block(1, rows-1, 1, cols-1);
|
||||||
//a.alias().minor(i, j) -= a.block(1, rows-1, 1, cols-1);
|
a.minor(i, j) -= eval(a.block(1, rows-1, 1, cols-1));
|
||||||
}
|
}
|
||||||
|
|
||||||
void EigenTest::testMatrixManip()
|
void EigenTest::testMatrixManip()
|
||||||
|
@ -44,10 +44,9 @@ template<typename MatrixType1,
|
|||||||
a = b;
|
a = b;
|
||||||
a = b + c;
|
a = b + c;
|
||||||
a = s * (b - c);
|
a = s * (b - c);
|
||||||
a.alias() = a + b;
|
a = eval(a + b);
|
||||||
|
|
||||||
a += b;
|
a += b;
|
||||||
a.alias() += b;
|
|
||||||
a -= b + b;
|
a -= b + b;
|
||||||
|
|
||||||
MatrixType1 d(rows1, cols1);
|
MatrixType1 d(rows1, cols1);
|
||||||
|
@ -42,11 +42,11 @@ template<typename VectorType> void vectorOps(const VectorType& v)
|
|||||||
a = b;
|
a = b;
|
||||||
a = b + c;
|
a = b + c;
|
||||||
a = s * (b - c);
|
a = s * (b - c);
|
||||||
a.alias() = a + b;
|
a = eval(s * (b - c));
|
||||||
|
|
||||||
a += b;
|
a += b;
|
||||||
a += b + b;
|
a += b + b;
|
||||||
a.alias() += a + a;
|
a += eval(a + a);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EigenTest::testVectorOps()
|
void EigenTest::testVectorOps()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user