1 #include <flens/flens.cxx>
 2 #include <iostream>
 3 
 4 using namespace flens;
 5 using namespace std;
 6 
 7 int
 8 main()
 9 {
10 ///
11 /// So this is our plain C-array
12 ///
13     double data[4*4] = { 1,  2,  3,  4,
14                          5,  6,  7,  8,
15                          9101112,
16                         13141516};
17 
18 ///
19 /// We define one typedef for a storage scheme that only references data
20 /// and another typedef for a corresponding general matrix type. The
21 /// trick is that you can construct `GeMatrix` objects from full storage
22 /// objects.  So what we later do is building a storage object that
23 /// just keeps internally a pointer to the C-array.
24 ///
25 /// Note: The type `GeMatrixView` is actually identicall with the one
26 /// from the previous example (there we defined it via `GeMatrix::View`).
27 ///
28     typedef FullStorageView<double, ColMajor>  FSView;
29     typedef GeMatrix<FSView>                   GeMatrixView;
30 
31 ///
32 /// We finally create a matrix view that references the C-array as follows:
33 ///   - We wrap the C-array in an anonymous full storage view object
34 ///   - and construct with it a general matrix view.
35 /// The syntax for the full storage view is
36 /// `FSView(numRows, numCols, data-pointer, leading dimension)`.
37 ///
38     GeMatrixView  A = FSView(44, data, 4);
39 
40     cout << "A = " << A << endl;
41 
42 ///
43 /// With a little pointer arithmetic we also can create views for a
44 /// sub-matrix directly (note that `data+5`points to the element of
45 /// `A(2,3)`.
46 ///
47     GeMatrixView  B = FSView(32, data+54);
48 
49     cout << "B = " << B << endl;
50 
51 ///
52 /// Note that in the following the data of the C-array gets copied into
53 /// matrix `M`.  This is because `GeMatrixNoView` has a non-view storage
54 /// scheme.
55 ///
56     typedef GeMatrix<FullStorage<double, ColMajor> >  GeMatrixNoView;
57     GeMatrixNoView  M = GeMatrixView(FSView(44, data, 4));
58 
59 ///
60 /// Note that you only can construct a `GeMatrix` from a storage object
61 /// if the type of the storage object is identical with `GeMatrix::Engine`
62 /// (`Engine` is the storage scheme used by `GeMatrix`).  Hence, the
63 /// following would not compile:
64 ///
65 //  GeMatrixNoView M = FSView(4, 4, data, 4);
66 //  error: This would not compile because
67 //         GeMatrixNoView::Engine is of type "FullStorage<...>"
68 //         FSView                 is of type "FullStorageView<...>"
69 
70 ///
71 /// Let us demonstrate what is a view and what not:
72 ///   - We change `M(2,2)`,
73 ///   - we change `B(1,2)`,
74 ///   - we output `A`.
75 /// Only the change of `B(1,2)` affects elements of `A` as both matrices
76 /// share data.
77 ///
78     M(2,2) = -666;
79     B(1,2) =  666;
80     cout << "now: A = " << A << endl;
81     cout << "Data dump of C-array: " << endl;
82     for (int i=0; i<16; ++i) {
83         cout << data[i] << "  ";
84     }
85     cout << endl;
86 
87     return 0;
88 }