From 9dcaf933fb82b6de8a2c53330363cf520c6d168d Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Fri, 12 Oct 2018 00:23:45 +0800 Subject: [PATCH 01/14] Added thomas algo in js --- .../code/javascript/thomas.js | 26 +++++++++++++++++++ contents/thomas_algorithm/thomas_algorithm.md | 6 ++--- 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 contents/thomas_algorithm/code/javascript/thomas.js diff --git a/contents/thomas_algorithm/code/javascript/thomas.js b/contents/thomas_algorithm/code/javascript/thomas.js new file mode 100644 index 000000000..2b4b10fba --- /dev/null +++ b/contents/thomas_algorithm/code/javascript/thomas.js @@ -0,0 +1,26 @@ +function thomas(a, b, c, d) { + const size = a.length; + + c[0] /= b[0]; + d[0] /= b[0]; + + for(let i = 1; i < size; ++i ) { + let scale = 1 / (b[i] - c[i-1] * a[i]); + c[i] *= scale; + d[i] = (d[i] - a[i] * d[i-1]) * scale; + } + + for(let i = size-2; i>=0; --i) { + d[i] -= c[i] * d[i+1]; + } + + return d; +} + +a = [0, 2, 3]; +b = [1, 3, 6]; +c = [4, 5, 0]; +d = [7, 5, 3]; + +sol = thomas(a,b,c,d); +console.log(sol); diff --git a/contents/thomas_algorithm/thomas_algorithm.md b/contents/thomas_algorithm/thomas_algorithm.md index de0c690b1..d7319c602 100644 --- a/contents/thomas_algorithm/thomas_algorithm.md +++ b/contents/thomas_algorithm/thomas_algorithm.md @@ -1,4 +1,4 @@ -# Thomas Algorithm +s# Thomas Algorithm As alluded to in the [Gaussian Elimination chapter](../gaussian_elimination/gaussian_elimination.md), the Thomas Algorithm (or TDMA, Tri-Diagonal Matrix Algorithm) allows for programmers to **massively** cut the computational cost of their code from $$ O(n^3)$$ to $$O(n)$$ in certain cases! This is done by exploiting a particular case of Gaussian Elimination where the matrix looks like this: @@ -111,8 +111,6 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e [import, lang:"java"](code/java/thomas.java) {% sample lang="hs" %} [import, lang:"haskell"](code/haskell/thomas.hs) -{% sample lang="go" %} -[import, lang:"go"](code/golang/thomas.go) {% sample lang="swift" %} [import, lang:"swift"](code/swift/thomas.swift) {% sample lang="php" %} @@ -121,6 +119,8 @@ You will find this algorithm implemented [in this project](https://scratch.mit.e [import, lang:"nim"](code/nim/thomas_algorithm.nim) {% sample lang="cpp" %} [import, lang:"c_cpp"](code/c++/thomas.cpp) +{% sample lang="js" %} +[import, lang:"js"](code/javascript/thomas.js) {% endmethod %} + From 02f514a98e7dfc3767ac228cef4e97ef07602ac5 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Fri, 12 Oct 2018 10:01:41 +0800 Subject: [PATCH 11/14] and fixed code --- .../euclidean_algorithm/code/bash/euclid.sh | 63 ++++++++++--------- 1 file changed, 34 insertions(+), 29 deletions(-) diff --git a/contents/euclidean_algorithm/code/bash/euclid.sh b/contents/euclidean_algorithm/code/bash/euclid.sh index 58f65a540..adcdf61fc 100755 --- a/contents/euclidean_algorithm/code/bash/euclid.sh +++ b/contents/euclidean_algorithm/code/bash/euclid.sh @@ -1,38 +1,43 @@ +#!/usr/bin/env bash abs() { - local ret=$1 - if [ $ret -lt 0 ]; then - let "ret = $ret * -1" - fi - echo $ret + local ret=$1 + if [[ $ret -lt 0 ]]; then + ((ret *= -1)) + fi + printf "%s" "$ret" } euclid_mod() { - local a=$(abs $1) - local b=$(abs $2) - - while [ $b -ne 0 ]; do - let "tmp = $b" - let "b = $a % $b" - let "a = tmp" - done - echo $a + local a + local b + a=$(abs "$1") + b=$(abs "$2") + + while [[ $b -ne 0 ]]; do + ((tmp = b)) + ((b = a % b)) + ((a = tmp)) + done + printf "%s" "$a" } euclid_sub() { - local a=$(abs $1) - local b=$(abs $2) - - while [ $a -ne $b ]; do - if [ $a -gt $b ]; then - let "a -= $b" - else - let "b -= $a" - fi - done - echo $a + local a + local b + a=$(abs "$1") + b=$(abs "$2") + + while [[ $a -ne $b ]]; do + if [[ $a -gt $b ]]; then + ((a -= b)) + else + ((b -= a)) + fi + done + printf "%s" "$a" } -result=$(euclid_mod 143 693) -echo $result -result=$(euclid_sub 150 400) -echo $result +result=$(euclid_mod $((64 * 67)) $((64 * 81))) +echo "$result" +result=$(euclid_sub $((128 * 12)) $((128 * 77))) +echo "$result" From f2bdb5456f3519d303d8eda94b55caf2da07d405 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Fri, 12 Oct 2018 10:15:30 +0800 Subject: [PATCH 12/14] more pretty conditionals --- contents/euclidean_algorithm/code/bash/euclid.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/contents/euclidean_algorithm/code/bash/euclid.sh b/contents/euclidean_algorithm/code/bash/euclid.sh index adcdf61fc..4d08adc7f 100755 --- a/contents/euclidean_algorithm/code/bash/euclid.sh +++ b/contents/euclidean_algorithm/code/bash/euclid.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash abs() { local ret=$1 - if [[ $ret -lt 0 ]]; then + if (( $ret < 0 )); then ((ret *= -1)) fi printf "%s" "$ret" @@ -13,7 +13,7 @@ euclid_mod() { a=$(abs "$1") b=$(abs "$2") - while [[ $b -ne 0 ]]; do + while (( $b != 0 )); do ((tmp = b)) ((b = a % b)) ((a = tmp)) @@ -27,8 +27,8 @@ euclid_sub() { a=$(abs "$1") b=$(abs "$2") - while [[ $a -ne $b ]]; do - if [[ $a -gt $b ]]; then + while (( $a != $b )); do + if (( $a > $b )); then ((a -= b)) else ((b -= a)) From f3be8edd533ba4cfab9168412e538cb6c070fcee Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Fri, 12 Oct 2018 11:33:03 +0800 Subject: [PATCH 13/14] Updated file --- .../code/bash/{euclid.sh => euclid.bash} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename contents/euclidean_algorithm/code/bash/{euclid.sh => euclid.bash} (83%) diff --git a/contents/euclidean_algorithm/code/bash/euclid.sh b/contents/euclidean_algorithm/code/bash/euclid.bash similarity index 83% rename from contents/euclidean_algorithm/code/bash/euclid.sh rename to contents/euclidean_algorithm/code/bash/euclid.bash index 4d08adc7f..bef4b5da0 100755 --- a/contents/euclidean_algorithm/code/bash/euclid.sh +++ b/contents/euclidean_algorithm/code/bash/euclid.bash @@ -1,7 +1,7 @@ #!/usr/bin/env bash abs() { local ret=$1 - if (( $ret < 0 )); then + if (( ret < 0 )); then ((ret *= -1)) fi printf "%s" "$ret" @@ -13,7 +13,7 @@ euclid_mod() { a=$(abs "$1") b=$(abs "$2") - while (( $b != 0 )); do + while (( b != 0 )); do ((tmp = b)) ((b = a % b)) ((a = tmp)) @@ -27,8 +27,8 @@ euclid_sub() { a=$(abs "$1") b=$(abs "$2") - while (( $a != $b )); do - if (( $a > $b )); then + while (( a != b )); do + if (( a > b )); then ((a -= b)) else ((b -= a)) From 0508ea2c58ad332e7bb93216c1c8aef5e72e2e89 Mon Sep 17 00:00:00 2001 From: Ariana <1729ariana@gmail.com> Date: Fri, 12 Oct 2018 11:34:18 +0800 Subject: [PATCH 14/14] Fixed line nos --- contents/euclidean_algorithm/euclidean_algorithm.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/euclidean_algorithm/euclidean_algorithm.md b/contents/euclidean_algorithm/euclidean_algorithm.md index ed48f620d..68a34ab04 100644 --- a/contents/euclidean_algorithm/euclidean_algorithm.md +++ b/contents/euclidean_algorithm/euclidean_algorithm.md @@ -60,7 +60,7 @@ The algorithm is a simple way to find the *greatest common divisor* (GCD) of two {% sample lang="lolcode" %} [import:25-40, lang="LOLCODE"](code/lolcode/euclid.lol) {% sample lang="bash" %} -[import:10-22, lang="bash"](code/bash/euclid.bash) +[import:24-38, lang="bash"](code/bash/euclid.bash) {% endmethod %} Here, we simply line the two numbers up every step and subtract the lower value from the higher one every timestep. Once the two values are equal, we call that value the greatest common divisor. A graph of `a` and `b` as they change every step would look something like this: @@ -127,7 +127,7 @@ Modern implementations, though, often use the modulus operator (%) like so {% sample lang="lolcode" %} [import:9-23, lang="LOLCODE"](code/lolcode/euclid.lol) {% sample lang="bash" %} -[import:24-38, lang="bash"](code/bash/euclid.bash) +[import:10-22, lang="bash"](code/bash/euclid.bash) {% endmethod %} Here, we set `b` to be the remainder of `a%b` and `a` to be whatever `b` was last timestep. Because of how the modulus operator works, this will provide the same information as the subtraction-based implementation, but when we show `a` and `b` as they change with time, we can see that it might take many fewer steps: