📄 utils.h
字号:
//// Copyright 1997, 1998, 1999 University of Notre Dame.// Authors: Andrew Lumsdaine, Jeremy G. Siek, Lie-Quan Lee//// This file is part of the Matrix Template Library//// You should have received a copy of the License Agreement for the// Matrix Template Library along with the software; see the// file LICENSE. If not, contact Office of Research, University of Notre// Dame, Notre Dame, IN 46556.//// Permission to modify the code and to distribute modified code is// granted, provided the text of this NOTICE is retained, a notice that// the code was modified is included with the above COPYRIGHT NOTICE and// with the COPYRIGHT NOTICE in the LICENSE file, and that the LICENSE// file is distributed with the modified code.//// LICENSOR MAKES NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.// By way of example, but not limitation, Licensor MAKES NO// REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY// PARTICULAR PURPOSE OR THAT THE USE OF THE LICENSED SOFTWARE COMPONENTS// OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, TRADEMARKS// OR OTHER RIGHTS.//#ifndef MTL_UTILS_H#define MTL_UTILS_H#include "mtl/mtl_config.h"#include "mtl/not_at.h"#include "mtl/entry.h"#include "mtl/matrix_traits.h"#include "mtl/dimension.h"#include <utility>#include "mtl/mtl_complex.h"#include <algorithm>#include <iostream>namespace mtl {using std::complex;/* Utility Functions */template <class Vector>inline voidprint_partitioned_vector(Vector x){ for (typename Vector::iterator i = x.begin(); i != x.end(); ++i) { mtl::print_vector(*i); }}template <class Matrix>inline voidprint_partitioned_matrix(const Matrix& A){ std::cout << "Top mat: " << A.nrows() << "x" << A.ncols() << std::endl; typedef typename mtl::matrix_traits<Matrix>::size_type Int; Int i,j; for (i=0; i < A.nrows(); ++i) { for (j=0; j < A.ncols(); ++j) mtl::print_all_matrix( A(i,j) ); std::cout << std::endl; }}template <class Matrix>inline voidprint_partitioned_by_row(const Matrix& A){ std::cout << "Top mat: " << A.nrows() << "x" << A.ncols() << std::endl; typename Matrix::const_iterator A_kk; typename Matrix::Row::const_iterator A_i, A_iend; A_kk = A.begin(); while (not_at(A_kk, A.end())) { A_i = (*A_kk).begin(); A_iend = (*A_kk).end(); while (not_at(A_i, A_iend)) { mtl::print_all_matrix( *A_i ); ++A_i; } ++A_kk; } }template <class Matrix>inline voidprint_partitioned_by_column(const Matrix& A){ std::cout << "Top mat: " << A.nrows() << "x" << A.ncols() << std::endl; typename Matrix::const_iterator A_kk; typename Matrix::Column::const_iterator A_i, A_iend; A_kk = A.begin(); while (not_at(A_kk, A.end())) { A_i = (*A_kk).begin(); A_iend = (*A_kk).end(); while (not_at(A_i, A_iend)) { mtl::print_all_matrix( *A_i ); ++A_i; } ++A_kk; } }//: utility function for banded// should use this in a couple more places//!noindex:template <class size_type>dimension<size_type>calc_start_fini(int i, int minor, dimension<size_type> bandwidth){ int start = MTL_MAX(i - int(bandwidth.first()), 0); int fini = MTL_MIN(i + int(bandwidth.second()) + 1, minor); if (start > fini) start = fini; return dimension<size_type>(start,fini);}template <class Iterator>inline voidprint_vector(Iterator y, Iterator y_end){ std::cout << "["; while (not_at(y, y_end)) { cout << *y << ","; ++y; } std::cout << "]" << std::endl;}///template <class Vector>inline voidprint_vector(Vector x){ typename Vector::iterator i = x.begin(); std::cout << "["; while (not_at(i, x.end())) { std::cout << *i << ","; ++i; } std::cout << "]" << std::endl;}template <class Vector>inline voidprint_vector_index(Vector x){ typename Vector::iterator i = x.begin(); std::cout << "["; while (not_at(i, x.end())) { std::cout << i.index() << ","; ++i; } std::cout << "]" << std::endl;}///template <class Matrix>inline voidprint_coord(const Matrix& A){ typename Matrix::const_iterator A_kk; typename Matrix::OneD::const_iterator A_i; A_kk = A.begin(); while (not_at(A_kk, A.end())) { A_i = (*A_kk).begin(); while (not_at(A_i, (*A_kk).end())) { std::cout << "(" << A_kk.row() << "," << A_i.column() << ") = " << *A_i << std::endl; ++A_i; } ++A_kk; } }///template <class Matrix>inline voidprint_all_matrix(const Matrix& A){ typedef typename matrix_traits<Matrix>::size_type Int; Int i,j; std::cout << A.nrows() << "x" << A.ncols() << std::endl; std::cout << "[" << std::endl; for (i=0; i < A.nrows(); ++i) { std::cout << "["; for (j=0; j < A.ncols(); ++j) { std::cout << A(i,j); if (j < A.ncols() - 1) std::cout << ","; } std::cout << "]"; if (i < A.nrows() - 1) std::cout << "," << std::endl; else std::cout << std::endl; } std::cout << "]" << std::endl;}///template <class Matrix>inline voidprint_all_banded(const Matrix& A, int lo, int up){ typedef typename matrix_traits<Matrix>::size_type Int; Int i, j; std::cout << A.nrows() << "x" << A.ncols() << std::endl; std::cout << "[" << std::endl; for (i = 0; i < A.nrows(); ++i) { Int first = MTL_MAX(0, int(i) - lo); Int last = MTL_MIN(int(A.ncols()), int(i) + up + 1); std::cout << "["; for (j = 0; j < A.ncols(); ++j) { if (j < first || j >= last) std::cout << 0; else { std::cout << A(i,j); } if (j < A.ncols() - 1) std::cout << ","; } std::cout << "]"; if (i < A.nrows() - 1) std::cout << "," << std::endl; else std::cout << std::endl; } std::cout << "]" << std::endl;}///template <class Matrix>void print_minor(const Matrix& A){ typename Matrix::const_minor_iterator A_kk; typename Matrix::MinorVector::const_iterator A_i; A_kk = A.begin_minor(); std::cout << "[" << std::endl; while (not_at(A_kk, A.end_minor())) { std::cout << "["; A_i = (*A_kk).begin(); while (not_at(A_i, (*A_kk).end())) { std::cout << *A_i; ++A_i; std::cout << ","; } std::cout << "]"; std::cout << "," << std::endl; ++A_kk; } std::cout << "]" << std::endl;}///template <class Matrix>void print_row(const Matrix& A){ typename Matrix::const_iterator A_kk; typename Matrix::Row::const_iterator A_i, A_iend; A_kk = A.begin(); std::cout << "[" << std::endl; while (not_at(A_kk, A.end())) { std::cout << "["; A_i = (*A_kk).begin(); A_iend = (*A_kk).end(); while (not_at(A_i, A_iend)) { std::cout << *A_i; ++A_i; std::cout << ","; } std::cout << "]"; std::cout << "," << std::endl; ++A_kk; } std::cout << "]" << std::endl;}///template <class Matrix>void print_rev_row(const Matrix& A){ typename Matrix::const_reverse_iterator A_kk; typename Matrix::Row::const_reverse_iterator A_i; typename Matrix::Row::const_reverse_iterator A_iend; A_kk = A.rbegin(); std::cout << "[" << std::endl; while (not_at(A_kk, A.rend())) { std::cout << "["; A_i = (*A_kk).rbegin(); A_iend = (*A_kk).rend(); while (not_at(A_i, A_iend)) { std::cout << *A_i; ++A_i; std::cout << ","; } std::cout << "]"; std::cout << "," << std::endl; ++A_kk; } std::cout << "]" << std::endl;}///template <class Matrix>void print_column(const Matrix& A){ typename Matrix::const_iterator A_kk; typename Matrix::Column::const_iterator A_i; A_kk = A.begin(); std::cout << "[" << std::endl; while (not_at(A_kk, A.end())) { std::cout << "["; A_i = (*A_kk).begin(); while (not_at(A_i, (*A_kk).end())) { std::cout << *A_i; ++A_i; std::cout << ","; } std::cout << "]"; std::cout << "," << std::endl; ++A_kk; } std::cout << "]" << std::endl;}///template <class Matrix>void print_rev_column(const Matrix& A){ typename Matrix::const_reverse_iterator A_kk; typename Matrix::Column::const_reverse_iterator A_i; A_kk = A.rbegin(); std::cout << "[" << std::endl; while (not_at(A_kk, A.rend())) { std::cout << "["; A_i = (*A_kk).rbegin(); while (not_at(A_i, (*A_kk).rend())) { std::cout << *A_i; ++A_i; std::cout << ","; } std::cout << "]"; std::cout << "," << std::endl; ++A_kk; } std::cout << "]" << std::endl;}///template <class Matrix>void print_major(const Matrix& A){ typename Matrix::const_iterator A_kk; typename Matrix::OneD::const_iterator A_i; A_kk = A.begin(); std::cout << "[" << std::endl; while (not_at(A_kk, A.end())) { std::cout << "[";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -