Skip to content

Commit 79ba791

Browse files
committed
Added Gauss Seidel in Lua
1 parent d80c031 commit 79ba791

3 files changed

Lines changed: 125 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
-- Gauss Seidel algorithm implementation
2+
-- See : http://en.wikipedia.org/wiki/Gauss%E2%80%93Seidel_method
3+
4+
-- Creates a vector of values
5+
local function vector(len, v)
6+
local x = {}
7+
for i = 1, len do x[i] = v end
8+
return x
9+
end
10+
11+
-- Computes the norm of a given vector
12+
local function norm(v)
13+
local n = 0
14+
for i, _v in ipairs(v) do
15+
n = n + (_v * _v)
16+
end
17+
return math.sqrt(n)
18+
end
19+
20+
-- Solves the given matrix using Gauss-Seidel algorithm
21+
-- m : a matrix A in [A][x] = [b]
22+
-- b : the result vector in [A][x] = [b]
23+
-- w : the relaxation parameter, defaults to 1.86
24+
-- eps : the convergence criterion, defaults to 1e-6
25+
-- maxiter : the maximum number of iterations, defaults to 1e4
26+
-- return : a solution vector
27+
local function gauss_seidel(m, b, w, eps, maxiter)
28+
local n = #m
29+
local x = vector(n, 0)
30+
local q, p, sum
31+
local t = 0
32+
33+
w = w or 1.86
34+
eps = eps or 1e-6
35+
maxiter = maxiter or 1e4
36+
37+
repeat
38+
t = t + 1
39+
q = norm(x)
40+
for i = 1, n do
41+
sum = 0
42+
for j = 1, n do
43+
if (i ~= j) then
44+
sum = sum + m[i][j] * x[j]
45+
end
46+
end
47+
x[i] = (1 / m[i][i]) * (b[i] - sum)
48+
end
49+
p = norm(x)
50+
until (math.abs(p - q) < eps) or (t >= maxiter)
51+
52+
return x
53+
end
54+
55+
return gauss_seidel
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- Tests for sor.lua
2+
local gauss_seidel = require 'gauss_seidel'
3+
4+
local total, pass = 0, 0
5+
6+
local function dec(str, len)
7+
return #str < len
8+
and str .. (('.'):rep(len-#str))
9+
or str:sub(1,len)
10+
end
11+
12+
local function run(message, f)
13+
total = total + 1
14+
local ok, err = pcall(f)
15+
if ok then pass = pass + 1 end
16+
local status = ok and 'PASSED' or 'FAILED'
17+
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
18+
end
19+
20+
-- Fuzzy equality test
21+
local function fuzzy_eq(a, b, eps)
22+
eps = eps or 1e-3
23+
return math.abs(a - b) < eps
24+
end
25+
26+
run('Testing Gauss Seidel', function()
27+
local m = {
28+
{-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0},
29+
{1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0},
30+
{0,1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0},
31+
{0,0,1,-4,0,0,0,1,0,0,0,0,0,0,0,0},
32+
{1,0,0,0,-4,1,0,0,1,0,0,0,0,0,0,0},
33+
{0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0,0},
34+
{0,0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0},
35+
{0,0,0,1,0,0,1,-4,0,0,0,1,0,0,0,0},
36+
{0,0,0,0,1,0,0,0,-4,1,0,0,1,0,0,0},
37+
{0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0,0},
38+
{0,0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0},
39+
{0,0,0,0,0,0,0,1,0,0,1,-4,0,0,0,1},
40+
{0,0,0,0,0,0,0,0,1,0,0,0,-4,1,0,0},
41+
{0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,0},
42+
{0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1},
43+
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4},
44+
}
45+
46+
local b = {-11, -3, -3, -11, -8, 0, 0, -8, -8, 0, 0, -8, -10,-2, -2, -10}
47+
48+
local x = gauss_seidel(m, b)
49+
assert(fuzzy_eq(x[1], 5.454459))
50+
assert(fuzzy_eq(x[2], 4.594688))
51+
assert(fuzzy_eq(x[3], 4.594679))
52+
assert(fuzzy_eq(x[4], 5.454531))
53+
assert(fuzzy_eq(x[5], 6.223469))
54+
assert(fuzzy_eq(x[6], 5.329580))
55+
assert(fuzzy_eq(x[7], 5.329561))
56+
assert(fuzzy_eq(x[8], 6.223491))
57+
assert(fuzzy_eq(x[9], 6.109833))
58+
assert(fuzzy_eq(x[10], 5.170488))
59+
assert(fuzzy_eq(x[11], 5.170455))
60+
assert(fuzzy_eq(x[12], 6.109845))
61+
assert(fuzzy_eq(x[13], 5.045460))
62+
assert(fuzzy_eq(x[14], 4.071980))
63+
assert(fuzzy_eq(x[15], 4.071964))
64+
assert(fuzzy_eq(x[16], 5.045457))
65+
end)
66+
67+
print(('-'):rep(80))
68+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
69+
:format(total, pass, total-pass, (pass*100/total)))

Gauss_Seidel_Algorithm/tags

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Algebra, Numerical, Matrix, Linear Systems

0 commit comments

Comments
 (0)