#include "Test.h"
class MatrixTestCase : public Test
{
bool TestMatrixMultiplication() {
double v = 2.0;
auto D = A * v;
for(size_t i = 0; i < D.rows(); ++i) {
for(size_t j = 0; j < D.columns(); ++j) { assert(D(i, j) == 2.0); }
}
D = A;
D *= v;
for(size_t i = 0; i < D.rows(); ++i) {
for(size_t j = 0; j < D.columns(); ++j) { assert(D(i, j) == 2.0); }
}
C* C2;
C3 = C * C2;
for(
size_t i = 0; i < C3.
rows(); ++i) {
for(
size_t j = 0; j < C3.
columns(); ++j) { assert(C3(i, j) == 4.0); }
}
auto E = A * B;
assert(E.columns() == 3);
assert(E.rows() == 3);
for(size_t i = 0; i < E.rows(); ++i) {
for(size_t j = 0; j < E.columns(); ++j) { assert(E(i, j) == 4); }
}
auto resVec = vec *
vec2;
assert(resVec.rows() == 10);
assert(resVec.columns() == 10);
auto resVec2 =
vec2 * vec;
assert(resVec2.rows() == 1);
assert(resVec2.columns() == 1);
assert(resA == vec2b);
assert(resB == mat2B);
return true;
}
bool TestMatrixDivision() {
AssertEqual(1.0 / A, B);
AssertEqual(A / 2, C);
AssertEqual(1 / A, B);
AssertEqual(A / 2.0f, C);
AssertEqual(E / F, 2 * E);
return true;
}
bool TestMatrixAddition() {
auto D = A + B;
assert(D == E);
C += A;
assert(C == B);
assert((
vec2 + vec2b) == vec2c);
AssertEqual(foo, F);
AssertEqual(A + vec2c, E);
return true;
}
bool TestHadamardMultiplication() {
assert(C == B);
assert(D.HadamardMulti(E) == resultA);
D.HadamardMulti(E);
assert(D == resultB);
assert(resD2E == resultA);
return true;
}
bool TestKroneckerMultiplication() {
assert(resA == C);
Matrix<double> C2({ { 0, 5, 0, 10 }, { 6, 7, 12, 14 }, { 0, 15, 0, 20 }, { 18, 21, 24, 28 } });
assert(resB == C2);
{ { 7, 8, 14, 16 }, { 9, 0, 18, 0 }, { 21, 24, 28, 32 }, { 27, 0, 36, 0 }, { 35, 40, 42, 48 }, { 45, 0, 54, 0 } });
assert(resC == C3);
assert(resC2 == resC && resC2 == C3);
return true;
}
bool TestHorizontalConcat() {
assert(res1 == C);
assert(res2 == C);
return true;
}
bool TestMatrixCompare() {
assert(A == B);
assert(A != C);
assert(F != G);
assert(F.Determinant() == G.Determinant());
AssertTrue(A < B);
AssertTrue(B < A);
AssertTrue(A > B);
AssertTrue(B > A);
AssertTrue(A > C);
AssertFalse(A < C);
AssertTrue(C < A);
AssertFalse(C > A);
AssertTrue(G > F);
return true;
}
bool TestMatrixDeterminant() {
Matrix<double> D({ { 9, 5, 2, 5 }, { 9, 5, 3, 7 }, { 6, 5, 4, 8 }, { 1, 5, 3, 7 } });
assert(A.Determinant() == 1.0);
assert(B.Determinant() == -1.0);
assert(C.Determinant() == -164);
assert(D.Determinant() == -40);
assert(E.Determinant() == -2);
return true;
}
bool TestMatrixTranspose() {
assert(
vec2.columns() == vec.rows());
assert(
vec2.rows() == vec.columns());
for(
size_t i = 0; i < 3; ++i) { AssertEqual(vec(0, i),
vec2(i, 0)); }
AssertEqual(B.Transpose(), C);
AssertEqual(D.Transpose(), E);
return true;
}
bool TestMatrixInit() {
assert(matrix.
rows() == 2);
Matrix<double> m2({ { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } });
assert(m2.rows() == 2);
assert(m2.columns() == 11);
assert(m3.rows() == 2);
assert(m3.columns() == 1);
for(size_t i = 0; i < 2; i++) {
for(size_t j = 0; j < 2; j++) { assert(m5(i, j) == 1.0); }
}
for(size_t i = 0; i < 2; i++) {
for(size_t j = 0; j < 2; j++) { assert(m6(i, j) == 1.0); }
}
AssertEqual(foo(0, 1), 2.0);
AssertEqual(bar(0, 1), 2.0f);
AssertEqual(baz(0, 2), 3);
return true;
}
bool TestWhere() {
std::function<bool(double)> cond = [](double i) { return bool(i == 1); };
auto B =
where(cond, A, { { 1, 1 }, { 1, 1 }, { 1, 1 } }, { { 0, 0 }, { 0, 0 }, { 0, 0 } });
assert(B == A);
return true;
}
bool TestGetIndex() {
AssertEqual(B.GetIndex(0, 0), 0);
AssertEqual(B.GetIndex(0, 1), 1);
AssertEqual(B.GetIndex(1, 0), 2);
AssertEqual(B.GetIndex(1, 1), 3);
AssertEqual(C.GetIndex(0, 0, 0), 0);
AssertEqual(C.GetIndex(0, 0, 1), 1);
AssertEqual(C.GetIndex(0, 1, 0), 2);
AssertEqual(C.GetIndex(0, 1, 1), 3);
AssertEqual(C.GetIndex(1, 0, 0), 4);
AssertEqual(C.GetIndex(1, 0, 1), 5);
AssertEqual(C.GetIndex(1, 1, 0), 6);
AssertEqual(C.GetIndex(1, 1, 1), 7);
return true;
}
bool TestArgMinMax() {
return true;
}
bool TestMax() {
return true;
}
bool TestCorr() {
return true;
}
bool TestRandom() {
return true;
}
bool TestFromVPtr() {
float in[4] = { 1, 2, 3, 4 };
assert(A == B);
return true;
}
bool TestVectorAccess() {
Matrix<int> A({ { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } });
auto B = A(0);
assert(B == C);
return true;
}
bool TestGetSlice() {
Matrix<int> A({ { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } });
AssertEqual(A.GetSlice(1, 2, 1, 2), B);
AssertEqual(A.GetSlice(1, 2, 1), B);
AssertEqual(C.GetSlice(0, 0), D);
AssertEqual(C.GetSlice(0, 0, 0), D);
AssertEqual(C.GetSlice(0, 0, 0, 1), D);
AssertEqual(C(0), D);
AssertEqual(C.GetSlice(1, 1), E);
AssertEqual(C.GetSlice(1, 1, 0), E);
AssertEqual(C.GetSlice(1, 1, 0, 1), E);
AssertEqual(C(1), E);
return true;
}
bool TestGetComponents() {
AssertEqual(A.GetComponents(0), A);
return true;
}
bool TestApply() {
auto C = A.
Apply([](
double val) {
return val * 2; });
auto E = D.
Apply([](
double val) {
return val * val; });
AssertEqual(A, B);
AssertEqual(C, D);
AssertEqual(E, F);
AssertEqual(2 * D, E);
return true;
}
bool TestUnique() {
return true;
}
bool TestMean() {
Matrix<double> A = { { 0., 1. }, { 0., 2. }, { 1., 1. }, { 1., 0. }, { 0., 1. } };
AssertEqual(
mean(A, 0), C);
AssertEqual(
mean(A, 1), D);
return true;
}
public:
void run() override {
TestMatrixInit();
TestMatrixMultiplication();
TestMatrixDivision();
TestMatrixAddition();
TestMatrixCompare();
TestMatrixDeterminant();
TestMatrixTranspose();
TestHadamardMultiplication();
TestKroneckerMultiplication();
TestHorizontalConcat();
TestWhere();
TestGetIndex();
TestArgMinMax();
TestCorr();
TestRandom();
TestFromVPtr();
TestVectorAccess();
TestGetSlice();
TestApply();
TestUnique();
TestMean();
TestMax();
TestGetComponents();
}
};
int main() {
MatrixTestCase().run();
return 0;
}
constexpr Matrix< T > Transpose() const
Definition: Matrix.h:256
Matrix & HadamardMulti(const Matrix &other)
Definition: Matrix.h:389
size_t rows() const
Definition: Matrix.h:193
size_t columns() const
Definition: Matrix.h:198
void assertSize(const Matrix< T > &other) const
Definition: Matrix.h:334
void Resize(size_t rows, size_t cols, size_t elementSize=1)
Definition: Matrix.h:585
Matrix< T > & KroneckerMulti(const Matrix< T > &other)
Definition: Matrix.h:400
Matrix< T > Apply(const std::function< T(T)> &fun) const
Definition: Matrix.h:375
static Matrix Random(size_t rows, size_t columns, size_t element_size=1, double minValue=0.0, double maxValue=1.0)
Definition: Matrix.h:158
int GetIndex(size_t row, size_t col, size_t elem=0) const
Definition: Matrix.h:605
Matrix< T > HorizontalConcat(const Matrix< T > &other)
Definition: Matrix.h:270
size_t Corr(const Matrix< T > &A, const Matrix< T > &B)
Definition: matrix_utils.h:103
size_t argmin(const Matrix< T > &mat)
Definition: matrix_utils.h:167
Matrix< T > HorizontalConcat(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:81
Matrix< T > mean(const Matrix< T > &mat, int axis=-1)
Definition: matrix_utils.h:401
size_t argmax(const Matrix< T > &mat)
Definition: matrix_utils.h:143
Matrix< T > KroneckerMulti(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:56
T elemMean(const Matrix< T > &mat, const size_t &elemIndex)
Definition: matrix_utils.h:431
Matrix< T > from_vptr(const T *value, MatrixDimension size)
Definition: matrix_utils.h:122
Matrix< T > HadamardMulti(const Matrix< T > &lhs, const Matrix< T > &rhs)
Definition: matrix_utils.h:17
T elemMax(const Matrix< T > &mat, const size_t &elemIndex)
Definition: matrix_utils.h:358
Matrix< T > unique(const Matrix< T > &in, int axis=0)
Definition: matrix_utils.h:466
Matrix< T > where(const std::function< bool(T)> &condition, const Matrix< T > &in, const Matrix< T > &valIfTrue, const Matrix< T > &valIfFalse)
Definition: matrix_utils.h:194
Matrix< double > eye(size_t rows, size_t columns=0)