Basic Linear and Multilinear Algebra Library

uBLAS is a C++ template class library that provides BLAS level 1, 2, 3 functionality for dense, packed and sparse matrices. The design and implementation unify mathematical notation via operator overloading and efficient code generation via expression templates. Since 2018, uBLAS also supports basic operations for multilinear algebra operations such as the outer and inner product of higher-order tensors. The goal of the new tensor extension is to provide basic operations that simplify the implementation of e.g. machine learning and quantum computing algorithms.

Functionality

uBLAS provides templated C++ classes for dense, unit and sparse vectors, dense, identity, triangular, banded, symmetric, hermitian and sparse matrices. Views into vectors and matrices can be constructed via ranges, slices, adaptor classes and indirect arrays. The library covers the usual basic linear and multilinear algebra operations on vectors, matrices and tensors: reductions like different norms, addition and subtraction of vectors and matrices and multiplication with a scalar, inner and outer products of vectors, matrix vector and matrix matrix products and triangular solver. Similar operations are also provided for the tensor type. The glue between containers, views and expression templated operations is a mostly STL conforming iterator interface.

Known limitations

  • The implementation assumes a linear memory address model.

  • Tuning was focussed on dense matrices.

Further Information

Authors and Credits

uBLAS initially was written by Joerg Walter and Mathias Koch. We would like thank all contributors who has supported this library. Amongst many contributors around the world, David Abrahams, Ed Brey, Fernando Cacciola, Juan Jose Gomez Cadenas, Beman Dawes, Matt Davies, Bob Fletcher, Kresimir Fresl, Joachim Kessel, Patrick Kowalzick, Toon Knapen, Hendrik Kueck, John Maddock, Jens Maurer, Alexei Novakov, Gary Powell, Joachim Pyras, Peter Schmitteckert, Jeremy Siek, Markus Steffl, Michael Stevens, Benedikt Weber, Martin Weiser, Gunter Winkler, Marc Zimmermann, Marco Guazzone, Nasos Iliopoulus and the members of Boost had a great impact and contribution helping the library to grow and mature.

Starting with the GSoC 2018 project, uBlas has been extended by a flexible tensor data type and basic tensor operations supporting general tensor contractions and the Einstein notation. The goal of the new tensor extension is to support the implementation of algorithms for e.g. machine learning and quantum computing applications. The initial implementation of this extension is written by Cem Bassoy. Contributors of the uBLAS extension are Amit Singh, Ashar Khan, Stefan Seefeld, Cem Bassoy and the members of Boost.

This library is currently maintained by David Bellot, Cem Bassoy and Stefan Seefeld.

Frequently Asked Questions

Q: Should I use uBLAS for new projects?
A: At the time of writing (09/2012) there are a lot of good matrix libraries available, e.g., MTL4, armadillo, eigen. uBLAS offers a stable, well tested set of vector and matrix classes, the typical operations for linear algebra and solvers for triangular systems of equations. uBLAS offers dense, structured and sparse matrices - all using similar interfaces. And finally uBLAS offers good (but not outstanding) performance. On the other side, the last major improvement of uBLAS was in 2008 and no significant change was committed since 2009. So one should ask himself some questions to aid the decision: Availability? uBLAS is part of boost and thus available in many environments. Easy to use? uBLAS is easy to use for simple things, but needs decent C knowledge when you leave the path. _Performance?_ There are faster alternatives. _Cutting edge?_ uBLAS is more than 10 years old and missed all new stuff from C11.

Q: I’m running the uBLAS dense vector and matrix benchmarks. Why do I see a significant performance difference between the native C and library implementations?
A: uBLAS distinguishes debug mode (size and type conformance checks enabled, expression templates disabled) and release mode (size and type conformance checks disabled, expression templates enabled). Please check, if the preprocessor symbol NDEBUG of cassert is defined. NDEBUG enables release mode, which in turn uses expression templates. You can optionally define BOOST_UBLAS_NDEBUG to disable all bounds, structure and similar checks of uBLAS.

Q: I’ve written some uBLAS tests, which try to incorrectly assign different matrix types or overrun vector and matrix dimensions. Why don’t I get a compile time or runtime diagnostic?
A: uBLAS distinguishes debug mode (size and type conformance checks enabled, expression templates disabled) and release mode (size and type conformance checks disabled, expression templates enabled). Please check, if the preprocessor symbol NDEBUG of cassert is defined. NDEBUG disables debug mode, which is needed to get size and type conformance checks.

Q: I’ve written some uBLAS benchmarks to measure the performance of matrix chain multiplications like prod (A, prod (B, C)) and see a significant performance penalty due to the use of expression templates. How can I disable expression templates?
A: You do not need to disable expression templates. Please try reintroducing temporaries using either prod (A, matrix_type (prod (B, C))) or prod (A, prod<``matrix_type > (B, C)).

Overview

Rationale

It would be nice if every kind of numeric software could be written in C without loss of efficiency, but unless something can be found that achieves this without compromising the C type system it may be preferable to rely on Fortran, assembler or architecture-specific extensions (Bjarne Stroustrup).

This C++ library is directed towards scientific computing on the level of basic linear algebra constructions with matrices and vectors and their corresponding abstract operations. The primary design goals were:

  • mathematical notation

  • efficiency

  • functionality

  • compatibility

Another intention was to evaluate, if the abstraction penalty resulting from the use of such matrix and vector classes is acceptable.

Resources

The development of this library was guided by a couple of similar efforts:

  • BLAS by Jack Dongarra et al.

  • Blitz++ by Todd Veldhuizen

  • POOMA by Scott Haney et al.

  • MTL by Jeremy Siek et al.

BLAS seems to be the most widely used library for basic linear algebra constructions, so it could be called a de-facto standard. Its interface is procedural, the individual functions are somewhat abstracted from simple linear algebra operations. Due to the fact that is has been implemented using Fortran and its optimizations, it also seems to be one of the fastest libraries available. As we decided to design and implement our library in an object-oriented way, the technical approaches are distinct. However anyone should be able to express BLAS abstractions in terms of our library operators and to compare the efficiency of the implementations.

Blitz is an impressive library implemented in C. Its main design seems to be oriented towards multidimensional arrays and their associated operators including tensors. The author of Blitz states, that the library achieves performance on par or better than corresponding Fortran code due to his implementation technique using expression templates and template metaprograms. However we see some reasons, to develop an own design and implementation approach. We do not know whether anybody tries to implement traditional linear algebra and other numerical algorithms using Blitz. We also presume that even today Blitz needs the most advanced C compiler technology due to its implementation idioms. On the other hand, Blitz++ convinced us, that the use of expression templates is mandatory to reduce the abstraction penalty to an acceptable limit.

POOMA’s design goals seem to parallel Blitz's in many parts . It extends Blitz's concepts with classes from the domains of partial differential equations and theoretical physics. The implementation supports even parallel architectures.

MTL is another approach supporting basic linear algebra operations in C. Its design mainly seems to be influenced by BLAS and the C Standard Template Library. We share the insight that a linear algebra library has to provide functionality comparable to BLAS. On the other hand we think, that the concepts of the C++ standard library have not yet been proven to support numerical computations as needed. As another difference MTL currently does not seem to use expression templates. This may result in one of two consequences: a possible loss of expressiveness or a possible loss of performance.

Concepts

Mathematical Notation

The usage of mathematical notation may ease the development of scientific algorithms. So a C library implementing basic linear algebra concepts carefully should overload selected C operators on matrix and vector classes.

We decided to use operator overloading for the following primitives:

Description Operator

Indexing of vectors and matrices

vector::operator(size_t i); matrix::operator(size_t i, size_t j);

Assignment of vectors and matrices

vector::operator = (const vector_expression &); vector::operator += (const vector_expression &); vector::operator -= (const vector_expression &); vector::operator *= (const scalar_expression &); matrix::operator = (const matrix_expression &); matrix::operator += (const matrix_expression &); matrix::operator -= (const matrix_expression &); matrix::operator *= (const scalar_expression &);

Unary operations on vectors and matrices

vector_expression operator - (const vector_expression &); matrix_expression operator - (const matrix_expression &);

Binary operations on vectors and matrices

vector_expression operator + (const vector_expression &, const vector_expression &); vector_expression operator - (const vector_expression &, const vector_expression &); matrix_expression operator + (const matrix_expression &, const matrix_expression &); matrix_expression operator - (const matrix_expression &, const matrix_expression &);

Multiplication of vectors and matrices with a scalar

vector_expression operator * (const scalar_expression &, const vector_expression &); vector_expression operator * (const vector_expression &, const scalar_expression &); matrix_expression operator * (const scalar_expression &, const matrix_expression &); matrix_expression operator * (const matrix_expression &, const scalar_expression &);

We decided to use no operator overloading for the following other primitives:

Description Function

Left multiplication of vectors with a matrix

vector_expression prod<vector_type > (const matrix_expression &, const vector_expression &); vector_expression prod (const matrix_expression &, const vector_expression &);

Right multiplication of vectors with a matrix

vector_expression prod<vector_type > (const vector_expression &, const matrix_expression &); vector_expression prod (const vector_expression &, const matrix_expression &);

Multiplication of matrices

matrix_expression prod<matrix_type > (const matrix_expression &, const matrix_expression &); matrix_expression prod (const matrix_expression &, const matrix_expression &);

Inner product of vectors

scalar_expression inner_prod (const vector_expression &, const vector_expression &);

Outer product of vectors

matrix_expression outer_prod (const vector_expression &, const vector_expression &);

Transpose of a matrix

matrix_expression trans (const matrix_expression &);

Efficiency

To achieve the goal of efficiency for numerical computing, one has to overcome two difficulties in formulating abstractions with C++, namely temporaries and virtual function calls. Expression templates solve these problems, but tend to slow down compilation times.

Eliminating Temporaries

Abstract formulas on vectors and matrices normally compose a couple of unary and binary operations. The conventional way of evaluating such a formula is first to evaluate every leaf operation of a composition into a temporary and next to evaluate the composite resulting in another temporary. This method is expensive in terms of time especially for small and space especially for large vectors and matrices. The approach to solve this problem is to use lazy evaluation as known from modern functional programming languages. The principle of this approach is to evaluate a complex expression element wise and to assign it directly to the target.

Two interesting and dangerous facts result:

Aliases

One may get serious side effects using element wise evaluation on vectors or matrices. Consider the matrix vector product x = A x. Evaluation of A1x and assignment to x1 changes the right hand side, so that the evaluation of A2x returns a wrong result. In this case there are aliases of the elements xn on both the left and right hand side of the assignment.

Our solution for this problem is to evaluate the right hand side of an assignment into a temporary and then to assign this temporary to the left hand side. To allow further optimizations, we provide a corresponding member function for every assignment operator and also a noalias syntax. By using this syntax a programmer can confirm, that the left and right hand sides of an assignment are independent, so that element wise evaluation and direct assignment to the target is safe.

Complexity

The computational complexity may be unexpectedly large under certain cirumstances. Consider the chained matrix vector product A (B x). Conventional evaluation of A (B x) is quadratic. Deferred evaluation of B xi is linear. As every element B xi is needed linearly depending of the size, a completely deferred evaluation of the chained matrix vector product A (B x) is cubic. In such cases one needs to reintroduce temporaries in the expression.

Eliminating Virtual Function Calls

Lazy expression evaluation normally leads to the definition of a class hierarchy of terms. This results in the usage of dynamic polymorphism to access single elements of vectors and matrices, which is also known to be expensive in terms of time. A solution was found a couple of years ago independently by David Vandervoorde and Todd Veldhuizen and is commonly called expression templates. Expression templates contain lazy evaluation and replace dynamic polymorphism with static, i.e. compile time polymorphism. Expression templates heavily depend on the famous Barton-Nackman trick, also coined 'curiously defined recursive templates' by Jim Coplien.

Expression templates form the base of our implementation.

Compilation times

It is also a well known fact, that expression templates challenge currently available compilers. We were able to significantly reduce the amount of needed expression templates using the Barton-Nackman trick consequently.

We also decided to support a dual conventional implementation (i.e. not using expression templates) with extensive bounds and type checking of vector and matrix operations to support the development cycle. Switching from debug mode to release mode is controlled by the NDEBUG preprocessor symbol of <cassert>.

Functionality

Every C++ library supporting linear algebra will be measured against the long-standing Fortran package BLAS. We now describe how BLAS calls may be mapped onto our classes.

The page Overview of Matrix and Vector Operations gives a short summary of the most used operations on vectors and matrices.

Blas Level 1
BLAS Call Mapped Library Expression Mathematical Description Comment

sasum OR dasum

norm_1 (x)

sum(mod(xi))

Computes the l1 (sum) norm of a real vector.

scasum OR dzasum

real (sum (v)) + imag (sum (v))

sum re(xi) + sum im(xi)

Computes the sum of elements of a complex vector.

_nrm2

norm_2 (x)

sqrt sum(mod(xi)2)

Computes the l2 (euclidean) norm of a vector.

i_amax

norm_inf (x) index_norm_inf (x)

max(mod(xi))

Computes the linf (maximum) norm of a vector.
BLAS computes the index of the first element having this value.

_dot _dotu _dotc

inner_prod (x, y) or inner_prod (conj (x), y)

xT y or
xH y

Computes the inner product of two vectors.
BLAS implements certain loop unrollment.

dsdot sdsdot

a + prec_inner_prod (x, y)

a + xT y

Computes the inner product in double precision.

_copy

x = y y.assign (x)

x ← y

Copies one vector to another.
BLAS implements certain loop unrollment.

_swap

swap (x, y)

x ←→ y

Swaps two vectors.
BLAS implements certain loop unrollment.

_scal csscal zdscal

x *= a

x ← a x

Scales a vector.
BLAS implements certain loop unrollment.

_axpy

y += a * x

y ← a x + y

Adds a scaled vector.
BLAS implements certain loop unrollment.

_rot _rotm csrot zdrot

t.assign (a * x + b * y), y.assign (- b * x + a * y), x.assign (t)

(x, y) ← (a x + b y, -b x + a y)

Applies a plane rotation.

_rotg _rotmg

 

(a, b) ←
  (? a / sqrt (a
2 + b2),
    ? b / sqrt (a
2 + b2)) or
(1, 0) ← (0, 0)

Constructs a plane rotation.

Blas Level 2
BLAS Call Mapped Library Expression Mathematical Description Comment

_t_mv

x = prod (A, x) or x = prod (trans (A), x) or x = prod (herm (A), x)

x ← A x or + x ← AT x or + x ← AH x

Computes the product of a matrix with a vector.

_t_sv

y = solve (A, x, tag) or
inplace_solve (A, x, tag) or
y = solve (trans (A), x, tag) or
inplace_solve (trans (A), x, tag) or
y = solve (herm (A), x, tag)`or
`inplace_solve (herm (A), x, tag)

y ← A-1 x or
x ← A
-1 x or
y ← AT-1 x or

x ← AT-1 x or
y ← AH-1 x or

x ← AH-1 x

Solves a system of linear equations with triangular form, i.e. A is triangular.

_g_mv _s_mv _h_mv

y = a * prod (A, x) + b * y or` y = a * prod (trans (A), x) + b * y` or` y = a * prod (herm (A), x) + b * y`

y ← a A x + b y or
y ← a A
T x + b y
y ← a A
H x + b y

Adds the scaled product of a matrix with a vector.

_g_r _g_ru _g_rc

A += a * outer_prod (x, y) or` A += a * outer_prod (x, conj (y))`

A ← a x yT + A or
A ← a x y
H + A

Performs a rank 1 update.

_s_r _h_r

A += a * outer_prod (x, x) or` A += a * outer_prod (x, conj (x))`

A ← a x xT + A or
A ← a x x
H + A

Performs a symmetric or hermitian rank 1 update.

_s_r2 _h_r2

A += a * outer_prod (x, y) +  a * outer_prod (y, x)) or` A += a * outer_prod (x, conj (y)) +  conj (a) * outer_prod (y, conj (x)))`

A ← a x yT + a y xT + A or
A ← a x y
H + a- y xH + A

Performs a symmetric or hermitian rank 2 update.

Blas Level 3
BLAS Call Mapped Library Expression Mathematical Description Comment

_t_mm

B = a * prod (A, B) or
B = a * prod (trans (A), B) or
B = a * prod (A, trans (B)) or
B = a * prod (trans (A), trans (B)) or
B = a * prod (herm (A), B) or
B = a * prod (A, herm (B)) or
B = a * prod (herm (A), trans (B)) or
B = a * prod (trans (A), herm (B)) or
B = a * prod (herm (A), herm (B))

B ← a op (A) op (B) with
  op (X) = X or
  op (X) = XT or
  op (X) = XH

Computes the scaled product of two matrices.

_t_sm

C = solve (A, B, tag) or
inplace_solve (A, B, tag) or
C = solve (trans (A), B, tag) or` inplace_solve (trans (A), B, tag)` or` C = solve (herm (A), B, tag)` or` inplace_solve (herm (A), B, tag)`

C ← A-1 B or
B ← A
-1 B or
C ← AT-1 B or

B ← A-1 B or
C ← AH-1 B or

B ← AH-1 B

Solves a system of linear equations with triangular form, i.e. A is triangular.

_g_mm _s_mm _h_mm

C = a * prod (A, B) + b * C or
C = a * prod (trans (A), B) + b * C or
C = a * prod (A, trans (B)) + b * C or
C = a * prod (trans (A), trans (B)) + b * C or
C = a * prod (herm (A), B) + b * C or
C = a * prod (A, herm (B)) + b * C or
C = a * prod (herm (A), trans (B)) + b * C or
C = a * prod (trans (A), herm (B)) + b * C or
C = a * prod (herm (A), herm (B)) + b * C

C ← a op (A) op (B) + b C with
  op (X) = X or
  op (X) = XT or
  op (X) = XH

Adds the scaled product of two matrices.

_s_rk _h_rk

B = a * prod (A, trans (A)) + b * B or
B = a * prod (trans (A), A) + b * B or
B = a * prod (A, herm (A)) + b * B or
B = a * prod (herm (A), A) + b * B

B ← a A AT + b B or
B ← a A
T A + b B or
B ← a A AH + b B or
B ← a A
H A + b B

Performs a symmetric or hermitian rank k update.

_s_r2k _h_r2k

C = a * prod (A, trans (B)) +  a * prod (B, trans (A)) + b * C or
C = a * prod (trans (A), B) +  a * prod (trans (B), A) + b * C or
C = a * prod (A, herm (B)) +  conj (a) * prod (B, herm (A)) + b * C or
C = a * prod (herm (A), B) +  conj (a) * prod (herm (B), A) + b * C

C ← a A BT + a B AT + b C or
C ← a A
T B + a BT A + b C or
C ← a A B
H + a- B AH + b C or
C ← a A
H B + a- BH A + b C

Performs a symmetric or hermitian rank 2 k update.

Storage Layout

uBLAS supports many different storage layouts. The full details can be found at the Overview of Types. Most types like vector<double> and matrix<double> are by default compatible to C arrays, but can also be configured to contain FORTAN compatible data.

Compatibility

For compatibility reasons we provide array like indexing for vectors and matrices. For some types (hermitian, sparse etc) this can be expensive for matrices due to the needed temporary proxy objects.

uBLAS uses STL compatible allocators for the allocation of the storage required for it’s containers.

Benchmark Results

The following tables contain results of one of our benchmarks. This benchmark compares a native C implementation ('C array') and some library based implementations. The safe variants based on the library assume aliasing, the fast variants do not use temporaries and are functionally equivalent to the native C implementation. Besides the generic vector and matrix classes the benchmark utilizes special classes c_vector and c_matrix, which are intended to avoid every overhead through genericity.

The benchmark program bench1 was compiled with GCC 4.0 and run on an Athlon 64 3000+. Times are scales for reasonable precision by running bench1 100.

First we comment the results for double vectors and matrices of dimension 3 and 3 x 3, respectively.

Comment

inner_prod

C array

0.61

782

Some abstraction penalty

c_vector

0.86

554

vector<unbounded_array>

1.02

467

vector + vector

C array

0.51

1122

Abstraction penalty: factor 2

c_vector fast

1.17

489

vector<unbounded_array> fast

1.32

433

c_vector safe

2.02

283

vector<unbounded_array> safe

6.95

82

outer_prod

C array

0.59

872

Some abstraction penalty

c_matrix, c_vector fast

0.88

585

matrix<unbounded_array>, vector<unbounded_array> fast

0.90

572

c_matrix, c_vector safe

1.66

310

matrix<unbounded_array>, vector<unbounded_array> safe

2.95

175

prod (matrix, vector)

C array

0.64

671

No significant abstraction penalty

c_matrix, c_vector fast

0.70

613

matrix<unbounded_array>, vector<unbounded_array> fast

0.79

543

c_matrix, c_vector safe

0.95

452

matrix<unbounded_array>, vector<unbounded_array> safe

2.61

164

matrix + matrix

C array

0.75

686

No significant abstraction penalty

c_matrix fast

0.99

520

matrix<unbounded_array> fast

1.29

399

c_matrix safe

1.7

303

matrix<unbounded_array> safe

3.14

164

prod (matrix, matrix)

C array

0.94

457

No significant abstraction penalty

c_matrix fast

1.17

367

matrix<unbounded_array> fast

1.34

320

c_matrix safe

1.56

275

matrix<unbounded_array> safe

2.06

208

We notice a two fold performance loss for small vectors and matrices: first the general abstraction penalty for using classes, and then a small loss when using the generic vector and matrix classes. The difference w.r.t. alias assumptions is also significant.

Next we comment the results for double vectors and matrices of dimension 100 and 100 x 100, respectively.

Operation Implementation Elapsed [s] MFLOP/s Comment

inner_prod

C array

0.64

889

No significant abstraction penalty

c_vector

0.66

862

vector<unbounded_array>

0.66

862

vector + vector

C array

0.64

894

No significant abstraction penalty

c_vector fast

0.66

867

vector<unbounded_array> fast

0.66

867

c_vector safe

1.14

501

vector<unbounded_array> safe

1.23

465

outer_prod

C array

0.50

1144

No significant abstraction penalty

c_matrix, c_vector fast

0.71

806

