Skip to content

Commit 2ca5603

Browse files
committed
Added SOR in Lua
1 parent a1da403 commit 2ca5603

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
-- Successive Over Relaxation algorithm implementation
2+
-- See : http://en.wikipedia.org/wiki/Successive_over-relaxation
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 SOR algorithm
21+
-- m : a matrix
22+
-- w : the relaxation parameter, defaults to 1.86
23+
-- eps : the convergence criterion, defaults to 1e-6
24+
-- maxiter : the maximum number of iterations, defaults to 1e4
25+
-- return : a solution vector
26+
local function SOR(m, w, eps, maxiter)
27+
local n = #m
28+
local x = vector(n, 0)
29+
local q, p, sum
30+
local t = 0
31+
32+
w = w or 1.86
33+
eps = eps or 1e-6
34+
maxiter = maxiter or 1e4
35+
36+
repeat
37+
t = t + 1
38+
q = norm(x)
39+
for i=1,n do
40+
sum = m[i][n+1]
41+
for j=1,n do
42+
if (i~=j) then
43+
sum = sum - m[i][j]*x[j]
44+
end
45+
end
46+
x[i] = (1-w) * x[i] + w * sum/m[i][i]
47+
end
48+
p = norm(x)
49+
until (math.abs(p-q) < eps) or (t >= maxiter)
50+
51+
return x
52+
end
53+
54+
return SOR
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
-- Tests for sor.lua
2+
local sor = require 'sor'
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+
local function fuzzy_eq(a, b, eps)
21+
eps = eps or 1e-6
22+
return math.abs(a - b) < eps
23+
end
24+
25+
run('Testing SOR', function()
26+
local m = {
27+
{-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,-11},
28+
{1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,0,-03},
29+
{0,1,-4,1,0,0,1,0,0,0,0,0,0,0,0,0,-03},
30+
{0,0,1,-4,0,0,0,1,0,0,0,0,0,0,0,0,-11},
31+
{1,0,0,0,-4,1,0,0,1,0,0,0,0,0,0,0,-08},
32+
{0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0,0, 00},
33+
{0,0,1,0,0,1,-4,1,0,0,1,0,0,0,0,0, 00},
34+
{0,0,0,1,0,0,1,-4,0,0,0,1,0,0,0,0,-08},
35+
{0,0,0,0,1,0,0,0,-4,1,0,0,1,0,0,0,-08},
36+
{0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0,0, 00},
37+
{0,0,0,0,0,0,1,0,0,1,-4,1,0,0,1,0, 00},
38+
{0,0,0,0,0,0,0,1,0,0,1,-4,0,0,0,1,-08},
39+
{0,0,0,0,0,0,0,0,1,0,0,0,-4,1,0,0,-10},
40+
{0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,0,-02},
41+
{0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,1,-02},
42+
{0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,-4,-10},
43+
}
44+
45+
local x = sor(m)
46+
assert(fuzzy_eq(x[1], 5.454459))
47+
assert(fuzzy_eq(x[2], 4.594688))
48+
assert(fuzzy_eq(x[3], 4.594679))
49+
assert(fuzzy_eq(x[4], 5.454531))
50+
assert(fuzzy_eq(x[5], 6.223469))
51+
assert(fuzzy_eq(x[6], 5.329580))
52+
assert(fuzzy_eq(x[7], 5.329561))
53+
assert(fuzzy_eq(x[8], 6.223491))
54+
assert(fuzzy_eq(x[9], 6.109833))
55+
assert(fuzzy_eq(x[10], 5.170488))
56+
assert(fuzzy_eq(x[11], 5.170455))
57+
assert(fuzzy_eq(x[12], 6.109845))
58+
assert(fuzzy_eq(x[13], 5.045460))
59+
assert(fuzzy_eq(x[14], 4.071980))
60+
assert(fuzzy_eq(x[15], 4.071964))
61+
assert(fuzzy_eq(x[16], 5.045457))
62+
end)
63+
64+
print(('-'):rep(80))
65+
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
66+
:format(total, pass, total-pass, (pass*100/total)))

Successive_Over_Relaxation/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)