Allium  0.1.0
local_vector.hpp
1 // Copyright 2020 Hannah Rittich
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ALLIUM_LA_LOCAL_VECTOR_HPP
16 #define ALLIUM_LA_LOCAL_VECTOR_HPP
17 
18 #include <Eigen/Core>
19 #include "vector_trait.hpp"
20 #include <allium/util/extern.hpp>
21 
22 namespace allium {
23 
28  template <typename N>
30  : public VectorTrait<LocalVector<N>, N>
31  {
32  public:
33  using Number = N;
34  using Real = real_part_t<Number>;
35  using EigenVector = Eigen::Matrix<N, Eigen::Dynamic, 1>;
36 
37  explicit LocalVector(size_t rows)
38  : m_storage(rows)
39  {}
40 
41  explicit LocalVector(std::initializer_list<N> entries)
42  : m_storage(entries.size())
43  {
44  size_t i_entry = 0;
45  for (auto entry : entries) {
46  m_storage(i_entry) = entry;
47  ++i_entry;
48  }
49  }
50 
51  explicit LocalVector(const EigenVector& v)
52  : m_storage(v)
53  {}
54 
60  static LocalVector constant(size_t rows, Number x) {
61  LocalVector v(rows);
62  for (size_t i=0; i < rows; ++i) {
63  v[i] = x;
64  }
65  return v;
66  }
67 
68  LocalVector& operator+= (const LocalVector& rhs);
69  LocalVector& operator*= (Number rhs);
70 
71  Number dot(const LocalVector& rhs) const {
72  return rhs.m_storage.dot(m_storage);
73  }
74 
75  Number& operator[] (size_t i_element) {
76  return m_storage(i_element);
77  }
78  Number operator[] (size_t i_element) const {
79  return (*const_cast<LocalVector*>(this))[i_element];
80  }
81 
85  size_t rows() const { return m_storage.rows(); }
86 
87  [[deprecated ("Use the 'rows' method instead.")]]
88  size_t nrows() const { return rows(); }
89 
90  const EigenVector& native() const { return m_storage; }
91  private:
92  EigenVector m_storage;
93  };
94 
95  template <typename N>
96  LocalVector<N>& LocalVector<N>::operator+= (const LocalVector& rhs)
97  {
98  m_storage += rhs.m_storage;
99  return *this;
100  }
101 
102  template <typename N>
103  LocalVector<N>& LocalVector<N>::operator*= (Number rhs)
104  {
105  m_storage *= rhs;
106  return *this;
107  }
108 
109  template <typename N>
110  LocalVector<N> operator* (N s, const LocalVector<N>& v) {
111  LocalVector<N> w = v;
112  w *= s;
113  return w;
114  }
115 
116  template <typename N>
117  std::ostream& operator<< (std::ostream& os, const LocalVector<N>& v)
118  {
119  bool first = true;
120  for (size_t i = 0; i < v.rows(); ++i) {
121  if (first) first = false;
122  else os << " ";
123 
124  os << v[i];
125  }
126 
127  return os;
128  }
129 
130  #define ALLIUM_LOCAL_VECTOR_DECL(extern, N) \
131  extern template class LocalVector<N>;
132  ALLIUM_EXTERN_N(ALLIUM_LOCAL_VECTOR_DECL)
133 }
134 
135 #endif
Implements a (mathematical) vector in the mathematical sense, which is stored locally,...
Definition: local_vector.hpp:31
static LocalVector constant(size_t rows, Number x)
Definition: local_vector.hpp:60
size_t rows() const
Definition: local_vector.hpp:85
Generates common vector operations automatically.
Definition: vector_trait.hpp:31