aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Array/PartialRedux.h
blob: 6b33caaa98d95b61b3377d4296494673c4b37f70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra. Eigen itself is part of the KDE project.
//
// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob@math.jussieu.fr>
//
// Eigen is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// Alternatively, 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 of
// the License, 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 Lesser General Public License or the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License and a copy of the GNU General Public License along with
// Eigen. If not, see <http://www.gnu.org/licenses/>.

#ifndef EIGEN_PARTIAL_REDUX_H
#define EIGEN_PARTIAL_REDUX_H

/** \class PartialRedux
  *
  * \brief Generic expression of a partially reduxed matrix
  *
  * \param Direction indicates the direction of the redux (Vertical or Horizontal)
  * \param BinaryOp type of the binary functor implementing the operator (must be associative)
  * \param MatrixType the type of the matrix we are applying the redux operation
  *
  * This class represents an expression of a partial redux operator of a matrix.
  * It is the return type of MatrixBase::verticalRedux(), MatrixBase::horizontalRedux(),
  * and most of the time this is the only way it is used.
  *
  * \sa class CwiseBinaryOp
  */
template<int Direction, typename BinaryOp, typename MatrixType>
struct ei_traits<PartialRedux<Direction, BinaryOp, MatrixType> >
{
  typedef typename ei_result_of<
                     BinaryOp(typename MatrixType::Scalar)
                   >::type Scalar;
  typedef typename ei_nested<MatrixType>::type MatrixTypeNested;
  typedef typename ei_unref<MatrixTypeNested>::type _MatrixTypeNested;
  enum {
    RowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::RowsAtCompileTime,
    ColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::ColsAtCompileTime,
    MaxRowsAtCompileTime = Direction==Vertical   ? 1 : MatrixType::MaxRowsAtCompileTime,
    MaxColsAtCompileTime = Direction==Horizontal ? 1 : MatrixType::MaxColsAtCompileTime,
    Flags = ((int(RowsAtCompileTime) == Dynamic || int(ColsAtCompileTime) == Dynamic)
          ? (unsigned int)_MatrixTypeNested::Flags
          : (unsigned int)_MatrixTypeNested::Flags & ~LargeBit) & HereditaryBits,
    TraversalSize = Direction==Vertical ? RowsAtCompileTime : ColsAtCompileTime,
    CoeffReadCost = TraversalSize * _MatrixTypeNested::CoeffReadCost
                  + (TraversalSize - 1) * ei_functor_traits<BinaryOp>::Cost
  };
};

template<int Direction, typename BinaryOp, typename MatrixType>
class PartialRedux : ei_no_assignment_operator,
  public MatrixBase<PartialRedux<Direction, BinaryOp, MatrixType> >
{
  public:

    EIGEN_GENERIC_PUBLIC_INTERFACE(PartialRedux)
    typedef typename ei_traits<PartialRedux>::MatrixTypeNested MatrixTypeNested;
    typedef typename ei_traits<PartialRedux>::_MatrixTypeNested _MatrixTypeNested;

    PartialRedux(const MatrixType& mat, const BinaryOp& func = BinaryOp())
      : m_matrix(mat), m_functor(func) {}

  private:

    int _rows() const { return (Direction==Vertical   ? 1 : m_matrix.rows()); }
    int _cols() const { return (Direction==Horizontal ? 1 : m_matrix.cols()); }

    const Scalar _coeff(int i, int j) const
    {
      if (Direction==Vertical)
        return m_matrix.col(j).redux(m_functor);
      else
        return m_matrix.row(i).redux(m_functor);
    }

  protected:
    const MatrixTypeNested m_matrix;
    const BinaryOp m_functor;
};

/** \returns a row vector expression of *this vertically reduxed by \a func
  *
  * The template parameter \a BinaryOp is the type of the functor
  * of the custom redux operator. Note that func must be an associative operator.
  *
  * \sa class PartialRedux, MatrixBase::horizontalRedux()
  */
template<typename Derived>
template<typename BinaryOp>
const PartialRedux<Vertical, BinaryOp, Derived>
MatrixBase<Derived>::verticalRedux(const BinaryOp& func) const
{
  return PartialRedux<Vertical, BinaryOp, Derived>(derived(), func);
}

/** \returns a row vector expression of *this horizontally reduxed by \a func
  *
  * The template parameter \a BinaryOp is the type of the functor
  * of the custom redux operator. Note that func must be an associative operator.
  *
  * \sa class PartialRedux, MatrixBase::verticalRedux()
  */
template<typename Derived>
template<typename BinaryOp>
const PartialRedux<Horizontal, BinaryOp, Derived>
MatrixBase<Derived>::horizontalRedux(const BinaryOp& func) const
{
  return PartialRedux<Horizontal, BinaryOp, Derived>(derived(), func);
}

#endif // EIGEN_PARTIAL_REDUX_H