This is an example on how to use the utils.h file.
#include "../Test.h"
class NumUtilsTestCase : public Test
{
bool TestLinspace() {
assert(vec(0, 0) == 0);
assert(vec(0, 1) == 0.5);
assert(vec(0, 2) == 1.0);
assert(vec(0, 0) == 1.0);
assert(vec(0, 1) == 1.5);
assert(vec(0, 2) == 2.0);
return true;
}
bool TestZeros() {
AssertEqual(A.rows(), 2);
AssertEqual(A.columns(), 3);
for(size_t i = 0; i < A.rows(); i++) {
for(size_t j = 0; j < A.columns(); ++j) { AssertEqual(A(i, j), 0.0); }
}
return true;
}
bool TestOnes() {
for(size_t i = 0; i < A.rows(); i++) {
for(size_t j = 0; j < A.columns(); ++j) { AssertEqual(A(i, j), 1.0); }
}
return true;
}
bool TestZerosV() {
AssertEqual(A.rows(), 3);
AssertEqual(A.columns(), 1);
for(size_t i = 0; i < A.rows(); i++) { AssertEqual(A(i, 0), 0.0); }
return true;
}
bool TestEye() {
for(size_t i = 0; i < B.rows(); ++i) {
for(size_t j = 0; j < B.columns(); ++j) {
if(i == j) this->AssertEqual(B(i, j), 1.0);
else
this->AssertEqual(B(i, j), 0.0);
}
}
};
AssertEqual(A.rows(), 3);
AssertEqual(A.columns(), 2);
testEye(A);
AssertEqual(A.rows(), 2);
AssertEqual(A.columns(), 3);
testEye(A);
AssertEqual(A.rows(), 3);
AssertEqual(A.columns(), 3);
testEye(A);
return true;
}
bool TestNorm() {
AssertEqual(
norm(A), 0.0);
A = { { 1, 1, 1, 1 } };
AssertEqual(
norm(A), 2.0);
A = { { 1e-16, 1e-16, 1e-16 } };
AssertEqual(
norm(A), 0.0);
return true;
}
public:
virtual void run() {
TestEye();
TestOnes();
TestZeros();
TestZerosV();
TestNorm();
TestLinspace();
}
};
int main() {
NumUtilsTestCase().run();
return 0;
}
Matrix< double > eye(size_t rows, size_t columns=0)
double norm(const Matrix< double > &in)
Matrix< double > ones(size_t rows, size_t columns=1, size_t elements=1)
Matrix< double > zerosV(size_t rows)
Matrix< double > zeros(size_t rows, size_t columns, size_t elements=1)
Matrix< double > linspace(double start, double end, unsigned long num_elements)