Col_meat.hpp   Col_meat.hpp 
skipping to change at line 132 skipping to change at line 132
} }
//! create a column vector from std::vector //! create a column vector from std::vector
template<typename eT> template<typename eT>
inline inline
Col<eT>::Col(const std::vector<eT>& x) Col<eT>::Col(const std::vector<eT>& x)
: Mat<eT>(arma_vec_indicator(), uword(x.size()), 1, 1) : Mat<eT>(arma_vec_indicator(), uword(x.size()), 1, 1)
{ {
arma_extra_debug_sigprint_this(this); arma_extra_debug_sigprint_this(this);
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) ); if(x.size() > 0)
{
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) );
}
} }
//! create a column vector from std::vector //! create a column vector from std::vector
template<typename eT> template<typename eT>
inline inline
const Col<eT>& const Col<eT>&
Col<eT>::operator=(const std::vector<eT>& x) Col<eT>::operator=(const std::vector<eT>& x)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
Mat<eT>::init_warm(uword(x.size()), 1); Mat<eT>::init_warm(uword(x.size()), 1);
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) ); if(x.size() > 0)
{
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) );
}
return *this; return *this;
} }
#if defined(ARMA_USE_CXX11) #if defined(ARMA_USE_CXX11)
template<typename eT> template<typename eT>
inline inline
Col<eT>::Col(const std::initializer_list<eT>& list) Col<eT>::Col(const std::initializer_list<eT>& list)
{ {
 End of changes. 2 change blocks. 
2 lines changed or deleted 8 lines changed or added


 Mat_bones.hpp   Mat_bones.hpp 
// Copyright (C) 2008-2012 NICTA (www.nicta.com.au) // Copyright (C) 2008-2012 NICTA (www.nicta.com.au)
// Copyright (C) 2008-2012 Conrad Sanderson // Copyright (C) 2008-2012 Conrad Sanderson
// Copyright (C) 2012 Ryan Curtin
// //
// This file is part of the Armadillo C++ library. // This file is part of the Armadillo C++ library.
// It is provided without any warranty of fitness // It is provided without any warranty of fitness
// for any purpose. You can redistribute this file // for any purpose. You can redistribute this file
// and/or modify it under the terms of the GNU // and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published // Lesser General Public License (LGPL) as published
// by the Free Software Foundation, either version 3 // by the Free Software Foundation, either version 3
// of the License or (at your option) any later version. // of the License or (at your option) any later version.
// (see http://www.opensource.org/licenses for more info) // (see http://www.opensource.org/licenses for more info)
 End of changes. 1 change blocks. 
0 lines changed or deleted 1 lines changed or added


 Mat_meat.hpp   Mat_meat.hpp 
// Copyright (C) 2008-2012 NICTA (www.nicta.com.au) // Copyright (C) 2008-2012 NICTA (www.nicta.com.au)
// Copyright (C) 2008-2012 Conrad Sanderson // Copyright (C) 2008-2012 Conrad Sanderson
// Copyright (C) 2012 Ryan Curtin
// //
// This file is part of the Armadillo C++ library. // This file is part of the Armadillo C++ library.
// It is provided without any warranty of fitness // It is provided without any warranty of fitness
// for any purpose. You can redistribute this file // for any purpose. You can redistribute this file
// and/or modify it under the terms of the GNU // and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published // Lesser General Public License (LGPL) as published
// by the Free Software Foundation, either version 3 // by the Free Software Foundation, either version 3
// of the License or (at your option) any later version. // of the License or (at your option) any later version.
// (see http://www.opensource.org/licenses for more info) // (see http://www.opensource.org/licenses for more info)
skipping to change at line 426 skipping to change at line 427
, n_cols(1) , n_cols(1)
, n_elem(uword(x.size())) , n_elem(uword(x.size()))
, vec_state(0) , vec_state(0)
, mem_state(0) , mem_state(0)
, mem() , mem()
{ {
arma_extra_debug_sigprint_this(this); arma_extra_debug_sigprint_this(this);
init_cold(); init_cold();
arrayops::copy( memptr(), &(x[0]), uword(x.size()) ); if(n_elem > 0)
{
arrayops::copy( memptr(), &(x[0]), n_elem );
}
} }
//! create the matrix from std::vector //! create the matrix from std::vector
template<typename eT> template<typename eT>
inline inline
const Mat<eT>& const Mat<eT>&
Mat<eT>::operator=(const std::vector<eT>& x) Mat<eT>::operator=(const std::vector<eT>& x)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
init_warm(uword(x.size()), 1); init_warm(uword(x.size()), 1);
arrayops::copy( memptr(), &(x[0]), uword(x.size()) ); if(x.size() > 0)
{
arrayops::copy( memptr(), &(x[0]), uword(x.size()) );
}
return *this; return *this;
} }
#if defined(ARMA_USE_CXX11) #if defined(ARMA_USE_CXX11)
template<typename eT> template<typename eT>
inline inline
Mat<eT>::Mat(const std::initializer_list<eT>& list) Mat<eT>::Mat(const std::initializer_list<eT>& list)
: n_rows(0) : n_rows(0)
 End of changes. 3 change blocks. 
