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 start with a typedef for a `GeMatrix` with elements of type `double`
 12 /// that are stored column-wise in memory.
 13 ///
 14     typedef FullStorage<double, ColMajor>  FS;
 15     typedef GeMatrix<FS>                   GeMatrix;
 16 
 17 ///
 18 /// Then we define matrix view types for referencing matrix parts.
 19 ///
 20     typedef GeMatrix::ConstView            GeMatrixConstView;
 21     typedef GeMatrix::View                 GeMatrixView;
 22 
 23 ///
 24 /// We setup some matrix.
 25 ///
 26     GeMatrix  A(3,4);
 27     A = 1,  2,  3,  4,
 28         5,  6,  7,  8,
 29         9101112;
 30 
 31     cout << "A = " << A << endl;
 32 
 33 ///
 34 /// We also define a "range-operator" to ease the selection of matrix parts.
 35 /// for this purpose we simply define an object called "`_`" of type
 36 /// `Underscore<I>` where `I` denotes the type of index used in the matrix.
 37 ///
 38     const Underscore<GeMatrix::IndexType>   _;
 39 
 40 ///
 41 /// Now we let `B` reference the matrix part `A(2:3,2:4)`.  The range operator
 42 /// lets you specify row/column ranges by `_(from, to)` similar to the Matlab
 43 /// notation `from:to`.
 44 ///
 45     GeMatrixView       B = A(_(2,3),_(2,4));
 46 
 47 ///
 48 /// Note that you can not define something like an uninitialized matrix view.
 49 /// Just like you can not define uninitialized references in C++.  I.e. this
 50 /// would give you an error
 51 //
 52 //  error:
 53 //  GeMatrixView       B;
 54 //
 55 
 56 ///
 57 /// Next we define a matrix view that provides read-only access to `A(:,2,4)`.
 58 /// So by the way, notice that you can also select all rows if you do not pass
 59 /// arguments to the range operator "`_`".  Analogously you can select all
 60 /// columns of course.
 61 ///
 62     GeMatrixConstView  C = A(_,_(2,4));
 63 
 64 ///
 65 /// We overwrite `B` and by that `A`
 66 ///
 67     B = 111222333,
 68         444555666;
 69 
 70     cout << "Changed B:" << endl;
 71     cout << "A = " << A << endl;
 72     cout << "B = " << B << endl;
 73     cout << "C = " << C << endl;
 74 
 75 ///
 76 /// Note that `B` acts like a "regular" matrix and indices also start at One.
 77 ///
 78     B(1,1) = 999;
 79 
 80     cout << "Changed B(1,1):" << endl;
 81     cout << "A = " << A << endl;
 82     cout << "B = " << B << endl;
 83     cout << "C = " << C << endl;
 84 
 85 ///
 86 /// Of course you also can use unnamed matrix views:
 87 ///
 88     A(_,_(1,2)) = A(_,_(3,4));
 89 
 90     cout << "After copying A(:,1:2) = A(:,3:4)" << endl;
 91     cout << "A = " << A << endl;
 92 
 93 ///
 94 /// And obviously it should not be possible to write to `C`.  Indeed this would
 95 /// give you a compile-time error:
 96 //
 97 //  error:
 98 //  C(1,1) = 3;
 99 //  C = 1, 2,
100 //      3, 4,
101 //      5, 6;
102 }