This is a variation on this which has bugs but might indeed ultimately be better conceived. See code here too.
Here is some Scheme code to deal with multivariate polynomials (MVPs). A polynomial can be represented as a list of the numbers which serve as the coefficients of the successive powers of the single variable. (3 5 2 8) thus represents λx.(3 + 5x + 2x2 + 8x3). Note that the variable is not named. For a polynomial in two variables, we allow each member of our list to itself be a polynomial of a single variable. Thus (3 5 (4 6) 8) represents λx.λy.(3 + 5x + (4 + 6y)x2 + 8x3). Given an ordered list of number values for the unnamed variables, an MVP may be evaluated to a number. See function ep below.
In this version an MVP is a number or a list of MVP’s. Recall Scheme’s definition of a list of zots is:
0 is a member of the ring of polynomials of a single variable. How many terms does it have? In line with omitting high order terms with 0 coefficients, we choose here to view it is having 0 terms and represent it thus: () rather than (0). Since each coefficient is a potential polynomial we code all 0’s as (). 1 + x2 is thus represented as (1 () 1). Note that 3 as a polynomial in one variable is represented the same as 3 as a polynomial in two variables. We thus conflate 3, λx.3 and λx.λy.3 — 3, (3) and ((3)) are all equivalent. (1 2) differs from ((1 2)) however for the 2 is a coefficient of different variables in the two cases.
The code in this page tests for numbers on the way down for it does not know from context how many variables there are. It sheds neither unneeded depth nor breadth yet. It sheds breadth if input is shorn.
(define (add a b) (let add ((a a)(b b)) (cond ((null? a) b) ((null? b) a) ((number? a) (if (number? b) (let ((z (+ a b))) (if (zero? z) '() z)) (cons (add a (car b)) (cdr b)))) ((number? b) (cons (add b (car a)) (cdr a))) (#t (cons (add (car a) (car b)) (add (cdr a) (cdr b))))))) ; tests (add '() '()) ; () (add '() 3) ; 3 (add 4 '()) ; 4 (add 3 5) ; 8 (add '(1 3) '(-1 -3)) ; () (add '(0 3) '((0 3))) ; ((() 3) 3) ; test rig (map (lambda (x) (equal? (add (caar x) (cdar x)) (cdr x))) (quote (((() . ()) . ()) ((() . 3) . 3) ((4 . ()) . 4) ((3 . 5) . 8) (((1 3) . (-1 -3)) . ()) (((0 3) . ((0 3))) . ((() 3) 3)) )))
; (define x (lambda (note w)(write (list note w))(newline) w)) (define (gmvp s) (let ((rn (grc4 s))) (lambda () (let gd ((b #t)) (if (and b (< 160 (rn 1))) (+ (modulo (rn 1) 10) 1) (cons (gd #t) (if (< 80 (rn 1)) '() (gd #f)))))))) ; (define g (gmvp "fundlebrat")) ; (g) (define (ep x v)(cond ((number? x) x) ((null? x) 0) (#t (if (null? v)(begin (write "Too deep")(newline))) (+ (ep (car x) (cdr v)) (* (car v) (ep (cdr x) v)))))) (define (depth p)(if (null? p) 0 (if (number? p) 0 (max (+ 1 (depth (car p))) (depth (cdr p)))))) ; (let ((a (g))) (cons a (depth a))) (let ((g (gmvp "fuyr"))(rn (grc4 "glif"))) (Do 10000 (lambda(dm) (let* ((a (g))(b (g)) (v (let gv ((k (max (depth a)(depth MVPb)))) (if (zero? k) '() (cons (+ 1 (modulo (rn 1) 9)) (gv (- k 1))))))) (let ((lf (ep (add a b) v))(rt (+ (ep a v)(ep b v)))) (if (not (= lf rt)) (begin (write (list a b v lf rt))(newline))))))))To multiply MVPs we introduce an auxiliary function, mx, that transforms w into λx.(xw), an MVP in one additional variable. Also we multiply an MVP by a number with sm.
(define (mx w)(cons '() w)) (define (sm n a) (cond ((null? a) '()) ((number? a) (* n a)) (#t (cons (sm n (car a))(sm n (cdr a)))))) (define (mul a b) (cond ((or (null? a)(null? b)) '()) ((number? a) (sm a b)) ((number? b) (sm b a)) (#t (let ((z (add (map (lambda (x) (mul (car a) x)) b) (mx (mul (cdr a) b))))) (if (> (depth z) (max (depth a)(depth b))) (begin (write (list "fizz" a b z))(newline))) z)))) (let ((g (gmvp "fayx"))(rn (grc4 "Fliw"))) (Do 1000 (lambda(dm) (let* ((a (g))(b (g)) (v (let gv ((k (max (depth a)(depth b)))) (if (zero? k) '() (cons (+ 1 (modulo (rn 1) 9)) (gv (- k 1))))))) (let ((lf (ep (mul a b) v))(rt (* (ep a v)(ep b v)))) (if (not (= lf rt)) (begin (write (list "glip" a b (mul a b) v lf rt))(newline)))))))); To generate xi
(define (gx i)(if (zero? i) '(() 1) (list (gx (- i 1)))))