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 /// We define typedefs for dense vectors and general matrices that allocate
12 /// their own memory.
13 ///
14     typedef GeMatrix<FullStorage<double, ColMajor> >    GeMatrix;
15     typedef DenseVector<Array<double> >                 DenseVector;
16     typedef DenseVector::IndexType                      IndexType;
17 
18 ///
19 /// For selecting vector parts we define a range operator
20 ///
21     const Underscore<IndexType> _;
22 
23 ///
24 /// We setup some matrix.
25 ///
26     GeMatrix  A(45);
27     A =  1,  2,  3,  4,  5,
28          6,  7,  8,  910,
29         1112131415,
30         1617181920;
31 
32 ///
33 /// We create a vector view `y` that references the second row of `A`.  Then
34 /// we reset all elements of `y` to `666`.  This will also change elements in
35 /// `A`.
36 ///
37     DenseVector::View y = A(2,_);
38     y = 666;
39 
40 ///
41 /// We create a "regular" vector `z` and initialize it with the third row
42 /// of `A`.  Just to show that `z` is not a view we then reset all elements
43 /// of `z` to `42`.  You will see that this will not change elements in `A`.
44 ///
45     DenseVector::NoView z = A(3,_);
46     z = 42;
47 
48     cout << "A = " << A << endl;
49     cout << "y = " << y << endl;
50     cout << "z = " << z << endl;
51 
52 ///
53 /// Of course we also can use the `auto` type instead of writing out the
54 /// `DenseVector::View` type.
55 ///
56     auto x = A(_(2,4),2);
57     x = 999;
58 
59 ///
60 /// You also can reference the `k`-th off-diagonal of `A`.  For $k=0$ you
61 /// get the main diagonal, for $k>0$ the `k`-th super-diagonal and for
62 /// $k<0$ the corresponding sub-diagonal.
63 ///
64     auto d_1 = A.diag(-1);
65     auto d0  = A.diag(0);
66     auto d1  = A.diag(1);
67     auto d2  = A.diag(2);
68 
69     cout << "d_1 = " << d_1 << endl;
70     cout << "d0  = " << d0 << endl;
71     cout << "d1  = " << d1 << endl;
72     cout << "d2  = " << d2 << endl;
73 
74 ///
75 /// Next we set all elements on the diagonal to `-1` and all elements on the
76 /// first super-diagonal to `-100`, ..., `-400`:
77 ///
78     d0 = -1;
79     d1 = -100, -200, -300, -400;
80 
81     cout << "A = " << A << endl;
82     cout << "x = " << x << endl;
83 
84 ///
85 /// We finally print the strides of the vectors.  Compare strides of vectors
86 /// referencing rows, columns and diagonals.
87 ///
88     cout << "A.leadingDimension() = " << A.leadingDimension() << endl;
89     cout << "x.stride() = " << x.stride() << endl;
90     cout << "y.stride() = " << y.stride() << endl;
91     cout << "z.stride() = " << z.stride() << endl;
92     cout << "d_1.stride() = " << d_1.stride() << endl;
93     cout << "d0.stride()  = " << d0.stride() << endl;
94     cout << "d1.stride()  = " << d1.stride() << endl;
95     cout << "d2.stride()  = " << d2.stride() << endl;
96 }