forked from sivabenepoivediamo/musicplusplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
253 lines (195 loc) · 8.6 KB
/
matrix.cpp
File metadata and controls
253 lines (195 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
* @file matrix.cpp
* @brief Example: matrix utilities (modal, transposition, rototranslation)
*
* Exercises `ModalMatrix`, `TranspositionMatrix` and related routines and prints results.
*
* @example
*/
#include "../src/matrix.h"
using namespace std;
// Helper function to print a separator
void printSeparator(const string& title) {
cout << "\n" << string(60, '=') << "\n";
cout << " " << title << "\n";
cout << string(60, '=') << "\n";
}
// Test ModalMatrix with IntervalVector
void testModalMatrixIntervalVector() {
printSeparator("Testing ModalMatrix<IntervalVector>");
// Create a major scale interval pattern: [2, 2, 1, 2, 2, 2, 1]
IntervalVector majorScale({2, 2, 1, 2, 2, 2, 1});
cout << "Input IntervalVector (Major Scale): " << majorScale << "\n\n";
ModalMatrix<IntervalVector> mm = modalMatrix(majorScale);
cout << "Modal Matrix (all rotations):\n";
cout << mm;
cout << "\nUtility methods:\n";
cout << "Matrix size: " << mm.size() << "\n";
cout << "Is empty: " << (mm.empty() ? "yes" : "no") << "\n";
auto vectors = mm.getVectors();
auto indices = mm.getIndices();
cout << "Number of vectors extracted: " << vectors.size() << "\n";
cout << "Number of indices extracted: " << indices.size() << "\n";
}
// Test ModalMatrix with PositionVector
void testModalMatrixPositionVector() {
printSeparator("Testing ModalMatrix<PositionVector>");
// Create C major scale: [0, 2, 4, 5, 7, 9, 11]
PositionVector cMajor({0, 2, 4, 5, 7, 9, 11}, 12);
cout << "Input PositionVector (C Major Scale): " << cMajor << "\n\n";
ModalMatrix<PositionVector> mm = modalMatrix(cMajor);
cout << "Modal Matrix (all rotations):\n";
cout << mm;
}
// Test TranspositionMatrix
void testTranspositionMatrix() {
printSeparator("Testing TranspositionMatrix");
// Create C major scale: [0, 2, 4, 5, 7, 9, 11]
PositionVector cMajor({0, 2, 4, 5, 7, 9, 11}, 12);
cout << "Input PositionVector (C Major Scale): " << cMajor << "\n\n";
TranspositionMatrix tm = transpositionMatrix(cMajor);
cout << "Transposition Matrix (all 12 transpositions):\n";
cout << tm;
cout << "\nUtility methods:\n";
auto transpositions = tm.getTranspositions();
cout << "All transposition indices: [";
for (size_t i = 0; i < transpositions.size(); ++i) {
cout << transpositions[i];
if (i < transpositions.size() - 1) cout << ", ";
}
cout << "]\n";
}
// Test RototranslationMatrix
void testRototranslationMatrix() {
printSeparator("Testing RototranslationMatrix");
// Create a simple triad
PositionVector triad({0, 4, 7}, 12);
int center = 0;
cout << "Input PositionVector: " << triad;
cout << "\nCenter: " << center << "\n\n";
RototranslationMatrix rtm = rototranslationMatrix(triad, center);
cout << "Rototranslation Matrix:\n";
cout << rtm;
cout << "\nUtility methods:\n";
cout << "Center used: " << rtm.getCenter() << "\n";
cout << "Matrix size: " << rtm.size() << "\n";
}
// Test ModalSelectionMatrix with IntervalVector
void testModalSelectionIntervalVector() {
printSeparator("Testing ModalSelectionMatrix<IntervalVector>");
// Source: major scale intervals
IntervalVector source({2, 2, 1, 2, 2, 2, 1});
// Criterion: triad pattern [2, 2, 3]
IntervalVector criterion({2, 2, 3});
int degree = 0;
cout << "Source: " << source;
cout << "\nCriterion: " << criterion;
cout << "\nDegree: " << degree << "\n\n";
ModalSelectionMatrix<IntervalVector> msm = modalSelection(source, criterion, degree);
cout << "Modal Selection Matrix:\n";
cout << msm;
cout << "\nUtility methods:\n";
auto chords = msm.getChords();
auto modeIndices = msm.getModeIndices();
cout << "Number of chords: " << chords.size() << "\n";
cout << "Mode indices: [";
for (size_t i = 0; i < modeIndices.size(); ++i) {
cout << modeIndices[i];
if (i < modeIndices.size() - 1) cout << ", ";
}
cout << "]\n";
}
// Test ModalSelectionMatrix with PositionVector
void testModalSelectionPositionVector() {
printSeparator("Testing ModalSelectionMatrix<PositionVector>");
// Source: C major scale
PositionVector source({0, 2, 4, 5, 7, 9, 11}, 12);
// Criterion: triad pattern [2, 2, 3]
IntervalVector criterion({2, 2, 3});
int degree = 0;
cout << "Source: " << source;
cout << "\nCriterion: " << criterion;
cout << "\nDegree: " << degree << "\n\n";
ModalSelectionMatrix<PositionVector> msm = modalSelection(source, criterion, degree);
cout << "Modal Selection Matrix:\n";
cout << msm;
}
// Test ModalRototranslationMatrix
void testModalRototranslation() {
printSeparator("Testing ModalRototranslationMatrix");
// Source: C major scale
PositionVector source({0, 2, 4, 5, 7, 9, 11}, 12);
// Criterion: triad pattern [2, 2, 3]
IntervalVector criterion({2, 2, 3});
int degree = 0;
cout << "Source: " << source;
cout << "\nCriterion: " << criterion;
cout << "\nDegree: " << degree << "\n\n";
ModalSelectionMatrix<PositionVector> msm = modalSelection(source, criterion, degree);
ModalRototranslationMatrix<PositionVector> mrtm = modalRototranslation(msm);
cout << "Modal Rototranslation Matrix:\n";
cout << mrtm;
cout << "\nUtility methods:\n";
cout << "Number of modes: " << mrtm.size() << "\n";
cout << "Total vector count: " << mrtm.getTotalVectorCount() << "\n";
}
// Test iterator usage
void testIterators() {
printSeparator("Testing Iterator Support");
IntervalVector iv({2, 2, 1, 2, 2, 2, 1});
ModalMatrix<IntervalVector> mm = modalMatrix(iv);
cout << "Using range-based for loop:\n";
int count = 1;
for (const auto& [rotated, index] : mm) {
cout << "Mode " << count++ << " (index " << index << "): " << rotated << "\n";
}
}
void testFilters(){
printSeparator("Testing Filter Functions");
// Use C major as source
PositionVector cMajor({0,2,4,5,7,9,11}, 12);
cout << "Source (C major): " << cMajor << "\n\n";
// Create modal and transposition matrices
ModalMatrix<PositionVector> mm = modalMatrix(cMajor);
TranspositionMatrix tm = transpositionMatrix(cMajor);
cout << "Full Modal Matrix:\n" << mm << "\n";
cout << "Full Transposition Matrix:\n" << tm << "\n";
// Filter by notes (C major triad: 0,4,7)
vector<int> notes = {63};
cout << "Filtering for notes: [63]\n\n";
// Non-destructive filtering
ModalMatrix<PositionVector> modalFiltered = filterModalMatrix(mm, notes);
TranspositionMatrix transFiltered = filterTranspositionMatrix(tm, notes);
cout << "Modal Matrix (filtered, non-destructive):\n" << modalFiltered << "\n";
cout << "Transposition Matrix (filtered, non-destructive):\n" << transFiltered << "\n";
// In-place filtering
ModalMatrix<PositionVector> mmCopy = mm;
TranspositionMatrix tmCopy = tm;
filterModalMatrixInPlace(mmCopy, notes);
filterTranspositionMatrixInPlace(tmCopy, notes);
cout << "Modal Matrix (after in-place filter):\n" << mmCopy << "\n";
cout << "Transposition Matrix (after in-place filter):\n" << tmCopy << "\n";
}
int main() {
cout << "\n";
cout << "╔═══════════════════════════════════════════════════════════╗\n";
cout << "║ MATRIX CLASSES TEST ║\n";
cout << "╚═══════════════════════════════════════════════════════════╝\n";
try {
testModalMatrixIntervalVector();
testModalMatrixPositionVector();
testTranspositionMatrix();
testRototranslationMatrix();
testModalSelectionIntervalVector();
testModalSelectionPositionVector();
testModalRototranslation();
testIterators();
testFilters();
printSeparator("ALL TESTS COMPLETED SUCCESSFULLY");
cout << "\n✓ All matrix classes working correctly!\n\n";
} catch (const exception& e) {
cerr << "\nError during testing: " << e.what() << "\n";
return 1;
}
return 0;
}