2013-08-22 16:55:51 +02:00
|
|
|
Concepts
|
|
|
|
=============================================================
|
|
|
|
|
|
|
|
In this section, we define the basic concepts (in the C++ sense)
|
|
|
|
related to the multidimentional arrays.
|
|
|
|
Readers not familiar with the idea of concepts in programming can skip this section,
|
|
|
|
which is however needed for a more advanced usage of the library.
|
|
|
|
|
|
|
|
|
|
|
|
A multidimentional array is basically a function of some indices, typically integers taken in a specific domain,
|
|
|
|
returning the element type of the array, e.g. int, double.
|
|
|
|
Indeed, if a is an two dimensionnal array of int,
|
|
|
|
it is expected that a(i,j) returns an int or a reference to an int, for i,j integers in some domain.
|
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
We distinguish two separate notions based on whether this function is `pure`
|
|
|
|
or not, i.e. whether one can or not modify a(i,j).
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
* An `Immutable` array is simply a pure function on the domain of definition.
|
2014-07-08 14:39:16 +02:00
|
|
|
a(i,j) returns a int, or a int const &, that cannot be modified (hence immutable).
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
* A `Mutable` array is an Immutable array that *can* be modified. The non-const
|
|
|
|
object returns a reference, e.g. a(i,j) can return a int &. Typically this is
|
|
|
|
a piece of memory, with a integer coordinate system on it.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
The main point here is that an `Immutable` array is a much more general notion:
|
|
|
|
a formal expression consisting of arrays (e.g. A + 2*B) models this concept,
|
|
|
|
but not the `Mutable` one.
|
|
|
|
Most algorithms only use the `Immutable` array notion, where they are pure
|
|
|
|
(mathematical) functions that return something depending on the value of an
|
|
|
|
object, without side effects.
|
2013-08-30 11:07:39 +02:00
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
.. _ImmutableCuboidArray:
|
|
|
|
|
|
|
|
ImmutableCuboidArray
|
|
|
|
----------------------------
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* **Purpose**:
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-30 11:07:39 +02:00
|
|
|
The most abstract definition of something that behaves like an immutable array on a cuboid domain.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-30 11:07:39 +02:00
|
|
|
* it has a cuboid domain (hence a rank).
|
2013-08-22 16:55:51 +02:00
|
|
|
* it can be evaluated on any value of the indices in the domain
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* NB: It does not need to be stored in memory. For example, a formal expression models this concept.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* **Definition** ([...] denotes something optional).
|
|
|
|
|
|
|
|
+-------------------------------------------------------+-------------------------------------------------------------------------+
|
|
|
|
| Members | Comment |
|
|
|
|
+=======================================================+=========================================================================+
|
|
|
|
| domain_type == cuboid_domain<Rank> | Type of the domain, with rank `Rank` |
|
|
|
|
+-------------------------------------------------------+-------------------------------------------------------------------------+
|
|
|
|
| domain_type [const &] domain() const | Access to the domain. |
|
|
|
|
+-------------------------------------------------------+-------------------------------------------------------------------------+
|
|
|
|
| value_type | Type of the element of the array |
|
|
|
|
+-------------------------------------------------------+-------------------------------------------------------------------------+
|
|
|
|
| value_type [const &] operator() (size_t ... i) const | Evaluation. Must have exactly rank argument (checked at compiled time). |
|
|
|
|
+-------------------------------------------------------+-------------------------------------------------------------------------+
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* **Examples**:
|
2013-08-22 16:55:51 +02:00
|
|
|
* array, array_view, matrix, matrix_view, vector, vector_view.
|
|
|
|
* array expressions.
|
|
|
|
|
|
|
|
.. _MutableCuboidArray:
|
|
|
|
|
|
|
|
MutableCuboidArray
|
|
|
|
-------------------------
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* **Purpose**: An array where the data can be modified.
|
|
|
|
* **Refines**: :ref:`ImmutableCuboidArray`.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* **Definition**
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
+----------------------------------------------+-----------------------------------------------------------------------------+
|
|
|
|
| Members | Comment |
|
|
|
|
+==============================================+=============================================================================+
|
|
|
|
| value_type & operator() (size_t ... i) | Element access: Must have exactly rank argument (checked at compiled time). |
|
|
|
|
+----------------------------------------------+-----------------------------------------------------------------------------+
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* **Examples**:
|
2013-08-22 16:55:51 +02:00
|
|
|
* array, array_view, matrix, matrix_view, vector, vector_view.
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
.. _ImmutableArray:
|
|
|
|
|
|
|
|
ImmutableArray
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
|
|
|
* Refines :ref:`ImmutableCuboidArray`
|
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
* If X is the type:
|
2013-08-27 19:17:17 +02:00
|
|
|
|
|
|
|
* ImmutableArray<A> == true_type
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the Array algebra.
|
2013-08-27 19:17:17 +02:00
|
|
|
|
2013-08-22 16:55:51 +02:00
|
|
|
.. _ImmutableMatrix:
|
|
|
|
|
|
|
|
ImmutableMatrix
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* Refines :ref:`ImmutableCuboidArray`
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* If A is the type:
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* ImmutableMatrix<A> == true_type
|
2013-12-31 14:22:00 +01:00
|
|
|
* A::domain_type::rank == 2
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the MatrixVector algebra.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
.. _ImmutableVector:
|
|
|
|
|
|
|
|
ImmutableVector
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* Refines :ref:`ImmutableCuboidArray`
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* If A is the type:
|
2013-08-27 19:17:17 +02:00
|
|
|
|
|
|
|
* ImmutableMatrix<A> == true_type
|
2013-12-31 14:22:00 +01:00
|
|
|
* A::domain_type::rank == 1
|
2013-08-27 19:17:17 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the MatrixVector algebra.
|
2013-08-27 19:17:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
.. _MutableArray:
|
|
|
|
|
|
|
|
MutableArray
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
|
|
|
* Refines :ref:`MutableCuboidArray`
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* If A is the type:
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* ImmutableArray<A> == true_type
|
|
|
|
* MutableArray<A> == true_type
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the Array algebra.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
.. _MutableMatrix:
|
|
|
|
|
|
|
|
MutableMatrix
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* Refines :ref:`MutableCuboidArray`
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* If A is the type:
|
2013-08-27 19:17:17 +02:00
|
|
|
|
|
|
|
* ImmutableMatrix<A> == true_type
|
|
|
|
* MutableMatrix<A> == true_type
|
|
|
|
* A::domain_type::rank ==2
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the MatrixVector algebra.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
.. _MutableVector:
|
|
|
|
|
|
|
|
MutableVector
|
|
|
|
-------------------------------------------------------------------
|
|
|
|
|
2013-08-27 19:17:17 +02:00
|
|
|
* Refines :ref:`MutableCuboidArray`
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
* If A is the type:
|
2013-08-27 19:17:17 +02:00
|
|
|
|
|
|
|
* ImmutableMatrix<A> == true_type
|
|
|
|
* MutableMatrix<A> == true_type
|
|
|
|
* A::domain_type::rank ==1
|
|
|
|
|
2014-10-17 18:15:19 +02:00
|
|
|
NB: this traits marks the fact that X belongs to the MatrixVector algebra.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
Why concepts ? [Advanced]
|
|
|
|
-----------------------------
|
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
Why is it useful to define these concepts ?
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
Simply because of lot of the library algorithms only use these concepts,
|
|
|
|
and such algorithms can be used for any array or custom class that models
|
|
|
|
the concept.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
2013-12-31 14:22:00 +01:00
|
|
|
For example:
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
* Problem: we want to quickly assemble a small class to store a diagonal matrix.
|
2013-12-31 14:22:00 +01:00
|
|
|
We want this class to operate with other matrices, e.g. be part of an
|
|
|
|
expression, be printed, etc.
|
|
|
|
However, we only want to store the diagonal element.
|
2013-08-22 16:55:51 +02:00
|
|
|
|
|
|
|
* A simple solution :
|
|
|
|
|
2014-05-31 19:12:21 +02:00
|
|
|
.. triqs_example:: ./concepts_0.cpp
|
2013-08-22 16:55:51 +02:00
|
|
|
* Discussion
|
|
|
|
|
|
|
|
* Of course, this solution is not perfect. Several algorithms could be optimised if we know that a matrix is diagonal.
|
|
|
|
E.g. multiplying a diagonal matrix by a full matrix. Currently, it creates a full matrix from the diagonal one, and
|
|
|
|
call gemm. This is clearly not optimal.
|
|
|
|
|
|
|
|
However, this is not the point.
|
|
|
|
|
|
|
|
This class *just works* out of the box, and takes only a few minutes to write.
|
|
|
|
One can of course then work more and specialize e.g. the operator * to optimize the multiplication,
|
|
|
|
or any other algorithm, `if and when this is necesssary`. That is an implementation detail,
|
|
|
|
that be done later, or by someone else in the team, without stopping the work.
|
|
|
|
|
|
|
|
* One can generalize for a Mutable diagonal matrix. Left as an exercise...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|