Skip to content

Thomas algorithm implementation in Common Lisp #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions contents/thomas_algorithm/code/clisp/thomas.lisp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
;;;; Thomas algorithm implementation in Common Lisp

(defun thomas (a b c d)
"Returns the solutions to a tri-diagonal matrix destructively"
(setf (svref c 0) (/ (svref c 0) (svref b 0)))
(setf (svref d 0) (/ (svref d 0) (svref b 0)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment about the macro here. Give them good docstrings.


(loop
for i from 1 upto (1- (length a)) do
(setf
(svref c i)
(/ (svref c i) (- (svref b i) (* (svref a i) (svref c (1- i))))))
(setf
(svref d i)
(/
(- (svref d i) (* (svref a i) (svref d (1- i))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to write a macro, (- (svref d i) (* (svref a i) (svref d (1- i)))) is probably the level of granularity that you want, with a separate macro for the 1+ version. Since you aren't shortening the code by using macros, make sure you use a descriptive macro name.

(- (svref b i) (* (svref a i) (svref c (1- i)))))))

(loop
for i from (- (length a) 2) downto 0 do
(decf (svref d i) (* (svref c i) (svref d (1+ i))))
finally (return d)))

(defvar diagonal-a #(0 2 3))
(defvar diagonal-b #(1 3 6))
(defvar diagonal-c #(4 5 0))
(defvar last-column #(7 5 3))

(print (thomas diagonal-a diagonal-b diagonal-c last-column))
2 changes: 2 additions & 0 deletions contents/thomas_algorithm/thomas_algorithm.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e
[import, lang:"crystal"](code/crystal/thomas.cr)
{% sample lang="kotlin" %}
[import, lang:"kotlin"](code/kotlin/thomas.kt)
{% sample lang="lisp" %}
[import, lang:"lisp"](code/clisp/thomas.lisp)
{% endmethod %}

<script>
Expand Down