| SpSubview_meat.hpp | | SpSubview_meat.hpp | |
| | | | |
| skipping to change at line 161 | | skipping to change at line 161 | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator*=(const eT val) | | SpSubview<eT>::operator*=(const eT val) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| if(val == eT(0)) | | if(val == eT(0)) | |
| { | | { | |
| // Turn it all into zeros. | | // Turn it all into zeros. | |
|
| for(typename SpMat<eT>::iterator it(access::rw(m), aux_row1, aux_col1);
it.col() < (aux_col1 + n_cols); ++it) | | for(iterator it(*this); it.pos() < n_nonzero; ++it) | |
| { | | { | |
|
| if((it.row() >= aux_row1) && (it.row() < (aux_row1 + n_cols))) | | (*it) = eT(0); // zero out the value. | |
| { | | it.internal_pos--; | |
| (*it) = eT(0); // zero out the value. | | | |
| } | | | |
| } | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| const uword start_row = aux_row1; | | const uword start_row = aux_row1; | |
| const uword end_row = aux_row1 + n_rows; | | const uword end_row = aux_row1 + n_rows; | |
| const uword start_col = aux_col1; | | const uword start_col = aux_col1; | |
| const uword end_col = aux_col1 + n_cols; | | const uword end_col = aux_col1 + n_cols; | |
| | | | |
| | | | |
| skipping to change at line 253 | | skipping to change at line 251 | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator+=(const Base<eT, T1>& x) | | SpSubview<eT>::operator+=(const Base<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const Proxy<T1> P(x.get_ref()); | | const Proxy<T1> P(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "addition into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "addition"); | |
| | | | |
| const uword old_n_nonzero = m.n_nonzero; | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
| for(uword c = 0; c < n_cols; ++c) | | for(uword c = 0; c < n_cols; ++c) | |
| { | | { | |
| for(uword r = 0; r < n_rows; ++r) | | for(uword r = 0; r < n_rows; ++r) | |
| { | | { | |
| at(r, c) += P.at(r, c); | | at(r, c) += P.at(r, c); | |
| } | | } | |
| } | | } | |
| | | | |
| skipping to change at line 282 | | skipping to change at line 280 | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator-=(const Base<eT, T1>& x) | | SpSubview<eT>::operator-=(const Base<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const Proxy<T1> P(x.get_ref()); | | const Proxy<T1> P(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "subtraction into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "subtraction"); | |
| | | | |
| const uword old_n_nonzero = m.n_nonzero; | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
| for(uword c = 0; c < n_cols; ++c) | | for(uword c = 0; c < n_cols; ++c) | |
| { | | { | |
| for(uword r = 0; r < n_rows; ++r) | | for(uword r = 0; r < n_rows; ++r) | |
| { | | { | |
| at(r, c) -= P.at(r, c); | | at(r, c) -= P.at(r, c); | |
| } | | } | |
| } | | } | |
| | | | |
| skipping to change at line 312 | | skipping to change at line 310 | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator*=(const Base<eT, T1>& x) | | SpSubview<eT>::operator*=(const Base<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const Proxy<T1> P(x.get_ref()); | | const Proxy<T1> P(x.get_ref()); | |
| | | | |
| // Must be exactly the same size for this (we can't modify our own size). | | // Must be exactly the same size for this (we can't modify our own size). | |
|
| arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "matrix multiplication into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "matrix multiplication"); | |
| | | | |
| SpMat<eT> tmp(*this); | | SpMat<eT> tmp(*this); | |
| Mat<eT> other_tmp(x.get_ref()); | | Mat<eT> other_tmp(x.get_ref()); | |
| tmp *= other_tmp; | | tmp *= other_tmp; | |
| operator=(tmp); | | operator=(tmp); | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator%=(const Base<eT, T1>& x) | | SpSubview<eT>::operator%=(const Base<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const Proxy<T1> P(x.get_ref()); | | const Proxy<T1> P(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "element-wise multiplication into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "element-wise multiplication"); | |
| | | | |
| const uword old_n_nonzero = m.n_nonzero; | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
|
| for(typename SpMat<eT>::iterator it(access::rw(m), aux_row1, aux_col1); i
t.col() < (aux_col1 + n_cols); ++it) | | for(iterator it(*this); it.pos() < n_nonzero; ++it) | |
| { | | { | |
|
| if((it.row() >= aux_row1) && (it.row() < (aux_row1 + n_rows))) | | (*it) *= P.at(it.row(), it.col()); | |
| | | if(P.at(it.row(), it.col()) == 0) | |
| { | | { | |
|
| (*it) *= P.at(it.row() - aux_row1, it.col() - aux_col1); | | it.internal_pos--; | |
| } | | } | |
| } | | } | |
| | | | |
| const uword new_n_nonzero = m.n_nonzero; | | const uword new_n_nonzero = m.n_nonzero; | |
| | | | |
| access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | | access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator/=(const Base<eT, T1>& x) | | SpSubview<eT>::operator/=(const Base<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const Proxy<T1> P(x.get_ref()); | | const Proxy<T1> P(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "element-wise division into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, P.get_n_rows(), P.get_n_cols(
), "element-wise division"); | |
| | | | |
| const uword old_n_nonzero = m.n_nonzero; | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
| for(uword c = 0; c < n_cols; ++c) | | for(uword c = 0; c < n_cols; ++c) | |
| { | | { | |
| for(uword r = 0; r < n_rows; ++r) | | for(uword r = 0; r < n_rows; ++r) | |
| { | | { | |
| at(r, c) /= P.at(r, c); | | at(r, c) /= P.at(r, c); | |
| } | | } | |
| } | | } | |
| | | | |
| skipping to change at line 389 | | skipping to change at line 388 | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator=(const SpSubview<eT>& x) | | SpSubview<eT>::operator=(const SpSubview<eT>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| arma_debug_assert_same_size(n_rows, n_cols, x.n_rows, x.n_cols, "insertio
n into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, x.n_rows, x.n_cols, "insertio
n into sparse submatrix"); | |
| | | | |
|
| const_iterator cit = x.begin(); | | const bool alias = ( &m == &(x.m) ); | |
| iterator it = begin(); | | | |
| | | | |
|
| while((cit.pos() < x.n_nonzero) || (it.pos() < n_nonzero)) | | if(alias == false) | |
| { | | { | |
|
| if((cit.row() == it.row()) && (cit.col() == it.col())) | | const_iterator cit = x.begin(); | |
| { | | iterator it = begin(); | |
| (*it) = (*cit); | | | |
| ++it; | | while((cit.pos() < x.n_nonzero) || (it.pos() < n_nonzero)) | |
| ++cit; | | | |
| } | | | |
| else | | | |
| { | | { | |
|
| if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() >
it.row()))) | | if((cit.row() == it.row()) && (cit.col() == it.col())) | |
| { | | { | |
|
| // cit is "ahead" | | (*it) = (*cit); | |
| (*it) = eT(0); // erase element | | | |
| it.internal_pos--; // update iterator so it still works | | | |
| ++it; | | ++it; | |
|
| | | ++cit; | |
| } | | } | |
| else | | else | |
| { | | { | |
|
| // it is "ahead" | | if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() | |
| at(cit.row(), cit.col()) = (*cit); | | > it.row()))) | |
| it.internal_pos++; // update iterator so it still works | | { | |
| ++cit; | | // cit is "ahead" | |
| | | (*it) = eT(0); // erase element | |
| | | it.internal_pos--; // update iterator so it still works | |
| | | ++it; | |
| | | } | |
| | | else | |
| | | { | |
| | | // it is "ahead" | |
| | | at(cit.row(), cit.col()) = (*cit); | |
| | | it.internal_pos++; // update iterator so it still works | |
| | | ++cit; | |
| | | } | |
| } | | } | |
| } | | } | |
|
| | | | |
| | | access::rw(n_nonzero) = x.n_nonzero; | |
| } | | } | |
|
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(x); | |
| | | | |
|
| access::rw(n_nonzero) = x.n_nonzero; | | (*this).operator=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const SpProxy<T1> p(x.get_ref()); | | const SpProxy<T1> p(x.get_ref()); | |
| | | | |
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "insertion into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "insertion into sparse submatrix"); | |
| | | | |
|
| typename SpProxy<T1>::const_iterator_type cit = p.begin(); | | if(p.is_alias(m) == false) | |
| iterator it(*this); | | | |
| | | | |
| while((cit.pos() < p.get_n_nonzero()) || (it.pos() < n_nonzero)) | | | |
| { | | { | |
|
| if(cit == it) // at the same location | | typename SpProxy<T1>::const_iterator_type cit = p.begin(); | |
| { | | iterator it(*this); | |
| (*it) = (*cit); | | | |
| ++it; | | while((cit.pos() < p.get_n_nonzero()) || (it.pos() < n_nonzero)) | |
| ++cit; | | | |
| } | | | |
| else | | | |
| { | | { | |
|
| if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() >
it.row()))) | | if(cit == it) // at the same location | |
| { | | { | |
|
| // cit is "ahead" | | (*it) = (*cit); | |
| (*it) = eT(0); // erase element | | | |
| it.internal_pos--; // update iterator so it still works | | | |
| ++it; | | ++it; | |
|
| | | ++cit; | |
| } | | } | |
| else | | else | |
| { | | { | |
|
| // it is "ahead" | | if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() | |
| at(cit.row(), cit.col()) = (*cit); | | > it.row()))) | |
| it.internal_pos++; // update iterator so it still works | | { | |
| ++cit; | | // cit is "ahead" | |
| | | (*it) = eT(0); // erase element | |
| | | it.internal_pos--; // update iterator so it still works | |
| | | ++it; | |
| | | } | |
| | | else | |
| | | { | |
| | | // it is "ahead" | |
| | | at(cit.row(), cit.col()) = (*cit); | |
| | | it.internal_pos++; // update iterator so it still works | |
| | | ++cit; | |
| | | } | |
| } | | } | |
| } | | } | |
|
| | | | |
| | | access::rw(n_nonzero) = p.get_n_nonzero(); | |
| } | | } | |
|
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(p.Q); | |
| | | | |
|
| access::rw(n_nonzero) = p.get_n_nonzero(); | | (*this).operator=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator+=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator+=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const SpProxy<T1> p(x.get_ref()); | | const SpProxy<T1> p(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( | |
| ), "addition into sparse submatrix"); | | ), "addition"); | |
| | | | |
| typename SpProxy<T1>::const_iterator_type cit = p.begin(); | | | |
| iterator it(*this); | | | |
| | | | |
| const uword old_n_nonzero = p.get_n_nonzero(); | | | |
| | | | |
|
| while(it.pos() < n_nonzero) | | if(p.is_alias(m) == false) | |
| { | | { | |
|
| if(it == cit) | | typename SpProxy<T1>::const_iterator_type cit = p.begin(); | |
| | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
| | | while(cit.pos() < p.get_n_nonzero()) | |
| { | | { | |
|
| const eT val = (*it) + (*cit); | | at(cit.row(), cit.col()) += (*cit); | |
| (*it) = val; | | | |
| if(val == 0) | | | |
| { | | | |
| it.internal_pos--; // to keep it valid | | | |
| } | | | |
| ++it; | | | |
| ++cit; | | ++cit; | |
| } | | } | |
|
| else | | | |
| { | | | |
| if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() > | | | |
| it.row()))) | | | |
| { | | | |
| // cit is "ahead"; we don't need to do anything | | | |
| ++it; | | | |
| } | | | |
| else | | | |
| { | | | |
| // it is "ahead"; we need to insert a new value | | | |
| at(cit.row(), cit.col()) = (*cit); | | | |
| it.internal_pos++; // update iterator so it still works | | | |
| ++cit; | | | |
| } | | | |
| } | | | |
| } | | | |
| | | | |
|
| const uword new_n_nonzero = p.get_n_nonzero(); | | const uword new_n_nonzero = m.n_nonzero; | |
| | | | |
|
| access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | | access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | |
| | | } | |
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(p.Q); | |
| | | | |
| | | (*this).operator+=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator-=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator-=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const SpProxy<T1> p(x.get_ref()); | | const SpProxy<T1> p(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "subtraction into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "subtraction"); | |
| | | | |
|
| typename SpProxy<T1>::const_iterator_type cit = p.begin(); | | if(p.is_alias(m) == false) | |
| iterator it(*this); | | { | |
| | | typename SpProxy<T1>::const_iterator_type cit = p.begin(); | |
| | | | |
|
| const uword old_n_nonzero = p.get_n_nonzero(); | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
|
| while(it.pos() < n_nonzero) | | while(cit.pos() < p.get_n_nonzero()) | |
| { | | | |
| if(cit == it) | | | |
| { | | { | |
|
| const eT val = (*it) - (*cit); | | at(cit.row(), cit.col()) -= (*cit); | |
| (*it) = val; | | | |
| if(val == 0) | | | |
| { | | | |
| it.internal_pos--; // to keep it valid | | | |
| } | | | |
| ++it; | | | |
| ++cit; | | ++cit; | |
| } | | } | |
|
| else | | | |
| { | | | |
| if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() > | | | |
| it.row()))) | | | |
| { | | | |
| // cit is "ahead"; we don't need to do anything | | | |
| ++it; | | | |
| } | | | |
| else | | | |
| { | | | |
| // it is "ahead"; we need to insert a new value | | | |
| at(cit.row(), cit.col()) = -(*cit); | | | |
| it.internal_pos++; // update iterator so it still works | | | |
| ++cit; | | | |
| } | | | |
| } | | | |
| } | | | |
| | | | |
|
| const uword new_n_nonzero = p.get_n_nonzero(); | | const uword new_n_nonzero = m.n_nonzero; | |
| | | | |
|
| access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | | access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | |
| | | } | |
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(p.Q); | |
| | | | |
| | | (*this).operator-=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator*=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator*=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const SpProxy<T1> p(x.get_ref()); | | const SpProxy<T1> p(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "matrix multiplication into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "matrix multiplication"); | |
| | | | |
| // Because we have to use a temporary anyway, it doesn't make sense to | | // Because we have to use a temporary anyway, it doesn't make sense to | |
| // reimplement this here. | | // reimplement this here. | |
| return operator=((*this) * x.get_ref()); | | return operator=((*this) * x.get_ref()); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator%=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator%=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const SpProxy<T1> p(x.get_ref()); | | const SpProxy<T1> p(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "element-wise multiplication into sparse submatrix"); | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols(
), "element-wise multiplication"); | |
| | | | |
|
| typename SpProxy<T1>::const_iterator_type cit = p.begin(); | | if(p.is_alias(m) == false) | |
| iterator it(*this); | | { | |
| | | typename SpProxy<T1>::const_iterator_type cit = p.begin(); | |
| | | iterator it(*this); | |
| | | | |
|
| const uword old_n_nonzero = p.get_n_nonzero(); | | const uword old_n_nonzero = m.n_nonzero; | |
| | | | |
|
| while(it.pos() < n_nonzero) | | while((it.pos() < n_nonzero) || (cit.pos() < p.get_n_nonzero())) | |
| { | | | |
| if(it == cit) | | | |
| { | | { | |
|
| (*it) *= (*cit); | | if((cit.row() == it.row()) && (cit.col() == it.col())) | |
| ++it; | | | |
| ++cit; | | | |
| } | | | |
| else | | | |
| { | | | |
| if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() > | | | |
| it.row()))) | | | |
| { | | { | |
|
| // cit is "ahead"; this value becomes zero. | | (*it) *= (*cit); | |
| (*it) = eT(0); | | | |
| it.internal_pos--; // keep it accurate | | | |
| ++it; | | ++it; | |
|
| | | ++cit; | |
| } | | } | |
| else | | else | |
| { | | { | |
|
| // it is "ahead"; we just need to catch cit up | | if((cit.col() > it.col()) || ((cit.col() == it.col()) && (cit.row() | |
| ++cit; | | > it.row()))) | |
| | | { | |
| | | // cit is "ahead" | |
| | | (*it) = eT(0); // erase element -- x has a zero here | |
| | | it.internal_pos--; // update iterator so it still works | |
| | | ++it; | |
| | | } | |
| | | else | |
| | | { | |
| | | // it is "ahead" | |
| | | ++cit; | |
| | | } | |
| } | | } | |
| } | | } | |
|
| } | | | |
| | | | |
|
| const uword new_n_nonzero = p.get_n_nonzero(); | | const uword new_n_nonzero = m.n_nonzero; | |
| | | | |
|
| access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | | access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | |
| | | } | |
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(p.Q); | |
| | | | |
| | | (*this).operator%=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
|
| | | //! If you are using this function, you are probably misguided. | |
| template<typename eT> | | template<typename eT> | |
| template<typename T1> | | template<typename T1> | |
| inline | | inline | |
| const SpSubview<eT>& | | const SpSubview<eT>& | |
| SpSubview<eT>::operator/=(const SpBase<eT, T1>& x) | | SpSubview<eT>::operator/=(const SpBase<eT, T1>& x) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| SpProxy<T1> p(x.get_ref()); | | SpProxy<T1> p(x.get_ref()); | |
| | | | |
|
| arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( | | arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( | |
| ), "element-wise division into sparse submatrix"); | | ), "element-wise division"); | |
| | | | |
| const uword old_n_nonzero = p.get_n_nonzero(); | | | |
| | | | |
|
| // If you are using this function, you are probably misguided. | | if(p.is_alias(m) == false) | |
| for(uword i = 0; i < n_elem; ++i) | | | |
| { | | { | |
|
| at(i) /= p[i]; | | const uword old_n_nonzero = p.get_n_nonzero(); | |
| } | | | |
| | | | |
|
| const uword new_n_nonzero = p.get_n_nonzero(); | | for(uword col = 0; col < n_cols; ++col) | |
| | | for(uword row = 0; row < n_rows; ++row) | |
| | | { | |
| | | at(row,col) /= p.at(row,col); | |
| | | } | |
| | | | |
|
| access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | | const uword new_n_nonzero = p.get_n_nonzero(); | |
| | | | |
| | | access::rw(n_nonzero) += (new_n_nonzero - old_n_nonzero); | |
| | | } | |
| | | else | |
| | | { | |
| | | const SpMat<eT> tmp(p.Q); | |
| | | | |
| | | (*this).operator/=(tmp); | |
| | | } | |
| | | | |
| return *this; | | return *this; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| void | | void | |
| SpSubview<eT>::fill(const eT val) | | SpSubview<eT>::fill(const eT val) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| (*this).operator=(val); | | if(val != eT(0)) | |
| | | { | |
| | | // TODO: implement a faster version; the code below is slow | |
| | | | |
| | | const uword start_row = aux_row1; | |
| | | const uword end_row = aux_row1 + n_rows; | |
| | | const uword start_col = aux_col1; | |
| | | const uword end_col = aux_col1 + n_cols; | |
| | | | |
| | | // iterate over our part of the sparse matrix | |
| | | for(uword col = start_col; col < end_col; ++col) | |
| | | for(uword row = start_row; row < end_row; ++row) | |
| | | { | |
| | | access::rw(m).at(row, col) = val; | |
| | | } | |
| | | | |
| | | access::rw(n_nonzero) = n_elem; | |
| | | } | |
| | | else | |
| | | { | |
| | | (*this).zeros(); | |
| | | } | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| void | | void | |
| SpSubview<eT>::zeros() | | SpSubview<eT>::zeros() | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| // we can be a little smarter here | | // we can be a little smarter here | |
|
| for(typename SpMat<eT>::iterator it(access::rw(m), aux_row1, aux_col1); | | iterator it(*this); | |
| (it.col < (aux_col1 + n_cols - 1)) || (it.col == (aux_col1 + n_cols - | | | |
| 1) && it.row < (aux_row1 + n_rows)); | | while(it.pos() < n_nonzero) | |
| ++it) | | | |
| { | | { | |
|
| // column will always be valid; no need to check that | | (*it) = 0; | |
| if((it.row >= aux_row1) && (it.row < (aux_row1 + n_rows))) | | it.internal_pos--; // hack to update iterator without requiring a new o | |
| { | | ne | |
| (*it) = 0; | | ++it; | |
| } | | | |
| } | | } | |
| | | | |
| access::rw(n_nonzero) = 0; | | access::rw(n_nonzero) = 0; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| void | | void | |
| SpSubview<eT>::ones() | | SpSubview<eT>::ones() | |
| { | | { | |
| | | | |
| skipping to change at line 736 | | skipping to change at line 762 | |
| | | | |
| for(uword ind = 0; ind < end_index; ++ind) | | for(uword ind = 0; ind < end_index; ++ind) | |
| { | | { | |
| m.at(ind + aux_row1, ind + aux_col1) = eT(1); | | m.at(ind + aux_row1, ind + aux_col1) = eT(1); | |
| } | | } | |
| | | | |
| access::rw(n_nonzero) = end_index; | | access::rw(n_nonzero) = end_index; | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| SpValProxy<SpSubview<eT> > | | SpValProxy<SpSubview<eT> > | |
| SpSubview<eT>::operator[](const uword i) | | SpSubview<eT>::operator[](const uword i) | |
| { | | { | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_rows; | | const uword col = i / n_rows; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| eT | | eT | |
| SpSubview<eT>::operator[](const uword i) const | | SpSubview<eT>::operator[](const uword i) const | |
| { | | { | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_rows; | | const uword col = i / n_rows; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
|
| SpValProxy<SpSubview<eT> > | | SpValProxy< SpSubview<eT> > | |
| SpSubview<eT>::operator()(const uword i) | | SpSubview<eT>::operator()(const uword i) | |
| { | | { | |
| arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bou
nds"); | | arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bou
nds"); | |
| | | | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_cols; | | const uword col = i / n_cols; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| eT | | eT | |
| SpSubview<eT>::operator()(const uword i) const | | SpSubview<eT>::operator()(const uword i) const | |
| { | | { | |
| arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bou
nds"); | | arma_debug_check( (i >= n_elem), "SpSubview::operator(): index out of bou
nds"); | |
| | | | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_cols; | | const uword col = i / n_cols; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
|
| SpValProxy<SpSubview<eT> > | | SpValProxy< SpSubview<eT> > | |
| SpSubview<eT>::operator()(const uword in_row, const uword in_col) | | SpSubview<eT>::operator()(const uword in_row, const uword in_col) | |
| { | | { | |
| arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::o
perator(): index out of bounds"); | | arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::o
perator(): index out of bounds"); | |
| | | | |
| return (*this).at(in_row, in_col); | | return (*this).at(in_row, in_col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| eT | | eT | |
| SpSubview<eT>::operator()(const uword in_row, const uword in_col) const | | SpSubview<eT>::operator()(const uword in_row, const uword in_col) const | |
| { | | { | |
| arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::o
perator(): index out of bounds"); | | arma_debug_check( (in_row >= n_rows) || (in_col >= n_cols), "SpSubview::o
perator(): index out of bounds"); | |
| | | | |
| return (*this).at(in_row, in_col); | | return (*this).at(in_row, in_col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
|
| SpValProxy<SpSubview<eT> > | | SpValProxy< SpSubview<eT> > | |
| SpSubview<eT>::at(const uword i) | | SpSubview<eT>::at(const uword i) | |
| { | | { | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_cols; | | const uword col = i / n_cols; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| eT | | eT | |
| SpSubview<eT>::at(const uword i) const | | SpSubview<eT>::at(const uword i) const | |
| { | | { | |
| const uword row = i % n_rows; | | const uword row = i % n_rows; | |
| const uword col = i / n_cols; | | const uword col = i / n_cols; | |
| | | | |
| return (*this).at(row, col); | | return (*this).at(row, col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
|
| SpValProxy<SpSubview<eT> > | | SpValProxy< SpSubview<eT> > | |
| SpSubview<eT>::at(const uword in_row, const uword in_col) | | SpSubview<eT>::at(const uword in_row, const uword in_col) | |
| { | | { | |
| const uword colptr = m.col_ptrs[in_col + aux_col1]; | | const uword colptr = m.col_ptrs[in_col + aux_col1]; | |
| const uword next_colptr = m.col_ptrs[in_col + aux_col1 + 1]; | | const uword next_colptr = m.col_ptrs[in_col + aux_col1 + 1]; | |
| | | | |
| // Step through the row indices to see if our element exists. | | // Step through the row indices to see if our element exists. | |
| for(uword i = colptr; i < next_colptr; ++i) | | for(uword i = colptr; i < next_colptr; ++i) | |
| { | | { | |
| // First check that we have not stepped past it. | | // First check that we have not stepped past it. | |
| if((in_row + aux_row1) < m.row_indices[i]) | | if((in_row + aux_row1) < m.row_indices[i]) | |
| | | | |
| skipping to change at line 854 | | skipping to change at line 889 | |
| { | | { | |
| return SpValProxy<SpSubview<eT> >(in_row, in_col, *this, &access::rw(
m.values[i])); | | return SpValProxy<SpSubview<eT> >(in_row, in_col, *this, &access::rw(
m.values[i])); | |
| } | | } | |
| } | | } | |
| | | | |
| // We did not find it, so it does not exist. | | // We did not find it, so it does not exist. | |
| return SpValProxy<SpSubview<eT> >(in_row, in_col, *this); | | return SpValProxy<SpSubview<eT> >(in_row, in_col, *this); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
|
| | | arma_hot | |
| inline | | inline | |
| eT | | eT | |
| SpSubview<eT>::at(const uword in_row, const uword in_col) const | | SpSubview<eT>::at(const uword in_row, const uword in_col) const | |
| { | | { | |
| return m.at(aux_row1 + in_row, aux_col1 + in_col); | | return m.at(aux_row1 + in_row, aux_col1 + in_col); | |
| } | | } | |
| | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
| | | | |
End of changes. 79 change blocks. |
| 168 lines changed or deleted | | 204 lines changed or added | |
|
| diskio_bones.hpp | | diskio_bones.hpp | |
| | | | |
| skipping to change at line 89 | | skipping to change at line 89 | |
| template<typename eT> inline static bool load_pgm_binary (Mat<eT>&
x, std::istream& is, std::string& err_msg); | | template<typename eT> inline static bool load_pgm_binary (Mat<eT>&
x, std::istream& is, std::string& err_msg); | |
| template<typename T> inline static bool load_pgm_binary (Mat< std::compl
ex<T> >& x, std::istream& is, std::string& err_msg); | | template<typename T> inline static bool load_pgm_binary (Mat< std::compl
ex<T> >& x, std::istream& is, std::string& err_msg); | |
| template<typename eT> inline static bool load_auto_detect(Mat<eT>&
x, std::istream& f, std::string& err_msg); | | template<typename eT> inline static bool load_auto_detect(Mat<eT>&
x, std::istream& f, std::string& err_msg); | |
| | | | |
| inline static void pnm_skip_comments(std::istream& f); | | inline static void pnm_skip_comments(std::istream& f); | |
| | | | |
| // | | // | |
| // sparse saving | | // sparse saving | |
| | | | |
| template<typename eT> inline static bool save_raw_ascii (const SpMat<eT>
& x, const std::string& final_name); | | template<typename eT> inline static bool save_raw_ascii (const SpMat<eT>
& x, const std::string& final_name); | |
|
| template<typename eT> inline static bool save_raw_binary (const SpMat<eT> | | //template<typename eT> inline static bool save_raw_binary (const SpMat<e | |
| & x, const std::string& final_name); | | T>& x, const std::string& final_name); | |
| template<typename eT> inline static bool save_arma_ascii (const SpMat<eT> | | //template<typename eT> inline static bool save_arma_ascii (const SpMat<e | |
| & x, const std::string& final_name); | | T>& x, const std::string& final_name); | |
| template<typename eT> inline static bool save_csv_ascii (const SpMat<eT> | | //template<typename eT> inline static bool save_csv_ascii (const SpMat<e | |
| & x, const std::string& final_name); | | T>& x, const std::string& final_name); | |
| template<typename eT> inline static bool save_arma_binary(const SpMat<eT>
& x, const std::string& final_name); | | template<typename eT> inline static bool save_arma_binary(const SpMat<eT>
& x, const std::string& final_name); | |
|
| template<typename eT> inline static bool save_pgm_binary (const SpMat<eT> | | //template<typename eT> inline static bool save_pgm_binary (const SpMat<e | |
| & x, const std::string& final_name); | | T>& x, const std::string& final_name); | |
| template<typename T> inline static bool save_pgm_binary (const SpMat<std | | //template<typename T> inline static bool save_pgm_binary (const SpMat<s | |
| ::complex<T> >& x, const std::string& final_name); | | td::complex<T> >& x, const std::string& final_name); | |
| template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>
& x, const std::string& final_name); | | template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>
& x, const std::string& final_name); | |
| | | | |
| template<typename eT> inline static bool save_raw_ascii (const SpMat<eT>
& x, std::ostream& f); | | template<typename eT> inline static bool save_raw_ascii (const SpMat<eT>
& x, std::ostream& f); | |
|
| template<typename eT> inline static bool save_raw_binary (const SpMat<eT> | | //template<typename eT> inline static bool save_raw_binary (const SpMat<e | |
| & x, std::ostream& f); | | T>& x, std::ostream& f); | |
| template<typename eT> inline static bool save_arma_ascii (const SpMat<eT> | | //template<typename eT> inline static bool save_arma_ascii (const SpMat<e | |
| & x, std::ostream& f); | | T>& x, std::ostream& f); | |
| template<typename eT> inline static bool save_csv_ascii (const SpMat<eT> | | //template<typename eT> inline static bool save_csv_ascii (const SpMat<e | |
| & x, std::ostream& f); | | T>& x, std::ostream& f); | |
| template<typename eT> inline static bool save_arma_binary(const SpMat<eT>
& x, std::ostream& f); | | template<typename eT> inline static bool save_arma_binary(const SpMat<eT>
& x, std::ostream& f); | |
|
| template<typename eT> inline static bool save_pgm_binary (const SpMat<eT> | | //template<typename eT> inline static bool save_pgm_binary (const SpMat<e | |
| & x, std::ostream& f); | | T>& x, std::ostream& f); | |
| template<typename T> inline static bool save_pgm_binary (const SpMat<std | | //template<typename T> inline static bool save_pgm_binary (const SpMat<s | |
| ::complex<T> >& x, std::ostream& f); | | td::complex<T> >& x, std::ostream& f); | |
| template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>
& x, std::ostream& f); | | template<typename eT> inline static bool save_coord_ascii(const SpMat<eT>
& x, std::ostream& f); | |
| | | | |
| // | | // | |
| // sparse loading | | // sparse loading | |
| | | | |
| template<typename eT> inline static bool load_raw_ascii (SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | | template<typename eT> inline static bool load_raw_ascii (SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | |
|
| template<typename eT> inline static bool load_raw_binary (SpMat<eT>& x, c | | //template<typename eT> inline static bool load_raw_binary (SpMat<eT>& x, | |
| onst std::string& name, std::string& err_msg); | | const std::string& name, std::string& err_msg); | |
| template<typename eT> inline static bool load_arma_ascii (SpMat<eT>& x, c | | //template<typename eT> inline static bool load_arma_ascii (SpMat<eT>& x, | |
| onst std::string& name, std::string& err_msg); | | const std::string& name, std::string& err_msg); | |
| template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, c | | //template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, | |
| onst std::string& name, std::string& err_msg); | | const std::string& name, std::string& err_msg); | |
| template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | | template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | |
|
| template<typename eT> inline static bool load_pgm_binary (SpMat<eT>& x, c | | //template<typename eT> inline static bool load_pgm_binary (SpMat<eT>& x, | |
| onst std::string& name, std::string& err_msg); | | const std::string& name, std::string& err_msg); | |
| template<typename T> inline static bool load_pgm_binary (SpMat<std::comp | | //template<typename T> inline static bool load_pgm_binary (SpMat<std::co | |
| lex<T> >& x, const std::string& name, std::string& err_msg); | | mplex<T> >& x, const std::string& name, std::string& err_msg); | |
| template<typename eT> inline static bool load_auto_detect(SpMat<eT>& x, c | | //template<typename eT> inline static bool load_auto_detect(SpMat<eT>& x, | |
| onst std::string& name, std::string& err_msg); | | const std::string& name, std::string& err_msg); | |
| template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | | template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, c
onst std::string& name, std::string& err_msg); | |
| | | | |
| template<typename eT> inline static bool load_raw_ascii (SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | | template<typename eT> inline static bool load_raw_ascii (SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | |
|
| template<typename eT> inline static bool load_raw_binary (SpMat<eT>& x, s | | //template<typename eT> inline static bool load_raw_binary (SpMat<eT>& x, | |
| td::istream& f, std::string& err_msg); | | std::istream& f, std::string& err_msg); | |
| template<typename eT> inline static bool load_arma_ascii (SpMat<eT>& x, s | | //template<typename eT> inline static bool load_arma_ascii (SpMat<eT>& x, | |
| td::istream& f, std::string& err_msg); | | std::istream& f, std::string& err_msg); | |
| template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, s | | //template<typename eT> inline static bool load_csv_ascii (SpMat<eT>& x, | |
| td::istream& f, std::string& err_msg); | | std::istream& f, std::string& err_msg); | |
| template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | | template<typename eT> inline static bool load_arma_binary(SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | |
|
| template<typename eT> inline static bool load_pgm_binary (SpMat<eT>& x, s | | //template<typename eT> inline static bool load_pgm_binary (SpMat<eT>& x, | |
| td::istream& is, std::string& err_msg); | | std::istream& is, std::string& err_msg); | |
| template<typename T> inline static bool load_pgm_binary (SpMat<std::comp | | //template<typename T> inline static bool load_pgm_binary (SpMat<std::co | |
| lex<T> >& x, std::istream& is, std::string& err_msg); | | mplex<T> >& x, std::istream& is, std::string& err_msg); | |
| template<typename eT> inline static bool load_auto_detect(SpMat<eT>& x, s | | //template<typename eT> inline static bool load_auto_detect(SpMat<eT>& x, | |
| td::istream& f, std::string& err_msg); | | std::istream& f, std::string& err_msg); | |
| template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | | template<typename eT> inline static bool load_coord_ascii(SpMat<eT>& x, s
td::istream& f, std::string& err_msg); | |
| | | | |
| // | | // | |
| // cube saving | | // cube saving | |
| | | | |
| template<typename eT> inline static bool save_raw_ascii (const Cube<eT>&
x, const std::string& name); | | template<typename eT> inline static bool save_raw_ascii (const Cube<eT>&
x, const std::string& name); | |
| template<typename eT> inline static bool save_raw_binary (const Cube<eT>&
x, const std::string& name); | | template<typename eT> inline static bool save_raw_binary (const Cube<eT>&
x, const std::string& name); | |
| template<typename eT> inline static bool save_arma_ascii (const Cube<eT>&
x, const std::string& name); | | template<typename eT> inline static bool save_arma_ascii (const Cube<eT>&
x, const std::string& name); | |
| template<typename eT> inline static bool save_arma_binary(const Cube<eT>&
x, const std::string& name); | | template<typename eT> inline static bool save_arma_binary(const Cube<eT>&
x, const std::string& name); | |
| | | | |
| | | | |
End of changes. 8 change blocks. |
| 44 lines changed or deleted | | 44 lines changed or added | |
|
| diskio_meat.hpp | | diskio_meat.hpp | |
| | | | |
| skipping to change at line 2074 | | skipping to change at line 2074 | |
| | | | |
| f << x.at(row,col); | | f << x.at(row,col); | |
| } | | } | |
| | | | |
| f.put('\n'); | | f.put('\n'); | |
| } | | } | |
| | | | |
| return f.good(); | | return f.good(); | |
| } | | } | |
| | | | |
|
| //! Save a matrix as raw binary (no header) | | //// TODO: this function doesn't make sense for sparse matrices, as there i | |
| | | s no universal definition of a "raw" CSC file | |
| | | // //! Save a matrix as raw binary (no header) | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_raw_binary(const SpMat<eT>& x, const std::string& final_nam | |
| | | e) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | // | |
| | | // std::ofstream f(tmp_name.c_str(), std::fstream::binary); | |
| | | // | |
| | | // bool save_okay = f.is_open(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::save_raw_binary(x, f); | |
| | | // | |
| | | // f.flush(); | |
| | | // f.close(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::safe_rename(tmp_name, final_name); | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | //// TODO: this function doesn't make sense for sparse matrices, as there i | |
| | | s no universal definition of a "raw" CSC file | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_raw_binary(const SpMat<eT>& x, std::ostream& f) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // //Write the dims of the matrix first | |
| | | // f.write( reinterpret_cast<const char*>(&x.n_rows), std::streamsize(siz | |
| | | eof(uword)) ); | |
| | | // f.write( reinterpret_cast<const char*>(&x.n_cols), std::streamsize(siz | |
| | | eof(uword)) ); | |
| | | // f.write( reinterpret_cast<const char*>(&x.n_nonzero), std::streamsize( | |
| | | sizeof(uword)) ); | |
| | | // | |
| | | // //Now write the data for the three vectors | |
| | | // f.write( reinterpret_cast<const char*>(x.values), std::streamsize(x.n_ | |
| | | nonzero*sizeof(eT)) ); | |
| | | // f.write( reinterpret_cast<const char*>(x.row_indices), std::streamsize | |
| | | (x.n_nonzero*sizeof(uword)) ); | |
| | | // f.write( reinterpret_cast<const char*>(x.col_ptrs), std::streamsize((x | |
| | | .n_cols+1)*sizeof(uword)) ); | |
| | | // | |
| | | // return f.good(); | |
| | | // } | |
| | | | |
| | | //// TODO: this function needs to be reworked to save only non-zero element | |
| | | s; we can define our own format, or adapt an existing format | |
| | | // //! Save a matrix in text format (human readable), | |
| | | // //! with a header that indicates the matrix type as well as its dimensio | |
| | | ns | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_arma_ascii(const SpMat<eT>& x, const std::string& final_nam | |
| | | e) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | // | |
| | | // std::ofstream f(tmp_name.c_str()); | |
| | | // | |
| | | // bool save_okay = f.is_open(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::save_arma_ascii(x, f); | |
| | | // | |
| | | // f.flush(); | |
| | | // f.close(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::safe_rename(tmp_name, final_name); | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | //// TODO: this function needs to be reworked to save only non-zero element | |
| | | s; we can define our own format, or adapt an existing format | |
| | | // //! Save a matrix in text format (human readable), | |
| | | // //! with a header that indicates the matrix type as well as its dimensio | |
| | | ns | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_arma_ascii(const SpMat<eT>& x, std::ostream& f) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const ios::fmtflags orig_flags = f.flags(); | |
| | | // | |
| | | // f << diskio::gen_txt_header(x) << '\n'; | |
| | | // f << x.n_rows << ' ' << x.n_cols << '\n'; | |
| | | // | |
| | | // uword cell_width; | |
| | | // | |
| | | // // TODO: need sane values for complex numbers | |
| | | // | |
| | | // if( (is_float<eT>::value == true) || (is_double<eT>::value == true) ) | |
| | | // { | |
| | | // f.setf(ios::scientific); | |
| | | // f.precision(10); | |
| | | // cell_width = 18; | |
| | | // } | |
| | | // | |
| | | // for(uword row=0; row < x.n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < x.n_cols; ++col) | |
| | | // { | |
| | | // f.put(' '); | |
| | | // | |
| | | // if( (is_float<eT>::value == true) || (is_double<eT>::value == true | |
| | | ) ) | |
| | | // { | |
| | | // f.width(cell_width); | |
| | | // } | |
| | | // | |
| | | // f << x.at(row,col); | |
| | | // } | |
| | | // | |
| | | // f.put('\n'); | |
| | | // } | |
| | | // | |
| | | // const bool save_okay = f.good(); | |
| | | // | |
| | | // f.flags(orig_flags); | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | //// TODO: this function needs to be reworked to save only non-zero element | |
| | | s | |
| | | // //! Save a matrix in CSV text format (human readable) | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_csv_ascii(const SpMat<eT>& x, const std::string& final_name | |
| | | ) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | // | |
| | | // std::ofstream f(tmp_name.c_str()); | |
| | | // | |
| | | // bool save_okay = f.is_open(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::save_csv_ascii(x, f); | |
| | | // | |
| | | // f.flush(); | |
| | | // f.close(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::safe_rename(tmp_name, final_name); | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | //// TODO: this function needs to be reworked to save only non-zero element | |
| | | s | |
| | | // //! Save a matrix in CSV text format (human readable) | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const ios::fmtflags orig_flags = f.flags(); | |
| | | // | |
| | | // // TODO: need sane values for complex numbers | |
| | | // | |
| | | // if( (is_float<eT>::value == true) || (is_double<eT>::value == true) ) | |
| | | // { | |
| | | // f.setf(ios::scientific); | |
| | | // f.precision(10); | |
| | | // } | |
| | | // | |
| | | // uword x_n_rows = x.n_rows; | |
| | | // uword x_n_cols = x.n_cols; | |
| | | // | |
| | | // for(uword row=0; row < x_n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < x_n_cols; ++col) | |
| | | // { | |
| | | // f << x.at(row,col); | |
| | | // | |
| | | // if( col < (x_n_cols-1) ) | |
| | | // { | |
| | | // f.put(','); | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // f.put('\n'); | |
| | | // } | |
| | | // | |
| | | // const bool save_okay = f.good(); | |
| | | // | |
| | | // f.flags(orig_flags); | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | //! Save a matrix in binary format, | |
| | | //! with a header that stores the matrix type as well as its dimensions | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_raw_binary(const SpMat<eT>& x, const std::string& final_name) | | diskio::save_arma_binary(const SpMat<eT>& x, const std::string& final_name) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | | |
| std::ofstream f(tmp_name.c_str(), std::fstream::binary); | | std::ofstream f(tmp_name.c_str(), std::fstream::binary); | |
| | | | |
| bool save_okay = f.is_open(); | | bool save_okay = f.is_open(); | |
| | | | |
| if(save_okay == true) | | if(save_okay == true) | |
| { | | { | |
|
| save_okay = diskio::save_raw_binary(x, f); | | save_okay = diskio::save_arma_binary(x, f); | |
| | | | |
| f.flush(); | | f.flush(); | |
| f.close(); | | f.close(); | |
| | | | |
| if(save_okay == true) | | if(save_okay == true) | |
| { | | { | |
| save_okay = diskio::safe_rename(tmp_name, final_name); | | save_okay = diskio::safe_rename(tmp_name, final_name); | |
| } | | } | |
| } | | } | |
| | | | |
| return save_okay; | | return save_okay; | |
| } | | } | |
| | | | |
|
| | | //! Save a matrix in binary format, | |
| | | //! with a header that stores the matrix type as well as its dimensions | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_raw_binary(const SpMat<eT>& x, std::ostream& f) | | diskio::save_arma_binary(const SpMat<eT>& x, std::ostream& f) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| //Write the dims of the matrix first | | //Write the dims of the matrix first | |
|
| f.write( reinterpret_cast<const char*>(&x.n_rows), std::streamsize(sizeof | | f << diskio::gen_bin_header(x) << '\n'; | |
| (uword)) ); | | f << x.n_rows << ' ' << x.n_cols << ' ' << x.n_nonzero << '\n'; | |
| f.write( reinterpret_cast<const char*>(&x.n_cols), std::streamsize(sizeof | | | |
| (uword)) ); | | | |
| f.write( reinterpret_cast<const char*>(&x.n_nonzero), std::streamsize(siz | | | |
| eof(uword)) ); | | | |
| | | | |
| //Now write the data for the three vectors | | //Now write the data for the three vectors | |
| f.write( reinterpret_cast<const char*>(x.values), std::streamsize(x.n_non
zero*sizeof(eT)) ); | | f.write( reinterpret_cast<const char*>(x.values), std::streamsize(x.n_non
zero*sizeof(eT)) ); | |
| f.write( reinterpret_cast<const char*>(x.row_indices), std::streamsize(x.
n_nonzero*sizeof(uword)) ); | | f.write( reinterpret_cast<const char*>(x.row_indices), std::streamsize(x.
n_nonzero*sizeof(uword)) ); | |
| f.write( reinterpret_cast<const char*>(x.col_ptrs), std::streamsize((x.n_
cols+1)*sizeof(uword)) ); | | f.write( reinterpret_cast<const char*>(x.col_ptrs), std::streamsize((x.n_
cols+1)*sizeof(uword)) ); | |
| | | | |
| return f.good(); | | return f.good(); | |
| } | | } | |
| | | | |
|
| //! Save a matrix in text format (human readable), | | // //! Save a matrix as a PGM greyscale image | |
| //! with a header that indicates the matrix type as well as its dimensions | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_pgm_binary(const SpMat<eT>& x, const std::string& final_nam | |
| | | e) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | // | |
| | | // std::fstream f(tmp_name.c_str(), std::fstream::out | std::fstream::bin | |
| | | ary); | |
| | | // | |
| | | // bool save_okay = f.is_open(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::save_pgm_binary(x, f); | |
| | | // | |
| | | // f.flush(); | |
| | | // f.close(); | |
| | | // | |
| | | // if(save_okay == true) | |
| | | // { | |
| | | // save_okay = diskio::safe_rename(tmp_name, final_name); | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // return save_okay; | |
| | | // } | |
| | | | |
| | | // //! Save a matrix as a PGM greyscale image | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_pgm_binary(const SpMat<eT>& x, std::ostream& f) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // f << "P5" << '\n'; | |
| | | // f << x.n_cols << ' ' << x.n_rows << ' ' << x.n_cols*x.n_rows << '\n'; | |
| | | // f << 255 << '\n'; | |
| | | // | |
| | | // const uword n_elem = x.n_nonzero; | |
| | | // podarray<u8> tmp(n_elem); | |
| | | // | |
| | | // uword i = 0; | |
| | | // | |
| | | // for(uword row=0; row < x.n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < x.n_cols; ++col) | |
| | | // { | |
| | | // tmp[i] = u8( x.at(row,col) ); // TODO: add round() ? | |
| | | // ++i; | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // f.write(reinterpret_cast<const char*>(tmp.mem), std::streamsize(x.n_ro | |
| | | ws*x.n_cols) ); | |
| | | // | |
| | | // return f.good(); | |
| | | // } | |
| | | | |
| | | // //! Save a matrix as a PGM greyscale image | |
| | | // template<typename T> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_pgm_binary(const SpMat< std::complex<T> >& x, const std::st | |
| | | ring& final_name) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // const uchar_mat tmp = conv_to<uchar_mat>::from(x); | |
| | | // | |
| | | // return diskio::save_pgm_binary(tmp, final_name); | |
| | | // } | |
| | | | |
| | | // //! Save a matrix as a PGM greyscale image | |
| | | // template<typename T> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::save_pgm_binary(const SpMat< std::complex<T> >& x, std::ostream& | |
| | | f) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // const uchar_mat tmp = conv_to<uchar_mat>::from(x); | |
| | | // | |
| | | // return diskio::save_pgm_binary(tmp, f); | |
| | | // } | |
| | | | |
| | | //! Save a matrix in ASCII coord format | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_arma_ascii(const SpMat<eT>& x, const std::string& final_name) | | diskio::save_coord_ascii(const SpMat<eT>& x, const std::string& final_name) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | const std::string tmp_name = diskio::gen_tmp_name(final_name); | |
| | | | |
| std::ofstream f(tmp_name.c_str()); | | std::ofstream f(tmp_name.c_str()); | |
| | | | |
| bool save_okay = f.is_open(); | | bool save_okay = f.is_open(); | |
| | | | |
| if(save_okay == true) | | if(save_okay == true) | |
| { | | { | |
|
| save_okay = diskio::save_arma_ascii(x, f); | | save_okay = diskio::save_coord_ascii(x, f); | |
| | | | |
| f.flush(); | | f.flush(); | |
| f.close(); | | f.close(); | |
| | | | |
| if(save_okay == true) | | if(save_okay == true) | |
| { | | { | |
| save_okay = diskio::safe_rename(tmp_name, final_name); | | save_okay = diskio::safe_rename(tmp_name, final_name); | |
| } | | } | |
| } | | } | |
| | | | |
| return save_okay; | | return save_okay; | |
| } | | } | |
| | | | |
|
| //! Save a matrix in text format (human readable), | | //! Save a matrix in ASCII coord format | |
| //! with a header that indicates the matrix type as well as its dimensions | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_arma_ascii(const SpMat<eT>& x, std::ostream& f) | | diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const ios::fmtflags orig_flags = f.flags(); | | uword x_n_rows = x.n_rows; | |
| | | uword x_n_cols = x.n_cols; | |
| f << diskio::gen_txt_header(x) << '\n'; | | uword x_n_nonzero = x.n_nonzero; | |
| f << x.n_rows << ' ' << x.n_cols << '\n'; | | | |
| | | | |
| uword cell_width; | | | |
| | | | |
| // TODO: need sane values for complex numbers | | | |
| | | | |
| if( (is_float<eT>::value == true) || (is_double<eT>::value == true) ) | | | |
| { | | | |
| f.setf(ios::scientific); | | | |
| f.precision(10); | | | |
| cell_width = 18; | | | |
| } | | | |
| | | | |
| for(uword row=0; row < x.n_rows; ++row) | | | |
| { | | | |
| for(uword col=0; col < x.n_cols; ++col) | | | |
| { | | | |
| f.put(' '); | | | |
| | | | |
| if( (is_float<eT>::value == true) || (is_double<eT>::value == true) ) | | | |
| { | | | |
| f.width(cell_width); | | | |
| } | | | |
| | | | |
|
| f << x.at(row,col); | | //Put down the header | |
| } | | f << x_n_rows << " " << x_n_cols << " " << x_n_nonzero << endl; | |
| | | | |
|
| f.put('\n'); | | typename SpMat<eT>::const_iterator iter = x.begin(); | |
| } | | for(; iter.pos() < x.n_nonzero; iter++) | |
| | | f << iter.row() << " " << iter.col() << " " << *iter << endl; | |
| | | | |
| const bool save_okay = f.good(); | | const bool save_okay = f.good(); | |
| | | | |
|
| f.flags(orig_flags); | | | |
| | | | |
| return save_okay; | | return save_okay; | |
| } | | } | |
| | | | |
|
| //! Save a matrix in CSV text format (human readable) | | //! Load a matrix as raw text (no header, human readable). | |
| | | //! Can read matrices saved as text in Matlab and Octave. | |
| | | //! NOTE: this is much slower than reading a file with a header. | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_csv_ascii(const SpMat<eT>& x, const std::string& final_name) | | diskio::load_raw_ascii(SpMat<eT>& x, const std::string& name, std::string&
err_msg) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | std::fstream f; | |
| | | f.open(name.c_str(), std::fstream::in); | |
| std::ofstream f(tmp_name.c_str()); | | | |
| | | | |
|
| bool save_okay = f.is_open(); | | bool load_okay = f.is_open(); | |
| | | | |
|
| if(save_okay == true) | | if(load_okay == true) | |
| { | | { | |
|
| save_okay = diskio::save_csv_ascii(x, f); | | load_okay = diskio::load_raw_ascii(x, f, err_msg); | |
| | | | |
| f.flush(); | | | |
| f.close(); | | f.close(); | |
|
| | | | |
| if(save_okay == true) | | | |
| { | | | |
| save_okay = diskio::safe_rename(tmp_name, final_name); | | | |
| } | | | |
| } | | } | |
| | | | |
|
| return save_okay; | | return load_okay; | |
| } | | } | |
| | | | |
|
| //! Save a matrix in CSV text format (human readable) | | //! Load a matrix as raw text (no header, human readable). | |
| | | //! Can read matrices saved as text in Matlab and Octave. | |
| | | //! NOTE: this is much slower than reading a file with a header. | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::save_csv_ascii(const SpMat<eT>& x, std::ostream& f) | | diskio::load_raw_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
|
| const ios::fmtflags orig_flags = f.flags(); | | bool load_okay = f.good(); | |
| | | | |
| // TODO: need sane values for complex numbers | | | |
| | | | |
|
| if( (is_float<eT>::value == true) || (is_double<eT>::value == true) ) | | f.clear(); | |
| { | | const std::fstream::pos_type pos1 = f.tellg(); | |
| f.setf(ios::scientific); | | | |
| f.precision(10); | | | |
| } | | | |
| | | | |
|
| uword x_n_rows = x.n_rows; | | // | |
| uword x_n_cols = x.n_cols; | | // work out the size | |
| | | | |
|
| for(uword row=0; row < x_n_rows; ++row) | | uword f_n_rows = 0; | |
| { | | uword f_n_cols = 0; | |
| for(uword col=0; col < x_n_cols; ++col) | | | |
| { | | | |
| f << x.at(row,col); | | | |
| | | | |
|
| if( col < (x_n_cols-1) ) | | bool f_n_cols_found = false; | |
| { | | | |
| f.put(','); | | | |
| } | | | |
| } | | | |
| | | | |
|
| f.put('\n'); | | std::string line_string; | |
| } | | std::string token; | |
| | | | |
|
| const bool save_okay = f.good(); | | while( (f.good() == true) && (load_okay == true) ) | |
| | | { | |
| | | std::getline(f, line_string); | |
| | | | |
|
| f.flags(orig_flags); | | if(line_string.size() == 0) | |
| | | { | |
| | | break; | |
| | | } | |
| | | | |
|
| return save_okay; | | std::stringstream line_stream(line_string); | |
| } | | | |
| | | | |
|
| //! Save a matrix in binary format, | | uword line_n_cols = 0; | |
| //! with a header that stores the matrix type as well as its dimensions | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::save_arma_binary(const SpMat<eT>& x, const std::string& final_name) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
|
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | while (line_stream >> token) | |
| | | { | |
| | | ++line_n_cols; | |
| | | } | |
| | | | |
|
| std::ofstream f(tmp_name.c_str(), std::fstream::binary); | | if(f_n_cols_found == false) | |
| | | { | |
| | | f_n_cols = line_n_cols; | |
| | | f_n_cols_found = true; | |
| | | } | |
| | | else | |
| | | { | |
| | | if(line_n_cols != f_n_cols) | |
| | | { | |
| | | err_msg = "inconsistent number of columns in "; | |
| | | load_okay = false; | |
| | | } | |
| | | } | |
| | | | |
|
| bool save_okay = f.is_open(); | | ++f_n_rows; | |
| | | } | |
| | | | |
|
| if(save_okay == true) | | if(load_okay == true) | |
| { | | { | |
|
| save_okay = diskio::save_arma_binary(x, f); | | f.clear(); | |
| | | f.seekg(pos1); | |
| | | | |
|
| f.flush(); | | x.set_size(f_n_rows, f_n_cols); | |
| f.close(); | | | |
| | | | |
|
| if(save_okay == true) | | eT val; | |
| | | | |
| | | for(uword row=0; (row < x.n_rows) && (load_okay == true); ++row) | |
| { | | { | |
|
| save_okay = diskio::safe_rename(tmp_name, final_name); | | for(uword col=0; (col < x.n_cols) && (load_okay == true); ++col) | |
| | | { | |
| | | f >> val; | |
| | | if(f.fail() == false) | |
| | | { | |
| | | x.at(row,col) = val; | |
| | | } | |
| | | else | |
| | | { | |
| | | load_okay = false; | |
| | | err_msg = "couldn't interpret data in "; | |
| | | //break; | |
| | | } | |
| | | } | |
| } | | } | |
| } | | } | |
| | | | |
|
| return save_okay; | | // an empty file indicates an empty matrix | |
| | | if( (f_n_cols_found == false) && (load_okay == true) ) | |
| | | { | |
| | | x.reset(); | |
| | | } | |
| | | | |
| | | return load_okay; | |
| } | | } | |
| | | | |
|
| //! Save a matrix in binary format, | | //TODO: this function doesn't make sense for sparse matrices, as there is n | |
| //! with a header that stores the matrix type as well as its dimensions | | o universal definition of a "raw" CSC file | |
| template<typename eT> | | // //! Load a matrix in binary format (no header); | |
| inline | | // //! the matrix is assumed to have one column | |
| bool | | // template<typename eT> | |
| diskio::save_arma_binary(const SpMat<eT>& x, std::ostream& f) | | // inline | |
| { | | // bool | |
| arma_extra_debug_sigprint(); | | // diskio::load_raw_binary(SpMat<eT>& x, const std::string& name, std::stri | |
| | | ng& err_msg) | |
| //Write the dims of the matrix first | | // { | |
| f << diskio::gen_bin_header(x) << '\n'; | | // arma_extra_debug_sigprint(); | |
| f << x.n_rows << ' ' << x.n_cols << ' ' << x.n_nonzero << '\n'; | | // | |
| | | // | |
| //Now write the data for the three vectors | | // std::ifstream f; | |
| f.write( reinterpret_cast<const char*>(x.values), std::streamsize(x.n_non | | // f.open(name.c_str(), std::fstream::binary); | |
| zero*sizeof(eT)) ); | | // | |
| f.write( reinterpret_cast<const char*>(x.row_indices), std::streamsize(x. | | // bool load_okay = f.is_open(); | |
| n_nonzero*sizeof(uword)) ); | | // | |
| f.write( reinterpret_cast<const char*>(x.col_ptrs), std::streamsize((x.n_ | | // if(load_okay == true) | |
| cols+1)*sizeof(uword)) ); | | // { | |
| | | // load_okay = diskio::load_raw_binary(x, f, err_msg); | |
| return f.good(); | | // f.close(); | |
| } | | // } | |
| | | // | |
| //! Save a matrix as a PGM greyscale image | | // return load_okay; | |
| template<typename eT> | | // } | |
| inline | | | |
| bool | | | |
| diskio::save_pgm_binary(const SpMat<eT>& x, const std::string& final_name) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | | |
| | | | |
| std::fstream f(tmp_name.c_str(), std::fstream::out | std::fstream::binary | | | |
| ); | | | |
| | | | |
| bool save_okay = f.is_open(); | | | |
| | | | |
| if(save_okay == true) | | | |
| { | | | |
| save_okay = diskio::save_pgm_binary(x, f); | | | |
| | | | |
| f.flush(); | | | |
| f.close(); | | | |
| | | | |
| if(save_okay == true) | | | |
| { | | | |
| save_okay = diskio::safe_rename(tmp_name, final_name); | | | |
| } | | | |
| } | | | |
| | | | |
| return save_okay; | | | |
| } | | | |
| | | | |
| // | | | |
| // TODO: | | | |
| // add functionality to save the image in a normalised format, | | | |
| // i.e. scaled so that every value falls in the [0,255] range. | | | |
| | | | |
| //! Save a matrix as a PGM greyscale image | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::save_pgm_binary(const SpMat<eT>& x, std::ostream& f) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| f << "P5" << '\n'; | | | |
| f << x.n_cols << ' ' << x.n_rows << ' ' << x.n_cols*x.n_rows << '\n'; | | | |
| f << 255 << '\n'; | | | |
| | | | |
| const uword n_elem = x.n_nonzero; | | | |
| podarray<u8> tmp(n_elem); | | | |
| | | | |
| uword i = 0; | | | |
| | | | |
| for(uword row=0; row < x.n_rows; ++row) | | | |
| { | | | |
| for(uword col=0; col < x.n_cols; ++col) | | | |
| { | | | |
| tmp[i] = u8( x.at(row,col) ); // TODO: add round() ? | | | |
| ++i; | | | |
| } | | | |
| } | | | |
| | | | |
| f.write(reinterpret_cast<const char*>(tmp.mem), std::streamsize(x.n_rows* | | | |
| x.n_cols) ); | | | |
| | | | |
| return f.good(); | | | |
| } | | | |
| | | | |
| //! Save a matrix as a PGM greyscale image | | | |
| template<typename T> | | | |
| inline | | | |
| bool | | | |
| diskio::save_pgm_binary(const SpMat< std::complex<T> >& x, const std::strin | | | |
| g& final_name) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| const uchar_mat tmp = conv_to<uchar_mat>::from(x); | | | |
| | | | |
| return diskio::save_pgm_binary(tmp, final_name); | | | |
| } | | | |
| | | | |
| //! Save a matrix as a PGM greyscale image | | | |
| template<typename T> | | | |
| inline | | | |
| bool | | | |
| diskio::save_pgm_binary(const SpMat< std::complex<T> >& x, std::ostream& f) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| const uchar_mat tmp = conv_to<uchar_mat>::from(x); | | | |
| | | | |
| return diskio::save_pgm_binary(tmp, f); | | | |
| } | | | |
| | | | |
| //! Save a matrix in ASCII coord format | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::save_coord_ascii(const SpMat<eT>& x, const std::string& final_name) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| const std::string tmp_name = diskio::gen_tmp_name(final_name); | | | |
| | | | |
| std::ofstream f(tmp_name.c_str()); | | | |
| | | | |
| bool save_okay = f.is_open(); | | | |
| | | | |
| if(save_okay == true) | | | |
| { | | | |
| save_okay = diskio::save_coord_ascii(x, f); | | | |
| | | | |
| f.flush(); | | | |
| f.close(); | | | |
| | | | |
| if(save_okay == true) | | | |
| { | | | |
| save_okay = diskio::safe_rename(tmp_name, final_name); | | | |
| } | | | |
| } | | | |
| | | | |
| return save_okay; | | | |
| } | | | |
| | | | |
| //! Save a matrix in CSV text format (human readable) | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::save_coord_ascii(const SpMat<eT>& x, std::ostream& f) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| uword x_n_rows = x.n_rows; | | | |
| uword x_n_cols = x.n_cols; | | | |
| uword x_n_nonzero = x.n_nonzero; | | | |
| | | | |
| //Put down the header | | | |
| f << x_n_rows << " " << x_n_cols << " " << x_n_nonzero << endl; | | | |
| | | | |
| typename SpMat<eT>::const_iterator iter = x.begin(); | | | |
| for(; iter.pos() < x.n_nonzero; iter++) | | | |
| f << iter.row() << " " << iter.col() << " " << *iter << endl; | | | |
| | | | |
| const bool save_okay = f.good(); | | | |
| | | | |
| return save_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix as raw text (no header, human readable). | | | |
| //! Can read matrices saved as text in Matlab and Octave. | | | |
| //! NOTE: this is much slower than reading a file with a header. | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_raw_ascii(SpMat<eT>& x, const std::string& name, std::string& | | | |
| err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| std::fstream f; | | | |
| f.open(name.c_str(), std::fstream::in); | | | |
| | | | |
| bool load_okay = f.is_open(); | | | |
| | | | |
| if(load_okay == true) | | | |
| { | | | |
| load_okay = diskio::load_raw_ascii(x, f, err_msg); | | | |
| f.close(); | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix as raw text (no header, human readable). | | | |
| //! Can read matrices saved as text in Matlab and Octave. | | | |
| //! NOTE: this is much slower than reading a file with a header. | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_raw_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| bool load_okay = f.good(); | | | |
| | | | |
| f.clear(); | | | |
| const std::fstream::pos_type pos1 = f.tellg(); | | | |
| | | | |
| // | | | |
| // work out the size | | | |
| | | | |
| uword f_n_rows = 0; | | | |
| uword f_n_cols = 0; | | | |
| | | | |
| bool f_n_cols_found = false; | | | |
| | | | |
| std::string line_string; | | | |
| std::string token; | | | |
| | | | |
| while( (f.good() == true) && (load_okay == true) ) | | | |
| { | | | |
| std::getline(f, line_string); | | | |
| | | | |
| if(line_string.size() == 0) | | | |
| { | | | |
| break; | | | |
| } | | | |
| | | | |
| std::stringstream line_stream(line_string); | | | |
| | | | |
| uword line_n_cols = 0; | | | |
| | | | |
| while (line_stream >> token) | | | |
| { | | | |
| ++line_n_cols; | | | |
| } | | | |
| | | | |
| if(f_n_cols_found == false) | | | |
| { | | | |
| f_n_cols = line_n_cols; | | | |
| f_n_cols_found = true; | | | |
| } | | | |
| else | | | |
| { | | | |
| if(line_n_cols != f_n_cols) | | | |
| { | | | |
| err_msg = "inconsistent number of columns in "; | | | |
| load_okay = false; | | | |
| } | | | |
| } | | | |
| | | | |
| ++f_n_rows; | | | |
| } | | | |
| | | | |
| if(load_okay == true) | | | |
| { | | | |
| f.clear(); | | | |
| f.seekg(pos1); | | | |
| | | | |
| x.set_size(f_n_rows, f_n_cols); | | | |
| | | | |
| eT val; | | | |
| | | | |
| for(uword row=0; (row < x.n_rows) && (load_okay == true); ++row) | | | |
| { | | | |
| for(uword col=0; (col < x.n_cols) && (load_okay == true); ++col) | | | |
| { | | | |
| f >> val; | | | |
| if(f.fail() == false) | | | |
| { | | | |
| x.at(row,col) = val; | | | |
| } | | | |
| else | | | |
| { | | | |
| load_okay = false; | | | |
| err_msg = "couldn't interpret data in "; | | | |
| //break; | | | |
| } | | | |
| } | | | |
| } | | | |
| } | | | |
| | | | |
| // an empty file indicates an empty matrix | | | |
| if( (f_n_cols_found == false) && (load_okay == true) ) | | | |
| { | | | |
| x.reset(); | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix in binary format (no header); | | | |
| //! the matrix is assumed to have one column | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_raw_binary(SpMat<eT>& x, const std::string& name, std::string& | | | |
| err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| std::ifstream f; | | | |
| f.open(name.c_str(), std::fstream::binary); | | | |
| | | | |
| bool load_okay = f.is_open(); | | | |
| | | | |
| if(load_okay == true) | | | |
| { | | | |
| load_okay = diskio::load_raw_binary(x, f, err_msg); | | | |
| f.close(); | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_raw_binary(SpMat<eT>& x, std::istream& f, std::string& err_msg | | | |
| ) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| arma_ignore(err_msg); | | | |
| | | | |
| //Make sure we are at top of file. | | | |
| f.clear(); | | | |
| | | | |
| //Read the dimensions of the matrix | | | |
| f.read( (char*) &access::rw(x.n_rows), std::streamsize(sizeof(uword)) ); | | | |
| f.read( (char*) &access::rw(x.n_cols), std::streamsize(sizeof(uword)) ); | | | |
| f.read( (char*) &access::rw(x.n_nonzero), std::streamsize(sizeof(uword)) | | | |
| ); | | | |
| access::rw(x.n_elem) = x.n_rows * x.n_cols; | | | |
| | | | |
| //Allocate appropriate memory, then fill it. | | | |
| eT* in_values = memory::acquire<eT>(x.n_nonzero); | | | |
| uword* in_row_indices = memory::acquire<uword>(x.n_nonzero); | | | |
| uword* in_col_ptrs = memory::acquire<uword>(x.n_cols + 1); | | | |
| | | | |
| f.read( reinterpret_cast<char*>(in_values), std::streamsize(x.n_nonzero * | | | |
| sizeof(eT)) ); | | | |
| f.read( reinterpret_cast<char*>(in_row_indices), std::streamsize(x.n_nonz | | | |
| ero * sizeof(uword)) ); | | | |
| f.read( reinterpret_cast<char*>(in_col_ptrs), std::streamsize((x.n_cols + | | | |
| 1) * sizeof(uword)) ); | | | |
| | | | |
| //Replace the current data in the sparse matrix | | | |
| if(x.n_nonzero > 0) | | | |
| { | | | |
| memory::release(x.values); | | | |
| memory::release(x.row_indices); | | | |
| } | | | |
| | | | |
| memory::release(x.col_ptrs); | | | |
| | | | |
| access::rwp(x.values) = in_values; | | | |
| access::rwp(x.row_indices) = in_row_indices; | | | |
| access::rwp(x.col_ptrs) = in_col_ptrs; | | | |
| | | | |
| return f.good(); | | | |
| } | | | |
| | | | |
| //! Load a matrix in text format (human readable), | | | |
| //! with a header that indicates the matrix type as well as its dimensions | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_arma_ascii(SpMat<eT>& x, const std::string& name, std::string& | | | |
| err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| std::ifstream f(name.c_str()); | | | |
| | | | |
| bool load_okay = f.is_open(); | | | |
| | | | |
| if(load_okay == true) | | | |
| { | | | |
| load_okay = diskio::load_arma_ascii(x, f, err_msg); | | | |
| f.close(); | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix in text format (human readable), | | | |
| //! with a header that indicates the matrix type as well as its dimensions | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_arma_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg | | | |
| ) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| bool load_okay = true; | | | |
| | | | |
| eT tmp; | | | |
| std::string f_header; | | | |
| uword f_n_rows; | | | |
| uword f_n_cols; | | | |
| | | | |
| f >> f_header; | | | |
| f >> f_n_rows; | | | |
| f >> f_n_cols; | | | |
| | | | |
| if(f_header == diskio::gen_txt_header(x)) | | | |
| { | | | |
| x.set_size(f_n_rows, f_n_cols); | | | |
| | | | |
| for(uword row=0; row < x.n_rows; ++row) | | | |
| { | | | |
| for(uword col=0; col < x.n_cols; ++col) | | | |
| { | | | |
| f >> tmp; | | | |
| x.at(row,col) = tmp; | | | |
| } | | | |
| } | | | |
| | | | |
| load_okay = f.good(); | | | |
| } | | | |
| else | | | |
| { | | | |
| load_okay = false; | | | |
| err_msg = "incorrect header in "; | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix in CSV text format (human readable) | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_csv_ascii(SpMat<eT>& x, const std::string& name, std::string& | | | |
| err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| std::fstream f; | | | |
| f.open(name.c_str(), std::fstream::in); | | | |
| | | | |
| bool load_okay = f.is_open(); | | | |
| | | | |
| if(load_okay == true) | | | |
| { | | | |
| load_okay = diskio::load_csv_ascii(x, f, err_msg); | | | |
| f.close(); | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a matrix in CSV text format (human readable) | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_csv_ascii(SpMat<eT>& x, std::istream& f, std::string& err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| bool load_okay = f.good(); | | | |
| | | | |
| f.clear(); | | | |
| const std::fstream::pos_type pos1 = f.tellg(); | | | |
| | | | |
| // | | | |
| // work out the size | | | |
| | | | |
| uword f_n_rows = 0; | | | |
| uword f_n_cols = 0; | | | |
| | | | |
| std::string line_string; | | | |
| std::string token; | | | |
| | | | |
| while( (f.good() == true) && (load_okay == true) ) | | | |
| { | | | |
| std::getline(f, line_string); | | | |
| | | | |
| if(line_string.size() == 0) | | | |
| { | | | |
| break; | | | |
| } | | | |
| | | | |
| std::stringstream line_stream(line_string); | | | |
| | | | |
| uword line_n_cols = 0; | | | |
| | | | |
| while(line_stream.good() == true) | | | |
| { | | | |
| getline(line_stream, token, ','); | | | |
| ++line_n_cols; | | | |
| } | | | |
| | | | |
| if(f_n_cols < line_n_cols) | | | |
| { | | | |
| f_n_cols = line_n_cols; | | | |
| } | | | |
| | | | |
| ++f_n_rows; | | | |
| } | | | |
| | | | |
| f.clear(); | | | |
| f.seekg(pos1); | | | |
| x.zeros(f_n_rows, f_n_cols); | | | |
| | | | |
| uword row = 0; | | | |
| | | | |
| while(f.good() == true) | | | |
| { | | | |
| std::getline(f, line_string); | | | |
| | | | |
| if(line_string.size() == 0) | | | |
| { | | | |
| break; | | | |
| } | | | |
| | | | |
| std::stringstream line_stream(line_string); | | | |
| | | | |
| uword col = 0; | | | |
| | | | |
| while(line_stream.good() == true) | | | |
| { | | | |
| getline(line_stream, token, ','); | | | |
| | | | |
| eT val; | | | |
| | | | |
| std::stringstream ss(token); | | | |
| | | | |
|
| ss >> val; | | //// TODO: this function doesn't make sense for sparse matrices, as there i | |
| | | s no universal definition of a "raw" CSC file | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_raw_binary(SpMat<eT>& x, std::istream& f, std::string& err_ | |
| | | msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // arma_ignore(err_msg); | |
| | | // | |
| | | // | |
| | | // //Make sure we are at top of file. | |
| | | // f.clear(); | |
| | | // | |
| | | // //Read the dimensions of the matrix | |
| | | // f.read( (char*) &access::rw(x.n_rows), std::streamsize(sizeof(uword)) | |
| | | ); | |
| | | // f.read( (char*) &access::rw(x.n_cols), std::streamsize(sizeof(uword)) | |
| | | ); | |
| | | // f.read( (char*) &access::rw(x.n_nonzero), std::streamsize(sizeof(uword | |
| | | )) ); | |
| | | // access::rw(x.n_elem) = x.n_rows * x.n_cols; | |
| | | // | |
| | | // //Allocate appropriate memory, then fill it. | |
| | | // eT* in_values = memory::acquire<eT>(x.n_nonzero); // TODO: BUG. use | |
| | | acquire_chunked | |
| | | // uword* in_row_indices = memory::acquire<uword>(x.n_nonzero); // TODO: | |
| | | BUG. use acquire_chunked | |
| | | // uword* in_col_ptrs = memory::acquire<uword>(x.n_cols + 1); | |
| | | // | |
| | | // f.read( reinterpret_cast<char*>(in_values), std::streamsize(x.n_nonzer | |
| | | o * sizeof(eT)) ); | |
| | | // f.read( reinterpret_cast<char*>(in_row_indices), std::streamsize(x.n_n | |
| | | onzero * sizeof(uword)) ); | |
| | | // f.read( reinterpret_cast<char*>(in_col_ptrs), std::streamsize((x.n_col | |
| | | s + 1) * sizeof(uword)) ); | |
| | | // | |
| | | // //Replace the current data in the sparse matrix | |
| | | // if(x.n_nonzero > 0) | |
| | | // { | |
| | | // memory::release(x.values); | |
| | | // memory::release(x.row_indices); | |
| | | // } | |
| | | // | |
| | | // memory::release(x.col_ptrs); | |
| | | // | |
| | | // access::rwp(x.values) = in_values; | |
| | | // access::rwp(x.row_indices) = in_row_indices; | |
| | | // access::rwp(x.col_ptrs) = in_col_ptrs; | |
| | | // | |
| | | // return f.good(); | |
| | | // } | |
| | | | |
|
| if(ss.fail() == false) | | //// TODO: this function needs to be reworked to save only non-zero element | |
| { | | s; we can define our own format, or adapt an existing format | |
| x.at(row,col) = val; | | // //! Load a matrix in text format (human readable), | |
| } | | // //! with a header that indicates the matrix type as well as its dimensio | |
| | | ns | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_arma_ascii(SpMat<eT>& x, const std::string& name, std::stri | |
| | | ng& err_msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // std::ifstream f(name.c_str()); | |
| | | // | |
| | | // bool load_okay = f.is_open(); | |
| | | // | |
| | | // if(load_okay == true) | |
| | | // { | |
| | | // load_okay = diskio::load_arma_ascii(x, f, err_msg); | |
| | | // f.close(); | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| ++col; | | //// TODO: this function needs to be reworked to save only non-zero element | |
| } | | s; we can define our own format, or adapt an existing format | |
| | | // //! Load a matrix in text format (human readable), | |
| | | // //! with a header that indicates the matrix type as well as its dimensio | |
| | | ns | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_arma_ascii(SpMat<eT>& x, std::istream& f, std::string& err_ | |
| | | msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // | |
| | | // bool load_okay = true; | |
| | | // | |
| | | // eT tmp; | |
| | | // std::string f_header; | |
| | | // uword f_n_rows; | |
| | | // uword f_n_cols; | |
| | | // | |
| | | // f >> f_header; | |
| | | // f >> f_n_rows; | |
| | | // f >> f_n_cols; | |
| | | // | |
| | | // if(f_header == diskio::gen_txt_header(x)) | |
| | | // { | |
| | | // x.set_size(f_n_rows, f_n_cols); | |
| | | // | |
| | | // for(uword row=0; row < x.n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < x.n_cols; ++col) | |
| | | // { | |
| | | // f >> tmp; | |
| | | // x.at(row,col) = tmp; | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // load_okay = f.good(); | |
| | | // } | |
| | | // else | |
| | | // { | |
| | | // load_okay = false; | |
| | | // err_msg = "incorrect header in "; | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| ++row; | | // //! Load a matrix in CSV text format (human readable) | |
| } | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_csv_ascii(SpMat<eT>& x, const std::string& name, std::strin | |
| | | g& err_msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // std::fstream f; | |
| | | // f.open(name.c_str(), std::fstream::in); | |
| | | // | |
| | | // bool load_okay = f.is_open(); | |
| | | // | |
| | | // if(load_okay == true) | |
| | | // { | |
| | | // load_okay = diskio::load_csv_ascii(x, f, err_msg); | |
| | | // f.close(); | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| return load_okay; | | // //! Load a matrix in CSV text format (human readable) | |
| } | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_csv_ascii(SpMat<eT>& x, std::istream& f, std::string& err_m | |
| | | sg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // bool load_okay = f.good(); | |
| | | // | |
| | | // f.clear(); | |
| | | // const std::fstream::pos_type pos1 = f.tellg(); | |
| | | // | |
| | | // // | |
| | | // // work out the size | |
| | | // | |
| | | // uword f_n_rows = 0; | |
| | | // uword f_n_cols = 0; | |
| | | // | |
| | | // std::string line_string; | |
| | | // std::string token; | |
| | | // | |
| | | // while( (f.good() == true) && (load_okay == true) ) | |
| | | // { | |
| | | // std::getline(f, line_string); | |
| | | // | |
| | | // if(line_string.size() == 0) | |
| | | // { | |
| | | // break; | |
| | | // } | |
| | | // | |
| | | // std::stringstream line_stream(line_string); | |
| | | // | |
| | | // uword line_n_cols = 0; | |
| | | // | |
| | | // while(line_stream.good() == true) | |
| | | // { | |
| | | // getline(line_stream, token, ','); | |
| | | // ++line_n_cols; | |
| | | // } | |
| | | // | |
| | | // if(f_n_cols < line_n_cols) | |
| | | // { | |
| | | // f_n_cols = line_n_cols; | |
| | | // } | |
| | | // | |
| | | // ++f_n_rows; | |
| | | // } | |
| | | // | |
| | | // f.clear(); | |
| | | // f.seekg(pos1); | |
| | | // x.zeros(f_n_rows, f_n_cols); | |
| | | // | |
| | | // uword row = 0; | |
| | | // | |
| | | // while(f.good() == true) | |
| | | // { | |
| | | // std::getline(f, line_string); | |
| | | // | |
| | | // if(line_string.size() == 0) | |
| | | // { | |
| | | // break; | |
| | | // } | |
| | | // | |
| | | // std::stringstream line_stream(line_string); | |
| | | // | |
| | | // uword col = 0; | |
| | | // | |
| | | // while(line_stream.good() == true) | |
| | | // { | |
| | | // getline(line_stream, token, ','); | |
| | | // | |
| | | // eT val; | |
| | | // | |
| | | // std::stringstream ss(token); | |
| | | // | |
| | | // ss >> val; | |
| | | // | |
| | | // if(ss.fail() == false) | |
| | | // { | |
| | | // x.at(row,col) = val; | |
| | | // } | |
| | | // | |
| | | // ++col; | |
| | | // } | |
| | | // | |
| | | // ++row; | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
| //! Load a matrix in binary format, | | //! Load a matrix in binary format, | |
| //! with a header that indicates the matrix type as well as its dimensions | | //! with a header that indicates the matrix type as well as its dimensions | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
| diskio::load_arma_binary(SpMat<eT>& x, const std::string& name, std::string
& err_msg) | | diskio::load_arma_binary(SpMat<eT>& x, const std::string& name, std::string
& err_msg) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| | | | |
| skipping to change at line 2888 | | skipping to change at line 2905 | |
| f >> access::rw(x.n_cols); | | f >> access::rw(x.n_cols); | |
| f >> access::rw(x.n_nonzero); | | f >> access::rw(x.n_nonzero); | |
| access::rw(x.n_elem) = x.n_rows*x.n_cols; | | access::rw(x.n_elem) = x.n_rows*x.n_cols; | |
| | | | |
| if(f_header == diskio::gen_bin_header(x)) | | if(f_header == diskio::gen_bin_header(x)) | |
| { | | { | |
| //f.seekg(1, ios::cur); // NOTE: this may not be portable, as on a Win
dows machine a newline could be two characters | | //f.seekg(1, ios::cur); // NOTE: this may not be portable, as on a Win
dows machine a newline could be two characters | |
| f.get(); | | f.get(); | |
| | | | |
| //Allocate memory for the incoming data | | //Allocate memory for the incoming data | |
|
| eT* in_values = memory::acquire<eT>(x.n_nonzero); | | eT* in_values = memory::acquire_chunked<eT> (x.n_nonzero); | |
| uword* in_row_indices = memory::acquire<uword>(x.n_nonzero); | | uword* in_row_indices = memory::acquire_chunked<uword>(x.n_nonzero); | |
| uword* in_col_ptrs = memory::acquire<uword>(x.n_cols + 1); | | uword* in_col_ptrs = memory::acquire<uword> (x.n_cols + 1); | |
| | | | |
| f.read((char*) in_values, std::streamsize(x.n_nonzero * sizeof(eT)) ); | | f.read((char*) in_values, std::streamsize(x.n_nonzero * sizeof(eT)) ); | |
| f.read((char*) in_row_indices, std::streamsize(x.n_nonzero * sizeof(uwo
rd)) ); | | f.read((char*) in_row_indices, std::streamsize(x.n_nonzero * sizeof(uwo
rd)) ); | |
| f.read((char*) in_col_ptrs, std::streamsize((x.n_cols + 1) * sizeof(uwo
rd)) ); | | f.read((char*) in_col_ptrs, std::streamsize((x.n_cols + 1) * sizeof(uwo
rd)) ); | |
| | | | |
| //Stick new data into sparse matrix | | //Stick new data into sparse matrix | |
| if (x.n_nonzero > 0) | | if (x.n_nonzero > 0) | |
| { | | { | |
| memory::release(x.values); | | memory::release(x.values); | |
| memory::release(x.row_indices); | | memory::release(x.row_indices); | |
| } | | } | |
| | | | |
| memory::release(x.col_ptrs); | | memory::release(x.col_ptrs); | |
| | | | |
|
| access::rw(x.values) = in_values; | | access::rw(x.values) = in_values; | |
| access::rw(x.row_indices) = in_row_indices; | | access::rw(x.row_indices) = in_row_indices; | |
|
| access::rw(x.col_ptrs) = in_col_ptrs; | | access::rw(x.col_ptrs) = in_col_ptrs; | |
| | | | |
| load_okay = f.good(); | | load_okay = f.good(); | |
| } | | } | |
| else | | else | |
| { | | { | |
| load_okay = false; | | load_okay = false; | |
| err_msg = "incorrect header in "; | | err_msg = "incorrect header in "; | |
| } | | } | |
| | | | |
| return load_okay; | | return load_okay; | |
| } | | } | |
| | | | |
|
| //! Load a PGM greyscale image as a matrix | | // //! Load a PGM greyscale image as a matrix | |
| template<typename eT> | | // template<typename eT> | |
| inline | | // inline | |
| bool | | // bool | |
| diskio::load_pgm_binary(SpMat<eT>& x, const std::string& name, std::string& | | // diskio::load_pgm_binary(SpMat<eT>& x, const std::string& name, std::stri | |
| err_msg) | | ng& err_msg) | |
| { | | // { | |
| arma_extra_debug_sigprint(); | | // arma_extra_debug_sigprint(); | |
| | | // | |
| std::fstream f; | | // | |
| f.open(name.c_str(), std::fstream::in | std::fstream::binary); | | // std::fstream f; | |
| | | // f.open(name.c_str(), std::fstream::in | std::fstream::binary); | |
| bool load_okay = f.is_open(); | | // | |
| | | // bool load_okay = f.is_open(); | |
| if(load_okay == true) | | // | |
| { | | // if(load_okay == true) | |
| load_okay = diskio::load_pgm_binary(x, f, err_msg); | | // { | |
| f.close(); | | // load_okay = diskio::load_pgm_binary(x, f, err_msg); | |
| } | | // f.close(); | |
| | | // } | |
| return load_okay; | | // | |
| } | | // return load_okay; | |
| | | // } | |
| //! Load a PGM greyscale image as a matrix | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_pgm_binary(SpMat<eT>& x, std::istream& f, std::string& err_msg | | | |
| ) | | | |
| { | | | |
| bool load_okay = true; | | | |
| | | | |
| std::string f_header; | | | |
| f >> f_header; | | | |
| | | | |
| if(f_header == "P5") | | | |
| { | | | |
| uword f_n_rows = 0; | | | |
| uword f_n_cols = 0; | | | |
| int f_maxval = 0; | | | |
| | | | |
| diskio::pnm_skip_comments(f); | | | |
| | | | |
| f >> f_n_cols; | | | |
| diskio::pnm_skip_comments(f); | | | |
| | | | |
| f >> f_n_rows; | | | |
| diskio::pnm_skip_comments(f); | | | |
| | | | |
| f >> f_maxval; //grabing a redundant size value | | | |
| | | | |
| f >> f_maxval; | | | |
| f.get(); | | | |
| | | | |
| if( (f_maxval > 0) || (f_maxval <= 65535) ) | | | |
| { | | | |
| x.set_size(f_n_rows,f_n_cols); | | | |
| | | | |
| if(f_maxval <= 255) | | | |
| { | | | |
| const uword n_elem = f_n_cols*f_n_rows; | | | |
| podarray<u8> tmp(n_elem); | | | |
| | | | |
| f.read( reinterpret_cast<char*>(tmp.memptr()), std::streamsize(n_el | | | |
| em) ); | | | |
| | | | |
| uword i = 0; | | | |
| | | | |
| //cout << "f_n_cols = " << f_n_cols << endl; | | | |
| //cout << "f_n_rows = " << f_n_rows << endl; | | | |
| | | | |
| for(uword row=0; row < f_n_rows; ++row) | | | |
| { | | | |
| for(uword col=0; col < f_n_cols; ++col) | | | |
| { | | | |
| x.at(row,col) = eT(tmp[i]); | | | |
| ++i; | | | |
| } | | | |
| } | | | |
| | | | |
| } | | | |
| else | | | |
| { | | | |
| const uword n_elem = f_n_cols*f_n_rows; | | | |
| podarray<u16> tmp(n_elem); | | | |
| | | | |
| f.read( reinterpret_cast<char *>(tmp.memptr()), std::streamsize(n_e | | | |
| lem*2) ); | | | |
| | | | |
| uword i = 0; | | | |
| | | | |
| for(uword row=0; row < f_n_rows; ++row) | | | |
| { | | | |
| for(uword col=0; col < f_n_cols; ++col) | | | |
| { | | | |
| x.at(row,col) = eT(tmp[i]); | | | |
| ++i; | | | |
| } | | | |
| } | | | |
| | | | |
| } | | | |
| | | | |
| } | | | |
| else | | | |
| { | | | |
| load_okay = false; | | | |
| err_msg = "currently no code available to handle loading "; | | | |
| } | | | |
| | | | |
| if(f.good() == false) | | | |
| { | | | |
| load_okay = false; | | | |
| } | | | |
| } | | | |
| else | | | |
| { | | | |
| load_okay = false; | | | |
| err_msg = "unsupported header in "; | | | |
| } | | | |
| | | | |
| return load_okay; | | | |
| } | | | |
| | | | |
| //! Load a PGM greyscale image as a matrix | | | |
| template<typename T> | | | |
| inline | | | |
| bool | | | |
| diskio::load_pgm_binary(SpMat< std::complex<T> >& x, const std::string& nam | | | |
| e, std::string& err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| uchar_mat tmp; | | | |
| const bool load_okay = diskio::load_pgm_binary(tmp, name, err_msg); | | | |
| | | | |
| x = conv_to< Mat< std::complex<T> > >::from(tmp); | | | |
| | | | |
|
| return load_okay; | | ////! Load a PGM greyscale image as a matrix | |
| } | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_pgm_binary(SpMat<eT>& x, std::istream& f, std::string& err_ | |
| | | msg) | |
| | | // { | |
| | | // bool load_okay = true; | |
| | | // | |
| | | // | |
| | | // std::string f_header; | |
| | | // f >> f_header; | |
| | | // | |
| | | // if(f_header == "P5") | |
| | | // { | |
| | | // uword f_n_rows = 0; | |
| | | // uword f_n_cols = 0; | |
| | | // int f_maxval = 0; | |
| | | // | |
| | | // diskio::pnm_skip_comments(f); | |
| | | // | |
| | | // f >> f_n_cols; | |
| | | // diskio::pnm_skip_comments(f); | |
| | | // | |
| | | // f >> f_n_rows; | |
| | | // diskio::pnm_skip_comments(f); | |
| | | // | |
| | | // f >> f_maxval; //grabing a redundant size value | |
| | | // | |
| | | // f >> f_maxval; | |
| | | // f.get(); | |
| | | // | |
| | | // if( (f_maxval > 0) || (f_maxval <= 65535) ) | |
| | | // { | |
| | | // x.set_size(f_n_rows,f_n_cols); | |
| | | // | |
| | | // if(f_maxval <= 255) | |
| | | // { | |
| | | // const uword n_elem = f_n_cols*f_n_rows; | |
| | | // podarray<u8> tmp(n_elem); | |
| | | // | |
| | | // f.read( reinterpret_cast<char*>(tmp.memptr()), std::streamsize(n | |
| | | _elem) ); | |
| | | // | |
| | | // uword i = 0; | |
| | | // | |
| | | // //cout << "f_n_cols = " << f_n_cols << endl; | |
| | | // //cout << "f_n_rows = " << f_n_rows << endl; | |
| | | // | |
| | | // | |
| | | // for(uword row=0; row < f_n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < f_n_cols; ++col) | |
| | | // { | |
| | | // x.at(row,col) = eT(tmp[i]); | |
| | | // ++i; | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // } | |
| | | // else | |
| | | // { | |
| | | // const uword n_elem = f_n_cols*f_n_rows; | |
| | | // podarray<u16> tmp(n_elem); | |
| | | // | |
| | | // f.read( reinterpret_cast<char *>(tmp.memptr()), std::streamsize( | |
| | | n_elem*2) ); | |
| | | // | |
| | | // uword i = 0; | |
| | | // | |
| | | // for(uword row=0; row < f_n_rows; ++row) | |
| | | // { | |
| | | // for(uword col=0; col < f_n_cols; ++col) | |
| | | // { | |
| | | // x.at(row,col) = eT(tmp[i]); | |
| | | // ++i; | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // } | |
| | | // | |
| | | // } | |
| | | // else | |
| | | // { | |
| | | // load_okay = false; | |
| | | // err_msg = "currently no code available to handle loading "; | |
| | | // } | |
| | | // | |
| | | // if(f.good() == false) | |
| | | // { | |
| | | // load_okay = false; | |
| | | // } | |
| | | // } | |
| | | // else | |
| | | // { | |
| | | // load_okay = false; | |
| | | // err_msg = "unsupported header in "; | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| //! Load a PGM greyscale image as a matrix | | // //! Load a PGM greyscale image as a matrix | |
| template<typename T> | | // template<typename T> | |
| inline | | // inline | |
| bool | | // bool | |
| diskio::load_pgm_binary(SpMat< std::complex<T> >& x, std::istream& is, std: | | // diskio::load_pgm_binary(SpMat< std::complex<T> >& x, const std::string& | |
| :string& err_msg) | | name, std::string& err_msg) | |
| { | | // { | |
| arma_extra_debug_sigprint(); | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // uchar_mat tmp; | |
| | | // const bool load_okay = diskio::load_pgm_binary(tmp, name, err_msg); | |
| | | // | |
| | | // x = conv_to< Mat< std::complex<T> > >::from(tmp); // TODO: BUG | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| uchar_mat tmp; | | // //! Load a PGM greyscale image as a matrix | |
| const bool load_okay = diskio::load_pgm_binary(tmp, is, err_msg); | | // template<typename T> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_pgm_binary(SpMat< std::complex<T> >& x, std::istream& is, s | |
| | | td::string& err_msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // uchar_mat tmp; | |
| | | // const bool load_okay = diskio::load_pgm_binary(tmp, is, err_msg); | |
| | | // | |
| | | // x = conv_to< Mat< std::complex<T> > >::from(tmp); // TODO: BUG | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| x = conv_to< Mat< std::complex<T> > >::from(tmp); | | // //! Try to load a matrix by automatically determining its type | |
| | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_auto_detect(SpMat<eT>& x, const std::string& name, std::str | |
| | | ing& err_msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // std::fstream f; | |
| | | // f.open(name.c_str(), std::fstream::in | std::fstream::binary); | |
| | | // | |
| | | // bool load_okay = f.is_open(); | |
| | | // | |
| | | // if(load_okay == true) | |
| | | // { | |
| | | // load_okay = diskio::load_auto_detect(x, f, err_msg); | |
| | | // f.close(); | |
| | | // } | |
| | | // | |
| | | // return load_okay; | |
| | | // } | |
| | | | |
|
| return load_okay; | | // //! Try to load a matrix by automatically determining its type | |
| } | | // template<typename eT> | |
| | | // inline | |
| | | // bool | |
| | | // diskio::load_auto_detect(SpMat<eT>& x, std::istream& f, std::string& err | |
| | | _msg) | |
| | | // { | |
| | | // arma_extra_debug_sigprint(); | |
| | | // | |
| | | // static const std::string ARMA_SPM_TXT = "ARMA_SPM_TXT"; | |
| | | // static const std::string ARMA_SPM_BIN = "ARMA_SPM_BIN"; | |
| | | // static const std::string P5 = "P5"; | |
| | | // | |
| | | // podarray<char> raw_header(ARMA_SPM_TXT.length() + 1); | |
| | | // | |
| | | // std::streampos pos = f.tellg(); | |
| | | // | |
| | | // f.read( raw_header.memptr(), std::streamsize(ARMA_SPM_TXT.length()) ); | |
| | | // raw_header[ARMA_SPM_TXT.length()] = '\0'; | |
| | | // | |
| | | // f.clear(); | |
| | | // f.seekg(pos); | |
| | | // | |
| | | // const std::string header = raw_header.mem; | |
| | | // | |
| | | // if(ARMA_SPM_TXT == header.substr(0,ARMA_SPM_TXT.length())) | |
| | | // { | |
| | | // return load_arma_ascii(x, f, err_msg); | |
| | | // } | |
| | | // else | |
| | | // if(ARMA_SPM_BIN == header.substr(0,ARMA_SPM_BIN.length())) | |
| | | // { | |
| | | // return load_arma_binary(x, f, err_msg); | |
| | | // } | |
| | | // else | |
| | | // if(P5 == header.substr(0,P5.length())) | |
| | | // { | |
| | | // return load_pgm_binary(x, f, err_msg); | |
| | | // } | |
| | | // else | |
| | | // { | |
| | | // const file_type ft = guess_file_type(f); | |
| | | // | |
| | | // switch(ft) | |
| | | // { | |
| | | // case csv_ascii: | |
| | | // return load_csv_ascii(x, f, err_msg); | |
| | | // break; | |
| | | // | |
| | | // case raw_binary: | |
| | | // return load_raw_binary(x, f, err_msg); | |
| | | // break; | |
| | | // | |
| | | // case raw_ascii: | |
| | | // return load_raw_ascii(x, f, err_msg); | |
| | | // break; | |
| | | // | |
| | | // default: | |
| | | // err_msg = "unknown data in "; | |
| | | // return false; | |
| | | // } | |
| | | // } | |
| | | // | |
| | | // return false; | |
| | | // } | |
| | | | |
|
| //! Try to load a matrix by automatically determining its type | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::load_auto_detect(SpMat<eT>& x, const std::string& name, std::string
& err_msg) | | diskio::load_coord_ascii(SpMat<eT>& x, const std::string& name, std::string
& err_msg) | |
| { | | { | |
| arma_extra_debug_sigprint(); | | arma_extra_debug_sigprint(); | |
| | | | |
| std::fstream f; | | std::fstream f; | |
| f.open(name.c_str(), std::fstream::in | std::fstream::binary); | | f.open(name.c_str(), std::fstream::in | std::fstream::binary); | |
| | | | |
| bool load_okay = f.is_open(); | | bool load_okay = f.is_open(); | |
| | | | |
| if(load_okay == true) | | if(load_okay == true) | |
| { | | { | |
|
| load_okay = diskio::load_auto_detect(x, f, err_msg); | | load_okay = diskio::load_coord_ascii(x, f, err_msg); | |
| f.close(); | | f.close(); | |
| } | | } | |
| | | | |
| return load_okay; | | return load_okay; | |
| } | | } | |
| | | | |
|
| //! Try to load a matrix by automatically determining its type | | | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
|
| diskio::load_auto_detect(SpMat<eT>& x, std::istream& f, std::string& err_ms
g) | | diskio::load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_ms
g) | |
| { | | { | |
|
| arma_extra_debug_sigprint(); | | uword r,c,nz; | |
| | | | |
| static const std::string ARMA_SPM_TXT = "ARMA_SPM_TXT"; | | | |
| static const std::string ARMA_SPM_BIN = "ARMA_SPM_BIN"; | | | |
| static const std::string P5 = "P5"; | | | |
| | | | |
| podarray<char> raw_header(ARMA_SPM_TXT.length() + 1); | | | |
| | | | |
| std::streampos pos = f.tellg(); | | | |
| | | | |
|
| f.read( raw_header.memptr(), std::streamsize(ARMA_SPM_TXT.length()) ); | | f >> r; f >> c; f >> nz; //Grab the header | |
| raw_header[ARMA_SPM_TXT.length()] = '\0'; | | x.set_size(r,c); | |
| | | | |
|
| f.clear(); | | for(size_t i = 0; i < nz; i++) | |
| f.seekg(pos); | | | |
| | | | |
| const std::string header = raw_header.mem; | | | |
| | | | |
| if(ARMA_SPM_TXT == header.substr(0,ARMA_SPM_TXT.length())) | | | |
| { | | | |
| return load_arma_ascii(x, f, err_msg); | | | |
| } | | | |
| else | | | |
| if(ARMA_SPM_BIN == header.substr(0,ARMA_SPM_BIN.length())) | | | |
| { | | | |
| return load_arma_binary(x, f, err_msg); | | | |
| } | | | |
| else | | | |
| if(P5 == header.substr(0,P5.length())) | | | |
| { | | | |
| return load_pgm_binary(x, f, err_msg); | | | |
| } | | | |
| else | | | |
| { | | { | |
|
| const file_type ft = guess_file_type(f); | | eT data; | |
| | | uword row, col; | |
| | | | |
|
| switch(ft) | | f >> row; f >> col; f >> data; | |
| { | | | |
| case csv_ascii: | | | |
| return load_csv_ascii(x, f, err_msg); | | | |
| break; | | | |
| | | | |
| case raw_binary: | | | |
| return load_raw_binary(x, f, err_msg); | | | |
| break; | | | |
| | | | |
| case raw_ascii: | | | |
| return load_raw_ascii(x, f, err_msg); | | | |
| break; | | | |
| | | | |
| default: | | | |
| err_msg = "unknown data in "; | | | |
| return false; | | | |
| } | | | |
| } | | | |
| | | | |
| return false; | | | |
| } | | | |
| | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_coord_ascii(SpMat<eT>& x, const std::string& name, std::string | | | |
| & err_msg) | | | |
| { | | | |
| arma_extra_debug_sigprint(); | | | |
| | | | |
| std::fstream f; | | | |
| f.open(name.c_str(), std::fstream::in | std::fstream::binary); | | | |
| | | | |
| bool load_okay = f.is_open(); | | | |
| | | | |
|
| if(load_okay == true) | | x.at(row,col) = data; | |
| { | | | |
| load_okay = diskio::load_coord_ascii(x, f, err_msg); | | | |
| f.close(); | | | |
| } | | } | |
| | | | |
|
| return load_okay; | | return f.good(); | |
| } | | | |
| | | | |
| template<typename eT> | | | |
| inline | | | |
| bool | | | |
| diskio::load_coord_ascii(SpMat<eT>& x, std::istream& f, std::string& err_ms | | | |
| g) | | | |
| { | | | |
| uword r,c,nz; | | | |
| f >> r; f >> c; f >> nz; //Grab the header | | | |
| x.set_size(r,c); | | | |
| for(size_t i = 0; i < nz; i++) | | | |
| { | | | |
| eT data; | | | |
| uword row, col; | | | |
| f >> row; f >> col; f >> data; | | | |
| x.at(row,col) = data; | | | |
| } | | | |
| return f.good(); | | | |
| } | | } | |
| | | | |
| // cubes | | // cubes | |
| | | | |
| //! Save a cube as raw text (no header, human readable). | | //! Save a cube as raw text (no header, human readable). | |
| template<typename eT> | | template<typename eT> | |
| inline | | inline | |
| bool | | bool | |
| diskio::save_raw_ascii(const Cube<eT>& x, const std::string& final_name) | | diskio::save_raw_ascii(const Cube<eT>& x, const std::string& final_name) | |
| { | | { | |
| | | | |
End of changes. 71 change blocks. |
| 907 lines changed or deleted | | 956 lines changed or added | |
|