Skip to content

Commit c34df99

Browse files
SYuryjakobkogler
authored andcommitted
Added some tests for linear algebra (#476)
1 parent 295e5d7 commit c34df99

File tree

4 files changed

+112
-2
lines changed

4 files changed

+112
-2
lines changed

src/linear_algebra/linear-system-gauss.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ The input to the function `gauss` is the system matrix $a$. The last column of t
7070

7171
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$.
7272

73-
```cpp
73+
```cpp gauss
74+
const double EPS = 1e-9;
75+
const int INF = 2; // it doesn't actually have to be infinity or a big number
76+
7477
int gauss (vector < vector<double> > a, vector<double> & ans) {
7578
int n = (int) a.size();
7679
int m = (int) a[0].size() - 1;
@@ -194,3 +197,6 @@ Thus, the solution turns into two-step: First, Gauss-Jordan algorithm is applied
194197
* [Codechef - Knight Moving](https://www.codechef.com/SEP12/problems/KNGHTMOV)
195198
* [Lightoj - Graph Coloring](http://lightoj.com/volume_showproblem.php?problem=1279)
196199
* [UVA 12910 - Snakes and Ladders](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4775)
200+
* [TIMUS1042 Central Heating](http://acm.timus.ru/problem.aspx?space=1&num=1042)
201+
* [TIMUS1766 Humpty Dumpty](http://acm.timus.ru/problem.aspx?space=1&num=1766)
202+
* [TIMUS1266 Kirchhoff's Law](http://acm.timus.ru/problem.aspx?space=1&num=1266)

src/linear_algebra/rank-matrix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This algorithm runs in $\mathcal{O}(n^3)$.
2020

2121
## Implementation
2222

23-
```cpp
23+
```cpp matrix-rank
2424
const double EPS = 1E-9;
2525

2626
int compute_rank(vector<vector<double>> A) {

test/test_gauss.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#include "gauss.h"
6+
7+
typedef double dbl;
8+
typedef vector<vector<dbl> > matrix;
9+
typedef vector<dbl> vec;
10+
11+
void test1(){
12+
//x + y = 0
13+
matrix A = {{1, 1}};
14+
vec b = {0};
15+
vec ans(2);
16+
A[0].push_back(b[0]);
17+
assert(gauss(A, ans) == INF);
18+
}
19+
20+
void test2(){
21+
//x + y = 0
22+
//2x + 3y = 1
23+
matrix A = {{1, 1}, {2, 3}};
24+
vec b = {0, 1};
25+
vec ans(2);
26+
A[0].push_back(b[0]);
27+
A[1].push_back(b[1]);
28+
assert(gauss(A, ans) == 1);
29+
assert(abs(ans[0] + 1) < EPS && abs(ans[1] - 1) < EPS);
30+
}
31+
32+
void test3(){
33+
//x = 0
34+
//y = 1
35+
//x + y = 2
36+
matrix A = {{1, 0}, {0, 1}, {1, 1}};
37+
vec b = {0, 1, 2};
38+
vec ans(2);
39+
A[0].push_back(b[0]);
40+
A[1].push_back(b[1]);
41+
A[2].push_back(b[2]);
42+
assert(gauss(A, ans) == 0);
43+
}
44+
45+
int main(){
46+
test1();
47+
test2();
48+
test3();
49+
}

test/test_rank_matrix.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#include "matrix-rank.h"
6+
7+
typedef double dbl;
8+
typedef vector<vector<dbl> > matrix;
9+
10+
void test1(){
11+
matrix A(3);
12+
A[0] = {1, 1, 0};
13+
A[1] = {0, 1, 0};
14+
A[2] = {1, 0, 0};
15+
assert(compute_rank(A) == 2);
16+
}
17+
18+
void test2(){
19+
matrix A(3);
20+
A[0] = {1, 1, 1};
21+
A[1] = {0, 1, 1};
22+
A[2] = {0, 0, 1};
23+
assert(compute_rank(A) == 3);
24+
}
25+
26+
void test3(){
27+
matrix A(3);
28+
A[0] = {0, 0, 0};
29+
A[1] = {0, 0, 0};
30+
A[2] = {0, 0, 0};
31+
assert(compute_rank(A) == 0);
32+
}
33+
34+
void test4(){
35+
matrix A(3);
36+
A[0] = {1, 1, 1};
37+
A[1] = {1, 1, 1};
38+
A[2] = {1, 1, 1};
39+
assert(compute_rank(A) == 1);
40+
}
41+
42+
void test5(){
43+
matrix A(2);
44+
A[0] = {1, 0, 1, 0};
45+
A[1] = {0, 1, 0, 1};
46+
assert(compute_rank(A) == 2);
47+
}
48+
49+
int main(){
50+
test1();
51+
test2();
52+
test3();
53+
test4();
54+
test5();
55+
}

0 commit comments

Comments
 (0)
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