ALPS Project: lattice library

Helper concepts

To get started we need dimensions of geometrical objects, locations of points, vectors between points, vector arithmetic and coordinates. We thus introduce the following concepts, which should preferable be defined some time in a broader contect: Unless mentioned otherwise all functions and traits classes below are declared in namespace lattice

Dimensional

is an entity which has a dimension. Models are all containers.

Notation

Xa type that is a model of Dimensional
xan object of type X

Associated Types

lattice::dimensional_traits<X>::dimension_type

an integral type large enough to store the dimension of X

Expressions

lattice::dimensional_traits<X>::infinity() return type dimensional_traits<X>::dimension_type
the value used to represent an infinite lattice
lattice::dimensional_traits<X>::fixed_dimension type bool, is true if the dimension is a compile time constant
See the concept FixedDimensional for additional information in that case
dimension(x) return type lattice::dimensional_traits<X>::dimension_type
the dimension of x

Note that

lattice::dimension(x) == lattice::dimensional_traits<X>::infinity()
is true for infinite-dimensional lattices.

Example Implementation: dimensional_traits.h

The example implementation in the file dimensional_traits.h works for standard containers and defines the dimension(x) by the size() member function of the container:
namespace alps {

  template <class Dimensional>
  struct dimensional_traits {
    typedef std::size_t dimension_type;
    static const bool fixed_dimension=false;
    static dimension_type infinity()
    {
      return std::numeric_limits<dimension_type>::max();
    }
  };

  template <class Dimensional>
  inline typename dimensional_traits::dimension_type
  dimension(const Dimensional& d)
  {
    return d.size();
  }
}

FixedDimensional

is a refinement of Dimensional. Models are C-arrays and blitz::TinyVector.

Notation

Xa type that is a model of FixedDimensional
xan object of type X

Expressions

dimensional_traits<X>::fixed_dimension evaluates to true
dimensional_traits<X>::dimension same as dimension(x)

Example Implementations

template <class T, int sz>
struct dimensional_traits<T[sz]> {
typedef int dimension_type;
bool fixed_dimension=true;
dimension_type dimension=sz;
};

template <class T, int sz>
typename dimensional_traits<T[sz]>::dimension_type
dimension(const T[sz]& d)
{
return sz;
}

template <class T, int sz>
struct dimensional_traits<blitz::TinyVector<T,sz> > {
typedef int dimension_type;
bool fixed_dimension=true;
dimension_type dimension=sz;
};

template <class T, int sz>
typename dimensional_traits<blitz::TinyVector<T,sz> >::dimension_type
dimension(const T[sz]& d)
{
return sz;
}

Point

describes a geometrical point, is a refinement of Dimensional,

Models are std::valarray, blitz::TinyVector, blitz::Array and others

Notation

Pa type that is a model of Point
p, qobjects of type P
vobject of type lattice::point_traits<P>::vector_type

Associated Type

lattice::point_traits<P>::vector_type

a type that is a model of the concept Vector, the result type of the difference of two points

Expressions

p-q return type lattice::point_traits<P>::vector_type
a vector pointing from q to p
p+v
v+p
return type P
the point p shifted by the vector v
p-v return type P
the point p shifted by the vector -v

Example Implementation: point_traits.h

The example implementation in the file point_traits.h assumes that we have a type that can represent both points and vectors. An improved implementation should make sure that operations not defined (such as p+p) cause compile time errors.
namespace alps {

template 
struct point_traits
{
  typedef P vector_type;	
};

}

Vector

describes a vector, is a refinement of Dimensional

models are std::valarray, blitz::TinyVector, blitz::Array

Notation

Va type that is a model of Vector
v, wobjects of type V
tan object of a scalar type (integer or floating point number)

Expressions

in addition to expressions mentioned in connection with the concept Point we require the following:
v+w return type V, the sum of two vectors
v-w return type V, the difference of two vectors
t*v
v*t
return type V, the vector v scaled by the facor t
v/t return type V, the vector v scaled by the facor 1/t
-v return type V, the inverted vector

Coordinate

refinement of Dimensional, describes coordinates of a point, vector, etc.. Models are all containers

Notation

Ca type that is a model of Coordinate
can object of type C
const_can object of type const C
ian object of type lattice::dimension_traits<C>::dimension_type

Associated Types

lattice::coordinate_traits<C>::iterator

an iterator type for C
lattice::coordinate_traits<C>::const_iterator

a const iterator type for C
lattice::coordinate_traits<C>::value_type

a type able to store a coordinate in one of the dimensions,
same as std::iterator_traits<typename lattice::coordinate_traits<C>::iterator>::value_type

Expressions

c[i] return type lattice::coordinate_traits<C>::value_type
the i-th coordinate, 0<=i<dimensionscopyright (c)
comment: is this actually needed or just convenient?
lattice::coordinatescopyright (c) return type std::pair<typename lattice::coordinate_traits<C>::iterator,typename lattice::coordinate_traits<C>::iterator>
the pair consists of two iterators, pointing to the first and one past the last coordinate
lattice::coordinates(const_c) return type std::pair<typename lattice::coordinate_traits<C>::const_iterator, typename lattice::coordinate_traits<C>::const_iterator>
the pair consists of two iterators, pointing to the first and one past the last coordinate

Example Implementation: coordinate_traits.h

The example implementation in the file coordinate_traits.h implements the functions and traits for standard containers and for std::valarray:
namespace alps {
  template <class C>
  struct coordinate_traits {
    typedef typename C::value_type value_type;
    typedef typename C::iterator iterator;
    typedef typename C::const_iterator const_iterator;
  };
  
  template <class C>
  inline std::pair<typename coordinate_traits<C>::iterator, typename   coordinate_traits<C>::iterator>
  coordinates(C& c)
  {
    return std::make_pair(c.begin(),c.end());
  }

  template <class C>
  inline std::pair<typename coordinate_traits<C>::const_iterator, typename coordinate_traits<C>::const_iterator>
  coordinates(const C& c)
  {
    return std::make_pair(c.begin(),c.end());
  }


  template <class T>
  struct coordinate_traits<std::valarray<T> > {
    typedef typename T value_type;
    typedef typename T* iterator;
    typedef typename const T* const_iterator;
  };
  
  template <class T>
  inline std::pair<T*, T*> coordinates(std::valarray<T>& c)
  {
    return make_pair(&(c[0]),&(c[0])+c.size());
  }

  template <class T>
  inline std::pair<const T*, const T*> coordinates(std::valarray<T>& c)
  {
    return std::pair<const T*, const T*>
      (&(const_cast<std::valarray<T>&>copyright (c)[0]),
      &(const_cast<std::valarray<T>&>copyright (c)[0])+c.size());
  }
}

Refinements

Useful concepts will be those which combine the above concepts FixedDimensional and Coordinate with Point and Vector: These are all straight forward refinements of above concepts. We can imagine the addition of extra member functions for the calculation of norms, etc. to CoordinateVector, but refrain from doing so until we see the need for such traits or functions in our applications.

copyright (c) 1994-2010 by Matthias Troyer

Distributed under the Boost Software License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)