1 | // Example for supporting serialization in boost::numeric::ublas::symmetrix_matrix.
|
---|
2 | // =====================================================================
|
---|
3 | // In response to https://svn.boost.org/trac/boost/ticket/10045
|
---|
4 |
|
---|
5 |
|
---|
6 | // Author
|
---|
7 | // ==============
|
---|
8 | // Shivam Mittal
|
---|
9 | // Email - 2015csb1032@iitrpr.ac.in
|
---|
10 | // Github profile - github.com/shivam5
|
---|
11 | // Date - 2nd April 2017
|
---|
12 |
|
---|
13 |
|
---|
14 | // Compiling
|
---|
15 | // ==========
|
---|
16 | // g++ -I <path to modular_boost> symmetric_matrix_serialize.cpp -L<path to installed libraries (stage/lib)> -static -lboost_serialization
|
---|
17 |
|
---|
18 | // Example (my computer):
|
---|
19 | // g++ -I /home/shivam/Desktop/boost/modular_boost/ symmetric_matrix_serialize.cpp -L/home/shivam/Desktop/boost/modular_boost/stage/lib -static -lboost_serialization
|
---|
20 |
|
---|
21 | // Running
|
---|
22 | // ==========
|
---|
23 | // ./a.out
|
---|
24 |
|
---|
25 | #include <boost/numeric/ublas/io.hpp>
|
---|
26 | #include <boost/numeric/ublas/symmetric.hpp>
|
---|
27 | #include <iostream>
|
---|
28 | #include <fstream>
|
---|
29 | #include <string>
|
---|
30 | #include <boost/archive/text_iarchive.hpp>
|
---|
31 | #include <boost/archive/text_oarchive.hpp>
|
---|
32 |
|
---|
33 | using namespace boost::numeric::ublas;
|
---|
34 |
|
---|
35 | template <typename T, typename U>
|
---|
36 | void save_smatrix(const symmetric_matrix<T,U> &s, const char * filename){
|
---|
37 | std::ofstream ofs(filename);
|
---|
38 | boost::archive::text_oarchive oa(ofs);
|
---|
39 | oa << s;
|
---|
40 | }
|
---|
41 |
|
---|
42 | template <typename T, typename U>
|
---|
43 | void load_smatrix(symmetric_matrix<T,U> &s, const char * filename){
|
---|
44 | std::ifstream ifs(filename);
|
---|
45 | boost::archive::text_iarchive ia(ifs);
|
---|
46 | ia >> s;
|
---|
47 | }
|
---|
48 |
|
---|
49 | int main () {
|
---|
50 |
|
---|
51 | std::string filename = "filename";
|
---|
52 |
|
---|
53 | symmetric_matrix<double, lower> ml (3, 3);
|
---|
54 | for (unsigned i = 0; i < ml.size1 (); ++ i)
|
---|
55 | for (unsigned j = 0; j <= i; ++ j)
|
---|
56 | ml (i, j) = 3 * i + j;
|
---|
57 | symmetric_matrix<double, upper> mu (3, 3);
|
---|
58 | for (unsigned i = 0; i < mu.size1 (); ++ i)
|
---|
59 | for (unsigned j = i; j < mu.size2 (); ++ j)
|
---|
60 | mu (i, j) = 3 * i + j;
|
---|
61 |
|
---|
62 |
|
---|
63 | std::cout << ml << std::endl;
|
---|
64 | save_smatrix <double, lower> (ml, filename.c_str());
|
---|
65 | symmetric_matrix<double, lower> ml2 (3, 3);
|
---|
66 | load_smatrix <double, lower>(ml2, filename.c_str());
|
---|
67 | std::cout << ml2 << std::endl;
|
---|
68 |
|
---|
69 | std::cout << mu << std::endl;
|
---|
70 | save_smatrix <double, upper> (mu, filename.c_str());
|
---|
71 | symmetric_matrix<double, upper> mu2 (3, 3);
|
---|
72 | load_smatrix <double, upper>(mu2, filename.c_str());
|
---|
73 | std::cout << mu2 << std::endl;
|
---|
74 |
|
---|
75 | return 0;
|
---|
76 | }
|
---|