OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
matrix_vector_operation.h
Go to the documentation of this file.
1 //
2 // File with Boost uBLAS extensions
3 // This file provides additional functions for the matrix classes
4 // of uBLAS that is part of the Boost library (http://www.boost.org/).
5 //
6 // Copyright (c) 2014, Matthias Frey, ETH Zürich
7 // All rights reserved
8 //
9 // Implemented as part of the Semester thesis by Matthias Frey
10 // "Matched Distributions in Cyclotrons"
11 //
12 // This file is part of OPAL.
13 //
14 // OPAL is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
21 //
22 
23 #ifndef MATRIX_OPERATION_H
24 #define MATRIX_OPERATION_H
25 
26 #include <boost/numeric/ublas/matrix.hpp>
27 #include <boost/numeric/ublas/matrix_proxy.hpp>
28 #include <boost/numeric/ublas/matrix_sparse.hpp>
29 
30 #include <boost/numeric/ublas/vector.hpp>
31 
32 #include <stdexcept>
33 
39 namespace matt_boost {
40  namespace ublas = boost::numeric::ublas;
41 
43  template<class V>
44  BOOST_UBLAS_INLINE
45  V trace(ublas::matrix<V>& e) {
46  V tr = 0;
47  if (e.size1() == e.size2()) {
48  ublas::matrix_vector_range<ublas::matrix<V> > diag(e,ublas::range(0,e.size1()),ublas::range(0,e.size2()));
49  tr = sum(diag);
50  }
51  else
52  throw std::length_error("Error in function trace() of matrix_vector_operation.h: Wrong matrix dimensions.");
53 
54  return tr;
55  }
56 
58  template<class V, class A>
59  BOOST_UBLAS_INLINE
60  ublas::vector<V, A> cross_prod(ublas::vector<V, A>& v1, ublas::vector<V, A>& v2) {
61  ublas::vector<V, A> v(v1.size());
62  if (v1.size() == v2.size() && v1.size() == 3) {
63  v(0) = v1(1) * v2(2) - v1(2) * v2(1);
64  v(1) = v1(2) * v2(0) - v1(0) * v2(2);
65  v(2) = v1(0) * v2(1) - v1(1) * v2(0);
66  }
67  else
68  throw std::length_error("Error in function cross_prod() of matrix_vector_operation.h: Wrong vector dimensions.");
69 
70  return v;
71  }
72 
74  template<class V>
75  BOOST_UBLAS_INLINE
76  ublas::matrix<V> taylor_exp(const ublas::matrix<V>& F, const V ds, const unsigned int order) {
77  double fac = 1.0;
78  ublas::matrix<V> Fn = ublas::identity_matrix<V>(6);
79  ublas::matrix<V> M = Fn;
80 
81  for (unsigned int k = 1; k < order; ++k) {
82  fac *= ds / V(k);
83  Fn = prod(Fn,F);
84  M = M + fac * Fn;
85  }
86  return M;
87  }
88 
90  template<class M, class E1, class E2, class E3>
91  BOOST_UBLAS_INLINE
92  M gemmm(const ublas::matrix_expression<E1>& e1, const ublas::matrix_expression<E2>& e2, const ublas::matrix_expression<E3>& e3) {
93  M tmp = prod(e2,e3);
94  return prod(e1,tmp);
95  }
96 }
97 
100 #endif
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
T::PETE_Expr_t::PETE_Return_t prod(const PETE_Expr< T > &expr)
Definition: PETE.h:1121
constexpr double e
The value of.
Definition: Physics.h:39
BOOST_UBLAS_INLINE ublas::vector< V, A > cross_prod(ublas::vector< V, A > &v1, ublas::vector< V, A > &v2)
Computes the cross product of two vectors in .
BOOST_UBLAS_INLINE ublas::matrix< V > taylor_exp(const ublas::matrix< V > &F, const V ds, const unsigned int order)
Computes Taylor-Series of M(s) = exp(F*s)
BOOST_UBLAS_INLINE V trace(ublas::matrix< V > &e)
Computes the trace of a square matrix.
BOOST_UBLAS_INLINE M gemmm(const ublas::matrix_expression< E1 > &e1, const ublas::matrix_expression< E2 > &e2, const ublas::matrix_expression< E3 > &e3)
Generalized matrix-matrix-matrix multiplication .