1

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
5

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))))
         (lead 1)
         (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)
              (setq i r lead (1+ lead))
              (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)
              (let ((j lead))
                (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)
                (let ((j lead) (c (nth lead (nth i m))))
                  (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)))))
        (setq r (1+ r) lead (1+ lead)))
      m)))

(define-key calc-mode-map (kbd "v !") #'calc-rref)

(provide 'calc-rref)
| improve this answer | |
  • Fantastic, thank you! discovering the many wonders of `calc which for me is like the vim of calculators – georg Jul 9 at 3:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.