LMMS
Loading...
Searching...
No Matches
juce_Matrix.h
Go to the documentation of this file.
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
26namespace juce
27{
28namespace dsp
29{
30
39template <typename ElementType>
40class Matrix
41{
42public:
43 //==============================================================================
45 Matrix (size_t numRows, size_t numColumns)
46 : rows (numRows), columns (numColumns)
47 {
48 resize();
49 clear();
50 }
51
55 Matrix (size_t numRows, size_t numColumns, const ElementType* dataPointer)
56 : rows (numRows), columns (numColumns)
57 {
58 resize();
59 memcpy (data.getRawDataPointer(), dataPointer, rows * columns * sizeof (ElementType));
60 }
61
63 Matrix (const Matrix&) = default;
64
66 Matrix (Matrix&&) noexcept = default;
67
69 Matrix& operator= (const Matrix&) = default;
70
72 Matrix& operator= (Matrix&&) noexcept = default;
73
74 //==============================================================================
76 static Matrix identity (size_t size);
77
79 static Matrix toeplitz (const Matrix& vector, size_t size);
80
88 static Matrix hankel (const Matrix& vector, size_t size, size_t offset = 0);
89
90 //==============================================================================
92 size_t getNumRows() const noexcept { return rows; }
93
95 size_t getNumColumns() const noexcept { return columns; }
96
101
103 void clear() noexcept { zeromem (data.begin(), (size_t) data.size() * sizeof (ElementType)); }
104
105 //==============================================================================
107 Matrix& swapRows (size_t rowOne, size_t rowTwo) noexcept;
108
110 Matrix& swapColumns (size_t columnOne, size_t columnTwo) noexcept;
111
112 //==============================================================================
114 inline ElementType operator() (size_t row, size_t column) const noexcept
115 {
116 jassert (row < rows && column < columns);
117 return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
118 }
119
121 inline ElementType& operator() (size_t row, size_t column) noexcept
122 {
123 jassert (row < rows && column < columns);
124 return data.getReference (static_cast<int> (dataAcceleration.getReference (static_cast<int> (row))) + static_cast<int> (column));
125 }
126
130 inline ElementType* getRawDataPointer() noexcept { return data.getRawDataPointer(); }
131
135 inline const ElementType* getRawDataPointer() const noexcept { return data.begin(); }
136
137 //==============================================================================
139 inline Matrix& operator+= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a + b; } ); }
140
142 inline Matrix& operator-= (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a - b; } ); }
143
145 inline Matrix& operator*= (ElementType scalar) noexcept
146 {
147 std::for_each (begin(), end(), [scalar] (ElementType& x) { x *= scalar; });
148 return *this;
149 }
150
152 inline Matrix operator+ (const Matrix& other) const { Matrix result (*this); result += other; return result; }
153
155 inline Matrix operator- (const Matrix& other) const { Matrix result (*this); result -= other; return result; }
156
158 inline Matrix operator* (ElementType scalar) const { Matrix result (*this); result *= scalar; return result; }
159
161 Matrix operator* (const Matrix& other) const;
162
164 inline Matrix& hadarmard (const Matrix& other) noexcept { return apply (other, [] (ElementType a, ElementType b) { return a * b; } ); }
165
167 static Matrix hadarmard (const Matrix& a, const Matrix& b) { Matrix result (a); result.hadarmard (b); return result; }
168
169 //==============================================================================
171 static bool compare (const Matrix& a, const Matrix& b, ElementType tolerance = 0) noexcept;
172
173 /* Comparison operator */
174 inline bool operator== (const Matrix& other) const noexcept { return compare (*this, other); }
175
176 //==============================================================================
178 bool isSquare() const noexcept { return rows == columns; }
179
182
184 bool isOneColumnVector() const noexcept { return columns == 1; }
185
187 bool isOneRowVector() const noexcept { return rows == 1; }
188
190 bool isNullMatrix() const noexcept { return rows == 0 || columns == 0; }
191
192 //==============================================================================
202 bool solve (Matrix& b) const noexcept;
203
204 //==============================================================================
206 String toString() const;
207
208 //==============================================================================
209 ElementType* begin() noexcept { return data.begin(); }
210 ElementType* end() noexcept { return data.end(); }
211
212 const ElementType* begin() const noexcept { return &data.getReference (0); }
213 const ElementType* end() const noexcept { return begin() + data.size(); }
214
215private:
216 //==============================================================================
218 void resize()
219 {
220 data.resize (static_cast<int> (columns * rows));
221 dataAcceleration.resize (static_cast<int> (rows));
222
223 for (size_t i = 0; i < rows; ++i)
224 dataAcceleration.setUnchecked (static_cast<int> (i), i * columns);
225 }
226
227 template <typename BinaryOperation>
228 Matrix& apply (const Matrix& other, BinaryOperation binaryOp)
229 {
230 jassert (rows == other.rows && columns == other.columns);
231
232 auto* dst = getRawDataPointer();
233
234 for (auto src : other)
235 {
236 *dst = binaryOp (*dst, src);
237 ++dst;
238 }
239
240 return *this;
241 }
242
243 //==============================================================================
246
247 size_t rows, columns;
248
249 //==============================================================================
251};
252
253} // namespace dsp
254} // namespace juce
#define noexcept
Definition DistrhoDefines.h:72
uint8_t a
Definition Spc_Cpu.h:141
Definition juce_Array.h:56
Definition juce_String.h:53
Definition juce_Matrix.h:41
Matrix & hadarmard(const Matrix &other) noexcept
Definition juce_Matrix.h:164
size_t getNumRows() const noexcept
Definition juce_Matrix.h:92
Matrix(Matrix &&) noexcept=default
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Definition juce_Matrix.h:167
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition juce_Matrix.cpp:148
ElementType * begin() noexcept
Definition juce_Matrix.h:209
Matrix(const Matrix &)=default
void resize()
Definition juce_Matrix.h:218
bool isOneColumnVector() const noexcept
Definition juce_Matrix.h:184
bool isVector() const noexcept
Definition juce_Matrix.h:181
bool isNullMatrix() const noexcept
Definition juce_Matrix.h:190
Array< size_t > getSize() const noexcept
Definition juce_Matrix.h:100
Matrix(size_t numRows, size_t numColumns)
Definition juce_Matrix.h:45
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Definition juce_Matrix.cpp:61
Matrix & apply(const Matrix &other, BinaryOperation binaryOp)
Definition juce_Matrix.h:228
size_t columns
Definition juce_Matrix.h:247
ElementType * end() noexcept
Definition juce_Matrix.h:210
Array< size_t > dataAcceleration
Definition juce_Matrix.h:245
const ElementType * getRawDataPointer() const noexcept
Definition juce_Matrix.h:135
const ElementType * begin() const noexcept
Definition juce_Matrix.h:212
static Matrix identity(size_t size)
Definition juce_Matrix.cpp:32
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition juce_Matrix.cpp:43
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Definition juce_Matrix.h:55
ElementType * getRawDataPointer() noexcept
Definition juce_Matrix.h:130
size_t getNumColumns() const noexcept
Definition juce_Matrix.h:95
bool isSquare() const noexcept
Definition juce_Matrix.h:178
const ElementType * end() const noexcept
Definition juce_Matrix.h:213
bool isOneRowVector() const noexcept
Definition juce_Matrix.h:187
size_t rows
Definition juce_Matrix.h:247
void clear() noexcept
Definition juce_Matrix.h:103
Array< ElementType > data
Definition juce_Matrix.h:244
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
#define JUCE_LEAK_DETECTOR(OwnerClass)
Definition juce_LeakedObjectDetector.h:138
#define jassert(expression)
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
juce::String toString(const Steinberg::char8 *string) noexcept
Definition juce_VST3Common.h:159
@ column
Definition juce_AccessibilityRole.h:52
@ row
Definition juce_AccessibilityRole.h:53
void zeromem(void *memory, size_t numBytes) noexcept
Definition juce_Memory.h:28
static int compare(const var &v1, const var &v2)
Definition juce_Variant.cpp:654
memcpy(hh, h, RAND_HEAD_LEN)
b
Definition crypt.c:628
ulg size
Definition extract.c:2350
int result
Definition process.c:1455
#define const
Definition zconf.h:137