Векторные выражения (vector expressions)

Векторное выражение (vector expression)

Описание

Шаблонный класс vector_expression<E> является публичным базовым классом для всех классов, для которых моделью объявлено векторное выражение Vector Expression.

Definition

Defined in the header expression_types.hpp.

Template parameters

Parameter Description Default
E The type of the vector expression.  

Model of

None. Not a Vector Expression!

Type requirements

None.

Public base classes

None.

Members

Member Description
const expression_type &operator () () const Returns a const reference of the expression.
expression_type &operator () () Returns a reference of the expression.

Notes

The range, slice and project functions have been removed. Use the free functions defined in vector proxy instead.

Vector Container

Description

The templated class vector_container<C> is required to be a public base of all classes which model the Vector concept. This includes the class vector itself.

Definition

Defined in the header expression_types.hpp.

Template parameters

Parameter Description Default
C The type of the vector container.  

Model of

None. Not a Vector Expression OR Vector!

Type requirements

None.

Public base classes

vector_expression<C>

Members

Member Description
const container_type &operator () () const Returns a const reference of the container.
container_type &operator () () Returns a reference of the container.

Векторные ссылки (vector references)

Описание

Шаблонный класс vector_reference<E> содержит ссылку на векторное выражение (vector expression).

Definition

Defined in the header vector_expression.hpp.

Template parameters

Parameter Description Default
E The type of the vector expression.  

Model of

Vector Expression .

Type requirements

None, except for those imposed by the requirements of Vector Expression .

Public base classes

vector_expression<vector_reference<E> >

Members

Member Description
vector_reference (expression_type &e) Constructs a reference of the expression.
void resize (size_type size) Resizes the expression to hold at most size elements.
size_type size () const Returns the size of the expression.
const_reference operator () (size_type i) const Returns the value of the i-th element.
reference operator () (size_type i) Returns a reference of the i-th element.
const_iterator begin () const Returns a const_iterator pointing to the beginning of the expression.
const_iterator end () const Returns a const_iterator pointing to the end of the expression.
iterator begin () Returns a iterator pointing to the beginning of the expression.
iterator end () Returns a iterator pointing to the end of the expression.
const_reverse_iterator rbegin () const Returns a const_reverse_iterator pointing to the beginning of the reversed expression.
const_reverse_iterator rend () const Returns a const_reverse_iterator pointing to the end of the reversed expression.
reverse_iterator rbegin () Returns a reverse_iterator pointing to the beginning of the reversed expression.
reverse_iterator rend () Returns a reverse_iterator pointing to the end of the reversed expression.

Векторные операции

Унарные операции

Описание

Шаблонный класс vector_unary<E, F> описывает унарную векторную операцию.

Определение

Определен в заголовочном файле vector_expression.hpp.

Параметры шаблона

Параметр Описание Значение по-умолчанию
E Тип векторного выражения.  
F Тип операции.  

Является моделью

Vector Expression .

Требования к типам данных

Для которых базовый класс Vector Expression .

Базовый класс

vector_expression<vector_unary<E, F> >

Функции-члены

Функции-члены и их описание приведены без перевода здесь.

Унарные операции

Ниже дано краткое описание унарных операций, достаточное для их использования. Для подробного ознакомления с ними смотрите здесь.

template<class E1, class E2, class F>
    struct vector_unary_traits {
        typedef vector_unary<typename E1::const_closure_type,
                               typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
    };
     
    // Вычисление инверсии векторного выражения
    // (- v) [i] = - v [i]
    result_type operator - (const vector_expression<E> &e);

    // Вычисление комплексного сопряженного векторного выражения
    // (conj v) [i] = conj (v [i])
    result_type conj (const vector_expression<E> &e);

    // Вычисление мнимой части векторного выражения
    // (real v) [i] = real (v [i])
    result_type real (const vector_expression<E> &e);

    // Вычисление действительной части векторного выражения
    // (imag v) [i] = imag (v [i])
    result_type imag (const vector_expression<E> &e);

    // Вычисление транспонированного векторного выражения
    // (trans v) [i] = v [i]
    result_type trans (const vector_expression<E> &e);

    // Вычисление эрмитового векторного выражения, т. е. комплексно сопряженного
    // к транспонированному векторному выражению
    // (herm v) [i] = conj (v [i])
    result_type herm (const vector_expression<E> &e);

Пример

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<std::complex<double> > v (3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = std::complex<double> (i, i);

    std::cout << - v << std::endl;
    std::cout << conj (v) << std::endl;
    std::cout << real (v) << std::endl;
    std::cout << imag (v) << std::endl;
    std::cout << trans (v) << std::endl;
    std::cout << herm (v) << std::endl;
}
Будет выведено на экран:

[3]((0,0),(-1,-1),(-2,-2))
[3]((0,0),(1,-1),(2,-2))
[3](0,1,2)
[3](0,1,2)
[3]((0,0),(1,1),(2,2))
[3]((0,0),(1,-1),(2,-2))

Бинарные операции

Описание

Шаблонный класс vector_binary<E1, E2, F> описывает бинарную векторную операцию.

