21#include "ns3/matrix-array.h"
48template <
typename IN,
typename T>
53 if (out.size() != in.size())
55 out.resize(in.size());
59 std::transform(std::begin(in), std::end(in), std::begin(out), [](IN i) {
60 return static_cast<T
>(i);
107 void DoRun()
override;
128 for (
size_t i = 0; i <
m1.GetNumRows(); ++i)
130 for (
size_t j = 0; j <
m1.GetNumCols(); ++j)
142 "The number of rows in resulting matrix is not correct");
145 "The number of cols in resulting matrix is not correct");
148 "The number of rows and cols should be equal");
155 "The element value should be " <<
m1.GetNumCols());
167 "The values are not equal");
189 "The number of rows in resulting matrix is not correct");
192 "The number of cols in resulting matrix is not correct");
195 "The number of rows and cols should be equal");
205 "The element value should be " << m5.
GetNumCols());
220 std::valarray<int> a{0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
221 std::valarray<int> b{0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, 1, 2, 3, 4};
222 std::valarray<T> aCasted(a.size());
223 std::valarray<T> bCasted(b.size());
224 for (
size_t i = 0; i < a.size(); ++i)
226 aCasted[i] =
static_cast<T
>(a[i]);
228 for (
size_t i = 0; i < b.size(); ++i)
230 bCasted[i] =
static_cast<T
>(b[i]);
238 NS_LOG_INFO(
"m8 (5, 3, 1) = m5.Transpose ()" << m8);
245 "Creation of vector is not correct.");
268 a = {0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5};
269 b = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
270 std::valarray<int> c{2, 3, 4, 6, 2, 3, 4, 6};
271 aCasted = std::valarray<T>(a.size());
272 bCasted = std::valarray<T>(b.size());
273 std::valarray<T> cCasted = std::valarray<T>(c.size());
275 for (
size_t i = 0; i < a.size(); ++i)
277 aCasted[i] =
static_cast<T
>(a[i]);
279 for (
size_t i = 0; i < b.size(); ++i)
281 bCasted[i] =
static_cast<T
>(b[i]);
283 for (
size_t i = 0; i < c.size(); ++i)
285 cCasted[i] =
static_cast<T
>(c[i]);
293 "The number of columns is not as expected.");
296 "The number of rows is not as expected.");
306 a = std::valarray<int>(
307 {0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5, 0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5});
308 b = std::valarray<int>({0, 1, 0, 1, 0, 1, 0, 10, 0, 10, 0, 10});
309 c = std::valarray<int>({2, 3, 2, 3, 4, 6, 4, 6, 20, 30, 20, 30, 40, 60, 40, 60});
310 aCasted = std::valarray<T>(a.size());
311 bCasted = std::valarray<T>(b.size());
312 cCasted = std::valarray<T>(c.size());
314 for (
size_t i = 0; i < a.size(); ++i)
316 aCasted[i] =
static_cast<T
>(a[i]);
318 for (
size_t i = 0; i < b.size(); ++i)
320 bCasted[i] =
static_cast<T
>(b[i]);
322 for (
size_t i = 0; i < c.size(); ++i)
324 cCasted[i] =
static_cast<T
>(c[i]);
333 "The number of columns is not as expected.");
336 "The number of rows is not as expected.");
346 a = std::valarray<int>({5, 4, 5, 5, 4, 5});
347 b = std::valarray<int>({0, 1, 0, 1, 0, 1, 1, 2, 3, 10, 100, 1000});
348 c = std::valarray<int>({4, 10, 28, 5450});
349 aCasted = std::valarray<T>(a.size());
350 bCasted = std::valarray<T>(b.size());
351 cCasted = std::valarray<T>(c.size());
353 for (
size_t i = 0; i < a.size(); ++i)
355 aCasted[i] =
static_cast<T
>(a[i]);
357 for (
size_t i = 0; i < b.size(); ++i)
359 bCasted[i] =
static_cast<T
>(b[i]);
361 for (
size_t i = 0; i < c.size(); ++i)
363 cCasted[i] =
static_cast<T
>(c[i]);
372 "The number of columns is not as expected.");
375 "The number of rows is not as expected.");
382 std::valarray<int> d{1, 1, 1};
383 std::valarray<int> e{1, 1};
384 std::valarray<int> f{1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3};
385 std::valarray<int> g{12, 12};
386 std::valarray<T> dCasted(d.size());
387 std::valarray<T> eCasted(e.size());
388 std::valarray<T> fCasted(f.size());
389 std::valarray<T> gCasted(g.size());
390 for (
size_t i = 0; i < d.size(); ++i)
392 dCasted[i] =
static_cast<T
>(d[i]);
394 for (
size_t i = 0; i < e.size(); ++i)
396 eCasted[i] =
static_cast<T
>(e[i]);
398 for (
size_t i = 0; i < f.size(); ++i)
400 fCasted[i] =
static_cast<T
>(f[i]);
402 for (
size_t i = 0; i < g.size(); ++i)
404 gCasted[i] =
static_cast<T
>(g[i]);
414 std::valarray<int> h{1, 3, 2, 2, 4, 0};
415 std::valarray<int> j{2, 2, 3, 4, 1, 3, 0, 5};
416 std::valarray<int> k{1, 2, 0, 0, 2, 3, 4, 1, 2, 3, 4, 1, 1, 2, 0, 0, 2, 3, 4, 1, 2, 3, 4, 1};
417 std::valarray<int> l{144, 132, 128, 104, 144, 132, 128, 104};
418 std::valarray<T> hCasted(h.size());
419 std::valarray<T> jCasted(j.size());
420 std::valarray<T> kCasted(k.size());
421 std::valarray<T> lCasted(l.size());
422 for (
size_t i = 0; i < h.size(); ++i)
424 hCasted[i] =
static_cast<T
>(h[i]);
426 for (
size_t i = 0; i < j.size(); ++i)
428 jCasted[i] =
static_cast<T
>(j[i]);
430 for (
size_t i = 0; i < k.size(); ++i)
432 kCasted[i] =
static_cast<T
>(k[i]);
434 for (
size_t i = 0; i < l.size(); ++i)
436 lCasted[i] =
static_cast<T
>(l[i]);
450 size_t lCastedSize = lCasted.size();
451 NS_LOG_INFO(
"size() of lCasted before move: " << lCasted.size());
456 size_t hCastedSize = hCasted.size();
457 NS_LOG_INFO(
"size() of hCasted before move: " << hCasted.size());
462 size_t jCastedSize = jCasted.size();
463 NS_LOG_INFO(
"size() of jCasted before move: " << jCasted.size());
470 std::vector<std::pair<std::valarray<int>, T>> detTestCases{
472 {{1, 0, 7, 4, 2, 0, 6, 5, 3}, 62},
474 {{1, 4, 6, 0, 2, 5, 7, 0, 3}, 62},
476 {{1, 0, 0, 0, 1, 0, 0, 0, 1}, 1},
478 {{1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, 1},
480 {{0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0}, -1},
482 {{36, -5, -5, 43}, 1523},
486 for (
const auto& [detVal, detRef] : detTestCases)
488 std::valarray<T> detCast(detVal.size());
490 auto side = sqrt(detVal.size());
493 static_cast<T
>(detRef),
494 "The determinants are not equal.");
498 std::valarray<int> multiPageMatrixValues{
529 std::valarray<T> castMultiPageMatrixValues(multiPageMatrixValues.size());
533 multiPageMatrixValues.size() / 9,
534 std::move(castMultiPageMatrixValues));
536 std::vector<int> determinants = {1, -1, 62, 60, -315, 4, 0};
538 for (
size_t page = 0; page < multiPageMatrix.
GetNumPages(); page++)
541 static_cast<T
>(determinants[page]),
542 "The determinants from the page " << std::to_string(page)
543 <<
" are not equal.");
547 std::vector<double> fnorms = {sqrt(3), sqrt(3), 11.8322, 15.3297, 14.7309, 3.4641, 2};
549 for (
size_t page = 0; page < multiPageMatrix.
GetNumPages(); page++)
552 std::abs(
static_cast<T
>(fnorms[page])),
554 "The Frobenius norm from the page " << std::to_string(page)
555 <<
" are not equal.");
559 for (
size_t noOfCopies = 1; noOfCopies < 4; noOfCopies++)
564 "Creating " << std::to_string(noOfCopies) <<
" copies failed.");
567 "The copy doesn't have the same number of rows as the original.");
570 "The copy doesn't have the same number of columns as the original.");
571 for (
size_t page = 0; page < copies.GetNumPages(); page++)
574 for (
size_t row = 0; row < copies.GetNumRows(); row++)
576 for (
size_t col = 0; col < copies.GetNumCols(); col++)
578 diff += m27(row, col, 0) - copies(row, col, page);
586 std::vector<MatrixArray<T>> pages{multiPageMatrix.
ExtractPage(1),
592 for (
size_t page = 0; page < jointPagesMatrix.GetNumPages(); page++)
595 for (
size_t row = 0; row < jointPagesMatrix.GetNumRows(); row++)
597 for (
size_t col = 0; col < jointPagesMatrix.GetNumCols(); col++)
599 diff += multiPageMatrix(row, col, 1 - page) - jointPagesMatrix(row, col, page);
606 auto identityRank3Reference = multiPageMatrix.
ExtractPage(0);
637 void DoRun()
override;
641 :
TestCase(
"ComplexMatrixArrayTestCase")
657 std::valarray<std::complex<double>> complexValarray1 = {
671 std::valarray<std::complex<double>> complexValarray2 = {
MatrixArray class inherits ValArray class and provides additional interfaces to ValArray which enable...
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< 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.
void AddTestCase(TestCase *testCase, Duration duration=Duration::QUICK)
Add an individual child TestCase to this test suite.
const std::valarray< T > & GetValues() const
Returns underlying values.
size_t GetNumPages() const
size_t GetNumRows() const
size_t GetNumCols() const
Test for testing functions that apply to MatrixArrays that use complex numbers, such as HermitianTran...
~ComplexMatrixArrayTestCase() override
Destructor.
ComplexMatrixArrayTestCase()
Constructor.
void DoRun() override
Implementation to actually run this TestCase.
MatrixArray test case for testing constructors, operators and other functions.
MatrixArrayTestCase(MatrixArrayTestCase< T > &&)=default
Move constructor.
MatrixArrayTestCase< T > & operator=(const MatrixArrayTestCase< T > &)=default
Copy assignment operator.
MatrixArrayTestCase()=default
~MatrixArrayTestCase() override
Destructor.
MatrixArrayTestCase(const MatrixArrayTestCase< T > &)=default
Copy constructor.
MatrixArrayTestCase< T > & operator=(MatrixArrayTestCase< T > &&)=default
Move assignment operator.
void DoRun() override
Implementation to actually run this TestCase.
MatrixArrayTestSuite()
Constructor.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
static MatrixArrayTestSuite g_matrixArrayTestSuite
MatrixArrayTestSuite instance variable.
#define NS_TEST_ASSERT_MSG_EQ(actual, limit, msg)
Test that an actual and expected (limit) value are equal and report and abort if not.
#define NS_TEST_ASSERT_MSG_NE(actual, limit, msg)
Test that an actual and expected (limit) value are not equal and report and abort if not.
#define NS_TEST_ASSERT_MSG_EQ_TOL(actual, limit, tol, msg)
Test that actual and expected (limit) values are equal to plus or minus some tolerance and report and...
const double m1
First component modulus, 232 - 209.
const double m2
Second component modulus, 232 - 22853.
void CastStdValarray(const std::valarray< IN > &in, std::valarray< T > &out)
Function casts an input valArray "in" (type IN) to an output valArray "out" (type T)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
MatrixArray< std::complex< double > > ComplexMatrixArray
Create an alias for MatrixArray using complex type.