OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
juce_Matrix.h
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
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 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26 namespace juce::dsp
27{
28
37 template <typename ElementType>
38 class Matrix
39{
40 public:
41 //==============================================================================
43 Matrix (size_t numRows, size_t numColumns)
44 : rows (numRows), columns (numColumns)
45 {
46 resize();
47 clear();
48 }
49
53 Matrix (size_t numRows, size_t numColumns, const ElementType* dataPointer)
54 : rows (numRows), columns (numColumns)
55 {
56 resize();
57 memcpy (data.getRawDataPointer(), dataPointer, rows * columns * sizeof (ElementType));
58 }
59
61 Matrix (const Matrix&) = default;
62
64 Matrix (Matrix&&) noexcept = default;
65
67 Matrix& operator= (const Matrix&) = default;
68
70 Matrix& operator= (Matrix&&) noexcept = default;
71
72 //==============================================================================
74 static Matrix identity (size_t size);
75
77 static Matrix toeplitz (const Matrix& vector, size_t size);
78
86 static Matrix hankel (const Matrix& vector, size_t size, size_t offset = 0);
87
88 //==============================================================================
90 size_t getNumRows() const noexcept { return rows; }
91
93 size_t getNumColumns() const noexcept { return columns; }
94
98 Array<size_t> getSize() const noexcept { return { rows, columns }; }
99
101 void clear() noexcept { zeromem (data.begin(), (size_t) data.size() * sizeof (ElementType)); }
102
103 //==============================================================================
105 Matrix& swapRows (size_t rowOne, size_t rowTwo) noexcept;
106
108 Matrix& swapColumns (size_t columnOne, size_t columnTwo) noexcept;
109
110 //==============================================================================
112 inline ElementType operator() (size_t row, size_t column) const noexcept
113 {
114 jassert (row < rows && column < columns);
115 return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
116 }
117
119 inline ElementType& operator() (size_t row, size_t column) noexcept
120 {
121 jassert (row < rows && column < columns);
122 return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
123 }
124
128 inline ElementType* getRawDataPointer() noexcept { return data.getRawDataPointer(); }
129
133 inline const ElementType* getRawDataPointer() const noexcept { return data.begin(); }
134
135 //==============================================================================
137 inline Matrix& operator+= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a + b; } ); }
138
140 inline Matrix& operator-= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a - b; } ); }
141
143 inline Matrix& operator*= (ElementType scalar) noexcept
144 {
145 std::for_each (begin(), end(), [scalar] (ElementType& x) { x *= scalar; });
146 return *this;
147 }
148
150 inline Matrix operator+ (const Matrix& other) const { Matrix result (*this); result += other; return result; }
151
153 inline Matrix operator- (const Matrix& other) const { Matrix result (*this); result -= other; return result; }
154
156 inline Matrix operator* (ElementType scalar) const { Matrix result (*this); result *= scalar; return result; }
157
159 Matrix operator* (const Matrix& other) const;
160
162 inline Matrix& hadarmard (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a * b; } ); }
163
165 static Matrix hadarmard (const Matrix& a, const Matrix& b) { Matrix result (a); result.hadarmard (b); return result; }
166
167 //==============================================================================
169 static bool compare (const Matrix& a, const Matrix& b, ElementType tolerance = 0) noexcept;
170
171 /* Comparison operator */
172 inline bool operator== (const Matrix& other) const noexcept { return compare (*this, other); }
173
174 //==============================================================================
176 bool isSquare() const noexcept { return rows == columns; }
177
179 bool isVector() const noexcept { return isOneColumnVector() || isOneRowVector(); }
180
182 bool isOneColumnVector() const noexcept { return columns == 1; }
183
185 bool isOneRowVector() const noexcept { return rows == 1; }
186
188 bool isNullMatrix() const noexcept { return rows == 0 || columns == 0; }
189
190 //==============================================================================
200 bool solve (Matrix& b) const noexcept;
201
202 //==============================================================================
204 String toString() const;
205
206 //==============================================================================
207 ElementType* begin() noexcept { return data.begin(); }
208 ElementType* end() noexcept { return data.end(); }
209
210 const ElementType* begin() const noexcept { return &data.getReference (0); }
211 const ElementType* end() const noexcept { return begin() + data.size(); }
212
213 private:
214 //==============================================================================
216 void resize()
217 {
218 data.resize (static_cast<int> (columns * rows));
219 dataAcceleration.resize (static_cast<int> (rows));
220
221 for (size_t i = 0; i < rows; ++i)
222 dataAcceleration.setUnchecked (static_cast<int> (i), i * columns);
223 }
224
225 template <typename BinaryOperation>
226 Matrix& apply (const Matrix& other, BinaryOperation binaryOp)
227 {
228 jassert (rows == other.rows && columns == other.columns);
229
230 auto* dst = getRawDataPointer();
231
232 for (auto src : other)
233 {
234 *dst = binaryOp (*dst, src);
235 ++dst;
236 }
237
238 return *this;
239 }
240
241 //==============================================================================
242 Array<ElementType> data;
243 Array<size_t> dataAcceleration;
244
245 size_t rows, columns;
246
247 //==============================================================================
248 JUCE_LEAK_DETECTOR (Matrix)
249};
250
251} // namespace juce::dsp
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:53
juce::dsp::Matrix
Definition juce_Matrix.h:39
juce::dsp::Matrix::operator-
Matrix operator-(const Matrix &other) const
Definition juce_Matrix.h:153
juce::dsp::Matrix::swapRows
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Definition juce_Matrix.cpp:94
juce::dsp::Matrix::hadarmard
Matrix & hadarmard(const Matrix &other) noexcept
Definition juce_Matrix.h:162
juce::dsp::Matrix::getNumRows
size_t getNumRows() const noexcept
Definition juce_Matrix.h:90
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:165
juce::dsp::Matrix::compare
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition juce_Matrix.cpp:146
juce::dsp::Matrix::Matrix
Matrix(const Matrix &)=default
juce::dsp::Matrix::isOneColumnVector
bool isOneColumnVector() const noexcept
Definition juce_Matrix.h:182
juce::dsp::Matrix::isVector
bool isVector() const noexcept
Definition juce_Matrix.h:179
juce::dsp::Matrix::isNullMatrix
bool isNullMatrix() const noexcept
Definition juce_Matrix.h:188
juce::dsp::Matrix::getSize
Array< size_t > getSize() const noexcept
Definition juce_Matrix.h:98
juce::dsp::Matrix::swapColumns
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Definition juce_Matrix.cpp:78
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns)
Definition juce_Matrix.h:43
juce::dsp::Matrix::hankel
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Definition juce_Matrix.cpp:59
juce::dsp::Matrix::operator*
Matrix operator*(ElementType scalar) const
Definition juce_Matrix.h:156
juce::dsp::Matrix::operator()
ElementType operator()(size_t row, size_t column) const noexcept
Definition juce_Matrix.h:112
juce::dsp::Matrix::getRawDataPointer
const ElementType * getRawDataPointer() const noexcept
Definition juce_Matrix.h:133
juce::dsp::Matrix::operator-=
Matrix & operator-=(const Matrix &other) noexcept
Definition juce_Matrix.h:140
juce::dsp::Matrix::operator+
Matrix operator+(const Matrix &other) const
Definition juce_Matrix.h:150
juce::dsp::Matrix::identity
static Matrix identity(size_t size)
Definition juce_Matrix.cpp:30
juce::dsp::Matrix::solve
bool solve(Matrix &b) const noexcept
Definition juce_Matrix.cpp:163
juce::dsp::Matrix::toeplitz
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition juce_Matrix.cpp:41
juce::dsp::Matrix::Matrix
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition juce_Matrix.h:53
juce::dsp::Matrix::getRawDataPointer
ElementType * getRawDataPointer() noexcept
Definition juce_Matrix.h:128
juce::dsp::Matrix::getNumColumns
size_t getNumColumns() const noexcept
Definition juce_Matrix.h:93
juce::dsp::Matrix::isSquare
bool isSquare() const noexcept
Definition juce_Matrix.h:176
juce::dsp::Matrix::operator*=
Matrix & operator*=(ElementType scalar) noexcept
Definition juce_Matrix.h:143
juce::dsp::Matrix::isOneRowVector
bool isOneRowVector() const noexcept
Definition juce_Matrix.h:185
juce::dsp::Matrix::clear
void clear() noexcept
Definition juce_Matrix.h:101
juce::dsp::Matrix::toString
String toString() const
Definition juce_Matrix.cpp:276
juce::dsp::Matrix::operator+=
Matrix & operator+=(const Matrix &other) noexcept
Definition juce_Matrix.h:137