RFFGen
 All Classes Namespaces Files Functions Typedefs Enumerations Groups
transpose.hh
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the C++-library RFFGen. */
4 /* Copyright 2015 Lars Lubkoll */
5 /* */
6 /* RFFGen is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* RFFGen is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU General Public License */
17 /* along with RFFGen. If not, see <http://www.gnu.org/licenses/>. */
18 /* */
19 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
20 
21 #ifndef RFFGEN_LINEAR_ALGEBRA_TRANSPOSE_HH
22 #define RFFGEN_LINEAR_ALGEBRA_TRANSPOSE_HH
23 
24 #include <type_traits>
25 
26 #include "extractRowsAndCols.hh"
27 #include "../Util/zero.hh"
28 #include "../Util/at.hh"
29 #include "../Util/staticChecks.hh"
30 #include "../Util/staticChecks_nRows_nCols.hh"
31 
32 namespace RFFGen
33 {
34  namespace LinearAlgebra
35  {
40  template <class Matrix, class TransposedMatrix = Matrix ,
41  class = std::enable_if_t<std::is_same<Matrix,TransposedMatrix>::value> ,
42  class = std::enable_if_t<Checks::isConstantSizeMatrix<Matrix>()> >
43  TransposedMatrix transpose(Matrix A)
44  {
45  auto a = at(A,0,0);
46  for(int i=0; i<numberOfRows<Matrix>(); ++i)
47  for(int j=i+1; j<numberOfColumns<Matrix>(); ++j)
48  {
49  a = at(A,i,j);
50  at(A,i,j) = at(A,j,i);
51  at(A,j,i) = a;
52  }
53 
54  return A;
55  }
56 
61  template <class TransposedMatrix, class Matrix ,
62  class = std::enable_if_t<!std::is_same<Matrix,TransposedMatrix>::value> ,
63  class = std::enable_if_t<Checks::isConstantSizeMatrix<Matrix>() && Checks::isConstantSizeMatrix<TransposedMatrix>()> >
64  TransposedMatrix transpose(const Matrix& A)
65  {
66  TransposedMatrix B = zero<TransposedMatrix>();
67  for(int i=0; i<numberOfRows<Matrix>(); ++i)
68  for(int j=0; j<numberOfColumns<Matrix>(); ++j)
69  at(B,j,i) = A(i,j);
70  return B;
71  }
72 
73 
78  template <class Matrix ,
79  class = std::enable_if_t<!Checks::isConstantSizeMatrix<Matrix>()> ,
80  class = std::enable_if_t<Checks::isDynamicMatrix<Matrix>()> >
81  Matrix transpose(Matrix A)
82  {
83  assert(rows(A) == cols(A));
84  using Index = decltype(rows(std::declval<Matrix>()));
85  auto a = std::remove_const_t<std::remove_reference_t<decltype(at(A,0,0))> >(0.);
86  for(Index i=0; i<rows(A); ++i)
87  for(Index j=i+1; j<cols(A); ++j)
88  {
89  a = at(A,i,j);
90  at(A,i,j) = at(A,j,i);
91  at(A,j,i) = a;
92  }
93 
94  return A;
95  }
96  }
97 }
98 
99 #endif // RFFGEN_LINEAR_ALGEBRA_TRANSPOSE_HH
Matrix transpose(Matrix A)
Compute transpose of square matrix.
Definition: transpose.hh:81