Thrill  0.1
matrix.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/matrix.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_COMMON_MATRIX_HEADER
13 #define THRILL_COMMON_MATRIX_HEADER
14 
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <functional>
20 #include <vector>
21 
22 namespace thrill {
23 namespace common {
24 
25 /*!
26  * A simple m x n dense matrix for generating statistics.
27  */
28 template <typename Type>
29 class Matrix
30 {
31 public:
32  //! empty matrix constructor
33  Matrix() = default;
34 
35  //! constructor of m times n matrix.
36  Matrix(size_t rows, size_t columns, const Type& initial = Type())
37  : rows_(rows), columns_(columns),
38  data_(rows_ * columns_, initial) { }
39 
40  //! constructor of square n times n matrix.
41  explicit Matrix(size_t rows_columns, const Type& initial = Type())
42  : Matrix(rows_columns, rows_columns, initial) { }
43 
44  //! constructor of m times n matrix from appropriate vector
45  Matrix(size_t rows, size_t columns, std::vector<Type>&& data)
46  : rows_(rows), columns_(columns),
47  data_(std::move(data)) {
48  assert(data_.size() == rows_ * columns_);
49  }
50 
51  //! number of rows in matrix
52  size_t rows() const { return rows_; }
53 
54  //! number of columns in matrix
55  size_t columns() const { return columns_; }
56 
57  //! raw data of matrix
58  Type * data() const { return data_.data(); }
59 
60  //! size of matrix raw data (rows * columns)
61  size_t size() const { return data_.size(); }
62 
63  //! return const reference to element at cell
64  const Type& at(size_t row, size_t column) const {
65  assert(row < rows_);
66  assert(column < columns_);
67  return data_[columns_ * row + column];
68  }
69 
70  //! return reference to element at cell
71  Type& at(size_t row, size_t column) {
72  assert(row < rows_);
73  assert(column < columns_);
74  return data_[columns_ * row + column];
75  }
76 
77  //! return const reference to element at cell
78  const Type& operator () (size_t row, size_t column) const {
79  return at(row, column);
80  }
81 
82  //! return reference to element at cell
83  Type& operator () (size_t row, size_t column) {
84  return at(row, column);
85  }
86 
87  //! add matrix to this one
88  Matrix& operator += (const Matrix& b) {
89  assert(rows() == b.rows() && columns() == b.columns());
90  std::transform(b.data_.begin(), b.data_.end(),
91  data_.begin(), data_.begin(), std::plus<Type>());
92  return *this;
93  }
94 
95  //! add matrix to this one, returning result as a new matrix
96  Matrix operator + (const Matrix& b) const {
97  assert(rows() == b.rows() && columns() == b.columns());
98  Matrix n = *this;
99  return (n += b);
100  }
101 
102  //! multiply matrix with a scalar
103  Matrix& operator *= (const Type& s) {
104  std::for_each(data_.begin(), data_.end(), [&s](Type& t) { t *= s; });
105  return *this;
106  }
107 
108  //! equality operator
109  bool operator == (const Matrix& b) const noexcept {
110  if (rows() != b.rows() || columns() != b.columns()) return false;
111  return std::equal(data_.begin(), data_.end(), b.data_.begin());
112  }
113 
114  //! inequality operator
115  bool operator != (const Matrix& b) const noexcept {
116  return !operator == (b);
117  }
118 
119  /**************************************************************************/
120 
121  static constexpr bool thrill_is_fixed_size = false;
122  static constexpr size_t thrill_fixed_size = 0;
123 
124  //! serialization with Thrill's serializer
125  template <typename Archive>
126  void ThrillSerialize(Archive& ar) const {
127  ar.template Put<size_t>(rows_);
128  ar.template Put<size_t>(columns_);
129  for (typename std::vector<Type>::const_iterator it = data_.begin();
130  it != data_.end(); ++it) {
132  }
133  }
134 
135  //! deserialization with Thrill's serializer
136  template <typename Archive>
137  static Matrix ThrillDeserialize(Archive& ar) {
138  size_t rows = ar.template Get<size_t>();
139  size_t columns = ar.template Get<size_t>();
140  std::vector<Type> data;
141  data.resize(0);
142  data.reserve(rows * columns);
143  for (size_t i = 0; i < rows * columns; ++i) {
144  data.emplace_back(
146  }
147  return Matrix(rows, columns, std::move(data));
148  }
149 
150 private:
151  //! number of rows in matrix
152  size_t rows_ = 0;
153  //! number of columns in matrix
154  size_t columns_ = 0;
155 
156  //! data of matrix.
157  std::vector<Type> data_;
158 };
159 
160 } // namespace common
161 } // namespace thrill
162 
163 #endif // !THRILL_COMMON_MATRIX_HEADER
164 
165 /******************************************************************************/
static Matrix ThrillDeserialize(Archive &ar)
deserialization with Thrill&#39;s serializer
Definition: matrix.hpp:137
Matrix()=default
empty matrix constructor
Matrix(size_t rows_columns, const Type &initial=Type())
constructor of square n times n matrix.
Definition: matrix.hpp:41
Matrix & operator*=(const Type &s)
multiply matrix with a scalar
Definition: matrix.hpp:103
bool operator==(const Matrix &b) const noexcept
equality operator
Definition: matrix.hpp:109
const Type & operator()(size_t row, size_t column) const
return const reference to element at cell
Definition: matrix.hpp:78
Type
VFS object type.
Definition: file_io.hpp:52
void ThrillSerialize(Archive &ar) const
serialization with Thrill&#39;s serializer
Definition: matrix.hpp:126
STL namespace.
A simple m x n dense matrix for generating statistics.
Definition: matrix.hpp:29
Type & at(size_t row, size_t column)
return reference to element at cell
Definition: matrix.hpp:71
Matrix(size_t rows, size_t columns, std::vector< Type > &&data)
constructor of m times n matrix from appropriate vector
Definition: matrix.hpp:45
std::vector< Type > data_
data of matrix.
Definition: matrix.hpp:157
size_t rows_
number of rows in matrix
Definition: matrix.hpp:152
const Type & at(size_t row, size_t column) const
return const reference to element at cell
Definition: matrix.hpp:64
static constexpr size_t thrill_fixed_size
Definition: matrix.hpp:122
static constexpr bool thrill_is_fixed_size
Definition: matrix.hpp:121
size_t columns_
number of columns in matrix
Definition: matrix.hpp:154
size_t size() const
size of matrix raw data (rows * columns)
Definition: matrix.hpp:61
Matrix operator+(const Matrix &b) const
add matrix to this one, returning result as a new matrix
Definition: matrix.hpp:96
size_t columns() const
number of columns in matrix
Definition: matrix.hpp:55
Matrix(size_t rows, size_t columns, const Type &initial=Type())
constructor of m times n matrix.
Definition: matrix.hpp:36
Type * data() const
raw data of matrix
Definition: matrix.hpp:58
Matrix & operator+=(const Matrix &b)
add matrix to this one
Definition: matrix.hpp:88
bool operator!=(const Matrix &b) const noexcept
inequality operator
Definition: matrix.hpp:115
size_t rows() const
number of rows in matrix
Definition: matrix.hpp:52