OpenShot Audio Library | OpenShotAudio  0.3.1
juce_Matrix.h
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  By using JUCE, you agree to the terms of both the JUCE 5 End-User License
11  Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
12  27th April 2017).
13 
14  End User License Agreement: www.juce.com/juce-5-licence
15  Privacy Policy: www.juce.com/juce-5-privacy-policy
16 
17  Or: You may also use this code under the terms of the GPL v3 (see
18  www.gnu.org/licenses).
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 namespace dsp
30 {
31 
40 template<typename ElementType>
41 class Matrix
42 {
43 public:
44  //==============================================================================
46  Matrix (size_t numRows, size_t numColumns)
47  : rows (numRows), columns (numColumns)
48  {
49  resize();
50  clear();
51  }
52 
56  Matrix (size_t numRows, size_t numColumns, const ElementType* dataPointer)
57  : rows (numRows), columns (numColumns)
58  {
59  resize();
60  memcpy (data.getRawDataPointer(), dataPointer, rows * columns * sizeof (ElementType));
61  }
62 
64  Matrix (const Matrix&) = default;
65 
67  Matrix (Matrix&&) noexcept = default;
68 
70  Matrix& operator= (const Matrix&) = default;
71 
73  Matrix& operator= (Matrix&&) noexcept = default;
74 
75  //==============================================================================
77  static Matrix identity (size_t size);
78 
80  static Matrix toeplitz (const Matrix& vector, size_t size);
81 
89  static Matrix hankel (const Matrix& vector, size_t size, size_t offset = 0);
90 
91  //==============================================================================
93  size_t getNumRows() const noexcept { return rows; }
94 
96  size_t getNumColumns() const noexcept { return columns; }
97 
101  Array<size_t> getSize() const noexcept { return { rows, columns }; }
102 
104  void clear() noexcept { zeromem (data.begin(), (size_t) data.size() * sizeof (ElementType)); }
105 
106  //==============================================================================
108  Matrix& swapRows (size_t rowOne, size_t rowTwo) noexcept;
109 
111  Matrix& swapColumns (size_t columnOne, size_t columnTwo) noexcept;
112 
113  //==============================================================================
115  inline ElementType operator() (size_t row, size_t column) const noexcept
116  {
117  jassert (row < rows && column < columns);
118  return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
119  }
120 
122  inline ElementType& operator() (size_t row, size_t column) noexcept
123  {
124  jassert (row < rows && column < columns);
125  return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
126  }
127 
131  inline ElementType* getRawDataPointer() noexcept { return data.getRawDataPointer(); }
132 
136  inline const ElementType* getRawDataPointer() const noexcept { return data.begin(); }
137 
138  //==============================================================================
140  inline Matrix& operator+= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a + b; } ); }
141 
143  inline Matrix& operator-= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a - b; } ); }
144 
146  inline Matrix& operator*= (ElementType scalar) noexcept
147  {
148  std::for_each (begin(), end(), [scalar] (ElementType& x) { x *= scalar; });
149  return *this;
150  }
151 
153  inline Matrix operator+ (const Matrix& other) const { Matrix result (*this); result += other; return result; }
154 
156  inline Matrix operator- (const Matrix& other) const { Matrix result (*this); result -= other; return result; }
157 
159  inline Matrix operator* (ElementType scalar) const { Matrix result (*this); result *= scalar; return result; }
160 
162  Matrix operator* (const Matrix& other) const;
163 
165  inline Matrix& hadarmard (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a * b; } ); }
166 
168  static inline Matrix hadarmard (const Matrix& a, const Matrix& b) { Matrix result (a); result.hadarmard (b); return result; }
169 
170  //==============================================================================
172  static bool compare (const Matrix& a, const Matrix& b, ElementType tolerance = 0) noexcept;
173 
174  /* Comparison operator */
175  inline bool operator== (const Matrix& other) const noexcept { return compare (*this, other); }
176 
177  //==============================================================================
179  bool isSquare() const noexcept { return rows == columns; }
180 
182  bool isVector() const noexcept { return isOneColumnVector() || isOneRowVector(); }
183 
185  bool isOneColumnVector() const noexcept { return columns == 1; }
186 
188  bool isOneRowVector() const noexcept { return rows == 1; }
189 
191  bool isNullMatrix() const noexcept { return rows == 0 || columns == 0; }
192 
193  //==============================================================================
203  bool solve (Matrix& b) const noexcept;
204 
205  //==============================================================================
207  String toString() const;
208 
209  //==============================================================================
210  ElementType* begin() noexcept { return data.begin(); }
211  ElementType* end() noexcept { return data.end(); }
212 
213  const ElementType* begin() const noexcept { return &data.getReference (0); }
214  const ElementType* end() const noexcept { return begin() + data.size(); }
215 
216 private:
217  //==============================================================================
219  void resize()
220  {
221  data.resize (static_cast<int> (columns * rows));
222  dataAcceleration.resize (static_cast<int> (rows));
223 
224  for (size_t i = 0; i < rows; ++i)
225  dataAcceleration.setUnchecked (static_cast<int> (i), i * columns);
226  }
227 
228  template <typename BinaryOperation>
229  Matrix& apply (const Matrix& other, BinaryOperation binaryOp)
230  {
231  jassert (rows == other.rows && columns == other.columns);
232 
233  auto* dst = getRawDataPointer();
234 
235  for (auto src : other)
236  {
237  *dst = binaryOp (*dst, src);
238  ++dst;
239  }
240 
241  return *this;
242  }
243 
244  //==============================================================================
245  Array<ElementType> data;
246  Array<size_t> dataAcceleration;
247 
248  size_t rows, columns;
249 
250  //==============================================================================
251  JUCE_LEAK_DETECTOR (Matrix)
252 };
253 
254 } // namespace dsp
255 } // namespace juce
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Definition: juce_Matrix.cpp:97
Matrix operator+(const Matrix &other) const
Definition: juce_Matrix.h:153
Matrix operator*(ElementType scalar) const
Definition: juce_Matrix.h:159
size_t getNumColumns() const noexcept
Definition: juce_Matrix.h:96
ElementType * end() noexcept
Definition: juce_Array.h:344
bool isOneColumnVector() const noexcept
Definition: juce_Matrix.h:185
static Matrix identity(size_t size)
Definition: juce_Matrix.cpp:33
Matrix & hadarmard(const Matrix &other) noexcept
Definition: juce_Matrix.h:165
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Definition: juce_Matrix.cpp:81
bool isOneRowVector() const noexcept
Definition: juce_Matrix.h:188
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Definition: juce_Matrix.h:168
void resize(int targetNumItems)
Definition: juce_Array.h:670
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Definition: juce_Matrix.cpp:62
String toString() const
Matrix(size_t numRows, size_t numColumns)
Definition: juce_Matrix.h:46
ElementType * begin() noexcept
Definition: juce_Array.h:328
size_t getNumRows() const noexcept
Definition: juce_Matrix.h:93
Matrix operator-(const Matrix &other) const
Definition: juce_Matrix.h:156
void setUnchecked(int indexToChange, ParameterType newValue)
Definition: juce_Array.h:568
ElementType * getRawDataPointer() noexcept
Definition: juce_Matrix.h:131
bool solve(Matrix &b) const noexcept
bool isSquare() const noexcept
Definition: juce_Matrix.h:179
Matrix & operator=(const Matrix &)=default
Array< size_t > getSize() const noexcept
Definition: juce_Matrix.h:101
int size() const noexcept
Definition: juce_Array.h:215
Matrix & operator+=(const Matrix &other) noexcept
Definition: juce_Matrix.h:140
ElementType * getRawDataPointer() noexcept
Definition: juce_Array.h:310
Matrix & operator*=(ElementType scalar) noexcept
Definition: juce_Matrix.h:146
ElementType & getReference(int index) noexcept
Definition: juce_Array.h:267
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition: juce_Matrix.h:56
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition: juce_Matrix.cpp:44
void clear() noexcept
Definition: juce_Matrix.h:104
const ElementType * getRawDataPointer() const noexcept
Definition: juce_Matrix.h:136
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
ElementType operator()(size_t row, size_t column) const noexcept
Definition: juce_Matrix.h:115
bool isNullMatrix() const noexcept
Definition: juce_Matrix.h:191
bool isVector() const noexcept
Definition: juce_Matrix.h:182
Matrix & operator-=(const Matrix &other) noexcept
Definition: juce_Matrix.h:143