Skip to content

Commit f4a5770

Browse files
committed
fixed the wrong test and added in a macro
1 parent 4aadc56 commit f4a5770

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
;;;; Thomas algorithm implementation in Common Lisp
22

3+
(defmacro divf (place divisor)
4+
"Divides the value at place by divisor"
5+
`(setf ,place (/ ,place ,divisor)))
6+
7+
(defun helper (v1 v2 v3 row)
8+
(- (svref v1 row) (* (svref v2 row) (svref v3 (1- row)))))
9+
310
(defun thomas (diagonal-a diagonal-b diagonal-c last-column)
411
"Returns the solutions to a tri-diagonal matrix non-destructively"
12+
;; We have to copy the inputs to ensure non-destructiveness
513
(let ((a (copy-seq diagonal-a))
614
(b (copy-seq diagonal-b))
715
(c (copy-seq diagonal-c))
816
(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)))
17+
(divf (svref c 0) (svref b 0))
18+
(divf (svref d 0) (svref b 0))
1119
(loop
1220
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))))))
16-
(setf
17-
(svref d i)
18-
(/
19-
(- (svref d i) (* (svref a i) (svref d (1- i))))
20-
(- (svref b i) (* (svref a i) (svref c (1- i)))))))
21+
(divf (svref c i) (helper b a c i))
22+
(setf (svref d i) (/ (helper d a d i) (helper b a c i))))
2123
(loop
2224
for i from (- (length a) 2) downto 0 do
2325
(decf (svref d i) (* (svref c i) (svref d (1+ i)))))
@@ -28,4 +30,5 @@
2830
(defparameter diagonal-c #(4 5 0))
2931
(defparameter last-column #(7 5 3))
3032

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

0 commit comments

Comments
 (0)