matrix<unbounded_array>, vector<unbounded_array> fast

0.57

1004

c_matrix, c_vector safe

1.91

300

matrix<unbounded_array>, vector<unbounded_array> safe

0.89

643

prod (matrix, vector)

C array

0.65

876

No significant abstraction penalty

c_matrix, c_vector fast

0.65

876

matrix<unbounded_array>, vector<unbounded_array> fast

0.66

863

c_matrix, c_vector safe

0.66

863

matrix<unbounded_array>, vector<unbounded_array> safe

0.66

863

matrix + matrix

C array

0.96

596

No significant abstraction penalty

c_matrix fast

1.21

473

matrix<unbounded_array> fast

1.00

572

c_matrix safe

2.44

235

matrix<unbounded_array> safe

1.30

440

prod (matrix, matrix)

C array

0.70

813

No significant abstraction penalty

c_matrix fast

0.73

780

matrix<unbounded_array> fast

0.76

749

c_matrix safe

0.75

759

matrix<unbounded_array> safe

0.76

749

For larger vectors and matrices the general abstraction penalty for using classes seems to decrease, the small loss when using generic vector and matrix classes seems to remain. The difference w.r.t. alias assumptions remains visible, too.

Vector

Vector Abstract

Description

The templated class vector<T, A> is the base container adaptor for dense vectors. For a n-dimensional vector and 0 < = i < n every element vi is mapped to the i-th element of the container.

Example
#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 << v << std::endl;
}
Definition

Defined in the header vector.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the vector.

A

The type of the Storage array. [1]

unbounded_array<T>

Type requirements

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

Public base classes

vector_container<vector<T, A> >

Members
Member Where defined Description

value_type

VectorExpression

reference

VectorExpression

const_reference

VectorExpression

size_type

VectorExpression

difference_type

VectorExpression

const_iterator

VectorExpression

iterator

VectorExpression

const_reverse_iterator

VectorExpression

reverse_iterator

VectorExpression

array_type

Vector

vector ()

VectorExpression

Allocates an uninitialized vector that holds zero elements.

vector (size_type size)

Vector

Allocates an uninitialized vector that holds size elements.

vector (const vector &v)

The copy constructor.

template<class AE> vector (const vector_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Vector

Reallocates a vector to hold size elements. The existing elements of the vector are preseved when specified.

size_type size () const

VectorExpression

Returns the size of the vector.

size_type max_size () const

RandomAccessContainer

Returns the upper bound on the size of the vector.

bool empty () const

RandomAccessContainer

Equivilent to size () == 0.

const array_type& data () const

Vector

array_type& data ()

Vector

const_reference operator () (size_type i) const

VectorExpression

Returns a const reference of the i -th element.

reference operator () (size_type i)

VectorExpression

Returns a reference of the i-th element.

const_reference operator [] (size_type i) const

Vector

Returns a const reference of the i -th element.

reference operator [] (size_type i)

Vector

Returns a reference of the i-th element.

vector &operator = (const vector &v)

VectorExpression

The assignment operator.

vector &assign_temporary (vector &v)

VectorExpression

Assigns a temporary. May change the vector v.

template<class AE> vector &operator = (const vector_expression<AE> &ae)

VectorExpression

The extended assignment operator.

template<class AE> vector &assign (const vector_expression<AE> &ae)

VectorExpression

Assigns a vector expression to the vector. Left and right hand side of the assignment should be independent.

template<class AE> vector &operator += (const vector_expression<AE> &ae)

VectorExpression

A computed assignment operator. Adds the vector expression to the vector.

template<class AE> vector &plus_assign (const vector_expression<AE> &ae)

VectorExpression

Adds a vector expression to the vector. Left and right hand side of the assignment should be independent.

template<class AE> vector &operator -= (const vector_expression<AE> &ae)

VectorExpression

A computed assignment operator. Subtracts the vector expression from the vector.

template<class AE> vector &minus_assign (const vector_expression<AE> &ae)

VectorExpression

Subtracts a vector expression from the vector. Left and right hand side of the assignment should be independent.

template<class AT> vector &operator *= (const AT &at)

VectorExpression

A computed assignment operator. Multiplies the vector with a scalar.

template<class AT> vector &operator /= (const AT &at)

VectorExpression

A computed assignment operator. Divides the vector through a scalar.

void swap (vector &v)

VectorExpression

Swaps the contents of the vectors.

void insert_element (size_type i, const_reference t)

Vector

Inserts the value t at the i-th element.

void erase_element (size_type i)

Vector

Erases the value at the i-th element.

void clear ()

Vector

Clears the vector.

const_iterator begin () const

VectorExpression

Returns a const_iterator pointing to the beginning of the vector.

const_iterator end () const

VectorExpression

Returns a const_iterator pointing to the end of the vector.

iterator begin ()

VectorExpression

Returns a iterator pointing to the beginning of the vector.

iterator end ()

VectorExpression

Returns a iterator pointing to the end of the vector.

const_reverse_iterator rbegin () const

VectorExpression

Returns a const_reverse_iterator pointing to the beginning of the reversed vector.

const_reverse_iterator rend () const

VectorExpression

Returns a const_reverse_iterator pointing to the end of the reversed vector.

reverse_iterator rbegin ()

VectorExpression

Returns a reverse_iterator pointing to the beginning of the reversed vector.

reverse_iterator rend ()

VectorExpression

Returns a reverse_iterator pointing to the end of the reversed vector.

Notes

[1] Common parameters for the Storage array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Unit Vector
Description

The templated class unit_vector<T, ALLOC> represents canonical unit vectors. For the k-th n-dimensional canonical unit vector and 0 ⇐ i < n holds uki = 0, if i <> k, and uki = 1.

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

int main () {
    using namespace boost::numeric::ublas;
    for (int i = 0; i < 3; ++ i) {
        unit_vector<double> v (3, i);
        std::cout << v << std::endl;
    }
}
Definition

Defined in the header vector.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the vector.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Vector .

Type requirements

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

Public base classes

vector_container<unit_vector<T> >

Members
Member Description

unit_vector ()

Constructs an unit_vector that holds zero elements.

unit_vector (size_type size, size_type index)

Constructs the index-th unit_vector that holds size elements.

unit_vector (const unit_vector &v)

The copy constructor.

void resize (size_type size, bool preserve = true)

Resizes a unit_vector to hold size elements. Therefore the existing elements of the unit_vector are always preseved.

size_type size () const

Returns the size of the unit_vector.

size_type index () const

Returns the index of the unit_vector.

const_reference operator () (size_type i) const

Returns the value of the i-th element.

const_reference operator [] (size_type i) const

Returns the value of the i-th element.

unit_vector &operator = (const unit_vector &v)

The assignment operator.

unit_vector &assign_temporary (unit_vector &v)

Assigns a temporary. May change the unit vector v .

void swap (unit_vector &v)

Swaps the contents of the unit vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the unit_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the unit_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed unit_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed unit_vector.

Zero Vector
Description

The templated class zero_vector<T, ALLOC> represents zero vectors. For a n-dimensional zero vector and 0 ⇐ i < n holds zi = 0.

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

int main () {
    using namespace boost::numeric::ublas;
    zero_vector<double> v (3);
    std::cout << v << std::endl;
}
Definition

Defined in the header vector.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the vector.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Vector .

Type requirements

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

Public base classes

vector_container<zero_vector<T> >

Members
Member Description

zero_vector ()

Constructs a zero_vector that holds zero elements.

zero_vector (size_type size)

Constructs a zero_vector that holds size elements.

zero_vector (const zero_vector &v)

The copy constructor.

void resize (size_type size, bool preserve = true)

Resizes a zero_vector to hold size elements. Therefore the existing elements of the zero_vector are always preseved.

size_type size () const

Returns the size of the zero_vector.

const_reference operator () (size_type i) const

Returns the value of the i-th element.

const_reference operator [] (size_type i) const

Returns the value of the i-th element.

zero_vector &operator = (const zero_vector &v)

The assignment operator.

zero_vector &assign_temporary (zero_vector &v)

Assigns a temporary. May change the zero vector v .

void swap (zero_vector &v)

Swaps the contents of the zero vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the zero_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the zero_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed zero_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed zero_vector.

Scalar Vector
Description

The templated class scalar_vector<T, ALLOC> represents scalar vectors. For a n-dimensional scalar vector and 0 ⇐ i < n holds zi = s.

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

int main () {
    using namespace boost::numeric::ublas;
    scalar_vector<double> v (3);
    std::cout << v << std::endl;
}
Definition

Defined in the header vector.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the vector.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Vector .

Type requirements

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

Public base classes

vector_container<scalar_vector<T> >

Members
Member Description

scalar_vector ()

Constructs a scalar_vector that holds zero elements.

scalar_vector (size_type size, const value_type &value)

Constructs a scalar_vector that holds size elements each of the specified value.

scalar_vector (const scalar_vector &v)

The copy constructor.

void resize (size_type size, bool preserve = true)

Resizes a scalar_vector to hold size elements. Therefore the existing elements of the scalar_vector are always preseved.

size_type size () const

Returns the size of the scalar_vector.

const_reference operator () (size_type i) const

Returns the value of the i-th element.

const_reference operator [] (size_type i) const

Returns the value of the i-th element.

scalar_vector &operator = (const scalar_vector &v)

The assignment operator.

scalar_vector &assign_temporary (scalar_vector &v)

Assigns a temporary. May change the scalar vector v .

void swap (scalar_vector &v)

Swaps the contents of the scalar vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the scalar_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the scalar_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed scalar_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed scalar_vector.

Sparse Vector

Mapped Vector
Description

The templated class mapped_vector<T, A> is the base container adaptor for sparse vectors using element maps. For a n-dimensional sparse vector and 0 < = i < n the non-zero elements vi are mapped to consecutive elements of the associative container, i.e. for elements k = vi1 and k + 1 = vi2 of the container holds i1 < i2 .

Example
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    mapped_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}
Definition

Defined in the header vector_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the mapped vector.

A

The type of the adapted array. [1]

map_std<std::size_t, T>

Model of

Vector .

Type requirements

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

Public base classes

vector_container<mapped_vector<T, A> >

Members
Member Description

mapped_vector ()

Allocates a mapped_vector that holds zero elements.

mapped_vector (size_type size, size_type non_zeros = 0)

Allocates a mapped_vector that holds at most size elements.

mapped_vector (const mapped_vector &v)

The copy constructor.

template<class AE> mapped_vector (size_type non_zeros, const vector_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Reallocates a mapped_vector to hold at most size elements. The existing elements of the mapped_vector are preseved when specified.

size_type size () const

Returns the size of the mapped_vector.

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_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.

mapped_vector &operator = (const mapped_vector &v)

The assignment operator.

mapped_vector &assign_temporary (mapped_vector &v)

Assigns a temporary. May change the mapped vector v .

template<class AE> mapped_vector &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> mapped_vector &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the mapped vector. Left and right hand side of the assignment should be independent.

template<class AE> mapped_vector &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the mapped vector.

template<class AE> mapped_vector &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the mapped vector. Left and right hand side of the assignment should be independent.

template<class AE> mapped_vector &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the mapped vector.

template<class AE> mapped_vector &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the mapped vector. Left and right hand side of the assignment should be independent.

template<class AT> mapped_vector &operator *= (const AT &at)

A computed assignment operator. Multiplies the mapped vector with a scalar.

template<class AT> mapped_vector &operator /= (const AT &at)

A computed assignment operator. Divides the mapped vector through a scalar.

void swap (mapped_vector &v)

Swaps the contents of the mapped vectors.

true_reference insert_element (size_type i, const_reference t)

Inserts the value t at the i-th element. Duplicates elements are not allowed.

void erase_element (size_type i)

Erases the value at the i-th element.

void clear ()

Clears the mapped vector.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the mapped_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the mapped_vector.

iterator begin ()

Returns a iterator pointing to the beginning of the mapped_vector.

iterator end ()

Returns a iterator pointing to the end of the mapped_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed mapped_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed mapped_vector.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed mapped_vector.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed mapped_vector.

Notes

[1] Supported parameters for the adapted array are map_array<std::size_t, T> and map_std<std::size_t, T>. The latter is equivalent to std::map<std::size_t, T>.

Compressed Vector
Description

The templated class compressed_vector<T, IB, IA, TA> is the base container adaptor for compressed vectors. For a n-dimensional compressed vector and 0 ⇐ i < n the non-zero elements vi are mapped to consecutive elements of the index and value container, i.e. for elements k = vi1 and k + 1 = vi2 of these containers holds i1 < i2 .

Example
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    compressed_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}
Definition

Defined in the header vector_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the compressed vector.

IB

The index base of the compressed vector. [1]

0

IA

The type of the adapted array for indices. [2]

unbounded_array<std::size_t>

TA

The type of the adapted array for values. [2]

unbounded_array<T>

Model of

Vector .

Type requirements

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

Public base classes

vector_container<compressed_vector<T, IB, IA, TA> >

Members
Member Description

compressed_vector ()

Allocates a compressed_vector that holds zero elements.

compressed_vector (size_type size, size_type non_zeros)

Allocates a compressed_vector that holds at most size elements.

compressed_vector (const compressed_vector &v)

The copy constructor.

template<class AE> compressed_vector (size_type non_zeros, const vector_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Reallocates a compressed_vector to hold at most size elements. The existing elements of the compress_vector are preseved when specified.

size_type size () const

Returns the size of the compressed_vector.

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_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.

compressed_vector &operator = (const compressed_vector &v)

The assignment operator.

compressed_vector &assign_temporary (compressed_vector &v)

Assigns a temporary. May change the compressed vector v.

template<class AE> compressed_vector &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> compressed_vector &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the compressed vector. Left and right hand side of the assignment should be independent.

template<class AE> compressed_vector &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the compressed vector.

template<class AE> compressed_vector &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the compressed vector. Left and right hand side of the assignment should be independent.

template<class AE> compressed_vector &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the compressed vector.

template<class AE> compressed_vector &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the compressed vector. Left and right hand side of the assignment should be independent.

template<class AT> compressed_vector &operator *= (const AT &at)

A computed assignment operator. Multiplies the compressed vector with a scalar.

template<class AT> compressed_vector &operator /= (const AT &at)

A computed assignment operator. Divides the compressed vector through a scalar.

void swap (compressed_vector &v)

Swaps the contents of the compressed vectors.

true_reference insert_element (size_type i, const_reference t)

Inserts the value t at the i-th element. Duplicates elements are not allowed.

void erase_element (size_type i)

Erases the value at the i-th element.

void clear ()

Clears the compressed vector.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the compressed_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the compressed_vector.

iterator begin ()

Returns a iterator pointing to the beginning of the compressed_vector.

iterator end ()

Returns a iterator pointing to the end of the compressed_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed compressed_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed compressed_vector.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed compressed_vector.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed compressed_vector.

Notes

[1] Supported parameters for the index base are 0 and 1 at least.

[2] Supported parameters for the adapted array are unbounded_array<> , bounded_array<> and std::vector<> .

Coordinate Vector
Description

The templated class coordinate_vector<T, IB, IA, TA> is the base container adaptor for compressed vectors. For a n-dimensional sorted coordinate vector and 0 ⇐ i < n the non-zero elements vi are mapped to consecutive elements of the index and value container, i.e. for elements k = vi1 and k + 1 = vi2 of these containers holds i1 < i2 .

Example
#include <boost/numeric/ublas/vector_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    coordinate_vector<double> v (3, 3);
    for (unsigned i = 0; i < v.size (); ++ i)
        v (i) = i;
    std::cout << v << std::endl;
}
Definition

Defined in the header vector_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the coordinate vector.

IB

The index base of the coordinate vector. [1]

0

IA

The type of the adapted array for indices. [2]

unbounded_array<std::size_t>

TA

The type of the adapted array for values. [2]

unbounded_array<T>

Model of

Vector .

Type requirements

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

Public base classes

vector_container<coordinate_vector<T, IB, IA, TA> >

Members
Member Description

coordinate_vector ()

Allocates a coordinate_vector that holds zero elements.

coordinate_vector (size_type size, size_type non_zeros)

Allocates a coordinate_vector that holds at most size elements.

coordinate_vector (const coordinate_vector &v)

The copy constructor.

template<class AE> coordinate_vector (size_type non_zeros, const vector_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Reallocates a coordinate_vector to hold at most size elements. The existing elements of the coordinate_vector are preseved when specified.

size_type size () const

Returns the size of the coordinate_vector.

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_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.

coordinate_vector &operator = (const coordinate_vector &v)

The assignment operator.

coordinate_vector &assign_temporary (coordinate_vector &v)

Assigns a temporary. May change the coordinate vector v.

template<class AE> coordinate_vector &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> coordinate_vector &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the coordinate vector. Left and right hand side of the assignment should be independent.

template<class AE> coordinate_vector &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the coordinate vector.

template<class AE> coordinate_vector &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the coordinate vector. Left and right hand side of the assignment should be independent.

template<class AE> coordinate_vector &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the coordinate vector.

template<class AE> coordinate_vector &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the coordinate vector. Left and right hand side of the assignment should be independent.

template<class AT> coordinate_vector &operator *= (const AT &at)

A computed assignment operator. Multiplies the coordinate vector with a scalar.

template<class AT> coordinate_vector &operator /= (const AT &at)

A computed assignment operator. Divides the coordinate vector through a scalar.

void swap (coordinate_vector &v)

Swaps the contents of the coordinate vectors.

true_reference insert_element (size_type i, const_reference t)

Inserts the value t at the i-th element. Duplicates elements are not allowed.

void append_element (size_type i, size_type j, const_reference t)

Appends the value t at the i-th element. Duplicate elements can be appended to a coordinate_vector. They are merged into a single arithmetically summed element by the sort function.

void erase_element (size_type i)

Erases the value at the i-th element.

void clear ()

Clears the coordinate vector.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the coordinate_vector.

const_iterator end () const

Returns a const_iterator pointing to the end of the coordinate_vector.

iterator begin ()

Returns a iterator pointing to the beginning of the coordinate_vector.

iterator end ()

Returns a iterator pointing to the end of the coordinate_vector.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed coordinate_vector.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed coordinate_vector.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed coordinate_vector.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed coordinate_vector.

Notes

[1] Supported parameters for the index base are 0 and 1 at least.

[2] Supported parameters for the adapted array are unbounded_array<> , bounded_array<> and std::vector<> .

Vector Proxies

Vector Range
Description

The templated class vector_range<V> allows addressing a sub-range of a vector’s element.

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

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    vector_range<vector<double> > vr (v, range (0, 3));
    for (unsigned i = 0; i < vr.size (); ++ i)
        vr (i) = i;
    std::cout << vr << std::endl;
}
Definition

Defined in the header vector_proxy.hpp.

Template parameters

Parameter

Description

Default

V

The type of vector referenced.

Model of

If the specified range falls outside that of the index range of the vector, then the vector_range is not a well formed Vector Expression. That is, access to an element which is outside of index range of the vector is undefined.

Type requirements

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

Public base classes

vector_expression<vector_range<V> >

Members
Member Description

vector_range (vector_type &data, const range &r)

Constructs a sub vector.

size_type start () const

Returns the start of the sub vector.

size_type size () const

Returns the size of the sub vector.

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_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.

vector_range &operator = (const vector_range &vr)

The assignment operator.

vector_range &assign_temporary (vector_range &vr)

Assigns a temporary. May change the vector range vr .

template<class AE> vector_range &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> vector_range &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> vector_range &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> vector_range &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> vector_range &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> vector_range &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> vector_range &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> vector_range &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (vector_range &vr)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the vector_range.

const_iterator end () const

Returns a const_iterator pointing to the end of the vector_range.

iterator begin ()

Returns a iterator pointing to the beginning of the vector_range.

iterator end ()

Returns a iterator pointing to the end of the vector_range.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed vector_range.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed vector_range.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed vector_range.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed vector_range.

Simple Projections
Description

The free subrange functions support the construction of vector ranges.

Prototypes
    template<class V>
    vector_range<V> subrange (V &data,
       V::size_type start, V::size_type stop);
    template<class V>
    const vector_range<const V> subrange (const V &data,
       V::size_type start, V::size_type stop);
Generic Projections
Description

The free project functions support the construction of vector ranges. Existing matrix_range 's can be composed with a further range. The resulting range is computed using this existing range’s compose function.

Prototypes
    template<class V>
    vector_range<V> project (V &data, const range &r);
    template<class V>
    const vector_range<const V> project (const V &data, const range &r);
    template<class V>
    vector_range<V> project (vector_range<V> &data, const range &r);
    template<class V>
    const vector_range<V> project (const vector_range<V> &data, const range &r);
Definition

Defined in the header vector_proxy.hpp.

Type requirements
Complexity

Linear depending from the size of the range.

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

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    for (int i = 0; i < 3; ++ i)
        project (v, range (0, 3)) (i) = i;
    std::cout << project (v, range (0, 3)) << std::endl;
}
Vector Slice
Description

The templated class vector_slice<V> allows addressing a slice of a vector.

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

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    vector_slice<vector<double> > vs (v, slice (0, 1, 3));
    for (unsigned i = 0; i < vs.size (); ++ i)
        vs (i) = i;
    std::cout << vs << std::endl;
}
Definition

Defined in the header vector_proxy.hpp.

Template parameters

Parameter

Description

Default

V

The type of vector referenced.

Model of

If the specified slice falls outside that of the index range of the vector, then the vector_slice is not a well formed Vector Expression. That is, access to an element which is outside of index range of the vector is undefined.

Type requirements

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

Public base classes

vector_expression<vector_slice<V> >

Members
Member Description

vector_slice (vector_type &data, const slice &s)

Constructs a sub vector.

size_type size () const

Returns the size of the sub vector.

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_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.

vector_slice &operator = (const vector_slice &vs)

The assignment operator.

vector_slice &assign_temporary (vector_slice &vs)

Assigns a temporary. May change the vector slice vs .

template<class AE> vector_slice &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> vector_slice &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> vector_slice &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> vector_slice &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> vector_slice &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> vector_slice &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> vector_slice &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> vector_slice &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (vector_slice &vs)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the vector_slice.

const_iterator end () const

