OpenShot Audio Library | OpenShotAudio 0.3.2
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>
41class 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
68
71
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
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
void setUnchecked(int indexToChange, ParameterType newValue)
Definition juce_Array.h:568
int size() const noexcept
Definition juce_Array.h:215
ElementType * begin() noexcept
Definition juce_Array.h:328
ElementType * end() noexcept
Definition juce_Array.h:344
ElementType * getRawDataPointer() noexcept
Definition juce_Array.h:310
void resize(int targetNumItems)
Definition juce_Array.h:670
ElementType & getReference(int index) noexcept
Definition juce_Array.h:267
Matrix operator-(const Matrix &other) const
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Matrix & hadarmard(const Matrix &other) noexcept
size_t getNumRows() const noexcept
Definition juce_Matrix.h:93
Matrix(Matrix &&) noexcept=default
static Matrix hadarmard(const Matrix &a, const Matrix &b)
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Matrix(const Matrix &)=default
bool isOneColumnVector() const noexcept
bool isVector() const noexcept
bool isNullMatrix() const noexcept
Array< size_t > getSize() const noexcept
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Matrix(size_t numRows, size_t numColumns)
Definition juce_Matrix.h:46
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Matrix operator*(ElementType scalar) const
ElementType operator()(size_t row, size_t column) const noexcept
const ElementType * getRawDataPointer() const noexcept
Matrix & operator-=(const Matrix &other) noexcept
Matrix operator+(const Matrix &other) const
static Matrix identity(size_t size)
bool solve(Matrix &b) const noexcept
static Matrix toeplitz(const Matrix &vector, size_t size)
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition juce_Matrix.h:56
ElementType * getRawDataPointer() noexcept
size_t getNumColumns() const noexcept
Definition juce_Matrix.h:96
bool isSquare() const noexcept
Matrix & operator*=(ElementType scalar) noexcept
bool isOneRowVector() const noexcept
void clear() noexcept
String toString() const
Matrix & operator+=(const Matrix &other) noexcept