ALPS Project: alea library

Header file alea/obsvalue.h

This header contains traits classes to facilitate generic implementation of observables. It is of interest only to users wanting to extend the functionality beyond the basic floating point and integral types, std::complex and std::valarray.

Synopsis

namespace alps {

template <class T>
struct obs_value_traits
{
typedef T value_type;
typedef double count_type;
typedef double time_type;
typedef typename TypeTraits<value_type>::average_t result_type;
typedef T element_type;
typedef double time_element_type;
typedef uint32_t size_type;
typedef uint32_t slice_iterator;

static const uint32_t magic_id = TypeTraits<value_type>::type_tag;
static const bool array_valued;

static element_type max();
static element_type min();
static element_type epsilon();
static time_element_type t_max();

static void check_for_max(const value_type& a,value_type& b);
static void check_for_min(const value_type& a,value_type& b);

static void fix_negative(value_type& x);

static value_type check_divide(const result_type& a,const result_type& b);

template <class X, class Y> static void resize_same_as(X& x,const Y& y);
template <class X, class Y> static void copy(X& x,const Y& y);
template <class X> static std::size_t size(const X& x);

template <class X> static T convert(const X& );

static slice_iterator slice_begin(const value_type&);
static slice_iterator slice_end(const value_type&);
static std::string slice_name(const value_type&,slice_iterator);
static value_type slice_value(const value_type& x, slice_iterator);

};

template <class T>
struct obs_value_traits<std::complex<T> >
{
...
};

template <class T>
struct obs_value_traits<std::valarray<T> >
{
...
};

template<typename T, std::size_t NumDims, typename Allocator>
struct obs_value_traits<alps::multi_array<T,NumDims,Allocator> >
{
...
};

template <class DST,class SRC> DST obs_value_cast(const SRC& s)
{
return obs_value_traits<DST>::convert(s);
}

} // end namespace alps

The obs_value_traits traits class

is a traits class to facilitate a generic implementation of observables for real, complex, integral and array-valued data types. Currently all built in numeric data types, std::complex complex numbers and std::valarray are supported. Plans exist to also add support for array data types from the Blitz++ and MTL libraries.

Type members

The following type members have to be defined
Type Explanation
value_type The type for which the obs_value_traits are specialized
count_type a floating point type appropriate to store the number of measurements
time_type a type appropriate to store autocorrelation times for the observables. This is typically a floating point type for scalar types, and an array of floating point types for array valued types.
result_type a type appropriate to store averages. Typically value_type except for integral types, where it must be a floating point type
element_type for array-valued types, the type of a single elements (usually typename value_type::value_type). For scalar types, the same as value_type
size_type for array-valued types, an integral type large enough to store the size or extent of the array (typically typename value_type::size_type). For scalar types, an arbitrary integral type.
slice_iterator an iterator to iterate through all elements in the value_type. Important for array-valued types.
The slice_iterator need not be a full iterator, but has to implement only the operators ++ and !=.

Data members

static const uint32_t magic_id;
is a unique identifier for the value_type.
static const bool array_valued;
is true if the type is array-valued and can be sliced.

General member functions

static element_type max();
static element_type min();
static element_type epsilon();
return the largest positive number, the largest negative number, and the floating point epsilon for the element_type, which is the same as value_type for scalar types, but the value_type of the array for array types.
static time_element_type t_max();
returns the largest possible time
static void check_for_max(const value_type& a,value_type& b);
static void check_for_min(const value_type& a,value_type& b);
replace b by the maximum (minimum) of a and b. For complex numbers the absolute value is used. For array types it is performed element wise.
static void fix_negative(value_type& a);
replaces negative values (element wise) by zero
static value_type check_divide(const result_type& a,const result_type& b);
calculates the ration a/b, replacig it by 1 if it is 0/0.

Copying and resizing

template <class X, class Y> static void resize_same_as(X& x,const Y& y);
resizes an array-valued x to the same size as y. Should also be implemented for scalar types, where it is a no-op. The types X are either value_type, time_type, count_type or result_type.
template <class X, class Y> static void copy(X& x,const Y& y);
Copies x to y, resizing the destination if needed. Note that this is needed since no such resizing is done for std::valarray when the assignment operator is used. Should also be implemented for scalar types. The types X are either value_type, time_type, count_type or result_type.
template <class X> static std::size_t size(const X& x);
returns the size of the array x or 1 for scalar types X. The type X is either value_type, time_type, count_type or result_type.
template <class X> T convert(const X& );
converts the argument to type T, performing e.g. static_cast<T> if defined.

Slicing

static slice_iterator slice_begin(const value_type&);
returns an iterator to the first element in the array-valued observable.
static slice_iterator slice_end(const value_type&);
returns an iterator pointing past the last element in the array-valued observable.
static std::string slice_name(const value_type&,slice_iterator);
returns a string, expressing the slice index in string form. E.g. for a std::valarray, the i-th slice is expressed by the string "[i]".
static value_type slice_value(const value_type& x, slice_iterator);
returns the value at the position pointed to by the slice_iterator.

Function obs_value_cast

template <class DST,class SRC> DST obs_value_cast(const SRC& s) 
{
return obs_value_traits<DST>::convert(s);
}
converts an object of type SRC to type DST, using the obs_value_traits class.

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)