Skip to content

Added some tests for linear algebra #476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/linear_algebra/linear-system-gauss.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> > a, vector<double> & ans) {
int n = (int) a.size();
int m = (int) a[0].size() - 1;
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/linear_algebra/rank-matrix.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<vector<double>> A) {
Expand Down
49 changes: 49 additions & 0 deletions test/test_gauss.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include<bits/stdc++.h>

using namespace std;

#include "gauss.h"

typedef double dbl;
typedef vector<vector<dbl> > matrix;
typedef vector<dbl> 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();
}
55 changes: 55 additions & 0 deletions test/test_rank_matrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <bits/stdc++.h>

using namespace std;

#include "matrix-rank.h"

typedef double dbl;
typedef vector<vector<dbl> > 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