LMMS
Loading...
Searching...
No Matches
juce_Matrix.cpp
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
31template <typename ElementType>
33{
35
36 for (size_t i = 0; i < size; ++i)
37 result(i, i) = 1;
38
39 return result;
40}
41
42template <typename ElementType>
44{
45 jassert (vector.isOneColumnVector());
46 jassert (size <= vector.rows);
47
49
50 for (size_t i = 0; i < size; ++i)
51 result (i, i) = vector (0, 0);
52
53 for (size_t i = 1; i < size; ++i)
54 for (size_t j = i; j < size; ++j)
55 result (j, j - i) = result (j - i, j) = vector (i, 0);
56
57 return result;
58}
59
60template <typename ElementType>
62{
63 jassert(vector.isOneColumnVector());
64 jassert(vector.rows >= (2 * (size - 1) + 1));
65
67
68 for (size_t i = 0; i < size; ++i)
69 result (i, i) = vector ((2 * i) + offset, 0);
70
71 for (size_t i = 1; i < size; ++i)
72 for (size_t j = i; j < size; ++j)
73 result (j, j - i) = result (j - i, j) = vector (i + 2 * (j - i) + offset, 0);
74
75 return result;
76}
77
78//==============================================================================
79template <typename ElementType>
80Matrix<ElementType>& Matrix<ElementType>::swapColumns (size_t columnOne, size_t columnTwo) noexcept
81{
82 jassert (columnOne < columns && columnTwo < columns);
83
84 auto* p = data.getRawDataPointer();
85
86 for (size_t i = 0; i < rows; ++i)
87 {
88 auto offset = dataAcceleration.getUnchecked (static_cast<int> (i));
89 std::swap (p[offset + columnOne], p[offset + columnTwo]);
90 }
91
92 return *this;
93}
94
95template <typename ElementType>
96Matrix<ElementType>& Matrix<ElementType>::swapRows (size_t rowOne, size_t rowTwo) noexcept
97{
98 jassert (rowOne < rows && rowTwo < rows);
99
100 auto offset1 = rowOne * columns;
101 auto offset2 = rowTwo * columns;
102
103 auto* p = data.getRawDataPointer();
104
105 for (size_t i = 0; i < columns; ++i)
106 std::swap (p[offset1 + i], p[offset2 + i]);
107
108 return *this;
109}
110
111//==============================================================================
112template <typename ElementType>
114{
115 auto n = getNumRows(), m = other.getNumColumns(), p = getNumColumns();
116 Matrix result (n, m);
117
118 jassert (p == other.getNumRows());
119
120 size_t offsetMat = 0, offsetlhs = 0;
121
122 auto* dst = result.getRawDataPointer();
123 auto* a = getRawDataPointer();
124 auto* b = other.getRawDataPointer();
125
126 for (size_t i = 0; i < n; ++i)
127 {
128 size_t offsetrhs = 0;
129
130 for (size_t k = 0; k < p; ++k)
131 {
132 auto ak = a[offsetlhs++];
133
134 for (size_t j = 0; j < m; ++j)
135 dst[offsetMat + j] += ak * b[offsetrhs + j];
136
137 offsetrhs += m;
138 }
139
140 offsetMat += m;
141 }
142
143 return result;
144}
145
146//==============================================================================
147template <typename ElementType>
148bool Matrix<ElementType>::compare (const Matrix& a, const Matrix& b, ElementType tolerance) noexcept
149{
150 if (a.rows != b.rows || a.columns != b.columns)
151 return false;
152
153 tolerance = std::abs (tolerance);
154
155 auto* bPtr = b.begin();
156 for (auto aValue : a)
157 if (std::abs (aValue - *bPtr++) > tolerance)
158 return false;
159
160 return true;
161}
162
163//==============================================================================
164template <typename ElementType>
165bool Matrix<ElementType>::solve (Matrix& b) const noexcept
166{
167 auto n = columns;
168 jassert (n == n && n == b.rows && b.isOneColumnVector());
169
170 auto* x = b.getRawDataPointer();
171 const auto& A = *this;
172
173 switch (n)
174 {
175 case 1:
176 {
177 auto denominator = A (0,0);
178
179 if (denominator == 0)
180 return false;
181
182 b (0, 0) /= denominator;
183 }
184 break;
185
186 case 2:
187 {
188 auto denominator = A (0, 0) * A (1, 1) - A (0, 1) * A (1, 0);
189
190 if (denominator == 0)
191 return false;
192
193 auto factor = (1 / denominator);
194 auto b0 = x[0], b1 = x[1];
195
196 x[0] = factor * (A (1, 1) * b0 - A (0, 1) * b1);
197 x[1] = factor * (A (0, 0) * b1 - A (1, 0) * b0);
198 }
199 break;
200
201 case 3:
202 {
203 auto denominator = A (0, 0) * (A (1, 1) * A (2, 2) - A (1, 2) * A (2, 1))
204 + A (0, 1) * (A (1, 2) * A (2, 0) - A (1, 0) * A (2, 2))
205 + A (0, 2) * (A (1, 0) * A (2, 1) - A (1, 1) * A (2, 0));
206
207 if (denominator == 0)
208 return false;
209
210 auto factor = 1 / denominator;
211 auto b0 = x[0], b1 = x[1], b2 = x[2];
212
213 x[0] = ( ( A (0, 1) * A (1, 2) - A (0, 2) * A (1, 1)) * b2
214 + (-A (0, 1) * A (2, 2) + A (0, 2) * A (2, 1)) * b1
215 + ( A (1, 1) * A (2, 2) - A (1, 2) * A (2, 1)) * b0) * factor;
216
217 x[1] = -( ( A (0, 0) * A (1, 2) - A (0, 2) * A (1, 0)) * b2
218 + (-A (0, 0) * A (2, 2) + A (0, 2) * A (2, 0)) * b1
219 + ( A (1, 0) * A (2, 2) - A (1, 2) * A (2, 0)) * b0) * factor;
220
221 x[2] = ( ( A (0, 0) * A (1, 1) - A (0, 1) * A (1, 0)) * b2
222 + (-A (0, 0) * A (2, 1) + A (0, 1) * A (2, 0)) * b1
223 + ( A (1, 0) * A (2, 1) - A (1, 1) * A (2, 0)) * b0) * factor;
224 }
225 break;
226
227
228 default:
229 {
231
232 for (size_t j = 0; j < n; ++j)
233 {
234 if (M (j, j) == 0)
235 {
236 auto i = j;
237 while (i < n && M (i, j) == 0)
238 ++i;
239
240 if (i == n)
241 return false;
242
243 for (size_t k = 0; k < n; ++k)
244 M (j, k) += M (i, k);
245
246 x[j] += x[i];
247 }
248
249 auto t = 1 / M (j, j);
250
251 for (size_t k = 0; k < n; ++k)
252 M (j, k) *= t;
253
254 x[j] *= t;
255
256 for (size_t k = j + 1; k < n; ++k)
257 {
258 auto u = -M (k, j);
259
260 for (size_t l = 0; l < n; ++l)
261 M (k, l) += u * M (j, l);
262
263 x[k] += u * x[j];
264 }
265 }
266
267 for (int k = static_cast<int> (n) - 2; k >= 0; --k)
268 for (size_t i = static_cast<size_t> (k) + 1; i < n; ++i)
269 x[k] -= M (static_cast<size_t> (k), i) * x[i];
270 }
271 }
272
273 return true;
274}
275
276//==============================================================================
277template <typename ElementType>
279{
280 StringArray entries;
281 int sizeMax = 0;
282
283 auto* p = data.begin();
284
285 for (size_t i = 0; i < rows; ++i)
286 {
287 for (size_t j = 0; j < columns; ++j)
288 {
289 String entry (*p++, 4);
290 sizeMax = jmax (sizeMax, entry.length());
291
292 entries.add (entry);
293 }
294 }
295
296 sizeMax = ((sizeMax + 1) / 4 + 1) * 4;
297
299
300 auto n = static_cast<size_t> (entries.size());
301
302 for (size_t i = 0; i < n; ++i)
303 {
304 result << entries[(int) i].paddedRight (' ', sizeMax);
305
306 if (i % columns == (columns - 1))
307 result << newLine;
308 }
309
310 return result.toString();
311}
312
313template class Matrix<float>;
314template class Matrix<double>;
315
316} // namespace dsp
317} // namespace juce
Type jmax(const Type a, const Type b)
Definition MathsFunctions.h:48
uint8_t a
Definition Spc_Cpu.h:141
Definition juce_MemoryOutputStream.h:36
Definition juce_StringArray.h:35
int size() const noexcept
Definition juce_StringArray.h:136
void add(String stringToAdd)
Definition juce_StringArray.cpp:136
Definition juce_String.h:53
int length() const noexcept
Definition juce_String.cpp:511
Definition juce_Matrix.h:41
Matrix & swapRows(size_t rowOne, size_t rowTwo) noexcept
Definition juce_Matrix.cpp:96
size_t getNumRows() const noexcept
Definition juce_Matrix.h:92
static bool compare(const Matrix &a, const Matrix &b, ElementType tolerance=0) noexcept
Definition juce_Matrix.cpp:148
Matrix & swapColumns(size_t columnOne, size_t columnTwo) noexcept
Definition juce_Matrix.cpp:80
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
size_t columns
Definition juce_Matrix.h:247
Array< size_t > dataAcceleration
Definition juce_Matrix.h:245
static Matrix identity(size_t size)
Definition juce_Matrix.cpp:32
bool solve(Matrix &b) const noexcept
Definition juce_Matrix.cpp:165
static Matrix toeplitz(const Matrix &vector, size_t size)
Definition juce_Matrix.cpp:43
ElementType * getRawDataPointer() noexcept
Definition juce_Matrix.h:130
size_t getNumColumns() const noexcept
Definition juce_Matrix.h:95
size_t rows
Definition juce_Matrix.h:247
String toString() const
Definition juce_Matrix.cpp:278
Array< ElementType > data
Definition juce_Matrix.h:244
int * l
Definition inflate.c:1579
unsigned * m
Definition inflate.c:1559
struct huft * t
Definition inflate.c:943
register unsigned k
Definition inflate.c:946
register unsigned j
Definition inflate.c:1576
struct huft * u[BMAX]
Definition inflate.c:1583
register unsigned i
Definition inflate.c:1575
unsigned x[BMAX+1]
Definition inflate.c:1586
#define jassert(expression)
#define A(x)
Definition lice_arc.cpp:13
Definition juce_AudioBlock.h:29
Definition carla_juce.cpp:31
NewLine newLine
Definition juce_String.cpp:28
#define M
Definition nseel-cfunc.c:37
int n
Definition crypt.c:458
uch * p
Definition crypt.c:594
b
Definition crypt.c:628
ulg size
Definition extract.c:2350
int result
Definition process.c:1455
typedef int(UZ_EXP MsgFn)()