Определение

Определен в заголовочном файле vector_expression.hpp.

Параметры шаблона

Параметр Описание Значение по-умолчанию
E1 Тип первого векторного выражения.
E2 Тип второго векторного выражения.
F Тип операции.

Является моделью

Vector Expression .

Требования к типам данных

Для которых базовый класс Vector Expression .

Базовый класс

vector_expression<vector_binary<E1, E2, F> >

Функции-члены

Функции-члены и их описание приведены без перевода здесь.

Бинарные операции

Ниже дано краткое описание бинарных операций, достаточное для их использования. Для подробного ознакомления с ними смотрите здесь.

template<class E1, class E2, class F>
    struct vector_binary_traits {
        typedef vector_binary<typename E1::const_closure_type,
                               typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // Сложение 2-х векторных выражений
    // (v1 + v2) [i] = v1 [i] + v2 [i]
    result_type operator + (const vector_expression<E1> &e1,
                 const vector_expression<E2> &e2);

    // Вычитание 2-х векторных выражений
    // (v1 - v2) [i] = v1 [i] - v2 [i]
    result_type operator - (const vector_expression<E1> &e1,
                 const vector_expression<E2> &e2);

Предусловие

Пример

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v1 (3), v2 (3);
    for (unsigned i = 0; i < std::min (v1.size (), v2.size ()); ++ i)
        v1 (i) = v2 (i) = i;

    std::cout << v1 + v2 << std::endl;
    std::cout << v1 - v2 << std::endl;
}
Будет выведено на экран:

[3](0,2,4)
[3](0,0,0)

Бинарная внешняя операция (outer operation)

Описание

Шаблонный класс vector_matrix_binary<E1, E2, F> описывает бинарную внешнюю векторную операцию.

Определение

Определена в заголовочном файле matrix_expression.hpp.

Параметры шаблона

Параметр Описание Значение по-умолчанию
E1 Тип первого векторного выражения.
E2 Тип второго векторного выражения.
F Тип операции.

Является моделью

Matrix Expression .

Требования к типам данных

Для которых базовый класс Matrix Expression .

Базовый класс

matrix_expression<vector_matrix_binary<E1, E2, F> >

Функции-члены

Функции-члены и их описание приведены без перевода здесь.

Binary Outer Operations

Prototypes

template<class E1, class E2, class F>
    struct vector_matrix_binary_traits {
        typedef vector_matrix_binary<typename E1::const_closure_type,
                                      typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (outer_prod (v1, v2)) [i] [j] = v1 [i] * v2 [j]
    template<class E1, class E2>
    typename vector_matrix_binary_traits<E1, E2, scalar_multiplies<typename E1::value_type, typename E2::value_type> >::result_type
    outer_prod (const vector_expression<E1> &e1,
                 const vector_expression<E2> &e2);

Description

outer_prod computes the outer product of two vector expressions.

Definition

Defined in the header matrix_expression.hpp.

Type requirements

Preconditions

None.

Complexity

Quadratic depending from the size of the vector expressions.

Examples

#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v1 (3), v2 (3);
    for (unsigned i = 0; i < std::min (v1.size (), v2.size ()); ++ i)
        v1 (i) = v2 (i) = i;

    std::cout << outer_prod (v1, v2) << std::endl;
}

Scalar Vector Operation Description

Description

The templated classes vector_binary_scalar1<E1, E2, F> and vector_binary_scalar2<E1, E2, F> describe binary operations between a scalar and a vector.

Definition

Defined in the header vector_expression.hpp.

Template parameters

Parameter Description Default
E1/E2 The type of the scalar expression.
E2/E1 The type of the vector expression.
F The type of the operation.

Model of

Vector Expression .

Type requirements

None, except for those imposed by the requirements of Vector Expression .

Public base classes

vector_expression<vector_binary_scalar1<E1, E2, F> > and vector_expression<vector_binary_scalar2<E1, E2, F> > resp.

Members

Member Description
vector_binary_scalar1 (const expression1_type &e1, const expression2_type &e2) Constructs a description of the expression.
vector_binary_scalar2 (const expression1_type &e1, const expression2_type &e2) Constructs a description of the expression.
size_type size () const Returns the size of the expression.
const_reference operator () (size_type i) const Returns the value of the i-th element.
const_iterator begin () const Returns a const_iterator pointing to the beginning of the expression.
const_iterator end () const Returns a const_iterator pointing to the end of the expression.
const_reverse_iterator rbegin () const Returns a const_reverse_iterator pointing to the beginning of the reversed expression.
const_reverse_iterator rend () const Returns a const_reverse_iterator pointing to the end of the reversed expression.

Scalar Vector Operations

Prototypes

