int AddDenseDenseTestObj() { DenseMatrix A(3, 3, 0.); A(0, 0) = 1; A(0, 1) = 1; A(0, 2) = 1; A(1, 2) = 5; A(2, 2) = -1; DenseMatrix B(3, 3, 1.); B(0, 0) = 2; B(0, 1) = -1; B(0, 2) = 3; B(1, 2) = -5; B(2, 2) = -1; DenseMatrix C = A + B; DenseMatrix D = (A+B) + B; DenseMatrix E = move(A) + (B+A); double expected[9] = {4, 1, 1, 1, 1, 1, 5, 5, -3}; assertEqual(expected, E.getData(), 9); return 1; }
int MultiplySparseDenseTestObj(){ SparseMatrix A{3,3, true}; A(0, 0) = 1; A(0, 1) = 1; A(0, 2) = 1; A(1, 2) = 5; A(2, 2) = -1; A.build(); DenseMatrix x{3,2, 1.}; x(0,1) = 3; x(1,1) = 7; x(2,0) = 9; DenseMatrix y{1,3}; y(0,0) = 3; y(0,1) = 7; y(0,2) = 9; DenseMatrix res = A*x; double expected[6] = {11, 46, -3, 11, 8, 37}; assertEqual(expected, res.getData(), 6); DenseMatrix res2 = y*A; double expected2[3] = {19, 48, 29}; assertEqual(expected2, res2.getData(), 3); return 1; }
int TestCaseFunctionOperatorObj(){ SparseMatrix A{3,3, true}; A(0, 0) = 1; A(0, 1) = 1; A(0, 2) = 1; A(1, 2) = 5; A(2, 2) = -1; A.build(); DenseMatrix b{3}; b(0) = 6; b(1) = -4; b(2) = 27; // Ax = b Factor factor = A.analyze(); bool res = factor.factorize(A); TINYTEST_ASSERT(res); DenseMatrix x = solve(factor, b); double expected[] = {2.78571f,4.57143f,-1.35714f}; assertEqual(expected, x.getData(), 3); // update values A.zero(); A(0, 0) = 2; A(0, 1) = 9; A(0, 2) = 7; A(1, 2) = 8; A(2, 2) = -3; factor.factorize(A); x = solve(factor, b); double expected2[] = {1.0935,1.76937,-1.73019}; assertEqual(expected2, x.getData(), 3); return 1; }
int SolveSparseDenseFactorTestObj() { SparseMatrix A{3,3, true}; A(0, 0) = 1; A(0, 1) = 1; A(0, 2) = 1; A(1, 2) = 5; A(2, 2) = -1; A.build(); DenseMatrix b{3}; b(0) = 6; b(1) = -4; b(2) = 27; Factor F; F = A.analyze(); bool res = F.factorize(A); DenseMatrix x = solve(F, b); double expected[] = {2.78571f,4.57143f,-1.35714f}; assertEqual(expected, x.getData(), 3); // update values A.zero(); A(0, 0) = 2; A(0, 1) = 9; A(0, 2) = 7; A(1, 2) = 8; A(2, 2) = -3; F.factorize(A); x = solve(F, b); double expected2[] = {1.0935,1.76937,-1.73019}; assertEqual(expected2, x.getData(), 3); return 1; }
int TransposeDenseTestObj() { DenseMatrix b{3}; b(0) = 6; b(1) = -4; b(2) = 27; b.transpose(); DenseMatrix c = transposed(b); DenseMatrix d = transposed(move(c)); assertEqual(b.getData(), d.getData(), 3) return 1; }
int SolveSparseDenseTestObj() { SparseMatrix A{3,3, true}; A(0, 0) = 1; A(0, 1) = 1; A(0, 2) = 1; A(1, 2) = 5; A(2, 2) = -1; A.build(); DenseMatrix b{3}; b(0) = 6; b(1) = -4; b(2) = 27; // Ax = b DenseMatrix x = solve(A, b); double expected[] = {2.78571f,4.57143f,-1.35714f}; assertEqual(expected, x.getData(), 3); return 1; }
int MultiplyDenseDenseTestObj(){ DenseMatrix x{3, 2, 1.}; x(0,1) = 3; x(1,1) = 7; x(2,0) = 9; DenseMatrix y{2, 3, 1.}; y(0,0) = -3; y(0,1) = 7; y(1,1) = 2; DenseMatrix res = y*x; double expected[4] = {13, 12, 41, 18}; assertEqual(expected, res.getData(), 3); DenseMatrix v{3,1}; v(0) = 3; v(1) = 7; v(2) = 9; DenseMatrix w{1,3}; w(0,0) = -3; w(0,1) = 7; w(0,2) = 2; DenseMatrix res2 = w*v; TINYTEST_ASSERT(58 == res2(0,0)); DenseMatrix res3 = v*w; TINYTEST_ASSERT(-9 == res3(0,0)); DenseMatrix res4 = w*x; double expected2[2] = {22, 42}; assertEqual(expected2, res4.getData(), 2); return 1; }
int SolveDenseDenseTestObj() { DenseMatrix A{3,3, 1.}; A(0, 0) = -1; A(0, 1) = 5; A(0, 2) = -3; A(1, 2) = 5; A(2, 0) = -1; DenseMatrix b{3, 2, 1.}; b(0) = 6; b(1) = -4; b(2) = 27; // Ax = b DenseMatrix x = solve(A, b); double expected[6] = {-23.8750, -1.0625, 4.1875, -0.5, 0.25, 0.25}; assertEqual(expected, x.getData(), 6); DenseMatrix x2 = solve(move(A), move(b)); assertEqual(expected, x2.getData(), 6); return 1; }