1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
#include <flens/flens.cxx>
#include <iostream>

using namespace flens;
using namespace std;

int
main()
{

///
/// The storage scheme for `DenseVector` is `Array`, `ArrayView` or
/// `ConstArrayView`.  We setup a typedef for a "regular" dense vector that
/// allocates its own memory.
///
    typedef DenseVector<Array<double> >   DenseVector;
    typedef DenseVector::IndexType        IndexType;

///
/// We define a dense vector of length `4` and initialize it.
///
    DenseVector x(4);
    x = 1, 2, 3, 4;

///
/// We retrieve some information like index range and vector length.  You
/// will see that the default index base is One.
///
    cout << "x.range() = " << x.range() << endl;
    cout << "x.length() = " << x.length() << endl;

///
/// We print the vector.
///
    cout << "x = " << x << endl;

///
/// We iterate through the vector and reset its elements (such that $x_i =
/// i^2$).
///
    for (IndexType i=x.firstIndex(); i<=x.lastIndex(); ++i) {
        x(i) = i*i;
    }

///
/// For selecting vector parts we define a range operator
///
    const Underscore<IndexType> _;

///
/// We create a vector view `y` that references a part of `x`.
///
    DenseVector::View y = x(_(2,4));
    y = 666;

///
/// Here we create a new vector `z` that allocates its own memory and
/// initializes it with a copy of a part of `x`.  So `z` is no vector view.
///
    DenseVector::NoView z = x(_(1,2));
    z = 42;

    cout << "x = " << x << endl;
    cout << "y = " << y << endl;
    cout << "z = " << z << endl;
}