os/ossrv/ossrv_pub/boost_apis/boost/numeric/ublas/blas.hpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
//
sl@0
     2
//  Copyright (c) 2000-2002
sl@0
     3
//  Joerg Walter, Mathias Koch
sl@0
     4
//
sl@0
     5
//  Permission to use, copy, modify, distribute and sell this software
sl@0
     6
//  and its documentation for any purpose is hereby granted without fee,
sl@0
     7
//  provided that the above copyright notice appear in all copies and
sl@0
     8
//  that both that copyright notice and this permission notice appear
sl@0
     9
//  in supporting documentation.  The authors make no representations
sl@0
    10
//  about the suitability of this software for any purpose.
sl@0
    11
//  It is provided "as is" without express or implied warranty.
sl@0
    12
//
sl@0
    13
//  The authors gratefully acknowledge the support of
sl@0
    14
//  GeNeSys mbH & Co. KG in producing this work.
sl@0
    15
//
sl@0
    16
sl@0
    17
#ifndef _BOOST_UBLAS_BLAS_
sl@0
    18
#define _BOOST_UBLAS_BLAS_
sl@0
    19
sl@0
    20
#include <boost/numeric/ublas/traits.hpp>
sl@0
    21
sl@0
    22
namespace boost { namespace numeric { namespace ublas {
sl@0
    23
sl@0
    24
    namespace blas_1 {
sl@0
    25
sl@0
    26
          /** \namespace boost::numeric::ublas::blas_1
sl@0
    27
                  \brief wrapper functions for level 1 blas
sl@0
    28
          */
sl@0
    29
sl@0
    30
sl@0
    31
          /** \brief 1-Norm: \f$\sum_i |x_i|\f$
sl@0
    32
                  \ingroup blas1
sl@0
    33
           */
sl@0
    34
        template<class V>
sl@0
    35
        typename type_traits<typename V::value_type>::real_type
sl@0
    36
        asum (const V &v) {
sl@0
    37
            return norm_1 (v);
sl@0
    38
        }
sl@0
    39
          /** \brief 2-Norm: \f$\sum_i |x_i|^2\f$
sl@0
    40
                  \ingroup blas1
sl@0
    41
           */
sl@0
    42
        template<class V>
sl@0
    43
        typename type_traits<typename V::value_type>::real_type
sl@0
    44
        nrm2 (const V &v) {
sl@0
    45
            return norm_2 (v);
sl@0
    46
        }
sl@0
    47
          /** \brief element with larges absolute value: \f$\max_i |x_i|\f$
sl@0
    48
                  \ingroup blas1
sl@0
    49
          */                 
sl@0
    50
        template<class V>
sl@0
    51
        typename type_traits<typename V::value_type>::real_type
sl@0
    52
        amax (const V &v) {
sl@0
    53
            return norm_inf (v);
sl@0
    54
        }
sl@0
    55
sl@0
    56
          /** \brief inner product of vectors \a v1 and \a v2
sl@0
    57
                  \ingroup blas1
sl@0
    58
          */                 
sl@0
    59
        template<class V1, class V2>
sl@0
    60
        typename promote_traits<typename V1::value_type, typename V2::value_type>::promote_type
sl@0
    61
        dot (const V1 &v1, const V2 &v2) {
sl@0
    62
            return inner_prod (v1, v2);
sl@0
    63
        }
sl@0
    64
sl@0
    65
          /** \brief copy vector \a v2 to \a v1
sl@0
    66
                  \ingroup blas1
sl@0
    67
          */                 
sl@0
    68
        template<class V1, class V2>
sl@0
    69
        V1 &
sl@0
    70
        copy (V1 &v1, const V2 &v2) {
sl@0
    71
            return v1.assign (v2);
sl@0
    72
        }
sl@0
    73
sl@0
    74
          /** \brief swap vectors \a v1 and \a v2
sl@0
    75
                  \ingroup blas1
sl@0
    76
          */                 
sl@0
    77
        template<class V1, class V2>
sl@0
    78
        void swap (V1 &v1, V2 &v2) {
sl@0
    79
            v1.swap (v2);
sl@0
    80
        }
sl@0
    81
sl@0
    82
          /** \brief scale vector \a v with scalar \a t
sl@0
    83
                  \ingroup blas1
sl@0
    84
          */                 
sl@0
    85
        template<class V, class T>
sl@0
    86
        V &
sl@0
    87
        scal (V &v, const T &t) {
sl@0
    88
            return v *= t;
sl@0
    89
        }
sl@0
    90
sl@0
    91
          /** \brief compute \a v1 = \a v1 + \a t * \a v2
sl@0
    92
                  \ingroup blas1
sl@0
    93
          */                 
sl@0
    94
        template<class V1, class T, class V2>
sl@0
    95
        V1 &
sl@0
    96
        axpy (V1 &v1, const T &t, const V2 &v2) {
sl@0
    97
            return v1.plus_assign (t * v2);
sl@0
    98
        }
sl@0
    99
sl@0
   100
          /** \brief apply plane rotation
sl@0
   101
                  \ingroup blas1
sl@0
   102
          */                 
sl@0
   103
        template<class T1, class V1, class T2, class V2>
sl@0
   104
        void
sl@0
   105
        rot (const T1 &t1, V1 &v1, const T2 &t2, V2 &v2) {
sl@0
   106
            typedef typename promote_traits<typename V1::value_type, typename V2::value_type>::promote_type promote_type;
sl@0
   107
            vector<promote_type> vt (t1 * v1 + t2 * v2);
sl@0
   108
            v2.assign (- t2 * v1 + t1 * v2);
sl@0
   109
            v1.assign (vt);
sl@0
   110
        }
sl@0
   111
sl@0
   112
    }
sl@0
   113
sl@0
   114
    namespace blas_2 {
sl@0
   115
sl@0
   116
          /** \namespace boost::numeric::ublas::blas_2
sl@0
   117
                  \brief wrapper functions for level 2 blas
sl@0
   118
          */
sl@0
   119
sl@0
   120
          /** \brief multiply vector \a v with triangular matrix \a m
sl@0
   121
                  \ingroup blas2
sl@0
   122
                  \todo: check that matrix is really triangular
sl@0
   123
          */                 
sl@0
   124
        template<class V, class M>
sl@0
   125
        V &
sl@0
   126
        tmv (V &v, const M &m) {
sl@0
   127
            return v = prod (m, v);
sl@0
   128
        }
sl@0
   129
sl@0
   130
          /** \brief solve \a m \a x = \a v in place, \a m is triangular matrix
sl@0
   131
                  \ingroup blas2
sl@0
   132
          */                 
sl@0
   133
        template<class V, class M, class C>
sl@0
   134
        V &
sl@0
   135
        tsv (V &v, const M &m, C) {
sl@0
   136
            return v = solve (m, v, C ());
sl@0
   137
        }
sl@0
   138
sl@0
   139
          /** \brief compute \a v1 = \a t1 * \a v1 + \a t2 * (\a m * \a v2)
sl@0
   140
                  \ingroup blas2
sl@0
   141
          */                 
sl@0
   142
        template<class V1, class T1, class T2, class M, class V2>
sl@0
   143
        V1 &
sl@0
   144
        gmv (V1 &v1, const T1 &t1, const T2 &t2, const M &m, const V2 &v2) {
sl@0
   145
            return v1 = t1 * v1 + t2 * prod (m, v2);
sl@0
   146
        }
sl@0
   147
sl@0
   148
          /** \brief rank 1 update: \a m = \a m + \a t * (\a v1 * \a v2<sup>T</sup>)
sl@0
   149
                  \ingroup blas2
sl@0
   150
          */                 
sl@0
   151
        template<class M, class T, class V1, class V2>
sl@0
   152
        M &
sl@0
   153
        gr (M &m, const T &t, const V1 &v1, const V2 &v2) {
sl@0
   154
#ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
sl@0
   155
            return m += t * outer_prod (v1, v2);
sl@0
   156
#else
sl@0
   157
            return m = m + t * outer_prod (v1, v2);
sl@0
   158
#endif
sl@0
   159
        }
sl@0
   160
sl@0
   161
          /** \brief symmetric rank 1 update: \a m = \a m + \a t * (\a v * \a v<sup>T</sup>)
sl@0
   162
                  \ingroup blas2
sl@0
   163
          */                 
sl@0
   164
        template<class M, class T, class V>
sl@0
   165
        M &
sl@0
   166
        sr (M &m, const T &t, const V &v) {
sl@0
   167
#ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
sl@0
   168
            return m += t * outer_prod (v, v);
sl@0
   169
#else
sl@0
   170
            return m = m + t * outer_prod (v, v);
sl@0
   171
#endif
sl@0
   172
        }
sl@0
   173
          /** \brief hermitian rank 1 update: \a m = \a m + \a t * (\a v * \a v<sup>H</sup>)
sl@0
   174
                  \ingroup blas2
sl@0
   175
          */                 
sl@0
   176
        template<class M, class T, class V>
sl@0
   177
        M &
sl@0
   178
        hr (M &m, const T &t, const V &v) {
sl@0
   179
#ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
sl@0
   180
            return m += t * outer_prod (v, conj (v));
sl@0
   181
#else
sl@0
   182
            return m = m + t * outer_prod (v, conj (v));
sl@0
   183
#endif
sl@0
   184
        }
sl@0
   185
sl@0
   186
          /** \brief symmetric rank 2 update: \a m = \a m + \a t * 
sl@0
   187
                  (\a v1 * \a v2<sup>T</sup> + \a v2 * \a v1<sup>T</sup>) 
sl@0
   188
                  \ingroup blas2
sl@0
   189
          */                 
sl@0
   190
        template<class M, class T, class V1, class V2>
sl@0
   191
        M &
sl@0
   192
        sr2 (M &m, const T &t, const V1 &v1, const V2 &v2) {
sl@0
   193
#ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
sl@0
   194
            return m += t * (outer_prod (v1, v2) + outer_prod (v2, v1));
sl@0
   195
#else
sl@0
   196
            return m = m + t * (outer_prod (v1, v2) + outer_prod (v2, v1));
sl@0
   197
#endif
sl@0
   198
        }
sl@0
   199
          /** \brief hermitian rank 2 update: \a m = \a m + 
sl@0
   200
                  \a t * (\a v1 * \a v2<sup>H</sup>)
sl@0
   201
                  + \a v2 * (\a t * \a v1)<sup>H</sup>) 
sl@0
   202
                  \ingroup blas2
sl@0
   203
          */                 
sl@0
   204
        template<class M, class T, class V1, class V2>
sl@0
   205
        M &
sl@0
   206
        hr2 (M &m, const T &t, const V1 &v1, const V2 &v2) {
sl@0
   207
#ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
sl@0
   208
            return m += t * outer_prod (v1, conj (v2)) + type_traits<T>::conj (t) * outer_prod (v2, conj (v1));
sl@0
   209
#else
sl@0
   210
            return m = m + t * outer_prod (v1, conj (v2)) + type_traits<T>::conj (t) * outer_prod (v2, conj (v1));
sl@0
   211
#endif
sl@0
   212
        }
sl@0
   213
sl@0
   214
    }
sl@0
   215
sl@0
   216
    namespace blas_3 {
sl@0
   217
sl@0
   218
          /** \namespace boost::numeric::ublas::blas_3
sl@0
   219
                  \brief wrapper functions for level 3 blas
sl@0
   220
          */
sl@0
   221
sl@0
   222
          /** \brief triangular matrix multiplication
sl@0
   223
                  \ingroup blas3
sl@0
   224
          */                 
sl@0
   225
        template<class M1, class T, class M2, class M3>
sl@0
   226
        M1 &
sl@0
   227
        tmm (M1 &m1, const T &t, const M2 &m2, const M3 &m3) {
sl@0
   228
            return m1 = t * prod (m2, m3);
sl@0
   229
        }
sl@0
   230
sl@0
   231
          /** \brief triangular solve \a m2 * \a x = \a t * \a m1 in place,
sl@0
   232
                  \a m2 is a triangular matrix
sl@0
   233
                  \ingroup blas3
sl@0
   234
          */                 
sl@0
   235
        template<class M1, class T, class M2, class C>
sl@0
   236
        M1 &
sl@0
   237
        tsm (M1 &m1, const T &t, const M2 &m2, C) {
sl@0
   238
            return m1 = solve (m2, t * m1, C ());
sl@0
   239
        }
sl@0
   240
sl@0
   241
          /** \brief general matrix multiplication
sl@0
   242
                  \ingroup blas3
sl@0
   243
          */                 
sl@0
   244
        template<class M1, class T1, class T2, class M2, class M3>
sl@0
   245
        M1 &
sl@0
   246
        gmm (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3) {
sl@0
   247
            return m1 = t1 * m1 + t2 * prod (m2, m3);
sl@0
   248
        }
sl@0
   249
sl@0
   250
          /** \brief symmetric rank k update: \a m1 = \a t * \a m1 + 
sl@0
   251
                  \a t2 * (\a m2 * \a m2<sup>T</sup>)
sl@0
   252
                  \ingroup blas3
sl@0
   253
                  \todo use opb_prod()
sl@0
   254
          */                 
sl@0
   255
        template<class M1, class T1, class T2, class M2>
sl@0
   256
        M1 &
sl@0
   257
        srk (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2) {
sl@0
   258
            return m1 = t1 * m1 + t2 * prod (m2, trans (m2));
sl@0
   259
        }
sl@0
   260
          /** \brief hermitian rank k update: \a m1 = \a t * \a m1 + 
sl@0
   261
                  \a t2 * (\a m2 * \a m2<sup>H</sup>)
sl@0
   262
                  \ingroup blas3
sl@0
   263
                  \todo use opb_prod()
sl@0
   264
          */                 
sl@0
   265
        template<class M1, class T1, class T2, class M2>
sl@0
   266
        M1 &
sl@0
   267
        hrk (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2) {
sl@0
   268
            return m1 = t1 * m1 + t2 * prod (m2, herm (m2));
sl@0
   269
        }
sl@0
   270
sl@0
   271
          /** \brief generalized symmetric rank k update:
sl@0
   272
                  \a m1 = \a t1 * \a m1 + \a t2 * (\a m2 * \a m3<sup>T</sup>)
sl@0
   273
                  + \a t2 * (\a m3 * \a m2<sup>T</sup>)
sl@0
   274
                  \ingroup blas3
sl@0
   275
                  \todo use opb_prod()
sl@0
   276
          */                 
sl@0
   277
        template<class M1, class T1, class T2, class M2, class M3>
sl@0
   278
        M1 &
sl@0
   279
        sr2k (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3) {
sl@0
   280
            return m1 = t1 * m1 + t2 * (prod (m2, trans (m3)) + prod (m3, trans (m2)));
sl@0
   281
        }
sl@0
   282
          /** \brief generalized hermitian rank k update:
sl@0
   283
                  \a m1 = \a t1 * \a m1 + \a t2 * (\a m2 * \a m3<sup>H</sup>)
sl@0
   284
                  + (\a m3 * (\a t2 * \a m2)<sup>H</sup>)
sl@0
   285
                  \ingroup blas3
sl@0
   286
                  \todo use opb_prod()
sl@0
   287
          */                 
sl@0
   288
        template<class M1, class T1, class T2, class M2, class M3>
sl@0
   289
        M1 &
sl@0
   290
        hr2k (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3) {
sl@0
   291
            return m1 = t1 * m1 + t2 * prod (m2, herm (m3)) + type_traits<T2>::conj (t2) * prod (m3, herm (m2));
sl@0
   292
        }
sl@0
   293
sl@0
   294
    }
sl@0
   295
sl@0
   296
}}}
sl@0
   297
sl@0
   298
#endif
sl@0
   299
sl@0
   300