blob: 40b4793426cde6b9ff8f64be29da8773239afaae [file] [log] [blame]
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +00001/*
2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_H_
13
14#include <algorithm>
Michael Graczykdfa36052015-03-25 16:37:27 -070015#include <cstring>
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000016#include <string>
17#include <vector>
18
19#include "webrtc/base/checks.h"
20#include "webrtc/base/constructormagic.h"
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000021
22namespace {
23
24// Wrappers to get around the compiler warning resulting from the fact that
25// there's no std::sqrt overload for ints. We cast all non-complex types to
26// a double for the sqrt method.
27template <typename T>
28T sqrt_wrapper(T x) {
29 return sqrt(static_cast<double>(x));
30}
31
32template <typename S>
33std::complex<S> sqrt_wrapper(std::complex<S> x) {
34 return sqrt(x);
35}
36} // namespace
37
38namespace webrtc {
39
40// Matrix is a class for doing standard matrix operations on 2 dimensional
41// matrices of any size. Results of matrix operations are stored in the
42// calling object. Function overloads exist for both in-place (the calling
43// object is used as both an operand and the result) and out-of-place (all
44// operands are passed in as parameters) operations. If operand dimensions
45// mismatch, the program crashes. Out-of-place operations change the size of
46// the calling object, if necessary, before operating.
47//
48// 'In-place' operations that inherently change the size of the matrix (eg.
49// Transpose, Multiply on different-sized matrices) must make temporary copies
50// (|scratch_elements_| and |scratch_data_|) of existing data to complete the
51// operations.
52//
53// The data is stored contiguously. Data can be accessed internally as a flat
54// array, |data_|, or as an array of row pointers, |elements_|, but is
55// available to users only as an array of row pointers through |elements()|.
56// Memory for storage is allocated when a matrix is resized only if the new
57// size overflows capacity. Memory needed temporarily for any operations is
58// similarly resized only if the new size overflows capacity.
59//
60// If you pass in storage through the ctor, that storage is copied into the
61// matrix. TODO(claguna): albeit tricky, allow for data to be referenced
62// instead of copied, and owned by the user.
63template <typename T>
64class Matrix {
65 public:
66 Matrix() : num_rows_(0), num_columns_(0) {}
67
68 // Allocates space for the elements and initializes all values to zero.
Peter Kasting69558702016-01-12 16:26:35 -080069 Matrix(size_t num_rows, size_t num_columns)
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000070 : num_rows_(num_rows), num_columns_(num_columns) {
71 Resize();
72 scratch_data_.resize(num_rows_ * num_columns_);
73 scratch_elements_.resize(num_rows_);
74 }
75
76 // Copies |data| into the new Matrix.
Peter Kasting69558702016-01-12 16:26:35 -080077 Matrix(const T* data, size_t num_rows, size_t num_columns)
aluebs@webrtc.org661af502015-02-19 19:02:17 +000078 : num_rows_(0), num_columns_(0) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000079 CopyFrom(data, num_rows, num_columns);
80 scratch_data_.resize(num_rows_ * num_columns_);
81 scratch_elements_.resize(num_rows_);
82 }
83
84 virtual ~Matrix() {}
85
86 // Deep copy an existing matrix.
87 void CopyFrom(const Matrix& other) {
88 CopyFrom(&other.data_[0], other.num_rows_, other.num_columns_);
89 }
90
91 // Copy |data| into the Matrix. The current data is lost.
Peter Kasting69558702016-01-12 16:26:35 -080092 void CopyFrom(const T* const data, size_t num_rows, size_t num_columns) {
aluebs@webrtc.org661af502015-02-19 19:02:17 +000093 Resize(num_rows, num_columns);
94 memcpy(&data_[0], data, num_rows_ * num_columns_ * sizeof(data_[0]));
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000095 }
96
Peter Kastingdce40cf2015-08-24 14:52:23 -070097 Matrix& CopyFromColumn(const T* const* src,
98 size_t column_index,
Peter Kasting69558702016-01-12 16:26:35 -080099 size_t num_rows) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000100 Resize(1, num_rows);
Peter Kasting69558702016-01-12 16:26:35 -0800101 for (size_t i = 0; i < num_columns_; ++i) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000102 data_[i] = src[i][column_index];
103 }
104
105 return *this;
106 }
107
Peter Kasting69558702016-01-12 16:26:35 -0800108 void Resize(size_t num_rows, size_t num_columns) {
aluebs@webrtc.org661af502015-02-19 19:02:17 +0000109 if (num_rows != num_rows_ || num_columns != num_columns_) {
110 num_rows_ = num_rows;
111 num_columns_ = num_columns;
112 Resize();
113 }
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000114 }
115
116 // Accessors and mutators.
Peter Kasting69558702016-01-12 16:26:35 -0800117 size_t num_rows() const { return num_rows_; }
118 size_t num_columns() const { return num_columns_; }
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000119 T* const* elements() { return &elements_[0]; }
120 const T* const* elements() const { return &elements_[0]; }
121
122 T Trace() {
henrikg91d6ede2015-09-17 00:24:34 -0700123 RTC_CHECK_EQ(num_rows_, num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000124
125 T trace = 0;
Peter Kasting69558702016-01-12 16:26:35 -0800126 for (size_t i = 0; i < num_rows_; ++i) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000127 trace += elements_[i][i];
128 }
129 return trace;
130 }
131
132 // Matrix Operations. Returns *this to support method chaining.
133 Matrix& Transpose() {
134 CopyDataToScratch();
aluebs@webrtc.org661af502015-02-19 19:02:17 +0000135 Resize(num_columns_, num_rows_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000136 return Transpose(scratch_elements());
137 }
138
139 Matrix& Transpose(const Matrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -0700140 RTC_CHECK_EQ(operand.num_rows_, num_columns_);
141 RTC_CHECK_EQ(operand.num_columns_, num_rows_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000142
143 return Transpose(operand.elements());
144 }
145
146 template <typename S>
147 Matrix& Scale(const S& scalar) {
148 for (size_t i = 0; i < data_.size(); ++i) {
149 data_[i] *= scalar;
150 }
151
152 return *this;
153 }
154
155 template <typename S>
156 Matrix& Scale(const Matrix& operand, const S& scalar) {
157 CopyFrom(operand);
158 return Scale(scalar);
159 }
160
161 Matrix& Add(const Matrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -0700162 RTC_CHECK_EQ(num_rows_, operand.num_rows_);
163 RTC_CHECK_EQ(num_columns_, operand.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000164
165 for (size_t i = 0; i < data_.size(); ++i) {
166 data_[i] += operand.data_[i];
167 }
168
169 return *this;
170 }
171
172 Matrix& Add(const Matrix& lhs, const Matrix& rhs) {
173 CopyFrom(lhs);
174 return Add(rhs);
175 }
176
177 Matrix& Subtract(const Matrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -0700178 RTC_CHECK_EQ(num_rows_, operand.num_rows_);
179 RTC_CHECK_EQ(num_columns_, operand.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000180
181 for (size_t i = 0; i < data_.size(); ++i) {
182 data_[i] -= operand.data_[i];
183 }
184
185 return *this;
186 }
187
188 Matrix& Subtract(const Matrix& lhs, const Matrix& rhs) {
189 CopyFrom(lhs);
190 return Subtract(rhs);
191 }
192
193 Matrix& PointwiseMultiply(const Matrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -0700194 RTC_CHECK_EQ(num_rows_, operand.num_rows_);
195 RTC_CHECK_EQ(num_columns_, operand.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000196
197 for (size_t i = 0; i < data_.size(); ++i) {
198 data_[i] *= operand.data_[i];
199 }
200
201 return *this;
202 }
203
204 Matrix& PointwiseMultiply(const Matrix& lhs, const Matrix& rhs) {
205 CopyFrom(lhs);
206 return PointwiseMultiply(rhs);
207 }
208
209 Matrix& PointwiseDivide(const Matrix& operand) {
henrikg91d6ede2015-09-17 00:24:34 -0700210 RTC_CHECK_EQ(num_rows_, operand.num_rows_);
211 RTC_CHECK_EQ(num_columns_, operand.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000212
213 for (size_t i = 0; i < data_.size(); ++i) {
214 data_[i] /= operand.data_[i];
215 }
216
217 return *this;
218 }
219
220 Matrix& PointwiseDivide(const Matrix& lhs, const Matrix& rhs) {
221 CopyFrom(lhs);
222 return PointwiseDivide(rhs);
223 }
224
225 Matrix& PointwiseSquareRoot() {
226 for (size_t i = 0; i < data_.size(); ++i) {
227 data_[i] = sqrt_wrapper(data_[i]);
228 }
229
230 return *this;
231 }
232
233 Matrix& PointwiseSquareRoot(const Matrix& operand) {
234 CopyFrom(operand);
235 return PointwiseSquareRoot();
236 }
237
238 Matrix& PointwiseAbsoluteValue() {
239 for (size_t i = 0; i < data_.size(); ++i) {
240 data_[i] = abs(data_[i]);
241 }
242
243 return *this;
244 }
245
246 Matrix& PointwiseAbsoluteValue(const Matrix& operand) {
247 CopyFrom(operand);
248 return PointwiseAbsoluteValue();
249 }
250
251 Matrix& PointwiseSquare() {
252 for (size_t i = 0; i < data_.size(); ++i) {
253 data_[i] *= data_[i];
254 }
255
256 return *this;
257 }
258
259 Matrix& PointwiseSquare(const Matrix& operand) {
260 CopyFrom(operand);
261 return PointwiseSquare();
262 }
263
264 Matrix& Multiply(const Matrix& lhs, const Matrix& rhs) {
henrikg91d6ede2015-09-17 00:24:34 -0700265 RTC_CHECK_EQ(lhs.num_columns_, rhs.num_rows_);
266 RTC_CHECK_EQ(num_rows_, lhs.num_rows_);
267 RTC_CHECK_EQ(num_columns_, rhs.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000268
269 return Multiply(lhs.elements(), rhs.num_rows_, rhs.elements());
270 }
271
272 Matrix& Multiply(const Matrix& rhs) {
henrikg91d6ede2015-09-17 00:24:34 -0700273 RTC_CHECK_EQ(num_columns_, rhs.num_rows_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000274
275 CopyDataToScratch();
aluebs@webrtc.org661af502015-02-19 19:02:17 +0000276 Resize(num_rows_, rhs.num_columns_);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000277 return Multiply(scratch_elements(), rhs.num_rows_, rhs.elements());
278 }
279
280 std::string ToString() const {
281 std::ostringstream ss;
282 ss << std::endl << "Matrix" << std::endl;
283
Peter Kasting69558702016-01-12 16:26:35 -0800284 for (size_t i = 0; i < num_rows_; ++i) {
285 for (size_t j = 0; j < num_columns_; ++j) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000286 ss << elements_[i][j] << " ";
287 }
288 ss << std::endl;
289 }
290 ss << std::endl;
291
292 return ss.str();
293 }
294
295 protected:
Peter Kasting69558702016-01-12 16:26:35 -0800296 void SetNumRows(const size_t num_rows) { num_rows_ = num_rows; }
297 void SetNumColumns(const size_t num_columns) { num_columns_ = num_columns; }
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000298 T* data() { return &data_[0]; }
299 const T* data() const { return &data_[0]; }
300 const T* const* scratch_elements() const { return &scratch_elements_[0]; }
301
302 // Resize the matrix. If an increase in capacity is required, the current
303 // data is lost.
304 void Resize() {
305 size_t size = num_rows_ * num_columns_;
306 data_.resize(size);
307 elements_.resize(num_rows_);
308
Peter Kasting69558702016-01-12 16:26:35 -0800309 for (size_t i = 0; i < num_rows_; ++i) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000310 elements_[i] = &data_[i * num_columns_];
311 }
312 }
313
314 // Copies data_ into scratch_data_ and updates scratch_elements_ accordingly.
315 void CopyDataToScratch() {
316 scratch_data_ = data_;
317 scratch_elements_.resize(num_rows_);
318
Peter Kasting69558702016-01-12 16:26:35 -0800319 for (size_t i = 0; i < num_rows_; ++i) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000320 scratch_elements_[i] = &scratch_data_[i * num_columns_];
321 }
322 }
323
324 private:
Peter Kasting69558702016-01-12 16:26:35 -0800325 size_t num_rows_;
326 size_t num_columns_;
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000327 std::vector<T> data_;
328 std::vector<T*> elements_;
329
330 // Stores temporary copies of |data_| and |elements_| for in-place operations
331 // where referring to original data is necessary.
332 std::vector<T> scratch_data_;
333 std::vector<T*> scratch_elements_;
334
335 // Helpers for Transpose and Multiply operations that unify in-place and
336 // out-of-place solutions.
337 Matrix& Transpose(const T* const* src) {
Peter Kasting69558702016-01-12 16:26:35 -0800338 for (size_t i = 0; i < num_rows_; ++i) {
339 for (size_t j = 0; j < num_columns_; ++j) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000340 elements_[i][j] = src[j][i];
341 }
342 }
343
344 return *this;
345 }
346
Peter Kasting69558702016-01-12 16:26:35 -0800347 Matrix& Multiply(const T* const* lhs,
348 size_t num_rows_rhs,
349 const T* const* rhs) {
350 for (size_t row = 0; row < num_rows_; ++row) {
351 for (size_t col = 0; col < num_columns_; ++col) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000352 T cur_element = 0;
Peter Kasting69558702016-01-12 16:26:35 -0800353 for (size_t i = 0; i < num_rows_rhs; ++i) {
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000354 cur_element += lhs[row][i] * rhs[i][col];
355 }
356
357 elements_[row][col] = cur_element;
358 }
359 }
360
361 return *this;
362 }
363
henrikg3c089d72015-09-16 05:37:44 -0700364 RTC_DISALLOW_COPY_AND_ASSIGN(Matrix);
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +0000365};
366
367} // namespace webrtc
368
369#endif // WEBRTC_MODULES_AUDIO_PROCESSING_BEAMFORMER_MATRIX_H_