template<class T1, class E2, class F>
    struct vector_binary_scalar1_traits {
        typedef vector_binary_scalar1<scalar_const_reference<T1>,
                                      typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
    };

    // (t * v) [i] = t * v [i]
    template<class T1, class E2>
    typename vector_binary_scalar1_traits<T1, E2, scalar_multiplies<T1, typename E2::value_type> >::result_type
    operator * (const T1 &e1,
                const vector_expression<E2> &e2);

    template<class E1, class T2, class F>
    struct vector_binary_scalar2_traits {
        typedef vector_binary_scalar2<typename E1::const_closure_type,
                                      scalar_const_reference<T2>, F> expression_type;
        typedef expression_type result_type;
    };

    // (v * t) [i] = v [i] * t
    template<class E1, class T2>
    typename vector_binary_scalar2_traits<E1, T2, scalar_multiplies<typename E1::value_type, T2> >::result_type
    operator * (const vector_expression<E1> &e1,
                const T2 &e2);

    // (v / t) [i] = v [i] / t
    template<class E1, class T2>
    typename vector_binary_scalar2_traits<E1, T2, scalar_divides<typename E1::value_type, T2> >::result_type
    operator / (const vector_expression<E1> &e1,
                const T2 &e2);

Description

operator * computes the product of a scalar and a vector expression. operator / multiplies the vector with the reciprocal of the scalar.

Definition

Defined in the header vector_expression.hpp.

Type requirements

Preconditions

None.

Complexity

Linear depending from the size of the vector expression.

Examples

#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;

    std::cout << 2.0 * v << std::endl;
    std::cout << v * 2.0 << std::endl;
}

Vector Reductions

Unary Reductions

Prototypes

template<class E, class F>
    struct vector_scalar_unary_traits {
         typedef typename F::result_type result_type;
    };

    // sum v = sum (v [i])
    template<class E>
    typename vector_scalar_unary_traits<E, vector_sum<typename E::value_type> >::result_type
    sum (const vector_expression<E> &e);

    // norm_1 v = sum (abs (v [i]))
    template<class E>
    typename vector_scalar_unary_traits<E, vector_norm_1<typename E::value_type> >::result_type
    norm_1 (const vector_expression<E> &e);

    // norm_2 v = sqrt (sum (v [i] * v [i]))
    template<class E>
    typename vector_scalar_unary_traits<E, vector_norm_2<typename E::value_type> >::result_type
    norm_2 (const vector_expression<E> &e);

    // norm_inf v = max (abs (v [i]))
    template<class E>
    typename vector_scalar_unary_traits<E, vector_norm_inf<typename E::value_type> >::result_type
    norm_inf (const vector_expression<E> &e);

    // index_norm_inf v = min (i: abs (v [i]) == max (abs (v [i])))
    template<class E>
    typename vector_scalar_unary_traits<E, vector_index_norm_inf<typename E::value_type> >::result_type
    index_norm_inf (const vector_expression<E> &e);

Description

sum computes the sum of the vector expression's elements. norm_1, norm_2 and norm_inf compute the corresponding ||.||1, ||.||2 and ||.||inf vector norms. index_norm_1 computes the index of the vector expression's first element having maximal absolute value.

Definition

Defined in the header vector_expression.hpp.

Type requirements

Preconditions

None.

Complexity

Linear depending from the size of the vector expression.

Examples

#include <boost/numeric/ublas/vector.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;

    std::cout << sum (v) << std::endl;
    std::cout << norm_1 (v) << std::endl;
    std::cout << norm_2 (v) << std::endl;
    std::cout << norm_inf (v) << std::endl;
    std::cout << index_norm_inf (v) << std::endl;
}

Binary Reductions

Prototypes

template<class E1, class E2, class F>
    struct vector_scalar_binary_traits {
        typedef typename F::result_type result_type;
    };

    // inner_prod (v1, v2) = sum (v1 [i] * v2 [i])
    template<class E1, class E2>
    typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<typename E1::value_type,
                                                                   typename E2::value_type,
                                                                   typename promote_traits<typename E1::value_type,
                                                                                           typename E2::value_type>::promote_type> >::result_type
    inner_prod (const vector_expression<E1> &e1,
                const vector_expression<E2> &e2);

    template<class E1, class E2>
    typename vector_scalar_binary_traits<E1, E2, vector_inner_prod<typename E1::value_type,
                                                                   typename E2::value_type,
                                                                   typename type_traits<typename promote_traits<typename E1::value_type,
                                                                                                                typename E2::value_type>::promote_type>::precision_type> >::result_type
    prec_inner_prod (const vector_expression<E1> &e1,
                     const vector_expression<E2> &e2);

Description

inner_prod computes the inner product of the vector expressions. prec_inner_prod computes the double precision inner product of the vector expressions.

Definition

Defined in the header vector_expression.hpp.

Type requirements

Preconditions

Complexity

Linear depending from the size of the vector expressions.

Examples

#include <boost/numeric/ublas/vector.hpp>

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v1 (3), v2 (3);
    for (unsigned i = 0; i < std::min (v1.size (), v2.size ()); ++ i)
        v1 (i) = v2 (i) = i;

    std::cout << inner_prod (v1, v2) << std::endl;
}

Copyright (©) 2000-2002 Joerg Walter, Mathias Koch
Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided ``as is'' without express or implied warranty, and with no claim as to its suitability for any purpose.