pretty printing row ops in preparation for automation

This commit is contained in:
mehbark 2023-01-14 17:01:58 -05:00
parent 376ae6035e
commit 80c7eff160
4 changed files with 88 additions and 1 deletions

28
src/matrix/auto.clj Normal file
View file

@ -0,0 +1,28 @@
(ns matrix.auto
(:require [matrix.base :refer :all]))
(defn get-before-bar-row
"Get the side of a row of an augmented matrix before the bar"
[r]
(let [width (count r)
to-take (dec width)]
(into [] (take to-take r))))
(defn get-before-bar
"Get the side of an augmented matrix before the bar"
[m]
(let [width (matrix-width m)
to-take (dec width)]
(->> m
(map get-before-bar-row)
(into []))))
(defn is-id-matrix
"Determines if a matrix is *exactly* the id-matrix"
[m]
(= (id-matrix (matrix-height m))
m))
;; (defn apply-operations-with-steps
;; "Takes a matrix and a list of operations and returns a seq of matrices"
;; )

View file

@ -1,6 +1,7 @@
(ns matrix.core
(:require [matrix.base :refer :all])
(:require [matrix.interactive :refer :all])
(:require [matrix.auto :refer :all])
(:gen-class))
(defn -main

View file

@ -1,7 +1,20 @@
; TODO: actually use the render options lol, and merging provided with default
(ns matrix.render
(:require [clojure.string :as str])
(:require [matrix.base :refer :all]))
(def default-render-options
{:bar? false
:bar-char "│"
:tl-char "┏"
:tr-char "┓"
:bl-char "┗"
:br-char "┛"
:pprint-row-ops? false})
(defn surround-with
"Surround a string with the given left and right"
([left right] #(surround-with left right %))
@ -117,3 +130,48 @@
z
(if (not-empty z) " ")
"= " eq))))
(defn shrink-digit
"Takes a digit and returns the subscript version"
[d]
(case (str d)
"0" "₀"
"1" "₁"
"2" "₂"
"3" "₃"
"4" "₄"
"5" "₅"
"6" "₆"
"7" "₇"
"8" "₈"
"9" "₉"))
(defn shrink-number
"Takes a number and returns the subscript version"
[n]
(str/join (map shrink-digit (str n))))
(defn pretty-swap [op]
(str "R" (shrink-number (:a op))
" ↔ "
"R" (shrink-number (:b op))))
(defn pretty-mul [op]
(str (:n op) "R" (shrink-number (:row op))))
(defn pretty-add [op]
(let [n (if (= 1 (:n op))
""
(str (:n op)))]
(str n
"R" (shrink-number (:from op))
" + "
"R" (shrink-number (:to op)))))
(defn pretty-row-op
"Returns a pretty string of a row operation"
[op]
(cond
(:swap op) (pretty-swap op)
(:mul op) (pretty-mul op)
(:add op) (pretty-add op)))

View file

@ -1,6 +1,6 @@
(ns matrix.core-test
(:require [clojure.test :refer :all]
[matrix.core :refer :all]))
[matrix.base :refer :all]))
(deftest test-mul-row
(testing "Multiplying by one remains the same"