A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
matrix-array.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Centre Tecnologic de Telecomunicacions de Catalunya (CTTC)
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Biljana Bojovic <bbojovic@cttc.es>
7 */
8
9#include "matrix-array.h"
10
11#ifdef HAVE_EIGEN3
12#include <Eigen/Dense>
13#endif
14
15namespace ns3
16{
17
18#ifdef HAVE_EIGEN3
19template <class T>
20using EigenMatrix = Eigen::Map<Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>;
21template <class T>
22using ConstEigenMatrix = Eigen::Map<const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>>;
23#endif
24
25template <class T>
26MatrixArray<T>::MatrixArray(size_t numRows, size_t numCols, size_t numPages)
27 : ValArray<T>(numRows, numCols, numPages)
28{
29}
30
31template <class T>
32MatrixArray<T>::MatrixArray(const std::valarray<T>& values)
33 : ValArray<T>(values)
34{
35}
36
37template <class T>
38MatrixArray<T>::MatrixArray(std::valarray<T>&& values)
39 : ValArray<T>(std::move(values))
40{
41}
42
43template <class T>
44MatrixArray<T>::MatrixArray(const std::vector<T>& values)
45 : ValArray<T>(values)
46{
47}
48
49template <class T>
50MatrixArray<T>::MatrixArray(size_t numRows, size_t numCols, const std::valarray<T>& values)
51 : ValArray<T>(numRows, numCols, values)
52{
53}
54
55template <class T>
56MatrixArray<T>::MatrixArray(size_t numRows, size_t numCols, std::valarray<T>&& values)
57 : ValArray<T>(numRows, numCols, std::move(values))
58{
59}
60
61template <class T>
63 size_t numCols,
64 size_t numPages,
65 const std::valarray<T>& values)
66 : ValArray<T>(numRows, numCols, numPages, values)
67{
68}
69
70template <class T>
72 size_t numCols,
73 size_t numPages,
74 std::valarray<T>&& values)
75 : ValArray<T>(numRows, numCols, numPages, std::move(values))
76{
77}
78
79template <class T>
82{
83 NS_ASSERT_MSG(m_numPages == rhs.m_numPages, "MatrixArrays have different numbers of matrices.");
84 NS_ASSERT_MSG(m_numCols == rhs.m_numRows, "Inner dimensions of matrices mismatch.");
85
86 MatrixArray<T> res{m_numRows, rhs.m_numCols, m_numPages};
87
88 for (size_t page = 0; page < res.m_numPages; ++page)
89 {
90#ifdef HAVE_EIGEN3 // Eigen found and enabled Eigen optimizations
91
92 ConstEigenMatrix<T> lhsEigenMatrix(GetPagePtr(page), m_numRows, m_numCols);
93 ConstEigenMatrix<T> rhsEigenMatrix(rhs.GetPagePtr(page), rhs.m_numRows, rhs.m_numCols);
94 EigenMatrix<T> resEigenMatrix(res.GetPagePtr(page), res.m_numRows, res.m_numCols);
95 resEigenMatrix = lhsEigenMatrix * rhsEigenMatrix;
97#else // Eigen not found or Eigen optimizations not enabled
98
99 size_t matrixOffset = page * m_numRows * m_numCols;
100 size_t rhsMatrixOffset = page * rhs.m_numRows * rhs.m_numCols;
101 for (size_t i = 0; i < res.m_numRows; ++i)
103 for (size_t j = 0; j < res.m_numCols; ++j)
104 {
105 res(i, j, page) = (m_values[std::slice(matrixOffset + i, m_numCols, m_numRows)] *
106 rhs.m_values[std::slice(rhsMatrixOffset + j * rhs.m_numRows,
107 rhs.m_numRows,
108 1)])
109 .sum();
111 }
112
113#endif
114 }
115 return res;
116}
117
118template <class T>
121{
122 // Create the matrix where m_numRows = this.m_numCols, m_numCols = this.m_numRows,
123 // m_numPages = this.m_numPages
124 MatrixArray<T> res{m_numCols, m_numRows, m_numPages};
125
126 for (size_t page = 0; page < m_numPages; ++page)
128#ifdef HAVE_EIGEN3 // Eigen found and Eigen optimizations enabled
129
130 ConstEigenMatrix<T> thisMatrix(GetPagePtr(page), m_numRows, m_numCols);
131 EigenMatrix<T> resEigenMatrix(res.GetPagePtr(page), res.m_numRows, res.m_numCols);
132 resEigenMatrix = thisMatrix.transpose();
133
134#else // Eigen not found or Eigen optimizations not enabled
135
136 size_t matrixIndex = page * m_numRows * m_numCols;
137 for (size_t i = 0; i < m_numRows; ++i)
138 {
139 res.m_values[std::slice(matrixIndex + i * res.m_numRows, res.m_numRows, 1)] =
140 m_values[std::slice(matrixIndex + i, m_numCols, m_numRows)];
141 }
142
143#endif
144 }
145 return res;
146}
147
148template <class T>
149MatrixArray<T>
151{
152 MatrixArray<T> res{1, 1, m_numPages};
153 NS_ASSERT_MSG(m_numRows == m_numCols, "Matrix is not square");
154 // In case of small matrices, we use a fast path
155 if (m_numRows == 1)
156 {
157 return *this;
158 }
159 // Calculate determinant for each matrix
160 for (size_t page = 0; page < m_numPages; ++page)
161 {
162 res(0, 0, page) = 0;
163 auto pageValues = GetPagePtr(page);
164
165 // Fast path for 2x2 matrices
166 if (m_numRows == 2)
167 {
168 res(0, 0, page) = pageValues[0] * pageValues[3] - pageValues[1] * pageValues[2];
169 continue;
170 }
171 for (size_t detN = 0; detN < m_numRows; detN++)
172 {
173 auto partDetP = T{0} + 1.0;
174 auto partDetN = T{0} + 1.0;
175 for (size_t row = 0; row < m_numRows; row++)
176 {
177 // Wraparound not to have to extend the matrix
178 // Positive determinant
179 size_t col = (row + detN) % m_numCols;
180 partDetP *= pageValues[row * m_numCols + col];
181
182 // Negative determinant
183 col = m_numCols - 1 - (row + detN) % m_numCols;
184 partDetN *= pageValues[row * m_numCols + col];
186 res(0, 0, page) += partDetP - partDetN;
187 }
188 }
189 return res;
191
192template <class T>
195{
196 MatrixArray<T> res{1, 1, m_numPages};
197 for (size_t page = 0; page < m_numPages; ++page)
198 {
199 // Calculate the sum of squared absolute values of each matrix page
200 res[page] = 0;
201 auto pagePtr = this->GetPagePtr(page);
202 for (size_t i = 0; i < m_numRows * m_numCols; i++)
203 {
204 auto absVal = std::abs(pagePtr[i]);
205 res[page] += absVal * absVal;
206 }
207 res[page] = sqrt(res[page]);
208 }
209 return res;
210}
211
212template <class T>
215 const MatrixArray<T>& rMatrix) const
216{
217 NS_ASSERT_MSG(lMatrix.m_numPages == 1 && rMatrix.m_numPages == 1,
218 "The left and right MatrixArray should have only one page.");
219 NS_ASSERT_MSG(lMatrix.m_numCols == m_numRows,
220 "Left vector numCols and this MatrixArray numRows mismatch.");
221 NS_ASSERT_MSG(m_numCols == rMatrix.m_numRows,
222 "Right vector numRows and this MatrixArray numCols mismatch.");
223
224 MatrixArray<T> res{lMatrix.m_numRows, rMatrix.m_numCols, m_numPages};
225
226#ifdef HAVE_EIGEN3
227
228 ConstEigenMatrix<T> lMatrixEigen(lMatrix.GetPagePtr(0), lMatrix.m_numRows, lMatrix.m_numCols);
229 ConstEigenMatrix<T> rMatrixEigen(rMatrix.GetPagePtr(0), rMatrix.m_numRows, rMatrix.m_numCols);
230#endif
231
232 for (size_t page = 0; page < m_numPages; ++page)
233 {
234#ifdef HAVE_EIGEN3 // Eigen found and Eigen optimizations enabled
235
236 ConstEigenMatrix<T> matrixEigen(GetPagePtr(page), m_numRows, m_numCols);
237 EigenMatrix<T> resEigenMap(res.GetPagePtr(page), res.m_numRows, res.m_numCols);
238
239 resEigenMap = lMatrixEigen * matrixEigen * rMatrixEigen;
240
241#else // Eigen not found or Eigen optimizations not enabled
242
243 size_t matrixOffset = page * m_numRows * m_numCols;
244 for (size_t resRow = 0; resRow < res.m_numRows; ++resRow)
245 {
246 for (size_t resCol = 0; resCol < res.m_numCols; ++resCol)
247 {
248 // create intermediate row result, a multiply of resRow row of lMatrix and each
249 // column of this matrix
250 std::valarray<T> interRes(m_numCols);
251 for (size_t thisCol = 0; thisCol < m_numCols; ++thisCol)
252 {
253 interRes[thisCol] =
254 (lMatrix
255 .m_values[std::slice(resRow, lMatrix.m_numCols, lMatrix.m_numRows)] *
256 m_values[std::slice(matrixOffset + thisCol * m_numRows, m_numRows, 1)])
257 .sum();
258 }
259 // multiply intermediate results and resCol column of the rMatrix
260 res(resRow, resCol, page) =
261 (interRes *
262 rMatrix.m_values[std::slice(resCol * rMatrix.m_numRows, rMatrix.m_numRows, 1)])
263 .sum();
264 }
265 }
266#endif
267 }
268 return res;
269}
270
271template <class T>
272template <bool EnableBool, typename>
273MatrixArray<T>
275{
276 MatrixArray<std::complex<double>> retMatrix = this->Transpose();
277
278 for (size_t index = 0; index < this->GetSize(); ++index)
279 {
280 retMatrix.m_values[index] = std::conj(retMatrix.m_values[index]);
281 }
282 return retMatrix;
283}
284
285template <class T>
287MatrixArray<T>::MakeNCopies(size_t nCopies) const
288{
289 NS_ASSERT_MSG(m_numPages == 1, "The MatrixArray should have only one page to be copied.");
290 auto copiedMatrix = MatrixArray<T>{m_numRows, m_numCols, nCopies};
291 for (size_t copy = 0; copy < nCopies; copy++)
292 {
293 for (size_t i = 0; i < m_numRows * m_numCols; i++)
294 {
295 copiedMatrix.GetPagePtr(copy)[i] = m_values[i];
296 }
297 }
298 return copiedMatrix;
299}
300
301template <class T>
304{
305 NS_ASSERT_MSG(page < m_numPages, "The page to extract from the MatrixArray is out of bounds.");
306 auto extractedPage = MatrixArray<T>{m_numRows, m_numCols, 1};
307
308 for (size_t i = 0; i < m_numRows * m_numCols; ++i)
309 {
310 extractedPage.m_values[i] = GetPagePtr(page)[i];
311 }
312 return extractedPage;
313}
314
315template <class T>
318{
319 auto jointMatrix =
320 MatrixArray<T>{pages.front().GetNumRows(), pages.front().GetNumCols(), pages.size()};
321 for (size_t page = 0; page < jointMatrix.GetNumPages(); page++)
322 {
323 NS_ASSERT_MSG(pages[page].GetNumRows() == jointMatrix.GetNumRows(),
324 "All page matrices should have the same number of rows");
325 NS_ASSERT_MSG(pages[page].GetNumCols() == jointMatrix.GetNumCols(),
326 "All page matrices should have the same number of columns");
327 NS_ASSERT_MSG(pages[page].GetNumPages() == 1,
328 "All page matrices should have a single page");
329
330 size_t i = 0;
331 for (auto a : pages[page].GetValues())
332 {
333 jointMatrix.GetPagePtr(page)[i] = a;
334 i++;
335 }
336 }
337 return jointMatrix;
338}
339
340template <class T>
342MatrixArray<T>::IdentityMatrix(const size_t size, const size_t pages)
343{
344 auto identityMatrix = MatrixArray<T>{size, size, pages};
345 for (std::size_t page = 0; page < pages; page++)
346 {
347 for (std::size_t i = 0; i < size; i++)
348 {
349 identityMatrix(i, i, page) = 1.0;
350 }
351 }
352 return identityMatrix;
353}
354
355template <class T>
358{
359 NS_ASSERT_MSG(likeme.GetNumRows() == likeme.GetNumCols(), "Template array is not square.");
360 return IdentityMatrix(likeme.GetNumRows(), likeme.GetNumPages());
361}
362
364 const;
366template class MatrixArray<double>;
367template class MatrixArray<int>;
368
369} // namespace ns3
MatrixArray class inherits ValArray class and provides additional interfaces to ValArray which enable...
MatrixArray< T > HermitianTranspose() const
Function that performs the Hermitian transpose of this MatrixArray and returns a new matrix that is t...
MatrixArray operator*(const T &rhs) const
Element-wise multiplication with a scalar value.
MatrixArray Determinant() const
This operator calculates a vector o determinants, one for each page.
static MatrixArray< T > IdentityMatrix(const size_t size, const size_t pages=1)
Function produces an identity MatrixArray with the specified size.
static MatrixArray< T > JoinPages(const std::vector< MatrixArray< T > > &pages)
Function joins multiple pages into a single MatrixArray.
MatrixArray Transpose() const
This operator interprets the 3D array as an array of matrices, and performs a linear algebra operatio...
MatrixArray< T > MakeNCopies(size_t nCopies) const
Function that copies the current 1-page matrix into a new matrix with n copies of the original matrix...
MatrixArray()=default
MatrixArray< T > ExtractPage(size_t page) const
Function extracts a page from a MatrixArray.
MatrixArray MultiplyByLeftAndRightMatrix(const MatrixArray< T > &lMatrix, const MatrixArray< T > &rMatrix) const
Multiply each matrix in the array by the left and the right matrix.
MatrixArray FrobeniusNorm() const
This operator calculates a vector of Frobenius norm, one for each page.
ValArray is a class to efficiently store 3D array.
Definition val-array.h:74
T * GetPagePtr(size_t pageIndex)
Get a data pointer to a specific 2D array for use in linear algebra libraries.
Definition val-array.h:516
size_t GetNumPages() const
Definition val-array.h:387
size_t m_numCols
The size of the second dimension, i.e., the number of columns of each 2D array.
Definition val-array.h:361
std::valarray< T > m_values
The data values.
Definition val-array.h:364
size_t GetNumRows() const
Definition val-array.h:373
size_t m_numRows
The size of the first dimension, i.e., the number of rows of each 2D array.
Definition val-array.h:359
size_t GetNumCols() const
Definition val-array.h:380
size_t m_numPages
The size of the third dimension, i.e., the number of 2D arrays.
Definition val-array.h:363
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition assert.h:75
Every class exported by the ns3 library is enclosed in the ns3 namespace.
uint32_t GetSize(Ptr< const Packet > packet, const WifiMacHeader *hdr, bool isAmpdu)
Return the total size of the packet after WifiMacHeader and FCS trailer have been added.
STL namespace.