|
ʻOhana
Population structure, admixture history, and selection using learning methods.
|
7 #ifndef JADE_MATRIX_HPP__
8 #define JADE_MATRIX_HPP__
10 #include "jade.blas.hpp"
11 #include "jade.error.hpp"
12 #include "jade.lapack.hpp"
19 template <
typename TValue>
33 typedef std::initializer_list<
34 std::initializer_list<value_type>
56 assert((cx == 0 && cy == 0) || (cx != 0 && cy != 0));
65 const char *
const path)
76 const std::string & path)
100 , _cx (_cy == 0 ? 0 : values.begin()->size())
103 assert(std::all_of(values.begin(), values.end(), [
this](
104 const std::initializer_list<value_type> & row_values) ->
bool {
105 return row_values.size() == _cx; }));
107 _m.reserve(_cy * _cx);
108 for (
const auto & row : values)
109 for (
const auto n : row)
116 template <
typename TPredicate>
118 const TPredicate predicate)
121 return std::all_of(_m.begin(), _m.end(), predicate);
127 template <
typename TPredicate>
129 const TPredicate predicate)
132 return std::any_of(_m.begin(), _m.end(), predicate);
147 while (dst_ptr != dst_end)
149 *dst_ptr = std::min(std::max(min, *dst_ptr), max);
168 while (dst_ptr != dst_end)
170 *dst_ptr = std::min(std::max(min, *dst_ptr), max);
187 const auto dst_end = dst_ptr +
get_width();
189 while (dst_ptr != dst_end)
191 *dst_ptr = std::min(std::max(min, *dst_ptr), max);
204 while (src_ptr != src_end)
205 if (std::isinf(*src_ptr++))
219 while (src_ptr != src_end)
220 if (std::isnan(*src_ptr++))
255 while (src_ptr != src_end)
257 *dst_ptr++ = *src_ptr;
285 while (dst_ptr != dst_end)
287 const auto dst_anchor = dst_ptr;
288 const auto src_anchor = src_ptr;
294 while (src_ptr != src_end)
296 *dst_ptr++ = *src_ptr;
303 dst_ptr = dst_anchor + n + 1;
304 src_ptr = src_anchor + n + 1;
335 const auto src_end = src_ptr +
get_width();
337 while (src_ptr != src_end)
338 *dst_ptr++ = *src_ptr++;
364 while (src_ptr != src_end)
366 const auto dst_anchor = dst_ptr;
367 const auto src_anchor = src_ptr;
373 while (dst_ptr != dst_end)
375 *dst_ptr = *src_ptr++;
382 dst_ptr = dst_anchor + n + 1;
383 src_ptr = src_anchor + n + 1;
412 std::swap(out._cx, out._cy);
418 for (
size_t i = 1; i < _cy; i++)
419 for (
size_t j = 0; j < i; j++)
420 std::swap(out(i, j), out(j, i));
440 for (
size_t i = 0; i < _cy; i++)
441 for (
size_t j = 0; j < _cx; j++)
442 out(j, i) = src(i, j);
503 std::vector<int> ipiv;
566 return _m.data() + index;
576 return _m.data() + index;
587 return _m.data() +
get_index(row, column);
597 return _m.data() +
get_index(row, column);
617 assert(column < _cx);
618 return row * _cx + column;
638 return *std::max_element(_m.begin(), _m.end());
650 return *std::min_element(_m.begin(), _m.end());
668 const auto p = *std::minmax_element(_m.begin(), _m.end());
705 min = std::min(min, v);
706 max = std::max(max, v);
737 std::ostringstream out;
738 out <<
'[' << _cy <<
'x' << _cx <<
']';
747 return std::accumulate(
751 std::plus<value_type>());
781 const auto index =
get_index(row, column);
815 for (
auto ptr =
get_data(); ptr != end; ptr += _cx + 1)
900 return (_cx == other._cx) && (_cy == other._cy);
911 return (_cy == height) && (_cx == width);
942 while (dst_ptr != dst_end)
964 while (a_ptr != a_end)
966 sum += *a_ptr * *b_ptr++;
983 const auto dst_end = dst_ptr +
get_width();
985 while (dst_ptr != dst_end)
1005 while (a_ptr != a_end)
1006 sum += *a_ptr++ * *b_ptr++;
1060 const std::string & path)
1071 const char *
const path)
1073 assert(path !=
nullptr);
1075 std::ifstream in (path);
1077 throw error() <<
"failed to open matrix '" << path <<
"'";
1083 catch (
const std::exception & e)
1086 <<
"failed to read matrix '"
1087 << path <<
"': " << e.
what();
1100 if (!(in >> cy >> cx))
1102 <<
"failed to parse matrix size";
1104 if ((cx == 0 && cy != 0) || (cx != 0 && cy == 0))
1106 <<
"invalid matrix size ["
1107 << cy <<
"x" << cx <<
"]";
1112 for (
size_t y = 0; y < cy; y++)
1114 for (
size_t x = 0; x < cx; x++)
1119 <<
"failed to parse matrix value at cell ["
1120 << y+1 <<
"," << x+1 <<
"]";
1136 const size_t height,
1139 assert((height == 0 && width == 0)
1140 || (height != 0 && width != 0));
1142 if (height == _cy && width == _cx)
1147 _m.resize(width * height);
1157 static const auto precision =
1158 1 + std::numeric_limits<value_type>::digits10;
1160 out << std::scientific << std::setprecision(precision);
1170 assert(index < _m.size());
1179 const size_t column,
1182 const auto index =
get_index(row, column);
1201 std::ostringstream out;
1212 std::swap(_cx, other._cx);
1213 std::swap(_cy, other._cy);
1231 const std::string & path)
1234 write(path.c_str());
1243 const char *
const path)
1246 assert(path !=
nullptr);
1248 std::ofstream out (path);
1251 throw error() <<
"failed to create matrix '" << path <<
"'";
1265 out << _cy <<
' ' << _cx <<
'\n';
1270 for (
size_t y = 0; y < _cy; y++)
1274 for (
size_t x = 1; x < _cx; x++)
1275 out <<
'\t' << *ptr++;
1310 const auto index =
get_index(rhs_1, rhs_2);
1321 const auto index =
get_index(rhs_1, rhs_2);
1340 while (dst_ptr != dst_end)
1341 *dst_ptr++ += *src_ptr++;
1357 while (dst_ptr != dst_end)
1378 while (dst_ptr != dst_end)
1379 *dst_ptr++ -= *src_ptr++;
1395 while (dst_ptr != dst_end)
1411 gemm(*
this, rhs, dst);
1428 while (dst_ptr != dst_end)
1446 while (dst_ptr != dst_end)
1522 gemm(*
this, rhs, dst);
1549 typedef std::vector<value_type> vector_type;
1562 template <
typename TValue>
1575 template <
typename TValue>
1576 std::ostream & operator << (
1589 template <
typename TValue>
1590 std::istream & operator >> (
1598 #endif // JADE_MATRIX_HPP__
basic_matrix operator/(const value_type rhs) const
Divides a scalar.
void read(const char *const path)
Reads the matrix values from the specified file.
bool invert()
Computes and stores the inverse of this matrix using the Cholesky square root method....
const value_type * get_data() const
bool get_min_max_column(const size_t column, value_type &min, value_type &max) const
Returns the minimum and maximum elements in a column. If the matrix is empty, this method returns fal...
size_t get_length() const
bool all_of(const TPredicate predicate) const
void set_value(const size_t row, const size_t column, const value_type &value)
Sets a single value of the matrix.
value_type get_value(const size_t row, const size_t column) const
void create_transpose(basic_matrix &out) const
Stores the transpose of the matrix.
bool gesv()
Computes the solution to the system of linear equations with a square coefficient matrix A and multip...
size_t get_index(const size_t row, const size_t column) const
void copy_column(const size_t column, basic_matrix &out) const
Copies a column into the specified vector.
void resize(const size_t height, const size_t width)
Resizes the matrix to the specified dimensions. The values of the matrix are not reset.
void write(std::ostream &out) const
Writes this matrix to the specified output stream.
static int potrf(layout_type layout, char uplo, int n, value_type *a, int lda)
Computes the Cholesky factorization of a symmetric (Hermitian) positive-definite matrix.
void transpose()
Transposes this matrix.
const value_type & operator()(const size_t rhs_1, const size_t rhs_2) const
void clamp(const value_type min, const value_type max)
Clamps all values in the matrix to the specified range.
virtual const char * what() const
static void set_high_precision(std::ostream &out)
Sets the specified stream to scientific notation with a high precision.
void swap(basic_matrix &other)
Swaps this matrix and another matrix.
basic_matrix(const initializer_list_type &values)
Initializes a new instance of the class based on the specified values.
value_type multiply_row(const size_t row, const basic_matrix &vector) const
value_type multiply_column(const size_t column, const basic_matrix &vector)
const value_type * get_data(const size_t row, const size_t column) const
void write(const std::string &path) const
Writes this matrix to the specified file.
basic_matrix & operator-=(const basic_matrix &rhs)
Subtracts the specified matrix from this instance.
std::string get_size_str() const
bool is_size(const size_t height, const size_t width) const
bool contains_nan() const
TValue value_type
The value type.
void read(std::istream &in)
Reads the matrix values from the specified stream.
bool contains_inf() const
value_type get_sum() const
void multiply_column(const size_t column, const value_type value)
Multiplies a column by a specified value.
void read(const std::string &path)
Reads the matrix values from the specified file.
bool is_size(const basic_matrix &other) const
const value_type & operator[](const size_t rhs) const
A template for a class providing access to BLAS.
basic_blas< value_type > blas_type
The BLAS type.
bool potrf_lower()
Forms the Cholesky factorization of a symmetric positive-definite matrix: [A = L * L^T] where L is th...
value_type * get_data(const size_t row, const size_t column)
static int potri(layout_type layout, char uplo, int n, value_type *a, int lda)
Computes the inverse of a symmetric (Hermitian) positive-definite matrix using the Cholesky factoriza...
basic_lapack< value_type > lapack_type
The LAPACK type.
value_type get_value(const size_t index) const
basic_matrix operator*(const basic_matrix &rhs) const
Multiplies a matrix.
basic_matrix(const std::string &path)
Initializes a new instance of the class based on values from the specified file.
void write(const char *const path) const
Writes this matrix to the specified file.
basic_matrix(const size_t cy, const size_t cx)
Initializes a new instance of the class based on the specified width and height.
basic_matrix()
Initializes a new instance of the class with no size.
size_t get_height() const
static int gesv(layout_type layout, int n, int nrhs, value_type *a, int lda, int *ipiv, value_type *b, int ldb)
Computes the solution to the system of linear equations with a square coefficient matrix A and multip...
basic_matrix(std::istream &in)
Initializes a new instance of the class based on values from the specified input stream.
bool get_min_max(value_type &min, value_type &max) const
Returns the minimum and maximum elements in the matrix. If the matrix is empty, this method returns f...
bool invert(value_type &log_det)
Computes and stores the inverse of this matrix using the Cholesky square root method....
basic_matrix & operator+=(const basic_matrix &rhs)
Adds the specified matrix to this instance.
value_type get_max_value() const
Returns the maximum element value. This method should not be called if the matrix is empty.
void clamp_column(const size_t column, const value_type min, const value_type max)
Clamps all values in a column to the specified range.
void set_value(const size_t index, const value_type &value)
Sets a single value of the vector.
bool is_length(const size_t length) const
void copy_upper_to_lower()
Copies the elements in the upper triangle of the square matrix to the corresponding lower triangle el...
value_type * get_data(const size_t index)
void multiply_row(const size_t row, const value_type value)
Multiplies a row by a specified value.
value_type get_column_sum(const size_t column) const
std::initializer_list< std::initializer_list< value_type > > initializer_list_type
The initializer list type.
bool is_length(const basic_matrix &other) const
basic_matrix & operator/=(const value_type rhs)
Divides this matrix by the specified scalar and assigns the quotient to this instance.
bool is_row_vector() const
basic_matrix operator-() const
basic_matrix & operator*=(const basic_matrix &rhs)
Multiplies this matrix and the specified matrix and assigns the product to this instance.
basic_matrix(const char *const path)
Initializes a new instance of the class based on values from the specified file.
value_type get_row_sum(const size_t row) const
bool is_column_vector() const
void copy_lower_to_upper()
Copies the elements in the lower triangle of the square matrix to the corresponding upper triangle el...
A template for a class representing an exception thrown from this namespace.
void set_values(const value_type value)
Sets all values of the matrix to the specified value.
static void gemm(const layout_type Order, const transpose_type TransA, const transpose_type TransB, const int M, const int N, const int K, const value_type alpha, const value_type *A, const int lda, const value_type *B, const int ldb, const value_type beta, value_type *C, const int ldc)
Computes a matrix-matrix product with general matrices.
bool any_of(const TPredicate predicate) const
basic_matrix operator+(const basic_matrix &rhs) const
Adds a matrix.
basic_matrix copy_column(const size_t column) const
const value_type * get_data(const size_t index) const
value_type get_min_value() const
Returns the minimum element value. This method should not be called if the matrix is empty.
A template class for a matrix.
basic_matrix copy_row(const size_t row) const
bool potri_lower()
Computes the inverse inv(A) of a symmetric positive definite matrix A. Before calling this routine,...
A template for a class providing access to LAPACK.
void clamp_row(const size_t row, const value_type min, const value_type max)
Clamps all values in a row to the specified range.
static void gemm(const basic_matrix &lhs, const basic_matrix &rhs, basic_matrix &dst, const value_type alpha=value_type(1), const value_type beta=value_type(0))
Multiplies a left matrix by a right matrix and stores the result into a destination matrix....
basic_matrix create_transpose() const
@ row_major
A row-major order.
void copy_row(const size_t row, basic_matrix &out) const
Copies a row into the specified matrix.