finish merge ugh

This commit is contained in:
mehbark 2023-01-13 15:04:30 -05:00
commit aaf9d826c6
2 changed files with 139 additions and 2 deletions

View file

@ -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 ┃
┗ ┛

View file

@ -135,10 +135,68 @@
m))
;; repl commands
(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]
@ -157,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]
@ -167,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