JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
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::dsp
27{
28
37template <typename ElementType>
38class Matrix
39{
40public:
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
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
213private:
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
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:56
void setUnchecked(int indexToChange, ParameterType newValue)
Replaces an element with a new value without doing any bounds-checking.
Definition juce_Array.h:568
void resize(int targetNumItems)
This will enlarge or shrink the array to the given number of elements, by adding or removing items fr...
Definition juce_Array.h:670
ElementType & getReference(int index) noexcept
Returns a direct reference to one of the elements in the array, without checking the index passed in.
Definition juce_Array.h:267
The JUCE String class!
Definition juce_String.h:53
General matrix and vectors class, meant for classic math manipulation such as additions,...
Definition juce_Matrix.h:39
Matrix operator-(const Matrix &other) const
Addition of two matrices.
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Swaps the contents of two rows in the matrix and returns a reference to itself.
Matrix & hadarmard(const Matrix &other) noexcept
Does a hadarmard product with the receiver and other and stores the result in the receiver.
size_t getNumRows() const noexcept
Returns the number of rows in the matrix.
Definition juce_Matrix.h:90
Matrix(Matrix &&) noexcept=default
Moves a copy of another matrix.
static Matrix hadarmard(const Matrix &a, const Matrix &b)
Does a hadarmard product with a and b returns the result.
Matrix(const Matrix &)=default
Creates a copy of another matrix.
bool isOneColumnVector() const noexcept
Tells if the matrix is a one column vector.
bool isVector() const noexcept
Tells if the matrix is a vector.
bool isNullMatrix() const noexcept
Tells if the matrix is a null matrix.
Array< size_t > getSize() const noexcept
Returns an Array of 2 integers with the number of rows and columns in the matrix.
Definition juce_Matrix.h:98
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Swaps the contents of two columns in the matrix and returns a reference to itself.
Matrix(size_t numRows, size_t numColumns)
Creates a new matrix with a given number of rows and columns.
Definition juce_Matrix.h:43
static Matrix hankel(const Matrix &vector, size_t size, size_t offset=0)
Creates a squared size x size Hankel Matrix from a vector with an optional offset.
Matrix operator*(ElementType scalar) const
Scalar multiplication.
ElementType operator()(size_t row, size_t column) const noexcept
Returns the value of the matrix at a given row and column (for reading).
const ElementType * getRawDataPointer() const noexcept
Returns a pointer to the raw data of the matrix object, ordered in row-major order (for reading).
Matrix & operator-=(const Matrix &other) noexcept
Subtraction of two matrices.
Matrix operator+(const Matrix &other) const
Addition of two matrices.
static Matrix identity(size_t size)
Creates the identity matrix.
bool solve(Matrix &b) const noexcept
Solves a linear system of equations represented by this object and the argument b,...
static Matrix toeplitz(const Matrix &vector, size_t size)
Creates a Toeplitz Matrix from a vector with a given squared size.
Matrix(size_t numRows, size_t numColumns, const ElementType *dataPointer)
Creates a new matrix with a given number of rows and columns, with initial data coming from an array,...
Definition juce_Matrix.h:53
ElementType * getRawDataPointer() noexcept
Returns a pointer to the raw data of the matrix object, ordered in row-major order (for modifying).
size_t getNumColumns() const noexcept
Returns the number of columns in the matrix.
Definition juce_Matrix.h:93
bool isSquare() const noexcept
Tells if the matrix is a square matrix.
Matrix & operator*=(ElementType scalar) noexcept
Scalar multiplication.
bool isOneRowVector() const noexcept
Tells if the matrix is a one row vector.
void clear() noexcept
Fills the contents of the matrix with zeroes.
String toString() const
Returns a String displaying in a convenient way the matrix contents.
Matrix & operator+=(const Matrix &other) noexcept
Addition of two matrices.
T for_each(T... args)
#define JUCE_LEAK_DETECTOR(OwnerClass)
This macro lets you embed a leak-detecting object inside a class.
#define jassert(expression)
Platform-independent assertion macro.
memcpy
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
void zeromem(void *memory, size_t numBytes) noexcept
Fills a block of memory with zeros.
Definition juce_Memory.h:28