TEST(H5Handle_Test, QuickMatrixTest) { Mat<float> mymat = ones<Mat<float> >(5,3); Mat<float>::iterator i; int count = 0; for (i = mymat.begin(); i != mymat.end(); i++) { *i = count++; } // mymat.raw_print(cout); H5File::WriteMatrix("tmp.h5:/path/to/mat", mymat); Mat<float> m; H5File::ReadMatrix("tmp.h5:/path/to/mat", m); Mat<float>::iterator gold, test; for (gold = mymat.begin(), test = m.begin(); gold != mymat.end(); gold++, test++) { float g = *gold; float t = *test; EXPECT_EQ(t, g); } }
Mat<elem_type> diff(const Mat<elem_type>& X, size_type n = 1, size_type dim = 0) { static_assert(ARMA_VERSION_MAJOR <= 5 && ARMA_VERSION_MINOR < 400, "This function is deprecated. Use arma::diff instead."); assert(n > 0); Mat<elem_type> y; if (X.empty() || dim > 1 || X.n_elem == 1) return y; if (n > 1) return diff(diff(X, n - 1, dim)); if (X.is_vec()) { y.resize(std::max(X.n_rows - 1, (size_type)1), std::max(X.n_cols - 1, (size_type)1)); #ifdef _MSC_VER std::adjacent_difference(X.begin(), X.end(), stdext::unchecked_array_iterator<elem_type *>(y.begin())); #else std::adjacent_difference(X.begin(), X.end(), y.begin()); #endif return y; } switch (dim) { case 0: // row difference y.resize(X.n_rows - 1, X.n_cols); for (size_type c = 0 ; c < X.n_cols ; c++) #ifdef _MSC_VER std::adjacent_difference(X.begin_col(c), X.end_col(c), stdext::unchecked_array_iterator<elem_type *>(y.begin_col(c))); #else std::adjacent_difference(X.begin_col(c), X.end_col(c), y.begin_col(c)); #endif break; case 1: // column difference y.resize(X.n_rows, X.n_cols - 1); for (size_type c = 0 ; c < X.n_cols - 1; c++) y.col(c) = X.col(c + 1) - X.col(c); break; default: throw std::invalid_argument("dim should be 0 or 1"); // } return y; }
TEST(H5Handle_Test, MatrixTest) { Mat<float> mymat = ones<Mat<float> >(5,3); Mat<float>::iterator i; int count = 0; for (i = mymat.begin(); i != mymat.end(); i++) { *i = count++; } // mymat.raw_print(cout); { H5File file; file.SetFile("tmp.h5"); file.Open(true); H5DataSet *ds = file.CreateDataSet("/tmp/fun/mat", mymat, 0); //H5DataSet *ds = file.CreateDataSet("mat", mymat, 0); ds->WriteMatrix(mymat); ds->Close(); } { H5File file; file.SetFile("tmp.h5"); file.Open(); H5DataSet *ds = file.OpenDataSet("/tmp/fun/mat"); // H5DataSet *ds = file.OpenDataSet("mat"); Mat<float> m; ds->ReadMatrix(m); // m.raw_print(cout); Mat<float>::iterator gold, test; for (gold = mymat.begin(), test = m.begin(); gold != mymat.end(); gold++, test++) { float g = *gold; float t = *test; EXPECT_EQ(t, g); } ds->Close(); } }
inline bool op_find_unique::apply_helper(Mat<uword>& out, const Proxy<T1>& P, const bool ascending_indices) { arma_extra_debug_sigprint(); typedef typename T1::elem_type eT; const uword n_elem = P.get_n_elem(); if(n_elem == 0) { out.set_size(0,1); return true; } if(n_elem == 1) { out.set_size(1,1); out[0] = 0; return true; } uvec indices(n_elem); std::vector< arma_find_unique_packet<eT> > packet_vec(n_elem); if(Proxy<T1>::prefer_at_accessor == false) { typename Proxy<T1>::ea_type Pea = P.get_ea(); for(uword i=0; i<n_elem; ++i) { const eT val = Pea[i]; if(arma_isnan(val)) { return false; } packet_vec[i].val = val; packet_vec[i].index = i; } } else { const uword n_rows = P.get_n_rows(); const uword n_cols = P.get_n_cols(); uword i = 0; for(uword col=0; col < n_cols; ++col) for(uword row=0; row < n_rows; ++row) { const eT val = P.at(row,col); if(arma_isnan(val)) { return false; } packet_vec[i].val = val; packet_vec[i].index = i; ++i; } } arma_find_unique_comparator<eT> comparator; std::sort( packet_vec.begin(), packet_vec.end(), comparator ); uword* indices_mem = indices.memptr(); indices_mem[0] = packet_vec[0].index; uword count = 1; for(uword i=1; i < n_elem; ++i) { const eT diff = packet_vec[i-1].val - packet_vec[i].val; if(diff != eT(0)) { indices_mem[count] = packet_vec[i].index; ++count; } } out.steal_mem_col(indices,count); if(ascending_indices) { std::sort(out.begin(), out.end()); } return true; }