70 lines
916 B
C++
70 lines
916 B
C++
#ifndef _transpose_n_
|
|
#define _transpose_n_
|
|
|
|
|
|
#include "./utils/matrix.h"
|
|
|
|
|
|
namespace numerics{
|
|
|
|
template <typename T>
|
|
void inplace_transpose(utils::Matrix<T>& A){
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
if (rows != cols){
|
|
throw std::runtime_error("inplace_transpose only valid for square matrices");
|
|
}
|
|
|
|
for (uint64_t i = 0; i < rows; ++i){
|
|
for (uint64_t j = i + 1; j < cols; ++j){
|
|
T tmp = A(j,i);
|
|
A(j,i) = A(i,j);
|
|
A(i,j) = tmp;
|
|
//std::swap(A(j,i), A(i,j));
|
|
}
|
|
}
|
|
}
|
|
|
|
template <typename T>
|
|
utils::Matrix<T> transpose(const utils::Matrix<T>& A){
|
|
|
|
const uint64_t rows = A.rows();
|
|
const uint64_t cols = A.cols();
|
|
|
|
utils::Matrix<T> B(cols, rows, T{});
|
|
for (uint64_t i = 0; i < rows; ++i){
|
|
for (uint64_t j = 0; j < cols; ++j){
|
|
B(j,i) = A(i,j);
|
|
}
|
|
}
|
|
return B;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace numerics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _transpose_n_
|