2 lines changed or deleted 9 lines changed or added


 Row_meat.hpp   Row_meat.hpp 
skipping to change at line 108 skipping to change at line 108
} }
//! create a row vector from std::vector //! create a row vector from std::vector
template<typename eT> template<typename eT>
inline inline
Row<eT>::Row(const std::vector<eT>& x) Row<eT>::Row(const std::vector<eT>& x)
: Mat<eT>(arma_vec_indicator(), 1, uword(x.size()), 2) : Mat<eT>(arma_vec_indicator(), 1, uword(x.size()), 2)
{ {
arma_extra_debug_sigprint_this(this); arma_extra_debug_sigprint_this(this);
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) ); if(x.size() > 0)
{
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) );
}
} }
//! create a row vector from std::vector //! create a row vector from std::vector
template<typename eT> template<typename eT>
inline inline
const Row<eT>& const Row<eT>&
Row<eT>::operator=(const std::vector<eT>& x) Row<eT>::operator=(const std::vector<eT>& x)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
Mat<eT>::init_warm(1, uword(x.size())); Mat<eT>::init_warm(1, uword(x.size()));
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) ); if(x.size() > 0)
{
arrayops::copy( Mat<eT>::memptr(), &(x[0]), uword(x.size()) );
}
return *this; return *this;
} }
#if defined(ARMA_USE_CXX11) #if defined(ARMA_USE_CXX11)
template<typename eT> template<typename eT>
inline inline
Row<eT>::Row(const std::initializer_list<eT>& list) Row<eT>::Row(const std::initializer_list<eT>& list)
{ {
 End of changes. 2 change blocks. 
2 lines changed or deleted 8 lines changed or added


 SpCol_meat.hpp   SpCol_meat.hpp 
skipping to change at line 485 skipping to change at line 485
typename SpCol<eT>::const_row_iterator typename SpCol<eT>::const_row_iterator
SpCol<eT>::end_row(const uword row_num) const SpCol<eT>::end_row(const uword row_num) const
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
arma_debug_check( (row_num >= SpMat<eT>::n_rows), "end_row(): index out o f bounds"); arma_debug_check( (row_num >= SpMat<eT>::n_rows), "end_row(): index out o f bounds");
return const_row_iterator(*this, row_num + 1, 0); return const_row_iterator(*this, row_num + 1, 0);
} }
#ifdef ARMA_EXTRA_COL_MEAT #ifdef ARMA_EXTRA_SPCOL_MEAT
#include ARMA_INCFILE_WRAP(ARMA_EXTRA_COL_MEAT) #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPCOL_MEAT)
#endif #endif
//! @} //! @}
 End of changes. 1 change blocks. 
2 lines changed or deleted 2 lines changed or added


 SpMat_bones.hpp   SpMat_bones.hpp 
skipping to change at line 499 skipping to change at line 499
*/ */
inline arma_hot void delete_element(const uword in_row, const uword in_co l); inline arma_hot void delete_element(const uword in_row, const uword in_co l);
public: public:
#ifdef ARMA_EXTRA_SPMAT_PROTO #ifdef ARMA_EXTRA_SPMAT_PROTO
#include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPMAT_PROTO) #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPMAT_PROTO)
#endif #endif
}; };
#define ARMA_HAS_SPMAT
//! @} //! @}
 End of changes. 1 change blocks. 
