max@0: // Copyright (C) 2008-2011 NICTA (www.nicta.com.au) max@0: // Copyright (C) 2008-2011 Conrad Sanderson max@0: // max@0: // This file is part of the Armadillo C++ library. max@0: // It is provided without any warranty of fitness max@0: // for any purpose. You can redistribute this file max@0: // and/or modify it under the terms of the GNU max@0: // Lesser General Public License (LGPL) as published max@0: // by the Free Software Foundation, either version 3 max@0: // of the License or (at your option) any later version. max@0: // (see http://www.opensource.org/licenses for more info) max@0: max@0: max@0: //! \addtogroup Mat max@0: //! @{ max@0: max@0: max@0: max@0: //! Dense matrix class max@0: max@0: template max@0: class Mat : public Base< eT, Mat > max@0: { max@0: public: max@0: max@0: typedef eT elem_type; //!< the type of elements stored in the matrix max@0: typedef typename get_pod_type::result pod_type; //!< if eT is non-complex, pod_type is same as eT. otherwise, pod_type is the underlying type used by std::complex max@0: max@0: const uword n_rows; //!< number of rows in the matrix (read-only) max@0: const uword n_cols; //!< number of columns in the matrix (read-only) max@0: const uword n_elem; //!< number of elements in the matrix (read-only) max@0: const uhword vec_state; //!< 0: matrix layout; 1: column vector layout; 2: row vector layout max@0: const uhword mem_state; max@0: max@0: // mem_state = 0: normal matrix that can be resized; max@0: // mem_state = 1: use auxiliary memory until change in the number of elements is requested; max@0: // mem_state = 2: use auxiliary memory and don't allow the number of elements to be changed; max@0: // mem_state = 3: fixed size (e.g. via template based size specification). max@0: max@0: arma_aligned const eT* const mem; //!< pointer to the memory used by the matrix (memory is read-only) max@0: max@0: protected: max@0: arma_aligned eT mem_local[ arma_config::mat_prealloc ]; max@0: max@0: max@0: public: max@0: max@0: inline ~Mat(); max@0: inline Mat(); max@0: max@0: inline Mat(const uword in_rows, const uword in_cols); max@0: max@0: inline Mat(const char* text); max@0: inline const Mat& operator=(const char* text); max@0: max@0: inline Mat(const std::string& text); max@0: inline const Mat& operator=(const std::string& text); max@0: max@0: #if defined(ARMA_USE_CXX11) max@0: inline Mat(const std::initializer_list& list); max@0: inline const Mat& operator=(const std::initializer_list& list); max@0: #endif max@0: max@0: inline Mat( eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const bool copy_aux_mem = true, const bool strict = true); max@0: inline Mat(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols); max@0: max@0: arma_inline const Mat& operator=(const eT val); max@0: arma_inline const Mat& operator+=(const eT val); max@0: arma_inline const Mat& operator-=(const eT val); max@0: arma_inline const Mat& operator*=(const eT val); max@0: arma_inline const Mat& operator/=(const eT val); max@0: max@0: inline Mat(const Mat& m); max@0: inline const Mat& operator=(const Mat& m); max@0: inline const Mat& operator+=(const Mat& m); max@0: inline const Mat& operator-=(const Mat& m); max@0: inline const Mat& operator*=(const Mat& m); max@0: inline const Mat& operator%=(const Mat& m); max@0: inline const Mat& operator/=(const Mat& m); max@0: max@0: template inline Mat(const BaseCube& X); max@0: template inline const Mat& operator=(const BaseCube& X); max@0: template inline const Mat& operator+=(const BaseCube& X); max@0: template inline const Mat& operator-=(const BaseCube& X); max@0: template inline const Mat& operator*=(const BaseCube& X); max@0: template inline const Mat& operator%=(const BaseCube& X); max@0: template inline const Mat& operator/=(const BaseCube& X); max@0: max@0: template max@0: inline explicit Mat(const Base& A, const Base& B); max@0: max@0: inline Mat(const subview& X); max@0: inline const Mat& operator=(const subview& X); max@0: inline const Mat& operator+=(const subview& X); max@0: inline const Mat& operator-=(const subview& X); max@0: inline const Mat& operator*=(const subview& X); max@0: inline const Mat& operator%=(const subview& X); max@0: inline const Mat& operator/=(const subview& X); max@0: max@0: //inline explicit Mat(const subview_cube& X); max@0: inline Mat(const subview_cube& X); max@0: inline const Mat& operator=(const subview_cube& X); max@0: inline const Mat& operator+=(const subview_cube& X); max@0: inline const Mat& operator-=(const subview_cube& X); max@0: inline const Mat& operator*=(const subview_cube& X); max@0: inline const Mat& operator%=(const subview_cube& X); max@0: inline const Mat& operator/=(const subview_cube& X); max@0: max@0: //inline explicit Mat(const diagview& X); max@0: inline Mat(const diagview& X); max@0: inline const Mat& operator=(const diagview& X); max@0: inline const Mat& operator+=(const diagview& X); max@0: inline const Mat& operator-=(const diagview& X); max@0: inline const Mat& operator*=(const diagview& X); max@0: inline const Mat& operator%=(const diagview& X); max@0: inline const Mat& operator/=(const diagview& X); max@0: max@0: template inline Mat(const subview_elem1& X); max@0: template inline const Mat& operator= (const subview_elem1& X); max@0: template inline const Mat& operator+=(const subview_elem1& X); max@0: template inline const Mat& operator-=(const subview_elem1& X); max@0: template inline const Mat& operator*=(const subview_elem1& X); max@0: template inline const Mat& operator%=(const subview_elem1& X); max@0: template inline const Mat& operator/=(const subview_elem1& X); max@0: max@0: max@0: inline mat_injector operator<<(const eT val); max@0: inline mat_injector operator<<(const injector_end_of_row& x); max@0: max@0: max@0: arma_inline subview_row row(const uword row_num); max@0: arma_inline const subview_row row(const uword row_num) const; max@0: max@0: inline subview_row operator()(const uword row_num, const span& col_span); max@0: inline const subview_row operator()(const uword row_num, const span& col_span) const; max@0: max@0: max@0: arma_inline subview_col col(const uword col_num); max@0: arma_inline const subview_col col(const uword col_num) const; max@0: max@0: inline subview_col operator()(const span& row_span, const uword col_num); max@0: inline const subview_col operator()(const span& row_span, const uword col_num) const; max@0: max@0: inline Col unsafe_col(const uword col_num); max@0: inline const Col unsafe_col(const uword col_num) const; max@0: max@0: max@0: arma_inline subview rows(const uword in_row1, const uword in_row2); max@0: arma_inline const subview rows(const uword in_row1, const uword in_row2) const; max@0: max@0: arma_inline subview cols(const uword in_col1, const uword in_col2); max@0: arma_inline const subview cols(const uword in_col1, const uword in_col2) const; max@0: max@0: arma_inline subview submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2); max@0: arma_inline const subview submat(const uword in_row1, const uword in_col1, const uword in_row2, const uword in_col2) const; max@0: max@0: max@0: inline subview submat (const span& row_span, const span& col_span); max@0: inline const subview submat (const span& row_span, const span& col_span) const; max@0: max@0: inline subview operator()(const span& row_span, const span& col_span); max@0: inline const subview operator()(const span& row_span, const span& col_span) const; max@0: max@0: max@0: template arma_inline subview_elem1 elem(const Base& a); max@0: template arma_inline const subview_elem1 elem(const Base& a) const; max@0: max@0: // template arma_inline subview_elem2 submat(const Base& a, const Base& b); max@0: // template arma_inline const subview_elem2 submat(const Base& a, const Base& b) const; max@0: max@0: max@0: arma_inline diagview diag(const sword in_id = 0); max@0: arma_inline const diagview diag(const sword in_id = 0) const; max@0: max@0: max@0: inline void swap_rows(const uword in_row1, const uword in_row2); max@0: inline void swap_cols(const uword in_col1, const uword in_col2); max@0: max@0: inline void shed_row(const uword row_num); max@0: inline void shed_col(const uword col_num); max@0: max@0: inline void shed_rows(const uword in_row1, const uword in_row2); max@0: inline void shed_cols(const uword in_col1, const uword in_col2); max@0: max@0: inline void insert_rows(const uword row_num, const uword N, const bool set_to_zero = true); max@0: inline void insert_cols(const uword col_num, const uword N, const bool set_to_zero = true); max@0: max@0: template inline void insert_rows(const uword row_num, const Base& X); max@0: template inline void insert_cols(const uword col_num, const Base& X); max@0: max@0: max@0: template inline Mat(const Gen& X); max@0: template inline const Mat& operator=(const Gen& X); max@0: template inline const Mat& operator+=(const Gen& X); max@0: template inline const Mat& operator-=(const Gen& X); max@0: template inline const Mat& operator*=(const Gen& X); max@0: template inline const Mat& operator%=(const Gen& X); max@0: template inline const Mat& operator/=(const Gen& X); max@0: max@0: template inline Mat(const Op& X); max@0: template inline const Mat& operator=(const Op& X); max@0: template inline const Mat& operator+=(const Op& X); max@0: template inline const Mat& operator-=(const Op& X); max@0: template inline const Mat& operator*=(const Op& X); max@0: template inline const Mat& operator%=(const Op& X); max@0: template inline const Mat& operator/=(const Op& X); max@0: max@0: template inline Mat(const eOp& X); max@0: template inline const Mat& operator=(const eOp& X); max@0: template inline const Mat& operator+=(const eOp& X); max@0: template inline const Mat& operator-=(const eOp& X); max@0: template inline const Mat& operator*=(const eOp& X); max@0: template inline const Mat& operator%=(const eOp& X); max@0: template inline const Mat& operator/=(const eOp& X); max@0: max@0: template inline Mat(const mtOp& X); max@0: template inline const Mat& operator=(const mtOp& X); max@0: template inline const Mat& operator+=(const mtOp& X); max@0: template inline const Mat& operator-=(const mtOp& X); max@0: template inline const Mat& operator*=(const mtOp& X); max@0: template inline const Mat& operator%=(const mtOp& X); max@0: template inline const Mat& operator/=(const mtOp& X); max@0: max@0: template inline Mat(const Glue& X); max@0: template inline const Mat& operator=(const Glue& X); max@0: template inline const Mat& operator+=(const Glue& X); max@0: template inline const Mat& operator-=(const Glue& X); max@0: template inline const Mat& operator*=(const Glue& X); max@0: template inline const Mat& operator%=(const Glue& X); max@0: template inline const Mat& operator/=(const Glue& X); max@0: max@0: template inline const Mat& operator+=(const Glue& X); max@0: template inline const Mat& operator-=(const Glue& X); max@0: max@0: template inline Mat(const eGlue& X); max@0: template inline const Mat& operator=(const eGlue& X); max@0: template inline const Mat& operator+=(const eGlue& X); max@0: template inline const Mat& operator-=(const eGlue& X); max@0: template inline const Mat& operator*=(const eGlue& X); max@0: template inline const Mat& operator%=(const eGlue& X); max@0: template inline const Mat& operator/=(const eGlue& X); max@0: max@0: template inline Mat(const mtGlue& X); max@0: template inline const Mat& operator=(const mtGlue& X); max@0: template inline const Mat& operator+=(const mtGlue& X); max@0: template inline const Mat& operator-=(const mtGlue& X); max@0: template inline const Mat& operator*=(const mtGlue& X); max@0: template inline const Mat& operator%=(const mtGlue& X); max@0: template inline const Mat& operator/=(const mtGlue& X); max@0: max@0: max@0: arma_inline arma_warn_unused eT& operator[] (const uword i); max@0: arma_inline arma_warn_unused eT operator[] (const uword i) const; max@0: arma_inline arma_warn_unused eT& at (const uword i); max@0: arma_inline arma_warn_unused eT at (const uword i) const; max@0: arma_inline arma_warn_unused eT& operator() (const uword i); max@0: arma_inline arma_warn_unused eT operator() (const uword i) const; max@0: max@0: arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col); max@0: arma_inline arma_warn_unused eT at (const uword in_row, const uword in_col) const; max@0: arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col); max@0: arma_inline arma_warn_unused eT operator() (const uword in_row, const uword in_col) const; max@0: max@0: arma_inline const Mat& operator++(); max@0: arma_inline void operator++(int); max@0: max@0: arma_inline const Mat& operator--(); max@0: arma_inline void operator--(int); max@0: max@0: arma_inline arma_warn_unused bool is_empty() const; max@0: arma_inline arma_warn_unused bool is_vec() const; max@0: arma_inline arma_warn_unused bool is_rowvec() const; max@0: arma_inline arma_warn_unused bool is_colvec() const; max@0: arma_inline arma_warn_unused bool is_square() const; max@0: inline arma_warn_unused bool is_finite() const; max@0: max@0: arma_inline arma_warn_unused bool in_range(const uword i) const; max@0: arma_inline arma_warn_unused bool in_range(const span& x) const; max@0: max@0: arma_inline arma_warn_unused bool in_range(const uword in_row, const uword in_col) const; max@0: arma_inline arma_warn_unused bool in_range(const span& row_span, const uword in_col) const; max@0: arma_inline arma_warn_unused bool in_range(const uword in_row, const span& col_span) const; max@0: arma_inline arma_warn_unused bool in_range(const span& row_span, const span& col_span) const; max@0: max@0: arma_inline arma_warn_unused eT* colptr(const uword in_col); max@0: arma_inline arma_warn_unused const eT* colptr(const uword in_col) const; max@0: max@0: arma_inline arma_warn_unused eT* memptr(); max@0: arma_inline arma_warn_unused const eT* memptr() const; max@0: max@0: max@0: inline void impl_print(const std::string& extra_text) const; max@0: inline void impl_print(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: inline void impl_print_trans(const std::string& extra_text) const; max@0: inline void impl_print_trans(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: inline void impl_raw_print(const std::string& extra_text) const; max@0: inline void impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: inline void impl_raw_print_trans(const std::string& extra_text) const; max@0: inline void impl_raw_print_trans(std::ostream& user_stream, const std::string& extra_text) const; max@0: max@0: max@0: template max@0: inline void copy_size(const Mat& m); max@0: max@0: inline void set_size(const uword in_elem); max@0: inline void set_size(const uword in_rows, const uword in_cols); max@0: max@0: inline void resize(const uword in_elem); max@0: inline void resize(const uword in_rows, const uword in_cols); max@0: inline void reshape(const uword in_rows, const uword in_cols, const uword dim = 0); max@0: max@0: max@0: arma_hot inline const Mat& fill(const eT val); max@0: max@0: inline const Mat& zeros(); max@0: inline const Mat& zeros(const uword in_elem); max@0: inline const Mat& zeros(const uword in_rows, const uword in_cols); max@0: max@0: inline const Mat& ones(); max@0: inline const Mat& ones(const uword in_elem); max@0: inline const Mat& ones(const uword in_rows, const uword in_cols); max@0: max@0: inline const Mat& randu(); max@0: inline const Mat& randu(const uword in_elem); max@0: inline const Mat& randu(const uword in_rows, const uword in_cols); max@0: max@0: inline const Mat& randn(); max@0: inline const Mat& randn(const uword in_elem); max@0: inline const Mat& randn(const uword in_rows, const uword in_cols); max@0: max@0: inline const Mat& eye(); max@0: inline const Mat& eye(const uword in_rows, const uword in_cols); max@0: max@0: inline void reset(); max@0: max@0: max@0: template inline void set_real(const Base& X); max@0: template inline void set_imag(const Base& X); max@0: max@0: max@0: inline arma_warn_unused eT min() const; max@0: inline arma_warn_unused eT max() const; max@0: max@0: inline eT min(uword& index_of_min_val) const; max@0: inline eT max(uword& index_of_max_val) const; max@0: max@0: inline eT min(uword& row_of_min_val, uword& col_of_min_val) const; max@0: inline eT max(uword& row_of_max_val, uword& col_of_max_val) const; max@0: max@0: max@0: inline bool save(const std::string name, const file_type type = arma_binary, const bool print_status = true) const; max@0: inline bool save( std::ostream& os, const file_type type = arma_binary, const bool print_status = true) const; max@0: max@0: inline bool load(const std::string name, const file_type type = auto_detect, const bool print_status = true); max@0: inline bool load( std::istream& is, const file_type type = auto_detect, const bool print_status = true); max@0: max@0: inline bool quiet_save(const std::string name, const file_type type = arma_binary) const; max@0: inline bool quiet_save( std::ostream& os, const file_type type = arma_binary) const; max@0: max@0: inline bool quiet_load(const std::string name, const file_type type = auto_detect); max@0: inline bool quiet_load( std::istream& is, const file_type type = auto_detect); max@0: max@0: max@0: // for container-like functionality max@0: max@0: typedef eT value_type; max@0: typedef uword size_type; max@0: max@0: typedef eT* iterator; max@0: typedef const eT* const_iterator; max@0: max@0: typedef eT* col_iterator; max@0: typedef const eT* const_col_iterator; max@0: max@0: class row_iterator max@0: { max@0: public: max@0: max@0: inline row_iterator(Mat& in_M, const uword in_row); max@0: max@0: inline eT& operator* (); max@0: max@0: inline row_iterator& operator++(); max@0: inline void operator++(int); max@0: max@0: inline row_iterator& operator--(); max@0: inline void operator--(int); max@0: max@0: inline bool operator!=(const row_iterator& X) const; max@0: inline bool operator==(const row_iterator& X) const; max@0: max@0: arma_aligned Mat& M; max@0: arma_aligned uword row; max@0: arma_aligned uword col; max@0: }; max@0: max@0: max@0: class const_row_iterator max@0: { max@0: public: max@0: max@0: const_row_iterator(const Mat& in_M, const uword in_row); max@0: const_row_iterator(const row_iterator& X); max@0: max@0: inline eT operator*() const; max@0: max@0: inline const_row_iterator& operator++(); max@0: inline void operator++(int); max@0: max@0: inline const_row_iterator& operator--(); max@0: inline void operator--(int); max@0: max@0: inline bool operator!=(const const_row_iterator& X) const; max@0: inline bool operator==(const const_row_iterator& X) const; max@0: max@0: arma_aligned const Mat& M; max@0: arma_aligned uword row; max@0: arma_aligned uword col; max@0: }; max@0: max@0: inline iterator begin(); max@0: inline const_iterator begin() const; max@0: max@0: inline iterator end(); max@0: inline const_iterator end() const; max@0: max@0: inline col_iterator begin_col(const uword col_num); max@0: inline const_col_iterator begin_col(const uword col_num) const; max@0: max@0: inline col_iterator end_col (const uword col_num); max@0: inline const_col_iterator end_col (const uword col_num) const; max@0: max@0: inline row_iterator begin_row(const uword row_num); max@0: inline const_row_iterator begin_row(const uword row_num) const; max@0: max@0: inline row_iterator end_row (const uword row_num); max@0: inline const_row_iterator end_row (const uword row_num) const; max@0: max@0: inline void clear(); max@0: inline bool empty() const; max@0: inline uword size() const; max@0: max@0: template max@0: class fixed : public Mat max@0: { max@0: private: max@0: max@0: static const uword fixed_n_elem = fixed_n_rows * fixed_n_cols; max@0: static const bool use_extra = (fixed_n_elem > arma_config::mat_prealloc); max@0: max@0: arma_aligned eT mem_local_extra[ (use_extra) ? fixed_n_elem : 1 ]; max@0: max@0: arma_inline void mem_setup(); max@0: max@0: max@0: public: max@0: max@0: static const uword n_rows = fixed_n_rows; max@0: static const uword n_cols = fixed_n_cols; max@0: static const uword n_elem = fixed_n_elem; max@0: max@0: max@0: arma_inline fixed(); max@0: arma_inline fixed(const fixed& X); max@0: max@0: template inline fixed(const Base& A); max@0: template inline fixed(const Base& A, const Base& B); max@0: max@0: inline fixed( eT* aux_mem, const bool copy_aux_mem = true); max@0: inline fixed(const eT* aux_mem); max@0: max@0: inline fixed(const char* text); max@0: inline fixed(const std::string& text); max@0: max@0: // TODO: handling of initializer_list ? max@0: max@0: template inline const Mat& operator=(const Base& A); max@0: max@0: inline const Mat& operator=(const eT val); max@0: inline const Mat& operator=(const char* text); max@0: inline const Mat& operator=(const std::string& text); max@0: max@0: max@0: inline subview_row operator()(const uword row_num, const span& col_span); max@0: inline const subview_row operator()(const uword row_num, const span& col_span) const; max@0: max@0: inline subview_col operator()(const span& row_span, const uword col_num); max@0: inline const subview_col operator()(const span& row_span, const uword col_num) const; max@0: max@0: inline subview operator()(const span& row_span, const span& col_span); max@0: inline const subview operator()(const span& row_span, const span& col_span) const; max@0: max@0: max@0: arma_inline arma_warn_unused eT& operator[] (const uword i); max@0: arma_inline arma_warn_unused eT operator[] (const uword i) const; max@0: arma_inline arma_warn_unused eT& at (const uword i); max@0: arma_inline arma_warn_unused eT at (const uword i) const; max@0: arma_inline arma_warn_unused eT& operator() (const uword i); max@0: arma_inline arma_warn_unused eT operator() (const uword i) const; max@0: max@0: arma_inline arma_warn_unused eT& at (const uword in_row, const uword in_col); max@0: arma_inline arma_warn_unused eT at (const uword in_row, const uword in_col) const; max@0: arma_inline arma_warn_unused eT& operator() (const uword in_row, const uword in_col); max@0: arma_inline arma_warn_unused eT operator() (const uword in_row, const uword in_col) const; max@0: max@0: max@0: arma_hot inline const Mat& fill(const eT val); max@0: arma_hot inline const Mat& zeros(); max@0: arma_hot inline const Mat& ones(); max@0: }; max@0: max@0: max@0: protected: max@0: max@0: inline void init_cold(); max@0: inline void init_warm(uword in_rows, uword in_cols); max@0: max@0: inline void init(const std::string& text); max@0: max@0: #if defined(ARMA_USE_CXX11) max@0: inline void init(const std::initializer_list& list); max@0: #endif max@0: max@0: template max@0: inline void init(const Base& A, const Base& B); max@0: max@0: inline void steal_mem(Mat& X); max@0: max@0: inline Mat(const char junk, const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols); max@0: max@0: friend class Cube; max@0: friend class glue_join; max@0: friend class op_strans; max@0: friend class op_htrans; max@0: friend class op_resize; max@0: max@0: max@0: public: max@0: max@0: #ifdef ARMA_EXTRA_MAT_PROTO max@0: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_MAT_PROTO) max@0: #endif max@0: }; max@0: max@0: max@0: max@0: class Mat_aux max@0: { max@0: public: max@0: max@0: template arma_inline static void prefix_pp(Mat& x); max@0: template arma_inline static void prefix_pp(Mat< std::complex >& x); max@0: max@0: template arma_inline static void postfix_pp(Mat& x); max@0: template arma_inline static void postfix_pp(Mat< std::complex >& x); max@0: max@0: template arma_inline static void prefix_mm(Mat& x); max@0: template arma_inline static void prefix_mm(Mat< std::complex >& x); max@0: max@0: template arma_inline static void postfix_mm(Mat& x); max@0: template arma_inline static void postfix_mm(Mat< std::complex >& x); max@0: max@0: template inline static void set_real(Mat& out, const Base& X); max@0: template inline static void set_real(Mat< std::complex >& out, const Base< T,T1>& X); max@0: max@0: template inline static void set_imag(Mat& out, const Base& X); max@0: template inline static void set_imag(Mat< std::complex >& out, const Base< T,T1>& X); max@0: }; max@0: max@0: max@0: max@0: //! @}