OpenShot Audio Library | OpenShotAudio: juce_Matrix.h Source File

OpenShot Audio Library | OpenShotAudio 0.3.1

Loading...
Searching...
No Matches
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
27namespace juce
28{
29namespace dsp
30{
31
40template<typename ElementType>
41 class Matrix
42{
43public:
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
216private:
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
juce::Array
Definition: juce_Array.h:56
juce::Array::setUnchecked
void setUnchecked(int indexToChange, ParameterType newValue)
Definition: juce_Array.h:568
juce::Array::resize
void resize(int targetNumItems)
Definition: juce_Array.h:670
juce::Array::getReference
ElementType & getReference(int index) noexcept
Definition: juce_Array.h:267
juce::String
Definition: juce_String.h:39
juce::dsp::Matrix
Definition: juce_Matrix.h:42
juce::dsp::Matrix::operator-
Matrix operator-(const Matrix &other) const
Definition: juce_Matrix.h:156
juce::dsp::Matrix::swapRows
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Definition: juce_Matrix.cpp:97
juce::dsp::Matrix::hadarmard
Matrix & hadarmard(const Matrix &other) noexcept
Definition: juce_Matrix.h:165
juce::dsp::Matrix::getNumRows
size_t getNumRows() const noexcept
Definition: juce_Matrix.h:93
juce::dsp::Matrix::Matrix
Matrix(Matrix &&) noexcept=default
juce::dsp::Matrix::hadarmard
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Definition: juce_Matrix.h:168
juce::dsp::Matrix::compare
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition: juce_Matrix.cpp:149
juce::dsp::Matrix::Matrix
Matrix(const Matrix &)=default
juce::dsp::Matrix::isOneColumnVector
bool isOneColumnVector() const noexcept
Definition: juce_Matrix.h:185
juce::dsp::Matrix::isVector
bool isVector() const noexcept
Definition: juce_Matrix.h:182
juce::dsp::Matrix::isNullMatrix
bool isNullMatrix() const noexcept
Definition: juce_Matrix.h:191
juce::dsp::Matrix::getSize
Array< size_t > getSize() const noexcept
Definition: juce_Matrix.h:101
juce::dsp::Matrix::swapColumns
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Definition: juce_Matrix.cpp:81
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns)
Definition: juce_Matrix.h:46
juce::dsp::Matrix::hankel
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Definition: juce_Matrix.cpp:62
juce::dsp::Matrix::operator*
Matrix operator*(ElementType scalar) const
Definition: juce_Matrix.h:159
juce::dsp::Matrix::operator()
ElementType operator()(size_t row, size_t column) const noexcept
Definition: juce_Matrix.h:115
juce::dsp::Matrix::getRawDataPointer
const ElementType * getRawDataPointer() const noexcept
Definition: juce_Matrix.h:136
juce::dsp::Matrix::operator-=
Matrix & operator-=(const Matrix &other) noexcept
Definition: juce_Matrix.h:143
juce::dsp::Matrix::operator+
Matrix operator+(const Matrix &other) const
Definition: juce_Matrix.h:153
juce::dsp::Matrix::identity
static Matrix identity(size_t size)
Definition: juce_Matrix.cpp:33
juce::dsp::Matrix::solve
bool solve(Matrix &b) const noexcept
Definition: juce_Matrix.cpp:166
juce::dsp::Matrix::toeplitz
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition: juce_Matrix.cpp:44
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition: juce_Matrix.h:56
juce::dsp::Matrix::getRawDataPointer
ElementType * getRawDataPointer() noexcept
Definition: juce_Matrix.h:131
juce::dsp::Matrix::getNumColumns
size_t getNumColumns() const noexcept
Definition: juce_Matrix.h:96
juce::dsp::Matrix::isSquare
bool isSquare() const noexcept
Definition: juce_Matrix.h:179
juce::dsp::Matrix::operator*=
Matrix & operator*=(ElementType scalar) noexcept
Definition: juce_Matrix.h:146
juce::dsp::Matrix::isOneRowVector
bool isOneRowVector() const noexcept
Definition: juce_Matrix.h:188
juce::dsp::Matrix::clear
void clear() noexcept
Definition: juce_Matrix.h:104
juce::dsp::Matrix::toString
String toString() const
Definition: juce_Matrix.cpp:279
juce::dsp::Matrix::operator+=
Matrix & operator+=(const Matrix &other) noexcept
Definition: juce_Matrix.h:140