224
( ((t_vec_state == 1) && (in_n_cols != 1)) || ((t_vec_state == 2) && (in_n_rows != 1)) ),
225
"Mat::init(): object is a row or column vector; requested size is not compatible"
227
"Mat::init(): requested size is not compatible with column vector layout"
238
"Mat::init(): requested size is not compatible with row vector layout"
281
297
arma_extra_debug_print("Mat::init(): allocating memory");
283
299
access::rw(mem) = memory::acquire<eT>(new_n_elem);
285
arma_check_bad_alloc( (mem == 0), "Mat::init(): out of memory" );
288
302
access::rw(n_rows) = in_n_rows;
362
376
template<typename eT>
365
Mat<eT>::init(const std::string& text)
379
Mat<eT>::init(const std::string& text_orig)
367
381
arma_extra_debug_sigprint();
383
const bool replace_commas = (is_cx<eT>::yes) ? false : ( text_orig.find(',') != std::string::npos );
385
std::string* text_mod = (replace_commas) ? new std::string(text_orig) : NULL;
387
const std::string& text = (replace_commas) ? ( std::replace((*text_mod).begin(), (*text_mod).end(), ',', ' '), (*text_mod) ) : text_orig;
370
390
// work out the size
379
399
std::string::size_type line_start = 0;
380
400
std::string::size_type line_end = 0;
402
std::stringstream line_stream;
382
404
while( line_start < text.length() )
385
406
line_end = text.find(';', line_start);
387
408
if(line_end == std::string::npos)
388
410
line_end = text.length()-1;
390
413
std::string::size_type line_len = line_end - line_start + 1;
391
std::stringstream line_stream( text.substr(line_start,line_len) );
416
line_stream.str( text.substr(line_start,line_len) );
394
418
uword line_n_cols = 0;
395
419
while(line_stream >> token)
401
424
if(line_n_cols > 0)
403
426
if(t_n_cols_found == false)
405
t_n_cols = line_n_cols;
428
t_n_cols = line_n_cols;
406
429
t_n_cols_found = true;
409
433
arma_check( (line_n_cols != t_n_cols), "Mat::init(): inconsistent number of columns in given string");
413
439
line_start = line_end+1;
417
443
Mat<eT>& x = *this;
418
444
x.set_size(t_n_rows, t_n_cols);
425
451
while( line_start < text.length() )
428
453
line_end = text.find(';', line_start);
430
455
if(line_end == std::string::npos)
431
457
line_end = text.length()-1;
433
460
std::string::size_type line_len = line_end - line_start + 1;
434
std::stringstream line_stream( text.substr(line_start,line_len) );
463
line_stream.str( text.substr(line_start,line_len) );
436
465
// uword ucol = 0;
437
466
// while(line_stream >> token)
535
568
template<typename eT>
537
Mat<eT>::Mat(Mat<eT>&& in_mat)
570
Mat<eT>::Mat(Mat<eT>&& X)
545
arma_extra_debug_sigprint_this(this);
546
arma_extra_debug_sigprint(arma_boost::format("this = %x in_mat = %x") % this % &in_mat);
578
arma_extra_debug_sigprint(arma_boost::format("this = %x X = %x") % this % &X);
548
(*this).steal_mem(in_mat);
580
if( ((X.mem_state == 0) && (X.n_elem > arma_config::mat_prealloc)) || (X.mem_state == 1) || (X.mem_state == 2) )
582
access::rw(mem_state) = X.mem_state;
583
access::rw(mem) = X.mem;
585
access::rw(X.n_rows) = 0;
586
access::rw(X.n_cols) = 0;
587
access::rw(X.n_elem) = 0;
588
access::rw(X.mem_state) = 0;
589
access::rw(X.mem) = 0;
595
arrayops::copy( memptr(), X.mem, X.n_elem );
553
601
template<typename eT>
556
Mat<eT>::operator=(Mat<eT>&& in_mat)
604
Mat<eT>::operator=(Mat<eT>&& X)
558
arma_extra_debug_sigprint(arma_boost::format("this = %x in_mat = %x") % this % &in_mat);
606
arma_extra_debug_sigprint(arma_boost::format("this = %x X = %x") % this % &X);
560
(*this).steal_mem(in_mat);
608
(*this).steal_mem(X);
773
821
Mat<eT>& A = (*this);
775
823
arma_extra_debug_sigprint(arma_boost::format("A = %x B = %x") % &A % &B);
825
bool layout_ok = false;
777
arma_debug_check( (A.vec_state != B.vec_state), "Mat::swap(): incompatible object types" );
827
if(A.vec_state == B.vec_state)
833
const uhword A_vec_state = A.vec_state;
834
const uhword B_vec_state = B.vec_state;
836
const bool A_absorbs_B = (A_vec_state == 0) || ( (A_vec_state == 1) && (B.n_cols == 1) ) || ( (A_vec_state == 2) && (B.n_rows == 1) );
837
const bool B_absorbs_A = (B_vec_state == 0) || ( (B_vec_state == 1) && (A.n_cols == 1) ) || ( (B_vec_state == 2) && (A.n_rows == 1) );
839
layout_ok = A_absorbs_B && B_absorbs_A;
779
842
const uhword A_mem_state = A.mem_state;
780
843
const uhword B_mem_state = B.mem_state;
782
if( (A_mem_state == 0) && (B_mem_state == 0) )
845
if( (A_mem_state == 0) && (B_mem_state == 0) && layout_ok )
784
847
const uword A_n_elem = A.n_elem;
785
848
const uword B_n_elem = B.n_elem;
832
895
std::swap( access::rw(A.n_elem), access::rw(B.n_elem) );
835
if( (A_mem_state == 3) && (B_mem_state == 3) )
837
arma_debug_check( ((A.n_rows != B.n_rows) || (A.n_cols != B.n_cols)), "Mat::swap(): incompatible object types" );
839
const uword N = A.n_elem;
841
eT* A_mem = A.memptr();
842
eT* B_mem = B.memptr();
844
for(uword ii=0; ii < N; ++ii) { std::swap(A_mem[ii], B_mem[ii]); }
848
arma_bad("Mat::swap(): incompatible object types");
898
if( (A_mem_state <= 2) && (B_mem_state <= 2) && (A.n_elem == B.n_elem) && layout_ok )
900
std::swap( access::rw(A.n_rows), access::rw(B.n_rows) );
901
std::swap( access::rw(A.n_cols), access::rw(B.n_cols) );
903
const uword N = A.n_elem;
905
eT* A_mem = A.memptr();
906
eT* B_mem = B.memptr();
908
for(uword ii=0; ii < N; ++ii) { std::swap(A_mem[ii], B_mem[ii]); }
911
if( (A.n_rows == B.n_rows) && (A.n_cols == B.n_cols) )
913
const uword N = A.n_elem;
915
eT* A_mem = A.memptr();
916
eT* B_mem = B.memptr();
918
for(uword ii=0; ii < N; ++ii) { std::swap(A_mem[ii], B_mem[ii]); }
922
// generic swap to handle remaining cases
924
if(A.n_elem <= B.n_elem)
882
if( (t_vec_state == 1) && ( x_n_cols == 1) )
887
if( (t_vec_state == 2) && ( x_n_rows == 1) )
971
if( (t_vec_state == 1) && (x_n_cols == 1) ) { layout_ok = true; }
972
if( (t_vec_state == 2) && (x_n_rows == 1) ) { layout_ok = true; }
894
if( (t_mem_state <= 1) && (x_mem_state <= 1) && (x_n_elem > arma_config::mat_prealloc) && (layout_ok == true) )
976
if( (t_mem_state <= 1) && ( ((x_mem_state == 0) && (x_n_elem > arma_config::mat_prealloc)) || (x_mem_state == 1) ) && layout_ok )
897
// note: calling reset() also prevents fixed size matrices from changing size or using non-local memory
899
access::rw(n_rows) = x_n_rows;
900
access::rw(n_cols) = x_n_cols;
901
access::rw(n_elem) = x_n_elem;
902
access::rw(mem) = x.mem;
980
access::rw(n_rows) = x_n_rows;
981
access::rw(n_cols) = x_n_cols;
982
access::rw(n_elem) = x_n_elem;
983
access::rw(mem_state) = x_mem_state;
984
access::rw(mem) = x.mem;
904
986
access::rw(x.n_rows) = 0;
905
987
access::rw(x.n_cols) = 0;
2834
2917
//! creation of subview (submatrix)
2835
2918
template<typename eT>
2921
Mat<eT>::submat(const uword in_row1, const uword in_col1, const SizeMat& s)
2923
arma_extra_debug_sigprint();
2925
const uword l_n_rows = n_rows;
2926
const uword l_n_cols = n_cols;
2928
const uword s_n_rows = s.n_rows;
2929
const uword s_n_cols = s.n_cols;
2933
((in_row1 >= l_n_rows) || (in_col1 >= l_n_cols) || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols)),
2934
"Mat::submat(): indices or size out of bounds"
2937
return subview<eT>(*this, in_row1, in_col1, s_n_rows, s_n_cols);
2942
//! creation of subview (submatrix)
2943
template<typename eT>
2946
Mat<eT>::submat(const uword in_row1, const uword in_col1, const SizeMat& s) const
2948
arma_extra_debug_sigprint();
2950
const uword l_n_rows = n_rows;
2951
const uword l_n_cols = n_cols;
2953
const uword s_n_rows = s.n_rows;
2954
const uword s_n_cols = s.n_cols;
2958
((in_row1 >= l_n_rows) || (in_col1 >= l_n_cols) || ((in_row1 + s_n_rows) > l_n_rows) || ((in_col1 + s_n_cols) > l_n_cols)),
2959
"Mat::submat(): indices or size out of bounds"
2962
return subview<eT>(*this, in_row1, in_col1, s_n_rows, s_n_cols);
2967
//! creation of subview (submatrix)
2968
template<typename eT>
2838
2971
Mat<eT>::submat(const span& row_span, const span& col_span)
2930
3063
template<typename eT>
3066
Mat<eT>::operator()(const uword in_row1, const uword in_col1, const SizeMat& s)
3068
arma_extra_debug_sigprint();
3070
return (*this).submat(in_row1, in_col1, s);
3075
template<typename eT>
3078
Mat<eT>::operator()(const uword in_row1, const uword in_col1, const SizeMat& s) const
3080
arma_extra_debug_sigprint();
3082
return (*this).submat(in_row1, in_col1, s);
3087
template<typename eT>
2931
3088
template<typename T1>
2933
3090
subview_elem1<eT,T1>
5092
template<typename eT>
5096
Mat<eT>::in_range(const uword in_row, const uword in_col, const SizeMat& s) const
5098
const uword l_n_rows = n_rows;
5099
const uword l_n_cols = n_cols;
5101
if( (in_row >= l_n_rows) || (in_col >= l_n_cols) || ((in_row + s.n_rows) > l_n_rows) || ((in_col + s.n_cols) > l_n_cols) )
4935
5113
//! returns a pointer to array of eTs for a specified column; no bounds check
4936
5114
template<typename eT>