|
A library for working with phylogenetic and population genetic data.
v0.27.0
|
|
Go to the documentation of this file. 1 #ifndef GENESIS_UTILS_CONTAINERS_MATRIX_OPERATORS_H_
2 #define GENESIS_UTILS_CONTAINERS_MATRIX_OPERATORS_H_
125 template <
typename T>
129 for(
size_t r = 0; r < data.
rows(); ++r ) {
130 for(
size_t c = 0; c < data.
cols(); ++c ) {
131 res( c, r ) = data( r, c );
141 template <
typename T>
151 template <
typename T>
160 for(
size_t i = 0; i < data.
rows(); ++i ) {
161 for(
size_t j = i + 1; j < data.
cols(); ++j ) {
162 if( data( i, j ) != data( j, i ) ) {
174 template <
typename T>
177 for (
size_t i = 0; i < matrix.
rows(); ++i) {
178 for (
size_t j = 0; j < matrix.
cols(); ++j) {
180 if (j < matrix.cols() - 1) {
193 std::ostream&
operator<< (std::ostream& os,
const Matrix<signed char>& matrix);
199 std::ostream&
operator<< (std::ostream& os,
const Matrix<unsigned char>& matrix);
205 template <
typename T>
206 void print( std::ostream& out,
Matrix<T> const& matrix,
size_t rows = 10,
size_t cols = 10 )
209 if( rows == 0 || rows >= matrix.
rows() ) {
210 rows = matrix.
rows();
212 if( cols == 0 || cols >= matrix.
cols() ) {
213 cols = matrix.
cols();
217 for (
size_t i = 0; i < rows; ++i) {
218 for (
size_t j = 0; j < cols; ++j) {
220 if (j < matrix.cols() - 1) {
224 if( cols < matrix.
cols() ) {
229 if( rows < matrix.
rows() ) {
242 template <
typename T>
245 std::ostringstream out;
246 print( out, matrix, rows, cols );
257 template <
typename T>
260 if( row_a >= data.
rows() || row_b >= data.
rows() ) {
261 throw std::invalid_argument(
"Invalid row index for swap_rows()." );
265 for(
size_t c = 0; c < data.
cols(); ++c ) {
266 swap( data( row_a, c ), data( row_b, c ) );
273 template <
typename T>
276 if( col_a >= data.
rows() || col_b >= data.
rows() ) {
277 throw std::invalid_argument(
"Invalid column index for swap_cols()." );
281 for(
size_t r = 0; r < data.
rows(); ++r ) {
282 swap( data( r, col_a ), data( r, col_b ) );
289 #endif // include guard
std::ostream & operator<<(std::ostream &os, const Matrix< signed char > &matrix)
Template specialization for signed char, in order to print nicely.
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.
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.
Matrix< T > transpose(Matrix< T > const &data)
Transpose a Matrix.
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 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.
void swap(Optional< T > &x, Optional< T > &y)
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.