Boost logo

Ublas :

From: Janek Kozicki (janek_listy_at_[hidden])
Date: 2006-02-21 04:54:44


in case anybody will need this when googling, the ugly looking method
for boost serialization of ublas vector and matrix:

        private :
                friend class boost::serialization::access;
                template<class Archive>
                void save(Archive & ar, const unsigned int version) const
                {
                        std::vector<std::vector<double> > l; // copy here ublas vector
                        std::vector<std::vector<std::vector<double> > > w; // copy here ublas matrix

                        l.resize(layers.size());
                        for(int i=0 ; i<layers.size() ; ++i)
                        {
                                l[i].resize(layers[i].size());
                                for(int j=0 ; j<layers[i].size() ; ++j)
                                        l[i][j] = layers[i](j);
                        }

                        w.resize(weights.size());
                        for(int i=0 ; i<weights.size() ; ++i)
                        {
                                w[i].resize(weights[i].size1());
                                for(int j=0 ; j<weights[i].size1() ; ++j)
                                {
                                        w[i][j].resize(weights[i].size2());
                                        for(int k=0 ; k<weights[i].size2() ; ++k)
                                                w[i][j][k] = weights[i](j,k);
                                }
                        }
                        ar & l;
                        ar & w;
                }
                template<class Archive>
                void load(Archive & ar, const unsigned int version)
                {
                        std::vector<std::vector<double> > l;
                        std::vector<std::vector<std::vector<double> > > w;
                        
                        ar & l;
                        ar & w;

                        layers.resize(l.size());
                        for(int i=0 ; i<layers.size() ; ++i)
                        {
                                layers[i].resize(l[i].size());
                                for(int j=0 ; j<layers[i].size() ; ++j)
                                        layers[i](j) = l[i][j];
                        }

                        weights.resize(w.size());
                        for(int i=0 ; i<weights.size() ; ++i)
                        {
                                weights[i].resize(w[i].size(),w[i][0].size());
                                for(int j=0 ; j<weights[i].size1() ; ++j)
                                        for(int k=0 ; k<weights[i].size2() ; ++k)
                                                weights[i](j,k) = w[i][j][k];
                        }
                }
                BOOST_SERIALIZATION_SPLIT_MEMBER()

-- 
Janek Kozicki                                                         |