# Is there a row-reduced echelon form for calc mode?

If i have the matrix

[ [1, 3, -1, -1], [1, 5, -1, -4], [-3, 6, 2, 11] ]

is there some RREF routine built in calc?

Note, i don't want to write out of the equation with variables x,y,z and then use solve `a ='.

• Not that I know of, but GNU Octave does have an `rref` routine which does exactly that. – NickD Jun 18 at 15:55
• ok, trying tik stay withiin 'calc – georg Jun 18 at 19:42

I don't think RREF is particularly useful other than for teaching linear algebra to college students, so your best hope is to find some math professor who uses Emacs and was sick of doing RREF by hand while grading homework... someone like me! Here's a little package I wrote the first time I taught linear algebra:

https://github.com/oantolin/emacs-config/blob/master/my-lisp/calc-rref.el

I think on StackExchange it is traditional to worry about the longevity of links ---"what if StackExchange outlives Microsoft!?"--- so I'll include the source here too:

``````;;; -*- lexical-binding: t; -*-
;; contents of calc-rref.el

(require 'calc-mtx)

(defun calc-rref (arg)
"Compute the reduce row echelon form of a matrix"
(interactive "P")
(calc-slow-wrapper
(calc-unary-op "rref" 'calcFunc-rref arg)))

(defun calcFunc-rref (m)
"Compute the reduce row echelon form of a matrix"
(if (math-matrixp m)
(math-with-extra-prec 2 (rref-raw m))
(math-reject-arg m 'matrixp)))

;; Algorithm from http://rosettacode.org/wiki/Reduced_row_echelon_form
(defun rref-raw (orig-m)
(let* ((m (math-copy-matrix orig-m))
(rows (1- (length m)))
(cols (1- (length (nth 1 m))))
(r 1))
(catch 'done
(while (and (<= r rows) (<= lead cols))
(let ((i r))
(while (math-zerop (nth lead (nth i m)))
(setq i (1+ i))
(when (> i rows)
(when (> lead cols) (throw 'done m))))
(setq m (math-swap-rows m i r))
(let ((pivot (nth lead (nth r m))) (i 1))
(unless (math-zerop pivot)
(while (<= j cols)
(setcar (nthcdr j (nth r m))
(math-div (nth j (nth r m)) pivot))
(setq j (1+ j)))))
(while (<= i rows)
(unless (= i r)
(while (<= j cols)
(setcar (nthcdr j (nth i m))
(math-sub (nth j (nth i m))
(math-mul c (nth j (nth r m)))))
(setq j (1+ j)))))
(setq i (1+ i)))))