Skip to content

Commit 4aadc56

Browse files
committed
output is now floating point, function is non-destructive.
1 parent ffff663 commit 4aadc56

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
;;;; Thomas algorithm implementation in Common Lisp
22

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
1013
(setf
1114
(svref c i)
1215
(/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i))))))
@@ -15,15 +18,14 @@
1518
(/
1619
(- (svref d i) (* (svref a i) (svref d (1- i))))
1720
(- (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))
1825

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))
2830

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

Comments
 (0)