Returns a const_iterator pointing to the end of the vector_slice.

iterator begin ()

Returns a iterator pointing to the beginning of the vector_slice.

iterator end ()

Returns a iterator pointing to the end of the vector_slice.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed vector_slice.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed vector_slice.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed vector_slice.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed vector_slice.

Simple Projections
Description

The free subslice functions support the construction of vector slices.

Prototypes
    template<class V>
    vector_slice<V> subslice (V &data,
       V::size_type start, V::difference_type stride, V::size_type size);
    template<class V>
    const vector_slice<const V> subslice (const V &data,
       V::size_type start, V::difference_type stride, V::size_type size);
Generic Projections
Description

The free project functions support the construction of vector slices. Existing vector_slice 's can be composed with a further range or slices. The resulting slice is computed using this existing slices’s compose function.

Prototypes
    template<class V>
    vector_slice<V> project (V &data, const slice &s);
    template<class V>
    const vector_slice<const V> project (const V &data, const slice &s);
    template<class V>
    vector_slice<V> project (vector_slice<V> &data, const range &r);
    template<class V>
    const vector_slice<V> project (const vector_slice<V> &data, const range &r);
    template<class V>
    vector_slice<V> project (vector_slice<V> &data, const slice &s);
    template<class V>
    const vector_slice<V> project (const vector_slice<V> &data, const slice &s);
Definition

Defined in the header vector_proxy.hpp.

Type requirements
Complexity

Linear depending from the size of the slice.

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

int main () {
    using namespace boost::numeric::ublas;
    vector<double> v (3);
    for (int i = 0; i < 3; ++ i)
        project (v, slice (0, 1, 3)) (i) = i;
    std::cout << project (v, slice (0, 1, 3)) << std::endl;
}

Vector Expressions

Description

The templated class vector_expression<E> is required to be a public base of all classes which model the Vector Expression concept.

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
Reference
Description

The templated class vector_reference<E> contains a reference to a vector expression.

Definition

Defined in the header vector_expression.hpp.

Template parameters

Parameter

Description

Default

E

The type of the vector expression.

 

Model of
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 Operations
Unary Operation Description
Description

The templated class vector_unary<E, F> describes a unary vector operation.

Definition

Defined in the header vector_expression.hpp.

Template parameters

Parameter

Description

Default

E

The type of the vector expression.

 

F

The type of the operation.

 

Model of
Type requirements

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

Public base classes

vector_expression<vector_unary<E, F> >

Members
Member Description

vector_unary (const expression_type &e)

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.

Unary Operations
Prototypes
template<class E, class F>
    struct vector_unary_traits {
        typedef vector_unary<typename E::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (- v) [i] = - v [i]
    template<class E>
     typename vector_unary_traits<E, scalar_negate<typename E::value_type> >::result_type
    operator - (const vector_expression<E> &e);

    // (conj v) [i] = conj (v [i])
    template<class E>
     typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
    conj (const vector_expression<E> &e);

    // (real v) [i] = real (v [i])
    template<class E>
     typename vector_unary_traits<E, scalar_real<typename E::value_type> >::result_type
    real (const vector_expression<E> &e);

    // (imag v) [i] = imag (v [i])
    template<class E>
     typename vector_unary_traits<E, scalar_imag<typename E::value_type> >::result_type
    imag (const vector_expression<E> &e);

    // (trans v) [i] = v [i]
    template<class E>
     typename vector_unary_traits<E, scalar_identity<typename E::value_type> >::result_type
    trans (const vector_expression<E> &e);

    // (herm v) [i] = conj (v [i])
    template<class E>
     typename vector_unary_traits<E, scalar_conj<typename E::value_type> >::result_type
    herm (const vector_expression<E> &e);
Description

operator - computes the additive inverse of a vector expression. conj computes the complex conjugate of a vector expression. real and imag compute the real and imaginary parts of a vector expression. trans computes the transpose of a vector expression. herm computes the hermitian, i.e. the complex conjugate of the transpose of a vector expression.

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<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;
}
Binary Operation Description
Description

The templated class vector_binary<E1, E2, F> describes a binary vector operation.

Definition

Defined in the header vector_expression.hpp.

Template parameters

Parameter

Description

Default

E1

The type of the first vector expression.

E2

The type of the second vector expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

vector_expression<vector_binary<E1, E2, F> >

Members
Member Description

vector_binary (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.

Binary Operations
Prototypes
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;
     };

    // (v1 + v2) [i] = v1 [i] + v2 [i]
    template<class E1, class E2>
    typename vector_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
                                                       typename E2::value_type> >::result_type
    operator + (const vector_expression<E1> &e1,
                 const vector_expression<E2> &e2);

    // (v1 - v2) [i] = v1 [i] - v2 [i]
    template<class E1, class E2>
    typename vector_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
                                                        typename E2::value_type> >::result_type
    operator - (const vector_expression<E1> &e1,
                 const vector_expression<E2> &e2);
Description

operator + computes the sum of two vector expressions. operator - computes the difference of two vector expressions.

Definition

Defined in the header vector_expression.hpp.

Type requirements
Preconditions
  • e1 ().size () == e2 ().size ()

Complexity

Linear depending from the size of the vector expressions.

Examples
#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;
}
Binary Outer Operation Description
Description

The templated class vector_matrix_binary<E1, E2, F> describes a binary outer vector operation.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E1

The type of the first vector expression.

E2

The type of the second vector expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

matrix_expression<vector_matrix_binary<E1, E2, F> >

Members
Member Description

