|
A library for working with phylogenetic and population genetic data.
v0.32.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_OPERATORS_H_
2 #define GENESIS_UTILS_CONTAINERS_MATRIX_OPERATORS_H_
39 #include <type_traits>
126 template <
typename T>
130 for(
size_t r = 0; r < mat.
rows(); ++r ) {
131 for(
size_t c = 0; c < mat.
cols(); ++c ) {
132 res( c, r ) = mat( r, c );
145 template <
typename T>
152 std::vector<bool> visited( mat.
data().size() );
153 size_t const div = mat.
data().size() - 1;
157 while( ++cycle < mat.
data().size() ) {
158 if( visited[cycle] ){
163 cur = cur == div ? div : (mat.
rows() * cur) % div;
164 std::swap( mat.data_[cur], mat.data_[cycle]);
166 }
while( cur != cycle );
177 template <
typename T>
187 template <
typename T>
196 for(
size_t i = 0; i < data.
rows(); ++i ) {
197 for(
size_t j = i + 1; j < data.
cols(); ++j ) {
198 if( data( i, j ) != data( j, i ) ) {
213 for (
size_t i = 0; i < matrix.
rows(); ++i) {
214 for (
size_t j = 0; j < matrix.
cols(); ++j) {
216 if (j < matrix.cols() - 1) {
229 std::ostream&
operator<< (std::ostream& os,
const Matrix<signed char>& matrix);
235 std::ostream&
operator<< (std::ostream& os,
const Matrix<unsigned char>& matrix);
241 template <
typename T>
242 void print( std::ostream& out,
Matrix<T> const& matrix,
size_t rows = 10,
size_t cols = 10 )
245 if( rows == 0 || rows >= matrix.
rows() ) {
246 rows = matrix.
rows();
248 if( cols == 0 || cols >= matrix.
cols() ) {
249 cols = matrix.
cols();
253 for (
size_t i = 0; i < rows; ++i) {
254 for (
size_t j = 0; j < cols; ++j) {
257 if( std::is_same<T, signed char>::value || std::is_same<T, unsigned char>::value ) {
258 out << static_cast<int>( matrix( i, j ));
262 if (j < matrix.
cols() - 1) {
266 if( cols < matrix.
cols() ) {
271 if( rows < matrix.
rows() ) {
284 template <
typename T>
287 std::ostringstream out;
288 print( out, matrix, rows, cols );
299 template <
typename T>
302 if( row_a >= data.
rows() || row_b >= data.
rows() ) {
303 throw std::invalid_argument(
"Invalid row index for swap_rows()." );
307 for(
size_t c = 0; c < data.
cols(); ++c ) {
308 swap( data( row_a, c ), data( row_b, c ) );
315 template <
typename T>
318 if( col_a >= data.
rows() || col_b >= data.
rows() ) {
319 throw std::invalid_argument(
"Invalid column index for swap_cols()." );
323 for(
size_t r = 0; r < data.
rows(); ++r ) {
324 swap( data( r, col_a ), data( r, col_b ) );
331 #endif // include guard
void swap(Sample &lhs, Sample &rhs)
Provides some valuable algorithms that are not part of the C++ 11 STL.
size_t triangular_index(size_t i, size_t j, size_t n)
Given indices i and j in a quadratic Matrix, find the corresponding linear index.
bool is_symmetric(Matrix< T > const &data)
Return whether a Matrix is symmetric, i.e., whether it is square and m[ i, j ] == m[ j,...
void print(std::ostream &out, Matrix< T > const &matrix, size_t rows=10, size_t cols=10)
Print a Matrix to an out stream. See print( Matrix<T> const&, size_t, size_t ) for details.
void transpose_inplace(Matrix< T > &mat)
Transpose a Matrix inplace, without allocating a new Matrix.
std::ostream & operator<<(std::ostream &os, Color const &color)
Write a textual representation of the Color the a stream, in the format "(r, g, b,...
std::pair< size_t, size_t > triangular_indices(size_t k, size_t n)
Given a linear index in a upper triangular Matrix, find the corresponding Matrix indices.
void swap(Color &lhs, Color &rhs)
bool is_square(Matrix< T > const &data)
Return whether a Matrix is a square matrix, that is, whether its number of rows and number of columns...
container_type const & data() const
Container namespace for all symbols of genesis in order to keep them separate when used as a library.
void swap_rows(Matrix< T > &data, size_t row_a, size_t row_b)
Swap (interchange) two rows of a Matrix, given their indices.
size_t triangular_size(size_t n)
Calculate the number of linear indices needed for a triangular Matrix of size n.
void swap_cols(Matrix< T > &data, size_t col_a, size_t col_b)
Swap (interchange) two columns of a Matrix, given their indices.
Matrix< T > transpose(Matrix< T > const &mat)
Transpose a Matrix.