File tree 1 file changed +8
-5
lines changed
contents/thomas_algorithm/code/clisp
1 file changed +8
-5
lines changed Original file line number Diff line number Diff line change 1
1
; ;;; Thomas algorithm implementation in Common Lisp
2
2
3
+ (defun div-set (number divisor)
4
+ (setf number (/ number divisor)))
5
+
3
6
(defun thomas (diagonal-a diagonal-b diagonal-c last-column)
4
7
" Returns the solutions to a tri-diagonal matrix non-destructively"
8
+ ; ; we copy the inputs to ensure non-destructiveness
5
9
(let ((a (copy-seq diagonal-a))
6
10
(b (copy-seq diagonal-b))
7
11
(c (copy-seq diagonal-c))
8
12
(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 ))
11
15
(loop
12
16
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)))))
16
18
(setf
17
19
(svref d i)
18
20
(/
28
30
(defparameter diagonal-c # (4 5 0 ))
29
31
(defparameter last-column # (7 5 3 ))
30
32
33
+ ; ; should print 0.8666667 1.5333333 -0.26666668
31
34
(format t " ~{ ~f ~} " (coerce (thomas diagonal-a diagonal-b diagonal-c last-column) ' list))
You can’t perform that action at this time.
0 commit comments