Skip to content

Commit 0b2b247

Browse files
committed
First working version thomas algorithm in lisp.
1 parent 131ae54 commit 0b2b247

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
;;;; Thomas algorithm implementation in Common Lisp
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
10+
(setf
11+
(svref c i)
12+
(/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i))))))
13+
(setf
14+
(svref d i)
15+
(/
16+
(- (svref d i) (* (svref a i) (svref d (1- i))))
17+
(- (svref b i) (* (svref a i) (svref c (1- i)))))))
18+
19+
(loop
20+
for i from (- (length a) 2) downto 0 do
21+
(setf (svref d i) (- (svref d i) (* (svref c i) (svref d (1+ i))))))
22+
23+
(return-from thomas d))
24+
25+
(defvar diagonal-a #(0 2 3))
26+
(defvar diagonal-b #(1 3 6))
27+
(defvar diagonal-c #(4 5 0))
28+
(defvar last-column #(7 5 3))
29+
30+
(print (thomas diagonal-a diagonal-b diagonal-c last-column))

0 commit comments

Comments
 (0)