From 29fca794b5d88671a902cff6c2cf4aef643fca23 Mon Sep 17 00:00:00 2001 From: cj12154 Date: Fri, 13 Jan 2023 12:09:31 -0500 Subject: [PATCH 1/4] add print-equations feature --- src/matrix/core.clj | 60 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/matrix/core.clj b/src/matrix/core.clj index 0fc2393..3cc91c1 100644 --- a/src/matrix/core.clj +++ b/src/matrix/core.clj @@ -134,10 +134,69 @@ ((comp println #(pretty-matrix % add-bar)) m) m)) +(defn fmt-num + "Format a number, empty string for 1" + [n] + (if (= 1 n) + "" + (str n))) + +(defn mid-sign + "Format a term as either '+ n' or '- n'" + [n] + (if (pos? n) + (str "+ " (fmt-num n)) + (str "- " (fmt-num (* -1 n))))) + +(defn single-term + [val name first] + (if (zero? val) + "" + (str (if first + (fmt-num val) + (mid-sign val)) + name)) + ) + +; could be generalized +(defn equation-from-row + "Write a pretty equation from one row of an augmented matrix" + ([row] (apply equation-from-row row)) + ([x eq] (str (single-term x "x" true) + " = " + eq)) + ([x y eq] + (let [x (single-term x "x" true) + y (single-term y "y" (empty? x))] + (str x + (if (not-empty x) " ") + y + (if (not-empty y) " ") + "= " eq))) + ([x y z eq] + (let [x (single-term x "x" true) + y (single-term y "y" (empty? x)) + z (single-term z "z" (and (empty? x) (empty? y)))] + (str x + (if (not-empty x) " ") + y + (if (not-empty y) " ") + z + (if (not-empty z) " ") + "= " eq)))) + +(defn print-matrix-equations + [m] + (do + (last (for [row m] + (println (equation-from-row row)))) + m)) + (defn swap [n m] #(swap-rows % n m)) (defn add [from to] #(add-rows % from to)) (defn add-mul [n from to] #(add-rows-with-mul % n from to)) (defn mul [n at] #(mul-row % n at)) +(def print-equations print-matrix-equations) (defn apply-input [m] @@ -156,7 +215,6 @@ (pprint-matrix m add-bar) (iterate (comp #(pprint-matrix % add-bar) apply-input) m))) - (defn -main "I don't do a whole lot ... yet." [& args] From 7c4f04564094ed1bddc776c0f1e4725cadfbd3cc Mon Sep 17 00:00:00 2001 From: cj12154 Date: Fri, 13 Jan 2023 13:51:41 -0500 Subject: [PATCH 2/4] a bunch of todos for the long weekend --- src/matrix/core.clj | 8 ++++++++ .../classes/META-INF/maven/matrix/matrix/pom.properties | 6 ++++++ .../leiningen.core.classpath.extract-native-dependencies | 1 + 3 files changed, 15 insertions(+) create mode 100644 target/default/classes/META-INF/maven/matrix/matrix/pom.properties create mode 100644 target/default/stale/leiningen.core.classpath.extract-native-dependencies diff --git a/src/matrix/core.clj b/src/matrix/core.clj index 3cc91c1..f459330 100644 --- a/src/matrix/core.clj +++ b/src/matrix/core.clj @@ -224,3 +224,11 @@ got) matrix-repl last))) + +; TODO: actual parsing of row operations +; TODO: parse equations (easy, but requires context (e.g. x = 1 could be any number of variables)) +; TODO: undoing + ; will have to have a higher level thing for storing state +; TODO: solving id-matrix +; TODO: solving gaussian-eliminated matrix +; TODO: automation diff --git a/target/default/classes/META-INF/maven/matrix/matrix/pom.properties b/target/default/classes/META-INF/maven/matrix/matrix/pom.properties new file mode 100644 index 0000000..29ccf6f --- /dev/null +++ b/target/default/classes/META-INF/maven/matrix/matrix/pom.properties @@ -0,0 +1,6 @@ +#Leiningen +#Fri Jan 13 12:10:35 EST 2023 +groupId=matrix +artifactId=matrix +version=0.1.0-SNAPSHOT +revision=29fca794b5d88671a902cff6c2cf4aef643fca23 diff --git a/target/default/stale/leiningen.core.classpath.extract-native-dependencies b/target/default/stale/leiningen.core.classpath.extract-native-dependencies new file mode 100644 index 0000000..01eaccd --- /dev/null +++ b/target/default/stale/leiningen.core.classpath.extract-native-dependencies @@ -0,0 +1 @@ +[{:dependencies {org.clojure/clojure {:vsn "1.11.1", :native-prefix nil}, org.clojure/spec.alpha {:vsn "0.3.218", :native-prefix nil}, org.clojure/core.specs.alpha {:vsn "0.2.62", :native-prefix nil}, nrepl {:vsn "0.6.0", :native-prefix nil}, clojure-complete {:vsn "0.2.5", :native-prefix nil}}, :native-path "target/default/native"} {:native-path "target/default/native", :dependencies {org.clojure/clojure {:vsn "1.11.1", :native-prefix nil, :native? false}, org.clojure/spec.alpha {:vsn "0.3.218", :native-prefix nil, :native? false}, org.clojure/core.specs.alpha {:vsn "0.2.62", :native-prefix nil, :native? false}, nrepl {:vsn "0.6.0", :native-prefix nil, :native? false}, clojure-complete {:vsn "0.2.5", :native-prefix nil, :native? false}}}] \ No newline at end of file From b8bbd083214bbf3ba0704663242ef1afa92461bf Mon Sep 17 00:00:00 2001 From: cj12154 Date: Fri, 13 Jan 2023 14:07:38 -0500 Subject: [PATCH 3/4] add an actual usage example from the homework --- README.md | 72 +++++++++++++++++++ .../maven/matrix/matrix/pom.properties | 4 +- target/default/repl-port | 1 + 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 target/default/repl-port diff --git a/README.md b/README.md index 258f7a6..45ea118 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,75 @@ clojure matrix-repl thing, the last two methods in src/matrix/core.clj are the only interesting bits src/matrix/core.clj is the only file with interesting stuff + +\*actual\* usage example: +``` +matrix.core=> (matrix-repl [[1 1 -1 -5] [5 -1 1 -1] [-1 3 -2 1]] :yeah) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 5 -1 1 │ -1 ┃ +┃ -1 3 -2 │ 1 ┃ +┗ ┛ +(add-mul 5 2 1) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 -9 │ 4 ┃ +┃ -1 3 -2 │ 1 ┃ +┗ ┛ +(add 0 2) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 -9 │ 4 ┃ +┃ 0 4 -3 │ -4 ┃ +┗ ┛ +(add-mul -4/14 1 2) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 -9 │ 4 ┃ +┃ 0 0 -3/7 │ -36/7 ┃ +┗ ┛ +(mul 7 2) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 -9 │ 4 ┃ +┃ 0 0 -3 │ -36 ┃ +┗ ┛ +(mul -1/3 2) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 -9 │ 4 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ +(add-mul 9 2 1) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 14 0 │ 112 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ +(mul 1/14 1) +┏ ┓ +┃ 1 1 -1 │ -5 ┃ +┃ 0 1 0 │ 8 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ +(add 2 0) +┏ ┓ +┃ 1 1 0 │ 7 ┃ +┃ 0 1 0 │ 8 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ +(add-mul -1 1 0) +┏ ┓ +┃ 1 0 0 │ -1 ┃ +┃ 0 1 0 │ 8 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ +print-equations +x = -1 +y = 8 +z = 12 +┏ ┓ +┃ 1 0 0 │ -1 ┃ +┃ 0 1 0 │ 8 ┃ +┃ 0 0 1 │ 12 ┃ +┗ ┛ diff --git a/target/default/classes/META-INF/maven/matrix/matrix/pom.properties b/target/default/classes/META-INF/maven/matrix/matrix/pom.properties index 29ccf6f..0c7f5a0 100644 --- a/target/default/classes/META-INF/maven/matrix/matrix/pom.properties +++ b/target/default/classes/META-INF/maven/matrix/matrix/pom.properties @@ -1,6 +1,6 @@ #Leiningen -#Fri Jan 13 12:10:35 EST 2023 +#Fri Jan 13 13:52:07 EST 2023 groupId=matrix artifactId=matrix version=0.1.0-SNAPSHOT -revision=29fca794b5d88671a902cff6c2cf4aef643fca23 +revision=7c4f04564094ed1bddc776c0f1e4725cadfbd3cc diff --git a/target/default/repl-port b/target/default/repl-port new file mode 100644 index 0000000..e45d98a --- /dev/null +++ b/target/default/repl-port @@ -0,0 +1 @@ +37245 \ No newline at end of file From f658e12e8913af871f55c148d625a451e6b6dff7 Mon Sep 17 00:00:00 2001 From: cj12154 Date: Fri, 13 Jan 2023 14:08:53 -0500 Subject: [PATCH 4/4] get rid of target directory (which really shouldn't be there) --- .../classes/META-INF/maven/matrix/matrix/pom.properties | 6 ------ target/default/repl-port | 1 - .../leiningen.core.classpath.extract-native-dependencies | 1 - 3 files changed, 8 deletions(-) delete mode 100644 target/default/classes/META-INF/maven/matrix/matrix/pom.properties delete mode 100644 target/default/repl-port delete mode 100644 target/default/stale/leiningen.core.classpath.extract-native-dependencies diff --git a/target/default/classes/META-INF/maven/matrix/matrix/pom.properties b/target/default/classes/META-INF/maven/matrix/matrix/pom.properties deleted file mode 100644 index 0c7f5a0..0000000 --- a/target/default/classes/META-INF/maven/matrix/matrix/pom.properties +++ /dev/null @@ -1,6 +0,0 @@ -#Leiningen -#Fri Jan 13 13:52:07 EST 2023 -groupId=matrix -artifactId=matrix -version=0.1.0-SNAPSHOT -revision=7c4f04564094ed1bddc776c0f1e4725cadfbd3cc diff --git a/target/default/repl-port b/target/default/repl-port deleted file mode 100644 index e45d98a..0000000 --- a/target/default/repl-port +++ /dev/null @@ -1 +0,0 @@ -37245 \ No newline at end of file diff --git a/target/default/stale/leiningen.core.classpath.extract-native-dependencies b/target/default/stale/leiningen.core.classpath.extract-native-dependencies deleted file mode 100644 index 01eaccd..0000000 --- a/target/default/stale/leiningen.core.classpath.extract-native-dependencies +++ /dev/null @@ -1 +0,0 @@ -[{:dependencies {org.clojure/clojure {:vsn "1.11.1", :native-prefix nil}, org.clojure/spec.alpha {:vsn "0.3.218", :native-prefix nil}, org.clojure/core.specs.alpha {:vsn "0.2.62", :native-prefix nil}, nrepl {:vsn "0.6.0", :native-prefix nil}, clojure-complete {:vsn "0.2.5", :native-prefix nil}}, :native-path "target/default/native"} {:native-path "target/default/native", :dependencies {org.clojure/clojure {:vsn "1.11.1", :native-prefix nil, :native? false}, org.clojure/spec.alpha {:vsn "0.3.218", :native-prefix nil, :native? false}, org.clojure/core.specs.alpha {:vsn "0.2.62", :native-prefix nil, :native? false}, nrepl {:vsn "0.6.0", :native-prefix nil, :native? false}, clojure-complete {:vsn "0.2.5", :native-prefix nil, :native? false}}}] \ No newline at end of file