Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (62 sloc) 2.48 KB
#include <Matrix.h>
#include <SparseMatrix.h>
#include <SuccessiveOverRelaxation.h>
#include <Vector.h>
#include <gtest/gtest.h>
TEST(SuccessiveOverRelaxationTest, Solve4x4LinearSystem)
{
/* linear system matrix */
freeaml::SparseMatrix<double> A = {{10.0, -1.0, 2.0, 0.0},
{-1.0, 11.0, -1.0, 3.0},
{2.0, -1.0, 10.0, -1.0},
{0.0, 3.0, -1.0, 8.0}};
/* solution vector (initially set to zero) */
freeaml::Vector<double> x = {0.0, 0.0, 0.0, 0.0};
/* right-hand side */
freeaml::Vector<double> b = {6.0, 25.0, -11.0, 15.0};
/*
* linear system solver: successive over-relaxation
* maximum number of iterations: 50
* residual tolerance: 1e-7
* relaxation factor: 1.5
*/
freeaml::SuccessiveOverRelaxation<double> lss(50, 1e-7, 1.5);
/* solve the linear system */
bool status = lss.solve(A, x, b);
EXPECT_TRUE(status);
double residual = (A * x - b).l2_norm();
EXPECT_LE(residual, lss.residual_tolerance());
EXPECT_LE(lss.num_iterations(), lss.max_iterations());
/* exact solution */
freeaml::Vector<double> y = {1.0, 2.0, -1.0, 1.0};
EXPECT_LE((x - y).l2_norm(), 1.e-7);
}
TEST(SuccessiveOverRelaxationTest, Solve5x5LinearSystem)
{
/* linear system matrix */
freeaml::Matrix<double> A = {{5.85, 1.05, 1.80, 0.78, 1.16},
{1.05, 6.52, 0.57, 1.09, 0.34},
{1.80, 0.57, 7.47, 1.01, 1.31},
{0.78, 1.09, 1.01, 4.07, 0.15},
{1.16, 0.34, 1.31, 0.15, 4.37}};
/* solution vector (initially set to zero) */
freeaml::Vector<double> x = {0.0, 0.0, 0.0, 0.0, 0.0};
/* right-hand side */
freeaml::Vector<double> b = {13.13, 18.15, 3.74, 14.76, 18.46};
/*
* linear system solver: successive over-relaxation
* maximum number of iterations: 50
* residual tolerance: 1e-8
* relaxation factor: 1.25
*/
freeaml::SuccessiveOverRelaxation<double> lss(50, 1e-8, 1.25);
/* solve the linear system */
bool status = lss.solve(A, x, b);
EXPECT_TRUE(status);
double residual = (A * x - b).l2_norm();
EXPECT_LE(residual, lss.residual_tolerance());
EXPECT_LE(lss.num_iterations(), lss.max_iterations());
/* exact solution */
freeaml::Vector<double> y = {1.0, 2.0, -1.0, 3.0, 4.0};
EXPECT_LE((x - y).l2_norm(), 1.e-7);
}
You can’t perform that action at this time.