From 44f07de53c79ef8a54f44194c38ed5679f35a205 Mon Sep 17 00:00:00 2001 From: SYury Date: Mon, 14 Oct 2019 18:23:56 +0300 Subject: [PATCH] Added some tests for linear algebra --- src/linear_algebra/linear-system-gauss.md | 8 +++- src/linear_algebra/rank-matrix.md | 2 +- test/test_gauss.cpp | 49 ++++++++++++++++++++ test/test_rank_matrix.cpp | 55 +++++++++++++++++++++++ 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 test/test_gauss.cpp create mode 100644 test/test_rank_matrix.cpp diff --git a/src/linear_algebra/linear-system-gauss.md b/src/linear_algebra/linear-system-gauss.md index a0233f2b5..9640d84bd 100644 --- a/src/linear_algebra/linear-system-gauss.md +++ b/src/linear_algebra/linear-system-gauss.md @@ -70,7 +70,10 @@ The input to the function `gauss` is the system matrix $a$. The last column of t The function returns the number of solutions of the system $(0, 1,\textrm{or } \infty)$. If at least one solution exists, then it is returned in the vector $ans$. -```cpp +```cpp gauss +const double EPS = 1e-9; +const int INF = 2; // it doesn't actually have to be infinity or a big number + int gauss (vector < vector > a, vector & ans) { int n = (int) a.size(); int m = (int) a[0].size() - 1; @@ -194,3 +197,6 @@ Thus, the solution turns into two-step: First, Gauss-Jordan algorithm is applied * [Codechef - Knight Moving](https://www.codechef.com/SEP12/problems/KNGHTMOV) * [Lightoj - Graph Coloring](http://lightoj.com/volume_showproblem.php?problem=1279) * [UVA 12910 - Snakes and Ladders](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4775) +* [TIMUS1042 Central Heating](http://acm.timus.ru/problem.aspx?space=1&num=1042) +* [TIMUS1766 Humpty Dumpty](http://acm.timus.ru/problem.aspx?space=1&num=1766) +* [TIMUS1266 Kirchhoff's Law](http://acm.timus.ru/problem.aspx?space=1&num=1266) diff --git a/src/linear_algebra/rank-matrix.md b/src/linear_algebra/rank-matrix.md index 48c213d79..23e56eb95 100644 --- a/src/linear_algebra/rank-matrix.md +++ b/src/linear_algebra/rank-matrix.md @@ -20,7 +20,7 @@ This algorithm runs in $\mathcal{O}(n^3)$. ## Implementation -```cpp +```cpp matrix-rank const double EPS = 1E-9; int compute_rank(vector> A) { diff --git a/test/test_gauss.cpp b/test/test_gauss.cpp new file mode 100644 index 000000000..c2d263343 --- /dev/null +++ b/test/test_gauss.cpp @@ -0,0 +1,49 @@ +#include + +using namespace std; + +#include "gauss.h" + +typedef double dbl; +typedef vector > matrix; +typedef vector vec; + +void test1(){ + //x + y = 0 + matrix A = {{1, 1}}; + vec b = {0}; + vec ans(2); + A[0].push_back(b[0]); + assert(gauss(A, ans) == INF); +} + +void test2(){ + //x + y = 0 + //2x + 3y = 1 + matrix A = {{1, 1}, {2, 3}}; + vec b = {0, 1}; + vec ans(2); + A[0].push_back(b[0]); + A[1].push_back(b[1]); + assert(gauss(A, ans) == 1); + assert(abs(ans[0] + 1) < EPS && abs(ans[1] - 1) < EPS); +} + +void test3(){ + //x = 0 + //y = 1 + //x + y = 2 + matrix A = {{1, 0}, {0, 1}, {1, 1}}; + vec b = {0, 1, 2}; + vec ans(2); + A[0].push_back(b[0]); + A[1].push_back(b[1]); + A[2].push_back(b[2]); + assert(gauss(A, ans) == 0); +} + +int main(){ + test1(); + test2(); + test3(); +} diff --git a/test/test_rank_matrix.cpp b/test/test_rank_matrix.cpp new file mode 100644 index 000000000..e494f024f --- /dev/null +++ b/test/test_rank_matrix.cpp @@ -0,0 +1,55 @@ +#include + +using namespace std; + +#include "matrix-rank.h" + +typedef double dbl; +typedef vector > matrix; + +void test1(){ + matrix A(3); + A[0] = {1, 1, 0}; + A[1] = {0, 1, 0}; + A[2] = {1, 0, 0}; + assert(compute_rank(A) == 2); +} + +void test2(){ + matrix A(3); + A[0] = {1, 1, 1}; + A[1] = {0, 1, 1}; + A[2] = {0, 0, 1}; + assert(compute_rank(A) == 3); +} + +void test3(){ + matrix A(3); + A[0] = {0, 0, 0}; + A[1] = {0, 0, 0}; + A[2] = {0, 0, 0}; + assert(compute_rank(A) == 0); +} + +void test4(){ + matrix A(3); + A[0] = {1, 1, 1}; + A[1] = {1, 1, 1}; + A[2] = {1, 1, 1}; + assert(compute_rank(A) == 1); +} + +void test5(){ + matrix A(2); + A[0] = {1, 0, 1, 0}; + A[1] = {0, 1, 0, 1}; + assert(compute_rank(A) == 2); +} + +int main(){ + test1(); + test2(); + test3(); + test4(); + test5(); +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy