Chris@49: // Copyright (C) 2008-2013 NICTA (www.nicta.com.au) Chris@49: // Copyright (C) 2008-2013 Conrad Sanderson Chris@49: // Chris@49: // This Source Code Form is subject to the terms of the Mozilla Public Chris@49: // License, v. 2.0. If a copy of the MPL was not distributed with this Chris@49: // file, You can obtain one at http://mozilla.org/MPL/2.0/. Chris@49: Chris@49: Chris@49: //! \addtogroup Cube Chris@49: //! @{ Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: Cube::~Cube() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: delete_mat(); Chris@49: Chris@49: if(mem_state == 0) Chris@49: { Chris@49: if(n_elem > Cube_prealloc::mem_n_elem) Chris@49: { Chris@49: memory::release( access::rw(mem) ); Chris@49: } Chris@49: } Chris@49: Chris@49: if(arma_config::debug == true) Chris@49: { Chris@49: // try to expose buggy user code that accesses deleted objects Chris@49: access::rw(mat_ptrs) = 0; Chris@49: access::rw(mem) = 0; Chris@49: } Chris@49: Chris@49: arma_type_check(( is_supported_elem_type::value == false )); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: Cube::Cube() Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct the cube to have user specified dimensions Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices) Chris@49: : n_rows(in_n_rows) Chris@49: , n_cols(in_n_cols) Chris@49: , n_elem_slice(in_n_rows*in_n_cols) Chris@49: , n_slices(in_n_slices) Chris@49: , n_elem(in_n_rows*in_n_cols*in_n_slices) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: init_cold(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::init_cold() Chris@49: { Chris@49: arma_extra_debug_sigprint( arma_boost::format("n_rows = %d, n_cols = %d, n_slices = %d") % n_rows % n_cols % n_slices ); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: ( Chris@49: ( (n_rows > 0x0FFF) || (n_cols > 0x0FFF) || (n_slices > 0xFF) ) Chris@49: ? ( (float(n_rows) * float(n_cols) * float(n_slices)) > float(ARMA_MAX_UWORD) ) Chris@49: : false Chris@49: ), Chris@49: "Cube::init(): requested size is too large" Chris@49: ); Chris@49: Chris@49: if(n_elem <= Cube_prealloc::mem_n_elem) Chris@49: { Chris@49: access::rw(mem) = mem_local; Chris@49: } Chris@49: else Chris@49: { Chris@49: arma_extra_debug_print("Cube::init(): allocating memory"); Chris@49: Chris@49: access::rw(mem) = memory::acquire(n_elem); Chris@49: Chris@49: arma_check_bad_alloc( (mem == 0), "Cube::init(): out of memory" ); Chris@49: } Chris@49: Chris@49: Chris@49: if(n_elem == 0) Chris@49: { Chris@49: access::rw(n_rows) = 0; Chris@49: access::rw(n_cols) = 0; Chris@49: access::rw(n_elem_slice) = 0; Chris@49: access::rw(n_slices) = 0; Chris@49: } Chris@49: else Chris@49: { Chris@49: create_mat(); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! internal cube construction; if the requested size is small enough, memory from the stack is used. Chris@49: //! otherwise memory is allocated via 'new' Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::init_warm(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint( arma_boost::format("in_n_rows = %d, in_n_cols = %d, in_n_slices = %d") % in_n_rows % in_n_cols % in_n_slices ); Chris@49: Chris@49: if( (n_rows == in_n_rows) && (n_cols == in_n_cols) && (n_slices == in_n_slices) ) Chris@49: { Chris@49: return; Chris@49: } Chris@49: Chris@49: const uword t_mem_state = mem_state; Chris@49: Chris@49: bool err_state = false; Chris@49: char* err_msg = 0; Chris@49: Chris@49: arma_debug_set_error Chris@49: ( Chris@49: err_state, Chris@49: err_msg, Chris@49: (t_mem_state == 3), Chris@49: "Cube::init(): size is fixed and hence cannot be changed" Chris@49: ); Chris@49: Chris@49: arma_debug_set_error Chris@49: ( Chris@49: err_state, Chris@49: err_msg, Chris@49: ( Chris@49: ( (in_n_rows > 0x0FFF) || (in_n_cols > 0x0FFF) || (in_n_slices > 0xFF) ) Chris@49: ? ( (float(in_n_rows) * float(in_n_cols) * float(in_n_slices)) > float(ARMA_MAX_UWORD) ) Chris@49: : false Chris@49: ), Chris@49: "Cube::init(): requested size is too large" Chris@49: ); Chris@49: Chris@49: arma_debug_check(err_state, err_msg); Chris@49: Chris@49: const uword old_n_elem = n_elem; Chris@49: const uword new_n_elem = in_n_rows * in_n_cols * in_n_slices; Chris@49: Chris@49: if(old_n_elem == new_n_elem) Chris@49: { Chris@49: delete_mat(); Chris@49: Chris@49: if(new_n_elem > 0) Chris@49: { Chris@49: access::rw(n_rows) = in_n_rows; Chris@49: access::rw(n_cols) = in_n_cols; Chris@49: access::rw(n_elem_slice) = in_n_rows*in_n_cols; Chris@49: access::rw(n_slices) = in_n_slices; Chris@49: Chris@49: create_mat(); Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: arma_debug_check( (t_mem_state == 2), "Cube::init(): requested size is not compatible with the size of auxiliary memory" ); Chris@49: Chris@49: delete_mat(); Chris@49: Chris@49: if(t_mem_state == 0) Chris@49: { Chris@49: if(n_elem > Cube_prealloc::mem_n_elem ) Chris@49: { Chris@49: arma_extra_debug_print("Cube::init(): freeing memory"); Chris@49: Chris@49: memory::release( access::rw(mem) ); Chris@49: } Chris@49: } Chris@49: Chris@49: access::rw(mem_state) = 0; Chris@49: Chris@49: if(new_n_elem <= Cube_prealloc::mem_n_elem) Chris@49: { Chris@49: access::rw(mem) = mem_local; Chris@49: } Chris@49: else Chris@49: { Chris@49: arma_extra_debug_print("Cube::init(): allocating memory"); Chris@49: Chris@49: access::rw(mem) = memory::acquire(new_n_elem); Chris@49: Chris@49: arma_check_bad_alloc( (mem == 0), "Cube::init(): out of memory" ); Chris@49: } Chris@49: Chris@49: if(new_n_elem > 0) Chris@49: { Chris@49: access::rw(n_rows) = in_n_rows; Chris@49: access::rw(n_cols) = in_n_cols; Chris@49: access::rw(n_elem_slice) = in_n_rows*in_n_cols; Chris@49: access::rw(n_slices) = in_n_slices; Chris@49: access::rw(n_elem) = new_n_elem; Chris@49: Chris@49: create_mat(); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: if(new_n_elem == 0) Chris@49: { Chris@49: access::rw(n_rows) = 0; Chris@49: access::rw(n_cols) = 0; Chris@49: access::rw(n_elem_slice) = 0; Chris@49: access::rw(n_slices) = 0; Chris@49: access::rw(n_elem) = 0; Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! for constructing a complex cube out of two non-complex cubes Chris@49: template Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::init Chris@49: ( Chris@49: const BaseCube::pod_type,T1>& X, Chris@49: const BaseCube::pod_type,T2>& Y Chris@49: ) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename T1::elem_type T; Chris@49: Chris@49: arma_type_check(( is_complex::value == false )); //!< compile-time abort if eT isn't std::complex Chris@49: arma_type_check(( is_complex< T>::value == true )); //!< compile-time abort if T is std::complex Chris@49: Chris@49: arma_type_check(( is_same_type< std::complex, eT >::value == false )); //!< compile-time abort if types are not compatible Chris@49: Chris@49: const ProxyCube PX(X.get_ref()); Chris@49: const ProxyCube PY(Y.get_ref()); Chris@49: Chris@49: arma_debug_assert_same_size(PX, PY, "Cube()"); Chris@49: Chris@49: const uword local_n_rows = PX.get_n_rows(); Chris@49: const uword local_n_cols = PX.get_n_cols(); Chris@49: const uword local_n_slices = PX.get_n_slices(); Chris@49: Chris@49: init_warm(local_n_rows, local_n_cols, local_n_slices); Chris@49: Chris@49: eT* out_mem = (*this).memptr(); Chris@49: Chris@49: const bool prefer_at_accessor = ( ProxyCube::prefer_at_accessor || ProxyCube::prefer_at_accessor ); Chris@49: Chris@49: if(prefer_at_accessor == false) Chris@49: { Chris@49: typedef typename ProxyCube::ea_type ea_type1; Chris@49: typedef typename ProxyCube::ea_type ea_type2; Chris@49: Chris@49: const uword N = n_elem; Chris@49: Chris@49: ea_type1 A = PX.get_ea(); Chris@49: ea_type2 B = PY.get_ea(); Chris@49: Chris@49: for(uword i=0; i(A[i], B[i]); Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: for(uword uslice = 0; uslice < local_n_slices; ++uslice) Chris@49: for(uword ucol = 0; ucol < local_n_cols; ++ucol ) Chris@49: for(uword urow = 0; urow < local_n_rows; ++urow ) Chris@49: { Chris@49: *out_mem = std::complex( PX.at(urow,ucol,uslice), PY.at(urow,ucol,uslice) ); Chris@49: out_mem++; Chris@49: } Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::delete_mat() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: for(uword uslice = 0; uslice < n_slices; ++uslice) Chris@49: { Chris@49: delete access::rw(mat_ptrs[uslice]); Chris@49: } Chris@49: Chris@49: if(mem_state <= 2) Chris@49: { Chris@49: if(n_slices > Cube_prealloc::mat_ptrs_size) Chris@49: { Chris@49: delete [] mat_ptrs; Chris@49: } Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::create_mat() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(mem_state <= 2) Chris@49: { Chris@49: if(n_slices <= Cube_prealloc::mat_ptrs_size) Chris@49: { Chris@49: access::rw(mat_ptrs) = const_cast< const Mat** >(mat_ptrs_local); Chris@49: } Chris@49: else Chris@49: { Chris@49: access::rw(mat_ptrs) = new(std::nothrow) const Mat*[n_slices]; Chris@49: Chris@49: arma_check_bad_alloc( (mat_ptrs == 0), "Cube::create_mat(): out of memory" ); Chris@49: } Chris@49: } Chris@49: Chris@49: for(uword uslice = 0; uslice < n_slices; ++uslice) Chris@49: { Chris@49: mat_ptrs[uslice] = new Mat('j', slice_memptr(uslice), n_rows, n_cols); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! Set the cube to be equal to the specified scalar. Chris@49: //! NOTE: the size of the cube will be 1x1x1 Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator=(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: init_warm(1,1,1); Chris@49: access::rw(mem[0]) = val; Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! In-place addition of a scalar to all elements of the cube Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator+=(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arrayops::inplace_plus( memptr(), val, n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! In-place subtraction of a scalar from all elements of the cube Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator-=(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arrayops::inplace_minus( memptr(), val, n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! In-place multiplication of all elements of the cube with a scalar Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator*=(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arrayops::inplace_mul( memptr(), val, n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! In-place division of all elements of the cube with a scalar Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator/=(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arrayops::inplace_div( memptr(), val, n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a given cube Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const Cube& x) Chris@49: : n_rows(x.n_rows) Chris@49: , n_cols(x.n_cols) Chris@49: , n_elem_slice(x.n_elem_slice) Chris@49: , n_slices(x.n_slices) Chris@49: , n_elem(x.n_elem) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: arma_extra_debug_sigprint(arma_boost::format("this = %x in_cube = %x") % this % &x); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: arrayops::copy( memptr(), x.mem, n_elem ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a given cube Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const Cube& x) Chris@49: { Chris@49: arma_extra_debug_sigprint(arma_boost::format("this = %x in_cube = %x") % this % &x); Chris@49: Chris@49: if(this != &x) Chris@49: { Chris@49: init_warm(x.n_rows, x.n_cols, x.n_slices); Chris@49: Chris@49: arrayops::copy( memptr(), x.mem, n_elem ); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a given auxiliary array of eTs. Chris@49: //! if copy_aux_mem is true, new memory is allocated and the array is copied. Chris@49: //! if copy_aux_mem is false, the auxiliary array is used directly (without allocating memory and copying). Chris@49: //! note that in the latter case Chris@49: //! the default is to copy the array. Chris@49: Chris@49: template Chris@49: inline Chris@49: Cube::Cube(eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices, const bool copy_aux_mem, const bool strict) Chris@49: : n_rows ( aux_n_rows ) Chris@49: , n_cols ( aux_n_cols ) Chris@49: , n_elem_slice( aux_n_rows*aux_n_cols ) Chris@49: , n_slices ( aux_n_slices ) Chris@49: , n_elem ( aux_n_rows*aux_n_cols*aux_n_slices ) Chris@49: , mem_state ( copy_aux_mem ? 0 : (strict ? 2 : 1) ) Chris@49: , mat_ptrs ( 0 ) Chris@49: , mem ( copy_aux_mem ? 0 : aux_mem ) Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: if(copy_aux_mem == true) Chris@49: { Chris@49: init_cold(); Chris@49: Chris@49: arrayops::copy( memptr(), aux_mem, n_elem ); Chris@49: } Chris@49: else Chris@49: { Chris@49: create_mat(); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a given auxiliary read-only array of eTs. Chris@49: //! the array is copied. Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const eT* aux_mem, const uword aux_n_rows, const uword aux_n_cols, const uword aux_n_slices) Chris@49: : n_rows(aux_n_rows) Chris@49: , n_cols(aux_n_cols) Chris@49: , n_elem_slice(aux_n_rows*aux_n_cols) Chris@49: , n_slices(aux_n_slices) Chris@49: , n_elem(aux_n_rows*aux_n_cols*aux_n_slices) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: arrayops::copy( memptr(), aux_mem, n_elem ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube addition Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const Cube& m) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_assert_same_size(*this, m, "addition"); Chris@49: Chris@49: arrayops::inplace_plus( memptr(), m.memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const Cube& m) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_assert_same_size(*this, m, "subtraction"); Chris@49: Chris@49: arrayops::inplace_minus( memptr(), m.memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place element-wise cube multiplication Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const Cube& m) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_assert_same_size(*this, m, "element-wise multiplication"); Chris@49: Chris@49: arrayops::inplace_mul( memptr(), m.memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place element-wise cube division Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const Cube& m) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_assert_same_size(*this, m, "element-wise division"); Chris@49: Chris@49: arrayops::inplace_div( memptr(), m.memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! for constructing a complex cube out of two non-complex cubes Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube Chris@49: ( Chris@49: const BaseCube::pod_type,T1>& A, Chris@49: const BaseCube::pod_type,T2>& B Chris@49: ) Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: init(A,B); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a subview_cube instance (e.g. construct a cube from a delayed subcube operation) Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const subview_cube& X) Chris@49: : n_rows(X.n_rows) Chris@49: , n_cols(X.n_cols) Chris@49: , n_elem_slice(X.n_elem_slice) Chris@49: , n_slices(X.n_slices) Chris@49: , n_elem(X.n_elem) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: subview_cube::extract(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! construct a cube from a subview_cube instance (e.g. construct a cube from a delayed subcube operation) Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const subview_cube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const bool alias = (this == &(X.m)); Chris@49: Chris@49: if(alias == false) Chris@49: { Chris@49: init_warm(X.n_rows, X.n_cols, X.n_slices); Chris@49: Chris@49: subview_cube::extract(*this, X); Chris@49: } Chris@49: else Chris@49: { Chris@49: Cube tmp(X); Chris@49: Chris@49: steal_mem(tmp); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube addition (using a subcube on the right-hand-side) Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const subview_cube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: subview_cube::plus_inplace(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction (using a subcube on the right-hand-side) Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const subview_cube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: subview_cube::minus_inplace(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place element-wise cube mutiplication (using a subcube on the right-hand-side) Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const subview_cube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: subview_cube::schur_inplace(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place element-wise cube division (using a subcube on the right-hand-side) Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const subview_cube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: subview_cube::div_inplace(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! provide the reference to the matrix representing a single slice Chris@49: template Chris@49: arma_inline Chris@49: Mat& Chris@49: Cube::slice(const uword in_slice) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_slice >= n_slices), Chris@49: "Cube::slice(): index out of bounds" Chris@49: ); Chris@49: Chris@49: return const_cast< Mat& >( *(mat_ptrs[in_slice]) ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! provide the reference to the matrix representing a single slice Chris@49: template Chris@49: arma_inline Chris@49: const Mat& Chris@49: Cube::slice(const uword in_slice) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_slice >= n_slices), Chris@49: "Cube::slice(): index out of bounds" Chris@49: ); Chris@49: Chris@49: return *(mat_ptrs[in_slice]); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (subcube comprised of specified slices) Chris@49: template Chris@49: arma_inline Chris@49: subview_cube Chris@49: Cube::slices(const uword in_slice1, const uword in_slice2) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_slice1 > in_slice2) || (in_slice2 >= n_slices), Chris@49: "Cube::slices(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: const uword subcube_n_slices = in_slice2 - in_slice1 + 1; Chris@49: Chris@49: return subview_cube(*this, 0, 0, in_slice1, n_rows, n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (subcube comprised of specified slices) Chris@49: template Chris@49: arma_inline Chris@49: const subview_cube Chris@49: Cube::slices(const uword in_slice1, const uword in_slice2) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_slice1 > in_slice2) || (in_slice2 >= n_slices), Chris@49: "Cube::rows(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: const uword subcube_n_slices = in_slice2 - in_slice1 + 1; Chris@49: Chris@49: return subview_cube(*this, 0, 0, in_slice1, n_rows, n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (generic subcube) Chris@49: template Chris@49: arma_inline Chris@49: subview_cube Chris@49: Cube::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row1 > in_row2) || (in_col1 > in_col2) || (in_slice1 > in_slice2) || Chris@49: (in_row2 >= n_rows) || (in_col2 >= n_cols) || (in_slice2 >= n_slices), Chris@49: "Cube::subcube(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: const uword subcube_n_rows = in_row2 - in_row1 + 1; Chris@49: const uword subcube_n_cols = in_col2 - in_col1 + 1; Chris@49: const uword subcube_n_slices = in_slice2 - in_slice1 + 1; Chris@49: Chris@49: return subview_cube(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (generic subcube) Chris@49: template Chris@49: arma_inline Chris@49: const subview_cube Chris@49: Cube::subcube(const uword in_row1, const uword in_col1, const uword in_slice1, const uword in_row2, const uword in_col2, const uword in_slice2) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row1 > in_row2) || (in_col1 > in_col2) || (in_slice1 > in_slice2) || Chris@49: (in_row2 >= n_rows) || (in_col2 >= n_cols) || (in_slice2 >= n_slices), Chris@49: "Cube::subcube(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: const uword subcube_n_rows = in_row2 - in_row1 + 1; Chris@49: const uword subcube_n_cols = in_col2 - in_col1 + 1; Chris@49: const uword subcube_n_slices = in_slice2 - in_slice1 + 1; Chris@49: Chris@49: return subview_cube(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (generic subcube) Chris@49: template Chris@49: inline Chris@49: subview_cube Chris@49: Cube::subcube(const span& row_span, const span& col_span, const span& slice_span) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const bool row_all = row_span.whole; Chris@49: const bool col_all = col_span.whole; Chris@49: const bool slice_all = slice_span.whole; Chris@49: Chris@49: const uword local_n_rows = n_rows; Chris@49: const uword local_n_cols = n_cols; Chris@49: const uword local_n_slices = n_slices; Chris@49: Chris@49: const uword in_row1 = row_all ? 0 : row_span.a; Chris@49: const uword in_row2 = row_span.b; Chris@49: const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1; Chris@49: Chris@49: const uword in_col1 = col_all ? 0 : col_span.a; Chris@49: const uword in_col2 = col_span.b; Chris@49: const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1; Chris@49: Chris@49: const uword in_slice1 = slice_all ? 0 : slice_span.a; Chris@49: const uword in_slice2 = slice_span.b; Chris@49: const uword subcube_n_slices = slice_all ? local_n_slices : in_slice2 - in_slice1 + 1; Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) ) Chris@49: || Chris@49: ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) ) Chris@49: || Chris@49: ( slice_all ? false : ((in_slice1 > in_slice2) || (in_slice2 >= local_n_slices)) ) Chris@49: , Chris@49: "Cube::subcube(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: return subview_cube(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! creation of subview_cube (generic subcube) Chris@49: template Chris@49: inline Chris@49: const subview_cube Chris@49: Cube::subcube(const span& row_span, const span& col_span, const span& slice_span) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const bool row_all = row_span.whole; Chris@49: const bool col_all = col_span.whole; Chris@49: const bool slice_all = slice_span.whole; Chris@49: Chris@49: const uword local_n_rows = n_rows; Chris@49: const uword local_n_cols = n_cols; Chris@49: const uword local_n_slices = n_slices; Chris@49: Chris@49: const uword in_row1 = row_all ? 0 : row_span.a; Chris@49: const uword in_row2 = row_span.b; Chris@49: const uword subcube_n_rows = row_all ? local_n_rows : in_row2 - in_row1 + 1; Chris@49: Chris@49: const uword in_col1 = col_all ? 0 : col_span.a; Chris@49: const uword in_col2 = col_span.b; Chris@49: const uword subcube_n_cols = col_all ? local_n_cols : in_col2 - in_col1 + 1; Chris@49: Chris@49: const uword in_slice1 = slice_all ? 0 : slice_span.a; Chris@49: const uword in_slice2 = slice_span.b; Chris@49: const uword subcube_n_slices = slice_all ? local_n_slices : in_slice2 - in_slice1 + 1; Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: ( row_all ? false : ((in_row1 > in_row2) || (in_row2 >= local_n_rows)) ) Chris@49: || Chris@49: ( col_all ? false : ((in_col1 > in_col2) || (in_col2 >= local_n_cols)) ) Chris@49: || Chris@49: ( slice_all ? false : ((in_slice1 > in_slice2) || (in_slice2 >= local_n_slices)) ) Chris@49: , Chris@49: "Cube::subcube(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: return subview_cube(*this, in_row1, in_col1, in_slice1, subcube_n_rows, subcube_n_cols, subcube_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: subview_cube Chris@49: Cube::operator()(const span& row_span, const span& col_span, const span& slice_span) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).subcube(row_span, col_span, slice_span); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const subview_cube Chris@49: Cube::operator()(const span& row_span, const span& col_span, const span& slice_span) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).subcube(row_span, col_span, slice_span); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! remove specified slice Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::shed_slice(const uword slice_num) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( slice_num >= n_slices, "Cube::shed_slice(): index out of bounds"); Chris@49: Chris@49: shed_slices(slice_num, slice_num); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! remove specified slices Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::shed_slices(const uword in_slice1, const uword in_slice2) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_slice1 > in_slice2) || (in_slice2 >= n_slices), Chris@49: "Cube::shed_slices(): indices out of bounds or incorrectly used" Chris@49: ); Chris@49: Chris@49: const uword n_keep_front = in_slice1; Chris@49: const uword n_keep_back = n_slices - (in_slice2 + 1); Chris@49: Chris@49: Cube X(n_rows, n_cols, n_keep_front + n_keep_back); Chris@49: Chris@49: if(n_keep_front > 0) Chris@49: { Chris@49: X.slices( 0, (n_keep_front-1) ) = slices( 0, (in_slice1-1) ); Chris@49: } Chris@49: Chris@49: if(n_keep_back > 0) Chris@49: { Chris@49: X.slices( n_keep_front, (n_keep_front+n_keep_back-1) ) = slices( (in_slice2+1), (n_slices-1) ); Chris@49: } Chris@49: Chris@49: steal_mem(X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! insert N slices at the specified slice position, Chris@49: //! optionally setting the elements of the inserted slices to zero Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::insert_slices(const uword slice_num, const uword N, const bool set_to_zero) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const uword t_n_slices = n_slices; Chris@49: Chris@49: const uword A_n_slices = slice_num; Chris@49: const uword B_n_slices = t_n_slices - slice_num; Chris@49: Chris@49: // insertion at slice_num == n_slices is in effect an append operation Chris@49: arma_debug_check( (slice_num > t_n_slices), "Cube::insert_slices(): index out of bounds"); Chris@49: Chris@49: if(N > 0) Chris@49: { Chris@49: Cube out(n_rows, n_cols, t_n_slices + N); Chris@49: Chris@49: if(A_n_slices > 0) Chris@49: { Chris@49: out.slices(0, A_n_slices-1) = slices(0, A_n_slices-1); Chris@49: } Chris@49: Chris@49: if(B_n_slices > 0) Chris@49: { Chris@49: out.slices(slice_num + N, t_n_slices + N - 1) = slices(slice_num, t_n_slices-1); Chris@49: } Chris@49: Chris@49: if(set_to_zero == true) Chris@49: { Chris@49: //out.slices(slice_num, slice_num + N - 1).zeros(); Chris@49: Chris@49: for(uword i=slice_num; i < (slice_num + N); ++i) Chris@49: { Chris@49: out.slice(i).zeros(); Chris@49: } Chris@49: } Chris@49: Chris@49: steal_mem(out); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! insert the given object at the specified slice position; Chris@49: //! the given object must have the same number of rows and columns as the cube Chris@49: template Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::insert_slices(const uword slice_num, const BaseCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const unwrap_cube tmp(X.get_ref()); Chris@49: const Cube& C = tmp.M; Chris@49: Chris@49: const uword N = C.n_slices; Chris@49: Chris@49: const uword t_n_slices = n_slices; Chris@49: Chris@49: const uword A_n_slices = slice_num; Chris@49: const uword B_n_slices = t_n_slices - slice_num; Chris@49: Chris@49: // insertion at slice_num == n_slices is in effect an append operation Chris@49: arma_debug_check( (slice_num > t_n_slices), "Cube::insert_slices(): index out of bounds"); Chris@49: Chris@49: arma_debug_check Chris@49: ( Chris@49: ( (C.n_rows != n_rows) || (C.n_cols != n_cols) ), Chris@49: "Cube::insert_slices(): given object has incompatible dimensions" Chris@49: ); Chris@49: Chris@49: if(N > 0) Chris@49: { Chris@49: Cube out(n_rows, n_cols, t_n_slices + N); Chris@49: Chris@49: if(A_n_slices > 0) Chris@49: { Chris@49: out.slices(0, A_n_slices-1) = slices(0, A_n_slices-1); Chris@49: } Chris@49: Chris@49: if(B_n_slices > 0) Chris@49: { Chris@49: out.slices(slice_num + N, t_n_slices + N - 1) = slices(slice_num, t_n_slices - 1); Chris@49: } Chris@49: Chris@49: out.slices(slice_num, slice_num + N - 1) = C; Chris@49: Chris@49: steal_mem(out); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from OpCube, i.e. run the previously delayed unary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const GenCube& X) Chris@49: : n_rows(X.n_rows) Chris@49: , n_cols(X.n_cols) Chris@49: , n_elem_slice(X.n_rows*X.n_cols) Chris@49: , n_slices(X.n_slices) Chris@49: , n_elem(X.n_rows*X.n_cols*X.n_slices) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: X.apply(*this); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const GenCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: init_warm(X.n_rows, X.n_cols, X.n_slices); Chris@49: Chris@49: X.apply(*this); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const GenCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: X.apply_inplace_plus(*this); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const GenCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: X.apply_inplace_minus(*this); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const GenCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: X.apply_inplace_schur(*this); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const GenCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: X.apply_inplace_div(*this); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from OpCube, i.e. run the previously delayed unary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const OpCube& X) Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: op_type::apply(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from OpCube, i.e. run the previously delayed unary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const OpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: op_type::apply(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube addition, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const OpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator+=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const OpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator-=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise multiplication, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const OpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator%=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise division, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const OpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator/=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from eOpCube, i.e. run the previously delayed unary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const eOpCube& X) Chris@49: : n_rows(X.get_n_rows()) Chris@49: , n_cols(X.get_n_cols()) Chris@49: , n_elem_slice(X.get_n_elem_slice()) Chris@49: , n_slices(X.get_n_slices()) Chris@49: , n_elem(X.get_n_elem()) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: eop_type::apply(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from eOpCube, i.e. run the previously delayed unary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const eOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: const bool bad_alias = ( X.P.has_subview && X.P.is_alias(*this) ); Chris@49: Chris@49: if(bad_alias == false) Chris@49: { Chris@49: init_warm(X.get_n_rows(), X.get_n_cols(), X.get_n_slices()); Chris@49: Chris@49: eop_type::apply(*this, X); Chris@49: } Chris@49: else Chris@49: { Chris@49: Cube tmp(X); Chris@49: Chris@49: steal_mem(tmp); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube addition, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const eOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: eop_type::apply_inplace_plus(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const eOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: eop_type::apply_inplace_minus(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise multiplication, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const eOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: eop_type::apply_inplace_schur(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise division, with the right-hand-side operand having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const eOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: Chris@49: eop_type::apply_inplace_div(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const mtOpCube& X) Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: op_type::apply(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const mtOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: op_type::apply(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const mtOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator+=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const mtOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator-=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const mtOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator%=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const mtOpCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator/=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from Glue, i.e. run the previously delayed binary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const GlueCube& X) Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: this->operator=(X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from Glue, i.e. run the previously delayed binary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const GlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: glue_type::apply(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: //! in-place cube addition, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const GlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator+=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const GlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator-=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise multiplication, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const GlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator%=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise division, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const GlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator/=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from eGlue, i.e. run the previously delayed binary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const eGlueCube& X) Chris@49: : n_rows(X.get_n_rows()) Chris@49: , n_cols(X.get_n_cols()) Chris@49: , n_elem_slice(X.get_n_elem_slice()) Chris@49: , n_slices(X.get_n_slices()) Chris@49: , n_elem(X.get_n_elem()) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: init_cold(); Chris@49: Chris@49: eglue_type::apply(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! create a cube from Glue, i.e. run the previously delayed binary operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const eGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: const bool bad_alias = ( (X.P1.has_subview && X.P1.is_alias(*this)) || (X.P2.has_subview && X.P2.is_alias(*this)) ); Chris@49: Chris@49: if(bad_alias == false) Chris@49: { Chris@49: init_warm(X.get_n_rows(), X.get_n_cols(), X.get_n_slices()); Chris@49: Chris@49: eglue_type::apply(*this, X); Chris@49: } Chris@49: else Chris@49: { Chris@49: Cube tmp(X); Chris@49: Chris@49: steal_mem(tmp); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube addition, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const eGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: eglue_type::apply_inplace_plus(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube subtraction, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const eGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: eglue_type::apply_inplace_minus(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise multiplication, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const eGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: eglue_type::apply_inplace_schur(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! in-place cube element-wise division, with the right-hand-side operands having delayed operations Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const eGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_type_check(( is_same_type< eT, typename T1::elem_type >::value == false )); Chris@49: arma_type_check(( is_same_type< eT, typename T2::elem_type >::value == false )); Chris@49: Chris@49: eglue_type::apply_inplace_div(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: Cube::Cube(const mtGlueCube& X) Chris@49: : n_rows(0) Chris@49: , n_cols(0) Chris@49: , n_elem_slice(0) Chris@49: , n_slices(0) Chris@49: , n_elem(0) Chris@49: , mem_state(0) Chris@49: , mat_ptrs() Chris@49: , mem() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: glue_type::apply(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator=(const mtGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: glue_type::apply(*this, X); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator+=(const mtGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator+=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator-=(const mtGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator-=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator%=(const mtGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator%=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! EXPERIMENTAL Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::operator/=(const mtGlueCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const Cube m(X); Chris@49: Chris@49: return (*this).operator/=(m); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); no bounds check; assumes memory is aligned Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::at_alt(const uword i) const Chris@49: { Chris@49: const eT* mem_aligned = mem; Chris@49: memory::mark_as_aligned(mem_aligned); Chris@49: Chris@49: return mem_aligned[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); bounds checking not done when ARMA_NO_DEBUG is defined Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::operator() (const uword i) Chris@49: { Chris@49: arma_debug_check( (i >= n_elem), "Cube::operator(): index out of bounds"); Chris@49: return access::rw(mem[i]); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); bounds checking not done when ARMA_NO_DEBUG is defined Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::operator() (const uword i) const Chris@49: { Chris@49: arma_debug_check( (i >= n_elem), "Cube::operator(): index out of bounds"); Chris@49: return mem[i]; Chris@49: } Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); no bounds check. Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::operator[] (const uword i) Chris@49: { Chris@49: return access::rw(mem[i]); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); no bounds check Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::operator[] (const uword i) const Chris@49: { Chris@49: return mem[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); no bounds check. Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::at(const uword i) Chris@49: { Chris@49: return access::rw(mem[i]); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! linear element accessor (treats the cube as a vector); no bounds check Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::at(const uword i) const Chris@49: { Chris@49: return mem[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::operator() (const uword in_row, const uword in_col, const uword in_slice) Chris@49: { Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row >= n_rows) || Chris@49: (in_col >= n_cols) || Chris@49: (in_slice >= n_slices) Chris@49: , Chris@49: "Cube::operator(): index out of bounds" Chris@49: ); Chris@49: Chris@49: return access::rw(mem[in_slice*n_elem_slice + in_col*n_rows + in_row]); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! element accessor; bounds checking not done when ARMA_NO_DEBUG is defined Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::operator() (const uword in_row, const uword in_col, const uword in_slice) const Chris@49: { Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row >= n_rows) || Chris@49: (in_col >= n_cols) || Chris@49: (in_slice >= n_slices) Chris@49: , Chris@49: "Cube::operator(): index out of bounds" Chris@49: ); Chris@49: Chris@49: return mem[in_slice*n_elem_slice + in_col*n_rows + in_row]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! element accessor; no bounds check Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::at(const uword in_row, const uword in_col, const uword in_slice) Chris@49: { Chris@49: return access::rw( mem[in_slice*n_elem_slice + in_col*n_rows + in_row] ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! element accessor; no bounds check Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::at(const uword in_row, const uword in_col, const uword in_slice) const Chris@49: { Chris@49: return mem[in_slice*n_elem_slice + in_col*n_rows + in_row]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! prefix ++ Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator++() Chris@49: { Chris@49: Cube_aux::prefix_pp(*this); Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! postfix ++ (must not return the object by reference) Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube::operator++(int) Chris@49: { Chris@49: Cube_aux::postfix_pp(*this); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! prefix -- Chris@49: template Chris@49: arma_inline Chris@49: const Cube& Chris@49: Cube::operator--() Chris@49: { Chris@49: Cube_aux::prefix_mm(*this); Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! postfix -- (must not return the object by reference) Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube::operator--(int) Chris@49: { Chris@49: Cube_aux::postfix_mm(*this); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if all of the elements are finite Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::is_finite() const Chris@49: { Chris@49: return arrayops::is_finite( memptr(), n_elem ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if the cube has no elements Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::is_empty() const Chris@49: { Chris@49: return (n_elem == 0); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if the given index is currently in range Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::in_range(const uword i) const Chris@49: { Chris@49: return (i < n_elem); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if the given start and end indices are currently in range Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::in_range(const span& x) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(x.whole == true) Chris@49: { Chris@49: return true; Chris@49: } Chris@49: else Chris@49: { Chris@49: const uword a = x.a; Chris@49: const uword b = x.b; Chris@49: Chris@49: return ( (a <= b) && (b < n_elem) ); Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if the given location is currently in range Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::in_range(const uword in_row, const uword in_col, const uword in_slice) const Chris@49: { Chris@49: return ( (in_row < n_rows) && (in_col < n_cols) && (in_slice < n_slices) ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: arma_warn_unused Chris@49: bool Chris@49: Cube::in_range(const span& row_span, const span& col_span, const span& slice_span) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const uword in_row1 = row_span.a; Chris@49: const uword in_row2 = row_span.b; Chris@49: Chris@49: const uword in_col1 = col_span.a; Chris@49: const uword in_col2 = col_span.b; Chris@49: Chris@49: const uword in_slice1 = slice_span.a; Chris@49: const uword in_slice2 = slice_span.b; Chris@49: Chris@49: Chris@49: const bool rows_ok = row_span.whole ? true : ( (in_row1 <= in_row2) && (in_row2 < n_rows) ); Chris@49: const bool cols_ok = col_span.whole ? true : ( (in_col1 <= in_col2) && (in_col2 < n_cols) ); Chris@49: const bool slices_ok = slice_span.whole ? true : ( (in_slice1 <= in_slice2) && (in_slice2 < n_slices) ); Chris@49: Chris@49: Chris@49: return ( (rows_ok == true) && (cols_ok == true) && (slices_ok == true) ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT* Chris@49: Cube::memptr() Chris@49: { Chris@49: return const_cast(mem); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT* Chris@49: Cube::memptr() const Chris@49: { Chris@49: return mem; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the specified slice in the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT* Chris@49: Cube::slice_memptr(const uword uslice) Chris@49: { Chris@49: return const_cast( &mem[ uslice*n_elem_slice ] ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the specified slice in the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT* Chris@49: Cube::slice_memptr(const uword uslice) const Chris@49: { Chris@49: return &mem[ uslice*n_elem_slice ]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the specified slice in the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT* Chris@49: Cube::slice_colptr(const uword uslice, const uword col) Chris@49: { Chris@49: return const_cast( &mem[ uslice*n_elem_slice + col*n_rows] ); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns a pointer to array of eTs used by the specified slice in the cube Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT* Chris@49: Cube::slice_colptr(const uword uslice, const uword col) const Chris@49: { Chris@49: return &mem[ uslice*n_elem_slice + col*n_rows ]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! print contents of the cube (to the cout stream), Chris@49: //! optionally preceding with a user specified line of text. Chris@49: //! the precision and cell width are modified. Chris@49: //! on return, the stream's state are restored to their original values. Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::impl_print(const std::string& extra_text) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(extra_text.length() != 0) Chris@49: { Chris@49: ARMA_DEFAULT_OSTREAM << extra_text << '\n'; Chris@49: } Chris@49: Chris@49: arma_ostream::print(ARMA_DEFAULT_OSTREAM, *this, true); Chris@49: } Chris@49: Chris@49: Chris@49: //! print contents of the cube to a user specified stream, Chris@49: //! optionally preceding with a user specified line of text. Chris@49: //! the precision and cell width are modified. Chris@49: //! on return, the stream's state are restored to their original values. Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::impl_print(std::ostream& user_stream, const std::string& extra_text) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(extra_text.length() != 0) Chris@49: { Chris@49: user_stream << extra_text << '\n'; Chris@49: } Chris@49: Chris@49: arma_ostream::print(user_stream, *this, true); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! print contents of the cube (to the cout stream), Chris@49: //! optionally preceding with a user specified line of text. Chris@49: //! the stream's state are used as is and are not modified Chris@49: //! (i.e. the precision and cell width are not modified). Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::impl_raw_print(const std::string& extra_text) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(extra_text.length() != 0) Chris@49: { Chris@49: ARMA_DEFAULT_OSTREAM << extra_text << '\n'; Chris@49: } Chris@49: Chris@49: arma_ostream::print(ARMA_DEFAULT_OSTREAM, *this, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! print contents of the cube to a user specified stream, Chris@49: //! optionally preceding with a user specified line of text. Chris@49: //! the stream's state are used as is and are not modified. Chris@49: //! (i.e. the precision and cell width are not modified). Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::impl_raw_print(std::ostream& user_stream, const std::string& extra_text) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(extra_text.length() != 0) Chris@49: { Chris@49: user_stream << extra_text << '\n'; Chris@49: } Chris@49: Chris@49: arma_ostream::print(user_stream, *this, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! change the cube to have user specified dimensions (data is not preserved) Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::set_size(const uword in_n_rows, const uword in_n_cols, const uword in_n_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: init_warm(in_n_rows, in_n_cols, in_n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! change the cube to have user specified dimensions (data is preserved) Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::reshape(const uword in_rows, const uword in_cols, const uword in_slices, const uword dim) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: *this = arma::reshape(*this, in_rows, in_cols, in_slices, dim); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! change the cube to have user specified dimensions (data is preserved) Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::resize(const uword in_rows, const uword in_cols, const uword in_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: *this = arma::resize(*this, in_rows, in_cols, in_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! change the cube (without preserving data) to have the same dimensions as the given cube Chris@49: template Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::copy_size(const Cube& m) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: init_warm(m.n_rows, m.n_cols, m.n_slices); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! transform each element in the cube using a functor Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::transform(functor F) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: eT* out_mem = memptr(); Chris@49: Chris@49: const uword N = n_elem; Chris@49: Chris@49: uword ii, jj; Chris@49: Chris@49: for(ii=0, jj=1; jj < N; ii+=2, jj+=2) Chris@49: { Chris@49: eT tmp_ii = out_mem[ii]; Chris@49: eT tmp_jj = out_mem[jj]; Chris@49: Chris@49: tmp_ii = eT( F(tmp_ii) ); Chris@49: tmp_jj = eT( F(tmp_jj) ); Chris@49: Chris@49: out_mem[ii] = tmp_ii; Chris@49: out_mem[jj] = tmp_jj; Chris@49: } Chris@49: Chris@49: if(ii < N) Chris@49: { Chris@49: out_mem[ii] = eT( F(out_mem[ii]) ); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! imbue (fill) the cube with values provided by a functor Chris@49: template Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::imbue(functor F) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: eT* out_mem = memptr(); Chris@49: Chris@49: const uword N = n_elem; Chris@49: Chris@49: uword ii, jj; Chris@49: Chris@49: for(ii=0, jj=1; jj < N; ii+=2, jj+=2) Chris@49: { Chris@49: const eT tmp_ii = eT( F() ); Chris@49: const eT tmp_jj = eT( F() ); Chris@49: Chris@49: out_mem[ii] = tmp_ii; Chris@49: out_mem[jj] = tmp_jj; Chris@49: } Chris@49: Chris@49: if(ii < N) Chris@49: { Chris@49: out_mem[ii] = eT( F() ); Chris@49: } Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! fill the cube with the specified value Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::fill(const eT val) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arrayops::inplace_set( memptr(), val, n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::zeros() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).fill(eT(0)); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::zeros(const uword in_rows, const uword in_cols, const uword in_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: set_size(in_rows, in_cols, in_slices); Chris@49: Chris@49: return (*this).fill(eT(0)); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::ones() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).fill(eT(1)); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::ones(const uword in_rows, const uword in_cols, const uword in_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: set_size(in_rows, in_cols, in_slices); Chris@49: Chris@49: return (*this).fill(eT(1)); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::randu() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: eop_aux_randu::fill( memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::randu(const uword in_rows, const uword in_cols, const uword in_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: set_size(in_rows, in_cols, in_slices); Chris@49: Chris@49: return (*this).randu(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::randn() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: eop_aux_randn::fill( memptr(), n_elem ); Chris@49: Chris@49: return *this; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: const Cube& Chris@49: Cube::randn(const uword in_rows, const uword in_cols, const uword in_slices) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: set_size(in_rows, in_cols, in_slices); Chris@49: Chris@49: return (*this).randn(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::reset() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: init_warm(0,0,0); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::set_real(const BaseCube::pod_type,T1>& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: Cube_aux::set_real(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::set_imag(const BaseCube::pod_type,T1>& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: Cube_aux::set_imag(*this, X); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: arma_warn_unused Chris@49: eT Chris@49: Cube::min() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "min(): object has no elements" ); Chris@49: Chris@49: return op_min::direct_min(memptr(), n_elem); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: arma_warn_unused Chris@49: eT Chris@49: Cube::max() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "max(): object has no elements" ); Chris@49: Chris@49: return op_max::direct_max(memptr(), n_elem); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: eT Chris@49: Cube::min(uword& index_of_min_val) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "min(): object has no elements" ); Chris@49: Chris@49: return op_min::direct_min(memptr(), n_elem, index_of_min_val); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: eT Chris@49: Cube::max(uword& index_of_max_val) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "max(): object has no elements" ); Chris@49: Chris@49: return op_max::direct_max(memptr(), n_elem, index_of_max_val); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: eT Chris@49: Cube::min(uword& row_of_min_val, uword& col_of_min_val, uword& slice_of_min_val) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "min(): object has no elements" ); Chris@49: Chris@49: uword i; Chris@49: Chris@49: eT val = op_min::direct_min(memptr(), n_elem, i); Chris@49: Chris@49: const uword in_slice = i / n_elem_slice; Chris@49: const uword offset = in_slice * n_elem_slice; Chris@49: const uword j = i - offset; Chris@49: Chris@49: row_of_min_val = j % n_rows; Chris@49: col_of_min_val = j / n_rows; Chris@49: slice_of_min_val = in_slice; Chris@49: Chris@49: return val; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: eT Chris@49: Cube::max(uword& row_of_max_val, uword& col_of_max_val, uword& slice_of_max_val) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (n_elem == 0), "max(): object has no elements" ); Chris@49: Chris@49: uword i; Chris@49: Chris@49: eT val = op_max::direct_max(memptr(), n_elem, i); Chris@49: Chris@49: const uword in_slice = i / n_elem_slice; Chris@49: const uword offset = in_slice * n_elem_slice; Chris@49: const uword j = i - offset; Chris@49: Chris@49: row_of_max_val = j % n_rows; Chris@49: col_of_max_val = j / n_rows; Chris@49: slice_of_max_val = in_slice; Chris@49: Chris@49: return val; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! save the cube to a file Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::save(const std::string name, const file_type type, const bool print_status) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: bool save_okay; Chris@49: Chris@49: switch(type) Chris@49: { Chris@49: case raw_ascii: Chris@49: save_okay = diskio::save_raw_ascii(*this, name); Chris@49: break; Chris@49: Chris@49: case arma_ascii: Chris@49: save_okay = diskio::save_arma_ascii(*this, name); Chris@49: break; Chris@49: Chris@49: case raw_binary: Chris@49: save_okay = diskio::save_raw_binary(*this, name); Chris@49: break; Chris@49: Chris@49: case arma_binary: Chris@49: save_okay = diskio::save_arma_binary(*this, name); Chris@49: break; Chris@49: Chris@49: case ppm_binary: Chris@49: save_okay = diskio::save_ppm_binary(*this, name); Chris@49: break; Chris@49: Chris@49: case hdf5_binary: Chris@49: save_okay = diskio::save_hdf5_binary(*this, name); Chris@49: break; Chris@49: Chris@49: default: Chris@49: arma_warn(print_status, "Cube::save(): unsupported file type"); Chris@49: save_okay = false; Chris@49: } Chris@49: Chris@49: arma_warn( (print_status && (save_okay == false)), "Cube::save(): couldn't write to ", name); Chris@49: Chris@49: return save_okay; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! save the cube to a stream Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::save(std::ostream& os, const file_type type, const bool print_status) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: bool save_okay; Chris@49: Chris@49: switch(type) Chris@49: { Chris@49: case raw_ascii: Chris@49: save_okay = diskio::save_raw_ascii(*this, os); Chris@49: break; Chris@49: Chris@49: case arma_ascii: Chris@49: save_okay = diskio::save_arma_ascii(*this, os); Chris@49: break; Chris@49: Chris@49: case raw_binary: Chris@49: save_okay = diskio::save_raw_binary(*this, os); Chris@49: break; Chris@49: Chris@49: case arma_binary: Chris@49: save_okay = diskio::save_arma_binary(*this, os); Chris@49: break; Chris@49: Chris@49: case ppm_binary: Chris@49: save_okay = diskio::save_ppm_binary(*this, os); Chris@49: break; Chris@49: Chris@49: default: Chris@49: arma_warn(print_status, "Cube::save(): unsupported file type"); Chris@49: save_okay = false; Chris@49: } Chris@49: Chris@49: arma_warn( (print_status && (save_okay == false)), "Cube::save(): couldn't write to given stream"); Chris@49: Chris@49: return save_okay; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! load a cube from a file Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::load(const std::string name, const file_type type, const bool print_status) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: bool load_okay; Chris@49: std::string err_msg; Chris@49: Chris@49: switch(type) Chris@49: { Chris@49: case auto_detect: Chris@49: load_okay = diskio::load_auto_detect(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case raw_ascii: Chris@49: load_okay = diskio::load_raw_ascii(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case arma_ascii: Chris@49: load_okay = diskio::load_arma_ascii(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case raw_binary: Chris@49: load_okay = diskio::load_raw_binary(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case arma_binary: Chris@49: load_okay = diskio::load_arma_binary(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case ppm_binary: Chris@49: load_okay = diskio::load_ppm_binary(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: case hdf5_binary: Chris@49: load_okay = diskio::load_hdf5_binary(*this, name, err_msg); Chris@49: break; Chris@49: Chris@49: default: Chris@49: arma_warn(print_status, "Cube::load(): unsupported file type"); Chris@49: load_okay = false; Chris@49: } Chris@49: Chris@49: if( (print_status == true) && (load_okay == false) ) Chris@49: { Chris@49: if(err_msg.length() > 0) Chris@49: { Chris@49: arma_warn(true, "Cube::load(): ", err_msg, name); Chris@49: } Chris@49: else Chris@49: { Chris@49: arma_warn(true, "Cube::load(): couldn't read ", name); Chris@49: } Chris@49: } Chris@49: Chris@49: if(load_okay == false) Chris@49: { Chris@49: (*this).reset(); Chris@49: } Chris@49: Chris@49: return load_okay; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! load a cube from a stream Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::load(std::istream& is, const file_type type, const bool print_status) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: bool load_okay; Chris@49: std::string err_msg; Chris@49: Chris@49: switch(type) Chris@49: { Chris@49: case auto_detect: Chris@49: load_okay = diskio::load_auto_detect(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: case raw_ascii: Chris@49: load_okay = diskio::load_raw_ascii(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: case arma_ascii: Chris@49: load_okay = diskio::load_arma_ascii(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: case raw_binary: Chris@49: load_okay = diskio::load_raw_binary(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: case arma_binary: Chris@49: load_okay = diskio::load_arma_binary(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: case ppm_binary: Chris@49: load_okay = diskio::load_ppm_binary(*this, is, err_msg); Chris@49: break; Chris@49: Chris@49: default: Chris@49: arma_warn(print_status, "Cube::load(): unsupported file type"); Chris@49: load_okay = false; Chris@49: } Chris@49: Chris@49: Chris@49: if( (print_status == true) && (load_okay == false) ) Chris@49: { Chris@49: if(err_msg.length() > 0) Chris@49: { Chris@49: arma_warn(true, "Cube::load(): ", err_msg, "the given stream"); Chris@49: } Chris@49: else Chris@49: { Chris@49: arma_warn(true, "Cube::load(): couldn't load from the given stream"); Chris@49: } Chris@49: } Chris@49: Chris@49: if(load_okay == false) Chris@49: { Chris@49: (*this).reset(); Chris@49: } Chris@49: Chris@49: return load_okay; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! save the cube to a file, without printing any error messages Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::quiet_save(const std::string name, const file_type type) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).save(name, type, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! save the cube to a stream, without printing any error messages Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::quiet_save(std::ostream& os, const file_type type) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).save(os, type, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! load a cube from a file, without printing any error messages Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::quiet_load(const std::string name, const file_type type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).load(name, type, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! load a cube from a stream, without printing any error messages Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::quiet_load(std::istream& is, const file_type type) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return (*this).load(is, type, false); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::iterator Chris@49: Cube::begin() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_iterator Chris@49: Cube::begin() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_iterator Chris@49: Cube::cbegin() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::iterator Chris@49: Cube::end() Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr() + n_elem; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_iterator Chris@49: Cube::end() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr() + n_elem; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_iterator Chris@49: Cube::cend() const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: return memptr() + n_elem; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::slice_iterator Chris@49: Cube::begin_slice(const uword slice_num) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (slice_num >= n_slices), "begin_slice(): index out of bounds"); Chris@49: Chris@49: return slice_memptr(slice_num); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_slice_iterator Chris@49: Cube::begin_slice(const uword slice_num) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (slice_num >= n_slices), "begin_slice(): index out of bounds"); Chris@49: Chris@49: return slice_memptr(slice_num); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::slice_iterator Chris@49: Cube::end_slice(const uword slice_num) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (slice_num >= n_slices), "end_slice(): index out of bounds"); Chris@49: Chris@49: return slice_memptr(slice_num) + n_elem_slice; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: typename Cube::const_slice_iterator Chris@49: Cube::end_slice(const uword slice_num) const Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: arma_debug_check( (slice_num >= n_slices), "end_slice(): index out of bounds"); Chris@49: Chris@49: return slice_memptr(slice_num) + n_elem_slice; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! resets this cube to an empty matrix Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::clear() Chris@49: { Chris@49: reset(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns true if the cube has no elements Chris@49: template Chris@49: inline Chris@49: bool Chris@49: Cube::empty() const Chris@49: { Chris@49: return (n_elem == 0); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! returns the number of elements in this cube Chris@49: template Chris@49: inline Chris@49: uword Chris@49: Cube::size() const Chris@49: { Chris@49: return n_elem; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: // template Chris@49: // inline Chris@49: // void Chris@49: // Cube::swap(Cube& B) Chris@49: // { Chris@49: // // TODO Chris@49: // } Chris@49: Chris@49: Chris@49: Chris@49: //! try to steal the memory from a given cube; Chris@49: //! if memory can't be stolen, copy the given cube Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube::steal_mem(Cube& x) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: if(this != &x) Chris@49: { Chris@49: if( (x.mem_state == 0) && (x.n_elem > Cube_prealloc::mem_n_elem) ) Chris@49: { Chris@49: reset(); Chris@49: Chris@49: const uword x_n_slices = x.n_slices; Chris@49: Chris@49: access::rw(n_rows) = x.n_rows; Chris@49: access::rw(n_cols) = x.n_cols; Chris@49: access::rw(n_elem_slice) = x.n_elem_slice; Chris@49: access::rw(n_slices) = x_n_slices; Chris@49: access::rw(n_elem) = x.n_elem; Chris@49: access::rw(mem) = x.mem; Chris@49: Chris@49: if(x_n_slices > Cube_prealloc::mat_ptrs_size) Chris@49: { Chris@49: access::rw( mat_ptrs) = x.mat_ptrs; Chris@49: access::rw(x.mat_ptrs) = 0; Chris@49: } Chris@49: else Chris@49: { Chris@49: access::rw(mat_ptrs) = const_cast< const Mat** >(mat_ptrs_local); Chris@49: Chris@49: for(uword i=0; i < x_n_slices; ++i) Chris@49: { Chris@49: mat_ptrs[i] = x.mat_ptrs[i]; Chris@49: x.mat_ptrs[i] = 0; Chris@49: } Chris@49: } Chris@49: Chris@49: access::rw(x.n_rows) = 0; Chris@49: access::rw(x.n_cols) = 0; Chris@49: access::rw(x.n_elem_slice) = 0; Chris@49: access::rw(x.n_slices) = 0; Chris@49: access::rw(x.n_elem) = 0; Chris@49: access::rw(x.mem) = 0; Chris@49: } Chris@49: else Chris@49: { Chris@49: (*this).operator=(x); Chris@49: } Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube::fixed::mem_setup() Chris@49: { Chris@49: arma_extra_debug_sigprint_this(this); Chris@49: Chris@49: if(fixed_n_elem > 0) Chris@49: { Chris@49: access::rw(Cube::n_rows) = fixed_n_rows; Chris@49: access::rw(Cube::n_cols) = fixed_n_cols; Chris@49: access::rw(Cube::n_elem_slice) = fixed_n_rows * fixed_n_cols; Chris@49: access::rw(Cube::n_slices) = fixed_n_slices; Chris@49: access::rw(Cube::n_elem) = fixed_n_elem; Chris@49: access::rw(Cube::mem_state) = 3; Chris@49: access::rw(Cube::mat_ptrs) = const_cast< const Mat** >( \ Chris@49: (fixed_n_slices > Cube_prealloc::mat_ptrs_size) ? mat_ptrs_local_extra : mat_ptrs_local ); Chris@49: access::rw(Cube::mem) = (fixed_n_elem > Cube_prealloc::mem_n_elem) ? mem_local_extra : mem_local; Chris@49: Chris@49: create_mat(); Chris@49: } Chris@49: else Chris@49: { Chris@49: access::rw(Cube::n_rows) = 0; Chris@49: access::rw(Cube::n_cols) = 0; Chris@49: access::rw(Cube::n_elem_slice) = 0; Chris@49: access::rw(Cube::n_slices) = 0; Chris@49: access::rw(Cube::n_elem) = 0; Chris@49: access::rw(Cube::mem_state) = 3; Chris@49: access::rw(Cube::mat_ptrs) = 0; Chris@49: access::rw(Cube::mem) = 0; Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::fixed::operator[] (const uword i) Chris@49: { Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::fixed::operator[] (const uword i) const Chris@49: { Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::fixed::at(const uword i) Chris@49: { Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::fixed::at(const uword i) const Chris@49: { Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::fixed::operator() (const uword i) Chris@49: { Chris@49: arma_debug_check( (i >= fixed_n_elem), "Cube::operator(): index out of bounds"); Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::fixed::operator() (const uword i) const Chris@49: { Chris@49: arma_debug_check( (i >= fixed_n_elem), "Cube::operator(): index out of bounds"); Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::fixed::at(const uword in_row, const uword in_col, const uword in_slice) Chris@49: { Chris@49: const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row; Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::fixed::at(const uword in_row, const uword in_col, const uword in_slice) const Chris@49: { Chris@49: const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row; Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: eT& Chris@49: Cube::fixed::operator() (const uword in_row, const uword in_col, const uword in_slice) Chris@49: { Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row >= fixed_n_rows ) || Chris@49: (in_col >= fixed_n_cols ) || Chris@49: (in_slice >= fixed_n_slices) Chris@49: , Chris@49: "operator(): index out of bounds" Chris@49: ); Chris@49: Chris@49: const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row; Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: template Chris@49: arma_inline Chris@49: arma_warn_unused Chris@49: const eT& Chris@49: Cube::fixed::operator() (const uword in_row, const uword in_col, const uword in_slice) const Chris@49: { Chris@49: arma_debug_check Chris@49: ( Chris@49: (in_row >= fixed_n_rows ) || Chris@49: (in_col >= fixed_n_cols ) || Chris@49: (in_slice >= fixed_n_slices) Chris@49: , Chris@49: "Cube::operator(): index out of bounds" Chris@49: ); Chris@49: Chris@49: const uword i = in_slice*fixed_n_elem_slice + in_col*fixed_n_rows + in_row; Chris@49: Chris@49: return (use_extra) ? mem_local_extra[i] : mem_local[i]; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! prefix ++ Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::prefix_pp(Cube& x) Chris@49: { Chris@49: eT* memptr = x.memptr(); Chris@49: const uword n_elem = x.n_elem; Chris@49: Chris@49: uword i,j; Chris@49: Chris@49: for(i=0, j=1; j Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::prefix_pp(Cube< std::complex >& x) Chris@49: { Chris@49: x += T(1); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! postfix ++ Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::postfix_pp(Cube& x) Chris@49: { Chris@49: eT* memptr = x.memptr(); Chris@49: const uword n_elem = x.n_elem; Chris@49: Chris@49: uword i,j; Chris@49: Chris@49: for(i=0, j=1; j Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::postfix_pp(Cube< std::complex >& x) Chris@49: { Chris@49: x += T(1); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! prefix -- Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::prefix_mm(Cube& x) Chris@49: { Chris@49: eT* memptr = x.memptr(); Chris@49: const uword n_elem = x.n_elem; Chris@49: Chris@49: uword i,j; Chris@49: Chris@49: for(i=0, j=1; j Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::prefix_mm(Cube< std::complex >& x) Chris@49: { Chris@49: x -= T(1); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: //! postfix -- Chris@49: template Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::postfix_mm(Cube& x) Chris@49: { Chris@49: eT* memptr = x.memptr(); Chris@49: const uword n_elem = x.n_elem; Chris@49: Chris@49: uword i,j; Chris@49: Chris@49: for(i=0, j=1; j Chris@49: arma_inline Chris@49: void Chris@49: Cube_aux::postfix_mm(Cube< std::complex >& x) Chris@49: { Chris@49: x -= T(1); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube_aux::set_real(Cube& out, const BaseCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: const unwrap_cube tmp(X.get_ref()); Chris@49: const Cube& A = tmp.M; Chris@49: Chris@49: arma_debug_assert_same_size( out, A, "Cube::set_real()" ); Chris@49: Chris@49: out = A; Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube_aux::set_imag(Cube&, const BaseCube&) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube_aux::set_real(Cube< std::complex >& out, const BaseCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename std::complex eT; Chris@49: Chris@49: const ProxyCube P(X.get_ref()); Chris@49: Chris@49: const uword local_n_rows = P.get_n_rows(); Chris@49: const uword local_n_cols = P.get_n_cols(); Chris@49: const uword local_n_slices = P.get_n_slices(); Chris@49: Chris@49: arma_debug_assert_same_size Chris@49: ( Chris@49: out.n_rows, out.n_cols, out.n_slices, Chris@49: local_n_rows, local_n_cols, local_n_slices, Chris@49: "Cube::set_real()" Chris@49: ); Chris@49: Chris@49: eT* out_mem = out.memptr(); Chris@49: Chris@49: if(ProxyCube::prefer_at_accessor == false) Chris@49: { Chris@49: typedef typename ProxyCube::ea_type ea_type; Chris@49: Chris@49: ea_type A = P.get_ea(); Chris@49: Chris@49: const uword N = out.n_elem; Chris@49: Chris@49: for(uword i=0; i( A[i], out_mem[i].imag() ); Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: for(uword slice = 0; slice < local_n_slices; ++slice) Chris@49: for(uword col = 0; col < local_n_cols; ++col ) Chris@49: for(uword row = 0; row < local_n_rows; ++row ) Chris@49: { Chris@49: (*out_mem) = std::complex( P.at(row,col,slice), (*out_mem).imag() ); Chris@49: out_mem++; Chris@49: } Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: template Chris@49: inline Chris@49: void Chris@49: Cube_aux::set_imag(Cube< std::complex >& out, const BaseCube& X) Chris@49: { Chris@49: arma_extra_debug_sigprint(); Chris@49: Chris@49: typedef typename std::complex eT; Chris@49: Chris@49: const ProxyCube P(X.get_ref()); Chris@49: Chris@49: const uword local_n_rows = P.get_n_rows(); Chris@49: const uword local_n_cols = P.get_n_cols(); Chris@49: const uword local_n_slices = P.get_n_slices(); Chris@49: Chris@49: arma_debug_assert_same_size Chris@49: ( Chris@49: out.n_rows, out.n_cols, out.n_slices, Chris@49: local_n_rows, local_n_cols, local_n_slices, Chris@49: "Cube::set_imag()" Chris@49: ); Chris@49: Chris@49: eT* out_mem = out.memptr(); Chris@49: Chris@49: if(ProxyCube::prefer_at_accessor == false) Chris@49: { Chris@49: typedef typename ProxyCube::ea_type ea_type; Chris@49: Chris@49: ea_type A = P.get_ea(); Chris@49: Chris@49: const uword N = out.n_elem; Chris@49: Chris@49: for(uword i=0; i( out_mem[i].real(), A[i] ); Chris@49: } Chris@49: } Chris@49: else Chris@49: { Chris@49: for(uword slice = 0; slice < local_n_slices; ++slice) Chris@49: for(uword col = 0; col < local_n_cols; ++col ) Chris@49: for(uword row = 0; row < local_n_rows; ++row ) Chris@49: { Chris@49: (*out_mem) = std::complex( (*out_mem).real(), P.at(row,col,slice) ); Chris@49: out_mem++; Chris@49: } Chris@49: } Chris@49: } Chris@49: Chris@49: Chris@49: Chris@49: #ifdef ARMA_EXTRA_CUBE_MEAT Chris@49: #include ARMA_INCFILE_WRAP(ARMA_EXTRA_CUBE_MEAT) Chris@49: #endif Chris@49: Chris@49: Chris@49: Chris@49: //! @}