From 7b5c628e89c834a3e8f018be0b1f3f146965bf0f Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 31 May 2019 16:02:13 +0200 Subject: [PATCH 01/11] Added a proof for the Euclidean Algorithm --- contents/euclidean_algorithm/euclidean_algorithm.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 4ff49152f..26c308501 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -166,6 +166,12 @@ Here's a video on the Euclidean algorithm: +## Proof + +Some intuition as to why the Euclidean Algorithm works lies in it's proof. Only a proof for the subtraction method will be given at this point, but the modular version follows the same line of reasoning. + +Given two positive integers $$a$$ and $$b$$, they have a greatest common divisor $$d$$. There is always a common divisor, because every number is divisable by 1. Since $$a$$ and $$b$$ is divisable by $$d$$, $$a - b$$ is also divisable by $$d$$ ($$b < a$$). Let's call this value $$c$$. Now we once more have two numbers $$b$$ and $$c$$, which are both divisable by $$d$$. This process can be continued until the values are equal: this is the greatest common divisor $$d$$. + ## Example Code {% method %} From 131ae549231b39174f6379b0247af56efc827b9c Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 31 May 2019 16:06:17 +0200 Subject: [PATCH 02/11] Revert "Added a proof for the Euclidean Algorithm" This reverts commit 7b5c628e89c834a3e8f018be0b1f3f146965bf0f. --- contents/euclidean_algorithm/euclidean_algorithm.md | 6 ------ 1 file changed, 6 deletions(-) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index 26c308501..4ff49152f 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -166,12 +166,6 @@ Here's a video on the Euclidean algorithm: -## Proof - -Some intuition as to why the Euclidean Algorithm works lies in it's proof. Only a proof for the subtraction method will be given at this point, but the modular version follows the same line of reasoning. - -Given two positive integers $$a$$ and $$b$$, they have a greatest common divisor $$d$$. There is always a common divisor, because every number is divisable by 1. Since $$a$$ and $$b$$ is divisable by $$d$$, $$a - b$$ is also divisable by $$d$$ ($$b < a$$). Let's call this value $$c$$. Now we once more have two numbers $$b$$ and $$c$$, which are both divisable by $$d$$. This process can be continued until the values are equal: this is the greatest common divisor $$d$$. - ## Example Code {% method %} From 8cce3c9f66d097c872f8f6d0e22ccf10d87192f0 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Wed, 5 Jun 2019 21:07:06 +0200 Subject: [PATCH 03/11] First version of Verlet in Common Lisp. --- .../verlet_integration/code/clisp/verlet.lisp | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 contents/verlet_integration/code/clisp/verlet.lisp diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp new file mode 100644 index 000000000..aaeff16f6 --- /dev/null +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -0,0 +1,53 @@ +;;;; Verlet integration implementation in Common Lisp + +(defun verlet (pos acc dt) + "Finds the time it takes for an object to hit the ground using verlet integration" + (loop + with prev-pos = pos + with temp-pos = 0 + with time = 0 + while (> pos 0) do + (incf time dt) + (setf temp-pos pos) + ;the starting speed is assumed to be zero + (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) + (setf prev-pos temp-pos) + finally (return time))) + +(defun stormer-verlet (pos acc dt) + "Finds the time and velocity when an object hits the ground using the stormer-verlet method" + (loop + with prev-pos = pos + with time = 0 + with vel = 0 + with temp-pos = 0 + while (> pos 0) do + (incf time dt) + (setf temp-pos pos) + (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) + (setf prev-pos temp-pos) + (incf vel (* acc dt)) + finally (return (values time vel)))) + +(defun velocity-verlet (pos acc dt) + (loop + with prev-pos = pos + with time = 0 + with vel = 0 + while (> pos 0) do + (incf time dt) + (incf pos (+ (* vel dt) (* 0.5 acc dt dt))) + (incf vel (* acc dt)) + finally (return (values time vel)))) + +(format T "Time for Verlet integration is: ~d~%" (verlet 5 (- 10) 0.01)) +(multiple-value-bind (time vel) + (stormer-verlet 5 (- 10) 0.01) + (format T + "Time for Stormer Verlet integration is: ~d~%Velocity for Stormer Verlet integration is: ~d~%" + time vel)) +(multiple-value-bind (time vel) + (velocity-verlet 5 (- 10) 0.01) + (format T + "Time for Stormer Verlet integration is: ~d~%Velocity for Stormer Verlet integration is: ~d~%" + time vel)) From 54cc5d3b90eca8442e9f063e176e024dab9678c8 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Wed, 5 Jun 2019 21:12:20 +0200 Subject: [PATCH 04/11] Added common lisp to verlet.md file. --- contents/verlet_integration/verlet_integration.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 62d94de74..3f0182cbc 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -69,6 +69,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod [import:3-15, lang:"kotlin"](code/kotlin/verlet.kt) {% sample lang="nim" %} [import:1-14, lang:"nim"](code/nim/verlet.nim) +{% sample lang="lisp" %} +[import:3-15, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, $$\frac{1}{2}mv^2$$? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to $$\mathcal{O}(\Delta t^2)$$ accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so @@ -125,6 +127,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod [import:17-30, lang:"kotlin"](code/kotlin/verlet.kt) {% sample lang="nim" %} [import:16-32, lang:"nim"](code/nim/verlet.nim) +{% sample lang="lisp" %} +[import:17-30, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} @@ -195,6 +199,8 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod [import:32-42, lang:"kotlin"](code/kotlin/verlet.kt) {% sample lang="nim" %} [import:34-45, lang:"nim"](code/nim/verlet.nim) +{% sample lang="lisp" %} +[import:32-41, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Even though this method is more widely used than the simple Verlet method mentioned above, it unforunately has an error term of $$\mathcal{O}(\Delta t^2)$$, which is two orders of magnitude worse. That said, if you want to have a simulaton with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulatons are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from $$\sim \mathcal{O}(n^2)$$ to $$\sim \mathcal{O}(n\log(n))$$. @@ -256,6 +262,8 @@ Submitted by P. Mekhail [import, lang:"kotlin"](code/kotlin/verlet.kt) {% sample lang="nim" %} [import, lang="nim"](code/nim/verlet.nim) +{% sample lang="lisp" %} +[import, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} From 763dffe287eccb1862697b759d4d978a50f8a30a Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Thu, 18 Jul 2019 16:06:48 +0200 Subject: [PATCH 05/11] Fixed indentation and made stormer and velocity return a list instead of values --- .../verlet_integration/code/clisp/verlet.lisp | 82 +++++++++---------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index aaeff16f6..b9244a855 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -1,53 +1,51 @@ ;;;; Verlet integration implementation in Common Lisp (defun verlet (pos acc dt) - "Finds the time it takes for an object to hit the ground using verlet integration" + "Finds the time it takes for an object to hit the ground using verlet integration." (loop - with prev-pos = pos - with temp-pos = 0 - with time = 0 - while (> pos 0) do - (incf time dt) - (setf temp-pos pos) - ;the starting speed is assumed to be zero - (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) - (setf prev-pos temp-pos) - finally (return time))) + with prev-pos = pos + with temp-pos = 0 + with time = 0 + while (> pos 0) + do (incf time dt) + (setf temp-pos pos) + ;; The starting speed is assumed to be zero. + (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) + (setf prev-pos temp-pos) + finally (return time))) (defun stormer-verlet (pos acc dt) - "Finds the time and velocity when an object hits the ground using the stormer-verlet method" + "Finds the time and velocity when an object hits the ground using the stormer-verlet method." (loop - with prev-pos = pos - with time = 0 - with vel = 0 - with temp-pos = 0 - while (> pos 0) do - (incf time dt) - (setf temp-pos pos) - (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) - (setf prev-pos temp-pos) - (incf vel (* acc dt)) - finally (return (values time vel)))) + with prev-pos = pos + with time = 0 + with vel = 0 + with temp-pos = 0 + while (> pos 0) + do (incf time dt) + (setf temp-pos pos) + (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) + (setf prev-pos temp-pos) + (incf vel (* acc dt)) + finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) (loop - with prev-pos = pos - with time = 0 - with vel = 0 - while (> pos 0) do - (incf time dt) - (incf pos (+ (* vel dt) (* 0.5 acc dt dt))) - (incf vel (* acc dt)) - finally (return (values time vel)))) + with prev-pos = pos + with time = 0 + with vel = 0 + while (> pos 0) + do (incf time dt) + (incf pos (+ (* vel dt) (* 0.5 acc dt dt))) + (incf vel (* acc dt)) + finally (return (list time vel)))) -(format T "Time for Verlet integration is: ~d~%" (verlet 5 (- 10) 0.01)) -(multiple-value-bind (time vel) - (stormer-verlet 5 (- 10) 0.01) - (format T - "Time for Stormer Verlet integration is: ~d~%Velocity for Stormer Verlet integration is: ~d~%" - time vel)) -(multiple-value-bind (time vel) - (velocity-verlet 5 (- 10) 0.01) - (format T - "Time for Stormer Verlet integration is: ~d~%Velocity for Stormer Verlet integration is: ~d~%" - time vel)) +(format T "Time for Verlet integration: ~d~%" (verlet 5 (- 10) 0.01)) + +(defvar stormer-verlet-result (stormer-verlet 5 (- 10) 0.01)) +(format T "Time for Stormer Verlet integration is: ~d~%" (first stormer-verlet-result)) +(format T "Velocity for Stormer Verlet integration is: ~d~%" (second stormer-verlet-result)) + +(defvar velocity-verlet-result (velocity-verlet 5 (- 10) 0.01)) +(format T "Time for velocity Verlet integration is: ~d~%" (first velocity-verlet-result)) +(format T "Velocity for velocity Verlet integration is: ~d~%" (second velocity-verlet-result)) From 7fc7f227959193b58baa686c9617494d6bf8c813 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Thu, 18 Jul 2019 16:49:25 +0200 Subject: [PATCH 06/11] replaced temp variables and removed unnecessary looping variable --- .../verlet_integration/code/clisp/verlet.lisp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index b9244a855..7e8beaf20 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -4,14 +4,13 @@ "Finds the time it takes for an object to hit the ground using verlet integration." (loop with prev-pos = pos - with temp-pos = 0 with time = 0 while (> pos 0) do (incf time dt) - (setf temp-pos pos) ;; The starting speed is assumed to be zero. - (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) - (setf prev-pos temp-pos) + (psetf + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) finally (return time))) (defun stormer-verlet (pos acc dt) @@ -20,18 +19,18 @@ with prev-pos = pos with time = 0 with vel = 0 - with temp-pos = 0 while (> pos 0) do (incf time dt) - (setf temp-pos pos) - (setf pos (+ (* pos 2) (- prev-pos) (* acc dt dt))) - (setf prev-pos temp-pos) + ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. + (psetf + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) (incf vel (* acc dt)) finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) + "Finds the time and velocity when an object hist the ground using the velocity in calculations." (loop - with prev-pos = pos with time = 0 with vel = 0 while (> pos 0) From ac4b84db1ee0460cc7fee12d05b3da80ba8eaeec Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Thu, 18 Jul 2019 16:58:02 +0200 Subject: [PATCH 07/11] Fixed weird indentation bug and the .md file --- contents/verlet_integration/code/clisp/verlet.lisp | 8 ++++---- contents/verlet_integration/verlet_integration.md | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index 7e8beaf20..0a7b905f0 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -9,8 +9,8 @@ do (incf time dt) ;; The starting speed is assumed to be zero. (psetf - pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) - prev-pos pos) + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) finally (return time))) (defun stormer-verlet (pos acc dt) @@ -23,8 +23,8 @@ do (incf time dt) ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. (psetf - pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) - prev-pos pos) + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) (incf vel (* acc dt)) finally (return (list time vel)))) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index 3f0182cbc..a3efa440c 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -70,7 +70,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:1-14, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:3-15, lang:"lisp"](code/clisp/verlet.lisp) +[import:3-14, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, $$\frac{1}{2}mv^2$$? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to $$\mathcal{O}(\Delta t^2)$$ accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so @@ -128,7 +128,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:16-32, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:17-30, lang:"lisp"](code/clisp/verlet.lisp) +[import:16-29, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} @@ -200,7 +200,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:34-45, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:32-41, lang:"lisp"](code/clisp/verlet.lisp) +[import:31-40, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Even though this method is more widely used than the simple Verlet method mentioned above, it unforunately has an error term of $$\mathcal{O}(\Delta t^2)$$, which is two orders of magnitude worse. That said, if you want to have a simulaton with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulatons are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from $$\sim \mathcal{O}(n^2)$$ to $$\sim \mathcal{O}(n\log(n))$$. From e7d32e873569ae542b901b189db10ad1aa5a6259 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Fri, 19 Jul 2019 11:46:52 +0200 Subject: [PATCH 08/11] the loop macro is incredible and safed a a couple of lines --- .../verlet_integration/code/clisp/verlet.lisp | 27 ++++++++----------- .../verlet_integration/verlet_integration.md | 6 ++--- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index 0a7b905f0..18df70ada 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -4,39 +4,34 @@ "Finds the time it takes for an object to hit the ground using verlet integration." (loop with prev-pos = pos - with time = 0 + for time = 0 then (incf time dt) while (> pos 0) - do (incf time dt) ;; The starting speed is assumed to be zero. - (psetf - pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) - prev-pos pos) + do (psetf + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) finally (return time))) (defun stormer-verlet (pos acc dt) "Finds the time and velocity when an object hits the ground using the stormer-verlet method." (loop with prev-pos = pos - with time = 0 - with vel = 0 + for time = 0 then (incf time dt) + for vel = 0 then (incf vel (* acc dt)) while (> pos 0) - do (incf time dt) ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. - (psetf + do (psetf pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) prev-pos pos) - (incf vel (* acc dt)) finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) "Finds the time and velocity when an object hist the ground using the velocity in calculations." (loop - with time = 0 - with vel = 0 - while (> pos 0) - do (incf time dt) - (incf pos (+ (* vel dt) (* 0.5 acc dt dt))) - (incf vel (* acc dt)) + for time = 0 then (incf time dt) + for vel = 0 then (incf vel (* acc dt)) + for p = pos then (incf p (+ (* vel dt) (* 0.5 acc dt dt))) + while (> p 0) finally (return (list time vel)))) (format T "Time for Verlet integration: ~d~%" (verlet 5 (- 10) 0.01)) diff --git a/contents/verlet_integration/verlet_integration.md b/contents/verlet_integration/verlet_integration.md index a3efa440c..6183db0ed 100644 --- a/contents/verlet_integration/verlet_integration.md +++ b/contents/verlet_integration/verlet_integration.md @@ -70,7 +70,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:1-14, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:3-14, lang:"lisp"](code/clisp/verlet.lisp) +[import:3-13, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Now, obviously this poses a problem; what if we want to calculate a term that requires velocity, like the kinetic energy, $$\frac{1}{2}mv^2$$? In this case, we certainly cannot get rid of the velocity! Well, we can find the velocity to $$\mathcal{O}(\Delta t^2)$$ accuracy by using the Stormer-Verlet method, which is the same as before, but we calculate velocity like so @@ -128,7 +128,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:16-32, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:16-29, lang:"lisp"](code/clisp/verlet.lisp) +[import:15-26, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} @@ -200,7 +200,7 @@ Unfortunately, this has not yet been implemented in LabVIEW, so here's Julia cod {% sample lang="nim" %} [import:34-45, lang:"nim"](code/nim/verlet.nim) {% sample lang="lisp" %} -[import:31-40, lang:"lisp"](code/clisp/verlet.lisp) +[import:28-35, lang:"lisp"](code/clisp/verlet.lisp) {% endmethod %} Even though this method is more widely used than the simple Verlet method mentioned above, it unforunately has an error term of $$\mathcal{O}(\Delta t^2)$$, which is two orders of magnitude worse. That said, if you want to have a simulaton with many objects that depend on one another --- like a gravity simulation --- the Velocity Verlet algorithm is a handy choice; however, you may have to play further tricks to allow everything to scale appropriately. These types of simulatons are sometimes called *n-body* simulations and one such trick is the Barnes-Hut algorithm, which cuts the complexity of n-body simulations from $$\sim \mathcal{O}(n^2)$$ to $$\sim \mathcal{O}(n\log(n))$$. From ed4371c3b62cd0b0a67abfe372b7098ea36d7803 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Sun, 8 Sep 2019 19:02:52 +0200 Subject: [PATCH 09/11] removed unneccesary brackets and redid the indentation --- .../verlet_integration/code/clisp/verlet.lisp | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index 18df70ada..2c3ace26a 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -3,43 +3,43 @@ (defun verlet (pos acc dt) "Finds the time it takes for an object to hit the ground using verlet integration." (loop - with prev-pos = pos - for time = 0 then (incf time dt) - while (> pos 0) - ;; The starting speed is assumed to be zero. - do (psetf - pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) - prev-pos pos) - finally (return time))) + with prev-pos = pos + for time = 0 then (incf time dt) + while (> pos 0) + ;; The starting speed is assumed to be zero. + do (psetf + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) + finally (return time))) (defun stormer-verlet (pos acc dt) "Finds the time and velocity when an object hits the ground using the stormer-verlet method." (loop - with prev-pos = pos - for time = 0 then (incf time dt) - for vel = 0 then (incf vel (* acc dt)) - while (> pos 0) - ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. - do (psetf - pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) - prev-pos pos) + with prev-pos = pos + for time = 0 then (incf time dt) + for vel = 0 then (incf vel (* acc dt)) + while (> pos 0) + ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. + do (psetf + pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) + prev-pos pos) finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) "Finds the time and velocity when an object hist the ground using the velocity in calculations." (loop - for time = 0 then (incf time dt) - for vel = 0 then (incf vel (* acc dt)) - for p = pos then (incf p (+ (* vel dt) (* 0.5 acc dt dt))) - while (> p 0) - finally (return (list time vel)))) + for time = 0 then (incf time dt) + for vel = 0 then (incf vel (* acc dt)) + for p = pos then (incf p (+ (* vel dt) (* 0.5 acc dt dt))) + while (> p 0) + finally (return (list time vel)))) -(format T "Time for Verlet integration: ~d~%" (verlet 5 (- 10) 0.01)) +(format T "Time for Verlet integration: ~d~%" (verlet 5 -10 0.01)) -(defvar stormer-verlet-result (stormer-verlet 5 (- 10) 0.01)) +(defvar stormer-verlet-result (stormer-verlet 5 -10 0.01)) (format T "Time for Stormer Verlet integration is: ~d~%" (first stormer-verlet-result)) (format T "Velocity for Stormer Verlet integration is: ~d~%" (second stormer-verlet-result)) -(defvar velocity-verlet-result (velocity-verlet 5 (- 10) 0.01)) +(defvar velocity-verlet-result (velocity-verlet 5 -10 0.01)) (format T "Time for velocity Verlet integration is: ~d~%" (first velocity-verlet-result)) (format T "Velocity for velocity Verlet integration is: ~d~%" (second velocity-verlet-result)) From 0d26bcf808a36c7f683b0c77ab74ebaea6d83ece Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Sun, 8 Sep 2019 19:21:03 +0200 Subject: [PATCH 10/11] redid some words --- contents/verlet_integration/code/clisp/verlet.lisp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index 2c3ace26a..d38464349 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -1,7 +1,7 @@ ;;;; Verlet integration implementation in Common Lisp (defun verlet (pos acc dt) - "Finds the time it takes for an object to hit the ground using verlet integration." + "Finds the time it takes for an object to hit the ground using Verlet integration." (loop with prev-pos = pos for time = 0 then (incf time dt) @@ -13,20 +13,20 @@ finally (return time))) (defun stormer-verlet (pos acc dt) - "Finds the time and velocity when an object hits the ground using the stormer-verlet method." + "Finds the time and velocity when an object hits the ground using the Stormer-Verlet method." (loop with prev-pos = pos for time = 0 then (incf time dt) for vel = 0 then (incf vel (* acc dt)) while (> pos 0) - ;; Variables are changed in parallel by 'psetf', so there's no need for a temporary variable. + ;; Variables are changed simultaneously by 'psetf', so there's no need for a temporary variable. do (psetf pos (+ (* pos 2) (- prev-pos) (* acc dt dt)) prev-pos pos) finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) - "Finds the time and velocity when an object hist the ground using the velocity in calculations." + "Finds the time and velocity when an object hits the ground using the velocity in calculations." (loop for time = 0 then (incf time dt) for vel = 0 then (incf vel (* acc dt)) From bc9a58ae05cec791725df1c9bd78076784283a26 Mon Sep 17 00:00:00 2001 From: Trashtalk Date: Mon, 7 Oct 2019 10:43:38 +0200 Subject: [PATCH 11/11] changed the doc strings --- contents/verlet_integration/code/clisp/verlet.lisp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/verlet_integration/code/clisp/verlet.lisp b/contents/verlet_integration/code/clisp/verlet.lisp index d38464349..726b1f1c6 100644 --- a/contents/verlet_integration/code/clisp/verlet.lisp +++ b/contents/verlet_integration/code/clisp/verlet.lisp @@ -1,7 +1,7 @@ ;;;; Verlet integration implementation in Common Lisp (defun verlet (pos acc dt) - "Finds the time it takes for an object to hit the ground using Verlet integration." + "Integrates Newton's equation for motion while pos > 0 using Verlet integration." (loop with prev-pos = pos for time = 0 then (incf time dt) @@ -13,7 +13,7 @@ finally (return time))) (defun stormer-verlet (pos acc dt) - "Finds the time and velocity when an object hits the ground using the Stormer-Verlet method." + "Integrates Newton's equation for motion while pos > 0 using the Stormer-Verlet method." (loop with prev-pos = pos for time = 0 then (incf time dt) @@ -26,7 +26,7 @@ finally (return (list time vel)))) (defun velocity-verlet (pos acc dt) - "Finds the time and velocity when an object hits the ground using the velocity in calculations." + "Integrates Newton's equation for motion while pos > 0 using the velocity in calculations." (loop for time = 0 then (incf time dt) for vel = 0 then (incf vel (* acc dt))