Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions TMB/inst/include/tmbutils/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,31 @@ struct array:Map< Array<Type,Dynamic,1> >{
return array(p,newdim);
}

/** \brief Extract sub-array with write access
Index i refers to the inner-most (i.e. first) dimension.
*/
array<Type> row(int i){
int nsize = this->MapBase::size()/this->rows();
int nslice = this->rows();

vector<int> newdim;
if(dim.size() > 1){
newdim = dim.tail(dim.size()-1);
} else {
newdim.resize(1);
newdim << 1;
}

vector<Type> x(nsize);
array<Type> ans(x, newdim); /* Create new array with nsize dimension */

for(int j=0; j < ans.size(); j++){ /* Loop through values of old array */
ans[j] = (*this)[j*nslice + i];
}

return ans;
}

/** \brief Elementwise subsetting 1D array.
Also allowed in general to access the underlying vector of n-dim array. */
Type& operator()(int i1){
Expand Down Expand Up @@ -305,9 +330,5 @@ struct array:Map< Array<Type,Dynamic,1> >{

/** \brief Convert TMB array to vector */
tmbutils::vector<Type> vec() { return *this; }

/* Methods this class should *not* inherit (generate compile time error if used) */
private:
using MapBase::row;
};