This is code to find the rank of a matrix.
(rank M) returns the integer rank, r, of the matrix M. M need not be square.
(Rank M) returns a full rank submatrix of M with r columns from M.

Use definitions of trnxx and matpak, ops and inv.

(define (rank a) (call-with-current-continuation (lambda (cc)
  (let rnk ((a a)) (begin (inv a (lambda (d)
    (rnk (map (lambda (r) (let lx ((r r)(x d)(n 0))
    (if (pair? x) (if (pair? r) (if (zero? (car x))
         (cons (car r) (lx (cdr r)(cdr x)(+ n 1))) (cdr r))
       (cc n)) r))) a)))) (cc (length a)))))))

these cases:
r= (3 ..), x= (0 ..) recurse lx
r= (3 ..), x= (1 ..) omit & return r
r= (3 ..), x= () recurse rnk
r= (),     x= (1) report rank

(rank '((1 0 0)(0 1 0)(0 0 1))) ; 3
(rank '((0 0 1)(1 0 0)(0 1 0))) ; 3
(rank '((0 0 0)(1 0 0)(0 1 0))) ; 2
(rank '((0 1 0)(0 2 1)(0 0 1))) ; 2
(rank '((0 1 0)(0 0 0)(0 0 1))) ; 2
(rank '((0 1 0)(0 0 0)(0 0 0))) ; 1
(rank '((0 0 0)(0 0 0)(0 0 0))) ; 0


(rank
'((2 3    5) ; rank 2
  (5 7.5  8)
  (6 9    4)))

(rank
'((3    5) ; rank 2
 (7.5  8)
 (9    4)))
 
(define (rv m v) (begin (write (list m v)) v))
; version with print statements.
(define (rank a) (call-with-current-continuation (lambda (cc)
  (let rnk ((a a)) (write (list "fuz" a)) (newline)
    (begin (inv a (lambda (d) (write (list "d=" d)) (newline)
    (rnk (rv "mapv" (map (lambda (r) 
      (write (list "jx" d r))
    (rv "lxv" (let lx ((r r)(x d)(n 0)) (write (list "maparg" r x n))
    (if (pair? x) (if (pair? r)
       (if (zero? (car x))
         (cons (car r) (lx (cdr r)(cdr x)(+ n 1))) (cdr r))
       (cc n)) r)))) a))))) (cc (length a)))))))
 
 (define (Rank a) (call-with-current-continuation (lambda (cc)
  (let rnk ((a a)) (begin (inv a (lambda (d)
    (rnk (map (lambda (r) (let lx ((r r)(x d))
    (if (pair? x) (if (pair? r) (if (zero? (car x))
         (cons (car r) (lx (cdr r)(cdr x))) (cdr r))
       (cc a)) r))) a)))) (cc a))))))