0 lines changed or deleted 2 lines changed or added


 SpMat_meat.hpp   SpMat_meat.hpp 
skipping to change at line 278 skipping to change at line 278
template<typename eT> template<typename eT>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator*=(const SpMat<eT>& y) SpMat<eT>::operator*=(const SpMat<eT>& y)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication"); arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication");
SpMat z; SpMat<eT> z;
z = (*this) * y; z = (*this) * y;
steal_mem(z); steal_mem(z);
return *this; return *this;
} }
// This is in-place element-wise matrix multiplication. // This is in-place element-wise matrix multiplication.
template<typename eT> template<typename eT>
inline inline
const SpMat<eT>& const SpMat<eT>&
skipping to change at line 312 skipping to change at line 312
if ((it.row() == x_it.row()) && (it.col() == x_it.col())) if ((it.row() == x_it.row()) && (it.col() == x_it.col()))
{ {
// There is an element at this place in both matrices. Multiply. // There is an element at this place in both matrices. Multiply.
(*it) *= (*x_it); (*it) *= (*x_it);
// Now move on to the next position. // Now move on to the next position.
it++; it++;
x_it++; x_it++;
} }
else if ((it.col() <= x_it.col()) && (it.row() < x_it.row())) else if ((it.col() < x_it.col()) || ((it.col() == x_it.col()) && (it.ro w() < x_it.row())))
{ {
// This case is when our matrix has an element which the other matrix does not. // This case is when our matrix has an element which the other matrix does not.
// So we must delete this element. // So we must delete this element.
(*it) = 0; (*it) = 0;
// Because we have deleted the element, we now have to manually set t he position... // Because we have deleted the element, we now have to manually set t he position...
it.internal_pos--; it.internal_pos--;
// Now we can increment our iterator. // Now we can increment our iterator.
it++; it++;
skipping to change at line 490 skipping to change at line 490
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator=(const Base<eT, T1>& x) SpMat<eT>::operator=(const Base<eT, T1>& x)
{ {
//No easy init function, will have to generate matrix manually. //No easy init function, will have to generate matrix manually.
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
const Proxy<T1> p(x.get_ref()); const Proxy<T1> p(x.get_ref());
const uword x_n_rows = p.get_n_rows(); const uword x_n_rows = p.get_n_rows();
const uword x_n_cols = p.get_n_cols(); const uword x_n_cols = p.get_n_cols();
const uword x_n_elem = p.get_n_elem();
init(x_n_rows, x_n_cols); init(x_n_rows, x_n_cols);
if(Proxy<T1>::prefer_at_accessor == true) for(uword j = 0; j < x_n_cols; ++j)
for(uword i = 0; i < x_n_rows; ++i)
{ {
for(uword j = 0; j < x_n_cols; ++j) at(i,j) = p.at(i,j); // let SpValProxy handle 0's.
{
for(uword i = 0; i < x_n_rows; ++i)
{
at(i) = p.at(i, j); // Let the proxy handle 0's.
}
}
}
else
{
for(uword i = 0; i < x_n_elem; ++i)
{
at(i) = p[i];
}
} }
return *this; return *this;
} }
template<typename eT> template<typename eT>
template<typename T1> template<typename T1>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator*=(const Base<eT, T1>& y) SpMat<eT>::operator*=(const Base<eT, T1>& y)
skipping to change at line 554 skipping to change at line 541
} }
// Now count the number of rows which have nonzero elements. // Now count the number of rows which have nonzero elements.
uword nonzero_rows = 0; uword nonzero_rows = 0;
while(last_index != n_rows + 1) while(last_index != n_rows + 1)
{ {
++nonzero_rows; ++nonzero_rows;
last_index = index[last_index]; last_index = index[last_index];
} }
SpMat z(n_rows, p.get_n_cols()); SpMat<eT> z(n_rows, p.get_n_cols());
z.mem_resize(nonzero_rows * p.get_n_cols()); // upper bound on size z.mem_resize(nonzero_rows * p.get_n_cols()); // upper bound on size
// Now we have to fill all the elements using a modification of the NUMBM M algorithm. // Now we have to fill all the elements using a modification of the NUMBM M algorithm.
uword cur_pos = 0; uword cur_pos = 0;
podarray<eT> partial_sums(n_rows); podarray<eT> partial_sums(n_rows);
partial_sums.zeros(); partial_sums.zeros();
for(uword col = 0; col < n_cols; ++col) for(uword col = 0; col < n_cols; ++col)
skipping to change at line 623 skipping to change at line 610
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
const Proxy<T1> p(x.get_ref()); const Proxy<T1> p(x.get_ref());
/** /**
* Don't use this function. It's not mathematically well-defined and was tes * Don't use this function. It's not mathematically well-defined and was tes
* cycles to trash all your data. This is dumb. * cycles to trash all your data. This is dumb.
*/ */
arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( ), "element-wise division"); arma_debug_assert_same_size(n_rows, n_cols, p.get_n_rows(), p.get_n_cols( ), "element-wise division");
if(Proxy<T1>::prefer_at_accessor == true) for(uword j = 0; j < n_cols; j++)
for(uword i = 0; i < n_rows; i++)
{ {
for(uword j = 0; j < n_cols; j++) at(i, j) /= p.at(i, j);
{
for(uword i = 0; i < n_rows; i++)
{
at(i, j) /= p.at(i, j);
}
}
}
else
{
for(uword i = 0; i < n_elem; ++i)
{
at(i) /= p[i];
}
} }
return *this; return *this;
} }
template<typename eT> template<typename eT>
template<typename T1> template<typename T1>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator%=(const Base<eT, T1>& x) SpMat<eT>::operator%=(const Base<eT, T1>& x)
skipping to change at line 823 skipping to change at line 798
template<typename eT> template<typename eT>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator*=(const SpSubview<eT>& y) SpMat<eT>::operator*=(const SpSubview<eT>& y)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication"); arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication");
// Cannot be done in-place (easily). // Cannot be done in-place (easily).
SpMat z = (*this) * y; SpMat<eT> z = (*this) * y;
steal_mem(z); steal_mem(z);
return *this; return *this;
} }
template<typename eT> template<typename eT>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator%=(const SpSubview<eT>& x) SpMat<eT>::operator%=(const SpSubview<eT>& x)
{ {
skipping to change at line 979 skipping to change at line 954
template<typename eT> template<typename eT>
inline inline
const SpMat<eT>& const SpMat<eT>&
SpMat<eT>::operator*=(const subview<eT>& y) SpMat<eT>::operator*=(const subview<eT>& y)
{ {
arma_extra_debug_sigprint(); arma_extra_debug_sigprint();
arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication"); arma_debug_assert_mul_size(n_rows, n_cols, y.n_rows, y.n_cols, "matrix mu ltiplication");
SpMat z(n_rows, y.n_cols); SpMat<eT> z(n_rows, y.n_cols);
// Performed in the same fashion as operator*=(SpMat). // Performed in the same fashion as operator*=(SpMat).
for (const_row_iterator x_row_it = begin_row(); x_row_it.pos() < n_nonzer o; ++x_row_it) for (const_row_iterator x_row_it = begin_row(); x_row_it.pos() < n_nonzer o; ++x_row_it)
{ {
for (uword col = 0; col < y.n_cols; ++col) for (uword col = 0; col < y.n_cols; ++col)
{ {
// At this moment in the loop, we are calculating anything that is co ntributed to by *x_row_it and *y_col_it. // At this moment in the loop, we are calculating anything that is co ntributed to by *x_row_it and *y_col_it.
// Given that our position is x_ab and y_bc, there will only be a con tribution if x.col == y.row, and that // Given that our position is x_ab and y_bc, there will only be a con tribution if x.col == y.row, and that
// contribution will be in location z_ac. // contribution will be in location z_ac.
z(x_row_it.row, col) += (*x_row_it) * y.at(x_row_it.col, col); z.at(x_row_it.row, col) += (*x_row_it) * y.at(x_row_it.col, col);
} }
} }
steal_mem(z); steal_mem(z);
return *this; return *this;
} }
template<typename eT> template<typename eT>
inline inline
 End of changes. 11 change blocks. 
37 lines changed or deleted 12 lines changed or added


 SpRow_bones.hpp   SpRow_bones.hpp 
skipping to change at line 80 skipping to change at line 80
typedef typename SpMat<eT>::iterator row_iterator; typedef typename SpMat<eT>::iterator row_iterator;
typedef typename SpMat<eT>::const_iterator const_row_iterator; typedef typename SpMat<eT>::const_iterator const_row_iterator;
inline row_iterator begin_row(); inline row_iterator begin_row();
inline const_row_iterator begin_row() const; inline const_row_iterator begin_row() const;
inline row_iterator end_row(); inline row_iterator end_row();
inline const_row_iterator end_row() const; inline const_row_iterator end_row() const;
#ifdef ARMA_EXTRA_ROW_PROTO #ifdef ARMA_EXTRA_SPROW_PROTO
#include ARMA_INCFILE_WRAP(ARMA_EXTRA_ROW_PROTO) #include ARMA_INCFILE_WRAP(ARMA_EXTRA_SPROW_PROTO)
#endif #endif
}; };
//! @} //! @}
 End of changes. 1 change blocks. 