vector_matrix_binary (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

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
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_2_square v = sum (v [i] * v [i])
    template<class E>
    typename vector_scalar_unary_traits<E, vector_norm_2_square<typename E::value_type> >::result_type
    norm_2_square (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
  • e1 ().size () == e2 ().size ()

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;
}

Matrix

Matrix Abstract

Description

The templated class matrix<T, F, A> is the base container adaptor for dense matrices. For a (m x n)-dimensional matrix and 0 ⇐ i < m, 0 ⇐ j < n every element mi,j is mapped to the (i x n j)-th element of the container for row major orientation or the (i+ j x m)-th element of the container for column major orientation.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

F

Functor describing the storage organization. [1]

row_major

A

The type of the Storage array. [2]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<matrix<T, F, A> >

Members
Member Description

matrix ()

Allocates an uninitialized matrix that holds zero rows of zero elements.

matrix (size_type size1, size_type size2)

Allocates an uninitialized matrix that holds size1 rows of size2 elements.

matrix (const matrix &m)

The copy constructor.

template<class AE> matrix (const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Reallocates a matrix to hold size1 rows of size2 elements. The existing elements of the matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const array_type& data () const

array_type& data ()

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

matrix &operator = (const matrix &m)

The assignment operator.

matrix &assign_temporary (matrix &m)

Assigns a temporary. May change the matrix m.

template<class AE> matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the matrix.

template<class AE> matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the matrix.

template<class AE> matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the matrix. Left and right hand side of the assignment should be independent.

template<class AT> matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the matrix with a scalar.

template<class AT> matrix &operator /= (const AT &at)

A computed assignment operator. Divides the matrix through a scalar.

void swap (matrix &m)

Swaps the contents of the matrices.

void insert_element (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row.

void erase_element (size_type i, size_type j)

Erases the value at the j-th element of the i-th row.

void clear ()

Clears the matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Common parameters for the storage array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Identity Matrix
Description

The templated class identity_matrix<T, ALLOC> represents identity matrices. For a (m x n)-dimensional identity matrix and 0 ⇐ i < m, 0 ⇐ j < n holds idi,j = 0, if i <> j, and idi,i .

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

int main () {
    using namespace boost::numeric::ublas;
    identity_matrix<double> m (3);
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<identity_matrix<T> >

Members
Member Description

identity_matrix ()

Constructs an identity_matrix that holds zero rows of zero elements.

identity_matrix (size_type size)

Constructs an identity_matrix that holds size rows of size elements.

identity_matrix (const identity_matrix &m)

The copy constructor.

void resize (size_type size, bool preserve = true)

Resizes a identity_matrix to hold size rows of size elements. Therefore the existing elements of the itendity_matrix are always preseved.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

identity_matrix &operator = (const identity_matrix &m)

The assignment operator.

identity_matrix &assign_temporary (identity_matrix &m)

Assigns a temporary. May change the identity matrix m .

void swap (identity_matrix &m)

Swaps the contents of the identity matrices.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the identity_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the identity_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the identity_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the identity_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed identity_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed identity_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed identity_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed identity_matrix.

Zero Matrix
Description

The templated class zero_matrix<T, ALLOC> represents zero matrices. For a (m x n)-dimensional zero matrix and 0 ⇐ i < m, 0 ⇐ j < n holds zi,j = 0.

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

int main () {
    using namespace boost::numeric::ublas;
    zero_matrix<double> m (3, 3);
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<zero_matrix<T> >

Members
Member Description

zero_matrix ()

Constructs a zero_matrix that holds zero rows of zero elements.

zero_matrix (size_type size1, size_type size2)

Constructs a zero_matrix that holds size1 rows of size2 elements.

zero_matrix (const zero_matrix &m)

The copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Resizes a zero_matrix to hold size1 rows of size2 elements. Therefore the existing elements of the zero_matrix are always preseved.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

zero_matrix &operator = (const zero_matrix &m)

The assignment operator.

zero_matrix &assign_temporary (zero_matrix &m)

Assigns a temporary. May change the zero matrix m .

void swap (zero_matrix &m)

Swaps the contents of the zero matrices.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the zero_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the zero_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the zero_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the zero_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed zero_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed zero_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed zero_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed zero_matrix.

Scalar Matrix
Description

The templated class scalar_matrix<T, ALLOC> represents scalar matrices. For a (m x n)-dimensional scalar matrix and 0 ⇐ i < m, 0 ⇐ j < n holds zi,j = s.

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

int main () {
    using namespace boost::numeric::ublas;
    scalar_matrix<double> m (3, 3);
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

int

ALLOC

An STL Allocator for size_type and difference_type.

std::allocator

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<scalar_matrix<T> >

Members
Member Description

scalar_matrix ()

Constructs a scalar_matrix that holds scalar rows of zero elements.

scalar_matrix (size_type size1, size_type size2, const value_type &value)

Constructs a scalar_matrix that holds size1 rows of size2 elements each of the specified value.

scalar_matrix (const scalar_matrix &m)

The copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Resizes a scalar_matrix to hold size1 rows of size2 elements. Therefore the existing elements of the scalar_matrix are always preseved.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

scalar_matrix &operator = (const scalar_matrix &m)

The assignment operator.

scalar_matrix &assign_temporary (scalar_matrix &m)

Assigns a temporary. May change the scalar matrix m .

void swap (scalar_matrix &m)

Swaps the contents of the scalar matrices.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the scalar_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the scalar_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the scalar_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the scalar_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed scalar_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed scalar_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed scalar_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed scalar_matrix.

Triangular Matrix

Description

The templated class triangular_matrix<T, F1, F2, A> is the base container adaptor for triangular matrices. For a (n x n )-dimensional lower triangular matrix and 0 < = i < n, 0 < = j < n holds ti,j = 0 , if i > j. If furthermore holds ti,i= 1 the matrix is called unit lower triangular. For a (n x n )-dimensional lower triangular matrix and 0 < = i < n, 0 < = j < n holds ti,j= 0 , if i < j. If furthermore holds ti,i= 1 the matrix is called unit lower triangular. The storage of triangular matrices is packed.

Example
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    triangular_matrix<double, lower> ml (3, 3);
    for (unsigned i = 0; i < ml.size1 (); ++ i)
        for (unsigned j = 0; j <= i; ++ j)
            ml (i, j) = 3 * i + j;
    std::cout << ml << std::endl;
    triangular_matrix<double, upper> mu (3, 3);
    for (unsigned i = 0; i < mu.size1 (); ++ i)
        for (unsigned j = i; j < mu.size2 (); ++ j)
            mu (i, j) = 3 * i + j;
    std::cout << mu << std::endl;
}

Please read the full triangular example for more details.

Definition

Defined in the header triangular.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

F1

Functor describing the type of the triangular matrix. [1]

lower

F2

Functor describing the storage organization. [2]

row_major

A

The type of the adapted array. [3]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<triangular_matrix<T, F1, F2, A> >

Members
Member Description

triangular_matrix ()

Allocates an uninitialized triangular_matrix that holds zero rows of zero elements.

triangular_matrix (size_type size1, size_type size2)

Allocates an uninitialized triangular_matrix that holds size1 rows of size2 elements.

triangular_matrix (const triangular_matrix &m)

The copy constructor.

template<class AE> triangular_matrix (const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Reallocates a triangular_matrix to hold size1 rows of size2 elements. The existing elements of the triangular_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

triangular_matrix &operator = (const triangular_matrix &m)

The assignment operator.

triangular_matrix &assign_temporary (triangular_matrix &m)

Assigns a temporary. May change the triangular matrix m.

template<class AE> triangular_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> triangular_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the triangular matrix. Left and right hand side of the assignment should be independent.

template<class AE> triangular_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the triangular matrix.

template<class AE> triangular_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the triangular matrix. Left and right hand side of the assignment should be independent.

template<class AE> triangular_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the triangular matrix.

template<class AE> triangular_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the triangular matrix. Left and right hand side of the assignment should be independent.

template<class AT> triangular_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the triangular matrix with a scalar.

template<class AT> triangular_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the triangular matrix through a scalar.

void swap (triangular_matrix &m)

Swaps the contents of the triangular matrices.

void insert (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row.

void erase (size_type i, size_type j)

Erases the value at the j-th elemenst of the i-th row.

void clear ()

Clears the matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the triangular_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the triangular_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the triangular_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the triangular_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the triangular_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the triangular_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the triangular_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the triangular_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed triangular_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed triangular_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed triangular_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed triangular_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed triangular_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed triangular_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed triangular_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed triangular_matrix.

Notes

[1] Supported parameters for the type of the triangular matrix are lower , unit_lower, upper and unit_upper .

[2] Supported parameters for the storage organization are row_major and column_major.

[3] Supported parameters for the adapted array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Triangular Adaptor
Description

The templated class triangular_adaptor<M, F> is a triangular matrix adaptor for other matrices.

Example
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    triangular_adaptor<matrix<double>, lower> tal (m);
    for (unsigned i = 0; i < tal.size1 (); ++ i)
        for (unsigned j = 0; j <= i; ++ j)
            tal (i, j) = 3 * i + j;
    std::cout << tal << std::endl;
    triangular_adaptor<matrix<double>, upper> tau (m);
    for (unsigned i = 0; i < tau.size1 (); ++ i)
        for (unsigned j = i; j < tau.size2 (); ++ j)
            tau (i, j) = 3 * i + j;
    std::cout << tau << std::endl;
}

Please read the full triangular example for more details.

Definition

Defined in the header triangular.hpp.

Template parameters
Parameter Description Default

M

The type of the adapted matrix.

F

Functor describing the type of the triangular adaptor. [1]

lower

Model of
Type requirements

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

Public base classes

matrix_expression<triangular_adaptor<M, F> >

Members
Member Description

triangular_adaptor (matrix_type &data)

Constructs a triangular_adaptor of a matrix.

triangular_adaptor (const triangular_adaptor &m)

The copy constructor.

template<class AE> triangular_adaptor (const matrix_expression<AE> &ae)

The extended copy constructor.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

triangular_adaptor &operator = (const triangular_adaptor &m)

The assignment operator.

triangular_adaptor &assign_temporary (triangular_adaptor &m)

Assigns a temporary. May change the triangular adaptor m.

template<class AE> triangular_adaptor &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> triangular_adaptor &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the triangular adaptor. Left and right hand side of the assignment should be independent.

template<class AE> triangular_adaptor &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the triangular adaptor.

template<class AE> triangular_adaptor &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the triangular adaptor. Left and right hand side of the assignment should be independent.

template<class AE> triangular_adaptor &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the triangular adaptor.

template<class AE> triangular_adaptor &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the triangular adaptor. Left and right hand side of the assignment should be independent.

template<class AT> triangular_adaptor &operator *= (const AT &at)

A computed assignment operator. Multiplies the triangular adaptor with a scalar.

template<class AT> triangular_adaptor &operator /= (const AT &at)

A computed assignment operator. Divides the triangular adaptor through a scalar.

void swap (triangular_adaptor &m)

Swaps the contents of the triangular adaptors.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the triangular_adaptor.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the triangular_adaptor.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the triangular_adaptor.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the triangular_adaptor.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the triangular_adaptor.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the triangular_adaptor.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the triangular_adaptor.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the triangular_adaptor.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed triangular_adaptor.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed triangular_adaptor.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed triangular_adaptor.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed triangular_adaptor.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed triangular_adaptor.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed triangular_adaptor.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed triangular_adaptor.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed triangular_adaptor.

Notes

[1] Supported parameters for the type of the triangular adaptor are lower , unit_lower, upper and unit_upper .

Symmetric Matrix

Description

The templated class symmetric_matrix<T, F1, F2, A> is the base container adaptor for symmetric matrices. For a (n x n )-dimensional symmetric matrix and 0 < = i < n, 0 < = j < n holds si,j= sj,i. The storage of symmetric matrices is packed.

Example
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    symmetric_matrix<double, lower> ml (3, 3);
    for (unsigned i = 0; i < ml.size1 (); ++ i)
        for (unsigned j = 0; j <= i; ++ j)
            ml (i, j) = 3 * i + j;
    std::cout << ml << std::endl;
    symmetric_matrix<double, upper> mu (3, 3);
    for (unsigned i = 0; i < mu.size1 (); ++ i)
        for (unsigned j = i; j < mu.size2 (); ++ j)
            mu (i, j) = 3 * i + j;
    std::cout << mu << std::endl;
}
Definition

Defined in the header symmetric.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

F1

Functor describing the type of the symmetric matrix. [1]

lower

F2

Functor describing the storage organization. [2]

row_major

A

The type of the adapted array. [3]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<symmetric_matrix<T, F1, F2, A> >

Members
Member Description

symmetric_matrix (size_type size)

Allocates an uninitialized symmetric_matrix that holds size rows of size elements.

symmetric_matrix (const symmetric_matrix &m)

The copy constructor.

template<class AE> symmetric_matrix (const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Reallocates a symmetric_matrix to hold size rows of size elements. The existing elements of the symmetric_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

symmetric_matrix &operator = (const symmetric_matrix &m)

The assignment operator.

symmetric_matrix &assign_temporary (symmetric_matrix &m)

Assigns a temporary. May change the symmetric matrix m .

template<class AE> symmetric_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> symmetric_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the symmetric matrix. Left and right hand side of the assignment should be independent.

template<class AE> symmetric_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the symmetric matrix.

template<class AE> symmetric_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the symmetric matrix. Left and right hand side of the assignment should be independent.

template<class AE> symmetric_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the symmetric matrix.

template<class AE> symmetric_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the symmetric matrix. Left and right hand side of the assignment should be independent.

template<class AT> symmetric_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the symmetric matrix with a scalar.

template<class AT> symmetric_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the symmetric matrix through a scalar.

void swap (symmetric_matrix &m)

Swaps the contents of the symmetric matrices.

void insert (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row.

void erase (size_type i, size_type j)

Erases the value at the j-th elemenst of the i-th row.

void clear ()

Clears the matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the symmetric_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the symmetric_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the symmetric_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the symmetric_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the symmetric_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the symmetric_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the symmetric_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the symmetric_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed symmetric_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed symmetric_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed symmetric_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed symmetric_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed symmetric_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed symmetric_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed symmetric_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed symmetric_matrix.

Notes

[1] Supported parameters for the type of the symmetric matrix are lower and upper.

[2] Supported parameters for the storage organization are row_major and column_major.

[3] Supported parameters for the adapted array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Symmetric Adaptor
Description

The templated class symmetric_adaptor<M, F> is a symmetric matrix adaptor for other matrices.

Example
#include <boost/numeric/ublas/symmetric.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    symmetric_adaptor<matrix<double>, lower> sal (m);
    for (unsigned i = 0; i < sal.size1 (); ++ i)
        for (unsigned j = 0; j <= i; ++ j)
            sal (i, j) = 3 * i + j;
    std::cout << sal << std::endl;
    symmetric_adaptor<matrix<double>, upper> sau (m);
    for (unsigned i = 0; i < sau.size1 (); ++ i)
        for (unsigned j = i; j < sau.size2 (); ++ j)
            sau (i, j) = 3 * i + j;
    std::cout << sau << std::endl;
}
Definition

Defined in the header symmetric.hpp.

Template parameters
Parameter Description Default

M

The type of the adapted matrix.

F

Functor describing the type of the symmetric adaptor. [1]

lower

Model of
Type requirements

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

Public base classes

matrix_expression<symmetric_adaptor<M, F> >

Members
Member Description

symmetric_adaptor ()

Constructs a symmetric_adaptor that holds zero rows of zero elements.

symmetric_adaptor (matrix_type &data)

Constructs a symmetric_adaptor of a matrix.

symmetric_adaptor (const symmetric_adaptor &m)

The copy constructor.

template<class AE> symmetric_adaptor (const matrix_expression<AE> &ae)

The extended copy constructor.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

symmetric_adaptor &operator = (const symmetric_adaptor &m)

The assignment operator.

symmetric_adaptor &assign_temporary (symmetric_adaptor &m)

Assigns a temporary. May change the symmetric adaptor m.

template<class AE> symmetric_adaptor &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> symmetric_adaptor &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the symmetric adaptor. Left and right hand side of the assignment should be independent.

template<class AE> symmetric_adaptor &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the symmetric adaptor.

template<class AE> symmetric_adaptor &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the symmetric adaptor. Left and right hand side of the assignment should be independent.

template<class AE> symmetric_adaptor &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the symmetric adaptor.

template<class AE> symmetric_adaptor &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the symmetric adaptor. Left and right hand side of the assignment should be independent.

template<class AT> symmetric_adaptor &operator *= (const AT &at)

A computed assignment operator. Multiplies the symmetric adaptor with a scalar.

template<class AT> symmetric_adaptor &operator /= (const AT &at)

A computed assignment operator. Divides the symmetric adaptor through a scalar.

void swap (symmetric_adaptor &m)

Swaps the contents of the symmetric adaptors.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the symmetric_adaptor.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the symmetric_adaptor.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the symmetric_adaptor.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the symmetric_adaptor.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the symmetric_adaptor.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the symmetric_adaptor.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the symmetric_adaptor.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the symmetric_adaptor.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed symmetric_adaptor.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed symmetric_adaptor.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed symmetric_adaptor.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed symmetric_adaptor.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed symmetric_adaptor.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed symmetric_adaptor.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed symmetric_adaptor.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed symmetric_adaptor.

Notes

[1] Supported parameters for the type of the symmetric adaptor are lower and upper.

Hermitian Matrix

Description

The templated class hermitian_matrix<T, F1, F2, A> is the base container adaptor for hermitian matrices. For a (n x n )-dimensional hermitian matrix and 0 < = i < n, 0 < = j < n holds hi,j= hj,i-. The storage of hermitian matrices is packed.

Example
#include <boost/numeric/ublas/hermitian.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    hermitian_matrix<std::complex<double>, lower> ml (3, 3);
    for (unsigned i = 0; i < ml.size1 (); ++ i) {
        for (unsigned j = 0; j < i; ++ j)
            ml (i, j) = std::complex<double> (3 * i + j, 3 * i + j);
        ml (i, i) = std::complex<double> (4 * i, 0);
    }
    std::cout << ml << std::endl;
    hermitian_matrix<std::complex<double>, upper> mu (3, 3);
    for (unsigned i = 0; i < mu.size1 (); ++ i) {
        mu (i, i) = std::complex<double> (4 * i, 0);
        for (unsigned j = i + 1; j < mu.size2 (); ++ j)
            mu (i, j) = std::complex<double> (3 * i + j, 3 * i + j);
    }
    std::cout << mu << std::endl;
}
Definition

Defined in the header hermitian.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

F1

Functor describing the type of the hermitian matrix. [1]

lower

F2

Functor describing the storage organization. [2]

row_major

A

The type of the adapted array. [3]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<hermitian_matrix<T, F1, F2, A> >

Members
Member Description

hermitian_matrix ()

Allocates an uninitialized hermitian_matrix that holds zero rows of zero elements.

hermitian_matrix (size_type size)

Allocates an uninitialized hermitian_matrix that holds size rows of size elements.

hermitian_matrix (const hermitian_matrix &m)

The copy constructor.

template<class AE> hermitian_matrix (const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size, bool preserve = true)

Reallocates a hermitian_matrix to hold size rows of size elements. The existing elements of the hermitian_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

hermitian_matrix &operator = (const hermitian_matrix &m)

The assignment operator.

hermitian_matrix &assign_temporary (hermitian_matrix &m)

Assigns a temporary. May change the hermitian matrix m .

template<class AE> hermitian_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> hermitian_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the hermitian matrix. Left and right hand side of the assignment should be independent.

template<class AE> hermitian_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the hermitian matrix.

template<class AE> hermitian_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the hermitian matrix. Left and right hand side of the assignment should be independent.

template<class AE> hermitian_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the hermitian matrix.

template<class AE> hermitian_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the hermitian matrix. Left and right hand side of the assignment should be independent.

template<class AT> hermitian_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the hermitian matrix with a scalar.

template<class AT> hermitian_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the hermitian matrix through a scalar.

void swap (hermitian_matrix &m)

Swaps the contents of the hermitian matrices.

void insert (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row.

void erase (size_type i, size_type j)

Erases the value at the j-th elemenst of the i-th row.

void clear ()

Clears the matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the hermitian_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the hermitian_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the hermitian_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the hermitian_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the hermitian_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the hermitian_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the hermitian_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the hermitian_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed hermitian_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed hermitian_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed hermitian_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed hermitian_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed hermitian_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed hermitian_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed hermitian_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed hermitian_matrix.

Notes

[1] Supported parameters for the type of the hermitian matrix are lower and upper.

[2] Supported parameters for the storage organization are row_major and column_major.

[3] Supported parameters for the adapted array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Hermitian Adaptor
Description

The templated class hermitian_adaptor<M, F> is a hermitian matrix adaptor for other matrices.

Example
#include <boost/numeric/ublas/hermitian.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<std::complex<double> > m (3, 3);
    hermitian_adaptor<matrix<std::complex<double> >, lower> hal (m);
    for (unsigned i = 0; i < hal.size1 (); ++ i) {
        for (unsigned j = 0; j < i; ++ j)
            hal (i, j) = std::complex<double> (3 * i + j, 3 * i + j);
        hal (i, i) = std::complex<double> (4 * i, 0);
    }
    std::cout << hal << std::endl;
    hermitian_adaptor<matrix<std::complex<double> >, upper> hau (m);
    for (unsigned i = 0; i < hau.size1 (); ++ i) {
        hau (i, i) = std::complex<double> (4 * i, 0);
        for (unsigned j = i + 1; j < hau.size2 (); ++ j)
            hau (i, j) = std::complex<double> (3 * i + j, 3 * i + j);
    }
    std::cout << hau << std::endl;
}
Definition

Defined in the header hermitian.hpp.

Template parameters
Parameter Description Default

M

The type of the adapted matrix.

F

Functor describing the type of the hermitian adaptor. [1]

lower

Model of
Type requirements

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

Public base classes

matrix_expression<hermitian_adaptor<M, F> >

Members
Member Description

hermitian_adaptor (matrix_type &data)

Constructs a hermitian_adaptor of a matrix.

hermitian_adaptor (const hermitian_adaptor &m)

The copy constructor.

template<class AE> hermitian_adaptor (const matrix_expression<AE> &ae)

The extended copy constructor.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

hermitian_adaptor &operator = (const hermitian_adaptor &m)

The assignment operator.

hermitian_adaptor &assign_temporary (hermitian_adaptor &m)

Assigns a temporary. May change the hermitian adaptor m.

template<class AE> hermitian_adaptor &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> hermitian_adaptor &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the hermitian adaptor. Left and right hand side of the assignment should be independent.

template<class AE> hermitian_adaptor &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the hermitian adaptor.

template<class AE> hermitian_adaptor &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the hermitian adaptor. Left and right hand side of the assignment should be independent.

template<class AE> hermitian_adaptor &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the hermitian adaptor.

template<class AE> hermitian_adaptor &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the hermitian adaptor. Left and right hand side of the assignment should be independent.

template<class AT> hermitian_adaptor &operator *= (const AT &at)

A computed assignment operator. Multiplies the hermitian adaptor with a scalar.

template<class AT> hermitian_adaptor &operator /= (const AT &at)

A computed assignment operator. Divides the hermitian adaptor through a scalar.

void swap (hermitian_adaptor &m)

Swaps the contents of the hermitian adaptors.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the hermitian_adaptor.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the hermitian_adaptor.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the hermitian_adaptor.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the hermitian_adaptor.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the hermitian_adaptor.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the hermitian_adaptor.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the hermitian_adaptor.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the hermitian_adaptor.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed hermitian_adaptor.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed hermitian_adaptor.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed hermitian_adaptor.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed hermitian_adaptor.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed hermitian_adaptor.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed hermitian_adaptor.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed hermitian_adaptor.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed hermitian_adaptor.

Notes

[1] Supported parameters for the type of the hermitian adaptor are lower and upper.

Banded Matrix

Description

The templated class banded_matrix<T, F, A> is the base container adaptor for banded matrices. For a (m x n)-dimensional banded matrix with l lower and u upper diagonals and 0 < = i < m, 0 < = j < n holds bi,j = 0, if i > j + l or i < j - u. The storage of banded matrices is packed.

Example
#include <boost/numeric/ublas/banded.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    banded_matrix<double> m (3, 3, 1, 1);
    for (signed i = 0; i < signed (m.size1 ()); ++ i)
        for (signed j = std::max (i - 1, 0); j < std::min (i + 2, signed (m.size2 ())); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Definition

Defined in the header banded.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the matrix.

F

Functor describing the storage organization. [1]

row_major

A

The type of the adapted array. [2]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<banded_matrix<T, F, A> >

Members
Member Description

banded_matrix ()

Allocates an uninitialized banded_matrix that holds zero rows of zero elements.

banded_matrix (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0)

Allocates an uninitialized banded_matrix that holds (lower + 1 + upper) diagonals around the main diagonal of a matrix with size1 rows of size2 elements.

banded_matrix (const banded_matrix &m)

The copy constructor.

template<class AE> banded_matrix (const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, size_type lower = 0, size_type upper = 0, bool preserve = true)

Reallocates a banded_matrix to hold (lower + 1 + upper) diagonals around the main diagonal of a matrix with size1 rows of size2 elements. The existing elements of the banded_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

size_type lower () const

Returns the number of diagonals below the main diagonal.

size_type upper () const

Returns the number of diagonals above the main diagonal.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

banded_matrix &operator = (const banded_matrix &m)

The assignment operator.

banded_matrix &assign_temporary (banded_matrix &m)

Assigns a temporary. May change the banded matrix m .

template<class AE> banded_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> banded_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the banded matrix. Left and right hand side of the assignment should be independent.

template<class AE> banded_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the banded matrix.

template<class AE> banded_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the banded matrix. Left and right hand side of the assignment should be independent.

template<class AE> banded_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the banded matrix.

template<class AE> banded_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the banded matrix. Left and right hand side of the assignment should be independent.

template<class AT> banded_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the banded matrix with a scalar.

template<class AT> banded_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the banded matrix through a scalar.

void swap (banded_matrix &m)

Swaps the contents of the banded matrices.

void insert (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row.

void erase (size_type i, size_type j)

Erases the value at the j-th elemenst of the i-th row.

void clear ()

Clears the matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the banded_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the banded_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the banded_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the banded_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the banded_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the banded_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the banded_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the banded_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed banded_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed banded_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed banded_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed banded_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed banded_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed banded_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed banded_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed banded_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Supported parameters for the adapted array are unbounded_array<T> , bounded_array<T> and std::vector<T> .

Banded Adaptor
Description

The templated class banded_adaptor<M> is a banded matrix adaptor for other matrices.

Example
#include <boost/numeric/ublas/banded.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    banded_adaptor<matrix<double> > ba (m, 1, 1);
    for (signed i = 0; i < signed (ba.size1 ()); ++ i)
        for (signed j = std::max (i - 1, 0); j < std::min (i + 2, signed (ba.size2 ())); ++ j)
            ba (i, j) = 3 * i + j;
    std::cout << ba << std::endl;
}
Definition

Defined in the header banded.hpp.

Template parameters

Parameter

Description

Default

M

The type of the adapted matrix.

Model of
Type requirements

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

Public base classes

matrix_expression<banded_adaptor<M> >

Members
Member Description

banded_adaptor (matrix_type &data, size_type lower = 0, size_type upper = 0)

Constructs a banded_adaptor that holds (lower + 1 + upper) diagonals around the main diagonal of a matrix.

banded_adaptor (const banded_adaptor &m)

The copy constructor.

template<class AE> banded_adaptor (const matrix_expression<AE> &ae)

The extended copy constructor.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

size_type lower () const

Returns the number of diagonals below the main diagonal.

size_type upper () const

Returns the number of diagonals above the main diagonal.

const_reference operator () (size_type i, size_type j) const

Returns a const reference of the j -th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

banded_adaptor &operator = (const banded_adaptor &m)

The assignment operator.

banded_adaptor &assign_temporary (banded_adaptor &m)

Assigns a temporary. May change the banded adaptor m .

template<class AE> banded_adaptor &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> banded_adaptor &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the banded adaptor. Left and right hand side of the assignment should be independent.

template<class AE> banded_adaptor &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the banded adaptor.

template<class AE> banded_adaptor &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the banded adaptor. Left and right hand side of the assignment should be independent.

template<class AE> banded_adaptor &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the banded adaptor.

template<class AE> banded_adaptor &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the banded adaptor. Left and right hand side of the assignment should be independent.

template<class AT> banded_adaptor &operator *= (const AT &at)

A computed assignment operator. Multiplies the banded adaptor with a scalar.

template<class AT> banded_adaptor &operator /= (const AT &at)

A computed assignment operator. Divides the banded adaptor through a scalar.

void swap (banded_adaptor &m)

Swaps the contents of the banded adaptors.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the banded_adaptor.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the banded_adaptor.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the banded_adaptor.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the banded_adaptor.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the banded_adaptor.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the banded_adaptor.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the banded_adaptor.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the banded_adaptor.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed banded_adaptor.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed banded_adaptor.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed banded_adaptor.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed banded_adaptor.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed banded_adaptor.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed banded_adaptor.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed banded_adaptor.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed banded_adaptor.

Sparse Matricies

Mapped Matrix
Description

The templated class mapped_matrix<T, F, A> is the base container adaptor for sparse matricies using element maps. For a (m xn)-dimensional sparse matrix and 0 < = i < m, 0 < = j < n the non-zero elements hi,j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the associative container, i.e. for elements k = mi1,j1 and k + 1 = mi2,j2 of the container holds i1 < i2 or (i1 = i2 and j1 < j2) with row major orientation or j1 < j2 or (j1 = j2 and i1 < i2) with column major orientation.

Example
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    mapped_matrix<double> m (3, 3, 3 * 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the mapped matrix.

F

Functor describing the storage organization. [1]

row_major

A

The type of the adapted array. [2]

map_std<std::size_t, T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<mapped_matrix<T, F, A> >

Members
Member Description

mapped_matrix ()

Allocates a mapped_matrix that holds at most zero rows of zero elements.

mapped_matrix (size_type size1, size_type2, size_type non_zeros = 0)

Allocates a mapped_matrix that holds at most size1 rows of size2 elements.

mapped_matrix (const mapped_matrix &m)

The copy constructor.

template<class AE> mapped_matrix (size_type non_zeros, const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Reallocates a mapped_matrix to hold at most size1 rows of size2 elements. The existing elements of the mapped_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

mapped_matrix &operator = (const mapped_matrix &m)

The assignment operator.

mapped_matrix &assign_temporary (mapped_matrix &m)

Assigns a temporary. May change the mapped matrix m .

template<class AE> mapped_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> mapped_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the mapped matrix. Left and right hand side of the assignment should be independent.

template<class AE> mapped_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the mapped matrix.

template<class AE> mapped_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the mapped matrix. Left and right hand side of the assignment should be independent.

template<class AE> mapped_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the mapped matrix.

template<class AE> mapped_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the mapped matrix. Left and right hand side of the assignment should be independent.

template<class AT> mapped_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the mapped matrix with a scalar.

template<class AT> mapped_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the mapped matrix through a scalar.

void swap (mapped_matrix &m)

Swaps the contents of the mapped matrices.

true_refrence insert_element (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row. Duplicates elements are not allowed.

void erase_element (size_type i, size_type j)

Erases the value at the j-th element of the i-th row.

void clear ()

Clears the mapped matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the mapped_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the mapped_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the mapped_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the mapped_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the mapped_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the mapped_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the mapped_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the mapped_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed mapped_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed mapped_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed mapped_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed mapped_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed mapped_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed mapped_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed mapped_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed mapped_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Supported parameters for the adapted array are map_array<std::size_t, T> and map_std<std::size_t, T>. The latter is equivalent to std::map<std::size_t, T>.

Compressed Matrix
Description

The templated class compressed_matrix<T, F, IB, IA, TA> is the base container adaptor for compressed matrices. For a (m x n )-dimensional compressed matrix and 0 < = i < m, 0 < = j < n the non-zero elements mi,j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the index and value containers, i.e. for elements k = mi1,j1and k + 1 = mi2,j2 of the container holds i1 < i2 or (i1 = i2 and j1 < j2) with row major orientation or j1 < j2 or (j1 = j2 and i1 < i__2) with column major orientation.

Example
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    compressed_matrix<double> m (3, 3, 3 * 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the compressed matrix.

F

Functor describing the storage organization. [1]

row_major

IB

The index base of the compressed vector. [2]

0

IA

The type of the adapted array for indices. [3]

unbounded_array<std::size_t>

TA

The type of the adapted array for values. [3]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<compressed_matrix<T, F, IB, IA, TA> >

Members
Member Description

compressed_matrix ()

Allocates a compressed_matrix that holds at most zero rows of zero elements.

compressed_matrix (size_type size1, size_type2, size_type non_zeros = 0)

Allocates a compressed_matrix that holds at most size1 rows of size2 elements.

compressed_matrix (const compressed_matrix &m)

The copy constructor.

template<class AE> compressed_matrix (size_type non_zeros, const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Reallocates a compressed_matrix to hold at most size1 rows of size2 elements. The existing elements of the compressed_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

compressed_matrix &operator = (const compressed_matrix &m)

The assignment operator.

compressed_matrix &assign_temporary (compressed_matrix &m)

Assigns a temporary. May change the compressed matrix m.

template<class AE> compressed_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> compressed_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the compressed matrix. Left and right hand side of the assignment should be independent.

template<class AE> compressed_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the compressed matrix.

template<class AE> compressed_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the compressed matrix. Left and right hand side of the assignment should be independent.

template<class AE> compressed_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the compressed matrix.

template<class AE> compressed_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the compressed matrix. Left and right hand side of the assignment should be independent.

template<class AT> compressed_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the compressed matrix with a scalar.

template<class AT> compressed_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the compressed matrix through a scalar.

void swap (compressed_matrix &m)

Swaps the contents of the compressed matrices.

true_reference insert_element (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row. Duplicates elements are not allowed.

void erase_element (size_type i, size_type j)

Erases the value at the j-th element of the i-th row.

void clear ()

Clears the compressed matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the compressed_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the compressed_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the compressed_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the compressed_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the compressed_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the compressed_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the compressed_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the compressed_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed compressed_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed compressed_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed compressed_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed compressed_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed compressed_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed compressed_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed compressed_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed compressed_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Supported parameters for the index base are 0 and 1 at least.

[3] Supported parameters for the adapted array are unbounded_array<> , bounded_array<> and std::vector<> .

Coordinate Matrix
Description

The templated class coordinate_matrix<T, F, IB, IA, TA> is the base container adaptor for compressed matrices. For a (m x n )-dimensional sorted coordinate matrix and 0 < = i < m, 0 < = j < n the non-zero elements mi,j are mapped via (i x n + j) for row major orientation or via (i + j x m) for column major orientation to consecutive elements of the index and value containers, i.e. for elements k = mi1,j1 and k + 1 = mi2,j2 of the container holds i1 < i2 or (i1 = i2 and j1 < j2) with row major orientation or j1 < j2 or (j1 = j2 and i1 < i__2) with column major orientation.

Example
#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    coordinate_matrix<double> m (3, 3, 3 * 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
    std::cout << m << std::endl;
}
Definition

Defined in the header matrix_sparse.hpp.

Template parameters
Parameter Description Default

T

The type of object stored in the coordinate matrix.

F

Functor describing the storage organization. [1]

row_major

IB

The index base of the coordinate vector. [2]

0

IA

The type of the adapted array for indices. [3]

unbounded_array<std::size_t>

TA

The type of the adapted array for values. [3]

unbounded_array<T>

Model of

Matrix .

Type requirements

None, except for those imposed by the requirements of Matrix .

Public base classes

matrix_container<coordinate_matrix<T, F, IB, IA, TA> >

Members
Member Description

coordinate_matrix ()

Allocates a coordinate_matrix that holds at most zero rows of zero elements.

coordinate_matrix (size_type size1, size_type2, size_type non_zeros = 0)

Allocates a coordinate_matrix that holds at most size1 rows of size2 elements.

coordinate_matrix (const coordinate_matrix &m)

The copy constructor.

template<class AE> coordinate_matrix (size_type non_zeros, const matrix_expression<AE> &ae)

The extended copy constructor.

void resize (size_type size1, size_type size2, bool preserve = true)

Reallocates a coordinate_matrix to hold at most size1 rows of size2 elements. The existing elements of the coordinate_matrix are preseved when specified.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

coordinate_matrix &operator = (const coordinate_matrix &m)

The assignment operator.

coordinate_matrix &assign_temporary (coordinate_matrix &m)

Assigns a temporary. May change the coordinate matrix m.

template<class AE> coordinate_matrix &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> coordinate_matrix &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the coordinate matrix. Left and right hand side of the assignment should be independent.

template<class AE> coordinate_matrix &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the coordinate matrix.

template<class AE> coordinate_matrix &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the coordinate matrix. Left and right hand side of the assignment should be independent.

template<class AE> coordinate_matrix &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the coordinate matrix.

template<class AE> coordinate_matrix &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the coordinate matrix. Left and right hand side of the assignment should be independent.

template<class AT> coordinate_matrix &operator *= (const AT &at)

A computed assignment operator. Multiplies the coordinate matrix with a scalar.

template<class AT> coordinate_matrix &operator /= (const AT &at)

A computed assignment operator. Divides the coordinate matrix through a scalar.

void swap (coordinate_matrix &m)

Swaps the contents of the coordinate matrices.

true_reference insert_element (size_type i, size_type j, const_reference t)

Inserts the value t at the j-th element of the i-th row. Duplicates elements are not allowed.

void append_element (size_type i, size_type j, const_reference t)

Appends the value t at the j-th element of the i-th row. Duplicate elements can be appended to a coordinate_matrix. They are merged into a single arithmetically summed element by the sort function.

void erase_element (size_type i, size_type j)

Erases the value at the j-th element of the i-th row.

void clear ()

Clears the coordinate matrix.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the coordinate_matrix.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the coordinate_matrix.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the coordinate_matrix.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the coordinate_matrix.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the coordinate_matrix.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the coordinate_matrix.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the coordinate_matrix.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the coordinate_matrix.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed coordinate_matrix.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed coordinate_matrix.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed coordinate_matrix.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed coordinate_matrix.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed coordinate_matrix.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed coordinate_matrix.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed coordinate_matrix.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed coordinate_matrix.

Notes

[1] Supported parameters for the storage organization are row_major and column_major.

[2] Supported parameters for the index base are 0 and 1 at least.

[3] Supported parameters for the adapted array are unbounded_array<> , bounded_array<> and std::vector<> .

Matrix Proxies

Matrix Row
Description

The templated class matrix_row<M> allows addressing a row of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i) {
        matrix_row<matrix<double> > mr (m, i);
        for (unsigned j = 0; j < mr.size (); ++ j)
            mr (j) = 3 * i + j;
        std::cout << mr << std::endl;
    }
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified row falls outside that of the row index range of the matrix, then the matrix_row is not a well formed Vector Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

vector_expression<matrix_row<M> >

Members
Member Description

matrix_row (matrix_type &data, size_type i)

Constructs a sub vector.

size_type size () const

Returns the size of the sub vector.

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.

matrix_row &operator = (const matrix_row &mr)

The assignment operator.

matrix_row &assign_temporary (matrix_row &mr)

Assigns a temporary. May change the matrix row mr .

template<class AE> matrix_row &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_row &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_row &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> matrix_row &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_row &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> matrix_row &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> matrix_row &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> matrix_row &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (matrix_row &mr)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the matrix_row.

const_iterator end () const

Returns a const_iterator pointing to the end of the matrix_row.

iterator begin ()

Returns a iterator pointing to the beginning of the matrix_row.

iterator end ()

Returns a iterator pointing to the end of the matrix_row.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed matrix_row.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed matrix_row.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed matrix_row.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed matrix_row.

Projections
Description

The free row functions support the construction of matrix rows.

Prototypes
    template<class M>
    matrix_row<M> row (M &data, std::size_t i);
    template<class M>
    const matrix_row<const M> row (const M &data, std::size_t i);
Definition

Defined in the header matrix_proxy.hpp.

Type requirements
Complexity

Linear depending from the size of the row.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i) {
        for (unsigned j = 0; j < m.size2 (); ++ j)
            row (m, i) (j) = 3 * i + j;
        std::cout << row (m, i) << std::endl;
    }
}
Matrix Column
Description

The templated class matrix_column<M> allows addressing a column of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned j = 0; j < m.size2 (); ++ j) {
        matrix_column<matrix<double> > mc (m, j);
        for (unsigned i = 0; i < mc.size (); ++ i)
            mc (i) = 3 * i + j;
        std::cout << mc << std::endl;
    }
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified column falls outside that of the column index range of the matrix, then the matrix_column is not a well formed Vector Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

vector_expression<matrix_column<M> >

Members
Member Description

matrix_column (matrix_type &data, size_type j)

Constructs a sub vector.

size_type size () const

Returns the size of the sub vector.

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.

matrix_column &operator = (const matrix_column &mc)

The assignment operator.

matrix_column &assign_temporary (matrix_column &mc)

Assigns a temporary. May change the matrix column mc .

template<class AE> matrix_column &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_column &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_column &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> matrix_column &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_column &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> matrix_column &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> matrix_column &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> matrix_column &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (matrix_column &mc)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the matrix_column.

const_iterator end () const

Returns a const_iterator pointing to the end of the matrix_column.

iterator begin ()

Returns a iterator pointing to the beginning of the matrix_column.

iterator end ()

Returns a iterator pointing to the end of the matrix_column.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed matrix_column.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed matrix_column.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed matrix_column.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed matrix_column.

Projections
Description

The free column functions support the construction of matrix columns.

Prototypes
    template<class M>
    matrix_column<M> column (M &data, std::size_t j);
    template<class M>
    const matrix_column<const M> column (const M &data, std::size_t j);
Definition

Defined in the header matrix_proxy.hpp.

Type requirements
Complexity

Linear depending from the size of the column.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned j = 0; j < m.size2 (); ++ j) {
        for (unsigned i = 0; i < m.size1 (); ++ i)
            column (m, j) (i) = 3 * i + j;
        std::cout << column (m, j) << std::endl;
    }
}
Vector Range
Description

The templated class matrix_vector_range<M> allows addressing a sub vector of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;

    matrix_vector_range<matrix<double> > mvr (m, range (0, 3), range (0, 3));
    std::cout << mvr << std::endl;
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified ranges fall outside that of the index range of the matrix, then the matrix_vector_range is not a well formed Vector Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

vector_expression<matrix_vector_range<M> >

Members
Member Description

matrix_vector_range (matrix_type &data, const range &r1, const range &r2)

Constructs a sub vector.

size_type size () const

Returns the size of the sub vector.

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.

matrix_vector_range &operator = (const matrix_vector_range &mvr)

The assignment operator.

matrix_vector_range &assign_temporary (matrix_vector_range &mvr)

Assigns a temporary. May change the matrix vector range mvr.

template<class AE> matrix_vector_range &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_vector_range &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_vector_range &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> matrix_vector_range &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_vector_range &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> matrix_vector_range &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> matrix_vector_range &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> matrix_vector_range &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (matrix_vector_range &mvr)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the matrix_vector_range.

const_iterator end () const

Returns a const_iterator pointing to the end of the matrix_vector_range.

iterator begin ()

Returns a iterator pointing to the beginning of the matrix_vector_range.

iterator end ()

Returns a iterator pointing to the end of the matrix_vector_range.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the matrix_vector_range.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed matrix_vector_range.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed matrix_vector_range.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed matrix_vector_range.

Vector Slice
Description

The templated class matrix_vector_slice<M> allows addressing a sliced sub vector of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;

    matrix_vector_slice<matrix<double> > mvs (m, slice (0, 1, 3), slice (0, 1, 3));
    std::cout << mvs << std::endl;
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified slices fall outside that of the index range of the matrix, then the matrix_vector_slice is not a well formed Vector Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

vector_expression<matrix_vector_slice<M> >

Members
Member Description

matrix_vector_slice (matrix_type &data, const slice &s1, const slice &s2)

Constructs a sub vector.

size_type size () const

Returns the size of the sub vector.

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.

matrix_vector_slice &operator = (const matrix_vector_slice &mvs)

The assignment operator.

matrix_vector_slice &assign_temporary (matrix_vector_slice &mvs)

Assigns a temporary. May change the matrix vector slice vs.

template<class AE> matrix_vector_slice &operator = (const vector_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_vector_slice &assign (const vector_expression<AE> &ae)

Assigns a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_vector_slice &operator += (const vector_expression<AE> &ae)

A computed assignment operator. Adds the vector expression to the sub vector.

template<class AE> matrix_vector_slice &plus_assign (const vector_expression<AE> &ae)

Adds a vector expression to the sub vector. Left and right hand side of the assignment should be independent.

template<class AE> matrix_vector_slice &operator -= (const vector_expression<AE> &ae)

A computed assignment operator. Subtracts the vector expression from the sub vector.

template<class AE> matrix_vector_slice &minus_assign (const vector_expression<AE> &ae)

Subtracts a vector expression from the sub vector. Left and right hand side of the assignment should be independent.

template<class AT> matrix_vector_slice &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub vector with a scalar.

template<class AT> matrix_vector_slice &operator /= (const AT &at)

A computed assignment operator. Divides the sub vector through a scalar.

void swap (matrix_vector_slice &mvs)

Swaps the contents of the sub vectors.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the matrix_vector_slice.

const_iterator end () const

Returns a const_iterator pointing to the end of the matrix_vector_slice.

iterator begin ()

Returns a iterator pointing to the beginning of the matrix_vector_slice.

iterator end ()

Returns a iterator pointing to the end of the matrix_vector_slice.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed matrix_vector_slice.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed matrix_vector_slice.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed matrix_vector_slice.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed matrix_vector_slice.

Matrix Range
Description

The templated class matrix_range<M> allows addressing a sub matrix of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    matrix_range<matrix<double> > mr (m, range (0, 3), range (0, 3));
    for (unsigned i = 0; i < mr.size1 (); ++ i)
        for (unsigned j = 0; j < mr.size2 (); ++ j)
            mr (i, j) = 3 * i + j;
    std::cout << mr << std::endl;
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified ranges fall outside that of the index range of the matrix, then the matrix_range is not a well formed Matrix Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

matrix_expression<matrix_range<M> >

Members
Member Description

matrix_range (matrix_type &data, const range &r1, const range &r2)

Constructs a sub matrix.

size_type start1 () const

Returns the index of the first row.

size_type size1 () const

Returns the number of rows.

size_type start2 () const

Returns the index of the first column.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

matrix_range &operator = (const matrix_range &mr)

The assignment operator.

matrix_range &assign_temporary (matrix_range &mr)

Assigns a temporary. May change the matrix range mr .

template<class AE> matrix_range &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_range &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the sub matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix_range &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the sub matrix.

template<class AE> matrix_range &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the sub matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix_range &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the sub matrix.

template<class AE> matrix_range &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the sub matrix. Left and right hand side of the assignment should be independent.

template<class AT> matrix_range &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub matrix with a scalar.

template<class AT> matrix_range &operator /= (const AT &at)

A computed assignment operator. Divides the sub matrix through a scalar.

void swap (matrix_range &mr)

Swaps the contents of the sub matrices.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the matrix_range.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the matrix_range.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the matrix_range.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the matrix_range.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the matrix_range.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the matrix_range.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the matrix_range.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the matrix_range.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed matrix_range.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed matrix_range.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed matrix_range.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed matrix_range.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed matrix_range.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed matrix_range.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed matrix_range.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of reversed the matrix_range.

Simple Projections
Description

The free subrange functions support the construction of matrix ranges.

Prototypes
    template<class M>
    matrix_range<M> subrange (M &data,
       M::size_type start1, M::size_type stop1, M::size_type start2, M::size_type, stop2);
    template<class M>
    const matrix_range<const M> subrange (const M &data,
       M::size_type start1, M::size_type stop1, M::size_type start2, M::size_type, stop2);
Generic Projections
Description

The free project functions support the construction of matrix ranges. Existing matrix_range’s can be composed with further ranges. The resulting ranges are computed using this existing ranges' `compose function.

Prototypes
    template<class M>
    matrix_range<M> project (M &data, const range &r1, const range &r2);
    template<class M>
    const matrix_range<const M> project (const M &data, const range &r1, const range &r2);
    template<class M>
    matrix_range<M> project (matrix_range<M> &data, const range &r1, const range &r2);
    template<class M>
    const matrix_range<M> project (const matrix_range<M> &data, const range &r1, const range &r2);
Definition

Defined in the header matrix_proxy.hpp.

Type requirements
Complexity

Quadratic depending from the size of the ranges.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            project (m, range (0, 3), range (0, 3)) (i, j) = 3 * i + j;
    std::cout << project (m, range (0, 3), range (0, 3)) << std::endl;
}
Matrix Slice
Description

The templated class matrix_slice<M> allows addressing a sliced sub matrix of a matrix.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    matrix_slice<matrix<double> > ms (m, slice (0, 1, 3), slice (0, 1, 3));
    for (unsigned i = 0; i < ms.size1 (); ++ i)
        for (unsigned j = 0; j < ms.size2 (); ++ j)
            ms (i, j) = 3 * i + j;
    std::cout << ms << std::endl;
}
Definition

Defined in the header matrix_proxy.hpp.

Template parameters

Parameter

Description

Default

M

The type of matrix referenced.

Model of

If the specified slices fall outside that of the index range of the matrix, then the matrix_slice is not a well formed Matrix Expression. That is, access to an element which is outside of the matrix is undefined.

Type requirements

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

Public base classes

matrix_expression<matrix_slice<M> >

Members
Member Description

matrix_slice (matrix_type &data, const slice &s1, const slice &s2)

Constructs a sub matrix.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

matrix_slice &operator = (const matrix_slice &ms)

The assignment operator.

matrix_slice &assign_temporary (matrix_slice &ms)

Assigns a temporary. May change the matrix slice ms .

template<class AE> matrix_slice &operator = (const matrix_expression<AE> &ae)

The extended assignment operator.

template<class AE> matrix_slice &assign (const matrix_expression<AE> &ae)

Assigns a matrix expression to the sub matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix_slice &operator += (const matrix_expression<AE> &ae)

A computed assignment operator. Adds the matrix expression to the sub matrix.

template<class AE> matrix_slice &plus_assign (const matrix_expression<AE> &ae)

Adds a matrix expression to the sub matrix. Left and right hand side of the assignment should be independent.

template<class AE> matrix_slice &operator -= (const matrix_expression<AE> &ae)

A computed assignment operator. Subtracts the matrix expression from the sub matrix.

template<class AE> matrix_slice &minus_assign (const matrix_expression<AE> &ae)

Subtracts a matrix expression from the sub matrix. Left and right hand side of the assignment should be independent.

template<class AT> matrix_slice &operator *= (const AT &at)

A computed assignment operator. Multiplies the sub matrix with a scalar.

template<class AT> matrix_slice &operator /= (const AT &at)

A computed assignment operator. Multiplies the sub matrix through a scalar.

void swap (matrix_slice &ms)

Swaps the contents of the sub matrices.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the matrix_slice.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the matrix_slice.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the matrix_slice.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the matrix_slice.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the matrix_slice.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the matrix_slice.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the matrix_slice.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the matrix_slice.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed matrix_slice.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed matrix_slice.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed matrix_slice.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed matrix_slice.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed matrix_slice.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed matrix_slice.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed matrix_slice.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed matrix_slice.

Simple Projections
Description

The free subslice functions support the construction of matrix slices.

Prototypes
    template<class M>
    matrix_slice<M> subslice (M &data,
       M::size_type start1, M::difference_type stride1, M::size_type size1,
       M::size_type start2, M::difference_type stride2, M::size_type size2);
    template<class M>
    const matrix_slice<const M> subslice (const M &data,
       M::size_type start1, M::difference_type stride1, M::size_type size1,
       M::size_type start2, M::difference_type stride2, M::size_type size2);
Generic Projections
Description

The free project functions support the construction of matrix slices. Existing matrix_slice 's can be composed with further ranges or slices. The resulting slices are computed using this existing slices' compose function.

Prototypes
    template<class M>
    matrix_slice<M> project (M &data, const slice &s1, const slice &s2);
    template<class M>
    const matrix_slice<const M> project (const M &data, const slice &s1, const slice &s2);
    template<class M>
    matrix_slice<M> project (matrix_slice<M> &data, const range &r1, const range &r2);
    template<class M>
    const matrix_slice<M> project (const matrix_slice<M> &data, const range &r1, const range &r2);
    template<class M>
    matrix_slice<M> project (matrix_slice<M> &data, const slice &s1, const slice &s2);
    template<class M>
    const matrix_slice<M> project (const matrix_slice<M> &data, const slice &s1, const slice &s2);
Definition

Defined in the header matrix_proxy.hpp.

Type requirements
Complexity

Quadratic depending from the size of the slices.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            project (m, slice (0, 1, 3), slice (0, 1, 3)) (i, j) = 3 * i + j;
    std::cout << project (m, slice (0, 1, 3), slice (0, 1, 3)) << std::endl;
}

Matrix Expressions

Description

The templated class matrix_expression<E> is required to be a public base of all classes which model the Matrix Expression concept.

Definition

Defined in the header expression_types.hpp.

Template parameters

Parameter

Description

Default

E

The type of the matrix expression.

 

Model of

None. Not a Matrix 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 operator[], row, column, range, slice and project functions have been removed. Use the free functions defined in matrix proxy instead.

Matrix Container
Description

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

Definition

Defined in the header expression_types.hpp.

Template parameters

Parameter

Description

Default

E

The type of the matrix expression.

 

Model of

None. Not a Matrix Expression OR Matrix

Type requirements

None.

Public base classes

matrix_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.

Matrix References
Reference
Description

The templated class matrix_reference<E> contains a reference to a matrix expression.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E

The type of the matrix expression.

 

Model of
Type requirements

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

Public base classes

matrix_expression<matrix_reference<E> >

Members
Member Description

matrix_reference (expression_type &e)

Constructs a constant reference of the expression.

void resize (size_type size1, size2)

Resizes the expression to hold at most size1 rows of size2 elements.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

reference operator () (size_type i, size_type j)

Returns a reference of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

iterator1 begin1 ()

Returns a iterator1 pointing to the beginning of the expression.

iterator1 end1 ()

Returns a iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

iterator2 begin2 ()

Returns a iterator2 pointing to the beginning of the expression.

iterator2 end2 ()

Returns a iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

reverse_iterator1 rbegin1 ()

Returns a reverse_iterator1 pointing to the beginning of the reversed expression.

reverse_iterator1 rend1 ()

Returns a reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

reverse_iterator2 rbegin2 ()

Returns a reverse_iterator2 pointing to the beginning of the reversed expression.

reverse_iterator2 rend2 ()

Returns a reverse_iterator2 pointing to the end of the reversed expression.

Matrix Operations
Unary Operation Description
Description

The templated classes matrix_unary1<E, F> and matrix_unary2<E, F> describe unary matrix operations.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E

The type of the matrix expression.

 

F

The type of the operation.

 

Model of
Type requirements

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

Public base classes

matrix_expression<matrix_unary1<E, F> > and matrix_expression<matrix_unary2<E, F> > resp.

Members
Member Description

matrix_unary1 (const expression_type &e)

Constructs a description of the expression.

matrix_unary2 (const expression_type &e)

Constructs a description of the expression.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

Unary Operations
Prototypes
template<class E, class F>
    struct matrix_unary1_traits {
        typedef matrix_unary1<typename E::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (- m) [i] [j] = - m [i] [j]
    template<class E>
     typename matrix_unary1_traits<E, scalar_negate<typename E::value_type> >::result_type
    operator - (const matrix_expression<E> &e);

    // (conj m) [i] [j] = conj (m [i] [j])
    template<class E>
     typename matrix_unary1_traits<E, scalar_conj<typename E::value_type> >::result_type
    conj (const matrix_expression<E> &e);

    // (real m) [i] [j] = real (m [i] [j])
    template<class E>
     typename matrix_unary1_traits<E, scalar_real<typename E::value_type> >::result_type
    real (const matrix_expression<E> &e);

    // (imag m) [i] [j] = imag (m [i] [j])
    template<class E>
     typename matrix_unary1_traits<E, scalar_imag<typename E::value_type> >::result_type
    imag (const matrix_expression<E> &e);

    template<class E, class F>
    struct matrix_unary2_traits {
        typedef matrix_unary2<typename E::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (trans m) [i] [j] = m [j] [i]
    template<class E>
     typename matrix_unary2_traits<E, scalar_identity<typename E::value_type> >::result_type
    trans (const matrix_expression<E> &e);

    // (herm m) [i] [j] = conj (m [j] [i])
    template<class E>
     typename matrix_unary2_traits<E, scalar_conj<typename E::value_type> >::result_type
    herm (const matrix_expression<E> &e);
Description

operator - computes the additive inverse of a matrix expression. conj computes the complex conjugate of a matrix expression. real and imag compute the real and imaginary parts of a matrix expression. trans computes the transpose of a matrix expression. herm computes the hermitian, i.e. the complex conjugate of the transpose of a matrix expression.

Definition

Defined in the header matrix_expression.hpp.

Type requirements
Preconditions

None.

Complexity

Quadratic depending from the size of the matrix expression.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<std::complex<double> > m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = std::complex<double> (3 * i + j, 3 * i + j);

    std::cout << - m << std::endl;
    std::cout << conj (m) << std::endl;
    std::cout << real (m) << std::endl;
    std::cout << imag (m) << std::endl;
    std::cout << trans (m) << std::endl;
    std::cout << herm (m) << std::endl;
}
Binary Operation Description
Description

The templated class matrix_binary<E1, E2, F> describes a binary matrix operation.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E1

The type of the first matrix expression.

E2

The type of the second matrix expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

matrix_expression<matrix_binary<E1, E2, F> >.

Members
Member Description

matrix_binary (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

Binary Operations
Prototypes
template<class E1, class E2, class F>
    struct matrix_binary_traits {
        typedef matrix_binary<typename E1::const_closure_type,
                               typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (m1 + m2) [i] [j] = m1 [i] [j] + m2 [i] [j]
    template<class E1, class E2>
    typename matrix_binary_traits<E1, E2, scalar_plus<typename E1::value_type,
                                                       typename E2::value_type> >::result_type
    operator + (const matrix_expression<E1> &e1,
                 const matrix_expression<E2> &e2);

    // (m1 - m2) [i] [j] = m1 [i] [j] - m2 [i] [j]
    template<class E1, class E2>
    typename matrix_binary_traits<E1, E2, scalar_minus<typename E1::value_type,
                                                        typename E2::value_type> >::result_type
    operator - (const matrix_expression<E1> &e1,
                 const matrix_expression<E2> &e2);
Description

operator + computes the sum of two matrix expressions. operator - computes the difference of two matrix expressions.

Definition

Defined in the header matrix_expression.hpp.

Type requirements
Preconditions
  • e1 ().size1 () == e2 ().size1 ()

  • e1 ().size2 () == e2 ().size2 ()

Complexity

Quadratic depending from the size of the matrix expressions.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m1 (3, 3), m2 (3, 3);
    for (unsigned i = 0; i < std::min (m1.size1 (), m2.size1 ()); ++ i)
        for (unsigned j = 0; j < std::min (m1.size2 (), m2.size2 ()); ++ j)
            m1 (i, j) = m2 (i, j) = 3 * i + j;

    std::cout << m1 + m2 << std::endl;
    std::cout << m1 - m2 << std::endl;
}
Scalar Matrix Operation Description
Description

The templated classes matrix_binary_scalar1<E1, E2, F> and matrix_binary_scalar2<E1, E2, F> describe binary operations between a scalar and a matrix.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E1/E2

The type of the scalar expression.

E2/E1

The type of the matrix expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

matrix_expression<matrix_binary_scalar1<E1, E2, F> > and matrix_expression<matrix_binary_scalar2<E1, E2, F> > resp.

Members
Member Description

matrix_binary_scalar1 (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

matrix_binary_scalar1 (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

Scalar Matrix Operations
Prototypes
template<class T1, class E2, class F>
    struct matrix_binary_scalar1_traits {
        typedef matrix_binary_scalar1<scalar_const_reference<T1>,
                                      typename E2::const_closure_type, F> expression_type;
        typedef expression_type result_type;
     };

    // (t * m) [i] [j] = t * m [i] [j]
    template<class T1, class E2>
    typename matrix_binary_scalar1_traits<T1, E2, scalar_multiplies<T1, typename E2::value_type> >::result_type
    operator * (const T1 &e1,
                 const matrix_expression<E2> &e2);

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

    // (m * t) [i] [j] = m [i] [j] * t
    template<class E1, class T2>
    typename matrix_binary_scalar2_traits<E1, T2, scalar_multiplies<typename E1::value_type, T2> >::result_type
    operator * (const matrix_expression<E1> &e1,
                 const T2 &e2);

    // (m / t) [i] [j] = m [i] [j] / t
    template<class E1, class T2>
    typename matrix_binary_scalar2_traits<E1, T2, scalar_divides<typename E1::value_type, T2> >::result_type
    operator / (const matrix_expression<E1> &e1,
                 const T2 &e2);
Description

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

Definition

Defined in the header matrix_expression.hpp.

Type requirements
Preconditions

None.

Complexity

Quadratic depending from the size of the matrix expression.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    for (unsigned i = 0; i < m.size1 (); ++ i)
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;

    std::cout << 2.0 * m << std::endl;
    std::cout << m * 2.0 << std::endl;
}
Matrix Vector Operations
Binary Operation Description
Description

The templated classes matrix_vector_binary1<E1, E2, F> and matrix_vector_binary2<E1, E2, F> describe binary matrix vector operations.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E1

The type of the matrix or vector expression.

E2

The type of the vector or matrix expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

vector_expression<matrix_vector_binary1<E1, E2, F> > and vector_expression<matrix_vector_binary2<E1, E2, F> > resp.

Members
Member Description

matrix_vector_binary1 (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

matrix_vector_binary2 (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.

Binary Operations
Prototypes
template<class T1, class E1, class T2, class E2>
    struct matrix_vector_binary1_traits {
        typedef row_major_tag dispatch_category;
        typedef typename promote_traits<T1, T2>::promote_type promote_type;
        typedef matrix_vector_binary1<typename E1::const_closure_type,
                                       typename E2::const_closure_type,
                                       matrix_vector_prod1<T1, T2, promote_type> > expression_type;
        typedef expression_type result_type;
     };

    template<class E1, class E2>
    typename matrix_vector_binary1_traits<typename E1::value_type, E1,
                                           typename E2::value_type, E2>::result_type
    prod (const matrix_expression<E1> &e1,
           const vector_expression<E2> &e2,
          row_major_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_vector_binary1_traits<typename E1::value_type, E1,
                                           typename E2::value_type, E2>::result_type
    prod (const matrix_expression<E1> &e1,
           const vector_expression<E2> &e2);

    template<class E1, class E2>
    typename matrix_vector_binary1_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                           typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const matrix_expression<E1> &e1,
                const vector_expression<E2> &e2,
               row_major_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_vector_binary1_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                           typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const matrix_expression<E1> &e1,
                const vector_expression<E2> &e2);

    template<class V, class E1, class E2>
    V
    prod (const matrix_expression<E1> &e1,
          const vector_expression<E2> &e2);

    template<class V, class E1, class E2>
    V
    prec_prod (const matrix_expression<E1> &e1,
               const vector_expression<E2> &e2);

    template<class T1, class E1, class T2, class E2>
    struct matrix_vector_binary2_traits {
        typedef column_major_tag dispatch_category;
        typedef typename promote_traits<T1, T2>::promote_type promote_type;
        typedef matrix_vector_binary2<typename E1::const_closure_type,
                                       typename E2::const_closure_type,
                                       matrix_vector_prod2<T1, T2, promote_type> > expression_type;
        typedef expression_type result_type;
     };

    template<class E1, class E2>
    typename matrix_vector_binary2_traits<typename E1::value_type, E1,
                                           typename E2::value_type, E2>::result_type
    prod (const vector_expression<E1> &e1,
           const matrix_expression<E2> &e2,
          column_major_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_vector_binary2_traits<typename E1::value_type, E1,
                                           typename E2::value_type, E2>::result_type
    prod (const vector_expression<E1> &e1,
           const matrix_expression<E2> &e2);

    template<class E1, class E2>
    typename matrix_vector_binary2_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                           typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const vector_expression<E1> &e1,
                const matrix_expression<E2> &e2,
               column_major_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_vector_binary2_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                           typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const vector_expression<E1> &e1,
                const matrix_expression<E2> &e2);

    template<class V, class E1, class E2>
    V
    prod (const vector_expression<E1> &e1,
          const matrix_expression<E2> &e2);

    template<class V, class E1, class E2>
    V
    prec_prod (const vector_expression<E1> &e1,
               const matrix_expression<E2> &e2);
Description

prod computes the product of the matrix and the vector expression. prec_prod computes the double precision product of the matrix and the vector expression.

Definition

Defined in the header matrix_expression.hpp.

Type requirements
Preconditions
  • e1 ().size2 () == e2 ().size ()

  • e1 ().size () == e2 ().size1 ()

Complexity

Quadratic depending from the size of the matrix expression.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    vector<double> v (3);
    for (unsigned i = 0; i < std::min (m.size1 (), v.size ()); ++ i) {
        for (unsigned j = 0; j < m.size2 (); ++ j)
            m (i, j) = 3 * i + j;
        v (i) = i;
    }

    std::cout << prod (m, v) << std::endl;
    std::cout << prod (v, m) << std::endl;
}
Triangular Solver
Prototypes
template<class E1, class E2>
    struct matrix_vector_solve_traits {
        typedef typename promote_traits<typename E1::value_type, typename E2::value_type>::promote_type promote_type;
        typedef vector<promote_type> result_type;
    };

    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                         E2 &e2,
                        lower_tag,
                        vector_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                         E2 &e2,
                        upper_tag,
                        vector_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                         E2 &e2,
                        unit_lower_tag,
                        vector_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                         E2 &e2,
                        unit_upper_tag,
                        vector_tag);

    template<class E1, class E2, class C>
    typename matrix_vector_solve_traits<E1, E2>::result_type
    solve (const matrix_expression<E1> &e1,
            const vector_expression<E2> &e2,
           C);

    template<class E1, class E2>
    void inplace_solve (E1 &e1,
                        const matrix_expression<E2> &e2,
                         vector_tag,
                         lower_tag);
    template<class E1, class E2>
    void inplace_solve (E1 &e1,
                        const matrix_expression<E2> &e2,
                         vector_tag,
                         upper_tag);
    template<class E1, class E2>
    void inplace_solve (E1 &e1,
                        const matrix_expression<E2> &e2,
                         vector_tag,
                         unit_lower_tag);
    template<class E1, class E2>
    void inplace_solve (E1 &e1,
                        const matrix_expression<E2> &e2,
                         vector_tag,
                         unit_upper_tag);

    template<class E1, class E2, class C>
    typename matrix_vector_solve_traits<E1, E2>::result_type
    solve (const vector_expression<E1> &e1,
            const matrix_expression<E2> &e2,
           C);
Description

solve solves a linear equation for lower or upper (unit) triangular matrices.

Definition

Defined in the header triangular.hpp.

Type requirements
Preconditions
  • e1 ().size1 () == e1 ().size2 ()

  • e1 ().size2 () == e2 ().size ()

  • e1 ().size () == e2 ().size1 ()

  • e2 ().size1 () == e2 ().size2 ()

Complexity

Quadratic depending from the size of the matrix expression.

Examples
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m (3, 3);
    vector<double> v (3);
    for (unsigned i = 0; i < std::min (m.size1 (), v.size ()); ++ i) {
        for (unsigned j = 0; j <= i; ++ j)
            m (i, j) = 3 * i + j + 1;
        v (i) = i;
    }

    std::cout << solve (m, v, lower_tag ()) << std::endl;
    std::cout << solve (v, m, lower_tag ()) << std::endl;
}
Matrix Matrix Operations
Binary Operation Description
Description

The templated class matrix_matrix_binary<E1, E2, F> describes a binary matrix operation.

Definition

Defined in the header matrix_expression.hpp.

Template parameters

Parameter

Description

Default

E1

The type of the first matrix expression.

E2

The type of the second matrix expression.

F

The type of the operation.

Model of
Type requirements

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

Public base classes

matrix_expression<matrix_matrix_binary<E1, E2, F> > .

Members
Member Description

matrix_matrix_binary (const expression1_type &e1, const expression2_type &e2)

Constructs a description of the expression.

size_type size1 () const

Returns the number of rows.

size_type size2 () const

Returns the number of columns.

const_reference operator () (size_type i, size_type j) const

Returns the value of the j-th element in the i-th row.

const_iterator1 begin1 () const

Returns a const_iterator1 pointing to the beginning of the expression.

const_iterator1 end1 () const

Returns a const_iterator1 pointing to the end of the expression.

const_iterator2 begin2 () const

Returns a const_iterator2 pointing to the beginning of the expression.

const_iterator2 end2 () const

Returns a const_iterator2 pointing to the end of the expression.

const_reverse_iterator1 rbegin1 () const

Returns a const_reverse_iterator1 pointing to the beginning of the reversed expression.

const_reverse_iterator1 rend1 () const

Returns a const_reverse_iterator1 pointing to the end of the reversed expression.

const_reverse_iterator2 rbegin2 () const

Returns a const_reverse_iterator2 pointing to the beginning of the reversed expression.

const_reverse_iterator2 rend2 () const

Returns a const_reverse_iterator2 pointing to the end of the reversed expression.

Binary Operations
Prototypes
template<class T1, class E1, class T2, class E2>
    struct matrix_matrix_binary_traits {
        typedef unknown_orientation_tag dispatch_category;
        typedef typename promote_traits<T1, T2>::promote_type promote_type;
        typedef matrix_matrix_binary<typename E1::const_closure_type,
                                     typename E2::const_closure_type,
                                     matrix_matrix_prod<T1, T2, promote_type> > expression_type;
        typedef expression_type result_type;
    };

    template<class E1, class E2>
    typename matrix_matrix_binary_traits<typename E1::value_type, E1,
                                         typename E2::value_type, E2>::result_type
    prod (const matrix_expression<E1> &e1,
          const matrix_expression<E2> &e2,
          unknown_orientation_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_matrix_binary_traits<typename E1::value_type, E1,
                                         typename E2::value_type, E2>::result_type
    prod (const matrix_expression<E1> &e1,
          const matrix_expression<E2> &e2);

    template<class E1, class E2>
    typename matrix_matrix_binary_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                         typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const matrix_expression<E1> &e1,
               const matrix_expression<E2> &e2,
               unknown_orientation_tag);

    // Dispatcher
    template<class E1, class E2>
    typename matrix_matrix_binary_traits<typename type_traits<typename E1::value_type>::precision_type, E1,
                                         typename type_traits<typename E2::value_type>::precision_type, E2>::result_type
    prec_prod (const matrix_expression<E1> &e1,
               const matrix_expression<E2> &e2);

    template<class M, class E1, class E2>
    M
    prod (const matrix_expression<E1> &e1,
          const matrix_expression<E2> &e2);

    template<class M, class E1, class E2>
    M
    prec_prod (const matrix_expression<E1> &e1,
               const matrix_expression<E2> &e2);
Description

prod computes the product of the matrix expressions. prec_prod computes the double precision product of the matrix expressions.

Definition

Defined in the header matrix_expression.hpp.

Type requirements
Preconditions
  • e1 ().size2 () == e2 ().size1 ()

Complexity

Cubic depending from the size of the matrix expression.

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

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m1 (3, 3), m2 (3, 3);
    for (unsigned i = 0; i < std::min (m1.size1 (), m2.size1 ()); ++ i)
        for (unsigned j = 0; j < std::min (m1.size2 (), m2.size2 ()); ++ j)
            m1 (i, j) = m2 (i, j) = 3 * i + j;

    std::cout << prod (m1, m2) << std::endl;
}
Triangular Solvers
Prototypes
template<class E1, class E2>
    struct matrix_matrix_solve_traits {
        typedef typename promote_traits<typename E1::value_type, typename E2::value_type>::promote_type promote_type;
        typedef matrix<promote_type> result_type;
    };

    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                        E2 &e2,
                        lower_tag,
                        matrix_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                        E2 &e2,
                        upper_tag,
                        matrix_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                        E2 &e2,
                        unit_lower_tag,
                        matrix_tag);
    template<class E1, class E2>
    void inplace_solve (const matrix_expression<E1> &e1,
                        E2 &e2,
                        unit_upper_tag,
                        matrix_tag);

    template<class E1, class E2, class C>
    typename matrix_matrix_solve_traits<E1, E2>::result_type
    solve (const matrix_expression<E1> &e1,
           const matrix_expression<E2> &e2,
           C);
Description

solve solves a linear equation for lower or upper (unit) triangular matrices.

Definition

Defined in the header triangular.hpp.

Type requirements
Preconditions
  • e1 ().size1 () == e1 ().size2 ()

  • e1 ().size2 () == e2 ().size1 ()

Complexity

Cubic depending from the size of the matrix expressions.

Examples
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/io.hpp>

int main () {
    using namespace boost::numeric::ublas;
    matrix<double> m1 (3, 3), m2 (3, 3);
    for (unsigned i = 0; i < std::min (m1.size1 (), m2.size1 ()); ++ i)
        for (unsigned j = 0; j <= i; ++ j)
            m1 (i, j) = m2 (i, j) = 3 * i + j + 1;

    std::cout << solve (m1, m2, lower_tag ()) << std::endl;
}

Tensor

Tensor Idealogy

tensor_index<value_t, storage_t, array_t, N>
Description

The template class tensor_index decorates the tensor template class with indices for tensor contraction.

Example
#include <boost/numeric/ublas/tensor/einstein.hpp>

int main () {
    using namespace boost::numeric::ublas;
    shape s{4,3,2};
    for (auto i = 0u; i < s.size(); ++i) {
        std::cout << s.at(i) << std::endl;
    }
}
Definition

Defined in the header tensor/tensor_einstein.hpp.

Public base classes

None.

Template parameters

Parameter

Description

value_t

The type of object stored in the tensor.

format_t

Storage organization of the tensor.

storage_t

The type of the storage array of the tensor.

N

Number of indices provided.

Tensor Abstract

a. Description

The templated class tensor<value_t,format_t,storage_t> is the base container adaptor for dense tensors. Every element $(t_/ i_1, i_2, \dots, i_p)$ of a $p$-order $(n_1 \times n_2 \times \cdots \times n_p)$-dimensional tensor $T$ is mapped to $j$-th element of a one-dimensional container where $(j = \sum_ /{r=1}^p i_r \cdot w_r)$ with $1 \leq i_r \leq n_r $ for $1 \leq r \leq p$. For the first-order orientation $w_1 = 1$ and $(w_k = n_{k-1} \cdot w_{k-1})$ for $k > 1$. For last-order orientation $w_p = 1$ and $(w_k = n_/{k+1} \cdot w_/{k+1})$ for $k < p$.

b. Example
#include <boost/numeric/ublas/tensor.hpp>

int main () {
  using namespace boost::numeric::ublas;
  tensor<double> t{4,2,3};
  for (auto k = 0ul; k < t.size (2); ++ k)
    for (auto j = 0ul; j < t.size (1); ++ j)
      for (auto i = 0ul; i < t.size (0); ++ i)
        t.at(i,j,k) = 3*i + 2*j + 5*k;

  std::cout << t << std::endl;
}
c. Definition

Defined in the header file tensor/tensor.hpp.

d. Model of
e. Type requirements

None, except for those imposed by the requirements of Tensor .

f. Public base classes

tensor_container<tensor<value_t,format_t,storage_t> >

g. Template parameters
Parameter Description Default

value_t

The type of object stored in the tensor.

format_t

Storage organization. [1]

first_order

storage_t

The type of the Storage array. [2]

std::vector<value_t>

h. Member types
Member type Description

value_type

Type value_t of the tensor elements.

layout_type

Format of the tensor which is either first_order or last_order.

array_type

Sequence container type that stores all tensor elements and is accessible with a single index.

strides_type

Type of the strides vector basic_strides<std::size_t,layout_type> that stores all tensor elements and is accessible with a single index.

extents_type

Type of the dimension extents vector shape that stores all tensor elements and is accessible with a single index.

size_type

Unsigned integer which is usually std::size_t.

difference_type

Unsigned integer which is usually std::ptrdiff_t.

reference

Reference type storage_type::reference which is in most cases value_type&.

const_reference

Constant reference type storage_type::const_reference which is in most cases const value_type&.

pointer

Pointer type storage_type::pointer which is in most cases value_type*.

const_pointer

Constant reference type storage_type::const_reference which is in most cases const value_type*.

iterator

RandomAccessIterator storage_type::iterator.

const_iterator

Constant RandomAccessIterator storage_type::const_iterator.

reverse_iterator

Reverse RandomAccessIterator storage_type::reverse_iterator.

const_reverse_iterator

Reverse RandomAccessIterator storage_type::const_reverse_iterator.

matrix_type

Type of the matrix matrix<value_type,layout_type,array_type> with which the tensor type interacts.

vector_type

Type of the vector matrix<value_type,layout_type,array_type> with which the tensor type interacts.

i. Alias templates
Alias template Description

template<class derived_type> using tensor_expression_type = detail::tensor_expression<self_type,derived_type>

Type of tensor_expression where self_type is the tensor type.

template<class derived_type> using matrix_expression_type = matrix_expression<derived_type>

Type of matrix_expression.

template<class derived_type> using vector_expression_type = vector_expression<derived_type>

Type of vector_expression.

j. Member Functions
i. Construction
Member function Description

tensor ()

Constructs an uninitialized tensor that holds zero elements.

tensor (std::initializer_list<size_type> list)

Constructs an uninitialized tensor where list specifies the dimension extents.

tensor (extents_type const& s)

Constructs an uninitialized tensor where s specifies the dimension extents.

tensor (extents_type const& e, array_type const& a)

Constructs an uninitialized tensor where e specifies the dimension extents and a the data elements of the tensor.

tensor (tensor<value_type,other_layout&rt; const& other)

Constructs tensor by copying elements from other where the layout is different from this layout type.

tensor (tensor const& other)

Constructs tensor by copying elements from other.

tensor (tensor && other)

Constructs tensor by moving elements from other.

tensor (matrix_type const& other)

Constructs tensor by copying elements from other matrix. The tensor will have the order 2.

tensor (matrix_type && other)

Constructs tensor by moving elements from other matrix. The tensor will have the order 2.

tensor (vector_type const& other)

Constructs tensor by copying elements from other vector. The tensor will have the order 1.

tensor (vector_type && other)

Constructs tensor by moving elements from other vector. The tensor will have the order 1.

tensor (tensor_expression_type<derived_type> const& expr)

Constructs tensor by evaluating the tensor expression expr and copying all elements of the result.

tensor (matrix_expression_type<derived_type> const& expr)

Constructs tensor by evaluating the matrix expression expr and copying all elements of the result.

tensor (vector_expression_type<derived_type> const& expr)

Constructs tensor by evaluating the vector expression expr and copying all elements of the result.

ii. Assignment
Member function Description

tensor& operator=(tensor_expression_type<derived_type> const& expr)

Evaluates the tensor expression expr and copyies all elements of the result.

tensor& operator=(tensor other)

Copies or moves elements of other.

tensor& operator=(const_reference v)

Initialiates all elements of a tensor with v.

iii. Capacity
Member function Description

bool empty() const

Returns true if a tensor has zero elements.

size_type size() const

Returns the number of elements of the tensor.

size_type rank() const

Returns the number of dimensions of the tensor.

size_type order() const

Returns the number of dimensions of the tensor.

strides_type const& strides() const

Returns a constant reference to the strides of the tensor.

extents_type const& extents() const

Returns a constant reference to the extents of the tensor.

iv. Element access
Member function Description

pointer data()

Returns a pointer the first element of the tensor.

const_pointer data() const

Returns a const_pointer the first element of the tensor.

reference operator[](size_type j)

Returns a reference to the j-th element of the storage array of the tensor. Corresponds to the function call tensor::data()+j

const_reference operator[](size_type j) const

Returns a const_reference to the j-th element of the storage array of the tensor. Corresponds to the function call tensor::data()+j.

template<class …​ size_types> reference at(size_type i, size_types …​ is)

Returns a reference to the (i,is…​)-th element of the tensor where ` (i,is…​)` denotes a multi-index with tensor::order() elements. If sizeof…​(is)==0, tensor::operator[i] is called.

template<class …​ size_types> const_reference at(size_type i, size_types …​ is)

Returns a const_reference to the (i,is…​)-th element of the tensor where ` (i,is…​)` denotes a multi-index with tensor::order() elements. If sizeof…​(is)==0, tensor::operator[i] is called.

v. Proxy Generation
Member function Description

template<std::size_t I, class …​ index_types> tensor_index operator()(indices::Index<I> p, index_types …​ ps)

Returns a tensor index instance with index objects (p,ps…​) for a tensor contraction where sizeof…​(ps)+1 must be equal to tensor::order().

vi. Iterators
Member function Description

const_iterator begin() const

Returns a const_iterator pointing to the first element of the tensor.

const_iterator cbegin() const

Returns a const_iterator pointing to the first element of the tensor.

iterator begin()

Returns an iterator pointing to the first element of the tensor.

const_iterator end() const

Returns a const_iterator pointing to the position after the last element of the tensor.

const_iterator cend() const

Returns a const_iterator pointing to the position after the last element of the tensor.

iterator begin()

Returns an iterator pointing to the position after the last element of the tensor.

vii. Modifiers
Member function Description

void reshape(extents_type const& e, value_type v = value_type{})

Reshapes the tensor according to the extents e. If e.product() is greater than tensor::size(), the tensor is resized with v.

viii. Notes

[1] Supported parameters for the storage organization are first_order and last_order.

[2] Common parameters for the storage array are std::array<N,T> and std::vector<T>.

Tensor Expressions

Description

The templated class tensor_expression<T,E> is required to be a public base of all classes. There is no Tensor Expression concept defined.

Definition

Defined in the header tensor/expression.hpp.

Model of

None. Not a Tensor Expression!

Type requirements

None.

Public base classes

ublas_expression<E>.

Template parameters

Parameter

Description

T

The type of the tensor.

E

The type of the tensor expression.

Member types

Member type

Description

expression_type

Type of the derived expression which is E.

type_category

Tag for categorization which is tensor_tag.

tensor_type

Reference type which is T.

Public Member Functions
Member Description

const expression_type &operator()() const

Returns a const reference to the derived expression.

Entrywise Tensor Operations
Binary Tensor Expression
Description

The templated class binary_tensor_expression<T,EL,ER,OP> contains a constant reference to a left and right expression that can be evaluated by using the access operator.

Definition

Defined in the header tensor/expression.hpp.

Model of

Tensor Expression

Type requirements

None.

Public base classes

tensor_expression<T,binary_tensor_expression<T,EL,ER,OP>>

Template parameters

Parameter

Description

T

Type of the tensor.

EL

Type of the left binary tensor expression.

ER

Type of the right binary tensor expression.

OP

Type of the binary operation.

Member types

Member type

Description

expression_type_left

Type of the left expression which is EL.

expression_type_right

Type of the right expression which is ER.

tensor_type

Reference type which is T.

binary_operation

Type of the binary operation which is OP.

Public Member Functions
Member Description

decltype(auto) operator()(std::size_t i) const

Returns a const reference to the i-th element of the expression.

Unary Tensor Expression
Description

The templated class unary_tensor_expression<T,E,OP> contains a constant reference to an expression that can be evaluated by using the access operator.

Definition

Defined in the header tensor/expression.hpp.

Model of

Tensor Expression

Type requirements

None.

Public base classes

tensor_expression<T,unary_tensor_expression<T,E,OP>>

Template parameters

Parameter

Description

T

Type of the tensor.

E

Type of the unary tensor expression.

OP

Type of the unary operation.

Member types

Member type

Description

expression_type

Type of the expression which is E.

tensor_type

Reference type which is T.

unary_operation

Type of the unary operation which is OP.

Public Member Functions
Member Description

decltype(auto) operator()(std::size_t i) const

Returns a const reference to the i-th element of the expression.

Tensor Extents

basic_extents<size_type>
Description

The template class basic_extents specifies dimension extents of a tensor instance.

Example
#include <boost/numeric/ublas/tensor/extents.hpp>

int main () {
    using namespace boost::numeric::ublas;
    shape s{4,3,2};
    for (auto i = 0u; i < s.size(); ++i) {
        std::cout << s.at(i) << std::endl;
    }
}
Definition

Defined in the header tensor/extents.hpp.

Public base classes

None.

Specialization

using shape = basic_extents<std::size_t>

Template parameters

Parameter

Description

size_type

Unsigned integer type.

Member types
Member type Description

value_type

Type size_type of the extents.

size_type

Unsigned integer such as std::size_t.

reference

Reference type which is value_type&.

const_reference

Constant reference type which is const value_type&.

pointer

Pointer type which is value_type*.

const_pointer

Constant reference type which is const value_type*.

Member Functions
Member Function Description

basic_extents ()

Constructs an empty instance of basic_extents.

basic_extents (std::vector<value_type> const& vector)

Constructs an instance copying the content of vector.

basic_extents (std::vector<value_type> && vector)

Constructs an instance moving the content of vector.

basic_extents (std::initializer_list<value_type> list)

Constructs an instance from list.

basic_extents (const_iterator first, const_iterator last)

Constructs an instance from the range specified by [first,last).

basic_extents (basic_extents const& other)

Constructs an instance from other copying its elements.

basic_extents (basic_extents && other)

Constructs an instance from other by moving its elements.

basic_extents& operator= (basic_extents other)

Assigns the elements of other to this instance.

bool is_scalar() const

Returns true if the elements are (1,1,[1,…​,1]).

bool is_vector() const

Returns true if the elements are (n,1,[1,…​,1]) or (1,n,[1,…​,1]) where n>1.

bool is_matrix() const

Returns true if the elements are (m,n,[1,…​,1]) where m>1 and n>1.

bool is_tensor() const

Returns true if it is not a scalar, vector or matrix.

const_pointer data() const

Returns a const_pointer to the first element.

pointer data()

Returns a pointer to the first element.

reference operator[](size_type i)

Returns a reference to the i-th element.

const_reference operator[](size_type i) const

Returns a const_reference to the i-th element.

reference at(size_type i)

Returns a reference to the i-th element.

const_reference at(size_type i) const

Returns a const_reference to the i-th element.

bool empty()

Returns true if the container has no elements.

size_type size() const

Returns the number of elements.

bool valid() const

Returns true if size()>1 and all elements are greater than one.

size_type product() const

Returns the multiplication of all entries.

basic_extents squeeze() const

Returns a new instance where entries equal to one are eliminated.

bool operator==(basic_extents const& b) const

Returns true if all elements are equal.

bool operator!=(basic_extents const& b) const

Returns true if some elements are not equal.

const_iterator begin() const

Returns an const_iterator pointing to the first element.

const_iterator end() const

Returns a const_iterator pointing to an element passed the last element.

std::vector<size_type> base() const

Returns a const reference to the private member sequence container holding all elements.

Tensor Strides

basic_strides<size_type, format_type>
Description

The template class basic_strides contains weights for a given storage format in order to map multi-indices to scalar memory indices for tensor instances.

Example
#include <boost/numeric/ublas/tensor/strides.hpp>

int main () {
    using namespace boost::numeric::ublas;
    auto wf = strides<first_order>(shape{4,3,2});
    for (auto i = 0u; i < wf.size(); ++i)
        std::cout << wf.at(i) << std::endl;
        // 1,4,12

    auto wl = strides<first_order>(shape{4,3,2});
    for (auto i = 0u; i < wl.size(); ++i)
        std::cout << wl.at(i) << std::endl;
        // 6,2,1
}
Definition

Defined in the header tensor/strides.hpp.

Public base classes

None.

Specialization

template<class format_t>using strides = basic_strides<std::size_t,format_t>

Template parameters

Parameter

Description

size_type

Unsigned integer type.

Member types
Member type Description

value_type

Type size_type of the strides.

size_type

Unsigned integer such as std::size_t.

reference

Reference type which is value_type&.

const_reference

Constant reference type which is const value_type&.

pointer

Pointer type which is value_type*.

const_pointer

Constant pointer type which is const value_type*.

layout_type

Layout type which can be either boost::numeric::ublas::first_order or boost::numeric::ublas::last_order.

Member Functions
Member Function Description

basic_strides ()

Constructs an empty instance of basic_strides.

basic_strides (basic_extents<value_type> const& be)

Constructs an instance based on the tensor extents specified by be.

basic_strides (std::vector<value_type> const& v)

Constructs an instance copying the content of v.

basic_strides (std::vector<value_type> && v)

Constructs an instance moving the content of v.

basic_strides (basic_strides const& other)

Constructs an instance from other copying its elements.

basic_strides (basic_strides && other)

Constructs an instance from other by moving its elements.

basic_strides& operator= (basic_strides other)

Assigns the elements of other to this instance.

const_pointer data() const

Returns a const_pointer to the first element.

const_reference operator[](size_type i) const

Returns a const_reference to the i-th element.

const_reference at(size_type i) const

Returns a const_reference to the i-th element.

bool empty()

Returns true if the container has no elements.

size_type size() const

Returns the number of elements.

void clear()

Erases all elements.

bool operator==(basic_strides const& b) const

Returns true if all elements are equal.

bool operator!=(basic_strides const& b) const

Returns true if some elements are not equal.

const_iterator begin() const

Returns an const_iterator pointing to the first element.

const_iterator end() const

Returns a const_iterator pointing to an element passed the last element.

std::vector<size_type> base() const

Returns the private member sequence container holding all elements.

Non-Member Functions
Function Description

access(std::vector<size_type> const& i, strides<layout_type> w)

Returns relative memory location depending on the multi-index vector i and strides w.

access(size_type sum, strides<layout_type> w, size_type i, size_types …​ is)

Returns relative memory location depending on the indices i, is …​ and stride vector w (recursive function).

Tensor Expressions

Description

The templated class tensor_expression<T,E> is required to be a public base of all classes. There is no Tensor Expression concept defined.

Definition

Defined in the header tensor/expression.hpp.

Model of

None. Not a Tensor Expression!

Type requirements

None.

Public base classes

ublas_expression<E>.

Template parameters

Parameter

Description

T

The type of the tensor.

E

The type of the tensor expression.

Member types

Member type

Description

expression_type

Type of the derived expression which is E.

type_category

Tag for categorization which is tensor_tag.

tensor_type

Reference type which is T.

Public Member Functions
Member Description

const expression_type &operator()() const

Returns a const reference to the derived expression.

Entrywise Tensor Operations
Binary Tensor Expression
Description

The templated class binary_tensor_expression<T,EL,ER,OP> contains a constant reference to a left and right expression that can be evaluated by using the access operator.

Definition

Defined in the header tensor/expression.hpp.

Model of

Tensor Expression

Type requirements

None.

Public base classes

tensor_expression<T,binary_tensor_expression<T,EL,ER,OP>>

Template parameters

Parameter

Description

T

Type of the tensor.

EL

Type of the left binary tensor expression.

ER

Type of the right binary tensor expression.

OP

Type of the binary operation.

Member types

Member type

Description

expression_type_left

Type of the left expression which is EL.

expression_type_right

Type of the right expression which is ER.

tensor_type

Reference type which is T.

binary_operation

Type of the binary operation which is OP.

Public Member Functions
Member Description

decltype(auto) operator()(std::size_t i) const

Returns a const reference to the i-th element of the expression.

Unary Tensor Expression
Description

The templated class unary_tensor_expression<T,E,OP> contains a constant reference to an expression that can be evaluated by using the access operator.

Definition

Defined in the header tensor/expression.hpp.

Model of

Tensor Expression

Type requirements

None.

Public base classes

tensor_expression<T,unary_tensor_expression<T,E,OP>>

Template parameters

Parameter

Description

T

Type of the tensor.

E

Type of the unary tensor expression.

OP

Type of the unary operation.

Member types

Member type

Description

expression_type

Type of the expression which is E.

tensor_type

Reference type which is T.

unary_operation

Type of the unary operation which is OP.

Public Member Functions
Member Description

decltype(auto) operator()(std::size_t i) const

Returns a const reference to the i-th element of the expression.

Miscellaneous

Unbounded Array Storage

Description

The templated class unbounded_array<T, ALLOC> implements a unbounded storage array using an allocator. The unbounded array is similar to a std::vector in that in can grow in size beyond any fixed bound. However unbounded_array is aimed at optimal performance. Therefore unbounded_array does not model a Sequence like std::vector does.

When resized unbounded_array will reallocate it’s storage even if the new size requirement is smaller. It is therefore inefficient to resize a unbounded_array

Example
#include <boost/numeric/ublas/storage.hpp>

int main () {
    using namespace boost::numeric::ublas;
    unbounded_array<double> a (3);
    for (unsigned i = 0; i < a.size (); ++ i) {
        a [i] = i;
        std::cout << a [i] << std::endl;
    }
}
Definition

Defined in the header storage.hpp.

Template parameters

Parameter

Description

Default

T

The type of object stored in the array.

ALLOC

An STL Allocator

std::allocator

Model of
Type requirements

None, except for those imposed by the requirements of Storage.

Public base classes

None.

Members
  • The description does not describe what the member actually does, this can be looked up in the corresponding concept documentation, but instead contains a remark on the implementation of the member inside this model of the concept.

  • Typography:

    • Members that are not part of the implemented concepts are in blue.

Member Where defined Description

value_type

Container

pointer

Container

Defined as value_type*

const_pointer

Container

Defined as const value_type*

reference

Container

Defined as value_type&

const_reference

Container

Defined as const value_type&

size_type

Container

Defined as Alloc::size_type

difference_type

Container

Defined as Alloc::difference_type

iterator

Container

Defined as pointer

const_iterator

Container

Defined as const_pointer

revere_iterator

Container

Defined as std::reverse_iterator<iterator>

const_revere_iterator

Container

Defined as std::reverse_iterator<const_iterator>

allocator_type

Defined as ALLOC

explicit unbounded_array (ALLOC &a = ALLOC())

Storage

Creates an unbounded_array that holds zero elements, using a specified allocator.

explicit unbounded_array (size_type size, ALLOC &a = ALLOC())

Storage

Creates a uninitialized unbounded_array that holds size elements, using a specified allocator. All the elements are default constructed.

unbounded_array (size_type size, const T& init, ALLOC& a = ALLOC())

Storage

Creates an initialized unbounded_array that holds size elements,using a specified allocator. All the elements are constructed from the init value.

unbounded_array (const unbounded_array &a)

Container

The copy constructor.

~unbounded_array ()

Container

Deallocates the unbounded_array itself.

void resize (size_type n)

Storage

Reallocates an unbounded_array to hold n elements. Values are uninitialised.

void resize(size_type n, const T& t)

Storage

Reallocates an unbounded_array to hold n elements. Values are copies of t

size_type size () const

Container

Returns the size of the unbounded_array.

const_reference operator [] (size_type i) const

Container

Returns a const reference of the i -th element.

reference operator [] (size_type i)

Container

Returns a reference of the i-th element.

unbounded_array &operator = (const unbounded_array &a)

Container

The assignment operator.

unbounded_array &assign_temporary (unbounded_array &a)

Assigns a temporary. May change the array a.

void swap (unbounded_array &a)

Container

Swaps the contents of the arrays.

const_iterator begin () const

Container

Returns a const_iterator pointing to the beginning of the unbounded_array.

const_iterator end () const

Container

Returns a const_iterator pointing to the end of the unbounded_array.

iterator begin ()

Container

Returns a iterator pointing to the beginning of the unbounded_array.

iterator end ()

Container

Returns a iterator pointing to the end of the unbounded_array.

const_reverse_iterator rbegin () const

ReversibleContainer

Returns a const_reverse_iterator pointing to the beginning of the reversed unbounded_array.

const_reverse_iterator rend () const

ReversibleContainer

Returns a const_reverse_iterator pointing to the end of the reversed unbounded_array.

reverse_iterator rbegin ()

ReversibleContainer

Returns a reverse_iterator pointing to the beginning of the reversed unbounded_array.

reverse_iterator rend ()

ReversibleContainer

Returns a reverse_iterator pointing to the end of the reversed unbounded_array.

Bounded Array Storage

Description

The templated class bounded_array<T, N, ALLOC> implements a bounded storage array. The bounded array is similar to a C++ array type in that its maximum size is bounded by N and is allocated on the stack instead of the heap. Similarly a bounded_array requires no secondary storage and ALLOC is only used to specify size_type and difference_type.

When resized bounded_array never reallocated the storage. It is therefore always efficient to resize a bounded_array but the size bound N must not be exceeded.

Example
#include <boost/numeric/ublas/storage.hpp>

int main () {
    using namespace boost::numeric::ublas;
    bounded_array<double, 3> a (3);
    for (unsigned i = 0; i < a.size (); ++ i) {
        a [i] = i;
        std::cout << a [i] << std::endl;
    }
}
Definition

Defined in the header storage.hpp.

Template parameters

Parameter

Description

Default

T

The type of object stored in the array.

N

The allocation size of the array.

ALLOC

An STL Allocator

std::allocator

Model of
Type requirements

None, except for those imposed by the requirements of Storage.

Public base classes

None.

Members
  • The description does not describe what the member actually does, this can be looked up in the corresponding concept documentation, but instead contains a remark on the implementation of the member inside this model of the concept.

  • Typography:

    • Members that are not part of the implemented concepts are in blue.

Member Where defined Description

value_type

Container

pointer

Container

Defined as value_type*

const_pointer

Container

Defined as const value_type*

reference

Container

Defined as value_type&

const_reference

Container

Defined as const value_type&

size_type

Container

Defined as Alloc::size_type

difference_type

Container

Defined as Alloc::difference_type

iterator

Container

Defined as pointer

const_iterator

Container

Defined as const_pointer

revere_iterator

Container

Defined as std::reverse_iterator<iterator>

const_revere_iterator

Container

Defined as std::reverse_iterator<const_iterator>

bounded_array ()

Storage

Creates an unbounded_array that holds zero elements.

bounded_array (size_type size)

Storage

Creates a uninitialized bounded_array that holds size elements. All the elements are default constructed.

bounded_array (size_type size, const T& init)

Storage

Creates an initialized bounded_array that holds size elements. All the elements are constructed from the init value.

bounded_array (const bounded_array &c)

Container

The copy constructor.

~bounded_array ()

Container

Deallocates the bounded_array itself.

void resize (size_type size)

Storage

Reallocates a bounded_array to hold size elements.

void resize (size_type size, const T& t)

Storage

Reallocates a bounded_array to hold size elements.

size_type size () const

Container

Returns the size of the bounded_array.

const_reference operator [] (size_type i) const

Container

Returns a const reference of the i -th element.

reference operator [] (size_type i)

Container

Returns a reference of the i-th element.

bounded_array &operator = (const bounded_array &a)

Container

The assignment operator.

bounded_array &assign_temporary (bounded_array &a)

Assigns a temporary. May change the array a.

void swap (bounded_array &a)

Container

Swaps the contents of the arrays.

const_iterator begin () const

Container

Returns a const_iterator pointing to the beginning of the bounded_array.

const_iterator end () const

Container

Returns a const_iterator pointing to the end of the bounded_array.

iterator begin ()

Container

Returns a iterator pointing to the beginning of the bounded_array.

iterator end ()

Container

Returns a iterator pointing to the end of the bounded_array.

const_reverse_iterator rbegin () const

Reversible Container

Returns a const_reverse_iterator pointing to the beginning of the reversed bounded_array.

const_reverse_iterator rend () const

Reversible Container

Returns a const_reverse_iterator pointing to the end of the reversed bounded_array.

reverse_iterator rbegin ()

Reversible Container

Returns a reverse_iterator pointing to the beginning of the reversed bounded_array.

reverse_iterator rend ()

Reversible Container

Returns a reverse_iterator pointing to the end of the reversed bounded_array.

Overview of Tensor, Matrix- and Vector Types

Notation

T

is the data type. For general linear algebra operations this will be a real type e.g. double, …​

F

is the orientation type, either row_major or column_major for matrices and first_order or last_order for tensors

A, IA, TA

is an array storage type, e.g. std::vector, bounded_array, unbounded_array, …​

TRI

is a triangular functor: lower, unit_lower, strict_lower, upper, unit_upper, strict_upper

M, N, K

are unsigned integer sizes (std::size_t)

IB

is an index base (std::size_t)

VEC

is any vector type

MAT

is any matrix type

TEN

is any tensor type

[…​]

denote optional arguments - for more details look at the section "storage layout".

Vectors
Definition Description

vector<T [, A]>   v(size);

a dense vector of values of type T of variable size. A storage type A can be specified which defaults to unbounded_array. Elements are constructed by A, which need not initialise their value.

bounded_vector<T, N>   v;

a dense vector of values of type T of variable size but with maximum N. The default constructor creates v with size N. Elements are constructed by the storage type bounded_array, which need not initialise their value.

c_vector<T, M>   v(size);

a dense vector of values of type T with the given size. The data is stored as an ordinary C++ array T data_[M]

zero_vector<T>   v(size);

the zero vector of type T with the given size.

unit_vector<T>   v(size, index);

the unit vector of type T with the given size. The vector is zero other then a single specified element.
index should be less than size.

mapped_vector<T [, S]>   v(size);

a sparse vector of values of type T of variable size. The sparse storage type S can be std::map<size_t, T> or map_array<size_t, T>.

compressed_vector<T [,IB, IA, TA]>   v(size);

a sparse vector of values of type T of variable size. The non zero values are stored as two seperate arrays - an index array and a value array. The index array is always sorted and there is at most one entry for each index.

coordinate_vector<T [,IB, IA, TA]>   v(size);

a sparse vector of values of type T of variable size. The non zero values are stored as two seperate arrays - an index array and a value array. The arrays may be out of order with multiple entries for each vector element. If there are multiple values for the same index the sum of these values is the real value.

Note: the default types are defined in boost/numeric/ublas/fwd.hpp.

Vector Proxies
Definition Description

vector_range<VEC>   vr(v, range);

a vector referencing a continuous subvector of elements of vector v containing all elements specified by range.

vector_slice<VEC>   vs(v, slice);

a vector referencing a non continuous subvector of elements of vector v containing all elements specified by slice.

matrix_row<MAT>   vr(m, index);

a vector referencing the index-th row of matrix m

matrix_column<MAT>   vc(m, index);

a vector referencing the index-th column of matrix m

Matrices
Definition Description

matrix<T [, F, A]>   m(size1, size2);

a dense matrix of values of type T of variable size. A storage type A can be specified which defaults to unbounded_array. The orientation functor F defaults to row_major. Elements are constructed by A, which need not initialise their value.

bounded_matrix<T, M, N [, F]>   m;

a dense matrix of type T with variable size with maximum M-by-N. The orientation functor F defaults to row_major. The default constructor creates m with size M-by-N. Elements are constructed by the storage type bounded_array, which need not initialise their value.

c_matrix<T, M, N>   m(size1, size2);

a dense matrix of values of type T with the given size. The data is stored as an ordinary C++ array T data_[N][M]

vector_of_vector<T [, F, A]>   m(size1, size2);

a dense matrix of values of type T with the given size. The data is stored as a vector of vectors. The orientation F defaults to row_major. The storage type S defaults to unbounded_array<unbounded_array<T> >

zero_matrix<T>   m(size1, size2);

a zero matrix of type T with the given size.

identity_matrix<T>   m(size1, size2);

an identity matrix of type T with the given size. The values are v(i,j) = (i==j)?T(1):T().

scalar_matrix<T>   m(size1, size2, value);

a matrix of type T with the given size that has the value value everywhere.

triangular_matrix<T [, TRI, F, A]>   m(size);

a triangular matrix of values of type T of variable size. Only the nonzero elements are stored in the given order F. ("triangular packed storage") The triangular type F defaults to lower, the orientation type F defaults to row_major.

banded_matrix<T [, F, A]>   m(size1, size2, n_lower, n_upper);

a banded matrix of values of type T of variable size with n_lower sub diagonals and n_upper super diagonals. Only the nonzero elements are stored in the given order F. ("packed storage")

symmetric_matrix<T [, TRI, F, A]>   m(size);

a symmetric matrix of values of type T of variable size. Only the given triangular matrix is stored in the given order F.

hermitian_matrix<T [, TRI, F, A]>   m(size);

a hermitian matrix of values of type T of variable size. Only the given triangular matrix is stored using the order F.

mapped_matrix<T, [F, S]>   m(size1, size2 [, non_zeros]);

a sparse matrix of values of type T of variable size. The sparse storage type S can be either std::map<size_t, std::map<size_t, T> > or map_array<size_t, map_array<size_t, T> >.

sparse_vector_of_sparse_vector<T, [F, C]>   m(size1, size2 [, non_zeros]);

a sparse matrix of values of type T of variable size.

compressed_matrix<T, [F, IB, IA, TA]>   m(size1, size2 [, non_zeros]);

a sparse matrix of values of type T of variable size. The values are stored in compressed row/column storage.

coordinate_matrix<T, [F, IB, IA, TA]>   m(size1, size2 [, non_zeros]);

a sparse matrix of values of type T of variable size. The values are stored in 3 parallel array as triples (i, j, value). More than one value for each pair of indices is possible, the real value is the sum of all.

generalized_vector_of_vector<T, F, A>   m(size1, size2 [, non_zeros]);

a sparse matrix of values of type T of variable size. The values are stored as a vector of sparse vectors, e.g. generalized_vector_of_vector<double, row_major, unbounded_array<coordinate_vector<double> > >

Note: the default types are defined in boost/numeric/ublas/fwd.hpp.

Matrix Proxies
Definition Description

triangular_adaptor<MAT, TRI>   ta(m);

a triangular matrix referencing a selection of elements of the matrix m.

symmetric_adaptor<MAT, TRI>   sa(m);

a symmetric matrix referencing a selection of elements of the matrix m.

hermitian_adaptor<MAT, TRI>   ha(m);

a hermitian matrix referencing a selection of elements of the matrix m.

banded_adaptor<MAT>   ba(m, n_lower, n_upper);

a banded matrix referencing a selection of elements of the matrix m.

matrix_range<MAT, TRI>   mr(m, range1, range2);

a matrix referencing a submatrix of elements in the matrix m.

matrix_slice<MAT, TRI>   ms(m, slice1, slice2);

a matrix referencing a non continues submatrix of elements in the matrix m.

Tensors
Definition Description

tensor<T [, F, A]>   t(size1, size2, …​ );

a dense matrix of values of type T of variable size. A storage type A can be specified which defaults to std::vector<T>. The orientation type F defaults to first_order. Elements are constructed by A, which need not initialise their value.

Special Storage Layouts

The library supports conventional dense, packed and basic sparse vector and matrix storage layouts. The description of the most common constructions of vectors and matrices comes next.

Construction Comment

vector<T,  std::vector<T> >   v (size)

a dense vector, storage is provided by a standard vector.
The storage layout usually is BLAS compliant.

vector<T,  unbounded_array<T> >   v (size)

a dense vector, storage is provided by a heap-based array.
The storage layout usually is BLAS compliant.

vector<T,  bounded_array<T, N> >   v (size)

a dense vector, storage is provided by a stack-based array.
The storage layout usually is BLAS compliant.

mapped_vector<T,  std::map<std::size_t, T> >   v (size, non_zeros)

a sparse vector, storage is provided by a standard map.

mapped_vector<T,  map_array<std::size_t, T> >   v (size, non_zeros)

a sparse vector, storage is provided by a map array.

matrix<T,  row_major,  std::vector<T> >   m (size1, size2)

a dense matrix, orientation is row major, storage is provided by a standard vector.

matrix<T,  column_major,  std::vector<T> >   m (size1, size2)

a dense matrix, orientation is column major, storage is provided by a standard vector.
The storage layout usually is BLAS compliant.

matrix<T,  row_major,  unbounded_array<T> >   m (size1, size2)

a dense matrix, orientation is row major, storage is provided by a heap-based array.

matrix<T,  column_major,  unbounded_array<T> >   m (size1, size2)

a dense matrix, orientation is column major, storage is provided by a heap-based array.
The storage layout usually is BLAS compliant.

matrix<T,  row_major,  bounded_array<T, N1 * N2> >   m (size1, size2)

a dense matrix, orientation is row major, storage is provided by a stack-based array.

matrix<T,  column_major,  bounded_array<T, N1 * N2> >   m (size1, size2)

a dense matrix, orientation is column major, storage is provided by a stack-based array.
The storage layout usually is BLAS compliant.

triangular_matrix<T,  row_major, F, A>   m (size)

a packed triangular matrix, orientation is row major.

triangular_matrix<T,  column_major, F, A>   m (size)

a packed triangular matrix, orientation is column major.
The storage layout usually is BLAS compliant.

banded_matrix<T,  row_major, A>   m (size1, size2, lower, upper)

a packed banded matrix, orientation is row major.

banded_matrix<T,  column_major, A>   m (size1, size2, lower, upper)

a packed banded matrix, orientation is column major.
The storage layout usually is BLAS compliant.

symmetric_matrix<T,  row_major, F, A>   m (size)

a packed symmetric matrix, orientation is row major.

symmetric_matrix<T,  column_major, F, A>   m (size)

a packed symmetric matrix, orientation is column major.
The storage layout usually is BLAS compliant.

hermitian_matrix<T,  row_major, F, A>   m (size)

a packed hermitian matrix, orientation is row major.

hermitian_matrix<T,  column_major, F, A>   m (size)

a packed hermitian matrix, orientation is column major.
The storage layout usually is BLAS compliant.

mapped_matrix<T,  row_major,  std::map<std::size_t, T> >   m (size1, size2, non_zeros)

a sparse matrix, orientation is row major, storage is provided by a standard map.

mapped_matrix<T,  column_major,  std::map<std::size_t, T> >   m (size1, size2, non_zeros)

a sparse matrix, orientation is column major, storage is provided by a standard map.

mapped_matrix<T,  row_major,  map_array<std::size_t, T> >   m (size1, size2, non_zeros)

a sparse matrix, orientation is row major, storage is provided by a map array.

mapped_matrix<T,  column_major,  map_array<std::size_t, T> >   m (size1, size2, non_zeros)

a sparse matrix, orientation is column major, storage is provided by a map array.

compressed_matrix<T,  row_major>   m (size1, size2, non_zeros)

a compressed matrix, orientation is row major.
The storage layout usually is BLAS compliant.

compressed_matrix<T,  column_major>   m (size1, size2, non_zeros)

a compressed matrix, orientation is column major.
The storage layout usually is BLAS compliant.

coordinate_matrix<T,  row_major>   m (size1, size2, non_zeros)

a coordinate matrix, orientation is row major.
The storage layout usually is BLAS compliant.

coordinate_matrix<T,  column_major>   m (size1, size2, non_zeros)

a coordinate matrix, orientation is column major.
The storage layout usually is BLAS compliant.

Range and Slice Storage

Range<SizeType,DistanceType>
Description

The class range specifies a range of indicies. The range is a sequence of indices from a start value to stop value. The indices increase by one and exlude the stop value. range can therefore be used to specify ranges of elements from vectors and matrices.

Example
#include <boost/numeric/ublas/storage.hpp>

int main () {
    using namespace boost::numeric::ublas;
    range r (0, 3);
    for (unsigned i = 0; i < r.size (); ++ i) {
        std::cout << r (i) << std::endl;
    }
}
Definition

Defined in the header storage.hpp.

Model of

Reversible Container.

Type requirements

None, except for those imposed by the requirements of Reversible Container.

Public base classes

None.

Members
Member Description

range (size_type start, size_type stop)

Constructs a range of indicies from start to stop (excluded) .

size_type start () const

Returns the beginning of the range.

size_type size () const

Returns the size of the range.

const_reference operator [] (size_type i) const

Returns the value start + i of the i -th element.

range compose (const range &r) const

Returns the composite range from start + r.start () to start + r.start () + r.size ().

bool operator == (const range &r) const

Tests two ranges for equality.

bool operator != (const range &r) const

Tests two ranges for inequality.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the range.

const_iterator end () const

Returns a const_iterator pointing to the end of the range.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed range.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed range.

Preconditions
  • start () < = stop ()

Slice<SizeType,DistanceType>
Description

The class slice specifies a 'slice' of indicies. Slices are more general then ranges, the stride allows the sequence of indicies to increase and decrease by the specified amount between element. slice can therefore be used to specify slices of element from vectors and matrices.

Example
#include <boost/numeric/ublas/storage.hpp>

int main () {
    using namespace boost::numeric::ublas;
    slice s (0, 1, 3);
    for (unsigned i = 0; i < s.size (); ++ i) {
        std::cout << s (i) << std::endl;
    }
}
Definition

Defined in the header storage.hpp.

Model of

Reversible Container.

Type requirements

None, except for those imposed by the requirements of Reversible Container.

Public base classes

None.

Members
Member Description

slice (size_type start, size_type stride, size_type size)

Constructs a slice start,start+stride,start+2*stride…​ with size elements.

size_type start () const

Returns the beginning of the slice.

size_type stride () const

Returns the stride of the slice.

size_type size () const

Returns the size of the slice.

const_reference operator [] (size_type i) const

Returns the value start + i * stride of the i-th element.

slice compose (const range &r) const

Returns the composite slice from start + stride * r.start () to start + stride * (r.start () + r.size ()) with stride stride.

slice compose (const slice &s) const

Returns the composite slice from start + stride * s.start () to start + stride * s.stride () * (s.start () + s.size ()) with stride stride * s.stride () .

bool operator == (const slice &s) const

Tests two slices for equality.

bool operator != (const slice &s) const

Tests two slices for inequality.

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the slice.

const_iterator end () const

Returns a const_iterator pointing to the end of the slice.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed slice.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed slice.

Preconditions
  • None all strides are vaild. However when an index is returned or an iterator is dereferenced its value must be representable as the size_type.

Boost Basic Linear Algebra - Configuration Options

NDEBUG

Make sure you define NDEBUG The only way uBLAS knows you want a release configuration is to check if you have defined NDEBUG. If you don’t it assumes you want a debug configuration and adds a lot of very useful runtime check. However these are very slow!

BOOST_UBLAS_MOVE_SEMANTICS

The patch and description was provided by Nasos Iliopoulos.

An immediate effect of this option is the elimination of the need for noalias in types vector<T> and matrix<T>, when assigned to the same type. This option doesn’t have an effect on bounded and c types. Although it is rare, not all compilers support copy elision (that allows for move semantics), so a test must be performed to make sure that there is a benefit when it is enabled. A small demonstration and test can be found in test_move_semantics.cpp

In the test example two tests are defined, one for vectors and one for matrices. The aim of this example is to print the pointers of the storage of each of the containers, before and after the assignment to a temporary object. When move semantics are enabled, the vector<T> and matrix<T> storage is moved from the temporary and no copy is performed.

If move semantics are supported by your compiler you will get an output like the following:

matrix<double> --------------------------------------------------------------------
Temporary pointer r: 0x94790c0
Pointer (must be equal to temp. pointer if move semantics are enabled) : 0x94790c0

Notes:

  • It should be no surprise to see matrices and vectors been passed by VALUE, the compiler takes care and either moves (if the underlying code does not modify the object), or copies (if the underlying code modifies the object).

  • There might be some space for some improvements (like clearing the data, before swaping)

  • Move semantics don’t eliminate temporaries. They rather move their storage around so no copies are performed.

  • MSVC does no implement Named Return Value Optimization in debug mode. So if you build in debug with this compiler you might get different behaviour than a release build.

  • Enabling move semantics is done via #define BOOST_UBLAS_MOVE_SEMANTICS.

  • There is plenty of room for optimizations when c++0x standard is out, taking advantage of rvalue references. (I have a sweet vector implementation using that).

  • If you enable move semantics and your compiler does not support them, the operation will just be as passing by const reference.

Interesting links

BOOST_UBLAS_CHECK_ENABLE

When BOOST_UBLAS_CHECK_ENABLE is defined then all index and parameter checks are enabled. This is enabled in debug mode and disabled in release mode.

BOOST_UBLAS_TYPE_CHECK

When BOOST_UBLAS_TYPE_CHECK is enabled then all possibly expensive structure checks are enabled. If this is not desireable then use #define BOOST_UBLAS_TYPE_CHECK 0 before including any uBLAS header. The define BOOST_UBLAS_TYPE_CHECK_EPSILON can be used to control the acceptable tolerance, see detail/matrix_assign.hpp for implementation details of this check.

BOOST_UBLAS_USE_LONG_DOUBLE

Enable uBLAS expressions that involve containers of 'long double'

BOOST_UBLAS_USE_INTERVAL

Enable uBLAS expressions that involve containers of 'boost::numeric::interval' types

Configuring uBLAS with Macros

Many macro’s appear in ublas/config.hpp and elsewhere. Hopefully in the future some of these will disappear! They fall into 4 groups:

  • Automatically set by 'boost/numeric/ublas/config.hpp' based on NDEBUG. Makes the distinction between debug (safe) and release (fast) mode. Similar to STLport

    • Release mode (NDEBUG defined)

      • BOOST_UBLAS_INLINE Compiler dependant definition to control function inlining.

      • BOOST_UBLAS_USE_FAST_SAME

    • Debug mode

      • BOOST_UBLAS_CHECK_ENABLE Enable checking of indexs, iterators and parameters. Prevents out of bound access etc.

      • BOOST_UBLAS_TYPE_CHECK Enable additional checks for the results of expressions using non dense types. Picks up runtime error such as the assignment of a numerically non-symmetric matrix to symmertic_matrix. Use #define BOOST_UBLAS_TYPE_CHECK 0 to disable expensive numeric type checks. (Note: "structure check" would be a much better name.)

      • BOOST_UBLAS_TYPE_CHECK_EPSILON default: sqrt(epsilon), controls how large the difference between the expected result and the computed result may become. Increase this value if you are going to use near singular or badly scaled matrices. Please, refer to detail/matrix_assign.hpp for implementation of these type checks.

  • Automatically set by 'boost/numeric/ublas/config.hpp' based on compiler and boost/config.hpp macro’s. Augments the compiler deficiency workarounds already supplied by boost/config.hpp

    • BOOST_UBLAS_NO_NESTED_CLASS_RELATION A particularly nasty problem with VC7.1 Requires that uBLAS and the user use begin(it) rather then it.begin()

    • BOOST_UBLAS_NO_SMART_PROXIES Disable the automatic propagation of 'constantness' to proxies. Smart proxies automatically determine if the underling container they reference is constant or not. They adjust there definition of iterators and container access to reflect this constantness.

  • For use by uBLAS authors to test implementation methods. Preset in config.hpp

    • BOOST_UBLAS_USE_INVARIANT_HOISTING

    • BOOST_UBLAS_USE_INDEXING

    • BOOST_UBLAS_USE_INDEXED_ITERATOR

    • BOOST_UBLAS_NON_CONFORMANT_PROXIES Gappy containers may be non-conformant, that is contain elements at different indices. Assigning between proxies (vector ranges for example) of these containers is difficult as the LHS may need insert new elements. This is slow.

    • BOOST_UBLAS_USE_DUFF_DEVICE Near useless on all platforms (see GCC’s -funroll-loops)

  • User options. Can be predefined by user before including any uBLAS headers. They may also be automatically defined for some compilers to work around compile bugs.

    • BOOST_UBLAS_USE_LONG_DOUBLE Enable uBLAS expressions that involve containers of 'long double'

    • BOOST_UBLAS_USE_INTERVAL Enable uBLAS expressions that involve containers of 'boost::numeric::interval' types

    • BOOST_UBLAS_SIMPLE_ET_DEBUG In order to simplify debugging is is possible to simplify expression templateso they are restricted to a single operation

    • BOOST_UBLAS_ENABLE_PROXY_SHORTCUTS enable automatic conversion from proxy class to matrix expression

    • BOOST_UBLAS_NO_ELEMENT_PROXIES Disables the use of element proxies for gappy types.

    • The Gappy types (sparse, coordinate, compressed) store non-zero elements in their own containers. When new non-zero elements are assigned they must rearrange these containers. This invalidates references, iterators or pointers to these elements. This can happen at some surprising times such as the expression "a [1] = a [0] = 1;". Element proxies guarantee all such expressions will work as expected. However they bring their own restrictions and efficiency problems. For example as of Boost 1.30.0 they prevent the assignment of elements between different types.

    • BOOST_UBLAS_REFERENCE_CONST_MEMBER Enable to allow refernces to be returned to fixed (zero or one) elements of triangular or banded matrices

    • BOOST_UBLAS_NO_EXCEPTIONS Disable the use exceptions of uBLAS internal checks and error conditions. BOOST_NO_EXCEPTIONS has same effect.

    • BOOST_UBLAS_SINGULAR_CHECK Check the for singularity in triangular solve() functions

Last modified: Wed Sep 16 23:16:45 CEST 2009

Sparse Storage

Default Standard Map
Description

The templated class map_std<I, T, ALLOC> provides a wrapper for the standard library associative container std::map. The wrapper has one simple purpose. It allows the definition of a default template parameter (for the adapted array) when declaring the sparse container types.

Example
#include <boost/numeric/ublas/storage_sparse.hpp>

int main () {
    using namespace boost::numeric::ublas;
    map_std<int, double> a (3);
    for (unsigned i = 0; i < a.size (); ++ i) {
        a [i] = i;
        std::cout << a [i] << std::endl;
    }
}
Definition

Defined in the header storage_sparse.hpp.

Template parameters

Parameter

Description

Default

I

The type of index stored in the array.

T

The type of object stored in the array.

ALLOC

An STL Allocator

std::allocator

Model of

Reversible Container.

Type requirements

None, except for those imposed by the requirements of Reversible Container.

Public base classes

std::map

Map Array
Description

The templated class map_array<I, T, ALLOC> implements a std::map like associative container as a sorted array. It therefore some of the Associative Container interface without having the same semantics as an std::map.

At any time the map_array has a capacity up to which new element can be inserted. If insert would cause the size of the map_array to exceeds its capactity then it is reallocated. Iterators and reference are invalidated. The capacity can be directly control using the reserve member function.

Example
#include <boost/numeric/ublas/storage_sparse.hpp>

int main () {
    using namespace boost::numeric::ublas;
    map_array<int, double> a (3);
    for (unsigned i = 0; i < a.size (); ++ i) {
        a [i] = i;
        std::cout << a [i] << std::endl;
    }
}
Definition

Defined in the header storage_sparse.hpp.

Template parameters

Parameter

Description

Default

I

The type of index stored in the array.

T

The type of object stored in the array.

ALLOC

An STL Allocator

std::allocator

Model of

Reversible Container.

Type requirements

None, except for those imposed by the requirements of Reversible Container.

Public base classes

None.

Members
Member Description

map_array (ALLOC &a = ALLOC())

Allocates a map_array that holds at most zero elements.

map_array (const map_array &c)

The copy constructor.

~map_array ()

Deallocates the map_array itself.

void reserve (size_type capacity)

Changes the`map_array` capacity. It can hold at most`capacity` elements without reallocation. The capacity can be reduced such that capacity >= size(). The content of the`map_array` is preserved.

size_type size () const

Returns the size of the map_array.

size_type size () const

Returns the capacity of the map_array.

data_reference operator [] (index_type i)

Returns a reference of the element that is associated with a particular index. If the map_array does not already contain such an element, operator[] inserts the default T ().

map_array &operator = (const map_array &a)

The assignment operator.

map_array &assign_temporary (map_array &a)

Assigns a temporary. May change the array a.

void swap (map_array &a)

Swaps the contents of the arrays.

std::pair insert (const value_type &p)

Inserts p into the array. The second part of the return value is true if p was inserted and false if was not inserted because it was aleady present.

iterator insert (iterator it, const value_type &p)

Inserts p into the array, using it as a hint to where it will be inserted.

void erase (iterator it)

Erases the value at it.

void clear ()

Clears the array.

const_iterator find (index_type i) const

Finds an element whose index is i.

iterator find (index_type i)

Finds an element whose index is i.

const_iterator lower_bound (index_type i) const

Finds the first element whose index is not less than i .

iterator lower_bound (index_type i)

Finds the first element whose index is not less than i .

const_iterator upper_bound (index_type i) const

Finds the first element whose index is greater than i .

iterator upper_bound (index_type i)

Finds the first element whose index is greater than i .

const_iterator begin () const

Returns a const_iterator pointing to the beginning of the map_array.

const_iterator end () const

Returns a const_iterator pointing to the end of the map_array.

iterator begin ()

Returns a iterator pointing to the beginning of the map_array.

iterator end ()

Returns a iterator pointing to the end of the map_array.

const_reverse_iterator rbegin () const

Returns a const_reverse_iterator pointing to the beginning of the reversed map_array.

const_reverse_iterator rend () const

Returns a const_reverse_iterator pointing to the end of the reversed map_array.

reverse_iterator rbegin ()

Returns a reverse_iterator pointing to the beginning of the reversed map_array.

reverse_iterator rend ()

Returns a reverse_iterator pointing to the end of the reversed map_array.

Range and Slice Storage

Range<SizeType,DistanceType>
Description

The class range specifies a range of indicies. The range is a sequence of indices from a start value to stop value. The indices increase by one and exlude the stop value. range can therefore be used to specify ranges of elements from vectors and matrices.

Example
#include <boost/numeric/ublas/storage.hpp>

int main () {
    using namespace boost::numeric::ublas;
    range r (0, 3);
    for (unsigned i = 0; i < r.size (); ++ i) {
        std::cout << r (i) << std::endl;
    }
}
Definition

Defined in the header storage.hpp.

Model of

Reversible Container.

Type requirements

None, except for those imposed by the requirements of Reversible Container.

Public base classes

None.

Members
Member Description

range (size_type start, size_type stop)

Constructs a range of indicies from start to stop (excluded) .

size_type start () const

Returns the beginning of the range.

size_type size () const

Returns the size of the range.

const_reference operator [] (size_type i) const

Returns the value start + i of the i -th element.