221 lines
5.1 KiB
C
221 lines
5.1 KiB
C
/// @file
|
|
/// @brief Task2: tests
|
|
|
|
#include "task2.h" // struct Matrix, matrix_mult
|
|
|
|
#include "iue-num/numerics.h" // iuenum_isclose
|
|
|
|
#include <assert.h> // assert
|
|
#include <stdbool.h> // bool, true, false
|
|
#include <stdio.h> // printf
|
|
|
|
// helper function
|
|
bool isclose(const struct Matrix* a, const struct Matrix* b) {
|
|
if (a->m != b->m)
|
|
return false;
|
|
if (a->n != b->n)
|
|
return false;
|
|
for (size_t i = 0; i != a->m; ++i)
|
|
for (size_t j = 0; j != a->n; ++j)
|
|
if (!iuenum_isclose(a->data[j + a->n * i], b->data[j + b->n * i]))
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
int main() {
|
|
|
|
{ // testing 'matrix_zeros' for 3x3
|
|
struct Matrix mat = matrix_zeros(3, 3);
|
|
double data[3][3] = {
|
|
{0, 0, 0},
|
|
{0, 0, 0},
|
|
{0, 0, 0},
|
|
};
|
|
struct Matrix expected = matrix_init(3, 3, &data[0][0]);
|
|
assert(isclose(&mat, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_zeros' for 4x3
|
|
struct Matrix mat = matrix_zeros(4, 3);
|
|
double data[4][3] = {
|
|
{0, 0, 0},
|
|
{0, 0, 0},
|
|
{0, 0, 0},
|
|
{0, 0, 0},
|
|
};
|
|
struct Matrix expected = matrix_init(4, 3, &data[0][0]);
|
|
assert(isclose(&mat, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_identity' for 3x3
|
|
|
|
double data_expected[3][3] = {
|
|
{1, 0, 0},
|
|
{0, 1, 0},
|
|
{0, 0, 1},
|
|
};
|
|
struct Matrix expected = matrix_init(3, 3, &data_expected[0][0]);
|
|
struct Matrix mat = matrix_identity(3);
|
|
|
|
// matrix_print(&mat);
|
|
assert(isclose(&mat, &expected));
|
|
// matrix_print(&expected);
|
|
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_identity' for 4x4
|
|
|
|
double data_expected[4][4] = {
|
|
{1, 0, 0, 0},
|
|
{0, 1, 0, 0},
|
|
{0, 0, 1, 0},
|
|
{0, 0, 0, 1},
|
|
};
|
|
struct Matrix expected = matrix_init(4, 4, &data_expected[0][0]);
|
|
struct Matrix mat = matrix_identity(4);
|
|
|
|
// matrix_print(&mat);
|
|
assert(isclose(&mat, &expected));
|
|
// matrix_print(&expected);
|
|
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_transpose' of a 3x3 identity
|
|
|
|
struct Matrix mat = matrix_identity(3);
|
|
|
|
double data_expected[3][3] = {
|
|
{1, 0, 0},
|
|
{0, 1, 0},
|
|
{0, 0, 1},
|
|
};
|
|
struct Matrix expected = matrix_init(3, 3, &data_expected[0][0]);
|
|
|
|
// matrix_print(&mat);
|
|
matrix_transpose(&mat);
|
|
// matrix_print(&mat);
|
|
// matrix_print(&expected);
|
|
|
|
assert(isclose(&mat, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_transpose' for 4x2
|
|
|
|
double data[4][2] = {
|
|
{11, 12},
|
|
{21, 22},
|
|
{31, 32},
|
|
{41, 42},
|
|
};
|
|
struct Matrix mat = matrix_init(4, 2, &data[0][0]);
|
|
|
|
double data_expected[2][4] = {
|
|
{11, 21, 31, 41},
|
|
{12, 22, 32, 42},
|
|
};
|
|
struct Matrix expected = matrix_init(2, 4, &data_expected[0][0]);
|
|
|
|
// matrix_print(&mat);
|
|
matrix_transpose(&mat);
|
|
// matrix_print(&mat);
|
|
// matrix_print(&expected);
|
|
|
|
assert(isclose(&mat, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
}
|
|
|
|
{ // testing 'matrix_mult' using a left multiplty with a row permuting matrix
|
|
|
|
double data[4][2] = {
|
|
{11, 12},
|
|
{21, 22},
|
|
{31, 32},
|
|
{41, 42},
|
|
};
|
|
struct Matrix mat = matrix_init(4, 2, &data[0][0]);
|
|
|
|
double data_permute[4][4] = {
|
|
{0, 0, 0, 1},
|
|
{0, 0, 1, 0},
|
|
{0, 1, 0, 0},
|
|
{1, 0, 0, 0},
|
|
};
|
|
struct Matrix permute = matrix_init(4, 4, &data_permute[0][0]);
|
|
|
|
double data_expected[4][2] = {
|
|
{41, 42},
|
|
{31, 32},
|
|
{21, 22},
|
|
{11, 12},
|
|
};
|
|
struct Matrix expected = matrix_init(4, 2, &data_expected[0][0]);
|
|
|
|
struct Matrix product = matrix_zeros(permute.m, mat.n);
|
|
|
|
// matrix_print(&permute);
|
|
// matrix_print(&mat);
|
|
matrix_mult(&permute, &mat, &product); // left multiply with permuation matrix
|
|
// matrix_print(&product);
|
|
|
|
assert(isclose(&product, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
matrix_clear(&permute);
|
|
matrix_clear(&product);
|
|
}
|
|
|
|
|
|
{ // testing 'matrix_mult' using a right multiplty with a column permuting matrix
|
|
|
|
double data[4][2] = {
|
|
{11, 12},
|
|
{21, 22},
|
|
{31, 32},
|
|
{41, 42},
|
|
};
|
|
struct Matrix mat = matrix_init(4, 2, &data[0][0]);
|
|
|
|
double data_permute[2][2] = {
|
|
{0, 1},
|
|
{1, 0},
|
|
};
|
|
struct Matrix permute = matrix_init(2, 2, &data_permute[0][0]);
|
|
|
|
double data_expected[4][2] = {
|
|
{12, 11},
|
|
{22, 21},
|
|
{32, 31},
|
|
{42, 41},
|
|
};
|
|
struct Matrix expected = matrix_init(4, 2, &data_expected[0][0]);
|
|
|
|
struct Matrix product = matrix_zeros(mat.m, permute.n);
|
|
|
|
// matrix_print(&permute);
|
|
// matrix_print(&mat);
|
|
matrix_mult(&mat, &permute, &product); // right multiply with permuation matrix
|
|
// matrix_print(&product);
|
|
|
|
assert(isclose(&product, &expected));
|
|
matrix_clear(&mat);
|
|
matrix_clear(&expected);
|
|
matrix_clear(&permute);
|
|
matrix_clear(&product);
|
|
}
|
|
|
|
printf("task2.test.c: all asserts passed\n");
|
|
|
|
return 0;
|
|
}
|