2 lines changed or deleted 2 lines changed or added


 SpSubview_bones.hpp   SpSubview_bones.hpp 
skipping to change at line 84 skipping to change at line 84
inline static void minus_inplace(Mat<eT>& out, const subview& in); inline static void minus_inplace(Mat<eT>& out, const subview& in);
inline static void schur_inplace(Mat<eT>& out, const subview& in); inline static void schur_inplace(Mat<eT>& out, const subview& in);
inline static void div_inplace(Mat<eT>& out, const subview& in); inline static void div_inplace(Mat<eT>& out, const subview& in);
*/ */
inline void fill(const eT val); inline void fill(const eT val);
inline void zeros(); inline void zeros();
inline void ones(); inline void ones();
inline void eye(); inline void eye();
inline SpValProxy<SpSubview<eT> > operator[](const uword i); arma_hot inline SpValProxy<SpSubview<eT> > operator[](const uword i);
inline eT operator[](const uword i) const; arma_hot inline eT operator[](const uword i) cons
t;
inline SpValProxy<SpSubview<eT> > operator()(const uword i); arma_hot inline SpValProxy<SpSubview<eT> > operator()(const uword i);
inline eT operator()(const uword i) const; arma_hot inline eT operator()(const uword i) cons
t;
inline SpValProxy<SpSubview<eT> > operator()(const uword in_row, const uw arma_hot inline SpValProxy<SpSubview<eT> > operator()(const uword in_row,
ord in_col); const uword in_col);
inline eT operator()(const uword in_row, const uw arma_hot inline eT operator()(const uword in_row,
ord in_col) const; const uword in_col) const;
inline SpValProxy<SpSubview<eT> > at(const uword i); arma_hot inline SpValProxy<SpSubview<eT> > at(const uword i);
inline eT at(const uword i) const; arma_hot inline eT at(const uword i) const;
inline SpValProxy<SpSubview<eT> > at(const uword in_row, const uword in_c arma_hot inline SpValProxy<SpSubview<eT> > at(const uword in_row, const u
ol); word in_col);
inline eT at(const uword in_row, const uword in_c arma_hot inline eT at(const uword in_row, const u
ol) const; word in_col) const;
inline bool check_overlap(const SpSubview& x) const; inline bool check_overlap(const SpSubview& x) const;
inline bool is_vec() const; inline bool is_vec() const;
/* not yet /* not yet
inline SpSubview_row<eT> row(const uword row_num); inline SpSubview_row<eT> row(const uword row_num);
inline const SpSubview_row<eT> row(const uword row_num) const; inline const SpSubview_row<eT> row(const uword row_num) const;
inline SpSubview_row<eT> operator()(const uword row_num, const span& col_span); inline SpSubview_row<eT> operator()(const uword row_num, const span& col_span);
 End of changes. 5 change blocks. 
14 lines changed or deleted 16 lines changed or added


 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


 arma_config.hpp   arma_config.hpp 
skipping to change at line 84 skipping to change at line 84
#else #else
static const bool good_comp = false; static const bool good_comp = false;
#endif #endif
#if ( \ #if ( \
defined(ARMA_EXTRA_MAT_PROTO) || defined(ARMA_EXTRA_MAT_MEAT) \ defined(ARMA_EXTRA_MAT_PROTO) || defined(ARMA_EXTRA_MAT_MEAT) \
|| defined(ARMA_EXTRA_COL_PROTO) || defined(ARMA_EXTRA_COL_MEAT) \ || defined(ARMA_EXTRA_COL_PROTO) || defined(ARMA_EXTRA_COL_MEAT) \
|| defined(ARMA_EXTRA_ROW_PROTO) || defined(ARMA_EXTRA_ROW_MEAT) \ || defined(ARMA_EXTRA_ROW_PROTO) || defined(ARMA_EXTRA_ROW_MEAT) \
|| defined(ARMA_EXTRA_CUBE_PROTO) || defined(ARMA_EXTRA_CUBE_MEAT) \ || defined(ARMA_EXTRA_CUBE_PROTO) || defined(ARMA_EXTRA_CUBE_MEAT) \
|| defined(ARMA_EXTRA_FIELD_PROTO) || defined(ARMA_EXTRA_FIELD_MEAT) \ || defined(ARMA_EXTRA_FIELD_PROTO) || defined(ARMA_EXTRA_FIELD_MEAT) \
|| defined(ARMA_EXTRA_SPMAT_PROTO) || defined(ARMA_EXTRA_SPMAT_MEAT)
\
|| defined(ARMA_EXTRA_SPCOL_PROTO) || defined(ARMA_EXTRA_SPCOL_MEAT)
\
|| defined(ARMA_EXTRA_SPROW_PROTO) || defined(ARMA_EXTRA_SPROW_MEAT)
\
) )
static const bool extra_code = true; static const bool extra_code = true;
#else #else
static const bool extra_code = false; static const bool extra_code = false;
#endif #endif
}; };
//! @} //! @}
 End of changes. 1 change blocks. 
0 lines changed or deleted 6 lines changed or added


 arma_version.hpp   arma_version.hpp 
skipping to change at line 17 skipping to change at line 17
// and/or modify it under the terms of the GNU // and/or modify it under the terms of the GNU
// Lesser General Public License (LGPL) as published // Lesser General Public License (LGPL) as published
// by the Free Software Foundation, either version 3 // by the Free Software Foundation, either version 3
// of the License or (at your option) any later version. // of the License or (at your option) any later version.
// (see http://www.opensource.org/licenses for more info) // (see http://www.opensource.org/licenses for more info)
//! \addtogroup arma_version //! \addtogroup arma_version
//! @{ //! @{
#define ARMA_VERSION_MAJOR 3 #define ARMA_VERSION_MAJOR 3
#define ARMA_VERSION_MINOR 3 #define ARMA_VERSION_MINOR 4
#define ARMA_VERSION_PATCH 92 #define ARMA_VERSION_PATCH 0
#define ARMA_VERSION_NAME "v3.4 beta 2; for testing use only" #define ARMA_VERSION_NAME "Ku De Ta"
struct arma_version struct arma_version
{ {
static const unsigned int major = ARMA_VERSION_MAJOR; static const unsigned int major = ARMA_VERSION_MAJOR;
static const unsigned int minor = ARMA_VERSION_MINOR; static const unsigned int minor = ARMA_VERSION_MINOR;
static const unsigned int patch = ARMA_VERSION_PATCH; static const unsigned int patch = ARMA_VERSION_PATCH;
static static
inline inline
std::string std::string
 End of changes. 1 change blocks. 
3 lines changed or deleted 3 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

This html diff was produced by rfcdiff 1.41. The latest version is available from http://tools.ietf.org/tools/rfcdiff/