|
1 | 1 | ;;;; Thomas algorithm implementation in Common Lisp
|
2 | 2 |
|
3 |
| -(defun thomas (a b c d) |
4 |
| - "Returns the solutions to a tri-diagonal matrix destructively" |
5 |
| - (setf (svref c 0) (/ (svref c 0) (svref b 0))) |
6 |
| - (setf (svref d 0) (/ (svref d 0) (svref b 0))) |
7 |
| - |
8 |
| - (loop |
9 |
| - for i from 1 upto (1- (length a)) do |
| 3 | +(defun thomas (diagonal-a diagonal-b diagonal-c last-column) |
| 4 | + "Returns the solutions to a tri-diagonal matrix non-destructively" |
| 5 | + (let ((a (copy-seq diagonal-a)) |
| 6 | + (b (copy-seq diagonal-b)) |
| 7 | + (c (copy-seq diagonal-c)) |
| 8 | + (d (copy-seq last-column))) |
| 9 | + (setf (svref c 0) (/ (svref c 0) (svref b 0))) |
| 10 | + (setf (svref d 0) (/ (svref d 0) (svref b 0))) |
| 11 | + (loop |
| 12 | + for i from 1 upto (1- (length a)) do |
10 | 13 | (setf
|
11 | 14 | (svref c i)
|
12 | 15 | (/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i))))))
|
|
15 | 18 | (/
|
16 | 19 | (- (svref d i) (* (svref a i) (svref d (1- i))))
|
17 | 20 | (- (svref b i) (* (svref a i) (svref c (1- i)))))))
|
| 21 | + (loop |
| 22 | + for i from (- (length a) 2) downto 0 do |
| 23 | + (decf (svref d i) (* (svref c i) (svref d (1+ i))))) |
| 24 | + d)) |
18 | 25 |
|
19 |
| - (loop |
20 |
| - for i from (- (length a) 2) downto 0 do |
21 |
| - (decf (svref d i) (* (svref c i) (svref d (1+ i)))) |
22 |
| - finally (return d))) |
23 |
| - |
24 |
| -(defvar diagonal-a #(0 2 3)) |
25 |
| -(defvar diagonal-b #(1 3 6)) |
26 |
| -(defvar diagonal-c #(4 5 0)) |
27 |
| -(defvar last-column #(7 5 3)) |
| 26 | +(defparameter diagonal-a #(0 2 3)) |
| 27 | +(defparameter diagonal-b #(1 3 6)) |
| 28 | +(defparameter diagonal-c #(4 5 0)) |
| 29 | +(defparameter last-column #(7 5 3)) |
28 | 30 |
|
29 |
| -(print (thomas diagonal-a diagonal-b diagonal-c last-column)) |
| 31 | +(format t "~{~f ~}" (coerce (thomas diagonal-a diagonal-b diagonal-c last-column) 'list)) |
0 commit comments