; http://cap-lore.com/MathPhys/Field/Inter2.html
(let (
(transpose (fileVal "transpose"))
(rref (((fileVal "gRREF") zero? 0 1 / * - #t) 'rref))
(cull (lambda (n) (lambda (l)
(let px ((l l)(n n)(k 0)) (if (null? n) l (if (null? l) '()
(if (= (car n) k) (px (cdr l) (cdr n) (+ k 1))
(cons (car l) (px (cdr l) n (+ k 1)))))))))))
(letrec (
(clz (lambda (a) (if (or (null? a) (not (zero? (car a)))) 0 (+ 1 (clz (cdr a))))))
(flip (lambda (x) (if (null? x) '() (cons
(let ifl ((a (car x))) (if (null? a) '() (cons (- (car a)) (ifl (cdr a)))))
(flip (cdr x))))))
(iden (lambda (k) (if (zero? k) '() (if (= k 1) '((1)) (let ((z (iden (- k 1))))
(cons (cons 1 (cons 0 (cdar z))) (map (lambda (v) (cons 0 v)) z))))))))
(let ((dss (lambda (k) (transpose (let* ((a (map clz k))
(k (let ((l (length (car k))))
(let ly ((k k) (a a)) (if (null? k) '()
(if (= l (car a)) '() (cons (car k) (ly (cdr k)(cdr a))))))))
(b (map (cull a) k)))
(let puff ((a a)(k (flip b))(i (iden (length (car b))))(j 0))
(if (and (null? k) (null? i)) '()
(if (and (not (null? a)) (= j (car a)))
(cons (car k) (puff (cdr a) (cdr k) i (+ j 1)))
(cons (car i) (puff a k (cdr i) (+ j 1)))
))))))))
(lambda (a b) (dss (rref (append (dss (rref a)) (dss (rref b)))))))))
; tests
(define a '((2.534 4.22 -5.39 0.72003 3.42) (2.344 3.621 0.7 -4.335 5.344) (2.335 4.212 -4.3 -2.1 6.4)))
(define b '((2.354 6.432 3.22 7.6 -2.1) (-3.22 5.4 3.8 3.2 3) (2.6 -4.3 2.2 6.2 3.22)))
(define inter (fileVal "Intersect"))
(inter a b) ; => ((-0.7283600205908938 -0.8280524922929828 -0.009575249421887921 -0.7131794687048522 1))
(inter a (cons '(3 4 2 6.4 3) b))
; => ((3.785652327371516 5.09353175508096 -1.738365240680639 1 0)
; (1.9714894949452109 2.8045497786269493 -1.2493416481854884 0 1))