aboutsummaryrefslogtreecommitdiffhomepage
path: root/Eigen/src/Core/InverseProduct.h
blob: 4a3579bbff8d1758d4ef00e8cd0423b3e1b42be3 (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
// 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>
//
// 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_INVERSEPRODUCT_H
#define EIGEN_INVERSEPRODUCT_H

/** \returns the product of the inverse of \c *this with \a other.
  *
  * This function computes the inverse-matrix matrix product inverse(\c*this) * \a other
  * It works as a forward (resp. backward) substitution if \c *this is an upper (resp. lower)
  * triangular matrix.
  *
  * It is required that \c *this be marked as either an upper or a lower triangular matrix, as
  * can be done by marked(), and as is automatically the case with expressions such as those returned
  * by extract().
  * Example: \include MatrixBase_marked.cpp
  * Output: \verbinclude MatrixBase_marked.out
  *
  * \sa marked(), extract()
  */
template<typename Derived>
template<typename OtherDerived>
typename OtherDerived::Eval MatrixBase<Derived>::inverseProduct(const MatrixBase<OtherDerived>& other) const
{
  assert(cols() == other.rows());
  assert(!(Flags & ZeroDiagBit));
  assert(Flags & (UpperTriangularBit|LowerTriangularBit));

  typename OtherDerived::Eval res(other.rows(), other.cols());

  for(int c=0 ; c<other.cols() ; ++c)
  {
    if(Flags & LowerTriangularBit)
    {
      // forward substitution
      if(Flags & UnitDiagBit)
	res.coeffRef(0,c) = other.coeff(0,c);
      else
	res.coeffRef(0,c) = other.coeff(0,c)/coeff(0, 0);
      for(int i=1; i<rows(); ++i)
      {
	Scalar tmp = other.coeff(i,c) - ((this->row(i).start(i)) * res.col(c).start(i)).coeff(0,0);
	if (Flags & UnitDiagBit)
	  res.coeffRef(i,c) = tmp;
	else
	  res.coeffRef(i,c) = tmp/coeff(i,i);
      }
    }
    else
    {
      // backward substitution
      if(Flags & UnitDiagBit)
	res.coeffRef(cols()-1,c) = other.coeff(cols()-1,c);
      else
	res.coeffRef(cols()-1,c) = other.coeff(cols()-1, c)/coeff(rows()-1, cols()-1);
      for(int i=rows()-2 ; i>=0 ; --i)
      {
	Scalar tmp = other.coeff(i,c)
                     - ((this->row(i).end(cols()-i-1)) * res.col(c).end(cols()-i-1)).coeff(0,0);
	if (Flags & UnitDiagBit)
	  res.coeffRef(i,c) = tmp;
	else
	  res.coeffRef(i,c) = tmp/coeff(i,i);
      }
    }
  }
  return res;
}

#endif // EIGEN_INVERSEPRODUCT_H