Skip to content

Commit 9eb09f6

Browse files
committed
further cleanup
1 parent 4aadc56 commit 9eb09f6

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

contents/thomas_algorithm/code/clisp/thomas.lisp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
;;;; Thomas algorithm implementation in Common Lisp
22

3+
(defun div-set (number divisor)
4+
(setf number (/ number divisor)))
5+
36
(defun thomas (diagonal-a diagonal-b diagonal-c last-column)
47
"Returns the solutions to a tri-diagonal matrix non-destructively"
8+
;; we copy the inputs to ensure non-destructiveness
59
(let ((a (copy-seq diagonal-a))
610
(b (copy-seq diagonal-b))
711
(c (copy-seq diagonal-c))
812
(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)))
13+
(div-set (svref c 0) (svref b 0))
14+
(div-set (svref d 0) (svref b 0))
1115
(loop
1216
for i from 1 upto (1- (length a)) do
13-
(setf
14-
(svref c i)
15-
(/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i))))))
17+
(div-set (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i)))))
1618
(setf
1719
(svref d i)
1820
(/
@@ -28,4 +30,5 @@
2830
(defparameter diagonal-c #(4 5 0))
2931
(defparameter last-column #(7 5 3))
3032

33+
;; should print 0.8666667 1.5333333 -0.26666668
3134
(format t "~{~f ~}" (coerce (thomas diagonal-a diagonal-b diagonal-c last-column) 'list))

0 commit comments

Comments
 (0)