add print-equations feature

This commit is contained in:
mehbark 2023-01-13 12:09:31 -05:00
parent 54b9f3bfed
commit 29fca794b5

View file

@ -134,10 +134,69 @@
((comp println #(pretty-matrix % add-bar)) m) ((comp println #(pretty-matrix % add-bar)) m)
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 swap [n m] #(swap-rows % n m))
(defn add [from to] #(add-rows % from to)) (defn add [from to] #(add-rows % from to))
(defn add-mul [n from to] #(add-rows-with-mul % n from to)) (defn add-mul [n from to] #(add-rows-with-mul % n from to))
(defn mul [n at] #(mul-row % n at)) (defn mul [n at] #(mul-row % n at))
(def print-equations print-matrix-equations)
(defn apply-input (defn apply-input
[m] [m]
@ -156,7 +215,6 @@
(pprint-matrix m add-bar) (pprint-matrix m add-bar)
(iterate (comp #(pprint-matrix % add-bar) apply-input) m))) (iterate (comp #(pprint-matrix % add-bar) apply-input) m)))
(defn -main (defn -main
"I don't do a whole lot ... yet." "I don't do a whole lot ... yet."
[& args] [& args]